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"
37 #include "freedreno_resource.h"
38 #include "freedreno_screen.h"
39 #include "freedreno_surface.h"
40 #include "freedreno_context.h"
41 #include "freedreno_query_hw.h"
42 #include "freedreno_util.h"
48 pending(struct fd_resource
*rsc
, enum fd_resource_status status
)
50 return (rsc
->status
& status
) ||
51 (rsc
->stencil
&& (rsc
->stencil
->status
& status
));
55 fd_invalidate_resource(struct fd_context
*ctx
, struct pipe_resource
*prsc
)
59 /* Go through the entire state and see if the resource is bound
60 * anywhere. If it is, mark the relevant state as dirty. This is called on
65 for (i
= 1; i
< PIPE_MAX_CONSTANT_BUFFERS
&& !(ctx
->dirty
& FD_DIRTY_CONSTBUF
); i
++) {
66 if (ctx
->constbuf
[PIPE_SHADER_VERTEX
].cb
[i
].buffer
== prsc
)
67 ctx
->dirty
|= FD_DIRTY_CONSTBUF
;
68 if (ctx
->constbuf
[PIPE_SHADER_FRAGMENT
].cb
[i
].buffer
== prsc
)
69 ctx
->dirty
|= FD_DIRTY_CONSTBUF
;
73 for (i
= 0; i
< ctx
->vtx
.vertexbuf
.count
&& !(ctx
->dirty
& FD_DIRTY_VTXBUF
); i
++) {
74 if (ctx
->vtx
.vertexbuf
.vb
[i
].buffer
== prsc
)
75 ctx
->dirty
|= FD_DIRTY_VTXBUF
;
79 if (ctx
->indexbuf
.buffer
== prsc
)
80 ctx
->dirty
|= FD_DIRTY_INDEXBUF
;
83 for (i
= 0; i
< ctx
->verttex
.num_textures
&& !(ctx
->dirty
& FD_DIRTY_VERTTEX
); i
++) {
84 if (ctx
->verttex
.textures
[i
] && (ctx
->verttex
.textures
[i
]->texture
== prsc
))
85 ctx
->dirty
|= FD_DIRTY_VERTTEX
;
87 for (i
= 0; i
< ctx
->fragtex
.num_textures
&& !(ctx
->dirty
& FD_DIRTY_FRAGTEX
); i
++) {
88 if (ctx
->fragtex
.textures
[i
] && (ctx
->fragtex
.textures
[i
]->texture
== prsc
))
89 ctx
->dirty
|= FD_DIRTY_FRAGTEX
;
94 realloc_bo(struct fd_resource
*rsc
, uint32_t size
)
96 struct fd_screen
*screen
= fd_screen(rsc
->base
.b
.screen
);
97 uint32_t flags
= DRM_FREEDRENO_GEM_CACHE_WCOMBINE
|
98 DRM_FREEDRENO_GEM_TYPE_KMEM
; /* TODO */
100 /* if we start using things other than write-combine,
101 * be sure to check for PIPE_RESOURCE_FLAG_MAP_COHERENT
107 rsc
->bo
= fd_bo_new(screen
->dev
, size
, flags
);
110 rsc
->pending_ctx
= NULL
;
111 list_delinit(&rsc
->list
);
112 util_range_set_empty(&rsc
->valid_buffer_range
);
116 fd_resource_layer_offset(struct fd_resource
*rsc
,
117 struct fd_resource_slice
*slice
,
120 if (rsc
->layer_first
)
121 return layer
* rsc
->layer_size
;
123 return layer
* slice
->size0
;
127 fd_resource_flush_z32s8(struct fd_transfer
*trans
, const struct pipe_box
*box
)
129 struct fd_resource
*rsc
= fd_resource(trans
->base
.resource
);
130 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, trans
->base
.level
);
131 struct fd_resource_slice
*sslice
= fd_resource_slice(rsc
->stencil
, trans
->base
.level
);
132 enum pipe_format format
= trans
->base
.resource
->format
;
134 float *depth
= fd_bo_map(rsc
->bo
) + slice
->offset
+
135 fd_resource_layer_offset(rsc
, slice
, trans
->base
.box
.z
) +
136 (trans
->base
.box
.y
+ box
->y
) * slice
->pitch
* 4 + (trans
->base
.box
.x
+ box
->x
) * 4;
137 uint8_t *stencil
= fd_bo_map(rsc
->stencil
->bo
) + sslice
->offset
+
138 fd_resource_layer_offset(rsc
->stencil
, sslice
, trans
->base
.box
.z
) +
139 (trans
->base
.box
.y
+ box
->y
) * sslice
->pitch
+ trans
->base
.box
.x
+ box
->x
;
141 if (format
!= PIPE_FORMAT_X32_S8X24_UINT
)
142 util_format_z32_float_s8x24_uint_unpack_z_float(
143 depth
, slice
->pitch
* 4,
144 trans
->staging
, trans
->base
.stride
,
145 box
->width
, box
->height
);
147 util_format_z32_float_s8x24_uint_unpack_s_8uint(
148 stencil
, sslice
->pitch
,
149 trans
->staging
, trans
->base
.stride
,
150 box
->width
, box
->height
);
154 fd_resource_flush_rgtc(struct fd_transfer
*trans
, const struct pipe_box
*box
)
156 struct fd_resource
*rsc
= fd_resource(trans
->base
.resource
);
157 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, trans
->base
.level
);
158 enum pipe_format format
= trans
->base
.resource
->format
;
160 uint8_t *data
= fd_bo_map(rsc
->bo
) + slice
->offset
+
161 fd_resource_layer_offset(rsc
, slice
, trans
->base
.box
.z
) +
162 ((trans
->base
.box
.y
+ box
->y
) * slice
->pitch
+
163 trans
->base
.box
.x
+ box
->x
) * rsc
->cpp
;
165 uint8_t *source
= trans
->staging
+
166 util_format_get_nblocksy(format
, box
->y
) * trans
->base
.stride
+
167 util_format_get_stride(format
, box
->x
);
170 case PIPE_FORMAT_RGTC1_UNORM
:
171 case PIPE_FORMAT_RGTC1_SNORM
:
172 case PIPE_FORMAT_LATC1_UNORM
:
173 case PIPE_FORMAT_LATC1_SNORM
:
174 util_format_rgtc1_unorm_unpack_rgba_8unorm(
175 data
, slice
->pitch
* rsc
->cpp
,
176 source
, trans
->base
.stride
,
177 box
->width
, box
->height
);
179 case PIPE_FORMAT_RGTC2_UNORM
:
180 case PIPE_FORMAT_RGTC2_SNORM
:
181 case PIPE_FORMAT_LATC2_UNORM
:
182 case PIPE_FORMAT_LATC2_SNORM
:
183 util_format_rgtc2_unorm_unpack_rgba_8unorm(
184 data
, slice
->pitch
* rsc
->cpp
,
185 source
, trans
->base
.stride
,
186 box
->width
, box
->height
);
189 assert(!"Unexpected format\n");
195 fd_resource_flush(struct fd_transfer
*trans
, const struct pipe_box
*box
)
197 enum pipe_format format
= trans
->base
.resource
->format
;
200 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
201 case PIPE_FORMAT_X32_S8X24_UINT
:
202 fd_resource_flush_z32s8(trans
, box
);
204 case PIPE_FORMAT_RGTC1_UNORM
:
205 case PIPE_FORMAT_RGTC1_SNORM
:
206 case PIPE_FORMAT_RGTC2_UNORM
:
207 case PIPE_FORMAT_RGTC2_SNORM
:
208 case PIPE_FORMAT_LATC1_UNORM
:
209 case PIPE_FORMAT_LATC1_SNORM
:
210 case PIPE_FORMAT_LATC2_UNORM
:
211 case PIPE_FORMAT_LATC2_SNORM
:
212 fd_resource_flush_rgtc(trans
, box
);
215 assert(!"Unexpected staging transfer type");
220 static void fd_resource_transfer_flush_region(struct pipe_context
*pctx
,
221 struct pipe_transfer
*ptrans
,
222 const struct pipe_box
*box
)
224 struct fd_resource
*rsc
= fd_resource(ptrans
->resource
);
225 struct fd_transfer
*trans
= fd_transfer(ptrans
);
227 if (ptrans
->resource
->target
== PIPE_BUFFER
)
228 util_range_add(&rsc
->valid_buffer_range
,
229 ptrans
->box
.x
+ box
->x
,
230 ptrans
->box
.x
+ box
->x
+ box
->width
);
233 fd_resource_flush(trans
, box
);
237 fd_resource_transfer_unmap(struct pipe_context
*pctx
,
238 struct pipe_transfer
*ptrans
)
240 struct fd_context
*ctx
= fd_context(pctx
);
241 struct fd_resource
*rsc
= fd_resource(ptrans
->resource
);
242 struct fd_transfer
*trans
= fd_transfer(ptrans
);
244 if (trans
->staging
&& !(ptrans
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
)) {
246 u_box_2d(0, 0, ptrans
->box
.width
, ptrans
->box
.height
, &box
);
247 fd_resource_flush(trans
, &box
);
250 if (!(ptrans
->usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
251 fd_bo_cpu_fini(rsc
->bo
);
253 fd_bo_cpu_fini(rsc
->stencil
->bo
);
256 util_range_add(&rsc
->valid_buffer_range
,
258 ptrans
->box
.x
+ ptrans
->box
.width
);
260 pipe_resource_reference(&ptrans
->resource
, NULL
);
261 util_slab_free(&ctx
->transfer_pool
, ptrans
);
264 free(trans
->staging
);
268 fd_resource_transfer_map(struct pipe_context
*pctx
,
269 struct pipe_resource
*prsc
,
270 unsigned level
, unsigned usage
,
271 const struct pipe_box
*box
,
272 struct pipe_transfer
**pptrans
)
274 struct fd_context
*ctx
= fd_context(pctx
);
275 struct fd_resource
*rsc
= fd_resource(prsc
);
276 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, level
);
277 struct fd_transfer
*trans
;
278 struct pipe_transfer
*ptrans
;
279 enum pipe_format format
= prsc
->format
;
285 DBG("prsc=%p, level=%u, usage=%x, box=%dx%d+%d,%d", prsc
, level
, usage
,
286 box
->width
, box
->height
, box
->x
, box
->y
);
288 ptrans
= util_slab_alloc(&ctx
->transfer_pool
);
292 /* util_slab_alloc() doesn't zero: */
293 trans
= fd_transfer(ptrans
);
294 memset(trans
, 0, sizeof(*trans
));
296 pipe_resource_reference(&ptrans
->resource
, prsc
);
297 ptrans
->level
= level
;
298 ptrans
->usage
= usage
;
300 ptrans
->stride
= util_format_get_nblocksx(format
, slice
->pitch
) * rsc
->cpp
;
301 ptrans
->layer_stride
= slice
->size0
;
303 if (usage
& PIPE_TRANSFER_READ
)
304 op
|= DRM_FREEDRENO_PREP_READ
;
306 if (usage
& PIPE_TRANSFER_WRITE
)
307 op
|= DRM_FREEDRENO_PREP_WRITE
;
309 if (usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) {
310 realloc_bo(rsc
, fd_bo_size(rsc
->bo
));
312 realloc_bo(rsc
->stencil
, fd_bo_size(rsc
->stencil
->bo
));
313 fd_invalidate_resource(ctx
, prsc
);
314 } else if ((usage
& PIPE_TRANSFER_WRITE
) &&
315 prsc
->target
== PIPE_BUFFER
&&
316 !util_ranges_intersect(&rsc
->valid_buffer_range
,
317 box
->x
, box
->x
+ box
->width
)) {
318 /* We are trying to write to a previously uninitialized range. No need
321 } else if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
322 /* If the GPU is writing to the resource, or if it is reading from the
323 * resource and we're trying to write to it, flush the renders.
325 if (((ptrans
->usage
& PIPE_TRANSFER_WRITE
) &&
326 pending(rsc
, FD_PENDING_READ
| FD_PENDING_WRITE
)) ||
327 pending(rsc
, FD_PENDING_WRITE
))
328 fd_context_render(pctx
);
330 /* The GPU keeps track of how the various bo's are being used, and
331 * will wait if necessary for the proper operation to have
334 ret
= fd_bo_cpu_prep(rsc
->bo
, ctx
->screen
->pipe
, op
);
339 buf
= fd_bo_map(rsc
->bo
);
343 offset
= slice
->offset
+
344 box
->y
/ util_format_get_blockheight(format
) * ptrans
->stride
+
345 box
->x
/ util_format_get_blockwidth(format
) * rsc
->cpp
+
346 fd_resource_layer_offset(rsc
, slice
, box
->z
);
348 if (prsc
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
||
349 prsc
->format
== PIPE_FORMAT_X32_S8X24_UINT
) {
350 assert(trans
->base
.box
.depth
== 1);
352 trans
->base
.stride
= trans
->base
.box
.width
* rsc
->cpp
* 2;
353 trans
->staging
= malloc(trans
->base
.stride
* trans
->base
.box
.height
);
357 /* if we're not discarding the whole range (or resource), we must copy
360 if (!(usage
& (PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
|
361 PIPE_TRANSFER_DISCARD_RANGE
))) {
362 struct fd_resource_slice
*sslice
=
363 fd_resource_slice(rsc
->stencil
, level
);
364 void *sbuf
= fd_bo_map(rsc
->stencil
->bo
);
368 float *depth
= (float *)(buf
+ slice
->offset
+
369 fd_resource_layer_offset(rsc
, slice
, box
->z
) +
370 box
->y
* slice
->pitch
* 4 + box
->x
* 4);
371 uint8_t *stencil
= sbuf
+ sslice
->offset
+
372 fd_resource_layer_offset(rsc
->stencil
, sslice
, box
->z
) +
373 box
->y
* sslice
->pitch
+ box
->x
;
375 if (format
!= PIPE_FORMAT_X32_S8X24_UINT
)
376 util_format_z32_float_s8x24_uint_pack_z_float(
377 trans
->staging
, trans
->base
.stride
,
378 depth
, slice
->pitch
* 4,
379 box
->width
, box
->height
);
381 util_format_z32_float_s8x24_uint_pack_s_8uint(
382 trans
->staging
, trans
->base
.stride
,
383 stencil
, sslice
->pitch
,
384 box
->width
, box
->height
);
387 buf
= trans
->staging
;
389 } else if (rsc
->internal_format
!= format
&&
390 util_format_description(format
)->layout
== UTIL_FORMAT_LAYOUT_RGTC
) {
391 assert(trans
->base
.box
.depth
== 1);
393 trans
->base
.stride
= util_format_get_stride(
394 format
, trans
->base
.box
.width
);
395 trans
->staging
= malloc(
396 util_format_get_2d_size(format
, trans
->base
.stride
,
397 trans
->base
.box
.height
));
401 /* if we're not discarding the whole range (or resource), we must copy
404 if (!(usage
& (PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
|
405 PIPE_TRANSFER_DISCARD_RANGE
))) {
406 uint8_t *rgba8
= (uint8_t *)buf
+ slice
->offset
+
407 fd_resource_layer_offset(rsc
, slice
, box
->z
) +
408 box
->y
* slice
->pitch
* rsc
->cpp
+ box
->x
* rsc
->cpp
;
411 case PIPE_FORMAT_RGTC1_UNORM
:
412 case PIPE_FORMAT_RGTC1_SNORM
:
413 case PIPE_FORMAT_LATC1_UNORM
:
414 case PIPE_FORMAT_LATC1_SNORM
:
415 util_format_rgtc1_unorm_pack_rgba_8unorm(
416 trans
->staging
, trans
->base
.stride
,
417 rgba8
, slice
->pitch
* rsc
->cpp
,
418 box
->width
, box
->height
);
420 case PIPE_FORMAT_RGTC2_UNORM
:
421 case PIPE_FORMAT_RGTC2_SNORM
:
422 case PIPE_FORMAT_LATC2_UNORM
:
423 case PIPE_FORMAT_LATC2_SNORM
:
424 util_format_rgtc2_unorm_pack_rgba_8unorm(
425 trans
->staging
, trans
->base
.stride
,
426 rgba8
, slice
->pitch
* rsc
->cpp
,
427 box
->width
, box
->height
);
430 assert(!"Unexpected format");
435 buf
= trans
->staging
;
444 fd_resource_transfer_unmap(pctx
, ptrans
);
449 fd_resource_destroy(struct pipe_screen
*pscreen
,
450 struct pipe_resource
*prsc
)
452 struct fd_resource
*rsc
= fd_resource(prsc
);
455 list_delinit(&rsc
->list
);
456 util_range_destroy(&rsc
->valid_buffer_range
);
461 fd_resource_get_handle(struct pipe_screen
*pscreen
,
462 struct pipe_resource
*prsc
,
463 struct winsys_handle
*handle
)
465 struct fd_resource
*rsc
= fd_resource(prsc
);
467 return fd_screen_bo_get_handle(pscreen
, rsc
->bo
,
468 rsc
->slices
[0].pitch
* rsc
->cpp
, handle
);
472 static const struct u_resource_vtbl fd_resource_vtbl
= {
473 .resource_get_handle
= fd_resource_get_handle
,
474 .resource_destroy
= fd_resource_destroy
,
475 .transfer_map
= fd_resource_transfer_map
,
476 .transfer_flush_region
= fd_resource_transfer_flush_region
,
477 .transfer_unmap
= fd_resource_transfer_unmap
,
478 .transfer_inline_write
= u_default_transfer_inline_write
,
482 setup_slices(struct fd_resource
*rsc
, uint32_t alignment
, enum pipe_format format
)
484 struct pipe_resource
*prsc
= &rsc
->base
.b
;
485 enum util_format_layout layout
= util_format_description(format
)->layout
;
486 uint32_t level
, size
= 0;
487 uint32_t width
= prsc
->width0
;
488 uint32_t height
= prsc
->height0
;
489 uint32_t depth
= prsc
->depth0
;
490 /* in layer_first layout, the level (slice) contains just one
491 * layer (since in fact the layer contains the slices)
493 uint32_t layers_in_level
= rsc
->layer_first
? 1 : prsc
->array_size
;
495 for (level
= 0; level
<= prsc
->last_level
; level
++) {
496 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, level
);
499 if (layout
== UTIL_FORMAT_LAYOUT_ASTC
)
500 slice
->pitch
= width
=
501 util_align_npot(width
, 32 * util_format_get_blockwidth(format
));
503 slice
->pitch
= width
= align(width
, 32);
504 slice
->offset
= size
;
505 blocks
= util_format_get_nblocks(format
, width
, height
);
506 /* 1d array and 2d array textures must all have the same layer size
507 * for each miplevel on a3xx. 3d textures can have different layer
508 * sizes for high levels, but the hw auto-sizer is buggy (or at least
509 * different than what this code does), so as soon as the layer size
510 * range gets into range, we stop reducing it.
512 if (prsc
->target
== PIPE_TEXTURE_3D
&& (
514 (level
> 1 && rsc
->slices
[level
- 1].size0
> 0xf000)))
515 slice
->size0
= align(blocks
* rsc
->cpp
, alignment
);
516 else if (level
== 0 || rsc
->layer_first
|| alignment
== 1)
517 slice
->size0
= align(blocks
* rsc
->cpp
, alignment
);
519 slice
->size0
= rsc
->slices
[level
- 1].size0
;
521 size
+= slice
->size0
* depth
* layers_in_level
;
523 width
= u_minify(width
, 1);
524 height
= u_minify(height
, 1);
525 depth
= u_minify(depth
, 1);
532 slice_alignment(struct pipe_screen
*pscreen
, const struct pipe_resource
*tmpl
)
534 /* on a3xx, 2d array and 3d textures seem to want their
535 * layers aligned to page boundaries:
537 switch (tmpl
->target
) {
538 case PIPE_TEXTURE_3D
:
539 case PIPE_TEXTURE_1D_ARRAY
:
540 case PIPE_TEXTURE_2D_ARRAY
:
548 * Create a new texture object, using the given template info.
550 static struct pipe_resource
*
551 fd_resource_create(struct pipe_screen
*pscreen
,
552 const struct pipe_resource
*tmpl
)
554 struct fd_resource
*rsc
= CALLOC_STRUCT(fd_resource
);
555 struct pipe_resource
*prsc
= &rsc
->base
.b
;
556 enum pipe_format format
= tmpl
->format
;
557 uint32_t size
, alignment
;
559 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
560 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
561 tmpl
->target
, util_format_name(format
),
562 tmpl
->width0
, tmpl
->height0
, tmpl
->depth0
,
563 tmpl
->array_size
, tmpl
->last_level
, tmpl
->nr_samples
,
564 tmpl
->usage
, tmpl
->bind
, tmpl
->flags
);
571 pipe_reference_init(&prsc
->reference
, 1);
572 list_inithead(&rsc
->list
);
573 prsc
->screen
= pscreen
;
575 util_range_init(&rsc
->valid_buffer_range
);
577 rsc
->base
.vtbl
= &fd_resource_vtbl
;
579 if (format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
)
580 format
= PIPE_FORMAT_Z32_FLOAT
;
581 else if (fd_screen(pscreen
)->gpu_id
< 400 &&
582 util_format_description(format
)->layout
== UTIL_FORMAT_LAYOUT_RGTC
)
583 format
= PIPE_FORMAT_R8G8B8A8_UNORM
;
584 rsc
->internal_format
= format
;
585 rsc
->cpp
= util_format_get_blocksize(format
);
589 alignment
= slice_alignment(pscreen
, tmpl
);
590 if (is_a4xx(fd_screen(pscreen
))) {
591 switch (tmpl
->target
) {
592 case PIPE_TEXTURE_3D
:
593 rsc
->layer_first
= false;
596 rsc
->layer_first
= true;
602 size
= setup_slices(rsc
, alignment
, format
);
604 if (rsc
->layer_first
) {
605 rsc
->layer_size
= align(size
, 4096);
606 size
= rsc
->layer_size
* prsc
->array_size
;
609 realloc_bo(rsc
, size
);
613 /* There is no native Z32F_S8 sampling or rendering format, so this must
614 * be emulated via two separate textures. The depth texture still keeps
615 * its Z32F_S8 format though, and we also keep a reference to a separate
618 if (tmpl
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
) {
619 struct pipe_resource stencil
= *tmpl
;
620 stencil
.format
= PIPE_FORMAT_S8_UINT
;
621 rsc
->stencil
= fd_resource(fd_resource_create(pscreen
, &stencil
));
628 fd_resource_destroy(pscreen
, prsc
);
633 * Create a texture from a winsys_handle. The handle is often created in
634 * another process by first creating a pipe texture and then calling
635 * resource_get_handle.
637 static struct pipe_resource
*
638 fd_resource_from_handle(struct pipe_screen
*pscreen
,
639 const struct pipe_resource
*tmpl
,
640 struct winsys_handle
*handle
,
643 struct fd_resource
*rsc
= CALLOC_STRUCT(fd_resource
);
644 struct fd_resource_slice
*slice
= &rsc
->slices
[0];
645 struct pipe_resource
*prsc
= &rsc
->base
.b
;
647 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
648 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
649 tmpl
->target
, util_format_name(tmpl
->format
),
650 tmpl
->width0
, tmpl
->height0
, tmpl
->depth0
,
651 tmpl
->array_size
, tmpl
->last_level
, tmpl
->nr_samples
,
652 tmpl
->usage
, tmpl
->bind
, tmpl
->flags
);
659 pipe_reference_init(&prsc
->reference
, 1);
660 list_inithead(&rsc
->list
);
661 prsc
->screen
= pscreen
;
663 util_range_init(&rsc
->valid_buffer_range
);
665 rsc
->bo
= fd_screen_bo_from_handle(pscreen
, handle
, &slice
->pitch
);
669 rsc
->base
.vtbl
= &fd_resource_vtbl
;
670 rsc
->cpp
= util_format_get_blocksize(tmpl
->format
);
671 slice
->pitch
/= rsc
->cpp
;
678 fd_resource_destroy(pscreen
, prsc
);
682 static void fd_blitter_pipe_begin(struct fd_context
*ctx
, bool render_cond
);
683 static void fd_blitter_pipe_end(struct fd_context
*ctx
);
686 * _copy_region using pipe (3d engine)
689 fd_blitter_pipe_copy_region(struct fd_context
*ctx
,
690 struct pipe_resource
*dst
,
692 unsigned dstx
, unsigned dsty
, unsigned dstz
,
693 struct pipe_resource
*src
,
695 const struct pipe_box
*src_box
)
697 /* not until we allow rendertargets to be buffers */
698 if (dst
->target
== PIPE_BUFFER
|| src
->target
== PIPE_BUFFER
)
701 if (!util_blitter_is_copy_supported(ctx
->blitter
, dst
, src
))
704 fd_blitter_pipe_begin(ctx
, false);
705 util_blitter_copy_texture(ctx
->blitter
,
706 dst
, dst_level
, dstx
, dsty
, dstz
,
707 src
, src_level
, src_box
);
708 fd_blitter_pipe_end(ctx
);
714 * Copy a block of pixels from one resource to another.
715 * The resource must be of the same format.
716 * Resources with nr_samples > 1 are not allowed.
719 fd_resource_copy_region(struct pipe_context
*pctx
,
720 struct pipe_resource
*dst
,
722 unsigned dstx
, unsigned dsty
, unsigned dstz
,
723 struct pipe_resource
*src
,
725 const struct pipe_box
*src_box
)
727 struct fd_context
*ctx
= fd_context(pctx
);
729 /* TODO if we have 2d core, or other DMA engine that could be used
730 * for simple copies and reasonably easily synchronized with the 3d
731 * core, this is where we'd plug it in..
734 /* try blit on 3d pipe: */
735 if (fd_blitter_pipe_copy_region(ctx
,
736 dst
, dst_level
, dstx
, dsty
, dstz
,
737 src
, src_level
, src_box
))
740 /* else fallback to pure sw: */
741 util_resource_copy_region(pctx
,
742 dst
, dst_level
, dstx
, dsty
, dstz
,
743 src
, src_level
, src_box
);
747 fd_render_condition_check(struct pipe_context
*pctx
)
749 struct fd_context
*ctx
= fd_context(pctx
);
751 if (!ctx
->cond_query
)
754 union pipe_query_result res
= { 0 };
756 ctx
->cond_mode
!= PIPE_RENDER_COND_NO_WAIT
&&
757 ctx
->cond_mode
!= PIPE_RENDER_COND_BY_REGION_NO_WAIT
;
759 if (pctx
->get_query_result(pctx
, ctx
->cond_query
, wait
, &res
))
760 return (bool)res
.u64
!= ctx
->cond_cond
;
766 * Optimal hardware path for blitting pixels.
767 * Scaling, format conversion, up- and downsampling (resolve) are allowed.
770 fd_blit(struct pipe_context
*pctx
, const struct pipe_blit_info
*blit_info
)
772 struct fd_context
*ctx
= fd_context(pctx
);
773 struct pipe_blit_info info
= *blit_info
;
775 if (info
.src
.resource
->nr_samples
> 1 &&
776 info
.dst
.resource
->nr_samples
<= 1 &&
777 !util_format_is_depth_or_stencil(info
.src
.resource
->format
) &&
778 !util_format_is_pure_integer(info
.src
.resource
->format
)) {
779 DBG("color resolve unimplemented");
783 if (info
.render_condition_enable
&& !fd_render_condition_check(pctx
))
786 if (util_try_blit_via_copy_region(pctx
, &info
)) {
790 if (info
.mask
& PIPE_MASK_S
) {
791 DBG("cannot blit stencil, skipping");
792 info
.mask
&= ~PIPE_MASK_S
;
795 if (!util_blitter_is_blit_supported(ctx
->blitter
, &info
)) {
796 DBG("blit unsupported %s -> %s",
797 util_format_short_name(info
.src
.resource
->format
),
798 util_format_short_name(info
.dst
.resource
->format
));
802 fd_blitter_pipe_begin(ctx
, info
.render_condition_enable
);
803 util_blitter_blit(ctx
->blitter
, &info
);
804 fd_blitter_pipe_end(ctx
);
808 fd_blitter_pipe_begin(struct fd_context
*ctx
, bool render_cond
)
810 util_blitter_save_vertex_buffer_slot(ctx
->blitter
, ctx
->vtx
.vertexbuf
.vb
);
811 util_blitter_save_vertex_elements(ctx
->blitter
, ctx
->vtx
.vtx
);
812 util_blitter_save_vertex_shader(ctx
->blitter
, ctx
->prog
.vp
);
813 util_blitter_save_so_targets(ctx
->blitter
, ctx
->streamout
.num_targets
,
814 ctx
->streamout
.targets
);
815 util_blitter_save_rasterizer(ctx
->blitter
, ctx
->rasterizer
);
816 util_blitter_save_viewport(ctx
->blitter
, &ctx
->viewport
);
817 util_blitter_save_scissor(ctx
->blitter
, &ctx
->scissor
);
818 util_blitter_save_fragment_shader(ctx
->blitter
, ctx
->prog
.fp
);
819 util_blitter_save_blend(ctx
->blitter
, ctx
->blend
);
820 util_blitter_save_depth_stencil_alpha(ctx
->blitter
, ctx
->zsa
);
821 util_blitter_save_stencil_ref(ctx
->blitter
, &ctx
->stencil_ref
);
822 util_blitter_save_sample_mask(ctx
->blitter
, ctx
->sample_mask
);
823 util_blitter_save_framebuffer(ctx
->blitter
, &ctx
->framebuffer
);
824 util_blitter_save_fragment_sampler_states(ctx
->blitter
,
825 ctx
->fragtex
.num_samplers
,
826 (void **)ctx
->fragtex
.samplers
);
827 util_blitter_save_fragment_sampler_views(ctx
->blitter
,
828 ctx
->fragtex
.num_textures
, ctx
->fragtex
.textures
);
830 util_blitter_save_render_condition(ctx
->blitter
,
831 ctx
->cond_query
, ctx
->cond_cond
, ctx
->cond_mode
);
833 fd_hw_query_set_stage(ctx
, ctx
->ring
, FD_STAGE_BLIT
);
837 fd_blitter_pipe_end(struct fd_context
*ctx
)
839 fd_hw_query_set_stage(ctx
, ctx
->ring
, FD_STAGE_NULL
);
843 fd_flush_resource(struct pipe_context
*pctx
, struct pipe_resource
*prsc
)
845 struct fd_resource
*rsc
= fd_resource(prsc
);
847 if (pending(rsc
, FD_PENDING_WRITE
| FD_PENDING_READ
))
848 fd_context_render(pctx
);
852 fd_resource_screen_init(struct pipe_screen
*pscreen
)
854 pscreen
->resource_create
= fd_resource_create
;
855 pscreen
->resource_from_handle
= fd_resource_from_handle
;
856 pscreen
->resource_get_handle
= u_resource_get_handle_vtbl
;
857 pscreen
->resource_destroy
= u_resource_destroy_vtbl
;
861 fd_resource_context_init(struct pipe_context
*pctx
)
863 pctx
->transfer_map
= u_transfer_map_vtbl
;
864 pctx
->transfer_flush_region
= u_transfer_flush_region_vtbl
;
865 pctx
->transfer_unmap
= u_transfer_unmap_vtbl
;
866 pctx
->transfer_inline_write
= u_transfer_inline_write_vtbl
;
867 pctx
->create_surface
= fd_create_surface
;
868 pctx
->surface_destroy
= fd_surface_destroy
;
869 pctx
->resource_copy_region
= fd_resource_copy_region
;
870 pctx
->blit
= fd_blit
;
871 pctx
->flush_resource
= fd_flush_resource
;