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 &&
170 usage
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
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.
178 if (prsc
->bind
& PIPE_BIND_VERTEX_BUFFER
)
179 vc4
->dirty
|= VC4_DIRTY_VTXBUF
;
181 /* If we failed to reallocate, flush users so that we
182 * don't violate any syncing requirements.
184 vc4_flush_jobs_reading_resource(vc4
, prsc
);
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.
191 if (usage
& PIPE_TRANSFER_WRITE
)
192 vc4_flush_jobs_reading_resource(vc4
, prsc
);
194 vc4_flush_jobs_writing_resource(vc4
, prsc
);
197 if (usage
& PIPE_TRANSFER_WRITE
) {
199 rsc
->initialized_buffers
= ~0;
202 trans
= slab_alloc(&vc4
->transfer_pool
);
206 /* XXX: Handle DONTBLOCK, DISCARD_RANGE, PERSISTENT, COHERENT. */
208 /* slab_alloc_st() doesn't zero: */
209 memset(trans
, 0, sizeof(*trans
));
210 ptrans
= &trans
->base
;
212 pipe_resource_reference(&ptrans
->resource
, prsc
);
213 ptrans
->level
= level
;
214 ptrans
->usage
= usage
;
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.
220 if (prsc
->nr_samples
> 1) {
221 trans
->ss_resource
= vc4_get_temp_resource(pctx
, prsc
, box
);
222 if (!trans
->ss_resource
)
224 assert(!trans
->ss_resource
->nr_samples
);
226 /* The ptrans->box gets modified for tile alignment, so save
227 * the original box for unmap time.
229 trans
->ss_box
= *box
;
231 if (usage
& PIPE_TRANSFER_READ
) {
232 struct pipe_blit_info blit
;
233 memset(&blit
, 0, sizeof(blit
));
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
;
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;
246 blit
.mask
= util_format_get_mask(prsc
->format
);
247 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
249 pctx
->blit(pctx
, &blit
);
250 vc4_flush_jobs_writing_resource(vc4
, blit
.dst
.resource
);
253 /* The rest of the mapping process should use our temporary. */
254 prsc
= trans
->ss_resource
;
255 rsc
= vc4_resource(prsc
);
261 /* Note that the current kernel implementation is synchronous, so no
262 * need to do syncing stuff here yet.
265 if (usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)
266 buf
= vc4_bo_map_unsynchronized(rsc
->bo
);
268 buf
= vc4_bo_map(rsc
->bo
);
270 fprintf(stderr
, "Failed to map bo\n");
276 struct vc4_resource_slice
*slice
= &rsc
->slices
[level
];
278 uint32_t utile_w
= vc4_utile_width(rsc
->cpp
);
279 uint32_t utile_h
= vc4_utile_height(rsc
->cpp
);
281 /* No direct mappings of tiled, since we need to manually
284 if (usage
& PIPE_TRANSFER_MAP_DIRECTLY
)
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
293 assert(!(ptrans
->box
.x
& 3));
294 assert(!(ptrans
->box
.y
& 3));
297 ptrans
->box
.width
= (ptrans
->box
.width
+ 3) >> 2;
298 ptrans
->box
.height
= (ptrans
->box
.height
+ 3) >> 2;
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.
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;
312 ptrans
->box
.width
+= box_start_x
;
313 ptrans
->box
.x
-= box_start_x
;
317 ptrans
->box
.height
+= box_start_y
;
318 ptrans
->box
.y
-= box_start_y
;
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
326 uint32_t slice_width
= u_minify(prsc
->width0
, level
);
327 if (ptrans
->box
.x
+ ptrans
->box
.width
!= slice_width
)
329 ptrans
->box
.width
= align(ptrans
->box
.width
, utile_w
);
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
)
335 ptrans
->box
.height
= align(ptrans
->box
.height
, utile_h
);
338 ptrans
->stride
= ptrans
->box
.width
* rsc
->cpp
;
339 ptrans
->layer_stride
= ptrans
->stride
* ptrans
->box
.height
;
341 trans
->map
= malloc(ptrans
->layer_stride
* ptrans
->box
.depth
);
344 vc4_load_tiled_image(trans
->map
, ptrans
->stride
,
345 buf
+ slice
->offset
+
346 ptrans
->box
.z
* rsc
->cube_map_stride
,
348 slice
->tiling
, rsc
->cpp
,
352 box_start_x
* rsc
->cpp
+
353 box_start_y
* ptrans
->stride
);
355 ptrans
->stride
= slice
->stride
;
356 ptrans
->layer_stride
= ptrans
->stride
;
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
;
366 vc4_resource_transfer_unmap(pctx
, ptrans
);
371 vc4_resource_destroy(struct pipe_screen
*pscreen
,
372 struct pipe_resource
*prsc
)
374 struct vc4_resource
*rsc
= vc4_resource(prsc
);
375 pipe_resource_reference(&rsc
->shadow_parent
, NULL
);
376 vc4_bo_unreference(&rsc
->bo
);
381 vc4_resource_get_handle(struct pipe_screen
*pscreen
,
382 struct pipe_context
*pctx
,
383 struct pipe_resource
*prsc
,
384 struct winsys_handle
*whandle
,
387 struct vc4_resource
*rsc
= vc4_resource(prsc
);
389 whandle
->stride
= rsc
->slices
[0].stride
;
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).
395 rsc
->bo
->private = false;
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
;
403 case DRM_API_HANDLE_TYPE_FD
:
404 whandle
->handle
= vc4_bo_get_dmabuf(rsc
->bo
);
405 return whandle
->handle
!= -1;
412 vc4_setup_slices(struct vc4_resource
*rsc
)
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;
422 uint32_t pot_width
= util_next_power_of_two(width
);
423 uint32_t pot_height
= util_next_power_of_two(height
);
425 uint32_t utile_w
= vc4_utile_width(rsc
->cpp
);
426 uint32_t utile_h
= vc4_utile_height(rsc
->cpp
);
428 for (int i
= prsc
->last_level
; i
>= 0; i
--) {
429 struct vc4_resource_slice
*slice
= &rsc
->slices
[i
];
431 uint32_t level_width
, level_height
;
434 level_height
= height
;
436 level_width
= u_minify(pot_width
, i
);
437 level_height
= u_minify(pot_height
, i
);
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);
447 level_width
= align(level_width
, utile_w
);
450 if (vc4_size_is_lt(level_width
, level_height
,
452 slice
->tiling
= VC4_TILING_FORMAT_LT
;
453 level_width
= align(level_width
, utile_w
);
454 level_height
= align(level_height
, utile_h
);
456 slice
->tiling
= VC4_TILING_FORMAT_T
;
457 level_width
= align(level_width
,
459 level_height
= align(level_height
,
464 slice
->offset
= offset
;
465 slice
->stride
= (level_width
* rsc
->cpp
*
466 MAX2(prsc
->nr_samples
, 1));
467 slice
->size
= level_height
* slice
->stride
;
469 offset
+= slice
->size
;
472 static const char tiling_chars
[] = {
473 [VC4_TILING_FORMAT_LINEAR
] = 'R',
474 [VC4_TILING_FORMAT_LT
] = 'L',
475 [VC4_TILING_FORMAT_T
] = 'T'
478 "rsc setup %p (format %s: vc4 %d), %dx%d: "
479 "level %d (%c) -> %dx%d, stride %d@0x%08x\n",
481 util_format_short_name(prsc
->format
),
483 prsc
->width0
, prsc
->height0
,
484 i
, tiling_chars
[slice
->tiling
],
485 level_width
, level_height
,
486 slice
->stride
, slice
->offset
);
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
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
;
501 /* Cube map faces appear as whole miptrees at a page-aligned offset
502 * from the first face's miptree.
504 if (prsc
->target
== PIPE_TEXTURE_CUBE
) {
505 rsc
->cube_map_stride
= align(rsc
->slices
[0].offset
+
506 rsc
->slices
[0].size
, 4096);
510 static struct vc4_resource
*
511 vc4_resource_setup(struct pipe_screen
*pscreen
,
512 const struct pipe_resource
*tmpl
)
514 struct vc4_resource
*rsc
= CALLOC_STRUCT(vc4_resource
);
517 struct pipe_resource
*prsc
= &rsc
->base
;
521 pipe_reference_init(&prsc
->reference
, 1);
522 prsc
->screen
= pscreen
;
524 if (prsc
->nr_samples
<= 1)
525 rsc
->cpp
= util_format_get_blocksize(tmpl
->format
);
527 rsc
->cpp
= sizeof(uint32_t);
534 static enum vc4_texture_data_type
535 get_resource_texture_format(struct pipe_resource
*prsc
)
537 struct vc4_resource
*rsc
= vc4_resource(prsc
);
538 uint8_t format
= vc4_get_tex_format(prsc
->format
);
541 if (prsc
->nr_samples
> 1) {
544 assert(format
== VC4_TEXTURE_TYPE_RGBA8888
);
545 return VC4_TEXTURE_TYPE_RGBA32R
;
552 struct pipe_resource
*
553 vc4_resource_create(struct pipe_screen
*pscreen
,
554 const struct pipe_resource
*tmpl
)
556 struct vc4_resource
*rsc
= vc4_resource_setup(pscreen
, tmpl
);
557 struct pipe_resource
*prsc
= &rsc
->base
;
559 /* We have to make shared be untiled, since we don't have any way to
560 * communicate metadata about tiling currently.
562 if (tmpl
->target
== PIPE_BUFFER
||
563 tmpl
->nr_samples
> 1 ||
564 (tmpl
->bind
& (PIPE_BIND_SCANOUT
|
567 PIPE_BIND_CURSOR
))) {
573 if (tmpl
->target
!= PIPE_BUFFER
)
574 rsc
->vc4_format
= get_resource_texture_format(prsc
);
576 vc4_setup_slices(rsc
);
577 if (!vc4_resource_bo_alloc(rsc
))
582 vc4_resource_destroy(pscreen
, prsc
);
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
,
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
;
602 if (whandle
->stride
!= expected_stride
) {
603 static bool warned
= false;
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
),
619 if (whandle
->offset
!= 0) {
621 "Attempt to import unsupported winsys offset %u\n",
626 switch (whandle
->type
) {
627 case DRM_API_HANDLE_TYPE_SHARED
:
628 rsc
->bo
= vc4_bo_open_name(screen
,
629 whandle
->handle
, whandle
->stride
);
631 case DRM_API_HANDLE_TYPE_FD
:
632 rsc
->bo
= vc4_bo_open_dmabuf(screen
,
633 whandle
->handle
, whandle
->stride
);
637 "Attempt to import unsupported handle type %d\n",
644 slice
->stride
= whandle
->stride
;
645 slice
->tiling
= VC4_TILING_FORMAT_LINEAR
;
647 rsc
->vc4_format
= get_resource_texture_format(prsc
);
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
);
661 vc4_resource_destroy(pscreen
, prsc
);
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
)
670 struct vc4_surface
*surface
= CALLOC_STRUCT(vc4_surface
);
671 struct vc4_resource
*rsc
= vc4_resource(ptex
);
676 assert(surf_tmpl
->u
.tex
.first_layer
== surf_tmpl
->u
.tex
.last_layer
);
678 struct pipe_surface
*psurf
= &surface
->base
;
679 unsigned level
= surf_tmpl
->u
.tex
.level
;
681 pipe_reference_init(&psurf
->reference
, 1);
682 pipe_resource_reference(&psurf
->texture
, ptex
);
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
;
695 return &surface
->base
;
699 vc4_surface_destroy(struct pipe_context
*pctx
, struct pipe_surface
*psurf
)
701 pipe_resource_reference(&psurf
->texture
, NULL
);
706 vc4_dump_surface_non_msaa(struct pipe_surface
*psurf
)
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;
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
));
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 */
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
];
734 for (i
= 0; i
< num_found_colors
; i
++) {
735 if (pix
== found_colors
[i
])
738 if (i
== num_found_colors
&&
740 ARRAY_SIZE(found_colors
)) {
741 found_colors
[num_found_colors
++] = pix
;
744 if (i
< num_found_colors
) {
745 if (all_found_color
== -1)
747 else if (i
!= all_found_color
)
748 all_found_color
= ARRAY_SIZE(found_colors
);
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.
757 if (all_found_color
>= 0 &&
758 all_found_color
< ARRAY_SIZE(found_colors
)) {
759 static const struct {
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",
779 /* For unnamed colors, print a number and the
780 * numbers will have values printed at the
783 if (i
== ARRAY_SIZE(named_colors
)) {
784 fprintf(stderr
, "%c",
785 '0' + all_found_color
);
788 /* If there's no consistent color, print this.
790 fprintf(stderr
, ".");
793 fprintf(stderr
, "\n");
796 for (int i
= 0; i
< num_found_colors
; i
++) {
797 fprintf(stderr
, "color %d: 0x%08x\n", i
, found_colors
[i
]);
802 vc4_surface_msaa_get_sample(struct pipe_surface
*psurf
,
803 uint32_t x
, uint32_t y
, uint32_t sample
)
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);
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
;
817 uint32_t quad_samples
= VC4_MAX_SAMPLES
* 4;
818 uint32_t tile_stride
= quad_samples
* tile_w
/ 2;
820 return *((uint32_t *)tile
+
821 (subtile_y
>> 1) * tile_stride
+
822 (subtile_x
>> 1) * quad_samples
+
823 ((subtile_y
& 1) << 1) +
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
)
833 bool all_same_color
= true;
834 uint32_t all_pix
= 0;
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
,
842 if (x
== start_x
&& y
== start_y
)
844 else if (all_pix
!= pix
)
845 all_same_color
= false;
849 if (all_same_color
) {
850 static const struct {
862 for (i
= 0; i
< ARRAY_SIZE(named_colors
); i
++) {
863 if (named_colors
[i
].val
== all_pix
) {
864 fprintf(stderr
, "%s",
869 fprintf(stderr
, "x");
871 fprintf(stderr
, ".");
876 vc4_dump_surface_msaa(struct pipe_surface
*psurf
)
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;
885 fprintf(stderr
, "Surface: %dx%d (%dx MSAA)\n",
886 psurf
->width
, psurf
->height
, psurf
->texture
->nr_samples
);
888 for (int x
= 0; x
< (char_w_per_tile
+ 1) * tiles_w
; x
++)
889 fprintf(stderr
, "-");
890 fprintf(stderr
, "\n");
892 for (int ty
= 0; ty
< psurf
->height
; ty
+= tile_h
) {
893 for (int y
= 0; y
< char_h_per_tile
; y
++) {
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
/
899 uint32_t bx2
= ((x
+ 1) * tile_w
/
901 uint32_t by1
= (y
* tile_h
/
903 uint32_t by2
= ((y
+ 1) * tile_h
/
906 vc4_dump_surface_msaa_char(psurf
,
912 fprintf(stderr
, "|");
914 fprintf(stderr
, "\n");
917 for (int x
= 0; x
< (char_w_per_tile
+ 1) * tiles_w
; x
++)
918 fprintf(stderr
, "-");
919 fprintf(stderr
, "\n");
923 /** Debug routine to dump the contents of an 8888 surface to the console */
925 vc4_dump_surface(struct pipe_surface
*psurf
)
930 if (psurf
->texture
->nr_samples
> 1)
931 vc4_dump_surface_msaa(psurf
);
933 vc4_dump_surface_non_msaa(psurf
);
937 vc4_flush_resource(struct pipe_context
*pctx
, struct pipe_resource
*resource
)
939 /* All calls to flush_resource are followed by a flush of the context,
940 * so there's nothing to do.
945 vc4_update_shadow_baselevel_texture(struct pipe_context
*pctx
,
946 struct pipe_sampler_view
*view
)
948 struct vc4_resource
*shadow
= vc4_resource(view
->texture
);
949 struct vc4_resource
*orig
= vc4_resource(shadow
->shadow_parent
);
952 if (shadow
->writes
== orig
->writes
&& orig
->bo
->private)
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");
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
= {
965 .resource
= &shadow
->base
,
975 .format
= shadow
->base
.format
,
978 .resource
= &orig
->base
,
979 .level
= view
->u
.tex
.first_level
+ i
,
988 .format
= orig
->base
.format
,
992 pctx
->blit(pctx
, &info
);
995 shadow
->writes
= orig
->writes
;
999 * Converts a 4-byte index buffer to 2 bytes.
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.
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.
1009 struct pipe_resource
*
1010 vc4_get_shadow_index_buffer(struct pipe_context
*pctx
,
1011 const struct pipe_draw_info
*info
,
1014 uint32_t *shadow_offset
)
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
);
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
;
1026 struct pipe_transfer
*src_transfer
= NULL
;
1027 const uint32_t *src
;
1028 if (info
->has_user_indices
) {
1029 src
= info
->index
.user
;
1031 src
= pipe_buffer_map_range(pctx
, &orig
->base
,
1034 PIPE_TRANSFER_READ
, &src_transfer
);
1037 for (int i
= 0; i
< count
; i
++) {
1038 uint32_t src_index
= src
[i
];
1039 assert(src_index
<= 0xffff);
1044 pctx
->transfer_unmap(pctx
, src_transfer
);
1050 vc4_resource_screen_init(struct pipe_screen
*pscreen
)
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
;
1060 vc4_resource_context_init(struct pipe_context
*pctx
)
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
;