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_memory.h"
26 #include "util/u_format.h"
27 #include "util/u_inlines.h"
28 #include "util/u_surface.h"
30 #include "vc4_screen.h"
31 #include "vc4_context.h"
32 #include "vc4_resource.h"
33 #include "vc4_tiling.h"
35 static bool miptree_debug
= false;
38 vc4_resource_bo_alloc(struct vc4_resource
*rsc
)
40 struct pipe_resource
*prsc
= &rsc
->base
.b
;
41 struct pipe_screen
*pscreen
= prsc
->screen
;
44 fprintf(stderr
, "alloc %p: size %d + offset %d -> %d\n",
47 rsc
->slices
[0].offset
,
48 rsc
->slices
[0].offset
+
50 rsc
->cube_map_stride
* (prsc
->array_size
- 1));
53 vc4_bo_unreference(&rsc
->bo
);
54 rsc
->bo
= vc4_bo_alloc(vc4_screen(pscreen
),
55 rsc
->slices
[0].offset
+
57 rsc
->cube_map_stride
* (prsc
->array_size
- 1),
62 vc4_resource_transfer_unmap(struct pipe_context
*pctx
,
63 struct pipe_transfer
*ptrans
)
65 struct vc4_context
*vc4
= vc4_context(pctx
);
66 struct vc4_transfer
*trans
= vc4_transfer(ptrans
);
67 struct pipe_resource
*prsc
= ptrans
->resource
;
68 struct vc4_resource
*rsc
= vc4_resource(prsc
);
69 struct vc4_resource_slice
*slice
= &rsc
->slices
[ptrans
->level
];
72 if (ptrans
->usage
& PIPE_TRANSFER_WRITE
) {
73 vc4_store_tiled_image(rsc
->bo
->map
+ slice
->offset
+
74 ptrans
->box
.z
* rsc
->cube_map_stride
,
76 trans
->map
, ptrans
->stride
,
77 slice
->tiling
, rsc
->cpp
,
83 pipe_resource_reference(&ptrans
->resource
, NULL
);
84 util_slab_free(&vc4
->transfer_pool
, ptrans
);
88 vc4_resource_transfer_map(struct pipe_context
*pctx
,
89 struct pipe_resource
*prsc
,
90 unsigned level
, unsigned usage
,
91 const struct pipe_box
*box
,
92 struct pipe_transfer
**pptrans
)
94 struct vc4_context
*vc4
= vc4_context(pctx
);
95 struct vc4_resource
*rsc
= vc4_resource(prsc
);
96 struct vc4_resource_slice
*slice
= &rsc
->slices
[level
];
97 struct vc4_transfer
*trans
;
98 struct pipe_transfer
*ptrans
;
99 enum pipe_format format
= prsc
->format
;
102 if (usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) {
103 vc4_resource_bo_alloc(rsc
);
104 } else if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
105 if (vc4_cl_references_bo(pctx
, rsc
->bo
)) {
106 if ((usage
& PIPE_TRANSFER_DISCARD_RANGE
) &&
107 prsc
->last_level
== 0 &&
108 prsc
->width0
== box
->width
&&
109 prsc
->height0
== box
->height
&&
110 prsc
->depth0
== box
->depth
) {
111 vc4_resource_bo_alloc(rsc
);
118 if (usage
& PIPE_TRANSFER_WRITE
)
121 trans
= util_slab_alloc(&vc4
->transfer_pool
);
125 /* XXX: Handle DONTBLOCK, DISCARD_RANGE, PERSISTENT, COHERENT. */
127 /* util_slab_alloc() doesn't zero: */
128 memset(trans
, 0, sizeof(*trans
));
129 ptrans
= &trans
->base
;
131 pipe_resource_reference(&ptrans
->resource
, prsc
);
132 ptrans
->level
= level
;
133 ptrans
->usage
= usage
;
136 /* Note that the current kernel implementation is synchronous, so no
137 * need to do syncing stuff here yet.
140 if (usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)
141 buf
= vc4_bo_map_unsynchronized(rsc
->bo
);
143 buf
= vc4_bo_map(rsc
->bo
);
145 fprintf(stderr
, "Failed to map bo\n");
152 uint32_t utile_w
= vc4_utile_width(rsc
->cpp
);
153 uint32_t utile_h
= vc4_utile_height(rsc
->cpp
);
155 /* No direct mappings of tiled, since we need to manually
158 if (usage
& PIPE_TRANSFER_MAP_DIRECTLY
)
161 /* We need to align the box to utile boundaries, since that's
162 * what load/store operate on.
164 uint32_t box_start_x
= ptrans
->box
.x
& (utile_w
- 1);
165 uint32_t box_start_y
= ptrans
->box
.y
& (utile_h
- 1);
166 ptrans
->box
.width
+= box_start_x
;
167 ptrans
->box
.x
-= box_start_x
;
168 ptrans
->box
.height
+= box_start_y
;
169 ptrans
->box
.y
-= box_start_y
;
170 ptrans
->box
.width
= align(ptrans
->box
.width
, utile_w
);
171 ptrans
->box
.height
= align(ptrans
->box
.height
, utile_h
);
173 ptrans
->stride
= ptrans
->box
.width
* rsc
->cpp
;
174 ptrans
->layer_stride
= ptrans
->stride
;
176 trans
->map
= malloc(ptrans
->stride
* ptrans
->box
.height
);
177 if (usage
& PIPE_TRANSFER_READ
) {
178 vc4_load_tiled_image(trans
->map
, ptrans
->stride
,
179 buf
+ slice
->offset
+
180 box
->z
* rsc
->cube_map_stride
,
182 slice
->tiling
, rsc
->cpp
,
186 box_start_x
* rsc
->cpp
+
187 box_start_y
* ptrans
->stride
);
189 ptrans
->stride
= slice
->stride
;
190 ptrans
->layer_stride
= ptrans
->stride
;
192 return buf
+ slice
->offset
+
193 box
->y
/ util_format_get_blockheight(format
) * ptrans
->stride
+
194 box
->x
/ util_format_get_blockwidth(format
) * rsc
->cpp
+
195 box
->z
* rsc
->cube_map_stride
;
200 vc4_resource_transfer_unmap(pctx
, ptrans
);
205 vc4_resource_destroy(struct pipe_screen
*pscreen
,
206 struct pipe_resource
*prsc
)
208 struct vc4_resource
*rsc
= vc4_resource(prsc
);
209 pipe_resource_reference(&rsc
->shadow_parent
, NULL
);
210 vc4_bo_unreference(&rsc
->bo
);
215 vc4_resource_get_handle(struct pipe_screen
*pscreen
,
216 struct pipe_resource
*prsc
,
217 struct winsys_handle
*handle
)
219 struct vc4_resource
*rsc
= vc4_resource(prsc
);
221 return vc4_screen_bo_get_handle(pscreen
, rsc
->bo
, rsc
->slices
[0].stride
,
225 static const struct u_resource_vtbl vc4_resource_vtbl
= {
226 .resource_get_handle
= vc4_resource_get_handle
,
227 .resource_destroy
= vc4_resource_destroy
,
228 .transfer_map
= vc4_resource_transfer_map
,
229 .transfer_flush_region
= u_default_transfer_flush_region
,
230 .transfer_unmap
= vc4_resource_transfer_unmap
,
231 .transfer_inline_write
= u_default_transfer_inline_write
,
235 vc4_setup_slices(struct vc4_resource
*rsc
)
237 struct pipe_resource
*prsc
= &rsc
->base
.b
;
238 uint32_t width
= prsc
->width0
;
239 uint32_t height
= prsc
->height0
;
240 uint32_t pot_width
= util_next_power_of_two(width
);
241 uint32_t pot_height
= util_next_power_of_two(height
);
243 uint32_t utile_w
= vc4_utile_width(rsc
->cpp
);
244 uint32_t utile_h
= vc4_utile_height(rsc
->cpp
);
246 for (int i
= prsc
->last_level
; i
>= 0; i
--) {
247 struct vc4_resource_slice
*slice
= &rsc
->slices
[i
];
249 uint32_t level_width
, level_height
;
252 level_height
= height
;
254 level_width
= u_minify(pot_width
, i
);
255 level_height
= u_minify(pot_height
, i
);
259 slice
->tiling
= VC4_TILING_FORMAT_LINEAR
;
260 level_width
= align(level_width
, utile_w
);
262 if (vc4_size_is_lt(level_width
, level_height
,
264 slice
->tiling
= VC4_TILING_FORMAT_LT
;
265 level_width
= align(level_width
, utile_w
);
266 level_height
= align(level_height
, utile_h
);
268 slice
->tiling
= VC4_TILING_FORMAT_T
;
269 level_width
= align(level_width
,
271 level_height
= align(level_height
,
276 slice
->offset
= offset
;
277 slice
->stride
= level_width
* rsc
->cpp
;
278 slice
->size
= level_height
* slice
->stride
;
280 offset
+= slice
->size
;
283 static const char tiling_chars
[] = {
284 [VC4_TILING_FORMAT_LINEAR
] = 'R',
285 [VC4_TILING_FORMAT_LT
] = 'L',
286 [VC4_TILING_FORMAT_T
] = 'T'
289 "rsc setup %p (format %d), %dx%d: "
290 "level %d (%c) -> %dx%d, stride %d@0x%08x\n",
291 rsc
, rsc
->vc4_format
,
292 prsc
->width0
, prsc
->height0
,
293 i
, tiling_chars
[slice
->tiling
],
294 level_width
, level_height
,
295 slice
->stride
, slice
->offset
);
299 /* The texture base pointer that has to point to level 0 doesn't have
300 * intra-page bits, so we have to align it, and thus shift up all the
303 uint32_t page_align_offset
= (align(rsc
->slices
[0].offset
, 4096) -
304 rsc
->slices
[0].offset
);
305 if (page_align_offset
) {
306 for (int i
= 0; i
<= prsc
->last_level
; i
++)
307 rsc
->slices
[i
].offset
+= page_align_offset
;
310 /* Cube map faces appear as whole miptrees at a page-aligned offset
311 * from the first face's miptree.
313 if (prsc
->target
== PIPE_TEXTURE_CUBE
) {
314 rsc
->cube_map_stride
= align(rsc
->slices
[0].offset
+
315 rsc
->slices
[0].size
, 4096);
319 static struct vc4_resource
*
320 vc4_resource_setup(struct pipe_screen
*pscreen
,
321 const struct pipe_resource
*tmpl
)
323 struct vc4_resource
*rsc
= CALLOC_STRUCT(vc4_resource
);
326 struct pipe_resource
*prsc
= &rsc
->base
.b
;
330 pipe_reference_init(&prsc
->reference
, 1);
331 prsc
->screen
= pscreen
;
333 rsc
->base
.vtbl
= &vc4_resource_vtbl
;
334 rsc
->cpp
= util_format_get_blocksize(tmpl
->format
);
341 static enum vc4_texture_data_type
342 get_resource_texture_format(struct pipe_resource
*prsc
)
344 struct vc4_resource
*rsc
= vc4_resource(prsc
);
345 uint8_t format
= vc4_get_tex_format(prsc
->format
);
348 assert(format
== VC4_TEXTURE_TYPE_RGBA8888
);
349 return VC4_TEXTURE_TYPE_RGBA32R
;
355 struct pipe_resource
*
356 vc4_resource_create(struct pipe_screen
*pscreen
,
357 const struct pipe_resource
*tmpl
)
359 struct vc4_resource
*rsc
= vc4_resource_setup(pscreen
, tmpl
);
360 struct pipe_resource
*prsc
= &rsc
->base
.b
;
362 /* We have to make shared be untiled, since we don't have any way to
363 * communicate metadata about tiling currently.
365 if (tmpl
->target
== PIPE_BUFFER
||
366 (tmpl
->bind
& (PIPE_BIND_SCANOUT
|
369 PIPE_BIND_CURSOR
))) {
375 if (tmpl
->target
!= PIPE_BUFFER
)
376 rsc
->vc4_format
= get_resource_texture_format(prsc
);
378 vc4_setup_slices(rsc
);
379 vc4_resource_bo_alloc(rsc
);
385 vc4_resource_destroy(pscreen
, prsc
);
389 static struct pipe_resource
*
390 vc4_resource_from_handle(struct pipe_screen
*pscreen
,
391 const struct pipe_resource
*tmpl
,
392 struct winsys_handle
*handle
)
394 struct vc4_resource
*rsc
= vc4_resource_setup(pscreen
, tmpl
);
395 struct pipe_resource
*prsc
= &rsc
->base
.b
;
396 struct vc4_resource_slice
*slice
= &rsc
->slices
[0];
402 rsc
->bo
= vc4_screen_bo_from_handle(pscreen
, handle
);
406 if (!using_vc4_simulator
)
407 slice
->stride
= handle
->stride
;
409 slice
->stride
= align(prsc
->width0
* rsc
->cpp
, 16);
411 slice
->tiling
= VC4_TILING_FORMAT_LINEAR
;
413 rsc
->vc4_format
= get_resource_texture_format(prsc
);
417 "rsc import %p (format %d), %dx%d: "
418 "level 0 (R) -> stride %d@0x%08x\n",
419 rsc
, rsc
->vc4_format
,
420 prsc
->width0
, prsc
->height0
,
421 slice
->stride
, slice
->offset
);
427 vc4_resource_destroy(pscreen
, prsc
);
431 static struct pipe_surface
*
432 vc4_create_surface(struct pipe_context
*pctx
,
433 struct pipe_resource
*ptex
,
434 const struct pipe_surface
*surf_tmpl
)
436 struct vc4_surface
*surface
= CALLOC_STRUCT(vc4_surface
);
437 struct vc4_resource
*rsc
= vc4_resource(ptex
);
442 assert(surf_tmpl
->u
.tex
.first_layer
== surf_tmpl
->u
.tex
.last_layer
);
444 struct pipe_surface
*psurf
= &surface
->base
;
445 unsigned level
= surf_tmpl
->u
.tex
.level
;
447 pipe_reference_init(&psurf
->reference
, 1);
448 pipe_resource_reference(&psurf
->texture
, ptex
);
450 psurf
->context
= pctx
;
451 psurf
->format
= surf_tmpl
->format
;
452 psurf
->width
= u_minify(ptex
->width0
, level
);
453 psurf
->height
= u_minify(ptex
->height0
, level
);
454 psurf
->u
.tex
.level
= level
;
455 psurf
->u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
456 psurf
->u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
457 surface
->offset
= rsc
->slices
[level
].offset
;
458 surface
->tiling
= rsc
->slices
[level
].tiling
;
460 return &surface
->base
;
464 vc4_surface_destroy(struct pipe_context
*pctx
, struct pipe_surface
*psurf
)
466 pipe_resource_reference(&psurf
->texture
, NULL
);
470 /** Debug routine to dump the contents of an 8888 surface to the console */
472 vc4_dump_surface(struct pipe_surface
*psurf
)
477 struct pipe_resource
*prsc
= psurf
->texture
;
478 struct vc4_resource
*rsc
= vc4_resource(prsc
);
479 uint32_t *map
= vc4_bo_map(rsc
->bo
);
480 uint32_t stride
= rsc
->slices
[0].stride
/ 4;
481 uint32_t width
= psurf
->width
;
482 uint32_t height
= psurf
->height
;
483 uint32_t chunk_w
= width
/ 79;
484 uint32_t chunk_h
= height
/ 40;
485 uint32_t found_colors
[10];
486 uint32_t num_found_colors
= 0;
488 if (rsc
->vc4_format
!= VC4_TEXTURE_TYPE_RGBA32R
) {
489 fprintf(stderr
, "%s: Unsupported format %s\n",
490 __func__
, util_format_short_name(psurf
->format
));
494 for (int by
= 0; by
< height
; by
+= chunk_h
) {
495 for (int bx
= 0; bx
< width
; bx
+= chunk_w
) {
496 int all_found_color
= -1; /* nothing found */
498 for (int y
= by
; y
< MIN2(height
, by
+ chunk_h
); y
++) {
499 for (int x
= bx
; x
< MIN2(width
, bx
+ chunk_w
); x
++) {
500 uint32_t pix
= map
[y
* stride
+ x
];
503 for (i
= 0; i
< num_found_colors
; i
++) {
504 if (pix
== found_colors
[i
])
507 if (i
== num_found_colors
&&
509 ARRAY_SIZE(found_colors
)) {
510 found_colors
[num_found_colors
++] = pix
;
513 if (i
< num_found_colors
) {
514 if (all_found_color
== -1)
516 else if (i
!= all_found_color
)
517 all_found_color
= ARRAY_SIZE(found_colors
);
521 /* If all pixels for this chunk have a consistent
522 * value, then print a character for it. Either a
523 * fixed name (particularly common for piglit tests),
524 * or a runtime-generated number.
526 if (all_found_color
>= 0 &&
527 all_found_color
< ARRAY_SIZE(found_colors
)) {
528 static const struct {
540 for (i
= 0; i
< ARRAY_SIZE(named_colors
); i
++) {
541 if (named_colors
[i
].val
==
542 found_colors
[all_found_color
]) {
543 fprintf(stderr
, "%s",
548 /* For unnamed colors, print a number and the
549 * numbers will have values printed at the
552 if (i
== ARRAY_SIZE(named_colors
)) {
553 fprintf(stderr
, "%c",
554 '0' + all_found_color
);
557 /* If there's no consistent color, print this.
559 fprintf(stderr
, ".");
562 fprintf(stderr
, "\n");
565 for (int i
= 0; i
< num_found_colors
; i
++) {
566 fprintf(stderr
, "color %d: 0x%08x\n", i
, found_colors
[i
]);
571 vc4_flush_resource(struct pipe_context
*pctx
, struct pipe_resource
*resource
)
573 /* All calls to flush_resource are followed by a flush of the context,
574 * so there's nothing to do.
579 vc4_update_shadow_baselevel_texture(struct pipe_context
*pctx
,
580 struct pipe_sampler_view
*view
)
582 struct vc4_resource
*shadow
= vc4_resource(view
->texture
);
583 struct vc4_resource
*orig
= vc4_resource(shadow
->shadow_parent
);
586 if (shadow
->writes
== orig
->writes
)
589 for (int i
= 0; i
<= shadow
->base
.b
.last_level
; i
++) {
590 unsigned width
= u_minify(shadow
->base
.b
.width0
, i
);
591 unsigned height
= u_minify(shadow
->base
.b
.height0
, i
);
592 struct pipe_blit_info info
= {
594 .resource
= &shadow
->base
.b
,
604 .format
= shadow
->base
.b
.format
,
607 .resource
= &orig
->base
.b
,
608 .level
= view
->u
.tex
.first_level
+ i
,
617 .format
= orig
->base
.b
.format
,
621 pctx
->blit(pctx
, &info
);
624 shadow
->writes
= orig
->writes
;
628 * Converts a 4-byte index buffer to 2 bytes.
630 * Since GLES2 only has support for 1 and 2-byte indices, the hardware doesn't
631 * include 4-byte index support, and we have to shrink it down.
633 * There's no fallback support for when indices end up being larger than 2^16,
634 * though it will at least assertion fail. Also, if the original index data
635 * was in user memory, it would be nice to not have uploaded it to a VBO
636 * before translating.
639 vc4_update_shadow_index_buffer(struct pipe_context
*pctx
,
640 const struct pipe_index_buffer
*ib
)
642 struct vc4_resource
*shadow
= vc4_resource(ib
->buffer
);
643 struct vc4_resource
*orig
= vc4_resource(shadow
->shadow_parent
);
644 uint32_t count
= shadow
->base
.b
.width0
/ 2;
646 if (shadow
->writes
== orig
->writes
)
649 struct pipe_transfer
*src_transfer
;
650 uint32_t *src
= pipe_buffer_map_range(pctx
, &orig
->base
.b
,
653 PIPE_TRANSFER_READ
, &src_transfer
);
655 struct pipe_transfer
*dst_transfer
;
656 uint16_t *dst
= pipe_buffer_map_range(pctx
, &shadow
->base
.b
,
659 PIPE_TRANSFER_WRITE
, &dst_transfer
);
661 for (int i
= 0; i
< count
; i
++) {
662 uint32_t src_index
= src
[i
];
663 assert(src_index
<= 0xffff);
667 pctx
->transfer_unmap(pctx
, dst_transfer
);
668 pctx
->transfer_unmap(pctx
, src_transfer
);
670 shadow
->writes
= orig
->writes
;
674 vc4_resource_screen_init(struct pipe_screen
*pscreen
)
676 pscreen
->resource_create
= vc4_resource_create
;
677 pscreen
->resource_from_handle
= vc4_resource_from_handle
;
678 pscreen
->resource_get_handle
= u_resource_get_handle_vtbl
;
679 pscreen
->resource_destroy
= u_resource_destroy_vtbl
;
683 vc4_resource_context_init(struct pipe_context
*pctx
)
685 pctx
->transfer_map
= u_transfer_map_vtbl
;
686 pctx
->transfer_flush_region
= u_transfer_flush_region_vtbl
;
687 pctx
->transfer_unmap
= u_transfer_unmap_vtbl
;
688 pctx
->transfer_inline_write
= u_transfer_inline_write_vtbl
;
689 pctx
->create_surface
= vc4_create_surface
;
690 pctx
->surface_destroy
= vc4_surface_destroy
;
691 pctx
->resource_copy_region
= util_resource_copy_region
;
692 pctx
->blit
= vc4_blit
;
693 pctx
->flush_resource
= vc4_flush_resource
;