1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Rob Clark <robclark@freedesktop.org>
29 #include "util/u_format.h"
30 #include "util/u_format_rgtc.h"
31 #include "util/u_format_zs.h"
32 #include "util/u_inlines.h"
33 #include "util/u_transfer.h"
34 #include "util/u_string.h"
35 #include "util/u_surface.h"
38 #include "freedreno_resource.h"
39 #include "freedreno_batch_cache.h"
40 #include "freedreno_screen.h"
41 #include "freedreno_surface.h"
42 #include "freedreno_context.h"
43 #include "freedreno_query_hw.h"
44 #include "freedreno_util.h"
48 /* XXX this should go away, needed for 'struct winsys_handle' */
49 #include "state_tracker/drm_driver.h"
52 pending(struct fd_resource
*rsc
, bool write
)
54 /* if we have a pending GPU write, we are busy in any case: */
58 /* if CPU wants to write, but we are pending a GPU read, we are busy: */
59 if (write
&& rsc
->batch_mask
)
62 if (rsc
->stencil
&& pending(rsc
->stencil
, write
))
69 fd_invalidate_resource(struct fd_context
*ctx
, struct pipe_resource
*prsc
)
73 /* Go through the entire state and see if the resource is bound
74 * anywhere. If it is, mark the relevant state as dirty. This is called on
79 for (i
= 1; i
< PIPE_MAX_CONSTANT_BUFFERS
&& !(ctx
->dirty
& FD_DIRTY_CONSTBUF
); i
++) {
80 if (ctx
->constbuf
[PIPE_SHADER_VERTEX
].cb
[i
].buffer
== prsc
)
81 ctx
->dirty
|= FD_DIRTY_CONSTBUF
;
82 if (ctx
->constbuf
[PIPE_SHADER_FRAGMENT
].cb
[i
].buffer
== prsc
)
83 ctx
->dirty
|= FD_DIRTY_CONSTBUF
;
87 for (i
= 0; i
< ctx
->vtx
.vertexbuf
.count
&& !(ctx
->dirty
& FD_DIRTY_VTXBUF
); i
++) {
88 if (ctx
->vtx
.vertexbuf
.vb
[i
].buffer
== prsc
)
89 ctx
->dirty
|= FD_DIRTY_VTXBUF
;
93 if (ctx
->indexbuf
.buffer
== prsc
)
94 ctx
->dirty
|= FD_DIRTY_INDEXBUF
;
97 for (i
= 0; i
< ctx
->verttex
.num_textures
&& !(ctx
->dirty
& FD_DIRTY_VERTTEX
); i
++) {
98 if (ctx
->verttex
.textures
[i
] && (ctx
->verttex
.textures
[i
]->texture
== prsc
))
99 ctx
->dirty
|= FD_DIRTY_VERTTEX
;
101 for (i
= 0; i
< ctx
->fragtex
.num_textures
&& !(ctx
->dirty
& FD_DIRTY_FRAGTEX
); i
++) {
102 if (ctx
->fragtex
.textures
[i
] && (ctx
->fragtex
.textures
[i
]->texture
== prsc
))
103 ctx
->dirty
|= FD_DIRTY_FRAGTEX
;
108 realloc_bo(struct fd_resource
*rsc
, uint32_t size
)
110 struct fd_screen
*screen
= fd_screen(rsc
->base
.b
.screen
);
111 uint32_t flags
= DRM_FREEDRENO_GEM_CACHE_WCOMBINE
|
112 DRM_FREEDRENO_GEM_TYPE_KMEM
; /* TODO */
114 /* if we start using things other than write-combine,
115 * be sure to check for PIPE_RESOURCE_FLAG_MAP_COHERENT
121 rsc
->bo
= fd_bo_new(screen
->dev
, size
, flags
);
123 util_range_set_empty(&rsc
->valid_buffer_range
);
124 fd_bc_invalidate_resource(rsc
, true);
127 static void fd_blitter_pipe_begin(struct fd_context
*ctx
, bool render_cond
);
128 static void fd_blitter_pipe_end(struct fd_context
*ctx
);
131 do_blit(struct fd_context
*ctx
, const struct pipe_blit_info
*blit
, bool fallback
)
133 /* TODO size threshold too?? */
134 if ((blit
->src
.resource
->target
!= PIPE_BUFFER
) && !fallback
) {
135 /* do blit on gpu: */
136 fd_blitter_pipe_begin(ctx
, false);
137 util_blitter_blit(ctx
->blitter
, blit
);
138 fd_blitter_pipe_end(ctx
);
140 /* do blit on cpu: */
141 util_resource_copy_region(&ctx
->base
,
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 fd_try_shadow_resource(struct fd_context
*ctx
, struct fd_resource
*rsc
,
150 unsigned level
, unsigned usage
, const struct pipe_box
*box
)
152 struct pipe_context
*pctx
= &ctx
->base
;
153 struct pipe_resource
*prsc
= &rsc
->base
.b
;
154 bool fallback
= false;
156 /* TODO: somehow munge dimensions and format to copy unsupported
157 * render target format to something that is supported?
159 if (!pctx
->screen
->is_format_supported(pctx
->screen
,
160 prsc
->format
, prsc
->target
, prsc
->nr_samples
,
161 PIPE_BIND_RENDER_TARGET
))
164 /* these cases should be handled elsewhere.. just for future
165 * reference in case this gets split into a more generic(ish)
168 debug_assert(!(usage
& PIPE_TRANSFER_READ
));
169 debug_assert(!(usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
));
171 /* if we do a gpu blit to clone the whole resource, we'll just
172 * end up stalling on that.. so only allow if we can discard
173 * current range (and blit, possibly cpu or gpu, the rest)
175 if (!(usage
& PIPE_TRANSFER_DISCARD_RANGE
))
178 bool whole_level
= 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
) && !whole_level
)
185 struct pipe_resource
*pshadow
=
186 pctx
->screen
->resource_create(pctx
->screen
, prsc
);
191 assert(!ctx
->in_shadow
);
192 ctx
->in_shadow
= true;
194 /* get rid of any references that batch-cache might have to us (which
195 * should empty/destroy rsc->batches hashset)
197 fd_bc_invalidate_resource(rsc
, false);
199 /* Swap the backing bo's, so shadow becomes the old buffer,
200 * blit from shadow to new buffer. From here on out, we
203 * Note that we need to do it in this order, otherwise if
204 * we go down cpu blit path, the recursive transfer_map()
205 * sees the wrong status..
207 struct fd_resource
*shadow
= fd_resource(pshadow
);
209 DBG("shadow: %p (%d) -> %p (%d)\n", rsc
, rsc
->base
.b
.reference
.count
,
210 shadow
, shadow
->base
.b
.reference
.count
);
212 /* TODO valid_buffer_range?? */
213 swap(rsc
->bo
, shadow
->bo
);
214 swap(rsc
->timestamp
, shadow
->timestamp
);
215 swap(rsc
->write_batch
, shadow
->write_batch
);
217 /* at this point, the newly created shadow buffer is not referenced
218 * by any batches, but the existing rsc (probably) is. We need to
219 * transfer those references over:
221 debug_assert(shadow
->batch_mask
== 0);
222 struct fd_batch
*batch
;
223 foreach_batch(batch
, &ctx
->screen
->batch_cache
, rsc
->batch_mask
) {
224 struct set_entry
*entry
= _mesa_set_search(batch
->resources
, rsc
);
225 _mesa_set_remove(batch
->resources
, entry
);
226 _mesa_set_add(batch
->resources
, shadow
);
228 swap(rsc
->batch_mask
, shadow
->batch_mask
);
230 struct pipe_blit_info blit
= {0};
231 blit
.dst
.resource
= prsc
;
232 blit
.dst
.format
= prsc
->format
;
233 blit
.src
.resource
= pshadow
;
234 blit
.src
.format
= pshadow
->format
;
235 blit
.mask
= util_format_get_mask(prsc
->format
);
236 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
238 #define set_box(field, val) do { \
239 blit.dst.field = (val); \
240 blit.src.field = (val); \
243 /* blit the other levels in their entirety: */
244 for (unsigned l
= 0; l
<= prsc
->last_level
; l
++) {
248 /* just blit whole level: */
250 set_box(box
.width
, u_minify(prsc
->width0
, l
));
251 set_box(box
.height
, u_minify(prsc
->height0
, l
));
252 set_box(box
.depth
, u_minify(prsc
->depth0
, l
));
254 do_blit(ctx
, &blit
, fallback
);
257 /* deal w/ current level specially, since we might need to split
258 * it up into a couple blits:
261 set_box(level
, level
);
263 switch (prsc
->target
) {
265 case PIPE_TEXTURE_1D
:
268 set_box(box
.height
, 1);
269 set_box(box
.depth
, 1);
273 set_box(box
.width
, box
->x
);
275 do_blit(ctx
, &blit
, fallback
);
277 if ((box
->x
+ box
->width
) < u_minify(prsc
->width0
, level
)) {
278 set_box(box
.x
, box
->x
+ box
->width
);
279 set_box(box
.width
, u_minify(prsc
->width0
, level
) - (box
->x
+ box
->width
));
281 do_blit(ctx
, &blit
, fallback
);
284 case PIPE_TEXTURE_2D
:
291 ctx
->in_shadow
= false;
293 pipe_resource_reference(&pshadow
, NULL
);
299 fd_resource_layer_offset(struct fd_resource
*rsc
,
300 struct fd_resource_slice
*slice
,
303 if (rsc
->layer_first
)
304 return layer
* rsc
->layer_size
;
306 return layer
* slice
->size0
;
310 fd_resource_flush_z32s8(struct fd_transfer
*trans
, const struct pipe_box
*box
)
312 struct fd_resource
*rsc
= fd_resource(trans
->base
.resource
);
313 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, trans
->base
.level
);
314 struct fd_resource_slice
*sslice
= fd_resource_slice(rsc
->stencil
, trans
->base
.level
);
315 enum pipe_format format
= trans
->base
.resource
->format
;
317 float *depth
= fd_bo_map(rsc
->bo
) + slice
->offset
+
318 fd_resource_layer_offset(rsc
, slice
, trans
->base
.box
.z
) +
319 (trans
->base
.box
.y
+ box
->y
) * slice
->pitch
* 4 + (trans
->base
.box
.x
+ box
->x
) * 4;
320 uint8_t *stencil
= fd_bo_map(rsc
->stencil
->bo
) + sslice
->offset
+
321 fd_resource_layer_offset(rsc
->stencil
, sslice
, trans
->base
.box
.z
) +
322 (trans
->base
.box
.y
+ box
->y
) * sslice
->pitch
+ trans
->base
.box
.x
+ box
->x
;
324 if (format
!= PIPE_FORMAT_X32_S8X24_UINT
)
325 util_format_z32_float_s8x24_uint_unpack_z_float(
326 depth
, slice
->pitch
* 4,
327 trans
->staging
, trans
->base
.stride
,
328 box
->width
, box
->height
);
330 util_format_z32_float_s8x24_uint_unpack_s_8uint(
331 stencil
, sslice
->pitch
,
332 trans
->staging
, trans
->base
.stride
,
333 box
->width
, box
->height
);
337 fd_resource_flush_rgtc(struct fd_transfer
*trans
, const struct pipe_box
*box
)
339 struct fd_resource
*rsc
= fd_resource(trans
->base
.resource
);
340 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, trans
->base
.level
);
341 enum pipe_format format
= trans
->base
.resource
->format
;
343 uint8_t *data
= fd_bo_map(rsc
->bo
) + slice
->offset
+
344 fd_resource_layer_offset(rsc
, slice
, trans
->base
.box
.z
) +
345 ((trans
->base
.box
.y
+ box
->y
) * slice
->pitch
+
346 trans
->base
.box
.x
+ box
->x
) * rsc
->cpp
;
348 uint8_t *source
= trans
->staging
+
349 util_format_get_nblocksy(format
, box
->y
) * trans
->base
.stride
+
350 util_format_get_stride(format
, box
->x
);
353 case PIPE_FORMAT_RGTC1_UNORM
:
354 case PIPE_FORMAT_RGTC1_SNORM
:
355 case PIPE_FORMAT_LATC1_UNORM
:
356 case PIPE_FORMAT_LATC1_SNORM
:
357 util_format_rgtc1_unorm_unpack_rgba_8unorm(
358 data
, slice
->pitch
* rsc
->cpp
,
359 source
, trans
->base
.stride
,
360 box
->width
, box
->height
);
362 case PIPE_FORMAT_RGTC2_UNORM
:
363 case PIPE_FORMAT_RGTC2_SNORM
:
364 case PIPE_FORMAT_LATC2_UNORM
:
365 case PIPE_FORMAT_LATC2_SNORM
:
366 util_format_rgtc2_unorm_unpack_rgba_8unorm(
367 data
, slice
->pitch
* rsc
->cpp
,
368 source
, trans
->base
.stride
,
369 box
->width
, box
->height
);
372 assert(!"Unexpected format\n");
378 fd_resource_flush(struct fd_transfer
*trans
, const struct pipe_box
*box
)
380 enum pipe_format format
= trans
->base
.resource
->format
;
383 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
384 case PIPE_FORMAT_X32_S8X24_UINT
:
385 fd_resource_flush_z32s8(trans
, box
);
387 case PIPE_FORMAT_RGTC1_UNORM
:
388 case PIPE_FORMAT_RGTC1_SNORM
:
389 case PIPE_FORMAT_RGTC2_UNORM
:
390 case PIPE_FORMAT_RGTC2_SNORM
:
391 case PIPE_FORMAT_LATC1_UNORM
:
392 case PIPE_FORMAT_LATC1_SNORM
:
393 case PIPE_FORMAT_LATC2_UNORM
:
394 case PIPE_FORMAT_LATC2_SNORM
:
395 fd_resource_flush_rgtc(trans
, box
);
398 assert(!"Unexpected staging transfer type");
403 static void fd_resource_transfer_flush_region(struct pipe_context
*pctx
,
404 struct pipe_transfer
*ptrans
,
405 const struct pipe_box
*box
)
407 struct fd_resource
*rsc
= fd_resource(ptrans
->resource
);
408 struct fd_transfer
*trans
= fd_transfer(ptrans
);
410 if (ptrans
->resource
->target
== PIPE_BUFFER
)
411 util_range_add(&rsc
->valid_buffer_range
,
412 ptrans
->box
.x
+ box
->x
,
413 ptrans
->box
.x
+ box
->x
+ box
->width
);
416 fd_resource_flush(trans
, box
);
420 fd_resource_transfer_unmap(struct pipe_context
*pctx
,
421 struct pipe_transfer
*ptrans
)
423 struct fd_context
*ctx
= fd_context(pctx
);
424 struct fd_resource
*rsc
= fd_resource(ptrans
->resource
);
425 struct fd_transfer
*trans
= fd_transfer(ptrans
);
427 if (trans
->staging
&& !(ptrans
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
)) {
429 u_box_2d(0, 0, ptrans
->box
.width
, ptrans
->box
.height
, &box
);
430 fd_resource_flush(trans
, &box
);
433 if (!(ptrans
->usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
434 fd_bo_cpu_fini(rsc
->bo
);
436 fd_bo_cpu_fini(rsc
->stencil
->bo
);
439 util_range_add(&rsc
->valid_buffer_range
,
441 ptrans
->box
.x
+ ptrans
->box
.width
);
443 pipe_resource_reference(&ptrans
->resource
, NULL
);
444 util_slab_free(&ctx
->transfer_pool
, ptrans
);
446 free(trans
->staging
);
450 fd_resource_transfer_map(struct pipe_context
*pctx
,
451 struct pipe_resource
*prsc
,
452 unsigned level
, unsigned usage
,
453 const struct pipe_box
*box
,
454 struct pipe_transfer
**pptrans
)
456 struct fd_context
*ctx
= fd_context(pctx
);
457 struct fd_resource
*rsc
= fd_resource(prsc
);
458 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, level
);
459 struct fd_transfer
*trans
;
460 struct pipe_transfer
*ptrans
;
461 enum pipe_format format
= prsc
->format
;
467 DBG("prsc=%p, level=%u, usage=%x, box=%dx%d+%d,%d", prsc
, level
, usage
,
468 box
->width
, box
->height
, box
->x
, box
->y
);
470 ptrans
= util_slab_alloc(&ctx
->transfer_pool
);
474 /* util_slab_alloc() doesn't zero: */
475 trans
= fd_transfer(ptrans
);
476 memset(trans
, 0, sizeof(*trans
));
478 pipe_resource_reference(&ptrans
->resource
, prsc
);
479 ptrans
->level
= level
;
480 ptrans
->usage
= usage
;
482 ptrans
->stride
= util_format_get_nblocksx(format
, slice
->pitch
) * rsc
->cpp
;
483 ptrans
->layer_stride
= rsc
->layer_first
? rsc
->layer_size
: slice
->size0
;
485 if (ctx
->in_shadow
&& !(usage
& PIPE_TRANSFER_READ
))
486 usage
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
488 if (usage
& PIPE_TRANSFER_READ
)
489 op
|= DRM_FREEDRENO_PREP_READ
;
491 if (usage
& PIPE_TRANSFER_WRITE
)
492 op
|= DRM_FREEDRENO_PREP_WRITE
;
494 if (usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) {
495 realloc_bo(rsc
, fd_bo_size(rsc
->bo
));
497 realloc_bo(rsc
->stencil
, fd_bo_size(rsc
->stencil
->bo
));
498 fd_invalidate_resource(ctx
, prsc
);
499 } else if ((usage
& PIPE_TRANSFER_WRITE
) &&
500 prsc
->target
== PIPE_BUFFER
&&
501 !util_ranges_intersect(&rsc
->valid_buffer_range
,
502 box
->x
, box
->x
+ box
->width
)) {
503 /* We are trying to write to a previously uninitialized range. No need
506 } else if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
507 /* If the GPU is writing to the resource, or if it is reading from the
508 * resource and we're trying to write to it, flush the renders.
510 bool needs_flush
= pending(rsc
, !!(usage
& PIPE_TRANSFER_WRITE
));
511 bool busy
= needs_flush
|| (0 != fd_bo_cpu_prep(rsc
->bo
,
512 ctx
->screen
->pipe
, op
| DRM_FREEDRENO_PREP_NOSYNC
));
514 /* if we need to flush/stall, see if we can make a shadow buffer
517 * TODO we could go down this path !reorder && !busy_for_read
518 * ie. we only *don't* want to go down this path if the blit
519 * will trigger a flush!
521 if (ctx
->screen
->reorder
&& busy
&& !(usage
& PIPE_TRANSFER_READ
)) {
522 if (fd_try_shadow_resource(ctx
, rsc
, level
, usage
, box
)) {
523 needs_flush
= busy
= false;
524 fd_invalidate_resource(ctx
, prsc
);
529 if (usage
& PIPE_TRANSFER_WRITE
) {
530 struct fd_batch
*batch
;
531 foreach_batch(batch
, &ctx
->screen
->batch_cache
, rsc
->batch_mask
)
532 fd_batch_flush(batch
);
533 assert(rsc
->batch_mask
== 0);
535 fd_batch_flush(rsc
->write_batch
);
537 assert(!rsc
->write_batch
);
540 /* The GPU keeps track of how the various bo's are being used, and
541 * will wait if necessary for the proper operation to have
545 ret
= fd_bo_cpu_prep(rsc
->bo
, ctx
->screen
->pipe
, op
);
551 buf
= fd_bo_map(rsc
->bo
);
555 offset
= slice
->offset
+
556 box
->y
/ util_format_get_blockheight(format
) * ptrans
->stride
+
557 box
->x
/ util_format_get_blockwidth(format
) * rsc
->cpp
+
558 fd_resource_layer_offset(rsc
, slice
, box
->z
);
560 if (prsc
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
||
561 prsc
->format
== PIPE_FORMAT_X32_S8X24_UINT
) {
562 assert(trans
->base
.box
.depth
== 1);
564 trans
->base
.stride
= trans
->base
.box
.width
* rsc
->cpp
* 2;
565 trans
->staging
= malloc(trans
->base
.stride
* trans
->base
.box
.height
);
569 /* if we're not discarding the whole range (or resource), we must copy
572 if (!(usage
& (PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
|
573 PIPE_TRANSFER_DISCARD_RANGE
))) {
574 struct fd_resource_slice
*sslice
=
575 fd_resource_slice(rsc
->stencil
, level
);
576 void *sbuf
= fd_bo_map(rsc
->stencil
->bo
);
580 float *depth
= (float *)(buf
+ slice
->offset
+
581 fd_resource_layer_offset(rsc
, slice
, box
->z
) +
582 box
->y
* slice
->pitch
* 4 + box
->x
* 4);
583 uint8_t *stencil
= sbuf
+ sslice
->offset
+
584 fd_resource_layer_offset(rsc
->stencil
, sslice
, box
->z
) +
585 box
->y
* sslice
->pitch
+ box
->x
;
587 if (format
!= PIPE_FORMAT_X32_S8X24_UINT
)
588 util_format_z32_float_s8x24_uint_pack_z_float(
589 trans
->staging
, trans
->base
.stride
,
590 depth
, slice
->pitch
* 4,
591 box
->width
, box
->height
);
593 util_format_z32_float_s8x24_uint_pack_s_8uint(
594 trans
->staging
, trans
->base
.stride
,
595 stencil
, sslice
->pitch
,
596 box
->width
, box
->height
);
599 buf
= trans
->staging
;
601 } else if (rsc
->internal_format
!= format
&&
602 util_format_description(format
)->layout
== UTIL_FORMAT_LAYOUT_RGTC
) {
603 assert(trans
->base
.box
.depth
== 1);
605 trans
->base
.stride
= util_format_get_stride(
606 format
, trans
->base
.box
.width
);
607 trans
->staging
= malloc(
608 util_format_get_2d_size(format
, trans
->base
.stride
,
609 trans
->base
.box
.height
));
613 /* if we're not discarding the whole range (or resource), we must copy
616 if (!(usage
& (PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
|
617 PIPE_TRANSFER_DISCARD_RANGE
))) {
618 uint8_t *rgba8
= (uint8_t *)buf
+ slice
->offset
+
619 fd_resource_layer_offset(rsc
, slice
, box
->z
) +
620 box
->y
* slice
->pitch
* rsc
->cpp
+ box
->x
* rsc
->cpp
;
623 case PIPE_FORMAT_RGTC1_UNORM
:
624 case PIPE_FORMAT_RGTC1_SNORM
:
625 case PIPE_FORMAT_LATC1_UNORM
:
626 case PIPE_FORMAT_LATC1_SNORM
:
627 util_format_rgtc1_unorm_pack_rgba_8unorm(
628 trans
->staging
, trans
->base
.stride
,
629 rgba8
, slice
->pitch
* rsc
->cpp
,
630 box
->width
, box
->height
);
632 case PIPE_FORMAT_RGTC2_UNORM
:
633 case PIPE_FORMAT_RGTC2_SNORM
:
634 case PIPE_FORMAT_LATC2_UNORM
:
635 case PIPE_FORMAT_LATC2_SNORM
:
636 util_format_rgtc2_unorm_pack_rgba_8unorm(
637 trans
->staging
, trans
->base
.stride
,
638 rgba8
, slice
->pitch
* rsc
->cpp
,
639 box
->width
, box
->height
);
642 assert(!"Unexpected format");
647 buf
= trans
->staging
;
656 fd_resource_transfer_unmap(pctx
, ptrans
);
661 fd_resource_destroy(struct pipe_screen
*pscreen
,
662 struct pipe_resource
*prsc
)
664 struct fd_resource
*rsc
= fd_resource(prsc
);
665 fd_bc_invalidate_resource(rsc
, true);
668 util_range_destroy(&rsc
->valid_buffer_range
);
673 fd_resource_get_handle(struct pipe_screen
*pscreen
,
674 struct pipe_resource
*prsc
,
675 struct winsys_handle
*handle
)
677 struct fd_resource
*rsc
= fd_resource(prsc
);
679 return fd_screen_bo_get_handle(pscreen
, rsc
->bo
,
680 rsc
->slices
[0].pitch
* rsc
->cpp
, handle
);
684 static const struct u_resource_vtbl fd_resource_vtbl
= {
685 .resource_get_handle
= fd_resource_get_handle
,
686 .resource_destroy
= fd_resource_destroy
,
687 .transfer_map
= fd_resource_transfer_map
,
688 .transfer_flush_region
= fd_resource_transfer_flush_region
,
689 .transfer_unmap
= fd_resource_transfer_unmap
,
693 setup_slices(struct fd_resource
*rsc
, uint32_t alignment
, enum pipe_format format
)
695 struct pipe_resource
*prsc
= &rsc
->base
.b
;
696 enum util_format_layout layout
= util_format_description(format
)->layout
;
697 uint32_t level
, size
= 0;
698 uint32_t width
= prsc
->width0
;
699 uint32_t height
= prsc
->height0
;
700 uint32_t depth
= prsc
->depth0
;
701 /* in layer_first layout, the level (slice) contains just one
702 * layer (since in fact the layer contains the slices)
704 uint32_t layers_in_level
= rsc
->layer_first
? 1 : prsc
->array_size
;
706 for (level
= 0; level
<= prsc
->last_level
; level
++) {
707 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, level
);
710 if (layout
== UTIL_FORMAT_LAYOUT_ASTC
)
711 slice
->pitch
= width
=
712 util_align_npot(width
, 32 * util_format_get_blockwidth(format
));
714 slice
->pitch
= width
= align(width
, 32);
715 slice
->offset
= size
;
716 blocks
= util_format_get_nblocks(format
, width
, height
);
717 /* 1d array and 2d array textures must all have the same layer size
718 * for each miplevel on a3xx. 3d textures can have different layer
719 * sizes for high levels, but the hw auto-sizer is buggy (or at least
720 * different than what this code does), so as soon as the layer size
721 * range gets into range, we stop reducing it.
723 if (prsc
->target
== PIPE_TEXTURE_3D
&& (
725 (level
> 1 && rsc
->slices
[level
- 1].size0
> 0xf000)))
726 slice
->size0
= align(blocks
* rsc
->cpp
, alignment
);
727 else if (level
== 0 || rsc
->layer_first
|| alignment
== 1)
728 slice
->size0
= align(blocks
* rsc
->cpp
, alignment
);
730 slice
->size0
= rsc
->slices
[level
- 1].size0
;
732 size
+= slice
->size0
* depth
* layers_in_level
;
734 width
= u_minify(width
, 1);
735 height
= u_minify(height
, 1);
736 depth
= u_minify(depth
, 1);
743 slice_alignment(struct pipe_screen
*pscreen
, const struct pipe_resource
*tmpl
)
745 /* on a3xx, 2d array and 3d textures seem to want their
746 * layers aligned to page boundaries:
748 switch (tmpl
->target
) {
749 case PIPE_TEXTURE_3D
:
750 case PIPE_TEXTURE_1D_ARRAY
:
751 case PIPE_TEXTURE_2D_ARRAY
:
759 * Create a new texture object, using the given template info.
761 static struct pipe_resource
*
762 fd_resource_create(struct pipe_screen
*pscreen
,
763 const struct pipe_resource
*tmpl
)
765 struct fd_resource
*rsc
= CALLOC_STRUCT(fd_resource
);
766 struct pipe_resource
*prsc
= &rsc
->base
.b
;
767 enum pipe_format format
= tmpl
->format
;
768 uint32_t size
, alignment
;
770 DBG("%p: target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
771 "nr_samples=%u, usage=%u, bind=%x, flags=%x", prsc
,
772 tmpl
->target
, util_format_name(format
),
773 tmpl
->width0
, tmpl
->height0
, tmpl
->depth0
,
774 tmpl
->array_size
, tmpl
->last_level
, tmpl
->nr_samples
,
775 tmpl
->usage
, tmpl
->bind
, tmpl
->flags
);
782 pipe_reference_init(&prsc
->reference
, 1);
784 prsc
->screen
= pscreen
;
786 util_range_init(&rsc
->valid_buffer_range
);
788 rsc
->base
.vtbl
= &fd_resource_vtbl
;
790 if (format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
)
791 format
= PIPE_FORMAT_Z32_FLOAT
;
792 else if (fd_screen(pscreen
)->gpu_id
< 400 &&
793 util_format_description(format
)->layout
== UTIL_FORMAT_LAYOUT_RGTC
)
794 format
= PIPE_FORMAT_R8G8B8A8_UNORM
;
795 rsc
->internal_format
= format
;
796 rsc
->cpp
= util_format_get_blocksize(format
);
800 alignment
= slice_alignment(pscreen
, tmpl
);
801 if (is_a4xx(fd_screen(pscreen
))) {
802 switch (tmpl
->target
) {
803 case PIPE_TEXTURE_3D
:
804 rsc
->layer_first
= false;
807 rsc
->layer_first
= true;
813 size
= setup_slices(rsc
, alignment
, format
);
815 if (rsc
->layer_first
) {
816 rsc
->layer_size
= align(size
, 4096);
817 size
= rsc
->layer_size
* prsc
->array_size
;
820 realloc_bo(rsc
, size
);
824 /* There is no native Z32F_S8 sampling or rendering format, so this must
825 * be emulated via two separate textures. The depth texture still keeps
826 * its Z32F_S8 format though, and we also keep a reference to a separate
829 if (tmpl
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
) {
830 struct pipe_resource stencil
= *tmpl
;
831 stencil
.format
= PIPE_FORMAT_S8_UINT
;
832 rsc
->stencil
= fd_resource(fd_resource_create(pscreen
, &stencil
));
839 fd_resource_destroy(pscreen
, prsc
);
844 * Create a texture from a winsys_handle. The handle is often created in
845 * another process by first creating a pipe texture and then calling
846 * resource_get_handle.
848 static struct pipe_resource
*
849 fd_resource_from_handle(struct pipe_screen
*pscreen
,
850 const struct pipe_resource
*tmpl
,
851 struct winsys_handle
*handle
, unsigned usage
)
853 struct fd_resource
*rsc
= CALLOC_STRUCT(fd_resource
);
854 struct fd_resource_slice
*slice
= &rsc
->slices
[0];
855 struct pipe_resource
*prsc
= &rsc
->base
.b
;
857 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
858 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
859 tmpl
->target
, util_format_name(tmpl
->format
),
860 tmpl
->width0
, tmpl
->height0
, tmpl
->depth0
,
861 tmpl
->array_size
, tmpl
->last_level
, tmpl
->nr_samples
,
862 tmpl
->usage
, tmpl
->bind
, tmpl
->flags
);
869 pipe_reference_init(&prsc
->reference
, 1);
871 prsc
->screen
= pscreen
;
873 util_range_init(&rsc
->valid_buffer_range
);
875 rsc
->bo
= fd_screen_bo_from_handle(pscreen
, handle
, &slice
->pitch
);
879 rsc
->base
.vtbl
= &fd_resource_vtbl
;
880 rsc
->cpp
= util_format_get_blocksize(tmpl
->format
);
881 slice
->pitch
/= rsc
->cpp
;
882 slice
->offset
= handle
->offset
;
889 fd_resource_destroy(pscreen
, prsc
);
894 * _copy_region using pipe (3d engine)
897 fd_blitter_pipe_copy_region(struct fd_context
*ctx
,
898 struct pipe_resource
*dst
,
900 unsigned dstx
, unsigned dsty
, unsigned dstz
,
901 struct pipe_resource
*src
,
903 const struct pipe_box
*src_box
)
905 /* not until we allow rendertargets to be buffers */
906 if (dst
->target
== PIPE_BUFFER
|| src
->target
== PIPE_BUFFER
)
909 if (!util_blitter_is_copy_supported(ctx
->blitter
, dst
, src
))
912 fd_blitter_pipe_begin(ctx
, false);
913 util_blitter_copy_texture(ctx
->blitter
,
914 dst
, dst_level
, dstx
, dsty
, dstz
,
915 src
, src_level
, src_box
);
916 fd_blitter_pipe_end(ctx
);
922 * Copy a block of pixels from one resource to another.
923 * The resource must be of the same format.
924 * Resources with nr_samples > 1 are not allowed.
927 fd_resource_copy_region(struct pipe_context
*pctx
,
928 struct pipe_resource
*dst
,
930 unsigned dstx
, unsigned dsty
, unsigned dstz
,
931 struct pipe_resource
*src
,
933 const struct pipe_box
*src_box
)
935 struct fd_context
*ctx
= fd_context(pctx
);
937 /* TODO if we have 2d core, or other DMA engine that could be used
938 * for simple copies and reasonably easily synchronized with the 3d
939 * core, this is where we'd plug it in..
942 /* try blit on 3d pipe: */
943 if (fd_blitter_pipe_copy_region(ctx
,
944 dst
, dst_level
, dstx
, dsty
, dstz
,
945 src
, src_level
, src_box
))
948 /* else fallback to pure sw: */
949 util_resource_copy_region(pctx
,
950 dst
, dst_level
, dstx
, dsty
, dstz
,
951 src
, src_level
, src_box
);
955 fd_render_condition_check(struct pipe_context
*pctx
)
957 struct fd_context
*ctx
= fd_context(pctx
);
959 if (!ctx
->cond_query
)
962 union pipe_query_result res
= { 0 };
964 ctx
->cond_mode
!= PIPE_RENDER_COND_NO_WAIT
&&
965 ctx
->cond_mode
!= PIPE_RENDER_COND_BY_REGION_NO_WAIT
;
967 if (pctx
->get_query_result(pctx
, ctx
->cond_query
, wait
, &res
))
968 return (bool)res
.u64
!= ctx
->cond_cond
;
974 * Optimal hardware path for blitting pixels.
975 * Scaling, format conversion, up- and downsampling (resolve) are allowed.
978 fd_blit(struct pipe_context
*pctx
, const struct pipe_blit_info
*blit_info
)
980 struct fd_context
*ctx
= fd_context(pctx
);
981 struct pipe_blit_info info
= *blit_info
;
983 if (info
.src
.resource
->nr_samples
> 1 &&
984 info
.dst
.resource
->nr_samples
<= 1 &&
985 !util_format_is_depth_or_stencil(info
.src
.resource
->format
) &&
986 !util_format_is_pure_integer(info
.src
.resource
->format
)) {
987 DBG("color resolve unimplemented");
991 if (info
.render_condition_enable
&& !fd_render_condition_check(pctx
))
994 if (util_try_blit_via_copy_region(pctx
, &info
)) {
998 if (info
.mask
& PIPE_MASK_S
) {
999 DBG("cannot blit stencil, skipping");
1000 info
.mask
&= ~PIPE_MASK_S
;
1003 if (!util_blitter_is_blit_supported(ctx
->blitter
, &info
)) {
1004 DBG("blit unsupported %s -> %s",
1005 util_format_short_name(info
.src
.resource
->format
),
1006 util_format_short_name(info
.dst
.resource
->format
));
1010 fd_blitter_pipe_begin(ctx
, info
.render_condition_enable
);
1011 util_blitter_blit(ctx
->blitter
, &info
);
1012 fd_blitter_pipe_end(ctx
);
1016 fd_blitter_pipe_begin(struct fd_context
*ctx
, bool render_cond
)
1018 util_blitter_save_vertex_buffer_slot(ctx
->blitter
, ctx
->vtx
.vertexbuf
.vb
);
1019 util_blitter_save_vertex_elements(ctx
->blitter
, ctx
->vtx
.vtx
);
1020 util_blitter_save_vertex_shader(ctx
->blitter
, ctx
->prog
.vp
);
1021 util_blitter_save_so_targets(ctx
->blitter
, ctx
->streamout
.num_targets
,
1022 ctx
->streamout
.targets
);
1023 util_blitter_save_rasterizer(ctx
->blitter
, ctx
->rasterizer
);
1024 util_blitter_save_viewport(ctx
->blitter
, &ctx
->viewport
);
1025 util_blitter_save_scissor(ctx
->blitter
, &ctx
->scissor
);
1026 util_blitter_save_fragment_shader(ctx
->blitter
, ctx
->prog
.fp
);
1027 util_blitter_save_blend(ctx
->blitter
, ctx
->blend
);
1028 util_blitter_save_depth_stencil_alpha(ctx
->blitter
, ctx
->zsa
);
1029 util_blitter_save_stencil_ref(ctx
->blitter
, &ctx
->stencil_ref
);
1030 util_blitter_save_sample_mask(ctx
->blitter
, ctx
->sample_mask
);
1031 util_blitter_save_framebuffer(ctx
->blitter
,
1032 ctx
->batch
? &ctx
->batch
->framebuffer
: NULL
);
1033 util_blitter_save_fragment_sampler_states(ctx
->blitter
,
1034 ctx
->fragtex
.num_samplers
,
1035 (void **)ctx
->fragtex
.samplers
);
1036 util_blitter_save_fragment_sampler_views(ctx
->blitter
,
1037 ctx
->fragtex
.num_textures
, ctx
->fragtex
.textures
);
1039 util_blitter_save_render_condition(ctx
->blitter
,
1040 ctx
->cond_query
, ctx
->cond_cond
, ctx
->cond_mode
);
1042 fd_hw_query_set_stage(ctx
, ctx
->batch
->draw
, FD_STAGE_BLIT
);
1046 fd_blitter_pipe_end(struct fd_context
*ctx
)
1048 fd_hw_query_set_stage(ctx
, ctx
->batch
->draw
, FD_STAGE_NULL
);
1052 fd_flush_resource(struct pipe_context
*pctx
, struct pipe_resource
*prsc
)
1054 struct fd_resource
*rsc
= fd_resource(prsc
);
1056 if (rsc
->write_batch
)
1057 fd_batch_flush(rsc
->write_batch
);
1059 assert(!rsc
->write_batch
);
1063 fd_resource_screen_init(struct pipe_screen
*pscreen
)
1065 pscreen
->resource_create
= fd_resource_create
;
1066 pscreen
->resource_from_handle
= fd_resource_from_handle
;
1067 pscreen
->resource_get_handle
= u_resource_get_handle_vtbl
;
1068 pscreen
->resource_destroy
= u_resource_destroy_vtbl
;
1072 fd_resource_context_init(struct pipe_context
*pctx
)
1074 pctx
->transfer_map
= u_transfer_map_vtbl
;
1075 pctx
->transfer_flush_region
= u_transfer_flush_region_vtbl
;
1076 pctx
->transfer_unmap
= u_transfer_unmap_vtbl
;
1077 pctx
->buffer_subdata
= u_default_buffer_subdata
;
1078 pctx
->texture_subdata
= u_default_texture_subdata
;
1079 pctx
->create_surface
= fd_create_surface
;
1080 pctx
->surface_destroy
= fd_surface_destroy
;
1081 pctx
->resource_copy_region
= fd_resource_copy_region
;
1082 pctx
->blit
= fd_blit
;
1083 pctx
->flush_resource
= fd_flush_resource
;