freedreno: simplify/cleanup resource status tracking
[mesa.git] / src / gallium / drivers / freedreno / freedreno_resource.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5 *
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:
12 *
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
15 * Software.
16 *
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
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #include "util/u_format.h"
30 #include "util/u_format_zs.h"
31 #include "util/u_inlines.h"
32 #include "util/u_transfer.h"
33 #include "util/u_string.h"
34 #include "util/u_surface.h"
35
36 #include "freedreno_resource.h"
37 #include "freedreno_screen.h"
38 #include "freedreno_surface.h"
39 #include "freedreno_context.h"
40 #include "freedreno_query_hw.h"
41 #include "freedreno_util.h"
42
43 #include <errno.h>
44
45
46 static bool
47 pending(struct fd_resource *rsc, enum fd_resource_status status)
48 {
49 return (rsc->status & status) ||
50 (rsc->stencil && (rsc->stencil->status & status));
51 }
52
53 static void
54 fd_invalidate_resource(struct fd_context *ctx, struct pipe_resource *prsc)
55 {
56 int i;
57
58 /* Go through the entire state and see if the resource is bound
59 * anywhere. If it is, mark the relevant state as dirty. This is called on
60 * realloc_bo.
61 */
62
63 /* Constbufs */
64 for (i = 1; i < PIPE_MAX_CONSTANT_BUFFERS && !(ctx->dirty & FD_DIRTY_CONSTBUF); i++) {
65 if (ctx->constbuf[PIPE_SHADER_VERTEX].cb[i].buffer == prsc)
66 ctx->dirty |= FD_DIRTY_CONSTBUF;
67 if (ctx->constbuf[PIPE_SHADER_FRAGMENT].cb[i].buffer == prsc)
68 ctx->dirty |= FD_DIRTY_CONSTBUF;
69 }
70
71 /* VBOs */
72 for (i = 0; i < ctx->vtx.vertexbuf.count && !(ctx->dirty & FD_DIRTY_VTXBUF); i++) {
73 if (ctx->vtx.vertexbuf.vb[i].buffer == prsc)
74 ctx->dirty |= FD_DIRTY_VTXBUF;
75 }
76
77 /* Index buffer */
78 if (ctx->indexbuf.buffer == prsc)
79 ctx->dirty |= FD_DIRTY_INDEXBUF;
80
81 /* Textures */
82 for (i = 0; i < ctx->verttex.num_textures && !(ctx->dirty & FD_DIRTY_VERTTEX); i++) {
83 if (ctx->verttex.textures[i] && (ctx->verttex.textures[i]->texture == prsc))
84 ctx->dirty |= FD_DIRTY_VERTTEX;
85 }
86 for (i = 0; i < ctx->fragtex.num_textures && !(ctx->dirty & FD_DIRTY_FRAGTEX); i++) {
87 if (ctx->fragtex.textures[i] && (ctx->fragtex.textures[i]->texture == prsc))
88 ctx->dirty |= FD_DIRTY_FRAGTEX;
89 }
90 }
91
92 static void
93 realloc_bo(struct fd_resource *rsc, uint32_t size)
94 {
95 struct fd_screen *screen = fd_screen(rsc->base.b.screen);
96 uint32_t flags = DRM_FREEDRENO_GEM_CACHE_WCOMBINE |
97 DRM_FREEDRENO_GEM_TYPE_KMEM; /* TODO */
98
99 /* if we start using things other than write-combine,
100 * be sure to check for PIPE_RESOURCE_FLAG_MAP_COHERENT
101 */
102
103 if (rsc->bo)
104 fd_bo_del(rsc->bo);
105
106 rsc->bo = fd_bo_new(screen->dev, size, flags);
107 rsc->timestamp = 0;
108 rsc->status = 0;
109 rsc->pending_ctx = NULL;
110 list_delinit(&rsc->list);
111 util_range_set_empty(&rsc->valid_buffer_range);
112 }
113
114 /* Currently this is only used for flushing Z32_S8 texture transfers, but
115 * eventually it should handle everything.
116 */
117 static void
118 fd_resource_flush(struct fd_transfer *trans, const struct pipe_box *box)
119 {
120 struct fd_resource *rsc = fd_resource(trans->base.resource);
121 struct fd_resource_slice *slice = fd_resource_slice(rsc, trans->base.level);
122 struct fd_resource_slice *sslice = fd_resource_slice(rsc->stencil, trans->base.level);
123 enum pipe_format format = trans->base.resource->format;
124
125 float *depth = fd_bo_map(rsc->bo) + slice->offset +
126 (trans->base.box.y + box->y) * slice->pitch * 4 + (trans->base.box.x + box->x) * 4;
127 uint8_t *stencil = fd_bo_map(rsc->stencil->bo) + sslice->offset +
128 (trans->base.box.y + box->y) * sslice->pitch + trans->base.box.x + box->x;
129
130 assert(format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT ||
131 format == PIPE_FORMAT_X32_S8X24_UINT);
132
133 if (format != PIPE_FORMAT_X32_S8X24_UINT)
134 util_format_z32_float_s8x24_uint_unpack_z_float(
135 depth, slice->pitch * 4,
136 trans->staging, trans->base.stride,
137 box->width, box->height);
138
139 util_format_z32_float_s8x24_uint_unpack_s_8uint(
140 stencil, sslice->pitch,
141 trans->staging, trans->base.stride,
142 box->width, box->height);
143 }
144
145 static void fd_resource_transfer_flush_region(struct pipe_context *pctx,
146 struct pipe_transfer *ptrans,
147 const struct pipe_box *box)
148 {
149 struct fd_resource *rsc = fd_resource(ptrans->resource);
150 struct fd_transfer *trans = fd_transfer(ptrans);
151
152 if (ptrans->resource->target == PIPE_BUFFER)
153 util_range_add(&rsc->valid_buffer_range,
154 ptrans->box.x + box->x,
155 ptrans->box.x + box->x + box->width);
156
157 if (trans->staging)
158 fd_resource_flush(trans, box);
159 }
160
161 static void
162 fd_resource_transfer_unmap(struct pipe_context *pctx,
163 struct pipe_transfer *ptrans)
164 {
165 struct fd_context *ctx = fd_context(pctx);
166 struct fd_resource *rsc = fd_resource(ptrans->resource);
167 struct fd_transfer *trans = fd_transfer(ptrans);
168
169 if (trans->staging && !(ptrans->usage & PIPE_TRANSFER_FLUSH_EXPLICIT)) {
170 struct pipe_box box;
171 u_box_2d(0, 0, ptrans->box.width, ptrans->box.height, &box);
172 fd_resource_flush(trans, &box);
173 }
174
175 if (!(ptrans->usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
176 fd_bo_cpu_fini(rsc->bo);
177 if (rsc->stencil)
178 fd_bo_cpu_fini(rsc->stencil->bo);
179 }
180
181 util_range_add(&rsc->valid_buffer_range,
182 ptrans->box.x,
183 ptrans->box.x + ptrans->box.width);
184
185 pipe_resource_reference(&ptrans->resource, NULL);
186 util_slab_free(&ctx->transfer_pool, ptrans);
187
188 if (trans->staging)
189 free(trans->staging);
190 }
191
192 static void *
193 fd_resource_transfer_map(struct pipe_context *pctx,
194 struct pipe_resource *prsc,
195 unsigned level, unsigned usage,
196 const struct pipe_box *box,
197 struct pipe_transfer **pptrans)
198 {
199 struct fd_context *ctx = fd_context(pctx);
200 struct fd_resource *rsc = fd_resource(prsc);
201 struct fd_resource_slice *slice = fd_resource_slice(rsc, level);
202 struct fd_transfer *trans;
203 struct pipe_transfer *ptrans;
204 enum pipe_format format = prsc->format;
205 uint32_t op = 0;
206 uint32_t offset;
207 char *buf;
208 int ret = 0;
209
210 DBG("prsc=%p, level=%u, usage=%x, box=%dx%d+%d,%d", prsc, level, usage,
211 box->width, box->height, box->x, box->y);
212
213 ptrans = util_slab_alloc(&ctx->transfer_pool);
214 if (!ptrans)
215 return NULL;
216
217 /* util_slab_alloc() doesn't zero: */
218 trans = fd_transfer(ptrans);
219 memset(trans, 0, sizeof(*trans));
220
221 pipe_resource_reference(&ptrans->resource, prsc);
222 ptrans->level = level;
223 ptrans->usage = usage;
224 ptrans->box = *box;
225 ptrans->stride = slice->pitch * rsc->cpp;
226 ptrans->layer_stride = slice->size0;
227
228 if (usage & PIPE_TRANSFER_READ)
229 op |= DRM_FREEDRENO_PREP_READ;
230
231 if (usage & PIPE_TRANSFER_WRITE)
232 op |= DRM_FREEDRENO_PREP_WRITE;
233
234 if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
235 realloc_bo(rsc, fd_bo_size(rsc->bo));
236 if (rsc->stencil)
237 realloc_bo(rsc->stencil, fd_bo_size(rsc->stencil->bo));
238 fd_invalidate_resource(ctx, prsc);
239 } else if ((usage & PIPE_TRANSFER_WRITE) &&
240 prsc->target == PIPE_BUFFER &&
241 !util_ranges_intersect(&rsc->valid_buffer_range,
242 box->x, box->x + box->width)) {
243 /* We are trying to write to a previously uninitialized range. No need
244 * to wait.
245 */
246 } else if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
247 /* If the GPU is writing to the resource, or if it is reading from the
248 * resource and we're trying to write to it, flush the renders.
249 */
250 if (((ptrans->usage & PIPE_TRANSFER_WRITE) &&
251 pending(rsc, FD_PENDING_READ | FD_PENDING_WRITE)) ||
252 pending(rsc, FD_PENDING_WRITE))
253 fd_context_render(pctx);
254
255 /* The GPU keeps track of how the various bo's are being used, and
256 * will wait if necessary for the proper operation to have
257 * completed.
258 */
259 ret = fd_bo_cpu_prep(rsc->bo, ctx->screen->pipe, op);
260 if (ret)
261 goto fail;
262 }
263
264 buf = fd_bo_map(rsc->bo);
265 if (!buf) {
266 fd_resource_transfer_unmap(pctx, ptrans);
267 return NULL;
268 }
269
270 if (rsc->layer_first) {
271 offset = slice->offset +
272 box->y / util_format_get_blockheight(format) * ptrans->stride +
273 box->x / util_format_get_blockwidth(format) * rsc->cpp +
274 box->z * rsc->layer_size;
275 } else {
276 offset = slice->offset +
277 box->y / util_format_get_blockheight(format) * ptrans->stride +
278 box->x / util_format_get_blockwidth(format) * rsc->cpp +
279 box->z * slice->size0;
280 }
281
282 if (prsc->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT ||
283 prsc->format == PIPE_FORMAT_X32_S8X24_UINT) {
284 trans->base.stride = trans->base.box.width * rsc->cpp * 2;
285 trans->staging = malloc(trans->base.stride * trans->base.box.height);
286 if (!trans->staging)
287 goto fail;
288
289 /* if we're not discarding the whole range (or resource), we must copy
290 * the real data in.
291 */
292 if (!(usage & (PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE |
293 PIPE_TRANSFER_DISCARD_RANGE))) {
294 struct fd_resource_slice *sslice =
295 fd_resource_slice(rsc->stencil, level);
296 void *sbuf = fd_bo_map(rsc->stencil->bo);
297 if (!sbuf)
298 goto fail;
299
300 float *depth = (float *)(buf + slice->offset +
301 box->y * slice->pitch * 4 + box->x * 4);
302 uint8_t *stencil = sbuf + sslice->offset +
303 box->y * sslice->pitch + box->x;
304
305 if (format != PIPE_FORMAT_X32_S8X24_UINT)
306 util_format_z32_float_s8x24_uint_pack_z_float(
307 trans->staging, trans->base.stride,
308 depth, slice->pitch * 4,
309 box->width, box->height);
310
311 util_format_z32_float_s8x24_uint_pack_s_8uint(
312 trans->staging, trans->base.stride,
313 stencil, sslice->pitch,
314 box->width, box->height);
315 }
316
317 buf = trans->staging;
318 offset = 0;
319 }
320
321 *pptrans = ptrans;
322
323 return buf + offset;
324
325 fail:
326 fd_resource_transfer_unmap(pctx, ptrans);
327 return NULL;
328 }
329
330 static void
331 fd_resource_destroy(struct pipe_screen *pscreen,
332 struct pipe_resource *prsc)
333 {
334 struct fd_resource *rsc = fd_resource(prsc);
335 if (rsc->bo)
336 fd_bo_del(rsc->bo);
337 list_delinit(&rsc->list);
338 util_range_destroy(&rsc->valid_buffer_range);
339 FREE(rsc);
340 }
341
342 static boolean
343 fd_resource_get_handle(struct pipe_screen *pscreen,
344 struct pipe_resource *prsc,
345 struct winsys_handle *handle)
346 {
347 struct fd_resource *rsc = fd_resource(prsc);
348
349 return fd_screen_bo_get_handle(pscreen, rsc->bo,
350 rsc->slices[0].pitch * rsc->cpp, handle);
351 }
352
353
354 static const struct u_resource_vtbl fd_resource_vtbl = {
355 .resource_get_handle = fd_resource_get_handle,
356 .resource_destroy = fd_resource_destroy,
357 .transfer_map = fd_resource_transfer_map,
358 .transfer_flush_region = fd_resource_transfer_flush_region,
359 .transfer_unmap = fd_resource_transfer_unmap,
360 .transfer_inline_write = u_default_transfer_inline_write,
361 };
362
363 static uint32_t
364 setup_slices(struct fd_resource *rsc, uint32_t alignment)
365 {
366 struct pipe_resource *prsc = &rsc->base.b;
367 uint32_t level, size = 0;
368 uint32_t width = prsc->width0;
369 uint32_t height = prsc->height0;
370 uint32_t depth = prsc->depth0;
371 /* in layer_first layout, the level (slice) contains just one
372 * layer (since in fact the layer contains the slices)
373 */
374 uint32_t layers_in_level = rsc->layer_first ? 1 : prsc->array_size;
375
376 for (level = 0; level <= prsc->last_level; level++) {
377 struct fd_resource_slice *slice = fd_resource_slice(rsc, level);
378
379 slice->pitch = width = align(width, 32);
380 slice->offset = size;
381 /* 1d array and 2d array textures must all have the same layer size
382 * for each miplevel on a3xx. 3d textures can have different layer
383 * sizes for high levels, but the hw auto-sizer is buggy (or at least
384 * different than what this code does), so as soon as the layer size
385 * range gets into range, we stop reducing it.
386 */
387 if (prsc->target == PIPE_TEXTURE_3D && (
388 level == 1 ||
389 (level > 1 && rsc->slices[level - 1].size0 > 0xf000)))
390 slice->size0 = align(slice->pitch * height * rsc->cpp, alignment);
391 else if (level == 0 || rsc->layer_first || alignment == 1)
392 slice->size0 = align(slice->pitch * height * rsc->cpp, alignment);
393 else
394 slice->size0 = rsc->slices[level - 1].size0;
395
396 size += slice->size0 * depth * layers_in_level;
397
398 width = u_minify(width, 1);
399 height = u_minify(height, 1);
400 depth = u_minify(depth, 1);
401 }
402
403 return size;
404 }
405
406 static uint32_t
407 slice_alignment(struct pipe_screen *pscreen, const struct pipe_resource *tmpl)
408 {
409 /* on a3xx, 2d array and 3d textures seem to want their
410 * layers aligned to page boundaries:
411 */
412 switch (tmpl->target) {
413 case PIPE_TEXTURE_3D:
414 case PIPE_TEXTURE_1D_ARRAY:
415 case PIPE_TEXTURE_2D_ARRAY:
416 return 4096;
417 default:
418 return 1;
419 }
420 }
421
422 /**
423 * Create a new texture object, using the given template info.
424 */
425 static struct pipe_resource *
426 fd_resource_create(struct pipe_screen *pscreen,
427 const struct pipe_resource *tmpl)
428 {
429 struct fd_resource *rsc = CALLOC_STRUCT(fd_resource);
430 struct pipe_resource *prsc = &rsc->base.b;
431 uint32_t size;
432
433 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
434 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
435 tmpl->target, util_format_name(tmpl->format),
436 tmpl->width0, tmpl->height0, tmpl->depth0,
437 tmpl->array_size, tmpl->last_level, tmpl->nr_samples,
438 tmpl->usage, tmpl->bind, tmpl->flags);
439
440 if (!rsc)
441 return NULL;
442
443 *prsc = *tmpl;
444
445 pipe_reference_init(&prsc->reference, 1);
446 list_inithead(&rsc->list);
447 prsc->screen = pscreen;
448
449 util_range_init(&rsc->valid_buffer_range);
450
451 rsc->base.vtbl = &fd_resource_vtbl;
452 if (tmpl->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT)
453 rsc->cpp = util_format_get_blocksize(PIPE_FORMAT_Z32_FLOAT);
454 else
455 rsc->cpp = util_format_get_blocksize(tmpl->format);
456
457 assert(rsc->cpp);
458
459 if (is_a4xx(fd_screen(pscreen))) {
460 switch (tmpl->target) {
461 case PIPE_TEXTURE_3D:
462 /* TODO 3D_ARRAY? */
463 rsc->layer_first = false;
464 break;
465 default:
466 rsc->layer_first = true;
467 break;
468 }
469 }
470
471 size = setup_slices(rsc, slice_alignment(pscreen, tmpl));
472
473 if (rsc->layer_first) {
474 rsc->layer_size = align(size, 4096);
475 size = rsc->layer_size * prsc->array_size;
476 }
477
478 realloc_bo(rsc, size);
479 if (!rsc->bo)
480 goto fail;
481
482 /* There is no native Z32F_S8 sampling or rendering format, so this must
483 * be emulated via two separate textures. The depth texture still keeps
484 * its Z32F_S8 format though, and we also keep a reference to a separate
485 * S8 texture.
486 */
487 if (tmpl->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT) {
488 struct pipe_resource stencil = *tmpl;
489 stencil.format = PIPE_FORMAT_S8_UINT;
490 rsc->stencil = fd_resource(fd_resource_create(pscreen, &stencil));
491 if (!rsc->stencil)
492 goto fail;
493 }
494
495 return prsc;
496 fail:
497 fd_resource_destroy(pscreen, prsc);
498 return NULL;
499 }
500
501 /**
502 * Create a texture from a winsys_handle. The handle is often created in
503 * another process by first creating a pipe texture and then calling
504 * resource_get_handle.
505 */
506 static struct pipe_resource *
507 fd_resource_from_handle(struct pipe_screen *pscreen,
508 const struct pipe_resource *tmpl,
509 struct winsys_handle *handle)
510 {
511 struct fd_resource *rsc = CALLOC_STRUCT(fd_resource);
512 struct fd_resource_slice *slice = &rsc->slices[0];
513 struct pipe_resource *prsc = &rsc->base.b;
514
515 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
516 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
517 tmpl->target, util_format_name(tmpl->format),
518 tmpl->width0, tmpl->height0, tmpl->depth0,
519 tmpl->array_size, tmpl->last_level, tmpl->nr_samples,
520 tmpl->usage, tmpl->bind, tmpl->flags);
521
522 if (!rsc)
523 return NULL;
524
525 *prsc = *tmpl;
526
527 pipe_reference_init(&prsc->reference, 1);
528 list_inithead(&rsc->list);
529 prsc->screen = pscreen;
530
531 util_range_init(&rsc->valid_buffer_range);
532
533 rsc->bo = fd_screen_bo_from_handle(pscreen, handle, &slice->pitch);
534 if (!rsc->bo)
535 goto fail;
536
537 rsc->base.vtbl = &fd_resource_vtbl;
538 rsc->cpp = util_format_get_blocksize(tmpl->format);
539 slice->pitch /= rsc->cpp;
540
541 assert(rsc->cpp);
542
543 return prsc;
544
545 fail:
546 fd_resource_destroy(pscreen, prsc);
547 return NULL;
548 }
549
550 static void fd_blitter_pipe_begin(struct fd_context *ctx);
551 static void fd_blitter_pipe_end(struct fd_context *ctx);
552
553 /**
554 * _copy_region using pipe (3d engine)
555 */
556 static bool
557 fd_blitter_pipe_copy_region(struct fd_context *ctx,
558 struct pipe_resource *dst,
559 unsigned dst_level,
560 unsigned dstx, unsigned dsty, unsigned dstz,
561 struct pipe_resource *src,
562 unsigned src_level,
563 const struct pipe_box *src_box)
564 {
565 /* not until we allow rendertargets to be buffers */
566 if (dst->target == PIPE_BUFFER || src->target == PIPE_BUFFER)
567 return false;
568
569 if (!util_blitter_is_copy_supported(ctx->blitter, dst, src))
570 return false;
571
572 fd_blitter_pipe_begin(ctx);
573 util_blitter_copy_texture(ctx->blitter,
574 dst, dst_level, dstx, dsty, dstz,
575 src, src_level, src_box);
576 fd_blitter_pipe_end(ctx);
577
578 return true;
579 }
580
581 /**
582 * Copy a block of pixels from one resource to another.
583 * The resource must be of the same format.
584 * Resources with nr_samples > 1 are not allowed.
585 */
586 static void
587 fd_resource_copy_region(struct pipe_context *pctx,
588 struct pipe_resource *dst,
589 unsigned dst_level,
590 unsigned dstx, unsigned dsty, unsigned dstz,
591 struct pipe_resource *src,
592 unsigned src_level,
593 const struct pipe_box *src_box)
594 {
595 struct fd_context *ctx = fd_context(pctx);
596
597 /* TODO if we have 2d core, or other DMA engine that could be used
598 * for simple copies and reasonably easily synchronized with the 3d
599 * core, this is where we'd plug it in..
600 */
601
602 /* try blit on 3d pipe: */
603 if (fd_blitter_pipe_copy_region(ctx,
604 dst, dst_level, dstx, dsty, dstz,
605 src, src_level, src_box))
606 return;
607
608 /* else fallback to pure sw: */
609 util_resource_copy_region(pctx,
610 dst, dst_level, dstx, dsty, dstz,
611 src, src_level, src_box);
612 }
613
614 /**
615 * Optimal hardware path for blitting pixels.
616 * Scaling, format conversion, up- and downsampling (resolve) are allowed.
617 */
618 static void
619 fd_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)
620 {
621 struct fd_context *ctx = fd_context(pctx);
622 struct pipe_blit_info info = *blit_info;
623
624 if (info.src.resource->nr_samples > 1 &&
625 info.dst.resource->nr_samples <= 1 &&
626 !util_format_is_depth_or_stencil(info.src.resource->format) &&
627 !util_format_is_pure_integer(info.src.resource->format)) {
628 DBG("color resolve unimplemented");
629 return;
630 }
631
632 if (util_try_blit_via_copy_region(pctx, &info)) {
633 return; /* done */
634 }
635
636 if (info.mask & PIPE_MASK_S) {
637 DBG("cannot blit stencil, skipping");
638 info.mask &= ~PIPE_MASK_S;
639 }
640
641 if (!util_blitter_is_blit_supported(ctx->blitter, &info)) {
642 DBG("blit unsupported %s -> %s",
643 util_format_short_name(info.src.resource->format),
644 util_format_short_name(info.dst.resource->format));
645 return;
646 }
647
648 fd_blitter_pipe_begin(ctx);
649 util_blitter_blit(ctx->blitter, &info);
650 fd_blitter_pipe_end(ctx);
651 }
652
653 static void
654 fd_blitter_pipe_begin(struct fd_context *ctx)
655 {
656 util_blitter_save_vertex_buffer_slot(ctx->blitter, ctx->vtx.vertexbuf.vb);
657 util_blitter_save_vertex_elements(ctx->blitter, ctx->vtx.vtx);
658 util_blitter_save_vertex_shader(ctx->blitter, ctx->prog.vp);
659 util_blitter_save_so_targets(ctx->blitter, ctx->streamout.num_targets,
660 ctx->streamout.targets);
661 util_blitter_save_rasterizer(ctx->blitter, ctx->rasterizer);
662 util_blitter_save_viewport(ctx->blitter, &ctx->viewport);
663 util_blitter_save_scissor(ctx->blitter, &ctx->scissor);
664 util_blitter_save_fragment_shader(ctx->blitter, ctx->prog.fp);
665 util_blitter_save_blend(ctx->blitter, ctx->blend);
666 util_blitter_save_depth_stencil_alpha(ctx->blitter, ctx->zsa);
667 util_blitter_save_stencil_ref(ctx->blitter, &ctx->stencil_ref);
668 util_blitter_save_sample_mask(ctx->blitter, ctx->sample_mask);
669 util_blitter_save_framebuffer(ctx->blitter, &ctx->framebuffer);
670 util_blitter_save_fragment_sampler_states(ctx->blitter,
671 ctx->fragtex.num_samplers,
672 (void **)ctx->fragtex.samplers);
673 util_blitter_save_fragment_sampler_views(ctx->blitter,
674 ctx->fragtex.num_textures, ctx->fragtex.textures);
675
676 fd_hw_query_set_stage(ctx, ctx->ring, FD_STAGE_BLIT);
677 }
678
679 static void
680 fd_blitter_pipe_end(struct fd_context *ctx)
681 {
682 fd_hw_query_set_stage(ctx, ctx->ring, FD_STAGE_NULL);
683 }
684
685 static void
686 fd_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
687 {
688 struct fd_resource *rsc = fd_resource(prsc);
689
690 if (pending(rsc, FD_PENDING_WRITE | FD_PENDING_READ))
691 fd_context_render(pctx);
692 }
693
694 void
695 fd_resource_screen_init(struct pipe_screen *pscreen)
696 {
697 pscreen->resource_create = fd_resource_create;
698 pscreen->resource_from_handle = fd_resource_from_handle;
699 pscreen->resource_get_handle = u_resource_get_handle_vtbl;
700 pscreen->resource_destroy = u_resource_destroy_vtbl;
701 }
702
703 void
704 fd_resource_context_init(struct pipe_context *pctx)
705 {
706 pctx->transfer_map = u_transfer_map_vtbl;
707 pctx->transfer_flush_region = u_transfer_flush_region_vtbl;
708 pctx->transfer_unmap = u_transfer_unmap_vtbl;
709 pctx->transfer_inline_write = u_transfer_inline_write_vtbl;
710 pctx->create_surface = fd_create_surface;
711 pctx->surface_destroy = fd_surface_destroy;
712 pctx->resource_copy_region = fd_resource_copy_region;
713 pctx->blit = fd_blit;
714 pctx->flush_resource = fd_flush_resource;
715 }