Path: blob/21.2-virgl/src/gallium/drivers/virgl/virgl_texture.c
4570 views
/*1* Copyright 2014, 2015 Red Hat.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* on the rights to use, copy, modify, merge, publish, distribute, sub7* license, and/or sell copies of the Software, and to permit persons to whom8* the Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL17* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,18* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR19* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE20* USE OR OTHER DEALINGS IN THE SOFTWARE.21*/22#include "util/format/u_format.h"23#include "util/u_inlines.h"24#include "util/u_memory.h"2526#include "virgl_context.h"27#include "virgl_encode.h"28#include "virgl_resource.h"29#include "virgl_screen.h"3031static void virgl_copy_region_with_blit(struct pipe_context *pipe,32struct pipe_resource *dst,33unsigned dst_level,34const struct pipe_box *dst_box,35struct pipe_resource *src,36unsigned src_level,37const struct pipe_box *src_box)38{39struct pipe_blit_info blit;4041memset(&blit, 0, sizeof(blit));42blit.src.resource = src;43blit.src.format = src->format;44blit.src.level = src_level;45blit.src.box = *src_box;46blit.dst.resource = dst;47blit.dst.format = dst->format;48blit.dst.level = dst_level;49blit.dst.box.x = dst_box->x;50blit.dst.box.y = dst_box->y;51blit.dst.box.z = dst_box->z;52blit.dst.box.width = dst_box->width;53blit.dst.box.height = dst_box->height;54blit.dst.box.depth = dst_box->depth;55blit.mask = util_format_get_mask(src->format) &56util_format_get_mask(dst->format);57blit.filter = PIPE_TEX_FILTER_NEAREST;5859if (blit.mask) {60pipe->blit(pipe, &blit);61}62}6364static unsigned temp_bind(unsigned orig)65{66unsigned warn = ~(PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL |67PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DISPLAY_TARGET);68if (orig & warn)69debug_printf("VIRGL: Warning, possibly unhandled bind: %x\n",70orig & warn);7172return orig & (PIPE_BIND_DEPTH_STENCIL | PIPE_BIND_RENDER_TARGET);73}7475static void virgl_init_temp_resource_from_box(struct pipe_resource *res,76struct pipe_resource *orig,77const struct pipe_box *box,78unsigned level, unsigned flags,79enum pipe_format fmt)80{81memset(res, 0, sizeof(*res));82res->bind = temp_bind(orig->bind);83res->format = fmt;84res->width0 = box->width;85res->height0 = box->height;86res->depth0 = 1;87res->array_size = 1;88res->usage = PIPE_USAGE_STAGING;89res->flags = flags;9091/* We must set the correct texture target and dimensions for a 3D box. */92if (box->depth > 1 && util_max_layer(orig, level) > 0)93res->target = orig->target;94else95res->target = PIPE_TEXTURE_2D;9697if (res->target != PIPE_BUFFER)98res->bind = PIPE_BIND_RENDER_TARGET;99100switch (res->target) {101case PIPE_TEXTURE_1D_ARRAY:102case PIPE_TEXTURE_2D_ARRAY:103case PIPE_TEXTURE_CUBE_ARRAY:104res->array_size = box->depth;105break;106case PIPE_TEXTURE_3D:107res->depth0 = box->depth;108break;109default:110break;111}112}113114static void *texture_transfer_map_resolve(struct pipe_context *ctx,115struct pipe_resource *resource,116unsigned level,117unsigned usage,118const struct pipe_box *box,119struct pipe_transfer **transfer)120{121struct virgl_context *vctx = virgl_context(ctx);122struct virgl_resource *vtex = virgl_resource(resource);123struct pipe_resource templ, *resolve_tmp;124struct virgl_transfer *trans;125126trans = virgl_resource_create_transfer(vctx, resource,127&vtex->metadata, level, usage, box);128if (!trans)129return NULL;130131enum pipe_format fmt = resource->format;132if (!virgl_has_readback_format(ctx->screen, pipe_to_virgl_format(fmt))) {133if (util_format_fits_8unorm(util_format_description(fmt)))134fmt = PIPE_FORMAT_R8G8B8A8_UNORM;135else if (util_format_is_pure_sint(fmt))136fmt = PIPE_FORMAT_R32G32B32A32_SINT;137else if (util_format_is_pure_uint(fmt))138fmt = PIPE_FORMAT_R32G32B32A32_UINT;139else140fmt = PIPE_FORMAT_R32G32B32A32_FLOAT;141assert(virgl_has_readback_format(ctx->screen, pipe_to_virgl_format(fmt)));142}143144struct pipe_box dst_box = *box;145dst_box.x = dst_box.y = dst_box.z = 0;146if (usage & PIPE_MAP_READ) {147/* readback should scale to the block size */148dst_box.width = align(dst_box.width,149util_format_get_blockwidth(resource->format));150dst_box.height = align(dst_box.height,151util_format_get_blockheight(resource->format));152}153154virgl_init_temp_resource_from_box(&templ, resource, &dst_box, level, 0, fmt);155156resolve_tmp = ctx->screen->resource_create(ctx->screen, &templ);157if (!resolve_tmp)158return NULL;159160if (usage & PIPE_MAP_READ) {161virgl_copy_region_with_blit(ctx, resolve_tmp, 0, &dst_box, resource,162level, box);163ctx->flush(ctx, NULL, 0);164}165166void *ptr = virgl_resource_transfer_map(ctx, resolve_tmp, 0, usage, &dst_box,167&trans->resolve_transfer);168if (!ptr)169goto fail;170171/* trans->resolve_transfer owns resolve_tmp now */172pipe_resource_reference(&resolve_tmp, NULL);173174*transfer = &trans->base;175if (fmt == resource->format) {176trans->base.stride = trans->resolve_transfer->stride;177trans->base.layer_stride = trans->resolve_transfer->layer_stride;178return ptr;179} else {180if (usage & PIPE_MAP_READ) {181struct virgl_winsys *vws = virgl_screen(ctx->screen)->vws;182void *src = ptr;183ptr = vws->resource_map(vws, vtex->hw_res);184if (!ptr)185goto fail;186187if (!util_format_translate_3d(resource->format,188ptr + vtex->metadata.level_offset[level],189trans->base.stride,190trans->base.layer_stride,191box->x, box->y, box->z,192fmt,193src,194trans->resolve_transfer->stride,195trans->resolve_transfer->layer_stride,1960, 0, 0,197dst_box.width,198dst_box.height,199dst_box.depth)) {200debug_printf("failed to translate format %s to %s\n",201util_format_short_name(fmt),202util_format_short_name(resource->format));203goto fail;204}205}206207if ((usage & PIPE_MAP_WRITE) == 0)208pipe_resource_reference(&trans->resolve_transfer->resource, NULL);209210return ptr + trans->offset;211}212213fail:214pipe_resource_reference(&resolve_tmp, NULL);215virgl_resource_destroy_transfer(vctx, trans);216return NULL;217}218219static bool needs_resolve(struct pipe_screen *screen,220struct pipe_resource *resource, unsigned usage)221{222if (resource->nr_samples > 1)223return true;224225if (usage & PIPE_MAP_READ)226return !util_format_is_depth_or_stencil(resource->format) &&227!virgl_has_readback_format(screen, pipe_to_virgl_format(resource->format));228229return false;230}231232void *virgl_texture_transfer_map(struct pipe_context *ctx,233struct pipe_resource *resource,234unsigned level,235unsigned usage,236const struct pipe_box *box,237struct pipe_transfer **transfer)238{239if (needs_resolve(ctx->screen, resource, usage))240return texture_transfer_map_resolve(ctx, resource, level, usage, box,241transfer);242243return virgl_resource_transfer_map(ctx, resource, level, usage, box, transfer);244}245246static void flush_data(struct pipe_context *ctx,247struct virgl_transfer *trans,248const struct pipe_box *box)249{250struct virgl_winsys *vws = virgl_screen(ctx->screen)->vws;251vws->transfer_put(vws, trans->hw_res, box,252trans->base.stride, trans->l_stride, trans->offset,253trans->base.level);254}255256void virgl_texture_transfer_unmap(struct pipe_context *ctx,257struct pipe_transfer *transfer)258{259struct virgl_context *vctx = virgl_context(ctx);260struct virgl_transfer *trans = virgl_transfer(transfer);261bool queue_unmap = false;262263if (transfer->usage & PIPE_MAP_WRITE &&264(transfer->usage & PIPE_MAP_FLUSH_EXPLICIT) == 0) {265266if (trans->resolve_transfer && (trans->base.resource->format ==267trans->resolve_transfer->resource->format)) {268flush_data(ctx, virgl_transfer(trans->resolve_transfer),269&trans->resolve_transfer->box);270271/* FINISHME: In case the destination format isn't renderable here, the272* blit here will currently fail. This could for instance happen if the273* mapped resource is of a compressed format, and it's mapped with both274* read and write usage.275*/276277virgl_copy_region_with_blit(ctx,278trans->base.resource, trans->base.level,279&transfer->box,280trans->resolve_transfer->resource, 0,281&trans->resolve_transfer->box);282ctx->flush(ctx, NULL, 0);283} else284queue_unmap = true;285}286287if (trans->resolve_transfer) {288virgl_resource_destroy_transfer(vctx,289virgl_transfer(trans->resolve_transfer));290}291292if (queue_unmap) {293if (trans->copy_src_hw_res) {294virgl_encode_copy_transfer(vctx, trans);295virgl_resource_destroy_transfer(vctx, trans);296} else {297virgl_transfer_queue_unmap(&vctx->queue, trans);298}299} else {300virgl_resource_destroy_transfer(vctx, trans);301}302}303304void virgl_texture_init(struct virgl_resource *res)305{306}307308309