nv50,nvc0: take level into account when doing eng2d multi-layer blits
[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 rsc->layer_first = false;
463 break;
464 default:
465 rsc->layer_first = true;
466 break;
467 }
468 }
469
470 size = setup_slices(rsc, slice_alignment(pscreen, tmpl));
471
472 if (rsc->layer_first) {
473 rsc->layer_size = align(size, 4096);
474 size = rsc->layer_size * prsc->array_size;
475 }
476
477 realloc_bo(rsc, size);
478 if (!rsc->bo)
479 goto fail;
480
481 /* There is no native Z32F_S8 sampling or rendering format, so this must
482 * be emulated via two separate textures. The depth texture still keeps
483 * its Z32F_S8 format though, and we also keep a reference to a separate
484 * S8 texture.
485 */
486 if (tmpl->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT) {
487 struct pipe_resource stencil = *tmpl;
488 stencil.format = PIPE_FORMAT_S8_UINT;
489 rsc->stencil = fd_resource(fd_resource_create(pscreen, &stencil));
490 if (!rsc->stencil)
491 goto fail;
492 }
493
494 return prsc;
495 fail:
496 fd_resource_destroy(pscreen, prsc);
497 return NULL;
498 }
499
500 /**
501 * Create a texture from a winsys_handle. The handle is often created in
502 * another process by first creating a pipe texture and then calling
503 * resource_get_handle.
504 */
505 static struct pipe_resource *
506 fd_resource_from_handle(struct pipe_screen *pscreen,
507 const struct pipe_resource *tmpl,
508 struct winsys_handle *handle)
509 {
510 struct fd_resource *rsc = CALLOC_STRUCT(fd_resource);
511 struct fd_resource_slice *slice = &rsc->slices[0];
512 struct pipe_resource *prsc = &rsc->base.b;
513
514 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
515 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
516 tmpl->target, util_format_name(tmpl->format),
517 tmpl->width0, tmpl->height0, tmpl->depth0,
518 tmpl->array_size, tmpl->last_level, tmpl->nr_samples,
519 tmpl->usage, tmpl->bind, tmpl->flags);
520
521 if (!rsc)
522 return NULL;
523
524 *prsc = *tmpl;
525
526 pipe_reference_init(&prsc->reference, 1);
527 list_inithead(&rsc->list);
528 prsc->screen = pscreen;
529
530 util_range_init(&rsc->valid_buffer_range);
531
532 rsc->bo = fd_screen_bo_from_handle(pscreen, handle, &slice->pitch);
533 if (!rsc->bo)
534 goto fail;
535
536 rsc->base.vtbl = &fd_resource_vtbl;
537 rsc->cpp = util_format_get_blocksize(tmpl->format);
538 slice->pitch /= rsc->cpp;
539
540 assert(rsc->cpp);
541
542 return prsc;
543
544 fail:
545 fd_resource_destroy(pscreen, prsc);
546 return NULL;
547 }
548
549 static void fd_blitter_pipe_begin(struct fd_context *ctx);
550 static void fd_blitter_pipe_end(struct fd_context *ctx);
551
552 /**
553 * _copy_region using pipe (3d engine)
554 */
555 static bool
556 fd_blitter_pipe_copy_region(struct fd_context *ctx,
557 struct pipe_resource *dst,
558 unsigned dst_level,
559 unsigned dstx, unsigned dsty, unsigned dstz,
560 struct pipe_resource *src,
561 unsigned src_level,
562 const struct pipe_box *src_box)
563 {
564 /* not until we allow rendertargets to be buffers */
565 if (dst->target == PIPE_BUFFER || src->target == PIPE_BUFFER)
566 return false;
567
568 if (!util_blitter_is_copy_supported(ctx->blitter, dst, src))
569 return false;
570
571 fd_blitter_pipe_begin(ctx);
572 util_blitter_copy_texture(ctx->blitter,
573 dst, dst_level, dstx, dsty, dstz,
574 src, src_level, src_box);
575 fd_blitter_pipe_end(ctx);
576
577 return true;
578 }
579
580 /**
581 * Copy a block of pixels from one resource to another.
582 * The resource must be of the same format.
583 * Resources with nr_samples > 1 are not allowed.
584 */
585 static void
586 fd_resource_copy_region(struct pipe_context *pctx,
587 struct pipe_resource *dst,
588 unsigned dst_level,
589 unsigned dstx, unsigned dsty, unsigned dstz,
590 struct pipe_resource *src,
591 unsigned src_level,
592 const struct pipe_box *src_box)
593 {
594 struct fd_context *ctx = fd_context(pctx);
595
596 /* TODO if we have 2d core, or other DMA engine that could be used
597 * for simple copies and reasonably easily synchronized with the 3d
598 * core, this is where we'd plug it in..
599 */
600
601 /* try blit on 3d pipe: */
602 if (fd_blitter_pipe_copy_region(ctx,
603 dst, dst_level, dstx, dsty, dstz,
604 src, src_level, src_box))
605 return;
606
607 /* else fallback to pure sw: */
608 util_resource_copy_region(pctx,
609 dst, dst_level, dstx, dsty, dstz,
610 src, src_level, src_box);
611 }
612
613 /**
614 * Optimal hardware path for blitting pixels.
615 * Scaling, format conversion, up- and downsampling (resolve) are allowed.
616 */
617 static void
618 fd_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)
619 {
620 struct fd_context *ctx = fd_context(pctx);
621 struct pipe_blit_info info = *blit_info;
622
623 if (info.src.resource->nr_samples > 1 &&
624 info.dst.resource->nr_samples <= 1 &&
625 !util_format_is_depth_or_stencil(info.src.resource->format) &&
626 !util_format_is_pure_integer(info.src.resource->format)) {
627 DBG("color resolve unimplemented");
628 return;
629 }
630
631 if (util_try_blit_via_copy_region(pctx, &info)) {
632 return; /* done */
633 }
634
635 if (info.mask & PIPE_MASK_S) {
636 DBG("cannot blit stencil, skipping");
637 info.mask &= ~PIPE_MASK_S;
638 }
639
640 if (!util_blitter_is_blit_supported(ctx->blitter, &info)) {
641 DBG("blit unsupported %s -> %s",
642 util_format_short_name(info.src.resource->format),
643 util_format_short_name(info.dst.resource->format));
644 return;
645 }
646
647 fd_blitter_pipe_begin(ctx);
648 util_blitter_blit(ctx->blitter, &info);
649 fd_blitter_pipe_end(ctx);
650 }
651
652 static void
653 fd_blitter_pipe_begin(struct fd_context *ctx)
654 {
655 util_blitter_save_vertex_buffer_slot(ctx->blitter, ctx->vtx.vertexbuf.vb);
656 util_blitter_save_vertex_elements(ctx->blitter, ctx->vtx.vtx);
657 util_blitter_save_vertex_shader(ctx->blitter, ctx->prog.vp);
658 util_blitter_save_so_targets(ctx->blitter, ctx->streamout.num_targets,
659 ctx->streamout.targets);
660 util_blitter_save_rasterizer(ctx->blitter, ctx->rasterizer);
661 util_blitter_save_viewport(ctx->blitter, &ctx->viewport);
662 util_blitter_save_scissor(ctx->blitter, &ctx->scissor);
663 util_blitter_save_fragment_shader(ctx->blitter, ctx->prog.fp);
664 util_blitter_save_blend(ctx->blitter, ctx->blend);
665 util_blitter_save_depth_stencil_alpha(ctx->blitter, ctx->zsa);
666 util_blitter_save_stencil_ref(ctx->blitter, &ctx->stencil_ref);
667 util_blitter_save_sample_mask(ctx->blitter, ctx->sample_mask);
668 util_blitter_save_framebuffer(ctx->blitter, &ctx->framebuffer);
669 util_blitter_save_fragment_sampler_states(ctx->blitter,
670 ctx->fragtex.num_samplers,
671 (void **)ctx->fragtex.samplers);
672 util_blitter_save_fragment_sampler_views(ctx->blitter,
673 ctx->fragtex.num_textures, ctx->fragtex.textures);
674
675 fd_hw_query_set_stage(ctx, ctx->ring, FD_STAGE_BLIT);
676 }
677
678 static void
679 fd_blitter_pipe_end(struct fd_context *ctx)
680 {
681 fd_hw_query_set_stage(ctx, ctx->ring, FD_STAGE_NULL);
682 }
683
684 static void
685 fd_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
686 {
687 struct fd_resource *rsc = fd_resource(prsc);
688
689 if (pending(rsc, FD_PENDING_WRITE | FD_PENDING_READ))
690 fd_context_render(pctx);
691 }
692
693 void
694 fd_resource_screen_init(struct pipe_screen *pscreen)
695 {
696 pscreen->resource_create = fd_resource_create;
697 pscreen->resource_from_handle = fd_resource_from_handle;
698 pscreen->resource_get_handle = u_resource_get_handle_vtbl;
699 pscreen->resource_destroy = u_resource_destroy_vtbl;
700 }
701
702 void
703 fd_resource_context_init(struct pipe_context *pctx)
704 {
705 pctx->transfer_map = u_transfer_map_vtbl;
706 pctx->transfer_flush_region = u_transfer_flush_region_vtbl;
707 pctx->transfer_unmap = u_transfer_unmap_vtbl;
708 pctx->transfer_inline_write = u_transfer_inline_write_vtbl;
709 pctx->create_surface = fd_create_surface;
710 pctx->surface_destroy = fd_surface_destroy;
711 pctx->resource_copy_region = fd_resource_copy_region;
712 pctx->blit = fd_blit;
713 pctx->flush_resource = fd_flush_resource;
714 }