2 * Copyright © 2014 Broadcom
3 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 #include "util/u_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"
32 #include "vc4_screen.h"
33 #include "vc4_context.h"
34 #include "vc4_resource.h"
35 #include "vc4_tiling.h"
37 static bool miptree_debug
= false;
40 vc4_resource_bo_alloc(struct vc4_resource
*rsc
)
42 struct pipe_resource
*prsc
= &rsc
->base
;
43 struct pipe_screen
*pscreen
= prsc
->screen
;
47 fprintf(stderr
, "alloc %p: size %d + offset %d -> %d\n",
50 rsc
->slices
[0].offset
,
51 rsc
->slices
[0].offset
+
53 rsc
->cube_map_stride
* (prsc
->array_size
- 1));
56 bo
= vc4_bo_alloc(vc4_screen(pscreen
),
57 rsc
->slices
[0].offset
+
59 rsc
->cube_map_stride
* (prsc
->array_size
- 1),
62 vc4_bo_unreference(&rsc
->bo
);
71 vc4_resource_transfer_unmap(struct pipe_context
*pctx
,
72 struct pipe_transfer
*ptrans
)
74 struct vc4_context
*vc4
= vc4_context(pctx
);
75 struct vc4_transfer
*trans
= vc4_transfer(ptrans
);
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];
84 rsc
= vc4_resource(ptrans
->resource
);
85 slice
= &rsc
->slices
[ptrans
->level
];
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
,
92 trans
->map
, ptrans
->stride
,
93 slice
->tiling
, rsc
->cpp
,
99 if (trans
->ss_resource
&& (ptrans
->usage
& PIPE_TRANSFER_WRITE
)) {
100 struct pipe_blit_info blit
;
101 memset(&blit
, 0, sizeof(blit
));
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;
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
;
114 blit
.mask
= util_format_get_mask(ptrans
->resource
->format
);
115 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
117 pctx
->blit(pctx
, &blit
);
119 pipe_resource_reference(&trans
->ss_resource
, NULL
);
122 pipe_resource_reference(&ptrans
->resource
, NULL
);
123 slab_free(&vc4
->transfer_pool
, ptrans
);
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
)
131 struct pipe_resource temp_setup
;
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;
141 return pctx
->screen
->resource_create(pctx
->screen
, &temp_setup
);
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
)
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
;
158 /* Upgrade DISCARD_RANGE to WHOLE_RESOURCE if the whole resource is
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
;
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.
177 if (prsc
->bind
& PIPE_BIND_VERTEX_BUFFER
)
178 vc4
->dirty
|= VC4_DIRTY_VTXBUF
;
180 /* If we failed to reallocate, flush users so that we
181 * don't violate any syncing requirements.
183 vc4_flush_jobs_reading_resource(vc4
, prsc
);
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.
190 if (usage
& PIPE_TRANSFER_WRITE
)
191 vc4_flush_jobs_reading_resource(vc4
, prsc
);
193 vc4_flush_jobs_writing_resource(vc4
, prsc
);
196 if (usage
& PIPE_TRANSFER_WRITE
) {
198 rsc
->initialized_buffers
= ~0;
201 trans
= slab_alloc(&vc4
->transfer_pool
);
205 /* XXX: Handle DONTBLOCK, DISCARD_RANGE, PERSISTENT, COHERENT. */
207 /* slab_alloc_st() doesn't zero: */
208 memset(trans
, 0, sizeof(*trans
));
209 ptrans
= &trans
->base
;
211 pipe_resource_reference(&ptrans
->resource
, prsc
);
212 ptrans
->level
= level
;
213 ptrans
->usage
= usage
;
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.
219 if (prsc
->nr_samples
> 1) {
220 trans
->ss_resource
= vc4_get_temp_resource(pctx
, prsc
, box
);
221 if (!trans
->ss_resource
)
223 assert(!trans
->ss_resource
->nr_samples
);
225 /* The ptrans->box gets modified for tile alignment, so save
226 * the original box for unmap time.
228 trans
->ss_box
= *box
;
230 if (usage
& PIPE_TRANSFER_READ
) {
231 struct pipe_blit_info blit
;
232 memset(&blit
, 0, sizeof(blit
));
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
;
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;
245 blit
.mask
= util_format_get_mask(prsc
->format
);
246 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
248 pctx
->blit(pctx
, &blit
);
249 vc4_flush_jobs_writing_resource(vc4
, blit
.dst
.resource
);
252 /* The rest of the mapping process should use our temporary. */
253 prsc
= trans
->ss_resource
;
254 rsc
= vc4_resource(prsc
);
260 /* Note that the current kernel implementation is synchronous, so no
261 * need to do syncing stuff here yet.
264 if (usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)
265 buf
= vc4_bo_map_unsynchronized(rsc
->bo
);
267 buf
= vc4_bo_map(rsc
->bo
);
269 fprintf(stderr
, "Failed to map bo\n");
275 struct vc4_resource_slice
*slice
= &rsc
->slices
[level
];
277 uint32_t utile_w
= vc4_utile_width(rsc
->cpp
);
278 uint32_t utile_h
= vc4_utile_height(rsc
->cpp
);
280 /* No direct mappings of tiled, since we need to manually
283 if (usage
& PIPE_TRANSFER_MAP_DIRECTLY
)
286 if (format
== PIPE_FORMAT_ETC1_RGB8
) {
287 /* ETC1 is arranged as 64-bit blocks, where each block
288 * is 4x4 pixels. Texture tiling operates on the
289 * 64-bit block the way it would an uncompressed
292 assert(!(ptrans
->box
.x
& 3));
293 assert(!(ptrans
->box
.y
& 3));
296 ptrans
->box
.width
= (ptrans
->box
.width
+ 3) >> 2;
297 ptrans
->box
.height
= (ptrans
->box
.height
+ 3) >> 2;
300 /* We need to align the box to utile boundaries, since that's
301 * what load/store operates on. This may cause us to need to
302 * read out the original contents in that border area. Right
303 * now we just read out the entire contents, including the
304 * middle area that will just get overwritten.
306 uint32_t box_start_x
= ptrans
->box
.x
& (utile_w
- 1);
307 uint32_t box_start_y
= ptrans
->box
.y
& (utile_h
- 1);
308 bool needs_load
= (usage
& PIPE_TRANSFER_READ
) != 0;
311 ptrans
->box
.width
+= box_start_x
;
312 ptrans
->box
.x
-= box_start_x
;
316 ptrans
->box
.height
+= box_start_y
;
317 ptrans
->box
.y
-= box_start_y
;
320 if (ptrans
->box
.width
& (utile_w
- 1)) {
321 /* We only need to force a load if our border region
322 * we're extending into is actually part of the
325 uint32_t slice_width
= u_minify(prsc
->width0
, level
);
326 if (ptrans
->box
.x
+ ptrans
->box
.width
!= slice_width
)
328 ptrans
->box
.width
= align(ptrans
->box
.width
, utile_w
);
330 if (ptrans
->box
.height
& (utile_h
- 1)) {
331 uint32_t slice_height
= u_minify(prsc
->height0
, level
);
332 if (ptrans
->box
.y
+ ptrans
->box
.height
!= slice_height
)
334 ptrans
->box
.height
= align(ptrans
->box
.height
, utile_h
);
337 ptrans
->stride
= ptrans
->box
.width
* rsc
->cpp
;
338 ptrans
->layer_stride
= ptrans
->stride
* ptrans
->box
.height
;
340 trans
->map
= malloc(ptrans
->layer_stride
* ptrans
->box
.depth
);
343 vc4_load_tiled_image(trans
->map
, ptrans
->stride
,
344 buf
+ slice
->offset
+
345 ptrans
->box
.z
* rsc
->cube_map_stride
,
347 slice
->tiling
, rsc
->cpp
,
351 box_start_x
* rsc
->cpp
+
352 box_start_y
* ptrans
->stride
);
354 ptrans
->stride
= slice
->stride
;
355 ptrans
->layer_stride
= ptrans
->stride
;
357 return buf
+ slice
->offset
+
358 ptrans
->box
.y
/ util_format_get_blockheight(format
) * ptrans
->stride
+
359 ptrans
->box
.x
/ util_format_get_blockwidth(format
) * rsc
->cpp
+
360 ptrans
->box
.z
* rsc
->cube_map_stride
;
365 vc4_resource_transfer_unmap(pctx
, ptrans
);
370 vc4_resource_destroy(struct pipe_screen
*pscreen
,
371 struct pipe_resource
*prsc
)
373 struct vc4_resource
*rsc
= vc4_resource(prsc
);
374 pipe_resource_reference(&rsc
->shadow_parent
, NULL
);
375 vc4_bo_unreference(&rsc
->bo
);
380 vc4_resource_get_handle(struct pipe_screen
*pscreen
,
381 struct pipe_context
*pctx
,
382 struct pipe_resource
*prsc
,
383 struct winsys_handle
*handle
,
386 struct vc4_resource
*rsc
= vc4_resource(prsc
);
388 return vc4_screen_bo_get_handle(pscreen
, rsc
->bo
, rsc
->slices
[0].stride
,
393 vc4_setup_slices(struct vc4_resource
*rsc
)
395 struct pipe_resource
*prsc
= &rsc
->base
;
396 uint32_t width
= prsc
->width0
;
397 uint32_t height
= prsc
->height0
;
398 if (prsc
->format
== PIPE_FORMAT_ETC1_RGB8
) {
399 width
= (width
+ 3) >> 2;
400 height
= (height
+ 3) >> 2;
403 uint32_t pot_width
= util_next_power_of_two(width
);
404 uint32_t pot_height
= util_next_power_of_two(height
);
406 uint32_t utile_w
= vc4_utile_width(rsc
->cpp
);
407 uint32_t utile_h
= vc4_utile_height(rsc
->cpp
);
409 for (int i
= prsc
->last_level
; i
>= 0; i
--) {
410 struct vc4_resource_slice
*slice
= &rsc
->slices
[i
];
412 uint32_t level_width
, level_height
;
415 level_height
= height
;
417 level_width
= u_minify(pot_width
, i
);
418 level_height
= u_minify(pot_height
, i
);
422 slice
->tiling
= VC4_TILING_FORMAT_LINEAR
;
423 if (prsc
->nr_samples
> 1) {
424 /* MSAA (4x) surfaces are stored as raw tile buffer contents. */
425 level_width
= align(level_width
, 32);
426 level_height
= align(level_height
, 32);
428 level_width
= align(level_width
, utile_w
);
431 if (vc4_size_is_lt(level_width
, level_height
,
433 slice
->tiling
= VC4_TILING_FORMAT_LT
;
434 level_width
= align(level_width
, utile_w
);
435 level_height
= align(level_height
, utile_h
);
437 slice
->tiling
= VC4_TILING_FORMAT_T
;
438 level_width
= align(level_width
,
440 level_height
= align(level_height
,
445 slice
->offset
= offset
;
446 slice
->stride
= (level_width
* rsc
->cpp
*
447 MAX2(prsc
->nr_samples
, 1));
448 slice
->size
= level_height
* slice
->stride
;
450 offset
+= slice
->size
;
453 static const char tiling_chars
[] = {
454 [VC4_TILING_FORMAT_LINEAR
] = 'R',
455 [VC4_TILING_FORMAT_LT
] = 'L',
456 [VC4_TILING_FORMAT_T
] = 'T'
459 "rsc setup %p (format %s: vc4 %d), %dx%d: "
460 "level %d (%c) -> %dx%d, stride %d@0x%08x\n",
462 util_format_short_name(prsc
->format
),
464 prsc
->width0
, prsc
->height0
,
465 i
, tiling_chars
[slice
->tiling
],
466 level_width
, level_height
,
467 slice
->stride
, slice
->offset
);
471 /* The texture base pointer that has to point to level 0 doesn't have
472 * intra-page bits, so we have to align it, and thus shift up all the
475 uint32_t page_align_offset
= (align(rsc
->slices
[0].offset
, 4096) -
476 rsc
->slices
[0].offset
);
477 if (page_align_offset
) {
478 for (int i
= 0; i
<= prsc
->last_level
; i
++)
479 rsc
->slices
[i
].offset
+= page_align_offset
;
482 /* Cube map faces appear as whole miptrees at a page-aligned offset
483 * from the first face's miptree.
485 if (prsc
->target
== PIPE_TEXTURE_CUBE
) {
486 rsc
->cube_map_stride
= align(rsc
->slices
[0].offset
+
487 rsc
->slices
[0].size
, 4096);
491 static struct vc4_resource
*
492 vc4_resource_setup(struct pipe_screen
*pscreen
,
493 const struct pipe_resource
*tmpl
)
495 struct vc4_resource
*rsc
= CALLOC_STRUCT(vc4_resource
);
498 struct pipe_resource
*prsc
= &rsc
->base
;
502 pipe_reference_init(&prsc
->reference
, 1);
503 prsc
->screen
= pscreen
;
505 if (prsc
->nr_samples
<= 1)
506 rsc
->cpp
= util_format_get_blocksize(tmpl
->format
);
508 rsc
->cpp
= sizeof(uint32_t);
515 static enum vc4_texture_data_type
516 get_resource_texture_format(struct pipe_resource
*prsc
)
518 struct vc4_resource
*rsc
= vc4_resource(prsc
);
519 uint8_t format
= vc4_get_tex_format(prsc
->format
);
522 if (prsc
->nr_samples
> 1) {
525 assert(format
== VC4_TEXTURE_TYPE_RGBA8888
);
526 return VC4_TEXTURE_TYPE_RGBA32R
;
533 struct pipe_resource
*
534 vc4_resource_create(struct pipe_screen
*pscreen
,
535 const struct pipe_resource
*tmpl
)
537 struct vc4_resource
*rsc
= vc4_resource_setup(pscreen
, tmpl
);
538 struct pipe_resource
*prsc
= &rsc
->base
;
540 /* We have to make shared be untiled, since we don't have any way to
541 * communicate metadata about tiling currently.
543 if (tmpl
->target
== PIPE_BUFFER
||
544 tmpl
->nr_samples
> 1 ||
545 (tmpl
->bind
& (PIPE_BIND_SCANOUT
|
548 PIPE_BIND_CURSOR
))) {
554 if (tmpl
->target
!= PIPE_BUFFER
)
555 rsc
->vc4_format
= get_resource_texture_format(prsc
);
557 vc4_setup_slices(rsc
);
558 if (!vc4_resource_bo_alloc(rsc
))
563 vc4_resource_destroy(pscreen
, prsc
);
567 static struct pipe_resource
*
568 vc4_resource_from_handle(struct pipe_screen
*pscreen
,
569 const struct pipe_resource
*tmpl
,
570 struct winsys_handle
*handle
,
573 struct vc4_resource
*rsc
= vc4_resource_setup(pscreen
, tmpl
);
574 struct pipe_resource
*prsc
= &rsc
->base
;
575 struct vc4_resource_slice
*slice
= &rsc
->slices
[0];
576 uint32_t expected_stride
=
577 align(prsc
->width0
, vc4_utile_width(rsc
->cpp
)) * rsc
->cpp
;
582 if (handle
->stride
!= expected_stride
) {
583 static bool warned
= false;
587 "Attempting to import %dx%d %s with "
588 "unsupported stride %d instead of %d\n",
589 prsc
->width0
, prsc
->height0
,
590 util_format_short_name(prsc
->format
),
598 rsc
->bo
= vc4_screen_bo_from_handle(pscreen
, handle
);
602 slice
->stride
= handle
->stride
;
603 slice
->tiling
= VC4_TILING_FORMAT_LINEAR
;
605 rsc
->vc4_format
= get_resource_texture_format(prsc
);
609 "rsc import %p (format %d), %dx%d: "
610 "level 0 (R) -> stride %d@0x%08x\n",
611 rsc
, rsc
->vc4_format
,
612 prsc
->width0
, prsc
->height0
,
613 slice
->stride
, slice
->offset
);
619 vc4_resource_destroy(pscreen
, prsc
);
623 static struct pipe_surface
*
624 vc4_create_surface(struct pipe_context
*pctx
,
625 struct pipe_resource
*ptex
,
626 const struct pipe_surface
*surf_tmpl
)
628 struct vc4_surface
*surface
= CALLOC_STRUCT(vc4_surface
);
629 struct vc4_resource
*rsc
= vc4_resource(ptex
);
634 assert(surf_tmpl
->u
.tex
.first_layer
== surf_tmpl
->u
.tex
.last_layer
);
636 struct pipe_surface
*psurf
= &surface
->base
;
637 unsigned level
= surf_tmpl
->u
.tex
.level
;
639 pipe_reference_init(&psurf
->reference
, 1);
640 pipe_resource_reference(&psurf
->texture
, ptex
);
642 psurf
->context
= pctx
;
643 psurf
->format
= surf_tmpl
->format
;
644 psurf
->width
= u_minify(ptex
->width0
, level
);
645 psurf
->height
= u_minify(ptex
->height0
, level
);
646 psurf
->u
.tex
.level
= level
;
647 psurf
->u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
648 psurf
->u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
649 surface
->offset
= (rsc
->slices
[level
].offset
+
650 psurf
->u
.tex
.first_layer
* rsc
->cube_map_stride
);
651 surface
->tiling
= rsc
->slices
[level
].tiling
;
653 return &surface
->base
;
657 vc4_surface_destroy(struct pipe_context
*pctx
, struct pipe_surface
*psurf
)
659 pipe_resource_reference(&psurf
->texture
, NULL
);
664 vc4_dump_surface_non_msaa(struct pipe_surface
*psurf
)
666 struct pipe_resource
*prsc
= psurf
->texture
;
667 struct vc4_resource
*rsc
= vc4_resource(prsc
);
668 uint32_t *map
= vc4_bo_map(rsc
->bo
);
669 uint32_t stride
= rsc
->slices
[0].stride
/ 4;
670 uint32_t width
= psurf
->width
;
671 uint32_t height
= psurf
->height
;
672 uint32_t chunk_w
= width
/ 79;
673 uint32_t chunk_h
= height
/ 40;
674 uint32_t found_colors
[10];
675 uint32_t num_found_colors
= 0;
677 if (rsc
->vc4_format
!= VC4_TEXTURE_TYPE_RGBA32R
) {
678 fprintf(stderr
, "%s: Unsupported format %s\n",
679 __func__
, util_format_short_name(psurf
->format
));
683 for (int by
= 0; by
< height
; by
+= chunk_h
) {
684 for (int bx
= 0; bx
< width
; bx
+= chunk_w
) {
685 int all_found_color
= -1; /* nothing found */
687 for (int y
= by
; y
< MIN2(height
, by
+ chunk_h
); y
++) {
688 for (int x
= bx
; x
< MIN2(width
, bx
+ chunk_w
); x
++) {
689 uint32_t pix
= map
[y
* stride
+ x
];
692 for (i
= 0; i
< num_found_colors
; i
++) {
693 if (pix
== found_colors
[i
])
696 if (i
== num_found_colors
&&
698 ARRAY_SIZE(found_colors
)) {
699 found_colors
[num_found_colors
++] = pix
;
702 if (i
< num_found_colors
) {
703 if (all_found_color
== -1)
705 else if (i
!= all_found_color
)
706 all_found_color
= ARRAY_SIZE(found_colors
);
710 /* If all pixels for this chunk have a consistent
711 * value, then print a character for it. Either a
712 * fixed name (particularly common for piglit tests),
713 * or a runtime-generated number.
715 if (all_found_color
>= 0 &&
716 all_found_color
< ARRAY_SIZE(found_colors
)) {
717 static const struct {
729 for (i
= 0; i
< ARRAY_SIZE(named_colors
); i
++) {
730 if (named_colors
[i
].val
==
731 found_colors
[all_found_color
]) {
732 fprintf(stderr
, "%s",
737 /* For unnamed colors, print a number and the
738 * numbers will have values printed at the
741 if (i
== ARRAY_SIZE(named_colors
)) {
742 fprintf(stderr
, "%c",
743 '0' + all_found_color
);
746 /* If there's no consistent color, print this.
748 fprintf(stderr
, ".");
751 fprintf(stderr
, "\n");
754 for (int i
= 0; i
< num_found_colors
; i
++) {
755 fprintf(stderr
, "color %d: 0x%08x\n", i
, found_colors
[i
]);
760 vc4_surface_msaa_get_sample(struct pipe_surface
*psurf
,
761 uint32_t x
, uint32_t y
, uint32_t sample
)
763 struct pipe_resource
*prsc
= psurf
->texture
;
764 struct vc4_resource
*rsc
= vc4_resource(prsc
);
765 uint32_t tile_w
= 32, tile_h
= 32;
766 uint32_t tiles_w
= DIV_ROUND_UP(psurf
->width
, 32);
768 uint32_t tile_x
= x
/ tile_w
;
769 uint32_t tile_y
= y
/ tile_h
;
770 uint32_t *tile
= (vc4_bo_map(rsc
->bo
) +
771 VC4_TILE_BUFFER_SIZE
* (tile_y
* tiles_w
+ tile_x
));
772 uint32_t subtile_x
= x
% tile_w
;
773 uint32_t subtile_y
= y
% tile_h
;
775 uint32_t quad_samples
= VC4_MAX_SAMPLES
* 4;
776 uint32_t tile_stride
= quad_samples
* tile_w
/ 2;
778 return *((uint32_t *)tile
+
779 (subtile_y
>> 1) * tile_stride
+
780 (subtile_x
>> 1) * quad_samples
+
781 ((subtile_y
& 1) << 1) +
787 vc4_dump_surface_msaa_char(struct pipe_surface
*psurf
,
788 uint32_t start_x
, uint32_t start_y
,
789 uint32_t w
, uint32_t h
)
791 bool all_same_color
= true;
792 uint32_t all_pix
= 0;
794 for (int y
= start_y
; y
< start_y
+ h
; y
++) {
795 for (int x
= start_x
; x
< start_x
+ w
; x
++) {
796 for (int s
= 0; s
< VC4_MAX_SAMPLES
; s
++) {
797 uint32_t pix
= vc4_surface_msaa_get_sample(psurf
,
800 if (x
== start_x
&& y
== start_y
)
802 else if (all_pix
!= pix
)
803 all_same_color
= false;
807 if (all_same_color
) {
808 static const struct {
820 for (i
= 0; i
< ARRAY_SIZE(named_colors
); i
++) {
821 if (named_colors
[i
].val
== all_pix
) {
822 fprintf(stderr
, "%s",
827 fprintf(stderr
, "x");
829 fprintf(stderr
, ".");
834 vc4_dump_surface_msaa(struct pipe_surface
*psurf
)
836 uint32_t tile_w
= 32, tile_h
= 32;
837 uint32_t tiles_w
= DIV_ROUND_UP(psurf
->width
, tile_w
);
838 uint32_t tiles_h
= DIV_ROUND_UP(psurf
->height
, tile_h
);
839 uint32_t char_w
= 140, char_h
= 60;
840 uint32_t char_w_per_tile
= char_w
/ tiles_w
- 1;
841 uint32_t char_h_per_tile
= char_h
/ tiles_h
- 1;
842 uint32_t found_colors
[10];
843 uint32_t num_found_colors
= 0;
845 fprintf(stderr
, "Surface: %dx%d (%dx MSAA)\n",
846 psurf
->width
, psurf
->height
, psurf
->texture
->nr_samples
);
848 for (int x
= 0; x
< (char_w_per_tile
+ 1) * tiles_w
; x
++)
849 fprintf(stderr
, "-");
850 fprintf(stderr
, "\n");
852 for (int ty
= 0; ty
< psurf
->height
; ty
+= tile_h
) {
853 for (int y
= 0; y
< char_h_per_tile
; y
++) {
855 for (int tx
= 0; tx
< psurf
->width
; tx
+= tile_w
) {
856 for (int x
= 0; x
< char_w_per_tile
; x
++) {
857 uint32_t bx1
= (x
* tile_w
/
859 uint32_t bx2
= ((x
+ 1) * tile_w
/
861 uint32_t by1
= (y
* tile_h
/
863 uint32_t by2
= ((y
+ 1) * tile_h
/
866 vc4_dump_surface_msaa_char(psurf
,
872 fprintf(stderr
, "|");
874 fprintf(stderr
, "\n");
877 for (int x
= 0; x
< (char_w_per_tile
+ 1) * tiles_w
; x
++)
878 fprintf(stderr
, "-");
879 fprintf(stderr
, "\n");
882 for (int i
= 0; i
< num_found_colors
; i
++) {
883 fprintf(stderr
, "color %d: 0x%08x\n", i
, found_colors
[i
]);
887 /** Debug routine to dump the contents of an 8888 surface to the console */
889 vc4_dump_surface(struct pipe_surface
*psurf
)
894 if (psurf
->texture
->nr_samples
> 1)
895 vc4_dump_surface_msaa(psurf
);
897 vc4_dump_surface_non_msaa(psurf
);
901 vc4_flush_resource(struct pipe_context
*pctx
, struct pipe_resource
*resource
)
903 /* All calls to flush_resource are followed by a flush of the context,
904 * so there's nothing to do.
909 vc4_update_shadow_baselevel_texture(struct pipe_context
*pctx
,
910 struct pipe_sampler_view
*view
)
912 struct vc4_resource
*shadow
= vc4_resource(view
->texture
);
913 struct vc4_resource
*orig
= vc4_resource(shadow
->shadow_parent
);
916 if (shadow
->writes
== orig
->writes
&& orig
->bo
->private)
919 perf_debug("Updating %dx%d@%d shadow texture due to %s\n",
920 orig
->base
.width0
, orig
->base
.height0
,
921 view
->u
.tex
.first_level
,
922 view
->u
.tex
.first_level
? "base level" : "raster layout");
924 for (int i
= 0; i
<= shadow
->base
.last_level
; i
++) {
925 unsigned width
= u_minify(shadow
->base
.width0
, i
);
926 unsigned height
= u_minify(shadow
->base
.height0
, i
);
927 struct pipe_blit_info info
= {
929 .resource
= &shadow
->base
,
939 .format
= shadow
->base
.format
,
942 .resource
= &orig
->base
,
943 .level
= view
->u
.tex
.first_level
+ i
,
952 .format
= orig
->base
.format
,
956 pctx
->blit(pctx
, &info
);
959 shadow
->writes
= orig
->writes
;
963 * Converts a 4-byte index buffer to 2 bytes.
965 * Since GLES2 only has support for 1 and 2-byte indices, the hardware doesn't
966 * include 4-byte index support, and we have to shrink it down.
968 * There's no fallback support for when indices end up being larger than 2^16,
969 * though it will at least assertion fail. Also, if the original index data
970 * was in user memory, it would be nice to not have uploaded it to a VBO
971 * before translating.
973 struct pipe_resource
*
974 vc4_get_shadow_index_buffer(struct pipe_context
*pctx
,
975 const struct pipe_draw_info
*info
,
978 uint32_t *shadow_offset
)
980 struct vc4_context
*vc4
= vc4_context(pctx
);
981 struct vc4_resource
*orig
= vc4_resource(info
->index
.resource
);
982 perf_debug("Fallback conversion for %d uint indices\n", count
);
985 struct pipe_resource
*shadow_rsc
= NULL
;
986 u_upload_alloc(vc4
->uploader
, 0, count
* 2, 4,
987 shadow_offset
, &shadow_rsc
, &data
);
988 uint16_t *dst
= data
;
990 struct pipe_transfer
*src_transfer
= NULL
;
992 if (info
->has_user_indices
) {
993 src
= info
->index
.user
;
995 src
= pipe_buffer_map_range(pctx
, &orig
->base
,
998 PIPE_TRANSFER_READ
, &src_transfer
);
1001 for (int i
= 0; i
< count
; i
++) {
1002 uint32_t src_index
= src
[i
];
1003 assert(src_index
<= 0xffff);
1008 pctx
->transfer_unmap(pctx
, src_transfer
);
1014 vc4_resource_screen_init(struct pipe_screen
*pscreen
)
1016 pscreen
->resource_create
= vc4_resource_create
;
1017 pscreen
->resource_from_handle
= vc4_resource_from_handle
;
1018 pscreen
->resource_destroy
= u_resource_destroy_vtbl
;
1019 pscreen
->resource_get_handle
= vc4_resource_get_handle
;
1020 pscreen
->resource_destroy
= vc4_resource_destroy
;
1024 vc4_resource_context_init(struct pipe_context
*pctx
)
1026 pctx
->transfer_map
= vc4_resource_transfer_map
;
1027 pctx
->transfer_flush_region
= u_default_transfer_flush_region
;
1028 pctx
->transfer_unmap
= vc4_resource_transfer_unmap
;
1029 pctx
->buffer_subdata
= u_default_buffer_subdata
;
1030 pctx
->texture_subdata
= u_default_texture_subdata
;
1031 pctx
->create_surface
= vc4_create_surface
;
1032 pctx
->surface_destroy
= vc4_surface_destroy
;
1033 pctx
->resource_copy_region
= util_resource_copy_region
;
1034 pctx
->blit
= vc4_blit
;
1035 pctx
->flush_resource
= vc4_flush_resource
;