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"
46 /* XXX this should go away, needed for 'struct winsys_handle' */
47 #include "state_tracker/drm_driver.h"
50 pending(struct fd_resource
*rsc
, enum fd_resource_status status
)
52 return (rsc
->status
& status
) ||
53 (rsc
->stencil
&& (rsc
->stencil
->status
& status
));
57 fd_invalidate_resource(struct fd_context
*ctx
, struct pipe_resource
*prsc
)
61 /* Go through the entire state and see if the resource is bound
62 * anywhere. If it is, mark the relevant state as dirty. This is called on
67 for (i
= 1; i
< PIPE_MAX_CONSTANT_BUFFERS
&& !(ctx
->dirty
& FD_DIRTY_CONSTBUF
); i
++) {
68 if (ctx
->constbuf
[PIPE_SHADER_VERTEX
].cb
[i
].buffer
== prsc
)
69 ctx
->dirty
|= FD_DIRTY_CONSTBUF
;
70 if (ctx
->constbuf
[PIPE_SHADER_FRAGMENT
].cb
[i
].buffer
== prsc
)
71 ctx
->dirty
|= FD_DIRTY_CONSTBUF
;
75 for (i
= 0; i
< ctx
->vtx
.vertexbuf
.count
&& !(ctx
->dirty
& FD_DIRTY_VTXBUF
); i
++) {
76 if (ctx
->vtx
.vertexbuf
.vb
[i
].buffer
== prsc
)
77 ctx
->dirty
|= FD_DIRTY_VTXBUF
;
81 if (ctx
->indexbuf
.buffer
== prsc
)
82 ctx
->dirty
|= FD_DIRTY_INDEXBUF
;
85 for (i
= 0; i
< ctx
->verttex
.num_textures
&& !(ctx
->dirty
& FD_DIRTY_VERTTEX
); i
++) {
86 if (ctx
->verttex
.textures
[i
] && (ctx
->verttex
.textures
[i
]->texture
== prsc
))
87 ctx
->dirty
|= FD_DIRTY_VERTTEX
;
89 for (i
= 0; i
< ctx
->fragtex
.num_textures
&& !(ctx
->dirty
& FD_DIRTY_FRAGTEX
); i
++) {
90 if (ctx
->fragtex
.textures
[i
] && (ctx
->fragtex
.textures
[i
]->texture
== prsc
))
91 ctx
->dirty
|= FD_DIRTY_FRAGTEX
;
96 realloc_bo(struct fd_resource
*rsc
, uint32_t size
)
98 struct fd_screen
*screen
= fd_screen(rsc
->base
.b
.screen
);
99 uint32_t flags
= DRM_FREEDRENO_GEM_CACHE_WCOMBINE
|
100 DRM_FREEDRENO_GEM_TYPE_KMEM
; /* TODO */
102 /* if we start using things other than write-combine,
103 * be sure to check for PIPE_RESOURCE_FLAG_MAP_COHERENT
109 rsc
->bo
= fd_bo_new(screen
->dev
, size
, flags
);
112 rsc
->pending_ctx
= NULL
;
113 list_delinit(&rsc
->list
);
114 util_range_set_empty(&rsc
->valid_buffer_range
);
118 fd_resource_layer_offset(struct fd_resource
*rsc
,
119 struct fd_resource_slice
*slice
,
122 if (rsc
->layer_first
)
123 return layer
* rsc
->layer_size
;
125 return layer
* slice
->size0
;
129 fd_resource_flush_z32s8(struct fd_transfer
*trans
, const struct pipe_box
*box
)
131 struct fd_resource
*rsc
= fd_resource(trans
->base
.resource
);
132 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, trans
->base
.level
);
133 struct fd_resource_slice
*sslice
= fd_resource_slice(rsc
->stencil
, trans
->base
.level
);
134 enum pipe_format format
= trans
->base
.resource
->format
;
136 float *depth
= fd_bo_map(rsc
->bo
) + slice
->offset
+
137 fd_resource_layer_offset(rsc
, slice
, trans
->base
.box
.z
) +
138 (trans
->base
.box
.y
+ box
->y
) * slice
->pitch
* 4 + (trans
->base
.box
.x
+ box
->x
) * 4;
139 uint8_t *stencil
= fd_bo_map(rsc
->stencil
->bo
) + sslice
->offset
+
140 fd_resource_layer_offset(rsc
->stencil
, sslice
, trans
->base
.box
.z
) +
141 (trans
->base
.box
.y
+ box
->y
) * sslice
->pitch
+ trans
->base
.box
.x
+ box
->x
;
143 if (format
!= PIPE_FORMAT_X32_S8X24_UINT
)
144 util_format_z32_float_s8x24_uint_unpack_z_float(
145 depth
, slice
->pitch
* 4,
146 trans
->staging
, trans
->base
.stride
,
147 box
->width
, box
->height
);
149 util_format_z32_float_s8x24_uint_unpack_s_8uint(
150 stencil
, sslice
->pitch
,
151 trans
->staging
, trans
->base
.stride
,
152 box
->width
, box
->height
);
156 fd_resource_flush_rgtc(struct fd_transfer
*trans
, const struct pipe_box
*box
)
158 struct fd_resource
*rsc
= fd_resource(trans
->base
.resource
);
159 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, trans
->base
.level
);
160 enum pipe_format format
= trans
->base
.resource
->format
;
162 uint8_t *data
= fd_bo_map(rsc
->bo
) + slice
->offset
+
163 fd_resource_layer_offset(rsc
, slice
, trans
->base
.box
.z
) +
164 ((trans
->base
.box
.y
+ box
->y
) * slice
->pitch
+
165 trans
->base
.box
.x
+ box
->x
) * rsc
->cpp
;
167 uint8_t *source
= trans
->staging
+
168 util_format_get_nblocksy(format
, box
->y
) * trans
->base
.stride
+
169 util_format_get_stride(format
, box
->x
);
172 case PIPE_FORMAT_RGTC1_UNORM
:
173 case PIPE_FORMAT_RGTC1_SNORM
:
174 case PIPE_FORMAT_LATC1_UNORM
:
175 case PIPE_FORMAT_LATC1_SNORM
:
176 util_format_rgtc1_unorm_unpack_rgba_8unorm(
177 data
, slice
->pitch
* rsc
->cpp
,
178 source
, trans
->base
.stride
,
179 box
->width
, box
->height
);
181 case PIPE_FORMAT_RGTC2_UNORM
:
182 case PIPE_FORMAT_RGTC2_SNORM
:
183 case PIPE_FORMAT_LATC2_UNORM
:
184 case PIPE_FORMAT_LATC2_SNORM
:
185 util_format_rgtc2_unorm_unpack_rgba_8unorm(
186 data
, slice
->pitch
* rsc
->cpp
,
187 source
, trans
->base
.stride
,
188 box
->width
, box
->height
);
191 assert(!"Unexpected format\n");
197 fd_resource_flush(struct fd_transfer
*trans
, const struct pipe_box
*box
)
199 enum pipe_format format
= trans
->base
.resource
->format
;
202 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
203 case PIPE_FORMAT_X32_S8X24_UINT
:
204 fd_resource_flush_z32s8(trans
, box
);
206 case PIPE_FORMAT_RGTC1_UNORM
:
207 case PIPE_FORMAT_RGTC1_SNORM
:
208 case PIPE_FORMAT_RGTC2_UNORM
:
209 case PIPE_FORMAT_RGTC2_SNORM
:
210 case PIPE_FORMAT_LATC1_UNORM
:
211 case PIPE_FORMAT_LATC1_SNORM
:
212 case PIPE_FORMAT_LATC2_UNORM
:
213 case PIPE_FORMAT_LATC2_SNORM
:
214 fd_resource_flush_rgtc(trans
, box
);
217 assert(!"Unexpected staging transfer type");
222 static void fd_resource_transfer_flush_region(struct pipe_context
*pctx
,
223 struct pipe_transfer
*ptrans
,
224 const struct pipe_box
*box
)
226 struct fd_resource
*rsc
= fd_resource(ptrans
->resource
);
227 struct fd_transfer
*trans
= fd_transfer(ptrans
);
229 if (ptrans
->resource
->target
== PIPE_BUFFER
)
230 util_range_add(&rsc
->valid_buffer_range
,
231 ptrans
->box
.x
+ box
->x
,
232 ptrans
->box
.x
+ box
->x
+ box
->width
);
235 fd_resource_flush(trans
, box
);
239 fd_resource_transfer_unmap(struct pipe_context
*pctx
,
240 struct pipe_transfer
*ptrans
)
242 struct fd_context
*ctx
= fd_context(pctx
);
243 struct fd_resource
*rsc
= fd_resource(ptrans
->resource
);
244 struct fd_transfer
*trans
= fd_transfer(ptrans
);
246 if (trans
->staging
&& !(ptrans
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
)) {
248 u_box_2d(0, 0, ptrans
->box
.width
, ptrans
->box
.height
, &box
);
249 fd_resource_flush(trans
, &box
);
252 if (!(ptrans
->usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
253 fd_bo_cpu_fini(rsc
->bo
);
255 fd_bo_cpu_fini(rsc
->stencil
->bo
);
258 util_range_add(&rsc
->valid_buffer_range
,
260 ptrans
->box
.x
+ ptrans
->box
.width
);
262 pipe_resource_reference(&ptrans
->resource
, NULL
);
263 util_slab_free(&ctx
->transfer_pool
, ptrans
);
266 free(trans
->staging
);
270 fd_resource_transfer_map(struct pipe_context
*pctx
,
271 struct pipe_resource
*prsc
,
272 unsigned level
, unsigned usage
,
273 const struct pipe_box
*box
,
274 struct pipe_transfer
**pptrans
)
276 struct fd_context
*ctx
= fd_context(pctx
);
277 struct fd_resource
*rsc
= fd_resource(prsc
);
278 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, level
);
279 struct fd_transfer
*trans
;
280 struct pipe_transfer
*ptrans
;
281 enum pipe_format format
= prsc
->format
;
287 DBG("prsc=%p, level=%u, usage=%x, box=%dx%d+%d,%d", prsc
, level
, usage
,
288 box
->width
, box
->height
, box
->x
, box
->y
);
290 ptrans
= util_slab_alloc(&ctx
->transfer_pool
);
294 /* util_slab_alloc() doesn't zero: */
295 trans
= fd_transfer(ptrans
);
296 memset(trans
, 0, sizeof(*trans
));
298 pipe_resource_reference(&ptrans
->resource
, prsc
);
299 ptrans
->level
= level
;
300 ptrans
->usage
= usage
;
302 ptrans
->stride
= util_format_get_nblocksx(format
, slice
->pitch
) * rsc
->cpp
;
303 ptrans
->layer_stride
= slice
->size0
;
305 if (usage
& PIPE_TRANSFER_READ
)
306 op
|= DRM_FREEDRENO_PREP_READ
;
308 if (usage
& PIPE_TRANSFER_WRITE
)
309 op
|= DRM_FREEDRENO_PREP_WRITE
;
311 if (usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) {
312 realloc_bo(rsc
, fd_bo_size(rsc
->bo
));
314 realloc_bo(rsc
->stencil
, fd_bo_size(rsc
->stencil
->bo
));
315 fd_invalidate_resource(ctx
, prsc
);
316 } else if ((usage
& PIPE_TRANSFER_WRITE
) &&
317 prsc
->target
== PIPE_BUFFER
&&
318 !util_ranges_intersect(&rsc
->valid_buffer_range
,
319 box
->x
, box
->x
+ box
->width
)) {
320 /* We are trying to write to a previously uninitialized range. No need
323 } else if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
324 /* If the GPU is writing to the resource, or if it is reading from the
325 * resource and we're trying to write to it, flush the renders.
327 if (((ptrans
->usage
& PIPE_TRANSFER_WRITE
) &&
328 pending(rsc
, FD_PENDING_READ
| FD_PENDING_WRITE
)) ||
329 pending(rsc
, FD_PENDING_WRITE
))
330 fd_context_render(pctx
);
332 /* The GPU keeps track of how the various bo's are being used, and
333 * will wait if necessary for the proper operation to have
336 ret
= fd_bo_cpu_prep(rsc
->bo
, ctx
->screen
->pipe
, op
);
341 buf
= fd_bo_map(rsc
->bo
);
345 offset
= slice
->offset
+
346 box
->y
/ util_format_get_blockheight(format
) * ptrans
->stride
+
347 box
->x
/ util_format_get_blockwidth(format
) * rsc
->cpp
+
348 fd_resource_layer_offset(rsc
, slice
, box
->z
);
350 if (prsc
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
||
351 prsc
->format
== PIPE_FORMAT_X32_S8X24_UINT
) {
352 assert(trans
->base
.box
.depth
== 1);
354 trans
->base
.stride
= trans
->base
.box
.width
* rsc
->cpp
* 2;
355 trans
->staging
= malloc(trans
->base
.stride
* trans
->base
.box
.height
);
359 /* if we're not discarding the whole range (or resource), we must copy
362 if (!(usage
& (PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
|
363 PIPE_TRANSFER_DISCARD_RANGE
))) {
364 struct fd_resource_slice
*sslice
=
365 fd_resource_slice(rsc
->stencil
, level
);
366 void *sbuf
= fd_bo_map(rsc
->stencil
->bo
);
370 float *depth
= (float *)(buf
+ slice
->offset
+
371 fd_resource_layer_offset(rsc
, slice
, box
->z
) +
372 box
->y
* slice
->pitch
* 4 + box
->x
* 4);
373 uint8_t *stencil
= sbuf
+ sslice
->offset
+
374 fd_resource_layer_offset(rsc
->stencil
, sslice
, box
->z
) +
375 box
->y
* sslice
->pitch
+ box
->x
;
377 if (format
!= PIPE_FORMAT_X32_S8X24_UINT
)
378 util_format_z32_float_s8x24_uint_pack_z_float(
379 trans
->staging
, trans
->base
.stride
,
380 depth
, slice
->pitch
* 4,
381 box
->width
, box
->height
);
383 util_format_z32_float_s8x24_uint_pack_s_8uint(
384 trans
->staging
, trans
->base
.stride
,
385 stencil
, sslice
->pitch
,
386 box
->width
, box
->height
);
389 buf
= trans
->staging
;
391 } else if (rsc
->internal_format
!= format
&&
392 util_format_description(format
)->layout
== UTIL_FORMAT_LAYOUT_RGTC
) {
393 assert(trans
->base
.box
.depth
== 1);
395 trans
->base
.stride
= util_format_get_stride(
396 format
, trans
->base
.box
.width
);
397 trans
->staging
= malloc(
398 util_format_get_2d_size(format
, trans
->base
.stride
,
399 trans
->base
.box
.height
));
403 /* if we're not discarding the whole range (or resource), we must copy
406 if (!(usage
& (PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
|
407 PIPE_TRANSFER_DISCARD_RANGE
))) {
408 uint8_t *rgba8
= (uint8_t *)buf
+ slice
->offset
+
409 fd_resource_layer_offset(rsc
, slice
, box
->z
) +
410 box
->y
* slice
->pitch
* rsc
->cpp
+ box
->x
* rsc
->cpp
;
413 case PIPE_FORMAT_RGTC1_UNORM
:
414 case PIPE_FORMAT_RGTC1_SNORM
:
415 case PIPE_FORMAT_LATC1_UNORM
:
416 case PIPE_FORMAT_LATC1_SNORM
:
417 util_format_rgtc1_unorm_pack_rgba_8unorm(
418 trans
->staging
, trans
->base
.stride
,
419 rgba8
, slice
->pitch
* rsc
->cpp
,
420 box
->width
, box
->height
);
422 case PIPE_FORMAT_RGTC2_UNORM
:
423 case PIPE_FORMAT_RGTC2_SNORM
:
424 case PIPE_FORMAT_LATC2_UNORM
:
425 case PIPE_FORMAT_LATC2_SNORM
:
426 util_format_rgtc2_unorm_pack_rgba_8unorm(
427 trans
->staging
, trans
->base
.stride
,
428 rgba8
, slice
->pitch
* rsc
->cpp
,
429 box
->width
, box
->height
);
432 assert(!"Unexpected format");
437 buf
= trans
->staging
;
446 fd_resource_transfer_unmap(pctx
, ptrans
);
451 fd_resource_destroy(struct pipe_screen
*pscreen
,
452 struct pipe_resource
*prsc
)
454 struct fd_resource
*rsc
= fd_resource(prsc
);
457 list_delinit(&rsc
->list
);
458 util_range_destroy(&rsc
->valid_buffer_range
);
463 fd_resource_get_handle(struct pipe_screen
*pscreen
,
464 struct pipe_resource
*prsc
,
465 struct winsys_handle
*handle
)
467 struct fd_resource
*rsc
= fd_resource(prsc
);
469 return fd_screen_bo_get_handle(pscreen
, rsc
->bo
,
470 rsc
->slices
[0].pitch
* rsc
->cpp
, handle
);
474 static const struct u_resource_vtbl fd_resource_vtbl
= {
475 .resource_get_handle
= fd_resource_get_handle
,
476 .resource_destroy
= fd_resource_destroy
,
477 .transfer_map
= fd_resource_transfer_map
,
478 .transfer_flush_region
= fd_resource_transfer_flush_region
,
479 .transfer_unmap
= fd_resource_transfer_unmap
,
480 .transfer_inline_write
= u_default_transfer_inline_write
,
484 setup_slices(struct fd_resource
*rsc
, uint32_t alignment
, enum pipe_format format
)
486 struct pipe_resource
*prsc
= &rsc
->base
.b
;
487 enum util_format_layout layout
= util_format_description(format
)->layout
;
488 uint32_t level
, size
= 0;
489 uint32_t width
= prsc
->width0
;
490 uint32_t height
= prsc
->height0
;
491 uint32_t depth
= prsc
->depth0
;
492 /* in layer_first layout, the level (slice) contains just one
493 * layer (since in fact the layer contains the slices)
495 uint32_t layers_in_level
= rsc
->layer_first
? 1 : prsc
->array_size
;
497 for (level
= 0; level
<= prsc
->last_level
; level
++) {
498 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, level
);
501 if (layout
== UTIL_FORMAT_LAYOUT_ASTC
)
502 slice
->pitch
= width
=
503 util_align_npot(width
, 32 * util_format_get_blockwidth(format
));
505 slice
->pitch
= width
= align(width
, 32);
506 slice
->offset
= size
;
507 blocks
= util_format_get_nblocks(format
, width
, height
);
508 /* 1d array and 2d array textures must all have the same layer size
509 * for each miplevel on a3xx. 3d textures can have different layer
510 * sizes for high levels, but the hw auto-sizer is buggy (or at least
511 * different than what this code does), so as soon as the layer size
512 * range gets into range, we stop reducing it.
514 if (prsc
->target
== PIPE_TEXTURE_3D
&& (
516 (level
> 1 && rsc
->slices
[level
- 1].size0
> 0xf000)))
517 slice
->size0
= align(blocks
* rsc
->cpp
, alignment
);
518 else if (level
== 0 || rsc
->layer_first
|| alignment
== 1)
519 slice
->size0
= align(blocks
* rsc
->cpp
, alignment
);
521 slice
->size0
= rsc
->slices
[level
- 1].size0
;
523 size
+= slice
->size0
* depth
* layers_in_level
;
525 width
= u_minify(width
, 1);
526 height
= u_minify(height
, 1);
527 depth
= u_minify(depth
, 1);
534 slice_alignment(struct pipe_screen
*pscreen
, const struct pipe_resource
*tmpl
)
536 /* on a3xx, 2d array and 3d textures seem to want their
537 * layers aligned to page boundaries:
539 switch (tmpl
->target
) {
540 case PIPE_TEXTURE_3D
:
541 case PIPE_TEXTURE_1D_ARRAY
:
542 case PIPE_TEXTURE_2D_ARRAY
:
550 * Create a new texture object, using the given template info.
552 static struct pipe_resource
*
553 fd_resource_create(struct pipe_screen
*pscreen
,
554 const struct pipe_resource
*tmpl
)
556 struct fd_resource
*rsc
= CALLOC_STRUCT(fd_resource
);
557 struct pipe_resource
*prsc
= &rsc
->base
.b
;
558 enum pipe_format format
= tmpl
->format
;
559 uint32_t size
, alignment
;
561 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
562 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
563 tmpl
->target
, util_format_name(format
),
564 tmpl
->width0
, tmpl
->height0
, tmpl
->depth0
,
565 tmpl
->array_size
, tmpl
->last_level
, tmpl
->nr_samples
,
566 tmpl
->usage
, tmpl
->bind
, tmpl
->flags
);
573 pipe_reference_init(&prsc
->reference
, 1);
574 list_inithead(&rsc
->list
);
575 prsc
->screen
= pscreen
;
577 util_range_init(&rsc
->valid_buffer_range
);
579 rsc
->base
.vtbl
= &fd_resource_vtbl
;
581 if (format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
)
582 format
= PIPE_FORMAT_Z32_FLOAT
;
583 else if (fd_screen(pscreen
)->gpu_id
< 400 &&
584 util_format_description(format
)->layout
== UTIL_FORMAT_LAYOUT_RGTC
)
585 format
= PIPE_FORMAT_R8G8B8A8_UNORM
;
586 rsc
->internal_format
= format
;
587 rsc
->cpp
= util_format_get_blocksize(format
);
591 alignment
= slice_alignment(pscreen
, tmpl
);
592 if (is_a4xx(fd_screen(pscreen
))) {
593 switch (tmpl
->target
) {
594 case PIPE_TEXTURE_3D
:
595 rsc
->layer_first
= false;
598 rsc
->layer_first
= true;
604 size
= setup_slices(rsc
, alignment
, format
);
606 if (rsc
->layer_first
) {
607 rsc
->layer_size
= align(size
, 4096);
608 size
= rsc
->layer_size
* prsc
->array_size
;
611 realloc_bo(rsc
, size
);
615 /* There is no native Z32F_S8 sampling or rendering format, so this must
616 * be emulated via two separate textures. The depth texture still keeps
617 * its Z32F_S8 format though, and we also keep a reference to a separate
620 if (tmpl
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
) {
621 struct pipe_resource stencil
= *tmpl
;
622 stencil
.format
= PIPE_FORMAT_S8_UINT
;
623 rsc
->stencil
= fd_resource(fd_resource_create(pscreen
, &stencil
));
630 fd_resource_destroy(pscreen
, prsc
);
635 * Create a texture from a winsys_handle. The handle is often created in
636 * another process by first creating a pipe texture and then calling
637 * resource_get_handle.
639 static struct pipe_resource
*
640 fd_resource_from_handle(struct pipe_screen
*pscreen
,
641 const struct pipe_resource
*tmpl
,
642 struct winsys_handle
*handle
, unsigned usage
)
644 struct fd_resource
*rsc
= CALLOC_STRUCT(fd_resource
);
645 struct fd_resource_slice
*slice
= &rsc
->slices
[0];
646 struct pipe_resource
*prsc
= &rsc
->base
.b
;
648 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
649 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
650 tmpl
->target
, util_format_name(tmpl
->format
),
651 tmpl
->width0
, tmpl
->height0
, tmpl
->depth0
,
652 tmpl
->array_size
, tmpl
->last_level
, tmpl
->nr_samples
,
653 tmpl
->usage
, tmpl
->bind
, tmpl
->flags
);
660 pipe_reference_init(&prsc
->reference
, 1);
661 list_inithead(&rsc
->list
);
662 prsc
->screen
= pscreen
;
664 util_range_init(&rsc
->valid_buffer_range
);
666 rsc
->bo
= fd_screen_bo_from_handle(pscreen
, handle
, &slice
->pitch
);
670 rsc
->base
.vtbl
= &fd_resource_vtbl
;
671 rsc
->cpp
= util_format_get_blocksize(tmpl
->format
);
672 slice
->pitch
/= rsc
->cpp
;
673 slice
->offset
= handle
->offset
;
680 fd_resource_destroy(pscreen
, prsc
);
684 static void fd_blitter_pipe_begin(struct fd_context
*ctx
, bool render_cond
);
685 static void fd_blitter_pipe_end(struct fd_context
*ctx
);
688 * _copy_region using pipe (3d engine)
691 fd_blitter_pipe_copy_region(struct fd_context
*ctx
,
692 struct pipe_resource
*dst
,
694 unsigned dstx
, unsigned dsty
, unsigned dstz
,
695 struct pipe_resource
*src
,
697 const struct pipe_box
*src_box
)
699 /* not until we allow rendertargets to be buffers */
700 if (dst
->target
== PIPE_BUFFER
|| src
->target
== PIPE_BUFFER
)
703 if (!util_blitter_is_copy_supported(ctx
->blitter
, dst
, src
))
706 fd_blitter_pipe_begin(ctx
, false);
707 util_blitter_copy_texture(ctx
->blitter
,
708 dst
, dst_level
, dstx
, dsty
, dstz
,
709 src
, src_level
, src_box
);
710 fd_blitter_pipe_end(ctx
);
716 * Copy a block of pixels from one resource to another.
717 * The resource must be of the same format.
718 * Resources with nr_samples > 1 are not allowed.
721 fd_resource_copy_region(struct pipe_context
*pctx
,
722 struct pipe_resource
*dst
,
724 unsigned dstx
, unsigned dsty
, unsigned dstz
,
725 struct pipe_resource
*src
,
727 const struct pipe_box
*src_box
)
729 struct fd_context
*ctx
= fd_context(pctx
);
731 /* TODO if we have 2d core, or other DMA engine that could be used
732 * for simple copies and reasonably easily synchronized with the 3d
733 * core, this is where we'd plug it in..
736 /* try blit on 3d pipe: */
737 if (fd_blitter_pipe_copy_region(ctx
,
738 dst
, dst_level
, dstx
, dsty
, dstz
,
739 src
, src_level
, src_box
))
742 /* else fallback to pure sw: */
743 util_resource_copy_region(pctx
,
744 dst
, dst_level
, dstx
, dsty
, dstz
,
745 src
, src_level
, src_box
);
749 fd_render_condition_check(struct pipe_context
*pctx
)
751 struct fd_context
*ctx
= fd_context(pctx
);
753 if (!ctx
->cond_query
)
756 union pipe_query_result res
= { 0 };
758 ctx
->cond_mode
!= PIPE_RENDER_COND_NO_WAIT
&&
759 ctx
->cond_mode
!= PIPE_RENDER_COND_BY_REGION_NO_WAIT
;
761 if (pctx
->get_query_result(pctx
, ctx
->cond_query
, wait
, &res
))
762 return (bool)res
.u64
!= ctx
->cond_cond
;
768 * Optimal hardware path for blitting pixels.
769 * Scaling, format conversion, up- and downsampling (resolve) are allowed.
772 fd_blit(struct pipe_context
*pctx
, const struct pipe_blit_info
*blit_info
)
774 struct fd_context
*ctx
= fd_context(pctx
);
775 struct pipe_blit_info info
= *blit_info
;
777 if (info
.src
.resource
->nr_samples
> 1 &&
778 info
.dst
.resource
->nr_samples
<= 1 &&
779 !util_format_is_depth_or_stencil(info
.src
.resource
->format
) &&
780 !util_format_is_pure_integer(info
.src
.resource
->format
)) {
781 DBG("color resolve unimplemented");
785 if (info
.render_condition_enable
&& !fd_render_condition_check(pctx
))
788 if (util_try_blit_via_copy_region(pctx
, &info
)) {
792 if (info
.mask
& PIPE_MASK_S
) {
793 DBG("cannot blit stencil, skipping");
794 info
.mask
&= ~PIPE_MASK_S
;
797 if (!util_blitter_is_blit_supported(ctx
->blitter
, &info
)) {
798 DBG("blit unsupported %s -> %s",
799 util_format_short_name(info
.src
.resource
->format
),
800 util_format_short_name(info
.dst
.resource
->format
));
804 fd_blitter_pipe_begin(ctx
, info
.render_condition_enable
);
805 util_blitter_blit(ctx
->blitter
, &info
);
806 fd_blitter_pipe_end(ctx
);
810 fd_blitter_pipe_begin(struct fd_context
*ctx
, bool render_cond
)
812 util_blitter_save_vertex_buffer_slot(ctx
->blitter
, ctx
->vtx
.vertexbuf
.vb
);
813 util_blitter_save_vertex_elements(ctx
->blitter
, ctx
->vtx
.vtx
);
814 util_blitter_save_vertex_shader(ctx
->blitter
, ctx
->prog
.vp
);
815 util_blitter_save_so_targets(ctx
->blitter
, ctx
->streamout
.num_targets
,
816 ctx
->streamout
.targets
);
817 util_blitter_save_rasterizer(ctx
->blitter
, ctx
->rasterizer
);
818 util_blitter_save_viewport(ctx
->blitter
, &ctx
->viewport
);
819 util_blitter_save_scissor(ctx
->blitter
, &ctx
->scissor
);
820 util_blitter_save_fragment_shader(ctx
->blitter
, ctx
->prog
.fp
);
821 util_blitter_save_blend(ctx
->blitter
, ctx
->blend
);
822 util_blitter_save_depth_stencil_alpha(ctx
->blitter
, ctx
->zsa
);
823 util_blitter_save_stencil_ref(ctx
->blitter
, &ctx
->stencil_ref
);
824 util_blitter_save_sample_mask(ctx
->blitter
, ctx
->sample_mask
);
825 util_blitter_save_framebuffer(ctx
->blitter
, &ctx
->framebuffer
);
826 util_blitter_save_fragment_sampler_states(ctx
->blitter
,
827 ctx
->fragtex
.num_samplers
,
828 (void **)ctx
->fragtex
.samplers
);
829 util_blitter_save_fragment_sampler_views(ctx
->blitter
,
830 ctx
->fragtex
.num_textures
, ctx
->fragtex
.textures
);
832 util_blitter_save_render_condition(ctx
->blitter
,
833 ctx
->cond_query
, ctx
->cond_cond
, ctx
->cond_mode
);
835 fd_hw_query_set_stage(ctx
, ctx
->ring
, FD_STAGE_BLIT
);
839 fd_blitter_pipe_end(struct fd_context
*ctx
)
841 fd_hw_query_set_stage(ctx
, ctx
->ring
, FD_STAGE_NULL
);
845 fd_flush_resource(struct pipe_context
*pctx
, struct pipe_resource
*prsc
)
847 struct fd_resource
*rsc
= fd_resource(prsc
);
849 if (pending(rsc
, FD_PENDING_WRITE
| FD_PENDING_READ
))
850 fd_context_render(pctx
);
854 fd_resource_screen_init(struct pipe_screen
*pscreen
)
856 pscreen
->resource_create
= fd_resource_create
;
857 pscreen
->resource_from_handle
= fd_resource_from_handle
;
858 pscreen
->resource_get_handle
= u_resource_get_handle_vtbl
;
859 pscreen
->resource_destroy
= u_resource_destroy_vtbl
;
863 fd_resource_context_init(struct pipe_context
*pctx
)
865 pctx
->transfer_map
= u_transfer_map_vtbl
;
866 pctx
->transfer_flush_region
= u_transfer_flush_region_vtbl
;
867 pctx
->transfer_unmap
= u_transfer_unmap_vtbl
;
868 pctx
->transfer_inline_write
= u_transfer_inline_write_vtbl
;
869 pctx
->create_surface
= fd_create_surface
;
870 pctx
->surface_destroy
= fd_surface_destroy
;
871 pctx
->resource_copy_region
= fd_resource_copy_region
;
872 pctx
->blit
= fd_blit
;
873 pctx
->flush_resource
= fd_flush_resource
;