- switch (format) {
- case PIPE_FORMAT_RGTC1_UNORM:
- case PIPE_FORMAT_RGTC1_SNORM:
- case PIPE_FORMAT_LATC1_UNORM:
- case PIPE_FORMAT_LATC1_SNORM:
- util_format_rgtc1_unorm_unpack_rgba_8unorm(
- data, slice->pitch * rsc->cpp,
- source, trans->base.stride,
- box->width, box->height);
- break;
- case PIPE_FORMAT_RGTC2_UNORM:
- case PIPE_FORMAT_RGTC2_SNORM:
- case PIPE_FORMAT_LATC2_UNORM:
- case PIPE_FORMAT_LATC2_SNORM:
- util_format_rgtc2_unorm_unpack_rgba_8unorm(
- data, slice->pitch * rsc->cpp,
- source, trans->base.stride,
- box->width, box->height);
- break;
- default:
- assert(!"Unexpected format\n");
- break;
+ if (!pshadow)
+ return false;
+
+ assert(!ctx->in_shadow);
+ ctx->in_shadow = true;
+
+ /* get rid of any references that batch-cache might have to us (which
+ * should empty/destroy rsc->batches hashset)
+ */
+ fd_bc_invalidate_resource(rsc, false);
+
+ mtx_lock(&ctx->screen->lock);
+
+ /* Swap the backing bo's, so shadow becomes the old buffer,
+ * blit from shadow to new buffer. From here on out, we
+ * cannot fail.
+ *
+ * Note that we need to do it in this order, otherwise if
+ * we go down cpu blit path, the recursive transfer_map()
+ * sees the wrong status..
+ */
+ struct fd_resource *shadow = fd_resource(pshadow);
+
+ DBG("shadow: %p (%d) -> %p (%d)\n", rsc, rsc->base.reference.count,
+ shadow, shadow->base.reference.count);
+
+ /* TODO valid_buffer_range?? */
+ swap(rsc->bo, shadow->bo);
+ swap(rsc->write_batch, shadow->write_batch);
+
+ /* at this point, the newly created shadow buffer is not referenced
+ * by any batches, but the existing rsc (probably) is. We need to
+ * transfer those references over:
+ */
+ debug_assert(shadow->batch_mask == 0);
+ struct fd_batch *batch;
+ foreach_batch(batch, &ctx->screen->batch_cache, rsc->batch_mask) {
+ struct set_entry *entry = _mesa_set_search(batch->resources, rsc);
+ _mesa_set_remove(batch->resources, entry);
+ _mesa_set_add(batch->resources, shadow);
+ }
+ swap(rsc->batch_mask, shadow->batch_mask);
+
+ mtx_unlock(&ctx->screen->lock);
+
+ struct pipe_blit_info blit = {0};
+ blit.dst.resource = prsc;
+ blit.dst.format = prsc->format;
+ blit.src.resource = pshadow;
+ blit.src.format = pshadow->format;
+ blit.mask = util_format_get_mask(prsc->format);
+ blit.filter = PIPE_TEX_FILTER_NEAREST;
+
+#define set_box(field, val) do { \
+ blit.dst.field = (val); \
+ blit.src.field = (val); \
+ } while (0)
+
+ /* blit the other levels in their entirety: */
+ for (unsigned l = 0; l <= prsc->last_level; l++) {
+ if (l == level)
+ continue;
+
+ /* just blit whole level: */
+ set_box(level, l);
+ set_box(box.width, u_minify(prsc->width0, l));
+ set_box(box.height, u_minify(prsc->height0, l));
+ set_box(box.depth, u_minify(prsc->depth0, l));
+
+ do_blit(ctx, &blit, fallback);
+ }
+
+ /* deal w/ current level specially, since we might need to split
+ * it up into a couple blits:
+ */
+ if (!whole_level) {
+ set_box(level, level);
+
+ switch (prsc->target) {
+ case PIPE_BUFFER:
+ case PIPE_TEXTURE_1D:
+ set_box(box.y, 0);
+ set_box(box.z, 0);
+ set_box(box.height, 1);
+ set_box(box.depth, 1);
+
+ if (box->x > 0) {
+ set_box(box.x, 0);
+ set_box(box.width, box->x);
+
+ do_blit(ctx, &blit, fallback);
+ }
+ if ((box->x + box->width) < u_minify(prsc->width0, level)) {
+ set_box(box.x, box->x + box->width);
+ set_box(box.width, u_minify(prsc->width0, level) - (box->x + box->width));
+
+ do_blit(ctx, &blit, fallback);
+ }
+ break;
+ case PIPE_TEXTURE_2D:
+ /* TODO */
+ default:
+ unreachable("TODO");
+ }