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 "drm_fourcc.h"
34 #include "vc4_screen.h"
35 #include "vc4_context.h"
36 #include "vc4_resource.h"
37 #include "vc4_tiling.h"
39 #ifndef DRM_FORMAT_MOD_INVALID
40 #define DRM_FORMAT_MOD_INVALID ((1ULL << 56) - 1)
44 vc4_resource_bo_alloc(struct vc4_resource
*rsc
)
46 struct pipe_resource
*prsc
= &rsc
->base
;
47 struct pipe_screen
*pscreen
= prsc
->screen
;
50 if (vc4_debug
& VC4_DEBUG_SURFACE
) {
51 fprintf(stderr
, "alloc %p: size %d + offset %d -> %d\n",
54 rsc
->slices
[0].offset
,
55 rsc
->slices
[0].offset
+
57 rsc
->cube_map_stride
* (prsc
->array_size
- 1));
60 bo
= vc4_bo_alloc(vc4_screen(pscreen
),
61 rsc
->slices
[0].offset
+
63 rsc
->cube_map_stride
* (prsc
->array_size
- 1),
66 vc4_bo_unreference(&rsc
->bo
);
75 vc4_resource_transfer_unmap(struct pipe_context
*pctx
,
76 struct pipe_transfer
*ptrans
)
78 struct vc4_context
*vc4
= vc4_context(pctx
);
79 struct vc4_transfer
*trans
= vc4_transfer(ptrans
);
82 struct vc4_resource
*rsc
;
83 struct vc4_resource_slice
*slice
;
84 if (trans
->ss_resource
) {
85 rsc
= vc4_resource(trans
->ss_resource
);
86 slice
= &rsc
->slices
[0];
88 rsc
= vc4_resource(ptrans
->resource
);
89 slice
= &rsc
->slices
[ptrans
->level
];
92 if (ptrans
->usage
& PIPE_TRANSFER_WRITE
) {
93 vc4_store_tiled_image(rsc
->bo
->map
+ slice
->offset
+
94 ptrans
->box
.z
* rsc
->cube_map_stride
,
96 trans
->map
, ptrans
->stride
,
97 slice
->tiling
, rsc
->cpp
,
103 if (trans
->ss_resource
&& (ptrans
->usage
& PIPE_TRANSFER_WRITE
)) {
104 struct pipe_blit_info blit
;
105 memset(&blit
, 0, sizeof(blit
));
107 blit
.src
.resource
= trans
->ss_resource
;
108 blit
.src
.format
= trans
->ss_resource
->format
;
109 blit
.src
.box
.width
= trans
->ss_box
.width
;
110 blit
.src
.box
.height
= trans
->ss_box
.height
;
111 blit
.src
.box
.depth
= 1;
113 blit
.dst
.resource
= ptrans
->resource
;
114 blit
.dst
.format
= ptrans
->resource
->format
;
115 blit
.dst
.level
= ptrans
->level
;
116 blit
.dst
.box
= trans
->ss_box
;
118 blit
.mask
= util_format_get_mask(ptrans
->resource
->format
);
119 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
121 pctx
->blit(pctx
, &blit
);
123 pipe_resource_reference(&trans
->ss_resource
, NULL
);
126 pipe_resource_reference(&ptrans
->resource
, NULL
);
127 slab_free(&vc4
->transfer_pool
, ptrans
);
130 static struct pipe_resource
*
131 vc4_get_temp_resource(struct pipe_context
*pctx
,
132 struct pipe_resource
*prsc
,
133 const struct pipe_box
*box
)
135 struct pipe_resource temp_setup
;
137 memset(&temp_setup
, 0, sizeof(temp_setup
));
138 temp_setup
.target
= prsc
->target
;
139 temp_setup
.format
= prsc
->format
;
140 temp_setup
.width0
= box
->width
;
141 temp_setup
.height0
= box
->height
;
142 temp_setup
.depth0
= 1;
143 temp_setup
.array_size
= 1;
145 return pctx
->screen
->resource_create(pctx
->screen
, &temp_setup
);
149 vc4_resource_transfer_map(struct pipe_context
*pctx
,
150 struct pipe_resource
*prsc
,
151 unsigned level
, unsigned usage
,
152 const struct pipe_box
*box
,
153 struct pipe_transfer
**pptrans
)
155 struct vc4_context
*vc4
= vc4_context(pctx
);
156 struct vc4_resource
*rsc
= vc4_resource(prsc
);
157 struct vc4_transfer
*trans
;
158 struct pipe_transfer
*ptrans
;
159 enum pipe_format format
= prsc
->format
;
162 /* Upgrade DISCARD_RANGE to WHOLE_RESOURCE if the whole resource is
165 if ((usage
& PIPE_TRANSFER_DISCARD_RANGE
) &&
166 !(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) &&
167 !(prsc
->flags
& PIPE_RESOURCE_FLAG_MAP_COHERENT
) &&
168 prsc
->last_level
== 0 &&
169 prsc
->width0
== box
->width
&&
170 prsc
->height0
== box
->height
&&
171 prsc
->depth0
== box
->depth
&&
172 prsc
->array_size
== 1 &&
174 usage
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
177 if (usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) {
178 if (vc4_resource_bo_alloc(rsc
)) {
179 /* If it might be bound as one of our vertex buffers,
180 * make sure we re-emit vertex buffer state.
182 if (prsc
->bind
& PIPE_BIND_VERTEX_BUFFER
)
183 vc4
->dirty
|= VC4_DIRTY_VTXBUF
;
185 /* If we failed to reallocate, flush users so that we
186 * don't violate any syncing requirements.
188 vc4_flush_jobs_reading_resource(vc4
, prsc
);
190 } else if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
191 /* If we're writing and the buffer is being used by the CL, we
192 * have to flush the CL first. If we're only reading, we need
193 * to flush if the CL has written our buffer.
195 if (usage
& PIPE_TRANSFER_WRITE
)
196 vc4_flush_jobs_reading_resource(vc4
, prsc
);
198 vc4_flush_jobs_writing_resource(vc4
, prsc
);
201 if (usage
& PIPE_TRANSFER_WRITE
) {
203 rsc
->initialized_buffers
= ~0;
206 trans
= slab_alloc(&vc4
->transfer_pool
);
210 /* XXX: Handle DONTBLOCK, DISCARD_RANGE, PERSISTENT, COHERENT. */
212 /* slab_alloc_st() doesn't zero: */
213 memset(trans
, 0, sizeof(*trans
));
214 ptrans
= &trans
->base
;
216 pipe_resource_reference(&ptrans
->resource
, prsc
);
217 ptrans
->level
= level
;
218 ptrans
->usage
= usage
;
221 /* If the resource is multisampled, we need to resolve to single
222 * sample. This seems like it should be handled at a higher layer.
224 if (prsc
->nr_samples
> 1) {
225 trans
->ss_resource
= vc4_get_temp_resource(pctx
, prsc
, box
);
226 if (!trans
->ss_resource
)
228 assert(!trans
->ss_resource
->nr_samples
);
230 /* The ptrans->box gets modified for tile alignment, so save
231 * the original box for unmap time.
233 trans
->ss_box
= *box
;
235 if (usage
& PIPE_TRANSFER_READ
) {
236 struct pipe_blit_info blit
;
237 memset(&blit
, 0, sizeof(blit
));
239 blit
.src
.resource
= ptrans
->resource
;
240 blit
.src
.format
= ptrans
->resource
->format
;
241 blit
.src
.level
= ptrans
->level
;
242 blit
.src
.box
= trans
->ss_box
;
244 blit
.dst
.resource
= trans
->ss_resource
;
245 blit
.dst
.format
= trans
->ss_resource
->format
;
246 blit
.dst
.box
.width
= trans
->ss_box
.width
;
247 blit
.dst
.box
.height
= trans
->ss_box
.height
;
248 blit
.dst
.box
.depth
= 1;
250 blit
.mask
= util_format_get_mask(prsc
->format
);
251 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
253 pctx
->blit(pctx
, &blit
);
254 vc4_flush_jobs_writing_resource(vc4
, blit
.dst
.resource
);
257 /* The rest of the mapping process should use our temporary. */
258 prsc
= trans
->ss_resource
;
259 rsc
= vc4_resource(prsc
);
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_screen
*screen
= vc4_screen(pscreen
);
375 struct vc4_resource
*rsc
= vc4_resource(prsc
);
376 pipe_resource_reference(&rsc
->shadow_parent
, NULL
);
377 vc4_bo_unreference(&rsc
->bo
);
380 renderonly_scanout_destroy(rsc
->scanout
, screen
->ro
);
386 vc4_resource_get_handle(struct pipe_screen
*pscreen
,
387 struct pipe_context
*pctx
,
388 struct pipe_resource
*prsc
,
389 struct winsys_handle
*whandle
,
392 struct vc4_screen
*screen
= vc4_screen(pscreen
);
393 struct vc4_resource
*rsc
= vc4_resource(prsc
);
395 whandle
->stride
= rsc
->slices
[0].stride
;
398 /* If we're passing some reference to our BO out to some other part of
399 * the system, then we can't do any optimizations about only us being
400 * the ones seeing it (like BO caching or shadow update avoidance).
402 rsc
->bo
->private = false;
405 whandle
->modifier
= DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED
;
407 whandle
->modifier
= DRM_FORMAT_MOD_LINEAR
;
409 switch (whandle
->type
) {
410 case DRM_API_HANDLE_TYPE_SHARED
:
412 /* This could probably be supported, assuming that a
413 * control node was used for pl111.
415 fprintf(stderr
, "flink unsupported with pl111\n");
419 return vc4_bo_flink(rsc
->bo
, &whandle
->handle
);
420 case DRM_API_HANDLE_TYPE_KMS
:
421 if (screen
->ro
&& renderonly_get_handle(rsc
->scanout
, whandle
))
423 whandle
->handle
= rsc
->bo
->handle
;
425 case DRM_API_HANDLE_TYPE_FD
:
426 /* FDs are cross-device, so we can export directly from vc4.
428 whandle
->handle
= vc4_bo_get_dmabuf(rsc
->bo
);
429 return whandle
->handle
!= -1;
436 vc4_setup_slices(struct vc4_resource
*rsc
, const char *caller
)
438 struct pipe_resource
*prsc
= &rsc
->base
;
439 uint32_t width
= prsc
->width0
;
440 uint32_t height
= prsc
->height0
;
441 if (prsc
->format
== PIPE_FORMAT_ETC1_RGB8
) {
442 width
= (width
+ 3) >> 2;
443 height
= (height
+ 3) >> 2;
446 uint32_t pot_width
= util_next_power_of_two(width
);
447 uint32_t pot_height
= util_next_power_of_two(height
);
449 uint32_t utile_w
= vc4_utile_width(rsc
->cpp
);
450 uint32_t utile_h
= vc4_utile_height(rsc
->cpp
);
452 for (int i
= prsc
->last_level
; i
>= 0; i
--) {
453 struct vc4_resource_slice
*slice
= &rsc
->slices
[i
];
455 uint32_t level_width
, level_height
;
458 level_height
= height
;
460 level_width
= u_minify(pot_width
, i
);
461 level_height
= u_minify(pot_height
, i
);
465 slice
->tiling
= VC4_TILING_FORMAT_LINEAR
;
466 if (prsc
->nr_samples
> 1) {
467 /* MSAA (4x) surfaces are stored as raw tile buffer contents. */
468 level_width
= align(level_width
, 32);
469 level_height
= align(level_height
, 32);
471 level_width
= align(level_width
, utile_w
);
474 if (vc4_size_is_lt(level_width
, level_height
,
476 slice
->tiling
= VC4_TILING_FORMAT_LT
;
477 level_width
= align(level_width
, utile_w
);
478 level_height
= align(level_height
, utile_h
);
480 slice
->tiling
= VC4_TILING_FORMAT_T
;
481 level_width
= align(level_width
,
483 level_height
= align(level_height
,
488 slice
->offset
= offset
;
489 slice
->stride
= (level_width
* rsc
->cpp
*
490 MAX2(prsc
->nr_samples
, 1));
491 slice
->size
= level_height
* slice
->stride
;
493 offset
+= slice
->size
;
495 if (vc4_debug
& VC4_DEBUG_SURFACE
) {
496 static const char tiling_chars
[] = {
497 [VC4_TILING_FORMAT_LINEAR
] = 'R',
498 [VC4_TILING_FORMAT_LT
] = 'L',
499 [VC4_TILING_FORMAT_T
] = 'T'
502 "rsc %s %p (format %s: vc4 %d), %dx%d: "
503 "level %d (%c) -> %dx%d, stride %d@0x%08x\n",
505 util_format_short_name(prsc
->format
),
507 prsc
->width0
, prsc
->height0
,
508 i
, tiling_chars
[slice
->tiling
],
509 level_width
, level_height
,
510 slice
->stride
, slice
->offset
);
514 /* The texture base pointer that has to point to level 0 doesn't have
515 * intra-page bits, so we have to align it, and thus shift up all the
518 uint32_t page_align_offset
= (align(rsc
->slices
[0].offset
, 4096) -
519 rsc
->slices
[0].offset
);
520 if (page_align_offset
) {
521 for (int i
= 0; i
<= prsc
->last_level
; i
++)
522 rsc
->slices
[i
].offset
+= page_align_offset
;
525 /* Cube map faces appear as whole miptrees at a page-aligned offset
526 * from the first face's miptree.
528 if (prsc
->target
== PIPE_TEXTURE_CUBE
) {
529 rsc
->cube_map_stride
= align(rsc
->slices
[0].offset
+
530 rsc
->slices
[0].size
, 4096);
534 static struct vc4_resource
*
535 vc4_resource_setup(struct pipe_screen
*pscreen
,
536 const struct pipe_resource
*tmpl
)
538 struct vc4_resource
*rsc
= CALLOC_STRUCT(vc4_resource
);
541 struct pipe_resource
*prsc
= &rsc
->base
;
545 pipe_reference_init(&prsc
->reference
, 1);
546 prsc
->screen
= pscreen
;
548 if (prsc
->nr_samples
<= 1)
549 rsc
->cpp
= util_format_get_blocksize(tmpl
->format
);
551 rsc
->cpp
= sizeof(uint32_t);
558 static enum vc4_texture_data_type
559 get_resource_texture_format(struct pipe_resource
*prsc
)
561 struct vc4_resource
*rsc
= vc4_resource(prsc
);
562 uint8_t format
= vc4_get_tex_format(prsc
->format
);
565 if (prsc
->nr_samples
> 1) {
568 assert(format
== VC4_TEXTURE_TYPE_RGBA8888
);
569 return VC4_TEXTURE_TYPE_RGBA32R
;
577 find_modifier(uint64_t needle
, const uint64_t *haystack
, int count
)
581 for (i
= 0; i
< count
; i
++) {
582 if (haystack
[i
] == needle
)
589 static struct pipe_resource
*
590 vc4_resource_create_with_modifiers(struct pipe_screen
*pscreen
,
591 const struct pipe_resource
*tmpl
,
592 const uint64_t *modifiers
,
595 struct vc4_screen
*screen
= vc4_screen(pscreen
);
596 struct vc4_resource
*rsc
= vc4_resource_setup(pscreen
, tmpl
);
597 struct pipe_resource
*prsc
= &rsc
->base
;
598 bool linear_ok
= find_modifier(DRM_FORMAT_MOD_LINEAR
, modifiers
, count
);
599 /* Use a tiled layout if we can, for better 3D performance. */
600 bool should_tile
= true;
602 /* VBOs/PBOs are untiled (and 1 height). */
603 if (tmpl
->target
== PIPE_BUFFER
)
606 /* MSAA buffers are linear. */
607 if (tmpl
->nr_samples
> 1)
610 /* No tiling when we're sharing with another device (pl111). */
611 if (screen
->ro
&& (tmpl
->bind
& PIPE_BIND_SCANOUT
))
614 /* Cursors are always linear, and the user can request linear as well.
616 if (tmpl
->bind
& (PIPE_BIND_LINEAR
| PIPE_BIND_CURSOR
))
619 /* No shared objects with LT format -- the kernel only has T-format
620 * metadata. LT objects are small enough it's not worth the trouble to
621 * give them metadata to tile.
623 if ((tmpl
->bind
& (PIPE_BIND_SHARED
| PIPE_BIND_SCANOUT
)) &&
624 vc4_size_is_lt(prsc
->width0
, prsc
->height0
, rsc
->cpp
))
627 /* If we're sharing or scanning out, we need the ioctl present to
628 * inform the kernel or the other side.
630 if ((tmpl
->bind
& (PIPE_BIND_SHARED
|
631 PIPE_BIND_SCANOUT
)) && !screen
->has_tiling_ioctl
)
634 /* No user-specified modifier; determine our own. */
635 if (count
== 1 && modifiers
[0] == DRM_FORMAT_MOD_INVALID
) {
637 rsc
->tiled
= should_tile
;
638 } else if (should_tile
&&
639 find_modifier(DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED
,
642 } else if (linear_ok
) {
645 fprintf(stderr
, "Unsupported modifier requested\n");
649 if (tmpl
->target
!= PIPE_BUFFER
)
650 rsc
->vc4_format
= get_resource_texture_format(prsc
);
652 vc4_setup_slices(rsc
, "create");
653 if (!vc4_resource_bo_alloc(rsc
))
656 if (screen
->has_tiling_ioctl
) {
659 modifier
= DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED
;
661 modifier
= DRM_FORMAT_MOD_LINEAR
;
662 struct drm_vc4_set_tiling set_tiling
= {
663 .handle
= rsc
->bo
->handle
,
664 .modifier
= modifier
,
666 int ret
= vc4_ioctl(screen
->fd
, DRM_IOCTL_VC4_SET_TILING
,
672 if (screen
->ro
&& tmpl
->bind
& PIPE_BIND_SCANOUT
) {
674 renderonly_scanout_for_resource(prsc
, screen
->ro
, NULL
);
681 vc4_resource_destroy(pscreen
, prsc
);
685 struct pipe_resource
*
686 vc4_resource_create(struct pipe_screen
*pscreen
,
687 const struct pipe_resource
*tmpl
)
689 const uint64_t mod
= DRM_FORMAT_MOD_INVALID
;
690 return vc4_resource_create_with_modifiers(pscreen
, tmpl
, &mod
, 1);
693 static struct pipe_resource
*
694 vc4_resource_from_handle(struct pipe_screen
*pscreen
,
695 const struct pipe_resource
*tmpl
,
696 struct winsys_handle
*whandle
,
699 struct vc4_screen
*screen
= vc4_screen(pscreen
);
700 struct vc4_resource
*rsc
= vc4_resource_setup(pscreen
, tmpl
);
701 struct pipe_resource
*prsc
= &rsc
->base
;
702 struct vc4_resource_slice
*slice
= &rsc
->slices
[0];
707 if (whandle
->offset
!= 0) {
709 "Attempt to import unsupported winsys offset %u\n",
714 switch (whandle
->type
) {
715 case DRM_API_HANDLE_TYPE_SHARED
:
716 rsc
->bo
= vc4_bo_open_name(screen
,
717 whandle
->handle
, whandle
->stride
);
719 case DRM_API_HANDLE_TYPE_FD
:
720 rsc
->bo
= vc4_bo_open_dmabuf(screen
,
721 whandle
->handle
, whandle
->stride
);
725 "Attempt to import unsupported handle type %d\n",
732 struct drm_vc4_get_tiling get_tiling
= {
733 .handle
= rsc
->bo
->handle
,
735 int ret
= vc4_ioctl(screen
->fd
, DRM_IOCTL_VC4_GET_TILING
, &get_tiling
);
738 whandle
->modifier
= DRM_FORMAT_MOD_LINEAR
;
739 } else if (whandle
->modifier
== DRM_FORMAT_MOD_INVALID
) {
740 whandle
->modifier
= get_tiling
.modifier
;
741 } else if (whandle
->modifier
!= get_tiling
.modifier
) {
743 "Modifier 0x%llx vs. tiling (0x%llx) mismatch\n",
744 (long long)whandle
->modifier
, get_tiling
.modifier
);
748 switch (whandle
->modifier
) {
749 case DRM_FORMAT_MOD_LINEAR
:
752 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED
:
757 "Attempt to import unsupported modifier 0x%llx\n",
758 (long long)whandle
->modifier
);
762 rsc
->vc4_format
= get_resource_texture_format(prsc
);
763 vc4_setup_slices(rsc
, "import");
766 /* Make sure that renderonly has a handle to our buffer in the
767 * display's fd, so that a later renderonly_get_handle()
768 * returns correct handles or GEM names.
771 renderonly_create_gpu_import_for_resource(prsc
,
778 if (whandle
->stride
!= slice
->stride
) {
779 static bool warned
= false;
783 "Attempting to import %dx%d %s with "
784 "unsupported stride %d instead of %d\n",
785 prsc
->width0
, prsc
->height0
,
786 util_format_short_name(prsc
->format
),
796 vc4_resource_destroy(pscreen
, prsc
);
800 static struct pipe_surface
*
801 vc4_create_surface(struct pipe_context
*pctx
,
802 struct pipe_resource
*ptex
,
803 const struct pipe_surface
*surf_tmpl
)
805 struct vc4_surface
*surface
= CALLOC_STRUCT(vc4_surface
);
806 struct vc4_resource
*rsc
= vc4_resource(ptex
);
811 assert(surf_tmpl
->u
.tex
.first_layer
== surf_tmpl
->u
.tex
.last_layer
);
813 struct pipe_surface
*psurf
= &surface
->base
;
814 unsigned level
= surf_tmpl
->u
.tex
.level
;
816 pipe_reference_init(&psurf
->reference
, 1);
817 pipe_resource_reference(&psurf
->texture
, ptex
);
819 psurf
->context
= pctx
;
820 psurf
->format
= surf_tmpl
->format
;
821 psurf
->width
= u_minify(ptex
->width0
, level
);
822 psurf
->height
= u_minify(ptex
->height0
, level
);
823 psurf
->u
.tex
.level
= level
;
824 psurf
->u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
825 psurf
->u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
826 surface
->offset
= (rsc
->slices
[level
].offset
+
827 psurf
->u
.tex
.first_layer
* rsc
->cube_map_stride
);
828 surface
->tiling
= rsc
->slices
[level
].tiling
;
830 return &surface
->base
;
834 vc4_surface_destroy(struct pipe_context
*pctx
, struct pipe_surface
*psurf
)
836 pipe_resource_reference(&psurf
->texture
, NULL
);
841 vc4_dump_surface_non_msaa(struct pipe_surface
*psurf
)
843 struct pipe_resource
*prsc
= psurf
->texture
;
844 struct vc4_resource
*rsc
= vc4_resource(prsc
);
845 uint32_t *map
= vc4_bo_map(rsc
->bo
);
846 uint32_t stride
= rsc
->slices
[0].stride
/ 4;
847 uint32_t width
= psurf
->width
;
848 uint32_t height
= psurf
->height
;
849 uint32_t chunk_w
= width
/ 79;
850 uint32_t chunk_h
= height
/ 40;
851 uint32_t found_colors
[10];
852 uint32_t num_found_colors
= 0;
854 if (rsc
->vc4_format
!= VC4_TEXTURE_TYPE_RGBA32R
) {
855 fprintf(stderr
, "%s: Unsupported format %s\n",
856 __func__
, util_format_short_name(psurf
->format
));
860 for (int by
= 0; by
< height
; by
+= chunk_h
) {
861 for (int bx
= 0; bx
< width
; bx
+= chunk_w
) {
862 int all_found_color
= -1; /* nothing found */
864 for (int y
= by
; y
< MIN2(height
, by
+ chunk_h
); y
++) {
865 for (int x
= bx
; x
< MIN2(width
, bx
+ chunk_w
); x
++) {
866 uint32_t pix
= map
[y
* stride
+ x
];
869 for (i
= 0; i
< num_found_colors
; i
++) {
870 if (pix
== found_colors
[i
])
873 if (i
== num_found_colors
&&
875 ARRAY_SIZE(found_colors
)) {
876 found_colors
[num_found_colors
++] = pix
;
879 if (i
< num_found_colors
) {
880 if (all_found_color
== -1)
882 else if (i
!= all_found_color
)
883 all_found_color
= ARRAY_SIZE(found_colors
);
887 /* If all pixels for this chunk have a consistent
888 * value, then print a character for it. Either a
889 * fixed name (particularly common for piglit tests),
890 * or a runtime-generated number.
892 if (all_found_color
>= 0 &&
893 all_found_color
< ARRAY_SIZE(found_colors
)) {
894 static const struct {
906 for (i
= 0; i
< ARRAY_SIZE(named_colors
); i
++) {
907 if (named_colors
[i
].val
==
908 found_colors
[all_found_color
]) {
909 fprintf(stderr
, "%s",
914 /* For unnamed colors, print a number and the
915 * numbers will have values printed at the
918 if (i
== ARRAY_SIZE(named_colors
)) {
919 fprintf(stderr
, "%c",
920 '0' + all_found_color
);
923 /* If there's no consistent color, print this.
925 fprintf(stderr
, ".");
928 fprintf(stderr
, "\n");
931 for (int i
= 0; i
< num_found_colors
; i
++) {
932 fprintf(stderr
, "color %d: 0x%08x\n", i
, found_colors
[i
]);
937 vc4_surface_msaa_get_sample(struct pipe_surface
*psurf
,
938 uint32_t x
, uint32_t y
, uint32_t sample
)
940 struct pipe_resource
*prsc
= psurf
->texture
;
941 struct vc4_resource
*rsc
= vc4_resource(prsc
);
942 uint32_t tile_w
= 32, tile_h
= 32;
943 uint32_t tiles_w
= DIV_ROUND_UP(psurf
->width
, 32);
945 uint32_t tile_x
= x
/ tile_w
;
946 uint32_t tile_y
= y
/ tile_h
;
947 uint32_t *tile
= (vc4_bo_map(rsc
->bo
) +
948 VC4_TILE_BUFFER_SIZE
* (tile_y
* tiles_w
+ tile_x
));
949 uint32_t subtile_x
= x
% tile_w
;
950 uint32_t subtile_y
= y
% tile_h
;
952 uint32_t quad_samples
= VC4_MAX_SAMPLES
* 4;
953 uint32_t tile_stride
= quad_samples
* tile_w
/ 2;
955 return *((uint32_t *)tile
+
956 (subtile_y
>> 1) * tile_stride
+
957 (subtile_x
>> 1) * quad_samples
+
958 ((subtile_y
& 1) << 1) +
964 vc4_dump_surface_msaa_char(struct pipe_surface
*psurf
,
965 uint32_t start_x
, uint32_t start_y
,
966 uint32_t w
, uint32_t h
)
968 bool all_same_color
= true;
969 uint32_t all_pix
= 0;
971 for (int y
= start_y
; y
< start_y
+ h
; y
++) {
972 for (int x
= start_x
; x
< start_x
+ w
; x
++) {
973 for (int s
= 0; s
< VC4_MAX_SAMPLES
; s
++) {
974 uint32_t pix
= vc4_surface_msaa_get_sample(psurf
,
977 if (x
== start_x
&& y
== start_y
)
979 else if (all_pix
!= pix
)
980 all_same_color
= false;
984 if (all_same_color
) {
985 static const struct {
997 for (i
= 0; i
< ARRAY_SIZE(named_colors
); i
++) {
998 if (named_colors
[i
].val
== all_pix
) {
999 fprintf(stderr
, "%s",
1004 fprintf(stderr
, "x");
1006 fprintf(stderr
, ".");
1011 vc4_dump_surface_msaa(struct pipe_surface
*psurf
)
1013 uint32_t tile_w
= 32, tile_h
= 32;
1014 uint32_t tiles_w
= DIV_ROUND_UP(psurf
->width
, tile_w
);
1015 uint32_t tiles_h
= DIV_ROUND_UP(psurf
->height
, tile_h
);
1016 uint32_t char_w
= 140, char_h
= 60;
1017 uint32_t char_w_per_tile
= char_w
/ tiles_w
- 1;
1018 uint32_t char_h_per_tile
= char_h
/ tiles_h
- 1;
1020 fprintf(stderr
, "Surface: %dx%d (%dx MSAA)\n",
1021 psurf
->width
, psurf
->height
, psurf
->texture
->nr_samples
);
1023 for (int x
= 0; x
< (char_w_per_tile
+ 1) * tiles_w
; x
++)
1024 fprintf(stderr
, "-");
1025 fprintf(stderr
, "\n");
1027 for (int ty
= 0; ty
< psurf
->height
; ty
+= tile_h
) {
1028 for (int y
= 0; y
< char_h_per_tile
; y
++) {
1030 for (int tx
= 0; tx
< psurf
->width
; tx
+= tile_w
) {
1031 for (int x
= 0; x
< char_w_per_tile
; x
++) {
1032 uint32_t bx1
= (x
* tile_w
/
1034 uint32_t bx2
= ((x
+ 1) * tile_w
/
1036 uint32_t by1
= (y
* tile_h
/
1038 uint32_t by2
= ((y
+ 1) * tile_h
/
1041 vc4_dump_surface_msaa_char(psurf
,
1047 fprintf(stderr
, "|");
1049 fprintf(stderr
, "\n");
1052 for (int x
= 0; x
< (char_w_per_tile
+ 1) * tiles_w
; x
++)
1053 fprintf(stderr
, "-");
1054 fprintf(stderr
, "\n");
1058 /** Debug routine to dump the contents of an 8888 surface to the console */
1060 vc4_dump_surface(struct pipe_surface
*psurf
)
1065 if (psurf
->texture
->nr_samples
> 1)
1066 vc4_dump_surface_msaa(psurf
);
1068 vc4_dump_surface_non_msaa(psurf
);
1072 vc4_flush_resource(struct pipe_context
*pctx
, struct pipe_resource
*resource
)
1074 /* All calls to flush_resource are followed by a flush of the context,
1075 * so there's nothing to do.
1080 vc4_update_shadow_baselevel_texture(struct pipe_context
*pctx
,
1081 struct pipe_sampler_view
*view
)
1083 struct vc4_resource
*shadow
= vc4_resource(view
->texture
);
1084 struct vc4_resource
*orig
= vc4_resource(shadow
->shadow_parent
);
1087 if (shadow
->writes
== orig
->writes
&& orig
->bo
->private)
1090 perf_debug("Updating %dx%d@%d shadow texture due to %s\n",
1091 orig
->base
.width0
, orig
->base
.height0
,
1092 view
->u
.tex
.first_level
,
1093 view
->u
.tex
.first_level
? "base level" : "raster layout");
1095 for (int i
= 0; i
<= shadow
->base
.last_level
; i
++) {
1096 unsigned width
= u_minify(shadow
->base
.width0
, i
);
1097 unsigned height
= u_minify(shadow
->base
.height0
, i
);
1098 struct pipe_blit_info info
= {
1100 .resource
= &shadow
->base
,
1110 .format
= shadow
->base
.format
,
1113 .resource
= &orig
->base
,
1114 .level
= view
->u
.tex
.first_level
+ i
,
1123 .format
= orig
->base
.format
,
1127 pctx
->blit(pctx
, &info
);
1130 shadow
->writes
= orig
->writes
;
1134 * Converts a 4-byte index buffer to 2 bytes.
1136 * Since GLES2 only has support for 1 and 2-byte indices, the hardware doesn't
1137 * include 4-byte index support, and we have to shrink it down.
1139 * There's no fallback support for when indices end up being larger than 2^16,
1140 * though it will at least assertion fail. Also, if the original index data
1141 * was in user memory, it would be nice to not have uploaded it to a VBO
1142 * before translating.
1144 struct pipe_resource
*
1145 vc4_get_shadow_index_buffer(struct pipe_context
*pctx
,
1146 const struct pipe_draw_info
*info
,
1149 uint32_t *shadow_offset
)
1151 struct vc4_context
*vc4
= vc4_context(pctx
);
1152 struct vc4_resource
*orig
= vc4_resource(info
->index
.resource
);
1153 perf_debug("Fallback conversion for %d uint indices\n", count
);
1156 struct pipe_resource
*shadow_rsc
= NULL
;
1157 u_upload_alloc(vc4
->uploader
, 0, count
* 2, 4,
1158 shadow_offset
, &shadow_rsc
, &data
);
1159 uint16_t *dst
= data
;
1161 struct pipe_transfer
*src_transfer
= NULL
;
1162 const uint32_t *src
;
1163 if (info
->has_user_indices
) {
1164 src
= info
->index
.user
;
1166 src
= pipe_buffer_map_range(pctx
, &orig
->base
,
1169 PIPE_TRANSFER_READ
, &src_transfer
);
1172 for (int i
= 0; i
< count
; i
++) {
1173 uint32_t src_index
= src
[i
];
1174 assert(src_index
<= 0xffff);
1179 pctx
->transfer_unmap(pctx
, src_transfer
);
1185 vc4_resource_screen_init(struct pipe_screen
*pscreen
)
1187 struct vc4_screen
*screen
= vc4_screen(pscreen
);
1189 pscreen
->resource_create
= vc4_resource_create
;
1190 pscreen
->resource_create_with_modifiers
=
1191 vc4_resource_create_with_modifiers
;
1192 pscreen
->resource_from_handle
= vc4_resource_from_handle
;
1193 pscreen
->resource_destroy
= u_resource_destroy_vtbl
;
1194 pscreen
->resource_get_handle
= vc4_resource_get_handle
;
1195 pscreen
->resource_destroy
= vc4_resource_destroy
;
1197 /* Test if the kernel has GET_TILING; it will return -EINVAL if the
1198 * ioctl does not exist, but -ENOENT if we pass an impossible handle.
1199 * 0 cannot be a valid GEM object, so use that.
1201 struct drm_vc4_get_tiling get_tiling
= {
1204 int ret
= vc4_ioctl(screen
->fd
, DRM_IOCTL_VC4_GET_TILING
, &get_tiling
);
1205 if (ret
== -1 && errno
== ENOENT
)
1206 screen
->has_tiling_ioctl
= true;
1210 vc4_resource_context_init(struct pipe_context
*pctx
)
1212 pctx
->transfer_map
= vc4_resource_transfer_map
;
1213 pctx
->transfer_flush_region
= u_default_transfer_flush_region
;
1214 pctx
->transfer_unmap
= vc4_resource_transfer_unmap
;
1215 pctx
->buffer_subdata
= u_default_buffer_subdata
;
1216 pctx
->texture_subdata
= u_default_texture_subdata
;
1217 pctx
->create_surface
= vc4_create_surface
;
1218 pctx
->surface_destroy
= vc4_surface_destroy
;
1219 pctx
->resource_copy_region
= util_resource_copy_region
;
1220 pctx
->blit
= vc4_blit
;
1221 pctx
->flush_resource
= vc4_flush_resource
;