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_ssbos
= util_last_bit(ctx
->shaderbuf
[stage
].enabled_mask
);
91 for (unsigned i
= 0; i
< num_ssbos
; i
++) {
92 if (ctx
->dirty_shader
[stage
] & FD_DIRTY_SHADER_SSBO
)
94 if (ctx
->shaderbuf
[stage
].sb
[i
].buffer
== prsc
)
95 ctx
->dirty_shader
[stage
] |= FD_DIRTY_SHADER_SSBO
;
101 realloc_bo(struct fd_resource
*rsc
, uint32_t size
)
103 struct pipe_resource
*prsc
= &rsc
->base
;
104 struct fd_screen
*screen
= fd_screen(rsc
->base
.screen
);
105 uint32_t flags
= DRM_FREEDRENO_GEM_CACHE_WCOMBINE
|
106 DRM_FREEDRENO_GEM_TYPE_KMEM
|
107 COND(prsc
->bind
& PIPE_BIND_SCANOUT
, DRM_FREEDRENO_GEM_SCANOUT
);
108 /* TODO other flags? */
110 /* if we start using things other than write-combine,
111 * be sure to check for PIPE_RESOURCE_FLAG_MAP_COHERENT
117 rsc
->bo
= fd_bo_new(screen
->dev
, size
, flags
, "%ux%ux%u@%u:%x",
118 prsc
->width0
, prsc
->height0
, prsc
->depth0
, rsc
->cpp
, prsc
->bind
);
119 rsc
->seqno
= p_atomic_inc_return(&screen
->rsc_seqno
);
120 util_range_set_empty(&rsc
->valid_buffer_range
);
121 fd_bc_invalidate_resource(rsc
, true);
125 do_blit(struct fd_context
*ctx
, const struct pipe_blit_info
*blit
, bool fallback
)
127 struct pipe_context
*pctx
= &ctx
->base
;
129 /* TODO size threshold too?? */
130 if (fallback
|| !fd_blit(pctx
, blit
)) {
131 /* do blit on cpu: */
132 util_resource_copy_region(pctx
,
133 blit
->dst
.resource
, blit
->dst
.level
, blit
->dst
.box
.x
,
134 blit
->dst
.box
.y
, blit
->dst
.box
.z
,
135 blit
->src
.resource
, blit
->src
.level
, &blit
->src
.box
);
140 fd_try_shadow_resource(struct fd_context
*ctx
, struct fd_resource
*rsc
,
141 unsigned level
, const struct pipe_box
*box
)
143 struct pipe_context
*pctx
= &ctx
->base
;
144 struct pipe_resource
*prsc
= &rsc
->base
;
145 bool fallback
= false;
150 /* TODO: somehow munge dimensions and format to copy unsupported
151 * render target format to something that is supported?
153 if (!pctx
->screen
->is_format_supported(pctx
->screen
,
154 prsc
->format
, prsc
->target
, prsc
->nr_samples
,
155 prsc
->nr_storage_samples
,
156 PIPE_BIND_RENDER_TARGET
))
159 /* do shadowing back-blits on the cpu for buffers: */
160 if (prsc
->target
== PIPE_BUFFER
)
163 bool whole_level
= util_texrange_covers_whole_level(prsc
, level
,
164 box
->x
, box
->y
, box
->z
, box
->width
, box
->height
, box
->depth
);
166 /* TODO need to be more clever about current level */
167 if ((prsc
->target
>= PIPE_TEXTURE_2D
) && !whole_level
)
170 struct pipe_resource
*pshadow
=
171 pctx
->screen
->resource_create(pctx
->screen
, prsc
);
176 assert(!ctx
->in_shadow
);
177 ctx
->in_shadow
= true;
179 /* get rid of any references that batch-cache might have to us (which
180 * should empty/destroy rsc->batches hashset)
182 fd_bc_invalidate_resource(rsc
, false);
184 mtx_lock(&ctx
->screen
->lock
);
186 /* Swap the backing bo's, so shadow becomes the old buffer,
187 * blit from shadow to new buffer. From here on out, we
190 * Note that we need to do it in this order, otherwise if
191 * we go down cpu blit path, the recursive transfer_map()
192 * sees the wrong status..
194 struct fd_resource
*shadow
= fd_resource(pshadow
);
196 DBG("shadow: %p (%d) -> %p (%d)\n", rsc
, rsc
->base
.reference
.count
,
197 shadow
, shadow
->base
.reference
.count
);
199 /* TODO valid_buffer_range?? */
200 swap(rsc
->bo
, shadow
->bo
);
201 swap(rsc
->write_batch
, shadow
->write_batch
);
202 rsc
->seqno
= p_atomic_inc_return(&ctx
->screen
->rsc_seqno
);
204 /* at this point, the newly created shadow buffer is not referenced
205 * by any batches, but the existing rsc (probably) is. We need to
206 * transfer those references over:
208 debug_assert(shadow
->batch_mask
== 0);
209 struct fd_batch
*batch
;
210 foreach_batch(batch
, &ctx
->screen
->batch_cache
, rsc
->batch_mask
) {
211 struct set_entry
*entry
= _mesa_set_search(batch
->resources
, rsc
);
212 _mesa_set_remove(batch
->resources
, entry
);
213 _mesa_set_add(batch
->resources
, shadow
);
215 swap(rsc
->batch_mask
, shadow
->batch_mask
);
217 mtx_unlock(&ctx
->screen
->lock
);
219 struct pipe_blit_info blit
= {};
220 blit
.dst
.resource
= prsc
;
221 blit
.dst
.format
= prsc
->format
;
222 blit
.src
.resource
= pshadow
;
223 blit
.src
.format
= pshadow
->format
;
224 blit
.mask
= util_format_get_mask(prsc
->format
);
225 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
227 #define set_box(field, val) do { \
228 blit.dst.field = (val); \
229 blit.src.field = (val); \
232 /* blit the other levels in their entirety: */
233 for (unsigned l
= 0; l
<= prsc
->last_level
; l
++) {
237 /* just blit whole level: */
239 set_box(box
.width
, u_minify(prsc
->width0
, l
));
240 set_box(box
.height
, u_minify(prsc
->height0
, l
));
241 set_box(box
.depth
, u_minify(prsc
->depth0
, l
));
243 do_blit(ctx
, &blit
, fallback
);
246 /* deal w/ current level specially, since we might need to split
247 * it up into a couple blits:
250 set_box(level
, level
);
252 switch (prsc
->target
) {
254 case PIPE_TEXTURE_1D
:
257 set_box(box
.height
, 1);
258 set_box(box
.depth
, 1);
262 set_box(box
.width
, box
->x
);
264 do_blit(ctx
, &blit
, fallback
);
266 if ((box
->x
+ box
->width
) < u_minify(prsc
->width0
, level
)) {
267 set_box(box
.x
, box
->x
+ box
->width
);
268 set_box(box
.width
, u_minify(prsc
->width0
, level
) - (box
->x
+ box
->width
));
270 do_blit(ctx
, &blit
, fallback
);
273 case PIPE_TEXTURE_2D
:
280 ctx
->in_shadow
= false;
282 pipe_resource_reference(&pshadow
, NULL
);
287 static struct fd_resource
*
288 fd_alloc_staging(struct fd_context
*ctx
, struct fd_resource
*rsc
,
289 unsigned level
, const struct pipe_box
*box
)
291 struct pipe_context
*pctx
= &ctx
->base
;
292 struct pipe_resource tmpl
= rsc
->base
;
294 tmpl
.width0
= box
->width
;
295 tmpl
.height0
= box
->height
;
296 /* for array textures, box->depth is the array_size, otherwise
297 * for 3d textures, it is the depth:
299 if (tmpl
.array_size
> 1) {
300 tmpl
.array_size
= box
->depth
;
304 tmpl
.depth0
= box
->depth
;
307 tmpl
.bind
|= PIPE_BIND_LINEAR
;
309 struct pipe_resource
*pstaging
=
310 pctx
->screen
->resource_create(pctx
->screen
, &tmpl
);
314 return fd_resource(pstaging
);
318 fd_blit_from_staging(struct fd_context
*ctx
, struct fd_transfer
*trans
)
320 struct pipe_resource
*dst
= trans
->base
.resource
;
321 struct pipe_blit_info blit
= {};
323 blit
.dst
.resource
= dst
;
324 blit
.dst
.format
= dst
->format
;
325 blit
.dst
.level
= trans
->base
.level
;
326 blit
.dst
.box
= trans
->base
.box
;
327 blit
.src
.resource
= trans
->staging_prsc
;
328 blit
.src
.format
= trans
->staging_prsc
->format
;
330 blit
.src
.box
= trans
->staging_box
;
331 blit
.mask
= util_format_get_mask(trans
->staging_prsc
->format
);
332 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
334 do_blit(ctx
, &blit
, false);
338 fd_blit_to_staging(struct fd_context
*ctx
, struct fd_transfer
*trans
)
340 struct pipe_resource
*src
= trans
->base
.resource
;
341 struct pipe_blit_info blit
= {};
343 blit
.src
.resource
= src
;
344 blit
.src
.format
= src
->format
;
345 blit
.src
.level
= trans
->base
.level
;
346 blit
.src
.box
= trans
->base
.box
;
347 blit
.dst
.resource
= trans
->staging_prsc
;
348 blit
.dst
.format
= trans
->staging_prsc
->format
;
350 blit
.dst
.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);
357 static void fd_resource_transfer_flush_region(struct pipe_context
*pctx
,
358 struct pipe_transfer
*ptrans
,
359 const struct pipe_box
*box
)
361 struct fd_resource
*rsc
= fd_resource(ptrans
->resource
);
363 if (ptrans
->resource
->target
== PIPE_BUFFER
)
364 util_range_add(&rsc
->valid_buffer_range
,
365 ptrans
->box
.x
+ box
->x
,
366 ptrans
->box
.x
+ box
->x
+ box
->width
);
370 flush_resource(struct fd_context
*ctx
, struct fd_resource
*rsc
, unsigned usage
)
372 struct fd_batch
*write_batch
= NULL
;
374 mtx_lock(&ctx
->screen
->lock
);
375 fd_batch_reference_locked(&write_batch
, rsc
->write_batch
);
376 mtx_unlock(&ctx
->screen
->lock
);
378 if (usage
& PIPE_TRANSFER_WRITE
) {
379 struct fd_batch
*batch
, *batches
[32] = {};
382 /* This is a bit awkward, probably a fd_batch_flush_locked()
383 * would make things simpler.. but we need to hold the lock
384 * to iterate the batches which reference this resource. So
385 * we must first grab references under a lock, then flush.
387 mtx_lock(&ctx
->screen
->lock
);
388 batch_mask
= rsc
->batch_mask
;
389 foreach_batch(batch
, &ctx
->screen
->batch_cache
, batch_mask
)
390 fd_batch_reference_locked(&batches
[batch
->idx
], batch
);
391 mtx_unlock(&ctx
->screen
->lock
);
393 foreach_batch(batch
, &ctx
->screen
->batch_cache
, batch_mask
)
394 fd_batch_flush(batch
, false, false);
396 foreach_batch(batch
, &ctx
->screen
->batch_cache
, batch_mask
) {
397 fd_batch_sync(batch
);
398 fd_batch_reference(&batches
[batch
->idx
], NULL
);
400 assert(rsc
->batch_mask
== 0);
401 } else if (write_batch
) {
402 fd_batch_flush(write_batch
, true, false);
405 fd_batch_reference(&write_batch
, NULL
);
407 assert(!rsc
->write_batch
);
411 fd_flush_resource(struct pipe_context
*pctx
, struct pipe_resource
*prsc
)
413 flush_resource(fd_context(pctx
), fd_resource(prsc
), PIPE_TRANSFER_READ
);
417 fd_resource_transfer_unmap(struct pipe_context
*pctx
,
418 struct pipe_transfer
*ptrans
)
420 struct fd_context
*ctx
= fd_context(pctx
);
421 struct fd_resource
*rsc
= fd_resource(ptrans
->resource
);
422 struct fd_transfer
*trans
= fd_transfer(ptrans
);
424 if (trans
->staging_prsc
) {
425 if (ptrans
->usage
& PIPE_TRANSFER_WRITE
)
426 fd_blit_from_staging(ctx
, trans
);
427 pipe_resource_reference(&trans
->staging_prsc
, NULL
);
430 if (!(ptrans
->usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
431 fd_bo_cpu_fini(rsc
->bo
);
434 util_range_add(&rsc
->valid_buffer_range
,
436 ptrans
->box
.x
+ ptrans
->box
.width
);
438 pipe_resource_reference(&ptrans
->resource
, NULL
);
439 slab_free(&ctx
->transfer_pool
, ptrans
);
443 fd_resource_transfer_map(struct pipe_context
*pctx
,
444 struct pipe_resource
*prsc
,
445 unsigned level
, unsigned usage
,
446 const struct pipe_box
*box
,
447 struct pipe_transfer
**pptrans
)
449 struct fd_context
*ctx
= fd_context(pctx
);
450 struct fd_resource
*rsc
= fd_resource(prsc
);
451 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, level
);
452 struct fd_transfer
*trans
;
453 struct pipe_transfer
*ptrans
;
454 enum pipe_format format
= prsc
->format
;
460 DBG("prsc=%p, level=%u, usage=%x, box=%dx%d+%d,%d", prsc
, level
, usage
,
461 box
->width
, box
->height
, box
->x
, box
->y
);
463 ptrans
= slab_alloc(&ctx
->transfer_pool
);
467 /* slab_alloc_st() doesn't zero: */
468 trans
= fd_transfer(ptrans
);
469 memset(trans
, 0, sizeof(*trans
));
471 pipe_resource_reference(&ptrans
->resource
, prsc
);
472 ptrans
->level
= level
;
473 ptrans
->usage
= usage
;
475 ptrans
->stride
= util_format_get_nblocksx(format
, slice
->pitch
) * rsc
->cpp
;
476 ptrans
->layer_stride
= rsc
->layer_first
? rsc
->layer_size
: slice
->size0
;
478 /* we always need a staging texture for tiled buffers:
480 * TODO we might sometimes want to *also* shadow the resource to avoid
481 * splitting a batch.. for ex, mid-frame texture uploads to a tiled
484 if (rsc
->tile_mode
) {
485 struct fd_resource
*staging_rsc
;
487 staging_rsc
= fd_alloc_staging(ctx
, rsc
, level
, box
);
489 // TODO for PIPE_TRANSFER_READ, need to do untiling blit..
490 trans
->staging_prsc
= &staging_rsc
->base
;
491 trans
->base
.stride
= util_format_get_nblocksx(format
,
492 staging_rsc
->slices
[0].pitch
) * staging_rsc
->cpp
;
493 trans
->base
.layer_stride
= staging_rsc
->layer_first
?
494 staging_rsc
->layer_size
: staging_rsc
->slices
[0].size0
;
495 trans
->staging_box
= *box
;
496 trans
->staging_box
.x
= 0;
497 trans
->staging_box
.y
= 0;
498 trans
->staging_box
.z
= 0;
500 if (usage
& PIPE_TRANSFER_READ
) {
501 fd_blit_to_staging(ctx
, trans
);
503 struct fd_batch
*batch
= NULL
;
505 fd_context_lock(ctx
);
506 fd_batch_reference_locked(&batch
, staging_rsc
->write_batch
);
507 fd_context_unlock(ctx
);
509 /* we can't fd_bo_cpu_prep() until the blit to staging
510 * is submitted to kernel.. in that case write_batch
511 * wouldn't be NULL yet:
514 fd_batch_sync(batch
);
515 fd_batch_reference(&batch
, NULL
);
518 fd_bo_cpu_prep(staging_rsc
->bo
, ctx
->pipe
,
519 DRM_FREEDRENO_PREP_READ
);
522 buf
= fd_bo_map(staging_rsc
->bo
);
527 ctx
->stats
.staging_uploads
++;
533 if (ctx
->in_shadow
&& !(usage
& PIPE_TRANSFER_READ
))
534 usage
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
536 if (usage
& PIPE_TRANSFER_READ
)
537 op
|= DRM_FREEDRENO_PREP_READ
;
539 if (usage
& PIPE_TRANSFER_WRITE
)
540 op
|= DRM_FREEDRENO_PREP_WRITE
;
542 if (usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) {
543 realloc_bo(rsc
, fd_bo_size(rsc
->bo
));
544 rebind_resource(ctx
, prsc
);
545 } else if ((usage
& PIPE_TRANSFER_WRITE
) &&
546 prsc
->target
== PIPE_BUFFER
&&
547 !util_ranges_intersect(&rsc
->valid_buffer_range
,
548 box
->x
, box
->x
+ box
->width
)) {
549 /* We are trying to write to a previously uninitialized range. No need
552 } else if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
553 struct fd_batch
*write_batch
= NULL
;
555 /* hold a reference, so it doesn't disappear under us: */
556 fd_context_lock(ctx
);
557 fd_batch_reference_locked(&write_batch
, rsc
->write_batch
);
558 fd_context_unlock(ctx
);
560 if ((usage
& PIPE_TRANSFER_WRITE
) && write_batch
&&
561 write_batch
->back_blit
) {
562 /* if only thing pending is a back-blit, we can discard it: */
563 fd_batch_reset(write_batch
);
566 /* If the GPU is writing to the resource, or if it is reading from the
567 * resource and we're trying to write to it, flush the renders.
569 bool needs_flush
= pending(rsc
, !!(usage
& PIPE_TRANSFER_WRITE
));
570 bool busy
= needs_flush
|| (0 != fd_bo_cpu_prep(rsc
->bo
,
571 ctx
->pipe
, op
| DRM_FREEDRENO_PREP_NOSYNC
));
573 /* if we need to flush/stall, see if we can make a shadow buffer
576 * TODO we could go down this path !reorder && !busy_for_read
577 * ie. we only *don't* want to go down this path if the blit
578 * will trigger a flush!
580 if (ctx
->screen
->reorder
&& busy
&& !(usage
& PIPE_TRANSFER_READ
) &&
581 (usage
& PIPE_TRANSFER_DISCARD_RANGE
)) {
582 /* try shadowing only if it avoids a flush, otherwise staging would
585 if (needs_flush
&& fd_try_shadow_resource(ctx
, rsc
, level
, box
)) {
586 needs_flush
= busy
= false;
587 rebind_resource(ctx
, prsc
);
588 ctx
->stats
.shadow_uploads
++;
590 struct fd_resource
*staging_rsc
;
593 flush_resource(ctx
, rsc
, usage
);
597 /* in this case, we don't need to shadow the whole resource,
598 * since any draw that references the previous contents has
599 * already had rendering flushed for all tiles. So we can
600 * use a staging buffer to do the upload.
602 staging_rsc
= fd_alloc_staging(ctx
, rsc
, level
, box
);
604 trans
->staging_prsc
= &staging_rsc
->base
;
605 trans
->base
.stride
= util_format_get_nblocksx(format
,
606 staging_rsc
->slices
[0].pitch
) * staging_rsc
->cpp
;
607 trans
->base
.layer_stride
= staging_rsc
->layer_first
?
608 staging_rsc
->layer_size
: staging_rsc
->slices
[0].size0
;
609 trans
->staging_box
= *box
;
610 trans
->staging_box
.x
= 0;
611 trans
->staging_box
.y
= 0;
612 trans
->staging_box
.z
= 0;
613 buf
= fd_bo_map(staging_rsc
->bo
);
618 fd_batch_reference(&write_batch
, NULL
);
620 ctx
->stats
.staging_uploads
++;
628 flush_resource(ctx
, rsc
, usage
);
632 fd_batch_reference(&write_batch
, NULL
);
634 /* The GPU keeps track of how the various bo's are being used, and
635 * will wait if necessary for the proper operation to have
639 ret
= fd_bo_cpu_prep(rsc
->bo
, ctx
->pipe
, op
);
645 buf
= fd_bo_map(rsc
->bo
);
647 box
->y
/ util_format_get_blockheight(format
) * ptrans
->stride
+
648 box
->x
/ util_format_get_blockwidth(format
) * rsc
->cpp
+
649 fd_resource_offset(rsc
, level
, box
->z
);
651 if (usage
& PIPE_TRANSFER_WRITE
)
659 fd_resource_transfer_unmap(pctx
, ptrans
);
664 fd_resource_destroy(struct pipe_screen
*pscreen
,
665 struct pipe_resource
*prsc
)
667 struct fd_resource
*rsc
= fd_resource(prsc
);
668 fd_bc_invalidate_resource(rsc
, true);
672 renderonly_scanout_destroy(rsc
->scanout
, fd_screen(pscreen
)->ro
);
674 util_range_destroy(&rsc
->valid_buffer_range
);
679 fd_resource_modifier(struct fd_resource
*rsc
)
682 return DRM_FORMAT_MOD_LINEAR
;
685 return DRM_FORMAT_MOD_QCOM_COMPRESSED
;
687 /* TODO invent a modifier for tiled but not UBWC buffers: */
688 return DRM_FORMAT_MOD_INVALID
;
692 fd_resource_get_handle(struct pipe_screen
*pscreen
,
693 struct pipe_context
*pctx
,
694 struct pipe_resource
*prsc
,
695 struct winsys_handle
*handle
,
698 struct fd_resource
*rsc
= fd_resource(prsc
);
700 handle
->modifier
= fd_resource_modifier(rsc
);
702 return fd_screen_bo_get_handle(pscreen
, rsc
->bo
, rsc
->scanout
,
703 rsc
->slices
[0].pitch
* rsc
->cpp
, handle
);
707 setup_slices(struct fd_resource
*rsc
, uint32_t alignment
, enum pipe_format format
)
709 struct pipe_resource
*prsc
= &rsc
->base
;
710 struct fd_screen
*screen
= fd_screen(prsc
->screen
);
711 enum util_format_layout layout
= util_format_description(format
)->layout
;
712 uint32_t pitchalign
= screen
->gmem_alignw
;
713 uint32_t level
, size
= 0;
714 uint32_t width
= prsc
->width0
;
715 uint32_t height
= prsc
->height0
;
716 uint32_t depth
= prsc
->depth0
;
717 /* in layer_first layout, the level (slice) contains just one
718 * layer (since in fact the layer contains the slices)
720 uint32_t layers_in_level
= rsc
->layer_first
? 1 : prsc
->array_size
;
722 for (level
= 0; level
<= prsc
->last_level
; level
++) {
723 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, level
);
726 if (layout
== UTIL_FORMAT_LAYOUT_ASTC
)
727 slice
->pitch
= width
=
728 util_align_npot(width
, pitchalign
* util_format_get_blockwidth(format
));
730 slice
->pitch
= width
= align(width
, pitchalign
);
731 slice
->offset
= size
;
732 blocks
= util_format_get_nblocks(format
, width
, height
);
733 /* 1d array and 2d array textures must all have the same layer size
734 * for each miplevel on a3xx. 3d textures can have different layer
735 * sizes for high levels, but the hw auto-sizer is buggy (or at least
736 * different than what this code does), so as soon as the layer size
737 * range gets into range, we stop reducing it.
739 if (prsc
->target
== PIPE_TEXTURE_3D
&& (
741 (level
> 1 && rsc
->slices
[level
- 1].size0
> 0xf000)))
742 slice
->size0
= align(blocks
* rsc
->cpp
, alignment
);
743 else if (level
== 0 || rsc
->layer_first
|| alignment
== 1)
744 slice
->size0
= align(blocks
* rsc
->cpp
, alignment
);
746 slice
->size0
= rsc
->slices
[level
- 1].size0
;
748 size
+= slice
->size0
* depth
* layers_in_level
;
750 width
= u_minify(width
, 1);
751 height
= u_minify(height
, 1);
752 depth
= u_minify(depth
, 1);
759 slice_alignment(enum pipe_texture_target target
)
761 /* on a3xx, 2d array and 3d textures seem to want their
762 * layers aligned to page boundaries:
765 case PIPE_TEXTURE_3D
:
766 case PIPE_TEXTURE_1D_ARRAY
:
767 case PIPE_TEXTURE_2D_ARRAY
:
774 /* cross generation texture layout to plug in to screen->setup_slices()..
775 * replace with generation specific one as-needed.
777 * TODO for a4xx probably can extract out the a4xx specific logic int
778 * a small fd4_setup_slices() wrapper that sets up layer_first, and then
782 fd_setup_slices(struct fd_resource
*rsc
)
786 alignment
= slice_alignment(rsc
->base
.target
);
788 struct fd_screen
*screen
= fd_screen(rsc
->base
.screen
);
789 if (is_a4xx(screen
)) {
790 switch (rsc
->base
.target
) {
791 case PIPE_TEXTURE_3D
:
792 rsc
->layer_first
= false;
795 rsc
->layer_first
= true;
801 return setup_slices(rsc
, alignment
, rsc
->base
.format
);
804 /* special case to resize query buf after allocated.. */
806 fd_resource_resize(struct pipe_resource
*prsc
, uint32_t sz
)
808 struct fd_resource
*rsc
= fd_resource(prsc
);
810 debug_assert(prsc
->width0
== 0);
811 debug_assert(prsc
->target
== PIPE_BUFFER
);
812 debug_assert(prsc
->bind
== PIPE_BIND_QUERY_BUFFER
);
815 realloc_bo(rsc
, fd_screen(prsc
->screen
)->setup_slices(rsc
));
818 // TODO common helper?
820 has_depth(enum pipe_format format
)
823 case PIPE_FORMAT_Z16_UNORM
:
824 case PIPE_FORMAT_Z32_UNORM
:
825 case PIPE_FORMAT_Z32_FLOAT
:
826 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
827 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
828 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
829 case PIPE_FORMAT_Z24X8_UNORM
:
830 case PIPE_FORMAT_X8Z24_UNORM
:
838 * Create a new texture object, using the given template info.
840 static struct pipe_resource
*
841 fd_resource_create_with_modifiers(struct pipe_screen
*pscreen
,
842 const struct pipe_resource
*tmpl
,
843 const uint64_t *modifiers
, int count
)
845 struct fd_screen
*screen
= fd_screen(pscreen
);
846 struct fd_resource
*rsc
;
847 struct pipe_resource
*prsc
;
848 enum pipe_format format
= tmpl
->format
;
851 /* when using kmsro, scanout buffers are allocated on the display device
852 * create_with_modifiers() doesn't give us usage flags, so we have to
853 * assume that all calls with modifiers are scanout-possible
856 ((tmpl
->bind
& PIPE_BIND_SCANOUT
) ||
857 !(count
== 1 && modifiers
[0] == DRM_FORMAT_MOD_INVALID
))) {
858 struct pipe_resource scanout_templat
= *tmpl
;
859 struct renderonly_scanout
*scanout
;
860 struct winsys_handle handle
;
862 scanout
= renderonly_scanout_for_resource(&scanout_templat
,
863 screen
->ro
, &handle
);
867 renderonly_scanout_destroy(scanout
, screen
->ro
);
869 assert(handle
.type
== WINSYS_HANDLE_TYPE_FD
);
870 rsc
= fd_resource(pscreen
->resource_from_handle(pscreen
, tmpl
,
872 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE
));
873 close(handle
.handle
);
880 rsc
= CALLOC_STRUCT(fd_resource
);
883 DBG("%p: target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
884 "nr_samples=%u, usage=%u, bind=%x, flags=%x", prsc
,
885 tmpl
->target
, util_format_name(format
),
886 tmpl
->width0
, tmpl
->height0
, tmpl
->depth0
,
887 tmpl
->array_size
, tmpl
->last_level
, tmpl
->nr_samples
,
888 tmpl
->usage
, tmpl
->bind
, tmpl
->flags
);
896 (PIPE_BIND_SCANOUT | \
898 PIPE_BIND_DISPLAY_TARGET)
900 bool linear
= drm_find_modifier(DRM_FORMAT_MOD_LINEAR
, modifiers
, count
);
901 if (tmpl
->bind
& LINEAR
)
904 /* Normally, for non-shared buffers, allow buffer compression if
905 * not shared, otherwise only allow if QCOM_COMPRESSED modifier
908 * TODO we should probably also limit tiled in a similar way,
909 * except we don't have a format modifier for tiled. (We probably
912 bool allow_ubwc
= drm_find_modifier(DRM_FORMAT_MOD_INVALID
, modifiers
, count
);
913 if (tmpl
->bind
& PIPE_BIND_SHARED
)
914 allow_ubwc
= drm_find_modifier(DRM_FORMAT_MOD_QCOM_COMPRESSED
, modifiers
, count
);
916 /* TODO turn on UBWC for all internal buffers
918 * There are still some regressions in deqp with UBWC enabled. I
919 * think it is mostly related to sampler/image views using a format
920 * that doesn't support compression with a resource created with
921 * a format that does. We need to track the compression state of
922 * a buffer and do an (in-place, hopefully?) resolve if it is re-
923 * interpreted with a format that does not support compression.
925 * It is possible (likely?) that we can't do atomic ops on a
926 * compressed buffer as well, so this would also require transition
927 * to a compressed state.
929 allow_ubwc
&= !!(fd_mesa_debug
& FD_DBG_UBWC
);
931 if (screen
->tile_mode
&&
932 (tmpl
->target
!= PIPE_BUFFER
) &&
934 rsc
->tile_mode
= screen
->tile_mode(tmpl
);
937 pipe_reference_init(&prsc
->reference
, 1);
939 prsc
->screen
= pscreen
;
941 util_range_init(&rsc
->valid_buffer_range
);
943 rsc
->internal_format
= format
;
944 rsc
->cpp
= util_format_get_blocksize(format
);
945 rsc
->cpp
*= fd_resource_nr_samples(prsc
);
949 // XXX probably need some extra work if we hit rsc shadowing path w/ lrz..
950 if ((is_a5xx(screen
) || is_a6xx(screen
)) &&
951 (fd_mesa_debug
& FD_DBG_LRZ
) && has_depth(format
)) {
952 const uint32_t flags
= DRM_FREEDRENO_GEM_CACHE_WCOMBINE
|
953 DRM_FREEDRENO_GEM_TYPE_KMEM
; /* TODO */
954 unsigned lrz_pitch
= align(DIV_ROUND_UP(tmpl
->width0
, 8), 64);
955 unsigned lrz_height
= DIV_ROUND_UP(tmpl
->height0
, 8);
957 /* LRZ buffer is super-sampled: */
958 switch (prsc
->nr_samples
) {
965 unsigned size
= lrz_pitch
* lrz_height
* 2;
967 size
+= 0x1000; /* for GRAS_LRZ_FAST_CLEAR_BUFFER */
969 rsc
->lrz_height
= lrz_height
;
970 rsc
->lrz_width
= lrz_pitch
;
971 rsc
->lrz_pitch
= lrz_pitch
;
972 rsc
->lrz
= fd_bo_new(screen
->dev
, size
, flags
, "lrz");
975 size
= screen
->setup_slices(rsc
);
977 if (allow_ubwc
&& screen
->fill_ubwc_buffer_sizes
&& rsc
->tile_mode
)
978 size
+= screen
->fill_ubwc_buffer_sizes(rsc
);
980 /* special case for hw-query buffer, which we need to allocate before we
984 /* note, semi-intention == instead of & */
985 debug_assert(prsc
->bind
== PIPE_BIND_QUERY_BUFFER
);
989 if (rsc
->layer_first
) {
990 rsc
->layer_size
= align(size
, 4096);
991 size
= rsc
->layer_size
* prsc
->array_size
;
994 realloc_bo(rsc
, size
);
1000 fd_resource_destroy(pscreen
, prsc
);
1004 static struct pipe_resource
*
1005 fd_resource_create(struct pipe_screen
*pscreen
,
1006 const struct pipe_resource
*tmpl
)
1008 const uint64_t mod
= DRM_FORMAT_MOD_INVALID
;
1009 return fd_resource_create_with_modifiers(pscreen
, tmpl
, &mod
, 1);
1013 is_supported_modifier(struct pipe_screen
*pscreen
, enum pipe_format pfmt
,
1018 /* Get the count of supported modifiers: */
1019 pscreen
->query_dmabuf_modifiers(pscreen
, pfmt
, 0, NULL
, NULL
, &count
);
1021 /* Get the supported modifiers: */
1022 uint64_t modifiers
[count
];
1023 pscreen
->query_dmabuf_modifiers(pscreen
, pfmt
, count
, modifiers
, NULL
, &count
);
1025 for (int i
= 0; i
< count
; i
++)
1026 if (modifiers
[i
] == mod
)
1033 * Create a texture from a winsys_handle. The handle is often created in
1034 * another process by first creating a pipe texture and then calling
1035 * resource_get_handle.
1037 static struct pipe_resource
*
1038 fd_resource_from_handle(struct pipe_screen
*pscreen
,
1039 const struct pipe_resource
*tmpl
,
1040 struct winsys_handle
*handle
, unsigned usage
)
1042 struct fd_screen
*screen
= fd_screen(pscreen
);
1043 struct fd_resource
*rsc
= CALLOC_STRUCT(fd_resource
);
1044 struct fd_resource_slice
*slice
= &rsc
->slices
[0];
1045 struct pipe_resource
*prsc
= &rsc
->base
;
1046 uint32_t pitchalign
= fd_screen(pscreen
)->gmem_alignw
;
1048 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
1049 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
1050 tmpl
->target
, util_format_name(tmpl
->format
),
1051 tmpl
->width0
, tmpl
->height0
, tmpl
->depth0
,
1052 tmpl
->array_size
, tmpl
->last_level
, tmpl
->nr_samples
,
1053 tmpl
->usage
, tmpl
->bind
, tmpl
->flags
);
1060 pipe_reference_init(&prsc
->reference
, 1);
1062 prsc
->screen
= pscreen
;
1064 util_range_init(&rsc
->valid_buffer_range
);
1066 rsc
->bo
= fd_screen_bo_from_handle(pscreen
, handle
);
1070 rsc
->internal_format
= tmpl
->format
;
1071 rsc
->cpp
= util_format_get_blocksize(tmpl
->format
);
1072 rsc
->cpp
*= fd_resource_nr_samples(prsc
);
1073 slice
->pitch
= handle
->stride
/ rsc
->cpp
;
1074 slice
->offset
= handle
->offset
;
1075 slice
->size0
= handle
->stride
* prsc
->height0
;
1077 if ((slice
->pitch
< align(prsc
->width0
, pitchalign
)) ||
1078 (slice
->pitch
& (pitchalign
- 1)))
1081 if (handle
->modifier
== DRM_FORMAT_MOD_QCOM_COMPRESSED
) {
1082 if (!is_supported_modifier(pscreen
, tmpl
->format
,
1083 DRM_FORMAT_MOD_QCOM_COMPRESSED
)) {
1084 DBG("bad modifier: %"PRIx64
, handle
->modifier
);
1087 debug_assert(screen
->fill_ubwc_buffer_sizes
);
1088 screen
->fill_ubwc_buffer_sizes(rsc
);
1089 } else if (handle
->modifier
&&
1090 (handle
->modifier
!= DRM_FORMAT_MOD_INVALID
)) {
1098 renderonly_create_gpu_import_for_resource(prsc
, screen
->ro
, NULL
);
1099 /* failure is expected in some cases.. */
1107 fd_resource_destroy(pscreen
, prsc
);
1112 fd_render_condition_check(struct pipe_context
*pctx
)
1114 struct fd_context
*ctx
= fd_context(pctx
);
1116 if (!ctx
->cond_query
)
1119 union pipe_query_result res
= { 0 };
1121 ctx
->cond_mode
!= PIPE_RENDER_COND_NO_WAIT
&&
1122 ctx
->cond_mode
!= PIPE_RENDER_COND_BY_REGION_NO_WAIT
;
1124 if (pctx
->get_query_result(pctx
, ctx
->cond_query
, wait
, &res
))
1125 return (bool)res
.u64
!= ctx
->cond_cond
;
1131 fd_invalidate_resource(struct pipe_context
*pctx
, struct pipe_resource
*prsc
)
1133 struct fd_context
*ctx
= fd_context(pctx
);
1134 struct fd_resource
*rsc
= fd_resource(prsc
);
1137 * TODO I guess we could track that the resource is invalidated and
1138 * use that as a hint to realloc rather than stall in _transfer_map(),
1139 * even in the non-DISCARD_WHOLE_RESOURCE case?
1141 * Note: we set dirty bits to trigger invalidate logic fd_draw_vbo
1144 if (rsc
->write_batch
) {
1145 struct fd_batch
*batch
= rsc
->write_batch
;
1146 struct pipe_framebuffer_state
*pfb
= &batch
->framebuffer
;
1148 if (pfb
->zsbuf
&& pfb
->zsbuf
->texture
== prsc
) {
1149 batch
->resolve
&= ~(FD_BUFFER_DEPTH
| FD_BUFFER_STENCIL
);
1150 ctx
->dirty
|= FD_DIRTY_ZSA
;
1153 for (unsigned i
= 0; i
< pfb
->nr_cbufs
; i
++) {
1154 if (pfb
->cbufs
[i
] && pfb
->cbufs
[i
]->texture
== prsc
) {
1155 batch
->resolve
&= ~(PIPE_CLEAR_COLOR0
<< i
);
1156 ctx
->dirty
|= FD_DIRTY_FRAMEBUFFER
;
1164 static enum pipe_format
1165 fd_resource_get_internal_format(struct pipe_resource
*prsc
)
1167 return fd_resource(prsc
)->internal_format
;
1171 fd_resource_set_stencil(struct pipe_resource
*prsc
,
1172 struct pipe_resource
*stencil
)
1174 fd_resource(prsc
)->stencil
= fd_resource(stencil
);
1177 static struct pipe_resource
*
1178 fd_resource_get_stencil(struct pipe_resource
*prsc
)
1180 struct fd_resource
*rsc
= fd_resource(prsc
);
1182 return &rsc
->stencil
->base
;
1186 static const struct u_transfer_vtbl transfer_vtbl
= {
1187 .resource_create
= fd_resource_create
,
1188 .resource_destroy
= fd_resource_destroy
,
1189 .transfer_map
= fd_resource_transfer_map
,
1190 .transfer_flush_region
= fd_resource_transfer_flush_region
,
1191 .transfer_unmap
= fd_resource_transfer_unmap
,
1192 .get_internal_format
= fd_resource_get_internal_format
,
1193 .set_stencil
= fd_resource_set_stencil
,
1194 .get_stencil
= fd_resource_get_stencil
,
1198 fd_resource_screen_init(struct pipe_screen
*pscreen
)
1200 struct fd_screen
*screen
= fd_screen(pscreen
);
1201 bool fake_rgtc
= screen
->gpu_id
< 400;
1203 pscreen
->resource_create
= u_transfer_helper_resource_create
;
1204 /* NOTE: u_transfer_helper does not yet support the _with_modifiers()
1207 pscreen
->resource_create_with_modifiers
= fd_resource_create_with_modifiers
;
1208 pscreen
->resource_from_handle
= fd_resource_from_handle
;
1209 pscreen
->resource_get_handle
= fd_resource_get_handle
;
1210 pscreen
->resource_destroy
= u_transfer_helper_resource_destroy
;
1212 pscreen
->transfer_helper
= u_transfer_helper_create(&transfer_vtbl
,
1213 true, false, fake_rgtc
, true);
1215 if (!screen
->setup_slices
)
1216 screen
->setup_slices
= fd_setup_slices
;
1220 fd_get_sample_position(struct pipe_context
*context
,
1221 unsigned sample_count
, unsigned sample_index
,
1224 /* The following is copied from nouveau/nv50 except for position
1225 * values, which are taken from blob driver */
1226 static const uint8_t pos1
[1][2] = { { 0x8, 0x8 } };
1227 static const uint8_t pos2
[2][2] = {
1228 { 0xc, 0xc }, { 0x4, 0x4 } };
1229 static const uint8_t pos4
[4][2] = {
1230 { 0x6, 0x2 }, { 0xe, 0x6 },
1231 { 0x2, 0xa }, { 0xa, 0xe } };
1232 /* TODO needs to be verified on supported hw */
1233 static const uint8_t pos8
[8][2] = {
1234 { 0x9, 0x5 }, { 0x7, 0xb },
1235 { 0xd, 0x9 }, { 0x5, 0x3 },
1236 { 0x3, 0xd }, { 0x1, 0x7 },
1237 { 0xb, 0xf }, { 0xf, 0x1 } };
1239 const uint8_t (*ptr
)[2];
1241 switch (sample_count
) {
1259 pos_out
[0] = ptr
[sample_index
][0] / 16.0f
;
1260 pos_out
[1] = ptr
[sample_index
][1] / 16.0f
;
1264 fd_blit_pipe(struct pipe_context
*pctx
, const struct pipe_blit_info
*blit_info
)
1266 /* wrap fd_blit to return void */
1267 fd_blit(pctx
, blit_info
);
1271 fd_resource_context_init(struct pipe_context
*pctx
)
1273 pctx
->transfer_map
= u_transfer_helper_transfer_map
;
1274 pctx
->transfer_flush_region
= u_transfer_helper_transfer_flush_region
;
1275 pctx
->transfer_unmap
= u_transfer_helper_transfer_unmap
;
1276 pctx
->buffer_subdata
= u_default_buffer_subdata
;
1277 pctx
->texture_subdata
= u_default_texture_subdata
;
1278 pctx
->create_surface
= fd_create_surface
;
1279 pctx
->surface_destroy
= fd_surface_destroy
;
1280 pctx
->resource_copy_region
= fd_resource_copy_region
;
1281 pctx
->blit
= fd_blit_pipe
;
1282 pctx
->flush_resource
= fd_flush_resource
;
1283 pctx
->invalidate_resource
= fd_invalidate_resource
;
1284 pctx
->get_sample_position
= fd_get_sample_position
;