2 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Rob Clark <robclark@freedesktop.org>
27 #include "util/format/u_format.h"
28 #include "util/format/u_format_rgtc.h"
29 #include "util/format/u_format_zs.h"
30 #include "util/u_inlines.h"
31 #include "util/u_transfer.h"
32 #include "util/u_string.h"
33 #include "util/u_surface.h"
35 #include "util/u_drm.h"
37 #include "freedreno_resource.h"
38 #include "freedreno_batch_cache.h"
39 #include "freedreno_blitter.h"
40 #include "freedreno_fence.h"
41 #include "freedreno_screen.h"
42 #include "freedreno_surface.h"
43 #include "freedreno_context.h"
44 #include "freedreno_query_hw.h"
45 #include "freedreno_util.h"
47 #include "drm-uapi/drm_fourcc.h"
50 /* XXX this should go away, needed for 'struct winsys_handle' */
51 #include "state_tracker/drm_driver.h"
53 /* A private modifier for now, so we have a way to request tiled but not
54 * compressed. It would perhaps be good to get real modifiers for the
55 * tiled formats, but would probably need to do some work to figure out
56 * the layout(s) of the tiled modes, and whether they are the same
59 #define FD_FORMAT_MOD_QCOM_TILED fourcc_mod_code(QCOM, 0xffffffff)
62 * Go through the entire state and see if the resource is bound
63 * anywhere. If it is, mark the relevant state as dirty. This is
64 * called on realloc_bo to ensure the neccessary state is re-
65 * emitted so the GPU looks at the new backing bo.
68 rebind_resource(struct fd_context
*ctx
, struct pipe_resource
*prsc
)
71 for (unsigned i
= 0; i
< ctx
->vtx
.vertexbuf
.count
&& !(ctx
->dirty
& FD_DIRTY_VTXBUF
); i
++) {
72 if (ctx
->vtx
.vertexbuf
.vb
[i
].buffer
.resource
== prsc
)
73 ctx
->dirty
|= FD_DIRTY_VTXBUF
;
76 /* per-shader-stage resources: */
77 for (unsigned stage
= 0; stage
< PIPE_SHADER_TYPES
; stage
++) {
78 /* Constbufs.. note that constbuf[0] is normal uniforms emitted in
79 * cmdstream rather than by pointer..
81 const unsigned num_ubos
= util_last_bit(ctx
->constbuf
[stage
].enabled_mask
);
82 for (unsigned i
= 1; i
< num_ubos
; i
++) {
83 if (ctx
->dirty_shader
[stage
] & FD_DIRTY_SHADER_CONST
)
85 if (ctx
->constbuf
[stage
].cb
[i
].buffer
== prsc
)
86 ctx
->dirty_shader
[stage
] |= FD_DIRTY_SHADER_CONST
;
90 for (unsigned i
= 0; i
< ctx
->tex
[stage
].num_textures
; i
++) {
91 if (ctx
->dirty_shader
[stage
] & FD_DIRTY_SHADER_TEX
)
93 if (ctx
->tex
[stage
].textures
[i
] && (ctx
->tex
[stage
].textures
[i
]->texture
== prsc
))
94 ctx
->dirty_shader
[stage
] |= FD_DIRTY_SHADER_TEX
;
98 const unsigned num_images
= util_last_bit(ctx
->shaderimg
[stage
].enabled_mask
);
99 for (unsigned i
= 0; i
< num_images
; i
++) {
100 if (ctx
->dirty_shader
[stage
] & FD_DIRTY_SHADER_IMAGE
)
102 if (ctx
->shaderimg
[stage
].si
[i
].resource
== prsc
)
103 ctx
->dirty_shader
[stage
] |= FD_DIRTY_SHADER_IMAGE
;
107 const unsigned num_ssbos
= util_last_bit(ctx
->shaderbuf
[stage
].enabled_mask
);
108 for (unsigned i
= 0; i
< num_ssbos
; i
++) {
109 if (ctx
->dirty_shader
[stage
] & FD_DIRTY_SHADER_SSBO
)
111 if (ctx
->shaderbuf
[stage
].sb
[i
].buffer
== prsc
)
112 ctx
->dirty_shader
[stage
] |= FD_DIRTY_SHADER_SSBO
;
118 realloc_bo(struct fd_resource
*rsc
, uint32_t size
)
120 struct pipe_resource
*prsc
= &rsc
->base
;
121 struct fd_screen
*screen
= fd_screen(rsc
->base
.screen
);
122 uint32_t flags
= DRM_FREEDRENO_GEM_CACHE_WCOMBINE
|
123 DRM_FREEDRENO_GEM_TYPE_KMEM
|
124 COND(prsc
->bind
& PIPE_BIND_SCANOUT
, DRM_FREEDRENO_GEM_SCANOUT
);
125 /* TODO other flags? */
127 /* if we start using things other than write-combine,
128 * be sure to check for PIPE_RESOURCE_FLAG_MAP_COHERENT
134 rsc
->bo
= fd_bo_new(screen
->dev
, size
, flags
, "%ux%ux%u@%u:%x",
135 prsc
->width0
, prsc
->height0
, prsc
->depth0
, rsc
->layout
.cpp
, prsc
->bind
);
136 rsc
->seqno
= p_atomic_inc_return(&screen
->rsc_seqno
);
137 util_range_set_empty(&rsc
->valid_buffer_range
);
138 fd_bc_invalidate_resource(rsc
, true);
142 do_blit(struct fd_context
*ctx
, const struct pipe_blit_info
*blit
, bool fallback
)
144 struct pipe_context
*pctx
= &ctx
->base
;
146 /* TODO size threshold too?? */
147 if (fallback
|| !fd_blit(pctx
, blit
)) {
148 /* do blit on cpu: */
149 util_resource_copy_region(pctx
,
150 blit
->dst
.resource
, blit
->dst
.level
, blit
->dst
.box
.x
,
151 blit
->dst
.box
.y
, blit
->dst
.box
.z
,
152 blit
->src
.resource
, blit
->src
.level
, &blit
->src
.box
);
157 * @rsc: the resource to shadow
158 * @level: the level to discard (if box != NULL, otherwise ignored)
159 * @box: the box to discard (or NULL if none)
160 * @modifier: the modifier for the new buffer state
163 fd_try_shadow_resource(struct fd_context
*ctx
, struct fd_resource
*rsc
,
164 unsigned level
, const struct pipe_box
*box
, uint64_t modifier
)
166 struct pipe_context
*pctx
= &ctx
->base
;
167 struct pipe_resource
*prsc
= &rsc
->base
;
168 bool fallback
= false;
173 /* TODO: somehow munge dimensions and format to copy unsupported
174 * render target format to something that is supported?
176 if (!pctx
->screen
->is_format_supported(pctx
->screen
,
177 prsc
->format
, prsc
->target
, prsc
->nr_samples
,
178 prsc
->nr_storage_samples
,
179 PIPE_BIND_RENDER_TARGET
))
182 /* do shadowing back-blits on the cpu for buffers: */
183 if (prsc
->target
== PIPE_BUFFER
)
186 bool discard_whole_level
= box
&& util_texrange_covers_whole_level(prsc
, level
,
187 box
->x
, box
->y
, box
->z
, box
->width
, box
->height
, box
->depth
);
189 /* TODO need to be more clever about current level */
190 if ((prsc
->target
>= PIPE_TEXTURE_2D
) && box
&& !discard_whole_level
)
193 struct pipe_resource
*pshadow
=
194 pctx
->screen
->resource_create_with_modifiers(pctx
->screen
,
200 assert(!ctx
->in_shadow
);
201 ctx
->in_shadow
= true;
203 /* get rid of any references that batch-cache might have to us (which
204 * should empty/destroy rsc->batches hashset)
206 fd_bc_invalidate_resource(rsc
, false);
208 mtx_lock(&ctx
->screen
->lock
);
210 /* Swap the backing bo's, so shadow becomes the old buffer,
211 * blit from shadow to new buffer. From here on out, we
214 * Note that we need to do it in this order, otherwise if
215 * we go down cpu blit path, the recursive transfer_map()
216 * sees the wrong status..
218 struct fd_resource
*shadow
= fd_resource(pshadow
);
220 DBG("shadow: %p (%d) -> %p (%d)\n", rsc
, rsc
->base
.reference
.count
,
221 shadow
, shadow
->base
.reference
.count
);
223 /* TODO valid_buffer_range?? */
224 swap(rsc
->bo
, shadow
->bo
);
225 swap(rsc
->write_batch
, shadow
->write_batch
);
226 swap(rsc
->layout
, shadow
->layout
);
227 rsc
->seqno
= p_atomic_inc_return(&ctx
->screen
->rsc_seqno
);
229 /* at this point, the newly created shadow buffer is not referenced
230 * by any batches, but the existing rsc (probably) is. We need to
231 * transfer those references over:
233 debug_assert(shadow
->batch_mask
== 0);
234 struct fd_batch
*batch
;
235 foreach_batch(batch
, &ctx
->screen
->batch_cache
, rsc
->batch_mask
) {
236 struct set_entry
*entry
= _mesa_set_search(batch
->resources
, rsc
);
237 _mesa_set_remove(batch
->resources
, entry
);
238 _mesa_set_add(batch
->resources
, shadow
);
240 swap(rsc
->batch_mask
, shadow
->batch_mask
);
242 mtx_unlock(&ctx
->screen
->lock
);
244 struct pipe_blit_info blit
= {};
245 blit
.dst
.resource
= prsc
;
246 blit
.dst
.format
= prsc
->format
;
247 blit
.src
.resource
= pshadow
;
248 blit
.src
.format
= pshadow
->format
;
249 blit
.mask
= util_format_get_mask(prsc
->format
);
250 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
252 #define set_box(field, val) do { \
253 blit.dst.field = (val); \
254 blit.src.field = (val); \
257 /* blit the other levels in their entirety: */
258 for (unsigned l
= 0; l
<= prsc
->last_level
; l
++) {
259 if (box
&& l
== level
)
262 /* just blit whole level: */
264 set_box(box
.width
, u_minify(prsc
->width0
, l
));
265 set_box(box
.height
, u_minify(prsc
->height0
, l
));
266 set_box(box
.depth
, u_minify(prsc
->depth0
, l
));
268 for (int i
= 0; i
< prsc
->array_size
; i
++) {
270 do_blit(ctx
, &blit
, fallback
);
274 /* deal w/ current level specially, since we might need to split
275 * it up into a couple blits:
277 if (box
&& !discard_whole_level
) {
278 set_box(level
, level
);
280 switch (prsc
->target
) {
282 case PIPE_TEXTURE_1D
:
285 set_box(box
.height
, 1);
286 set_box(box
.depth
, 1);
290 set_box(box
.width
, box
->x
);
292 do_blit(ctx
, &blit
, fallback
);
294 if ((box
->x
+ box
->width
) < u_minify(prsc
->width0
, level
)) {
295 set_box(box
.x
, box
->x
+ box
->width
);
296 set_box(box
.width
, u_minify(prsc
->width0
, level
) - (box
->x
+ box
->width
));
298 do_blit(ctx
, &blit
, fallback
);
301 case PIPE_TEXTURE_2D
:
308 ctx
->in_shadow
= false;
310 pipe_resource_reference(&pshadow
, NULL
);
316 * Uncompress an UBWC compressed buffer "in place". This works basically
317 * like resource shadowing, creating a new resource, and doing an uncompress
318 * blit, and swapping the state between shadow and original resource so it
319 * appears to the state tracker as if nothing changed.
322 fd_resource_uncompress(struct fd_context
*ctx
, struct fd_resource
*rsc
)
325 fd_try_shadow_resource(ctx
, rsc
, 0, NULL
, FD_FORMAT_MOD_QCOM_TILED
);
327 /* shadow should not fail in any cases where we need to uncompress: */
328 debug_assert(success
);
331 * TODO what if rsc is used in other contexts, we don't currently
332 * have a good way to rebind_resource() in other contexts. And an
333 * app that is reading one resource in multiple contexts, isn't
334 * going to expect that the resource is modified.
336 * Hopefully the edge cases where we need to uncompress are rare
337 * enough that they mostly only show up in deqp.
340 rebind_resource(ctx
, &rsc
->base
);
343 static struct fd_resource
*
344 fd_alloc_staging(struct fd_context
*ctx
, struct fd_resource
*rsc
,
345 unsigned level
, const struct pipe_box
*box
)
347 struct pipe_context
*pctx
= &ctx
->base
;
348 struct pipe_resource tmpl
= rsc
->base
;
350 tmpl
.width0
= box
->width
;
351 tmpl
.height0
= box
->height
;
352 /* for array textures, box->depth is the array_size, otherwise
353 * for 3d textures, it is the depth:
355 if (tmpl
.array_size
> 1) {
356 if (tmpl
.target
== PIPE_TEXTURE_CUBE
)
357 tmpl
.target
= PIPE_TEXTURE_2D_ARRAY
;
358 tmpl
.array_size
= box
->depth
;
362 tmpl
.depth0
= box
->depth
;
365 tmpl
.bind
|= PIPE_BIND_LINEAR
;
367 struct pipe_resource
*pstaging
=
368 pctx
->screen
->resource_create(pctx
->screen
, &tmpl
);
372 return fd_resource(pstaging
);
376 fd_blit_from_staging(struct fd_context
*ctx
, struct fd_transfer
*trans
)
378 struct pipe_resource
*dst
= trans
->base
.resource
;
379 struct pipe_blit_info blit
= {};
381 blit
.dst
.resource
= dst
;
382 blit
.dst
.format
= dst
->format
;
383 blit
.dst
.level
= trans
->base
.level
;
384 blit
.dst
.box
= trans
->base
.box
;
385 blit
.src
.resource
= trans
->staging_prsc
;
386 blit
.src
.format
= trans
->staging_prsc
->format
;
388 blit
.src
.box
= trans
->staging_box
;
389 blit
.mask
= util_format_get_mask(trans
->staging_prsc
->format
);
390 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
392 do_blit(ctx
, &blit
, false);
396 fd_blit_to_staging(struct fd_context
*ctx
, struct fd_transfer
*trans
)
398 struct pipe_resource
*src
= trans
->base
.resource
;
399 struct pipe_blit_info blit
= {};
401 blit
.src
.resource
= src
;
402 blit
.src
.format
= src
->format
;
403 blit
.src
.level
= trans
->base
.level
;
404 blit
.src
.box
= trans
->base
.box
;
405 blit
.dst
.resource
= trans
->staging_prsc
;
406 blit
.dst
.format
= trans
->staging_prsc
->format
;
408 blit
.dst
.box
= trans
->staging_box
;
409 blit
.mask
= util_format_get_mask(trans
->staging_prsc
->format
);
410 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
412 do_blit(ctx
, &blit
, false);
415 static void fd_resource_transfer_flush_region(struct pipe_context
*pctx
,
416 struct pipe_transfer
*ptrans
,
417 const struct pipe_box
*box
)
419 struct fd_resource
*rsc
= fd_resource(ptrans
->resource
);
421 if (ptrans
->resource
->target
== PIPE_BUFFER
)
422 util_range_add(&rsc
->base
, &rsc
->valid_buffer_range
,
423 ptrans
->box
.x
+ box
->x
,
424 ptrans
->box
.x
+ box
->x
+ box
->width
);
428 flush_resource(struct fd_context
*ctx
, struct fd_resource
*rsc
, unsigned usage
)
430 struct fd_batch
*write_batch
= NULL
;
432 mtx_lock(&ctx
->screen
->lock
);
433 fd_batch_reference_locked(&write_batch
, rsc
->write_batch
);
434 mtx_unlock(&ctx
->screen
->lock
);
436 if (usage
& PIPE_TRANSFER_WRITE
) {
437 struct fd_batch
*batch
, *batches
[32] = {};
440 /* This is a bit awkward, probably a fd_batch_flush_locked()
441 * would make things simpler.. but we need to hold the lock
442 * to iterate the batches which reference this resource. So
443 * we must first grab references under a lock, then flush.
445 mtx_lock(&ctx
->screen
->lock
);
446 batch_mask
= rsc
->batch_mask
;
447 foreach_batch(batch
, &ctx
->screen
->batch_cache
, batch_mask
)
448 fd_batch_reference_locked(&batches
[batch
->idx
], batch
);
449 mtx_unlock(&ctx
->screen
->lock
);
451 foreach_batch(batch
, &ctx
->screen
->batch_cache
, batch_mask
)
452 fd_batch_flush(batch
);
454 foreach_batch(batch
, &ctx
->screen
->batch_cache
, batch_mask
) {
455 fd_batch_reference(&batches
[batch
->idx
], NULL
);
457 assert(rsc
->batch_mask
== 0);
458 } else if (write_batch
) {
459 fd_batch_flush(write_batch
);
462 fd_batch_reference(&write_batch
, NULL
);
464 assert(!rsc
->write_batch
);
468 fd_flush_resource(struct pipe_context
*pctx
, struct pipe_resource
*prsc
)
470 flush_resource(fd_context(pctx
), fd_resource(prsc
), PIPE_TRANSFER_READ
);
474 fd_resource_transfer_unmap(struct pipe_context
*pctx
,
475 struct pipe_transfer
*ptrans
)
477 struct fd_context
*ctx
= fd_context(pctx
);
478 struct fd_resource
*rsc
= fd_resource(ptrans
->resource
);
479 struct fd_transfer
*trans
= fd_transfer(ptrans
);
481 if (trans
->staging_prsc
) {
482 if (ptrans
->usage
& PIPE_TRANSFER_WRITE
)
483 fd_blit_from_staging(ctx
, trans
);
484 pipe_resource_reference(&trans
->staging_prsc
, NULL
);
487 if (!(ptrans
->usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
488 fd_bo_cpu_fini(rsc
->bo
);
491 util_range_add(&rsc
->base
, &rsc
->valid_buffer_range
,
493 ptrans
->box
.x
+ ptrans
->box
.width
);
495 pipe_resource_reference(&ptrans
->resource
, NULL
);
496 slab_free(&ctx
->transfer_pool
, ptrans
);
500 fd_resource_transfer_map(struct pipe_context
*pctx
,
501 struct pipe_resource
*prsc
,
502 unsigned level
, unsigned usage
,
503 const struct pipe_box
*box
,
504 struct pipe_transfer
**pptrans
)
506 struct fd_context
*ctx
= fd_context(pctx
);
507 struct fd_resource
*rsc
= fd_resource(prsc
);
508 struct fdl_slice
*slice
= fd_resource_slice(rsc
, level
);
509 struct fd_transfer
*trans
;
510 struct pipe_transfer
*ptrans
;
511 enum pipe_format format
= prsc
->format
;
517 DBG("prsc=%p, level=%u, usage=%x, box=%dx%d+%d,%d", prsc
, level
, usage
,
518 box
->width
, box
->height
, box
->x
, box
->y
);
520 ptrans
= slab_alloc(&ctx
->transfer_pool
);
524 /* slab_alloc_st() doesn't zero: */
525 trans
= fd_transfer(ptrans
);
526 memset(trans
, 0, sizeof(*trans
));
528 pipe_resource_reference(&ptrans
->resource
, prsc
);
529 ptrans
->level
= level
;
530 ptrans
->usage
= usage
;
532 ptrans
->stride
= util_format_get_nblocksx(format
, slice
->pitch
) * rsc
->layout
.cpp
;
533 ptrans
->layer_stride
= fd_resource_layer_stride(rsc
, level
);
535 /* we always need a staging texture for tiled buffers:
537 * TODO we might sometimes want to *also* shadow the resource to avoid
538 * splitting a batch.. for ex, mid-frame texture uploads to a tiled
541 if (rsc
->layout
.tile_mode
) {
542 struct fd_resource
*staging_rsc
;
544 staging_rsc
= fd_alloc_staging(ctx
, rsc
, level
, box
);
546 struct fdl_slice
*staging_slice
=
547 fd_resource_slice(staging_rsc
, 0);
548 // TODO for PIPE_TRANSFER_READ, need to do untiling blit..
549 trans
->staging_prsc
= &staging_rsc
->base
;
550 trans
->base
.stride
= util_format_get_nblocksx(format
,
551 staging_slice
->pitch
) * staging_rsc
->layout
.cpp
;
552 trans
->base
.layer_stride
= fd_resource_layer_stride(staging_rsc
, 0);
553 trans
->staging_box
= *box
;
554 trans
->staging_box
.x
= 0;
555 trans
->staging_box
.y
= 0;
556 trans
->staging_box
.z
= 0;
558 if (usage
& PIPE_TRANSFER_READ
) {
559 fd_blit_to_staging(ctx
, trans
);
561 fd_bo_cpu_prep(staging_rsc
->bo
, ctx
->pipe
,
562 DRM_FREEDRENO_PREP_READ
);
565 buf
= fd_bo_map(staging_rsc
->bo
);
570 ctx
->stats
.staging_uploads
++;
576 if (ctx
->in_shadow
&& !(usage
& PIPE_TRANSFER_READ
))
577 usage
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
579 if (usage
& PIPE_TRANSFER_READ
)
580 op
|= DRM_FREEDRENO_PREP_READ
;
582 if (usage
& PIPE_TRANSFER_WRITE
)
583 op
|= DRM_FREEDRENO_PREP_WRITE
;
585 if (usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) {
586 realloc_bo(rsc
, fd_bo_size(rsc
->bo
));
587 rebind_resource(ctx
, prsc
);
588 } else if ((usage
& PIPE_TRANSFER_WRITE
) &&
589 prsc
->target
== PIPE_BUFFER
&&
590 !util_ranges_intersect(&rsc
->valid_buffer_range
,
591 box
->x
, box
->x
+ box
->width
)) {
592 /* We are trying to write to a previously uninitialized range. No need
595 } else if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
596 struct fd_batch
*write_batch
= NULL
;
598 /* hold a reference, so it doesn't disappear under us: */
599 fd_context_lock(ctx
);
600 fd_batch_reference_locked(&write_batch
, rsc
->write_batch
);
601 fd_context_unlock(ctx
);
603 if ((usage
& PIPE_TRANSFER_WRITE
) && write_batch
&&
604 write_batch
->back_blit
) {
605 /* if only thing pending is a back-blit, we can discard it: */
606 fd_batch_reset(write_batch
);
609 /* If the GPU is writing to the resource, or if it is reading from the
610 * resource and we're trying to write to it, flush the renders.
612 bool needs_flush
= pending(rsc
, !!(usage
& PIPE_TRANSFER_WRITE
));
613 bool busy
= needs_flush
|| (0 != fd_bo_cpu_prep(rsc
->bo
,
614 ctx
->pipe
, op
| DRM_FREEDRENO_PREP_NOSYNC
));
616 /* if we need to flush/stall, see if we can make a shadow buffer
619 * TODO we could go down this path !reorder && !busy_for_read
620 * ie. we only *don't* want to go down this path if the blit
621 * will trigger a flush!
623 if (ctx
->screen
->reorder
&& busy
&& !(usage
& PIPE_TRANSFER_READ
) &&
624 (usage
& PIPE_TRANSFER_DISCARD_RANGE
)) {
625 /* try shadowing only if it avoids a flush, otherwise staging would
628 if (needs_flush
&& fd_try_shadow_resource(ctx
, rsc
, level
,
629 box
, DRM_FORMAT_MOD_LINEAR
)) {
630 needs_flush
= busy
= false;
631 rebind_resource(ctx
, prsc
);
632 ctx
->stats
.shadow_uploads
++;
634 struct fd_resource
*staging_rsc
;
637 flush_resource(ctx
, rsc
, usage
);
641 /* in this case, we don't need to shadow the whole resource,
642 * since any draw that references the previous contents has
643 * already had rendering flushed for all tiles. So we can
644 * use a staging buffer to do the upload.
646 staging_rsc
= fd_alloc_staging(ctx
, rsc
, level
, box
);
648 struct fdl_slice
*staging_slice
=
649 fd_resource_slice(staging_rsc
, 0);
650 trans
->staging_prsc
= &staging_rsc
->base
;
651 trans
->base
.stride
= util_format_get_nblocksx(format
,
652 staging_slice
->pitch
) * staging_rsc
->layout
.cpp
;
653 trans
->base
.layer_stride
=
654 fd_resource_layer_stride(staging_rsc
, 0);
655 trans
->staging_box
= *box
;
656 trans
->staging_box
.x
= 0;
657 trans
->staging_box
.y
= 0;
658 trans
->staging_box
.z
= 0;
659 buf
= fd_bo_map(staging_rsc
->bo
);
664 fd_batch_reference(&write_batch
, NULL
);
666 ctx
->stats
.staging_uploads
++;
674 flush_resource(ctx
, rsc
, usage
);
678 fd_batch_reference(&write_batch
, NULL
);
680 /* The GPU keeps track of how the various bo's are being used, and
681 * will wait if necessary for the proper operation to have
685 ret
= fd_bo_cpu_prep(rsc
->bo
, ctx
->pipe
, op
);
691 buf
= fd_bo_map(rsc
->bo
);
693 box
->y
/ util_format_get_blockheight(format
) * ptrans
->stride
+
694 box
->x
/ util_format_get_blockwidth(format
) * rsc
->layout
.cpp
+
695 fd_resource_offset(rsc
, level
, box
->z
);
697 if (usage
& PIPE_TRANSFER_WRITE
)
705 fd_resource_transfer_unmap(pctx
, ptrans
);
710 fd_resource_destroy(struct pipe_screen
*pscreen
,
711 struct pipe_resource
*prsc
)
713 struct fd_resource
*rsc
= fd_resource(prsc
);
714 fd_bc_invalidate_resource(rsc
, true);
718 renderonly_scanout_destroy(rsc
->scanout
, fd_screen(pscreen
)->ro
);
720 util_range_destroy(&rsc
->valid_buffer_range
);
725 fd_resource_modifier(struct fd_resource
*rsc
)
727 if (!rsc
->layout
.tile_mode
)
728 return DRM_FORMAT_MOD_LINEAR
;
730 if (rsc
->layout
.ubwc_layer_size
)
731 return DRM_FORMAT_MOD_QCOM_COMPRESSED
;
733 /* TODO invent a modifier for tiled but not UBWC buffers: */
734 return DRM_FORMAT_MOD_INVALID
;
738 fd_resource_get_handle(struct pipe_screen
*pscreen
,
739 struct pipe_context
*pctx
,
740 struct pipe_resource
*prsc
,
741 struct winsys_handle
*handle
,
744 struct fd_resource
*rsc
= fd_resource(prsc
);
746 handle
->modifier
= fd_resource_modifier(rsc
);
748 return fd_screen_bo_get_handle(pscreen
, rsc
->bo
, rsc
->scanout
,
749 fd_resource_slice(rsc
, 0)->pitch
* rsc
->layout
.cpp
, handle
);
753 setup_slices(struct fd_resource
*rsc
, uint32_t alignment
, enum pipe_format format
)
755 struct pipe_resource
*prsc
= &rsc
->base
;
756 struct fd_screen
*screen
= fd_screen(prsc
->screen
);
757 enum util_format_layout layout
= util_format_description(format
)->layout
;
758 uint32_t pitchalign
= screen
->gmem_alignw
;
759 uint32_t level
, size
= 0;
760 uint32_t width
= prsc
->width0
;
761 uint32_t height
= prsc
->height0
;
762 uint32_t depth
= prsc
->depth0
;
763 /* in layer_first layout, the level (slice) contains just one
764 * layer (since in fact the layer contains the slices)
766 uint32_t layers_in_level
= rsc
->layout
.layer_first
? 1 : prsc
->array_size
;
768 for (level
= 0; level
<= prsc
->last_level
; level
++) {
769 struct fdl_slice
*slice
= fd_resource_slice(rsc
, level
);
772 if (layout
== UTIL_FORMAT_LAYOUT_ASTC
)
773 slice
->pitch
= width
=
774 util_align_npot(width
, pitchalign
* util_format_get_blockwidth(format
));
776 slice
->pitch
= width
= align(width
, pitchalign
);
777 slice
->offset
= size
;
778 blocks
= util_format_get_nblocks(format
, width
, height
);
779 /* 1d array and 2d array textures must all have the same layer size
780 * for each miplevel on a3xx. 3d textures can have different layer
781 * sizes for high levels, but the hw auto-sizer is buggy (or at least
782 * different than what this code does), so as soon as the layer size
783 * range gets into range, we stop reducing it.
785 if (prsc
->target
== PIPE_TEXTURE_3D
&& (
787 (level
> 1 && fd_resource_slice(rsc
, level
- 1)->size0
> 0xf000)))
788 slice
->size0
= align(blocks
* rsc
->layout
.cpp
, alignment
);
789 else if (level
== 0 || rsc
->layout
.layer_first
|| alignment
== 1)
790 slice
->size0
= align(blocks
* rsc
->layout
.cpp
, alignment
);
792 slice
->size0
= fd_resource_slice(rsc
, level
- 1)->size0
;
794 size
+= slice
->size0
* depth
* layers_in_level
;
796 width
= u_minify(width
, 1);
797 height
= u_minify(height
, 1);
798 depth
= u_minify(depth
, 1);
805 slice_alignment(enum pipe_texture_target target
)
807 /* on a3xx, 2d array and 3d textures seem to want their
808 * layers aligned to page boundaries:
811 case PIPE_TEXTURE_3D
:
812 case PIPE_TEXTURE_1D_ARRAY
:
813 case PIPE_TEXTURE_2D_ARRAY
:
820 /* cross generation texture layout to plug in to screen->setup_slices()..
821 * replace with generation specific one as-needed.
823 * TODO for a4xx probably can extract out the a4xx specific logic int
824 * a small fd4_setup_slices() wrapper that sets up layer_first, and then
828 fd_setup_slices(struct fd_resource
*rsc
)
832 alignment
= slice_alignment(rsc
->base
.target
);
834 struct fd_screen
*screen
= fd_screen(rsc
->base
.screen
);
835 if (is_a4xx(screen
)) {
836 switch (rsc
->base
.target
) {
837 case PIPE_TEXTURE_3D
:
838 rsc
->layout
.layer_first
= false;
841 rsc
->layout
.layer_first
= true;
847 return setup_slices(rsc
, alignment
, rsc
->base
.format
);
850 /* special case to resize query buf after allocated.. */
852 fd_resource_resize(struct pipe_resource
*prsc
, uint32_t sz
)
854 struct fd_resource
*rsc
= fd_resource(prsc
);
856 debug_assert(prsc
->width0
== 0);
857 debug_assert(prsc
->target
== PIPE_BUFFER
);
858 debug_assert(prsc
->bind
== PIPE_BIND_QUERY_BUFFER
);
861 realloc_bo(rsc
, fd_screen(prsc
->screen
)->setup_slices(rsc
));
865 fd_resource_layout_init(struct pipe_resource
*prsc
)
867 struct fd_resource
*rsc
= fd_resource(prsc
);
868 struct fdl_layout
*layout
= &rsc
->layout
;
870 layout
->width0
= prsc
->width0
;
871 layout
->height0
= prsc
->height0
;
872 layout
->depth0
= prsc
->depth0
;
874 layout
->cpp
= util_format_get_blocksize(prsc
->format
);
875 layout
->cpp
*= fd_resource_nr_samples(prsc
);
879 * Create a new texture object, using the given template info.
881 static struct pipe_resource
*
882 fd_resource_create_with_modifiers(struct pipe_screen
*pscreen
,
883 const struct pipe_resource
*tmpl
,
884 const uint64_t *modifiers
, int count
)
886 struct fd_screen
*screen
= fd_screen(pscreen
);
887 struct fd_resource
*rsc
;
888 struct pipe_resource
*prsc
;
889 enum pipe_format format
= tmpl
->format
;
892 /* when using kmsro, scanout buffers are allocated on the display device
893 * create_with_modifiers() doesn't give us usage flags, so we have to
894 * assume that all calls with modifiers are scanout-possible
897 ((tmpl
->bind
& PIPE_BIND_SCANOUT
) ||
898 !(count
== 1 && modifiers
[0] == DRM_FORMAT_MOD_INVALID
))) {
899 struct pipe_resource scanout_templat
= *tmpl
;
900 struct renderonly_scanout
*scanout
;
901 struct winsys_handle handle
;
903 /* apply freedreno alignment requirement */
904 scanout_templat
.width0
= align(tmpl
->width0
, screen
->gmem_alignw
);
906 scanout
= renderonly_scanout_for_resource(&scanout_templat
,
907 screen
->ro
, &handle
);
911 renderonly_scanout_destroy(scanout
, screen
->ro
);
913 assert(handle
.type
== WINSYS_HANDLE_TYPE_FD
);
914 rsc
= fd_resource(pscreen
->resource_from_handle(pscreen
, tmpl
,
916 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE
));
917 close(handle
.handle
);
924 rsc
= CALLOC_STRUCT(fd_resource
);
927 DBG("%p: target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
928 "nr_samples=%u, usage=%u, bind=%x, flags=%x", prsc
,
929 tmpl
->target
, util_format_name(format
),
930 tmpl
->width0
, tmpl
->height0
, tmpl
->depth0
,
931 tmpl
->array_size
, tmpl
->last_level
, tmpl
->nr_samples
,
932 tmpl
->usage
, tmpl
->bind
, tmpl
->flags
);
938 fd_resource_layout_init(prsc
);
941 (PIPE_BIND_SCANOUT | \
943 PIPE_BIND_DISPLAY_TARGET)
945 bool linear
= drm_find_modifier(DRM_FORMAT_MOD_LINEAR
, modifiers
, count
);
946 if (tmpl
->bind
& LINEAR
)
949 if (fd_mesa_debug
& FD_DBG_NOTILE
)
952 /* Normally, for non-shared buffers, allow buffer compression if
953 * not shared, otherwise only allow if QCOM_COMPRESSED modifier
956 * TODO we should probably also limit tiled in a similar way,
957 * except we don't have a format modifier for tiled. (We probably
960 bool allow_ubwc
= drm_find_modifier(DRM_FORMAT_MOD_INVALID
, modifiers
, count
);
961 if (tmpl
->bind
& PIPE_BIND_SHARED
)
962 allow_ubwc
= drm_find_modifier(DRM_FORMAT_MOD_QCOM_COMPRESSED
, modifiers
, count
);
964 allow_ubwc
&= !(fd_mesa_debug
& FD_DBG_NOUBWC
);
966 pipe_reference_init(&prsc
->reference
, 1);
968 prsc
->screen
= pscreen
;
970 if (screen
->tile_mode
&&
971 (tmpl
->target
!= PIPE_BUFFER
) &&
973 rsc
->layout
.tile_mode
= screen
->tile_mode(prsc
);
976 util_range_init(&rsc
->valid_buffer_range
);
978 rsc
->internal_format
= format
;
980 if (prsc
->target
== PIPE_BUFFER
) {
981 assert(prsc
->format
== PIPE_FORMAT_R8_UNORM
);
983 fdl_layout_buffer(&rsc
->layout
, size
);
985 size
= screen
->setup_slices(rsc
);
988 if (allow_ubwc
&& screen
->fill_ubwc_buffer_sizes
&& rsc
->layout
.tile_mode
)
989 size
+= screen
->fill_ubwc_buffer_sizes(rsc
);
991 /* special case for hw-query buffer, which we need to allocate before we
995 /* note, semi-intention == instead of & */
996 debug_assert(prsc
->bind
== PIPE_BIND_QUERY_BUFFER
);
1000 /* Set the layer size if the (non-a6xx) backend hasn't done so. */
1001 if (rsc
->layout
.layer_first
&& !rsc
->layout
.layer_size
) {
1002 rsc
->layout
.layer_size
= align(size
, 4096);
1003 size
= rsc
->layout
.layer_size
* prsc
->array_size
;
1006 if (fd_mesa_debug
& FD_DBG_LAYOUT
)
1007 fdl_dump_layout(&rsc
->layout
);
1009 realloc_bo(rsc
, size
);
1015 fd_resource_destroy(pscreen
, prsc
);
1019 static struct pipe_resource
*
1020 fd_resource_create(struct pipe_screen
*pscreen
,
1021 const struct pipe_resource
*tmpl
)
1023 const uint64_t mod
= DRM_FORMAT_MOD_INVALID
;
1024 return fd_resource_create_with_modifiers(pscreen
, tmpl
, &mod
, 1);
1028 is_supported_modifier(struct pipe_screen
*pscreen
, enum pipe_format pfmt
,
1033 /* Get the count of supported modifiers: */
1034 pscreen
->query_dmabuf_modifiers(pscreen
, pfmt
, 0, NULL
, NULL
, &count
);
1036 /* Get the supported modifiers: */
1037 uint64_t modifiers
[count
];
1038 pscreen
->query_dmabuf_modifiers(pscreen
, pfmt
, count
, modifiers
, NULL
, &count
);
1040 for (int i
= 0; i
< count
; i
++)
1041 if (modifiers
[i
] == mod
)
1048 * Create a texture from a winsys_handle. The handle is often created in
1049 * another process by first creating a pipe texture and then calling
1050 * resource_get_handle.
1052 static struct pipe_resource
*
1053 fd_resource_from_handle(struct pipe_screen
*pscreen
,
1054 const struct pipe_resource
*tmpl
,
1055 struct winsys_handle
*handle
, unsigned usage
)
1057 struct fd_screen
*screen
= fd_screen(pscreen
);
1058 struct fd_resource
*rsc
= CALLOC_STRUCT(fd_resource
);
1059 struct fdl_slice
*slice
= fd_resource_slice(rsc
, 0);
1060 struct pipe_resource
*prsc
= &rsc
->base
;
1061 uint32_t pitchalign
= fd_screen(pscreen
)->gmem_alignw
;
1063 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
1064 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
1065 tmpl
->target
, util_format_name(tmpl
->format
),
1066 tmpl
->width0
, tmpl
->height0
, tmpl
->depth0
,
1067 tmpl
->array_size
, tmpl
->last_level
, tmpl
->nr_samples
,
1068 tmpl
->usage
, tmpl
->bind
, tmpl
->flags
);
1074 fd_resource_layout_init(prsc
);
1076 pipe_reference_init(&prsc
->reference
, 1);
1078 prsc
->screen
= pscreen
;
1080 util_range_init(&rsc
->valid_buffer_range
);
1082 rsc
->bo
= fd_screen_bo_from_handle(pscreen
, handle
);
1086 rsc
->internal_format
= tmpl
->format
;
1087 slice
->pitch
= handle
->stride
/ rsc
->layout
.cpp
;
1088 slice
->offset
= handle
->offset
;
1089 slice
->size0
= handle
->stride
* prsc
->height0
;
1091 if ((slice
->pitch
< align(prsc
->width0
, pitchalign
)) ||
1092 (slice
->pitch
& (pitchalign
- 1)))
1095 if (handle
->modifier
== DRM_FORMAT_MOD_QCOM_COMPRESSED
) {
1096 if (!is_supported_modifier(pscreen
, tmpl
->format
,
1097 DRM_FORMAT_MOD_QCOM_COMPRESSED
)) {
1098 DBG("bad modifier: %"PRIx64
, handle
->modifier
);
1101 debug_assert(screen
->fill_ubwc_buffer_sizes
);
1102 screen
->fill_ubwc_buffer_sizes(rsc
);
1103 } else if (handle
->modifier
&&
1104 (handle
->modifier
!= DRM_FORMAT_MOD_INVALID
)) {
1108 assert(rsc
->layout
.cpp
);
1112 renderonly_create_gpu_import_for_resource(prsc
, screen
->ro
, NULL
);
1113 /* failure is expected in some cases.. */
1121 fd_resource_destroy(pscreen
, prsc
);
1126 fd_render_condition_check(struct pipe_context
*pctx
)
1128 struct fd_context
*ctx
= fd_context(pctx
);
1130 if (!ctx
->cond_query
)
1133 union pipe_query_result res
= { 0 };
1135 ctx
->cond_mode
!= PIPE_RENDER_COND_NO_WAIT
&&
1136 ctx
->cond_mode
!= PIPE_RENDER_COND_BY_REGION_NO_WAIT
;
1138 if (pctx
->get_query_result(pctx
, ctx
->cond_query
, wait
, &res
))
1139 return (bool)res
.u64
!= ctx
->cond_cond
;
1145 fd_invalidate_resource(struct pipe_context
*pctx
, struct pipe_resource
*prsc
)
1147 struct fd_context
*ctx
= fd_context(pctx
);
1148 struct fd_resource
*rsc
= fd_resource(prsc
);
1151 * TODO I guess we could track that the resource is invalidated and
1152 * use that as a hint to realloc rather than stall in _transfer_map(),
1153 * even in the non-DISCARD_WHOLE_RESOURCE case?
1155 * Note: we set dirty bits to trigger invalidate logic fd_draw_vbo
1158 if (rsc
->write_batch
) {
1159 struct fd_batch
*batch
= rsc
->write_batch
;
1160 struct pipe_framebuffer_state
*pfb
= &batch
->framebuffer
;
1162 if (pfb
->zsbuf
&& pfb
->zsbuf
->texture
== prsc
) {
1163 batch
->resolve
&= ~(FD_BUFFER_DEPTH
| FD_BUFFER_STENCIL
);
1164 ctx
->dirty
|= FD_DIRTY_ZSA
;
1167 for (unsigned i
= 0; i
< pfb
->nr_cbufs
; i
++) {
1168 if (pfb
->cbufs
[i
] && pfb
->cbufs
[i
]->texture
== prsc
) {
1169 batch
->resolve
&= ~(PIPE_CLEAR_COLOR0
<< i
);
1170 ctx
->dirty
|= FD_DIRTY_FRAMEBUFFER
;
1178 static enum pipe_format
1179 fd_resource_get_internal_format(struct pipe_resource
*prsc
)
1181 return fd_resource(prsc
)->internal_format
;
1185 fd_resource_set_stencil(struct pipe_resource
*prsc
,
1186 struct pipe_resource
*stencil
)
1188 fd_resource(prsc
)->stencil
= fd_resource(stencil
);
1191 static struct pipe_resource
*
1192 fd_resource_get_stencil(struct pipe_resource
*prsc
)
1194 struct fd_resource
*rsc
= fd_resource(prsc
);
1196 return &rsc
->stencil
->base
;
1200 static const struct u_transfer_vtbl transfer_vtbl
= {
1201 .resource_create
= fd_resource_create
,
1202 .resource_destroy
= fd_resource_destroy
,
1203 .transfer_map
= fd_resource_transfer_map
,
1204 .transfer_flush_region
= fd_resource_transfer_flush_region
,
1205 .transfer_unmap
= fd_resource_transfer_unmap
,
1206 .get_internal_format
= fd_resource_get_internal_format
,
1207 .set_stencil
= fd_resource_set_stencil
,
1208 .get_stencil
= fd_resource_get_stencil
,
1212 fd_resource_screen_init(struct pipe_screen
*pscreen
)
1214 struct fd_screen
*screen
= fd_screen(pscreen
);
1215 bool fake_rgtc
= screen
->gpu_id
< 400;
1217 pscreen
->resource_create
= u_transfer_helper_resource_create
;
1218 /* NOTE: u_transfer_helper does not yet support the _with_modifiers()
1221 pscreen
->resource_create_with_modifiers
= fd_resource_create_with_modifiers
;
1222 pscreen
->resource_from_handle
= fd_resource_from_handle
;
1223 pscreen
->resource_get_handle
= fd_resource_get_handle
;
1224 pscreen
->resource_destroy
= u_transfer_helper_resource_destroy
;
1226 pscreen
->transfer_helper
= u_transfer_helper_create(&transfer_vtbl
,
1227 true, false, fake_rgtc
, true);
1229 if (!screen
->setup_slices
)
1230 screen
->setup_slices
= fd_setup_slices
;
1234 fd_get_sample_position(struct pipe_context
*context
,
1235 unsigned sample_count
, unsigned sample_index
,
1238 /* The following is copied from nouveau/nv50 except for position
1239 * values, which are taken from blob driver */
1240 static const uint8_t pos1
[1][2] = { { 0x8, 0x8 } };
1241 static const uint8_t pos2
[2][2] = {
1242 { 0xc, 0xc }, { 0x4, 0x4 } };
1243 static const uint8_t pos4
[4][2] = {
1244 { 0x6, 0x2 }, { 0xe, 0x6 },
1245 { 0x2, 0xa }, { 0xa, 0xe } };
1246 /* TODO needs to be verified on supported hw */
1247 static const uint8_t pos8
[8][2] = {
1248 { 0x9, 0x5 }, { 0x7, 0xb },
1249 { 0xd, 0x9 }, { 0x5, 0x3 },
1250 { 0x3, 0xd }, { 0x1, 0x7 },
1251 { 0xb, 0xf }, { 0xf, 0x1 } };
1253 const uint8_t (*ptr
)[2];
1255 switch (sample_count
) {
1273 pos_out
[0] = ptr
[sample_index
][0] / 16.0f
;
1274 pos_out
[1] = ptr
[sample_index
][1] / 16.0f
;
1278 fd_blit_pipe(struct pipe_context
*pctx
, const struct pipe_blit_info
*blit_info
)
1280 /* wrap fd_blit to return void */
1281 fd_blit(pctx
, blit_info
);
1285 fd_resource_context_init(struct pipe_context
*pctx
)
1287 pctx
->transfer_map
= u_transfer_helper_transfer_map
;
1288 pctx
->transfer_flush_region
= u_transfer_helper_transfer_flush_region
;
1289 pctx
->transfer_unmap
= u_transfer_helper_transfer_unmap
;
1290 pctx
->buffer_subdata
= u_default_buffer_subdata
;
1291 pctx
->texture_subdata
= u_default_texture_subdata
;
1292 pctx
->create_surface
= fd_create_surface
;
1293 pctx
->surface_destroy
= fd_surface_destroy
;
1294 pctx
->resource_copy_region
= fd_resource_copy_region
;
1295 pctx
->blit
= fd_blit_pipe
;
1296 pctx
->flush_resource
= fd_flush_resource
;
1297 pctx
->invalidate_resource
= fd_invalidate_resource
;
1298 pctx
->get_sample_position
= fd_get_sample_position
;