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/u_format.h"
28 #include "util/u_format_rgtc.h"
29 #include "util/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"
54 * Go through the entire state and see if the resource is bound
55 * anywhere. If it is, mark the relevant state as dirty. This is
56 * called on realloc_bo to ensure the neccessary state is re-
57 * emitted so the GPU looks at the new backing bo.
60 rebind_resource(struct fd_context
*ctx
, struct pipe_resource
*prsc
)
63 for (unsigned i
= 0; i
< ctx
->vtx
.vertexbuf
.count
&& !(ctx
->dirty
& FD_DIRTY_VTXBUF
); i
++) {
64 if (ctx
->vtx
.vertexbuf
.vb
[i
].buffer
.resource
== prsc
)
65 ctx
->dirty
|= FD_DIRTY_VTXBUF
;
68 /* per-shader-stage resources: */
69 for (unsigned stage
= 0; stage
< PIPE_SHADER_TYPES
; stage
++) {
70 /* Constbufs.. note that constbuf[0] is normal uniforms emitted in
71 * cmdstream rather than by pointer..
73 const unsigned num_ubos
= util_last_bit(ctx
->constbuf
[stage
].enabled_mask
);
74 for (unsigned i
= 1; i
< num_ubos
; i
++) {
75 if (ctx
->dirty_shader
[stage
] & FD_DIRTY_SHADER_CONST
)
77 if (ctx
->constbuf
[stage
].cb
[i
].buffer
== prsc
)
78 ctx
->dirty_shader
[stage
] |= FD_DIRTY_SHADER_CONST
;
82 for (unsigned i
= 0; i
< ctx
->tex
[stage
].num_textures
; i
++) {
83 if (ctx
->dirty_shader
[stage
] & FD_DIRTY_SHADER_TEX
)
85 if (ctx
->tex
[stage
].textures
[i
] && (ctx
->tex
[stage
].textures
[i
]->texture
== prsc
))
86 ctx
->dirty_shader
[stage
] |= FD_DIRTY_SHADER_TEX
;
90 const unsigned num_images
= util_last_bit(ctx
->shaderimg
[stage
].enabled_mask
);
91 for (unsigned i
= 0; i
< num_images
; i
++) {
92 if (ctx
->dirty_shader
[stage
] & FD_DIRTY_SHADER_IMAGE
)
94 if (ctx
->shaderimg
[stage
].si
[i
].resource
== prsc
)
95 ctx
->dirty_shader
[stage
] |= FD_DIRTY_SHADER_IMAGE
;
99 const unsigned num_ssbos
= util_last_bit(ctx
->shaderbuf
[stage
].enabled_mask
);
100 for (unsigned i
= 0; i
< num_ssbos
; i
++) {
101 if (ctx
->dirty_shader
[stage
] & FD_DIRTY_SHADER_SSBO
)
103 if (ctx
->shaderbuf
[stage
].sb
[i
].buffer
== prsc
)
104 ctx
->dirty_shader
[stage
] |= FD_DIRTY_SHADER_SSBO
;
110 realloc_bo(struct fd_resource
*rsc
, uint32_t size
)
112 struct pipe_resource
*prsc
= &rsc
->base
;
113 struct fd_screen
*screen
= fd_screen(rsc
->base
.screen
);
114 uint32_t flags
= DRM_FREEDRENO_GEM_CACHE_WCOMBINE
|
115 DRM_FREEDRENO_GEM_TYPE_KMEM
|
116 COND(prsc
->bind
& PIPE_BIND_SCANOUT
, DRM_FREEDRENO_GEM_SCANOUT
);
117 /* TODO other flags? */
119 /* if we start using things other than write-combine,
120 * be sure to check for PIPE_RESOURCE_FLAG_MAP_COHERENT
126 rsc
->bo
= fd_bo_new(screen
->dev
, size
, flags
, "%ux%ux%u@%u:%x",
127 prsc
->width0
, prsc
->height0
, prsc
->depth0
, rsc
->cpp
, prsc
->bind
);
128 rsc
->seqno
= p_atomic_inc_return(&screen
->rsc_seqno
);
129 util_range_set_empty(&rsc
->valid_buffer_range
);
130 fd_bc_invalidate_resource(rsc
, true);
134 do_blit(struct fd_context
*ctx
, const struct pipe_blit_info
*blit
, bool fallback
)
136 struct pipe_context
*pctx
= &ctx
->base
;
138 /* TODO size threshold too?? */
139 if (fallback
|| !fd_blit(pctx
, blit
)) {
140 /* do blit on cpu: */
141 util_resource_copy_region(pctx
,
142 blit
->dst
.resource
, blit
->dst
.level
, blit
->dst
.box
.x
,
143 blit
->dst
.box
.y
, blit
->dst
.box
.z
,
144 blit
->src
.resource
, blit
->src
.level
, &blit
->src
.box
);
149 * @rsc: the resource to shadow
150 * @level: the level to discard (if box != NULL, otherwise ignored)
151 * @box: the box to discard (or NULL if none)
152 * @modifier: the modifier for the new buffer state
155 fd_try_shadow_resource(struct fd_context
*ctx
, struct fd_resource
*rsc
,
156 unsigned level
, const struct pipe_box
*box
, uint64_t modifier
)
158 struct pipe_context
*pctx
= &ctx
->base
;
159 struct pipe_resource
*prsc
= &rsc
->base
;
160 bool fallback
= false;
165 /* TODO: somehow munge dimensions and format to copy unsupported
166 * render target format to something that is supported?
168 if (!pctx
->screen
->is_format_supported(pctx
->screen
,
169 prsc
->format
, prsc
->target
, prsc
->nr_samples
,
170 prsc
->nr_storage_samples
,
171 PIPE_BIND_RENDER_TARGET
))
174 /* do shadowing back-blits on the cpu for buffers: */
175 if (prsc
->target
== PIPE_BUFFER
)
178 bool discard_whole_level
= box
&& util_texrange_covers_whole_level(prsc
, level
,
179 box
->x
, box
->y
, box
->z
, box
->width
, box
->height
, box
->depth
);
181 /* TODO need to be more clever about current level */
182 if ((prsc
->target
>= PIPE_TEXTURE_2D
) && box
&& !discard_whole_level
)
185 struct pipe_resource
*pshadow
=
186 pctx
->screen
->resource_create_with_modifiers(pctx
->screen
,
192 assert(!ctx
->in_shadow
);
193 ctx
->in_shadow
= true;
195 /* get rid of any references that batch-cache might have to us (which
196 * should empty/destroy rsc->batches hashset)
198 fd_bc_invalidate_resource(rsc
, false);
200 mtx_lock(&ctx
->screen
->lock
);
202 /* Swap the backing bo's, so shadow becomes the old buffer,
203 * blit from shadow to new buffer. From here on out, we
206 * Note that we need to do it in this order, otherwise if
207 * we go down cpu blit path, the recursive transfer_map()
208 * sees the wrong status..
210 struct fd_resource
*shadow
= fd_resource(pshadow
);
212 DBG("shadow: %p (%d) -> %p (%d)\n", rsc
, rsc
->base
.reference
.count
,
213 shadow
, shadow
->base
.reference
.count
);
215 /* TODO valid_buffer_range?? */
216 swap(rsc
->bo
, shadow
->bo
);
217 swap(rsc
->write_batch
, shadow
->write_batch
);
218 swap(rsc
->offset
, shadow
->offset
);
219 swap(rsc
->ubwc_offset
, shadow
->ubwc_offset
);
220 swap(rsc
->ubwc_pitch
, shadow
->ubwc_pitch
);
221 swap(rsc
->ubwc_size
, shadow
->ubwc_size
);
222 rsc
->seqno
= p_atomic_inc_return(&ctx
->screen
->rsc_seqno
);
224 /* at this point, the newly created shadow buffer is not referenced
225 * by any batches, but the existing rsc (probably) is. We need to
226 * transfer those references over:
228 debug_assert(shadow
->batch_mask
== 0);
229 struct fd_batch
*batch
;
230 foreach_batch(batch
, &ctx
->screen
->batch_cache
, rsc
->batch_mask
) {
231 struct set_entry
*entry
= _mesa_set_search(batch
->resources
, rsc
);
232 _mesa_set_remove(batch
->resources
, entry
);
233 _mesa_set_add(batch
->resources
, shadow
);
235 swap(rsc
->batch_mask
, shadow
->batch_mask
);
237 mtx_unlock(&ctx
->screen
->lock
);
239 struct pipe_blit_info blit
= {};
240 blit
.dst
.resource
= prsc
;
241 blit
.dst
.format
= prsc
->format
;
242 blit
.src
.resource
= pshadow
;
243 blit
.src
.format
= pshadow
->format
;
244 blit
.mask
= util_format_get_mask(prsc
->format
);
245 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
247 #define set_box(field, val) do { \
248 blit.dst.field = (val); \
249 blit.src.field = (val); \
252 /* blit the other levels in their entirety: */
253 for (unsigned l
= 0; l
<= prsc
->last_level
; l
++) {
254 if (box
&& l
== level
)
257 /* just blit whole level: */
259 set_box(box
.width
, u_minify(prsc
->width0
, l
));
260 set_box(box
.height
, u_minify(prsc
->height0
, l
));
261 set_box(box
.depth
, u_minify(prsc
->depth0
, l
));
263 do_blit(ctx
, &blit
, fallback
);
266 /* deal w/ current level specially, since we might need to split
267 * it up into a couple blits:
269 if (box
&& !discard_whole_level
) {
270 set_box(level
, level
);
272 switch (prsc
->target
) {
274 case PIPE_TEXTURE_1D
:
277 set_box(box
.height
, 1);
278 set_box(box
.depth
, 1);
282 set_box(box
.width
, box
->x
);
284 do_blit(ctx
, &blit
, fallback
);
286 if ((box
->x
+ box
->width
) < u_minify(prsc
->width0
, level
)) {
287 set_box(box
.x
, box
->x
+ box
->width
);
288 set_box(box
.width
, u_minify(prsc
->width0
, level
) - (box
->x
+ box
->width
));
290 do_blit(ctx
, &blit
, fallback
);
293 case PIPE_TEXTURE_2D
:
300 ctx
->in_shadow
= false;
302 pipe_resource_reference(&pshadow
, NULL
);
307 static struct fd_resource
*
308 fd_alloc_staging(struct fd_context
*ctx
, struct fd_resource
*rsc
,
309 unsigned level
, const struct pipe_box
*box
)
311 struct pipe_context
*pctx
= &ctx
->base
;
312 struct pipe_resource tmpl
= rsc
->base
;
314 tmpl
.width0
= box
->width
;
315 tmpl
.height0
= box
->height
;
316 /* for array textures, box->depth is the array_size, otherwise
317 * for 3d textures, it is the depth:
319 if (tmpl
.array_size
> 1) {
320 tmpl
.array_size
= box
->depth
;
324 tmpl
.depth0
= box
->depth
;
327 tmpl
.bind
|= PIPE_BIND_LINEAR
;
329 struct pipe_resource
*pstaging
=
330 pctx
->screen
->resource_create(pctx
->screen
, &tmpl
);
334 return fd_resource(pstaging
);
338 fd_blit_from_staging(struct fd_context
*ctx
, struct fd_transfer
*trans
)
340 struct pipe_resource
*dst
= trans
->base
.resource
;
341 struct pipe_blit_info blit
= {};
343 blit
.dst
.resource
= dst
;
344 blit
.dst
.format
= dst
->format
;
345 blit
.dst
.level
= trans
->base
.level
;
346 blit
.dst
.box
= trans
->base
.box
;
347 blit
.src
.resource
= trans
->staging_prsc
;
348 blit
.src
.format
= trans
->staging_prsc
->format
;
350 blit
.src
.box
= trans
->staging_box
;
351 blit
.mask
= util_format_get_mask(trans
->staging_prsc
->format
);
352 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
354 do_blit(ctx
, &blit
, false);
358 fd_blit_to_staging(struct fd_context
*ctx
, struct fd_transfer
*trans
)
360 struct pipe_resource
*src
= trans
->base
.resource
;
361 struct pipe_blit_info blit
= {};
363 blit
.src
.resource
= src
;
364 blit
.src
.format
= src
->format
;
365 blit
.src
.level
= trans
->base
.level
;
366 blit
.src
.box
= trans
->base
.box
;
367 blit
.dst
.resource
= trans
->staging_prsc
;
368 blit
.dst
.format
= trans
->staging_prsc
->format
;
370 blit
.dst
.box
= trans
->staging_box
;
371 blit
.mask
= util_format_get_mask(trans
->staging_prsc
->format
);
372 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
374 do_blit(ctx
, &blit
, false);
377 static void fd_resource_transfer_flush_region(struct pipe_context
*pctx
,
378 struct pipe_transfer
*ptrans
,
379 const struct pipe_box
*box
)
381 struct fd_resource
*rsc
= fd_resource(ptrans
->resource
);
383 if (ptrans
->resource
->target
== PIPE_BUFFER
)
384 util_range_add(&rsc
->valid_buffer_range
,
385 ptrans
->box
.x
+ box
->x
,
386 ptrans
->box
.x
+ box
->x
+ box
->width
);
390 flush_resource(struct fd_context
*ctx
, struct fd_resource
*rsc
, unsigned usage
)
392 struct fd_batch
*write_batch
= NULL
;
394 mtx_lock(&ctx
->screen
->lock
);
395 fd_batch_reference_locked(&write_batch
, rsc
->write_batch
);
396 mtx_unlock(&ctx
->screen
->lock
);
398 if (usage
& PIPE_TRANSFER_WRITE
) {
399 struct fd_batch
*batch
, *batches
[32] = {};
402 /* This is a bit awkward, probably a fd_batch_flush_locked()
403 * would make things simpler.. but we need to hold the lock
404 * to iterate the batches which reference this resource. So
405 * we must first grab references under a lock, then flush.
407 mtx_lock(&ctx
->screen
->lock
);
408 batch_mask
= rsc
->batch_mask
;
409 foreach_batch(batch
, &ctx
->screen
->batch_cache
, batch_mask
)
410 fd_batch_reference_locked(&batches
[batch
->idx
], batch
);
411 mtx_unlock(&ctx
->screen
->lock
);
413 foreach_batch(batch
, &ctx
->screen
->batch_cache
, batch_mask
)
414 fd_batch_flush(batch
, false, false);
416 foreach_batch(batch
, &ctx
->screen
->batch_cache
, batch_mask
) {
417 fd_batch_sync(batch
);
418 fd_batch_reference(&batches
[batch
->idx
], NULL
);
420 assert(rsc
->batch_mask
== 0);
421 } else if (write_batch
) {
422 fd_batch_flush(write_batch
, true, false);
425 fd_batch_reference(&write_batch
, NULL
);
427 assert(!rsc
->write_batch
);
431 fd_flush_resource(struct pipe_context
*pctx
, struct pipe_resource
*prsc
)
433 flush_resource(fd_context(pctx
), fd_resource(prsc
), PIPE_TRANSFER_READ
);
437 fd_resource_transfer_unmap(struct pipe_context
*pctx
,
438 struct pipe_transfer
*ptrans
)
440 struct fd_context
*ctx
= fd_context(pctx
);
441 struct fd_resource
*rsc
= fd_resource(ptrans
->resource
);
442 struct fd_transfer
*trans
= fd_transfer(ptrans
);
444 if (trans
->staging_prsc
) {
445 if (ptrans
->usage
& PIPE_TRANSFER_WRITE
)
446 fd_blit_from_staging(ctx
, trans
);
447 pipe_resource_reference(&trans
->staging_prsc
, NULL
);
450 if (!(ptrans
->usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
451 fd_bo_cpu_fini(rsc
->bo
);
454 util_range_add(&rsc
->valid_buffer_range
,
456 ptrans
->box
.x
+ ptrans
->box
.width
);
458 pipe_resource_reference(&ptrans
->resource
, NULL
);
459 slab_free(&ctx
->transfer_pool
, ptrans
);
463 fd_resource_transfer_map(struct pipe_context
*pctx
,
464 struct pipe_resource
*prsc
,
465 unsigned level
, unsigned usage
,
466 const struct pipe_box
*box
,
467 struct pipe_transfer
**pptrans
)
469 struct fd_context
*ctx
= fd_context(pctx
);
470 struct fd_resource
*rsc
= fd_resource(prsc
);
471 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, level
);
472 struct fd_transfer
*trans
;
473 struct pipe_transfer
*ptrans
;
474 enum pipe_format format
= prsc
->format
;
480 DBG("prsc=%p, level=%u, usage=%x, box=%dx%d+%d,%d", prsc
, level
, usage
,
481 box
->width
, box
->height
, box
->x
, box
->y
);
483 ptrans
= slab_alloc(&ctx
->transfer_pool
);
487 /* slab_alloc_st() doesn't zero: */
488 trans
= fd_transfer(ptrans
);
489 memset(trans
, 0, sizeof(*trans
));
491 pipe_resource_reference(&ptrans
->resource
, prsc
);
492 ptrans
->level
= level
;
493 ptrans
->usage
= usage
;
495 ptrans
->stride
= util_format_get_nblocksx(format
, slice
->pitch
) * rsc
->cpp
;
496 ptrans
->layer_stride
= rsc
->layer_first
? rsc
->layer_size
: slice
->size0
;
498 /* we always need a staging texture for tiled buffers:
500 * TODO we might sometimes want to *also* shadow the resource to avoid
501 * splitting a batch.. for ex, mid-frame texture uploads to a tiled
504 if (rsc
->tile_mode
) {
505 struct fd_resource
*staging_rsc
;
507 staging_rsc
= fd_alloc_staging(ctx
, rsc
, level
, box
);
509 // TODO for PIPE_TRANSFER_READ, need to do untiling blit..
510 trans
->staging_prsc
= &staging_rsc
->base
;
511 trans
->base
.stride
= util_format_get_nblocksx(format
,
512 staging_rsc
->slices
[0].pitch
) * staging_rsc
->cpp
;
513 trans
->base
.layer_stride
= staging_rsc
->layer_first
?
514 staging_rsc
->layer_size
: staging_rsc
->slices
[0].size0
;
515 trans
->staging_box
= *box
;
516 trans
->staging_box
.x
= 0;
517 trans
->staging_box
.y
= 0;
518 trans
->staging_box
.z
= 0;
520 if (usage
& PIPE_TRANSFER_READ
) {
521 fd_blit_to_staging(ctx
, trans
);
523 struct fd_batch
*batch
= NULL
;
525 fd_context_lock(ctx
);
526 fd_batch_reference_locked(&batch
, staging_rsc
->write_batch
);
527 fd_context_unlock(ctx
);
529 /* we can't fd_bo_cpu_prep() until the blit to staging
530 * is submitted to kernel.. in that case write_batch
531 * wouldn't be NULL yet:
534 fd_batch_sync(batch
);
535 fd_batch_reference(&batch
, NULL
);
538 fd_bo_cpu_prep(staging_rsc
->bo
, ctx
->pipe
,
539 DRM_FREEDRENO_PREP_READ
);
542 buf
= fd_bo_map(staging_rsc
->bo
);
547 ctx
->stats
.staging_uploads
++;
553 if (ctx
->in_shadow
&& !(usage
& PIPE_TRANSFER_READ
))
554 usage
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
556 if (usage
& PIPE_TRANSFER_READ
)
557 op
|= DRM_FREEDRENO_PREP_READ
;
559 if (usage
& PIPE_TRANSFER_WRITE
)
560 op
|= DRM_FREEDRENO_PREP_WRITE
;
562 if (usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) {
563 realloc_bo(rsc
, fd_bo_size(rsc
->bo
));
564 rebind_resource(ctx
, prsc
);
565 } else if ((usage
& PIPE_TRANSFER_WRITE
) &&
566 prsc
->target
== PIPE_BUFFER
&&
567 !util_ranges_intersect(&rsc
->valid_buffer_range
,
568 box
->x
, box
->x
+ box
->width
)) {
569 /* We are trying to write to a previously uninitialized range. No need
572 } else if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
573 struct fd_batch
*write_batch
= NULL
;
575 /* hold a reference, so it doesn't disappear under us: */
576 fd_context_lock(ctx
);
577 fd_batch_reference_locked(&write_batch
, rsc
->write_batch
);
578 fd_context_unlock(ctx
);
580 if ((usage
& PIPE_TRANSFER_WRITE
) && write_batch
&&
581 write_batch
->back_blit
) {
582 /* if only thing pending is a back-blit, we can discard it: */
583 fd_batch_reset(write_batch
);
586 /* If the GPU is writing to the resource, or if it is reading from the
587 * resource and we're trying to write to it, flush the renders.
589 bool needs_flush
= pending(rsc
, !!(usage
& PIPE_TRANSFER_WRITE
));
590 bool busy
= needs_flush
|| (0 != fd_bo_cpu_prep(rsc
->bo
,
591 ctx
->pipe
, op
| DRM_FREEDRENO_PREP_NOSYNC
));
593 /* if we need to flush/stall, see if we can make a shadow buffer
596 * TODO we could go down this path !reorder && !busy_for_read
597 * ie. we only *don't* want to go down this path if the blit
598 * will trigger a flush!
600 if (ctx
->screen
->reorder
&& busy
&& !(usage
& PIPE_TRANSFER_READ
) &&
601 (usage
& PIPE_TRANSFER_DISCARD_RANGE
)) {
602 /* try shadowing only if it avoids a flush, otherwise staging would
605 if (needs_flush
&& fd_try_shadow_resource(ctx
, rsc
, level
,
606 box
, DRM_FORMAT_MOD_LINEAR
)) {
607 needs_flush
= busy
= false;
608 rebind_resource(ctx
, prsc
);
609 ctx
->stats
.shadow_uploads
++;
611 struct fd_resource
*staging_rsc
;
614 flush_resource(ctx
, rsc
, usage
);
618 /* in this case, we don't need to shadow the whole resource,
619 * since any draw that references the previous contents has
620 * already had rendering flushed for all tiles. So we can
621 * use a staging buffer to do the upload.
623 staging_rsc
= fd_alloc_staging(ctx
, rsc
, level
, box
);
625 trans
->staging_prsc
= &staging_rsc
->base
;
626 trans
->base
.stride
= util_format_get_nblocksx(format
,
627 staging_rsc
->slices
[0].pitch
) * staging_rsc
->cpp
;
628 trans
->base
.layer_stride
= staging_rsc
->layer_first
?
629 staging_rsc
->layer_size
: staging_rsc
->slices
[0].size0
;
630 trans
->staging_box
= *box
;
631 trans
->staging_box
.x
= 0;
632 trans
->staging_box
.y
= 0;
633 trans
->staging_box
.z
= 0;
634 buf
= fd_bo_map(staging_rsc
->bo
);
639 fd_batch_reference(&write_batch
, NULL
);
641 ctx
->stats
.staging_uploads
++;
649 flush_resource(ctx
, rsc
, usage
);
653 fd_batch_reference(&write_batch
, NULL
);
655 /* The GPU keeps track of how the various bo's are being used, and
656 * will wait if necessary for the proper operation to have
660 ret
= fd_bo_cpu_prep(rsc
->bo
, ctx
->pipe
, op
);
666 buf
= fd_bo_map(rsc
->bo
);
668 box
->y
/ util_format_get_blockheight(format
) * ptrans
->stride
+
669 box
->x
/ util_format_get_blockwidth(format
) * rsc
->cpp
+
670 fd_resource_offset(rsc
, level
, box
->z
);
672 if (usage
& PIPE_TRANSFER_WRITE
)
680 fd_resource_transfer_unmap(pctx
, ptrans
);
685 fd_resource_destroy(struct pipe_screen
*pscreen
,
686 struct pipe_resource
*prsc
)
688 struct fd_resource
*rsc
= fd_resource(prsc
);
689 fd_bc_invalidate_resource(rsc
, true);
693 renderonly_scanout_destroy(rsc
->scanout
, fd_screen(pscreen
)->ro
);
695 util_range_destroy(&rsc
->valid_buffer_range
);
700 fd_resource_modifier(struct fd_resource
*rsc
)
703 return DRM_FORMAT_MOD_LINEAR
;
706 return DRM_FORMAT_MOD_QCOM_COMPRESSED
;
708 /* TODO invent a modifier for tiled but not UBWC buffers: */
709 return DRM_FORMAT_MOD_INVALID
;
713 fd_resource_get_handle(struct pipe_screen
*pscreen
,
714 struct pipe_context
*pctx
,
715 struct pipe_resource
*prsc
,
716 struct winsys_handle
*handle
,
719 struct fd_resource
*rsc
= fd_resource(prsc
);
721 handle
->modifier
= fd_resource_modifier(rsc
);
723 return fd_screen_bo_get_handle(pscreen
, rsc
->bo
, rsc
->scanout
,
724 rsc
->slices
[0].pitch
* rsc
->cpp
, handle
);
728 setup_slices(struct fd_resource
*rsc
, uint32_t alignment
, enum pipe_format format
)
730 struct pipe_resource
*prsc
= &rsc
->base
;
731 struct fd_screen
*screen
= fd_screen(prsc
->screen
);
732 enum util_format_layout layout
= util_format_description(format
)->layout
;
733 uint32_t pitchalign
= screen
->gmem_alignw
;
734 uint32_t level
, size
= 0;
735 uint32_t width
= prsc
->width0
;
736 uint32_t height
= prsc
->height0
;
737 uint32_t depth
= prsc
->depth0
;
738 /* in layer_first layout, the level (slice) contains just one
739 * layer (since in fact the layer contains the slices)
741 uint32_t layers_in_level
= rsc
->layer_first
? 1 : prsc
->array_size
;
743 for (level
= 0; level
<= prsc
->last_level
; level
++) {
744 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, level
);
747 if (layout
== UTIL_FORMAT_LAYOUT_ASTC
)
748 slice
->pitch
= width
=
749 util_align_npot(width
, pitchalign
* util_format_get_blockwidth(format
));
751 slice
->pitch
= width
= align(width
, pitchalign
);
752 slice
->offset
= size
;
753 blocks
= util_format_get_nblocks(format
, width
, height
);
754 /* 1d array and 2d array textures must all have the same layer size
755 * for each miplevel on a3xx. 3d textures can have different layer
756 * sizes for high levels, but the hw auto-sizer is buggy (or at least
757 * different than what this code does), so as soon as the layer size
758 * range gets into range, we stop reducing it.
760 if (prsc
->target
== PIPE_TEXTURE_3D
&& (
762 (level
> 1 && rsc
->slices
[level
- 1].size0
> 0xf000)))
763 slice
->size0
= align(blocks
* rsc
->cpp
, alignment
);
764 else if (level
== 0 || rsc
->layer_first
|| alignment
== 1)
765 slice
->size0
= align(blocks
* rsc
->cpp
, alignment
);
767 slice
->size0
= rsc
->slices
[level
- 1].size0
;
769 size
+= slice
->size0
* depth
* layers_in_level
;
771 width
= u_minify(width
, 1);
772 height
= u_minify(height
, 1);
773 depth
= u_minify(depth
, 1);
780 slice_alignment(enum pipe_texture_target target
)
782 /* on a3xx, 2d array and 3d textures seem to want their
783 * layers aligned to page boundaries:
786 case PIPE_TEXTURE_3D
:
787 case PIPE_TEXTURE_1D_ARRAY
:
788 case PIPE_TEXTURE_2D_ARRAY
:
795 /* cross generation texture layout to plug in to screen->setup_slices()..
796 * replace with generation specific one as-needed.
798 * TODO for a4xx probably can extract out the a4xx specific logic int
799 * a small fd4_setup_slices() wrapper that sets up layer_first, and then
803 fd_setup_slices(struct fd_resource
*rsc
)
807 alignment
= slice_alignment(rsc
->base
.target
);
809 struct fd_screen
*screen
= fd_screen(rsc
->base
.screen
);
810 if (is_a4xx(screen
)) {
811 switch (rsc
->base
.target
) {
812 case PIPE_TEXTURE_3D
:
813 rsc
->layer_first
= false;
816 rsc
->layer_first
= true;
822 return setup_slices(rsc
, alignment
, rsc
->base
.format
);
825 /* special case to resize query buf after allocated.. */
827 fd_resource_resize(struct pipe_resource
*prsc
, uint32_t sz
)
829 struct fd_resource
*rsc
= fd_resource(prsc
);
831 debug_assert(prsc
->width0
== 0);
832 debug_assert(prsc
->target
== PIPE_BUFFER
);
833 debug_assert(prsc
->bind
== PIPE_BIND_QUERY_BUFFER
);
836 realloc_bo(rsc
, fd_screen(prsc
->screen
)->setup_slices(rsc
));
839 // TODO common helper?
841 has_depth(enum pipe_format format
)
844 case PIPE_FORMAT_Z16_UNORM
:
845 case PIPE_FORMAT_Z32_UNORM
:
846 case PIPE_FORMAT_Z32_FLOAT
:
847 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
848 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
849 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
850 case PIPE_FORMAT_Z24X8_UNORM
:
851 case PIPE_FORMAT_X8Z24_UNORM
:
859 * Create a new texture object, using the given template info.
861 static struct pipe_resource
*
862 fd_resource_create_with_modifiers(struct pipe_screen
*pscreen
,
863 const struct pipe_resource
*tmpl
,
864 const uint64_t *modifiers
, int count
)
866 struct fd_screen
*screen
= fd_screen(pscreen
);
867 struct fd_resource
*rsc
;
868 struct pipe_resource
*prsc
;
869 enum pipe_format format
= tmpl
->format
;
872 /* when using kmsro, scanout buffers are allocated on the display device
873 * create_with_modifiers() doesn't give us usage flags, so we have to
874 * assume that all calls with modifiers are scanout-possible
877 ((tmpl
->bind
& PIPE_BIND_SCANOUT
) ||
878 !(count
== 1 && modifiers
[0] == DRM_FORMAT_MOD_INVALID
))) {
879 struct pipe_resource scanout_templat
= *tmpl
;
880 struct renderonly_scanout
*scanout
;
881 struct winsys_handle handle
;
883 scanout
= renderonly_scanout_for_resource(&scanout_templat
,
884 screen
->ro
, &handle
);
888 renderonly_scanout_destroy(scanout
, screen
->ro
);
890 assert(handle
.type
== WINSYS_HANDLE_TYPE_FD
);
891 rsc
= fd_resource(pscreen
->resource_from_handle(pscreen
, tmpl
,
893 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE
));
894 close(handle
.handle
);
901 rsc
= CALLOC_STRUCT(fd_resource
);
904 DBG("%p: target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
905 "nr_samples=%u, usage=%u, bind=%x, flags=%x", prsc
,
906 tmpl
->target
, util_format_name(format
),
907 tmpl
->width0
, tmpl
->height0
, tmpl
->depth0
,
908 tmpl
->array_size
, tmpl
->last_level
, tmpl
->nr_samples
,
909 tmpl
->usage
, tmpl
->bind
, tmpl
->flags
);
917 (PIPE_BIND_SCANOUT | \
919 PIPE_BIND_DISPLAY_TARGET)
921 bool linear
= drm_find_modifier(DRM_FORMAT_MOD_LINEAR
, modifiers
, count
);
922 if (tmpl
->bind
& LINEAR
)
925 /* Normally, for non-shared buffers, allow buffer compression if
926 * not shared, otherwise only allow if QCOM_COMPRESSED modifier
929 * TODO we should probably also limit tiled in a similar way,
930 * except we don't have a format modifier for tiled. (We probably
933 bool allow_ubwc
= drm_find_modifier(DRM_FORMAT_MOD_INVALID
, modifiers
, count
);
934 if (tmpl
->bind
& PIPE_BIND_SHARED
)
935 allow_ubwc
= drm_find_modifier(DRM_FORMAT_MOD_QCOM_COMPRESSED
, modifiers
, count
);
937 /* TODO turn on UBWC for all internal buffers
939 * There are still some regressions in deqp with UBWC enabled. I
940 * think it is mostly related to sampler/image views using a format
941 * that doesn't support compression with a resource created with
942 * a format that does. We need to track the compression state of
943 * a buffer and do an (in-place, hopefully?) resolve if it is re-
944 * interpreted with a format that does not support compression.
946 * It is possible (likely?) that we can't do atomic ops on a
947 * compressed buffer as well, so this would also require transition
948 * to a compressed state.
950 allow_ubwc
&= !!(fd_mesa_debug
& FD_DBG_UBWC
);
952 if (screen
->tile_mode
&&
953 (tmpl
->target
!= PIPE_BUFFER
) &&
955 rsc
->tile_mode
= screen
->tile_mode(tmpl
);
958 pipe_reference_init(&prsc
->reference
, 1);
960 prsc
->screen
= pscreen
;
962 util_range_init(&rsc
->valid_buffer_range
);
964 rsc
->internal_format
= format
;
965 rsc
->cpp
= util_format_get_blocksize(format
);
966 rsc
->cpp
*= fd_resource_nr_samples(prsc
);
970 // XXX probably need some extra work if we hit rsc shadowing path w/ lrz..
971 if ((is_a5xx(screen
) || is_a6xx(screen
)) &&
972 (fd_mesa_debug
& FD_DBG_LRZ
) && has_depth(format
)) {
973 const uint32_t flags
= DRM_FREEDRENO_GEM_CACHE_WCOMBINE
|
974 DRM_FREEDRENO_GEM_TYPE_KMEM
; /* TODO */
975 unsigned lrz_pitch
= align(DIV_ROUND_UP(tmpl
->width0
, 8), 64);
976 unsigned lrz_height
= DIV_ROUND_UP(tmpl
->height0
, 8);
978 /* LRZ buffer is super-sampled: */
979 switch (prsc
->nr_samples
) {
986 unsigned size
= lrz_pitch
* lrz_height
* 2;
988 size
+= 0x1000; /* for GRAS_LRZ_FAST_CLEAR_BUFFER */
990 rsc
->lrz_height
= lrz_height
;
991 rsc
->lrz_width
= lrz_pitch
;
992 rsc
->lrz_pitch
= lrz_pitch
;
993 rsc
->lrz
= fd_bo_new(screen
->dev
, size
, flags
, "lrz");
996 size
= screen
->setup_slices(rsc
);
998 if (allow_ubwc
&& screen
->fill_ubwc_buffer_sizes
&& rsc
->tile_mode
)
999 size
+= screen
->fill_ubwc_buffer_sizes(rsc
);
1001 /* special case for hw-query buffer, which we need to allocate before we
1005 /* note, semi-intention == instead of & */
1006 debug_assert(prsc
->bind
== PIPE_BIND_QUERY_BUFFER
);
1010 if (rsc
->layer_first
) {
1011 rsc
->layer_size
= align(size
, 4096);
1012 size
= rsc
->layer_size
* prsc
->array_size
;
1015 realloc_bo(rsc
, size
);
1021 fd_resource_destroy(pscreen
, prsc
);
1025 static struct pipe_resource
*
1026 fd_resource_create(struct pipe_screen
*pscreen
,
1027 const struct pipe_resource
*tmpl
)
1029 const uint64_t mod
= DRM_FORMAT_MOD_INVALID
;
1030 return fd_resource_create_with_modifiers(pscreen
, tmpl
, &mod
, 1);
1034 is_supported_modifier(struct pipe_screen
*pscreen
, enum pipe_format pfmt
,
1039 /* Get the count of supported modifiers: */
1040 pscreen
->query_dmabuf_modifiers(pscreen
, pfmt
, 0, NULL
, NULL
, &count
);
1042 /* Get the supported modifiers: */
1043 uint64_t modifiers
[count
];
1044 pscreen
->query_dmabuf_modifiers(pscreen
, pfmt
, count
, modifiers
, NULL
, &count
);
1046 for (int i
= 0; i
< count
; i
++)
1047 if (modifiers
[i
] == mod
)
1054 * Create a texture from a winsys_handle. The handle is often created in
1055 * another process by first creating a pipe texture and then calling
1056 * resource_get_handle.
1058 static struct pipe_resource
*
1059 fd_resource_from_handle(struct pipe_screen
*pscreen
,
1060 const struct pipe_resource
*tmpl
,
1061 struct winsys_handle
*handle
, unsigned usage
)
1063 struct fd_screen
*screen
= fd_screen(pscreen
);
1064 struct fd_resource
*rsc
= CALLOC_STRUCT(fd_resource
);
1065 struct fd_resource_slice
*slice
= &rsc
->slices
[0];
1066 struct pipe_resource
*prsc
= &rsc
->base
;
1067 uint32_t pitchalign
= fd_screen(pscreen
)->gmem_alignw
;
1069 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
1070 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
1071 tmpl
->target
, util_format_name(tmpl
->format
),
1072 tmpl
->width0
, tmpl
->height0
, tmpl
->depth0
,
1073 tmpl
->array_size
, tmpl
->last_level
, tmpl
->nr_samples
,
1074 tmpl
->usage
, tmpl
->bind
, tmpl
->flags
);
1081 pipe_reference_init(&prsc
->reference
, 1);
1083 prsc
->screen
= pscreen
;
1085 util_range_init(&rsc
->valid_buffer_range
);
1087 rsc
->bo
= fd_screen_bo_from_handle(pscreen
, handle
);
1091 rsc
->internal_format
= tmpl
->format
;
1092 rsc
->cpp
= util_format_get_blocksize(tmpl
->format
);
1093 rsc
->cpp
*= fd_resource_nr_samples(prsc
);
1094 slice
->pitch
= handle
->stride
/ rsc
->cpp
;
1095 slice
->offset
= handle
->offset
;
1096 slice
->size0
= handle
->stride
* prsc
->height0
;
1098 if ((slice
->pitch
< align(prsc
->width0
, pitchalign
)) ||
1099 (slice
->pitch
& (pitchalign
- 1)))
1102 if (handle
->modifier
== DRM_FORMAT_MOD_QCOM_COMPRESSED
) {
1103 if (!is_supported_modifier(pscreen
, tmpl
->format
,
1104 DRM_FORMAT_MOD_QCOM_COMPRESSED
)) {
1105 DBG("bad modifier: %"PRIx64
, handle
->modifier
);
1108 debug_assert(screen
->fill_ubwc_buffer_sizes
);
1109 screen
->fill_ubwc_buffer_sizes(rsc
);
1110 } else if (handle
->modifier
&&
1111 (handle
->modifier
!= DRM_FORMAT_MOD_INVALID
)) {
1119 renderonly_create_gpu_import_for_resource(prsc
, screen
->ro
, NULL
);
1120 /* failure is expected in some cases.. */
1128 fd_resource_destroy(pscreen
, prsc
);
1133 fd_render_condition_check(struct pipe_context
*pctx
)
1135 struct fd_context
*ctx
= fd_context(pctx
);
1137 if (!ctx
->cond_query
)
1140 union pipe_query_result res
= { 0 };
1142 ctx
->cond_mode
!= PIPE_RENDER_COND_NO_WAIT
&&
1143 ctx
->cond_mode
!= PIPE_RENDER_COND_BY_REGION_NO_WAIT
;
1145 if (pctx
->get_query_result(pctx
, ctx
->cond_query
, wait
, &res
))
1146 return (bool)res
.u64
!= ctx
->cond_cond
;
1152 fd_invalidate_resource(struct pipe_context
*pctx
, struct pipe_resource
*prsc
)
1154 struct fd_context
*ctx
= fd_context(pctx
);
1155 struct fd_resource
*rsc
= fd_resource(prsc
);
1158 * TODO I guess we could track that the resource is invalidated and
1159 * use that as a hint to realloc rather than stall in _transfer_map(),
1160 * even in the non-DISCARD_WHOLE_RESOURCE case?
1162 * Note: we set dirty bits to trigger invalidate logic fd_draw_vbo
1165 if (rsc
->write_batch
) {
1166 struct fd_batch
*batch
= rsc
->write_batch
;
1167 struct pipe_framebuffer_state
*pfb
= &batch
->framebuffer
;
1169 if (pfb
->zsbuf
&& pfb
->zsbuf
->texture
== prsc
) {
1170 batch
->resolve
&= ~(FD_BUFFER_DEPTH
| FD_BUFFER_STENCIL
);
1171 ctx
->dirty
|= FD_DIRTY_ZSA
;
1174 for (unsigned i
= 0; i
< pfb
->nr_cbufs
; i
++) {
1175 if (pfb
->cbufs
[i
] && pfb
->cbufs
[i
]->texture
== prsc
) {
1176 batch
->resolve
&= ~(PIPE_CLEAR_COLOR0
<< i
);
1177 ctx
->dirty
|= FD_DIRTY_FRAMEBUFFER
;
1185 static enum pipe_format
1186 fd_resource_get_internal_format(struct pipe_resource
*prsc
)
1188 return fd_resource(prsc
)->internal_format
;
1192 fd_resource_set_stencil(struct pipe_resource
*prsc
,
1193 struct pipe_resource
*stencil
)
1195 fd_resource(prsc
)->stencil
= fd_resource(stencil
);
1198 static struct pipe_resource
*
1199 fd_resource_get_stencil(struct pipe_resource
*prsc
)
1201 struct fd_resource
*rsc
= fd_resource(prsc
);
1203 return &rsc
->stencil
->base
;
1207 static const struct u_transfer_vtbl transfer_vtbl
= {
1208 .resource_create
= fd_resource_create
,
1209 .resource_destroy
= fd_resource_destroy
,
1210 .transfer_map
= fd_resource_transfer_map
,
1211 .transfer_flush_region
= fd_resource_transfer_flush_region
,
1212 .transfer_unmap
= fd_resource_transfer_unmap
,
1213 .get_internal_format
= fd_resource_get_internal_format
,
1214 .set_stencil
= fd_resource_set_stencil
,
1215 .get_stencil
= fd_resource_get_stencil
,
1219 fd_resource_screen_init(struct pipe_screen
*pscreen
)
1221 struct fd_screen
*screen
= fd_screen(pscreen
);
1222 bool fake_rgtc
= screen
->gpu_id
< 400;
1224 pscreen
->resource_create
= u_transfer_helper_resource_create
;
1225 /* NOTE: u_transfer_helper does not yet support the _with_modifiers()
1228 pscreen
->resource_create_with_modifiers
= fd_resource_create_with_modifiers
;
1229 pscreen
->resource_from_handle
= fd_resource_from_handle
;
1230 pscreen
->resource_get_handle
= fd_resource_get_handle
;
1231 pscreen
->resource_destroy
= u_transfer_helper_resource_destroy
;
1233 pscreen
->transfer_helper
= u_transfer_helper_create(&transfer_vtbl
,
1234 true, false, fake_rgtc
, true);
1236 if (!screen
->setup_slices
)
1237 screen
->setup_slices
= fd_setup_slices
;
1241 fd_get_sample_position(struct pipe_context
*context
,
1242 unsigned sample_count
, unsigned sample_index
,
1245 /* The following is copied from nouveau/nv50 except for position
1246 * values, which are taken from blob driver */
1247 static const uint8_t pos1
[1][2] = { { 0x8, 0x8 } };
1248 static const uint8_t pos2
[2][2] = {
1249 { 0xc, 0xc }, { 0x4, 0x4 } };
1250 static const uint8_t pos4
[4][2] = {
1251 { 0x6, 0x2 }, { 0xe, 0x6 },
1252 { 0x2, 0xa }, { 0xa, 0xe } };
1253 /* TODO needs to be verified on supported hw */
1254 static const uint8_t pos8
[8][2] = {
1255 { 0x9, 0x5 }, { 0x7, 0xb },
1256 { 0xd, 0x9 }, { 0x5, 0x3 },
1257 { 0x3, 0xd }, { 0x1, 0x7 },
1258 { 0xb, 0xf }, { 0xf, 0x1 } };
1260 const uint8_t (*ptr
)[2];
1262 switch (sample_count
) {
1280 pos_out
[0] = ptr
[sample_index
][0] / 16.0f
;
1281 pos_out
[1] = ptr
[sample_index
][1] / 16.0f
;
1285 fd_blit_pipe(struct pipe_context
*pctx
, const struct pipe_blit_info
*blit_info
)
1287 /* wrap fd_blit to return void */
1288 fd_blit(pctx
, blit_info
);
1292 fd_resource_context_init(struct pipe_context
*pctx
)
1294 pctx
->transfer_map
= u_transfer_helper_transfer_map
;
1295 pctx
->transfer_flush_region
= u_transfer_helper_transfer_flush_region
;
1296 pctx
->transfer_unmap
= u_transfer_helper_transfer_unmap
;
1297 pctx
->buffer_subdata
= u_default_buffer_subdata
;
1298 pctx
->texture_subdata
= u_default_texture_subdata
;
1299 pctx
->create_surface
= fd_create_surface
;
1300 pctx
->surface_destroy
= fd_surface_destroy
;
1301 pctx
->resource_copy_region
= fd_resource_copy_region
;
1302 pctx
->blit
= fd_blit_pipe
;
1303 pctx
->flush_resource
= fd_flush_resource
;
1304 pctx
->invalidate_resource
= fd_invalidate_resource
;
1305 pctx
->get_sample_position
= fd_get_sample_position
;