Path: blob/21.2-virgl/src/gallium/drivers/etnaviv/etnaviv_clear_blit.c
4570 views
/*1* Copyright (c) 2012-2015 Etnaviv Project2*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* the rights to use, copy, modify, merge, publish, distribute, sub license,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the11* next paragraph) shall be included in all copies or substantial portions12* of the 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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER20* DEALINGS IN THE SOFTWARE.21*22* Authors:23* Wladimir J. van der Laan <[email protected]>24*/2526#include "etnaviv_clear_blit.h"2728#include "hw/common.xml.h"2930#include "etnaviv_blt.h"31#include "etnaviv_context.h"32#include "etnaviv_emit.h"33#include "etnaviv_format.h"34#include "etnaviv_resource.h"35#include "etnaviv_rs.h"36#include "etnaviv_surface.h"37#include "etnaviv_translate.h"3839#include "pipe/p_defines.h"40#include "pipe/p_state.h"41#include "util/compiler.h"42#include "util/u_blitter.h"43#include "util/u_inlines.h"44#include "util/u_memory.h"45#include "util/u_surface.h"4647/* Save current state for blitter operation */48void49etna_blit_save_state(struct etna_context *ctx)50{51util_blitter_save_fragment_constant_buffer_slot(ctx->blitter,52ctx->constant_buffer[PIPE_SHADER_FRAGMENT].cb);53util_blitter_save_vertex_buffer_slot(ctx->blitter, ctx->vertex_buffer.vb);54util_blitter_save_vertex_elements(ctx->blitter, ctx->vertex_elements);55util_blitter_save_vertex_shader(ctx->blitter, ctx->shader.bind_vs);56util_blitter_save_rasterizer(ctx->blitter, ctx->rasterizer);57util_blitter_save_viewport(ctx->blitter, &ctx->viewport_s);58util_blitter_save_scissor(ctx->blitter, &ctx->scissor);59util_blitter_save_fragment_shader(ctx->blitter, ctx->shader.bind_fs);60util_blitter_save_blend(ctx->blitter, ctx->blend);61util_blitter_save_depth_stencil_alpha(ctx->blitter, ctx->zsa);62util_blitter_save_stencil_ref(ctx->blitter, &ctx->stencil_ref_s);63util_blitter_save_sample_mask(ctx->blitter, ctx->sample_mask);64util_blitter_save_framebuffer(ctx->blitter, &ctx->framebuffer_s);65util_blitter_save_fragment_sampler_states(ctx->blitter,66ctx->num_fragment_samplers, (void **)ctx->sampler);67util_blitter_save_fragment_sampler_views(ctx->blitter,68ctx->num_fragment_sampler_views, ctx->sampler_view);69}7071uint64_t72etna_clear_blit_pack_rgba(enum pipe_format format, const union pipe_color_union *color)73{74union util_color uc;7576util_pack_color_union(format, &uc, color);7778switch (util_format_get_blocksize(format)) {79case 1:80uc.ui[0] = uc.ui[0] << 8 | (uc.ui[0] & 0xff);81FALLTHROUGH;82case 2:83uc.ui[0] = uc.ui[0] << 16 | (uc.ui[0] & 0xffff);84FALLTHROUGH;85case 4:86uc.ui[1] = uc.ui[0];87FALLTHROUGH;88default:89return (uint64_t) uc.ui[1] << 32 | uc.ui[0];90}91}9293static void94etna_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)95{96struct etna_context *ctx = etna_context(pctx);97struct pipe_blit_info info = *blit_info;9899if (ctx->blit(pctx, &info))100return;101102if (util_try_blit_via_copy_region(pctx, &info))103return;104105if (info.mask & PIPE_MASK_S) {106DBG("cannot blit stencil, skipping");107info.mask &= ~PIPE_MASK_S;108}109110if (!util_blitter_is_blit_supported(ctx->blitter, &info)) {111DBG("blit unsupported %s -> %s",112util_format_short_name(info.src.resource->format),113util_format_short_name(info.dst.resource->format));114return;115}116117etna_blit_save_state(ctx);118util_blitter_blit(ctx->blitter, &info);119}120121static void122etna_clear_render_target(struct pipe_context *pctx, struct pipe_surface *dst,123const union pipe_color_union *color, unsigned dstx,124unsigned dsty, unsigned width, unsigned height,125bool render_condition_enabled)126{127struct etna_context *ctx = etna_context(pctx);128129/* XXX could fall back to RS when target area is full screen / resolveable130* and no TS. */131etna_blit_save_state(ctx);132util_blitter_clear_render_target(ctx->blitter, dst, color, dstx, dsty, width, height);133}134135static void136etna_clear_depth_stencil(struct pipe_context *pctx, struct pipe_surface *dst,137unsigned clear_flags, double depth, unsigned stencil,138unsigned dstx, unsigned dsty, unsigned width,139unsigned height, bool render_condition_enabled)140{141struct etna_context *ctx = etna_context(pctx);142143/* XXX could fall back to RS when target area is full screen / resolveable144* and no TS. */145etna_blit_save_state(ctx);146util_blitter_clear_depth_stencil(ctx->blitter, dst, clear_flags, depth,147stencil, dstx, dsty, width, height);148}149150static void151etna_resource_copy_region(struct pipe_context *pctx, struct pipe_resource *dst,152unsigned dst_level, unsigned dstx, unsigned dsty,153unsigned dstz, struct pipe_resource *src,154unsigned src_level, const struct pipe_box *src_box)155{156struct etna_context *ctx = etna_context(pctx);157158if (src->target != PIPE_BUFFER && dst->target != PIPE_BUFFER &&159util_blitter_is_copy_supported(ctx->blitter, dst, src)) {160etna_blit_save_state(ctx);161util_blitter_copy_texture(ctx->blitter, dst, dst_level, dstx, dsty, dstz,162src, src_level, src_box);163} else {164util_resource_copy_region(pctx, dst, dst_level, dstx, dsty, dstz, src,165src_level, src_box);166}167}168169static void170etna_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)171{172struct etna_resource *rsc = etna_resource(prsc);173174if (rsc->render) {175if (etna_resource_older(rsc, etna_resource(rsc->render))) {176etna_copy_resource(pctx, prsc, rsc->render, 0, 0);177rsc->seqno = etna_resource(rsc->render)->seqno;178}179} else if (etna_resource_needs_flush(rsc)) {180etna_copy_resource(pctx, prsc, prsc, 0, 0);181rsc->flush_seqno = rsc->seqno;182}183}184185void186etna_copy_resource(struct pipe_context *pctx, struct pipe_resource *dst,187struct pipe_resource *src, int first_level, int last_level)188{189struct etna_resource *src_priv = etna_resource(src);190struct etna_resource *dst_priv = etna_resource(dst);191192assert(src->format == dst->format);193assert(src->array_size == dst->array_size);194assert(last_level <= dst->last_level && last_level <= src->last_level);195196struct pipe_blit_info blit = {};197blit.mask = util_format_get_mask(dst->format);198blit.filter = PIPE_TEX_FILTER_NEAREST;199blit.src.resource = src;200blit.src.format = src->format;201blit.dst.resource = dst;202blit.dst.format = dst->format;203blit.dst.box.depth = blit.src.box.depth = 1;204205/* Copy each level and each layer */206for (int level = first_level; level <= last_level; level++) {207blit.src.level = blit.dst.level = level;208blit.src.box.width = blit.dst.box.width =209MIN2(src_priv->levels[level].padded_width, dst_priv->levels[level].padded_width);210blit.src.box.height = blit.dst.box.height =211MIN2(src_priv->levels[level].padded_height, dst_priv->levels[level].padded_height);212unsigned depth = MIN2(src_priv->levels[level].depth, dst_priv->levels[level].depth);213if (dst->array_size > 1) {214assert(depth == 1); /* no array of 3d texture */215depth = dst->array_size;216}217218for (int z = 0; z < depth; z++) {219blit.src.box.z = blit.dst.box.z = z;220pctx->blit(pctx, &blit);221}222}223}224225void226etna_copy_resource_box(struct pipe_context *pctx, struct pipe_resource *dst,227struct pipe_resource *src, int level,228struct pipe_box *box)229{230assert(src->format == dst->format);231assert(src->array_size == dst->array_size);232233struct pipe_blit_info blit = {};234blit.mask = util_format_get_mask(dst->format);235blit.filter = PIPE_TEX_FILTER_NEAREST;236blit.src.resource = src;237blit.src.format = src->format;238blit.src.box = *box;239blit.dst.resource = dst;240blit.dst.format = dst->format;241blit.dst.box = *box;242243blit.dst.box.depth = blit.src.box.depth = 1;244blit.src.level = blit.dst.level = level;245246for (int z = 0; z < box->depth; z++) {247blit.src.box.z = blit.dst.box.z = box->z + z;248pctx->blit(pctx, &blit);249}250}251252void253etna_clear_blit_init(struct pipe_context *pctx)254{255struct etna_context *ctx = etna_context(pctx);256struct etna_screen *screen = ctx->screen;257258pctx->blit = etna_blit;259pctx->clear_render_target = etna_clear_render_target;260pctx->clear_depth_stencil = etna_clear_depth_stencil;261pctx->resource_copy_region = etna_resource_copy_region;262pctx->flush_resource = etna_flush_resource;263264if (screen->specs.use_blt)265etna_clear_blit_blt_init(pctx);266else267etna_clear_blit_rs_init(pctx);268}269270271