Path: blob/21.2-virgl/src/gallium/drivers/radeonsi/si_blit.c
4570 views
/*1* Copyright 2010 Jerome Glisse <[email protected]>2* Copyright 2015 Advanced Micro Devices, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the "Software"),7* to deal in the Software without restriction, including without limitation8* on the rights to use, copy, modify, merge, publish, distribute, sub9* license, and/or sell copies of the Software, and to permit persons to whom10* the Software is furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice (including the next13* paragraph) shall be included in all copies or substantial portions of the14* Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL19* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,20* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR21* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE22* USE OR OTHER DEALINGS IN THE SOFTWARE.23*/2425#include "si_compute.h"26#include "si_pipe.h"27#include "util/format/u_format.h"28#include "util/u_log.h"29#include "util/u_surface.h"3031enum32{33SI_COPY =34SI_SAVE_FRAMEBUFFER | SI_SAVE_TEXTURES | SI_SAVE_FRAGMENT_STATE | SI_DISABLE_RENDER_COND,3536SI_BLIT = SI_SAVE_FRAMEBUFFER | SI_SAVE_TEXTURES | SI_SAVE_FRAGMENT_STATE,3738SI_DECOMPRESS = SI_SAVE_FRAMEBUFFER | SI_SAVE_FRAGMENT_STATE | SI_DISABLE_RENDER_COND,3940SI_COLOR_RESOLVE = SI_SAVE_FRAMEBUFFER | SI_SAVE_FRAGMENT_STATE41};4243void si_blitter_begin(struct si_context *sctx, enum si_blitter_op op)44{45util_blitter_save_vertex_shader(sctx->blitter, sctx->shader.vs.cso);46util_blitter_save_tessctrl_shader(sctx->blitter, sctx->shader.tcs.cso);47util_blitter_save_tesseval_shader(sctx->blitter, sctx->shader.tes.cso);48util_blitter_save_geometry_shader(sctx->blitter, sctx->shader.gs.cso);49util_blitter_save_so_targets(sctx->blitter, sctx->streamout.num_targets,50(struct pipe_stream_output_target **)sctx->streamout.targets);51util_blitter_save_rasterizer(sctx->blitter, sctx->queued.named.rasterizer);5253if (op & SI_SAVE_FRAGMENT_STATE) {54util_blitter_save_blend(sctx->blitter, sctx->queued.named.blend);55util_blitter_save_depth_stencil_alpha(sctx->blitter, sctx->queued.named.dsa);56util_blitter_save_stencil_ref(sctx->blitter, &sctx->stencil_ref.state);57util_blitter_save_fragment_shader(sctx->blitter, sctx->shader.ps.cso);58util_blitter_save_sample_mask(sctx->blitter, sctx->sample_mask);59util_blitter_save_scissor(sctx->blitter, &sctx->scissors[0]);60util_blitter_save_window_rectangles(sctx->blitter, sctx->window_rectangles_include,61sctx->num_window_rectangles, sctx->window_rectangles);62}6364if (op & SI_SAVE_FRAMEBUFFER)65util_blitter_save_framebuffer(sctx->blitter, &sctx->framebuffer.state);6667if (op & SI_SAVE_TEXTURES) {68util_blitter_save_fragment_sampler_states(69sctx->blitter, 2, (void **)sctx->samplers[PIPE_SHADER_FRAGMENT].sampler_states);7071util_blitter_save_fragment_sampler_views(sctx->blitter, 2,72sctx->samplers[PIPE_SHADER_FRAGMENT].views);73}7475if (op & SI_DISABLE_RENDER_COND)76sctx->render_cond_enabled = false;7778if (sctx->screen->dpbb_allowed) {79sctx->dpbb_force_off = true;80si_mark_atom_dirty(sctx, &sctx->atoms.s.dpbb_state);81}8283sctx->blitter_running = true;84}8586void si_blitter_end(struct si_context *sctx)87{88sctx->blitter_running = false;8990if (sctx->screen->dpbb_allowed) {91sctx->dpbb_force_off = false;92si_mark_atom_dirty(sctx, &sctx->atoms.s.dpbb_state);93}9495sctx->render_cond_enabled = sctx->render_cond;9697/* Restore shader pointers because the VS blit shader changed all98* non-global VS user SGPRs. */99sctx->shader_pointers_dirty |= SI_DESCS_SHADER_MASK(VERTEX);100sctx->vertex_buffer_pointer_dirty = sctx->vb_descriptors_buffer != NULL &&101sctx->num_vertex_elements >102sctx->screen->num_vbos_in_user_sgprs;103sctx->vertex_buffer_user_sgprs_dirty = sctx->num_vertex_elements > 0 &&104sctx->screen->num_vbos_in_user_sgprs;105si_mark_atom_dirty(sctx, &sctx->atoms.s.shader_pointers);106}107108static unsigned u_max_sample(struct pipe_resource *r)109{110return r->nr_samples ? r->nr_samples - 1 : 0;111}112113static unsigned si_blit_dbcb_copy(struct si_context *sctx, struct si_texture *src,114struct si_texture *dst, unsigned planes, unsigned level_mask,115unsigned first_layer, unsigned last_layer, unsigned first_sample,116unsigned last_sample)117{118struct pipe_surface surf_tmpl = {{0}};119unsigned layer, sample, checked_last_layer, max_layer;120unsigned fully_copied_levels = 0;121122if (planes & PIPE_MASK_Z)123sctx->dbcb_depth_copy_enabled = true;124if (planes & PIPE_MASK_S)125sctx->dbcb_stencil_copy_enabled = true;126si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);127128assert(sctx->dbcb_depth_copy_enabled || sctx->dbcb_stencil_copy_enabled);129130sctx->decompression_enabled = true;131132while (level_mask) {133unsigned level = u_bit_scan(&level_mask);134135/* The smaller the mipmap level, the less layers there are136* as far as 3D textures are concerned. */137max_layer = util_max_layer(&src->buffer.b.b, level);138checked_last_layer = MIN2(last_layer, max_layer);139140surf_tmpl.u.tex.level = level;141142for (layer = first_layer; layer <= checked_last_layer; layer++) {143struct pipe_surface *zsurf, *cbsurf;144145surf_tmpl.format = src->buffer.b.b.format;146surf_tmpl.u.tex.first_layer = layer;147surf_tmpl.u.tex.last_layer = layer;148149zsurf = sctx->b.create_surface(&sctx->b, &src->buffer.b.b, &surf_tmpl);150151surf_tmpl.format = dst->buffer.b.b.format;152cbsurf = sctx->b.create_surface(&sctx->b, &dst->buffer.b.b, &surf_tmpl);153154for (sample = first_sample; sample <= last_sample; sample++) {155if (sample != sctx->dbcb_copy_sample) {156sctx->dbcb_copy_sample = sample;157si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);158}159160si_blitter_begin(sctx, SI_DECOMPRESS);161util_blitter_custom_depth_stencil(sctx->blitter, zsurf, cbsurf, 1 << sample,162sctx->custom_dsa_flush, 1.0f);163si_blitter_end(sctx);164}165166pipe_surface_reference(&zsurf, NULL);167pipe_surface_reference(&cbsurf, NULL);168}169170if (first_layer == 0 && last_layer >= max_layer && first_sample == 0 &&171last_sample >= u_max_sample(&src->buffer.b.b))172fully_copied_levels |= 1u << level;173}174175sctx->decompression_enabled = false;176sctx->dbcb_depth_copy_enabled = false;177sctx->dbcb_stencil_copy_enabled = false;178si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);179180return fully_copied_levels;181}182183/* Helper function for si_blit_decompress_zs_in_place.184*/185static void si_blit_decompress_zs_planes_in_place(struct si_context *sctx,186struct si_texture *texture, unsigned planes,187unsigned level_mask, unsigned first_layer,188unsigned last_layer)189{190struct pipe_surface *zsurf, surf_tmpl = {{0}};191unsigned layer, max_layer, checked_last_layer;192unsigned fully_decompressed_mask = 0;193194if (!level_mask)195return;196197if (planes & PIPE_MASK_S)198sctx->db_flush_stencil_inplace = true;199if (planes & PIPE_MASK_Z)200sctx->db_flush_depth_inplace = true;201si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);202203surf_tmpl.format = texture->buffer.b.b.format;204205sctx->decompression_enabled = true;206207while (level_mask) {208unsigned level = u_bit_scan(&level_mask);209210surf_tmpl.u.tex.level = level;211212/* The smaller the mipmap level, the less layers there are213* as far as 3D textures are concerned. */214max_layer = util_max_layer(&texture->buffer.b.b, level);215checked_last_layer = MIN2(last_layer, max_layer);216217for (layer = first_layer; layer <= checked_last_layer; layer++) {218surf_tmpl.u.tex.first_layer = layer;219surf_tmpl.u.tex.last_layer = layer;220221zsurf = sctx->b.create_surface(&sctx->b, &texture->buffer.b.b, &surf_tmpl);222223si_blitter_begin(sctx, SI_DECOMPRESS);224util_blitter_custom_depth_stencil(sctx->blitter, zsurf, NULL, ~0, sctx->custom_dsa_flush,2251.0f);226si_blitter_end(sctx);227228pipe_surface_reference(&zsurf, NULL);229}230231/* The texture will always be dirty if some layers aren't flushed.232* I don't think this case occurs often though. */233if (first_layer == 0 && last_layer >= max_layer) {234fully_decompressed_mask |= 1u << level;235}236}237238if (planes & PIPE_MASK_Z)239texture->dirty_level_mask &= ~fully_decompressed_mask;240if (planes & PIPE_MASK_S)241texture->stencil_dirty_level_mask &= ~fully_decompressed_mask;242243sctx->decompression_enabled = false;244sctx->db_flush_depth_inplace = false;245sctx->db_flush_stencil_inplace = false;246si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);247}248249/* Helper function of si_flush_depth_texture: decompress the given levels250* of Z and/or S planes in place.251*/252static void si_blit_decompress_zs_in_place(struct si_context *sctx, struct si_texture *texture,253unsigned levels_z, unsigned levels_s,254unsigned first_layer, unsigned last_layer)255{256unsigned both = levels_z & levels_s;257258/* First, do combined Z & S decompresses for levels that need it. */259if (both) {260si_blit_decompress_zs_planes_in_place(sctx, texture, PIPE_MASK_Z | PIPE_MASK_S, both,261first_layer, last_layer);262levels_z &= ~both;263levels_s &= ~both;264}265266/* Now do separate Z and S decompresses. */267if (levels_z) {268si_blit_decompress_zs_planes_in_place(sctx, texture, PIPE_MASK_Z, levels_z, first_layer,269last_layer);270}271272if (levels_s) {273si_blit_decompress_zs_planes_in_place(sctx, texture, PIPE_MASK_S, levels_s, first_layer,274last_layer);275}276}277278static void si_decompress_depth(struct si_context *sctx, struct si_texture *tex,279unsigned required_planes, unsigned first_level, unsigned last_level,280unsigned first_layer, unsigned last_layer)281{282unsigned inplace_planes = 0;283unsigned copy_planes = 0;284unsigned level_mask = u_bit_consecutive(first_level, last_level - first_level + 1);285unsigned levels_z = 0;286unsigned levels_s = 0;287288if (required_planes & PIPE_MASK_Z) {289levels_z = level_mask & tex->dirty_level_mask;290291if (levels_z) {292if (si_can_sample_zs(tex, false))293inplace_planes |= PIPE_MASK_Z;294else295copy_planes |= PIPE_MASK_Z;296}297}298if (required_planes & PIPE_MASK_S) {299levels_s = level_mask & tex->stencil_dirty_level_mask;300301if (levels_s) {302if (si_can_sample_zs(tex, true))303inplace_planes |= PIPE_MASK_S;304else305copy_planes |= PIPE_MASK_S;306}307}308309if (unlikely(sctx->log))310u_log_printf(sctx->log,311"\n------------------------------------------------\n"312"Decompress Depth (levels %u - %u, levels Z: 0x%x S: 0x%x)\n\n",313first_level, last_level, levels_z, levels_s);314315/* We may have to allocate the flushed texture here when called from316* si_decompress_subresource.317*/318if (copy_planes &&319(tex->flushed_depth_texture || si_init_flushed_depth_texture(&sctx->b, &tex->buffer.b.b))) {320struct si_texture *dst = tex->flushed_depth_texture;321unsigned fully_copied_levels;322unsigned levels = 0;323324assert(tex->flushed_depth_texture);325326if (util_format_is_depth_and_stencil(dst->buffer.b.b.format))327copy_planes = PIPE_MASK_Z | PIPE_MASK_S;328329if (copy_planes & PIPE_MASK_Z) {330levels |= levels_z;331levels_z = 0;332}333if (copy_planes & PIPE_MASK_S) {334levels |= levels_s;335levels_s = 0;336}337338fully_copied_levels = si_blit_dbcb_copy(sctx, tex, dst, copy_planes, levels, first_layer,339last_layer, 0, u_max_sample(&tex->buffer.b.b));340341if (copy_planes & PIPE_MASK_Z)342tex->dirty_level_mask &= ~fully_copied_levels;343if (copy_planes & PIPE_MASK_S)344tex->stencil_dirty_level_mask &= ~fully_copied_levels;345}346347if (inplace_planes) {348bool has_htile = si_htile_enabled(tex, first_level, inplace_planes);349bool tc_compat_htile = vi_tc_compat_htile_enabled(tex, first_level, inplace_planes);350351/* Don't decompress if there is no HTILE or when HTILE is352* TC-compatible. */353if (has_htile && !tc_compat_htile) {354si_blit_decompress_zs_in_place(sctx, tex, levels_z, levels_s, first_layer, last_layer);355} else {356/* This is only a cache flush.357*358* Only clear the mask that we are flushing, because359* si_make_DB_shader_coherent() treats different levels360* and depth and stencil differently.361*/362if (inplace_planes & PIPE_MASK_Z)363tex->dirty_level_mask &= ~levels_z;364if (inplace_planes & PIPE_MASK_S)365tex->stencil_dirty_level_mask &= ~levels_s;366}367368/* We just had to completely decompress Z/S for texturing. Enable369* TC-compatible HTILE on the next clear, so that the decompression370* doesn't have to be done for this texture ever again.371*372* TC-compatible HTILE might slightly reduce Z/S performance, but373* the decompression is much worse.374*/375if (has_htile && !tc_compat_htile &&376/* We can only transition the whole buffer in one clear, so no mipmapping: */377tex->buffer.b.b.last_level == 0 &&378tex->surface.flags & RADEON_SURF_TC_COMPATIBLE_HTILE &&379(inplace_planes & PIPE_MASK_Z || !tex->htile_stencil_disabled))380tex->enable_tc_compatible_htile_next_clear = true;381382/* Only in-place decompression needs to flush DB caches, or383* when we don't decompress but TC-compatible planes are dirty.384*/385si_make_DB_shader_coherent(sctx, tex->buffer.b.b.nr_samples, inplace_planes & PIPE_MASK_S,386tc_compat_htile);387}388/* set_framebuffer_state takes care of coherency for single-sample.389* The DB->CB copy uses CB for the final writes.390*/391if (copy_planes && tex->buffer.b.b.nr_samples > 1)392si_make_CB_shader_coherent(sctx, tex->buffer.b.b.nr_samples, false, true /* no DCC */);393}394395static void si_decompress_sampler_depth_textures(struct si_context *sctx,396struct si_samplers *textures)397{398unsigned i;399unsigned mask = textures->needs_depth_decompress_mask;400401while (mask) {402struct pipe_sampler_view *view;403struct si_sampler_view *sview;404struct si_texture *tex;405406i = u_bit_scan(&mask);407408view = textures->views[i];409assert(view);410sview = (struct si_sampler_view *)view;411412tex = (struct si_texture *)view->texture;413assert(tex->db_compatible);414415si_decompress_depth(sctx, tex, sview->is_stencil_sampler ? PIPE_MASK_S : PIPE_MASK_Z,416view->u.tex.first_level, view->u.tex.last_level, 0,417util_max_layer(&tex->buffer.b.b, view->u.tex.first_level));418}419}420421static void si_blit_decompress_color(struct si_context *sctx, struct si_texture *tex,422unsigned first_level, unsigned last_level,423unsigned first_layer, unsigned last_layer,424bool need_dcc_decompress, bool need_fmask_expand)425{426void *custom_blend;427unsigned layer, checked_last_layer, max_layer;428unsigned level_mask = u_bit_consecutive(first_level, last_level - first_level + 1);429430if (!need_dcc_decompress)431level_mask &= tex->dirty_level_mask;432if (!level_mask)433goto expand_fmask;434435if (unlikely(sctx->log))436u_log_printf(sctx->log,437"\n------------------------------------------------\n"438"Decompress Color (levels %u - %u, mask 0x%x)\n\n",439first_level, last_level, level_mask);440441if (need_dcc_decompress) {442assert(sctx->chip_class == GFX8 || tex->buffer.b.b.nr_storage_samples >= 2);443custom_blend = sctx->custom_blend_dcc_decompress;444445assert(vi_dcc_enabled(tex, first_level));446447/* disable levels without DCC */448for (int i = first_level; i <= last_level; i++) {449if (!vi_dcc_enabled(tex, i))450level_mask &= ~(1 << i);451}452} else if (tex->surface.fmask_size) {453custom_blend = sctx->custom_blend_fmask_decompress;454} else {455custom_blend = sctx->custom_blend_eliminate_fastclear;456}457458sctx->decompression_enabled = true;459460while (level_mask) {461unsigned level = u_bit_scan(&level_mask);462463/* The smaller the mipmap level, the less layers there are464* as far as 3D textures are concerned. */465max_layer = util_max_layer(&tex->buffer.b.b, level);466checked_last_layer = MIN2(last_layer, max_layer);467468for (layer = first_layer; layer <= checked_last_layer; layer++) {469struct pipe_surface *cbsurf, surf_tmpl;470471surf_tmpl.format = tex->buffer.b.b.format;472surf_tmpl.u.tex.level = level;473surf_tmpl.u.tex.first_layer = layer;474surf_tmpl.u.tex.last_layer = layer;475cbsurf = sctx->b.create_surface(&sctx->b, &tex->buffer.b.b, &surf_tmpl);476477/* Required before and after FMASK and DCC_DECOMPRESS. */478if (custom_blend == sctx->custom_blend_fmask_decompress ||479custom_blend == sctx->custom_blend_dcc_decompress)480sctx->flags |= SI_CONTEXT_FLUSH_AND_INV_CB;481482si_blitter_begin(sctx, SI_DECOMPRESS);483util_blitter_custom_color(sctx->blitter, cbsurf, custom_blend);484si_blitter_end(sctx);485486if (custom_blend == sctx->custom_blend_fmask_decompress ||487custom_blend == sctx->custom_blend_dcc_decompress)488sctx->flags |= SI_CONTEXT_FLUSH_AND_INV_CB;489490/* When running FMASK decompresion with DCC, we need to run the "eliminate fast clear" pass491* separately because FMASK decompression doesn't eliminate DCC fast clear. This makes492* render->texture transitions more expensive. It can be disabled by493* allow_dcc_msaa_clear_to_reg_for_bpp.494*495* TODO: When we get here, change the compression to TC-compatible on the next clear496* to disable both the FMASK decompression and fast clear elimination passes.497*/498if (sctx->screen->allow_dcc_msaa_clear_to_reg_for_bpp[util_logbase2(tex->surface.bpe)] &&499custom_blend == sctx->custom_blend_fmask_decompress &&500vi_dcc_enabled(tex, level)) {501si_blitter_begin(sctx, SI_DECOMPRESS);502util_blitter_custom_color(sctx->blitter, cbsurf, sctx->custom_blend_eliminate_fastclear);503si_blitter_end(sctx);504}505506pipe_surface_reference(&cbsurf, NULL);507}508509/* The texture will always be dirty if some layers aren't flushed.510* I don't think this case occurs often though. */511if (first_layer == 0 && last_layer >= max_layer) {512tex->dirty_level_mask &= ~(1 << level);513}514}515516sctx->decompression_enabled = false;517si_make_CB_shader_coherent(sctx, tex->buffer.b.b.nr_samples, vi_dcc_enabled(tex, first_level),518tex->surface.u.gfx9.color.dcc.pipe_aligned);519520expand_fmask:521if (need_fmask_expand && tex->surface.fmask_offset && !tex->fmask_is_identity) {522si_compute_expand_fmask(&sctx->b, &tex->buffer.b.b);523tex->fmask_is_identity = true;524}525}526527static void si_decompress_color_texture(struct si_context *sctx, struct si_texture *tex,528unsigned first_level, unsigned last_level,529bool need_fmask_expand)530{531/* CMASK or DCC can be discarded and we can still end up here. */532if (!tex->cmask_buffer && !tex->surface.fmask_size &&533!vi_dcc_enabled(tex, first_level))534return;535536si_blit_decompress_color(sctx, tex, first_level, last_level, 0,537util_max_layer(&tex->buffer.b.b, first_level), false,538need_fmask_expand);539}540541static void si_decompress_sampler_color_textures(struct si_context *sctx,542struct si_samplers *textures)543{544unsigned i;545unsigned mask = textures->needs_color_decompress_mask;546547while (mask) {548struct pipe_sampler_view *view;549struct si_texture *tex;550551i = u_bit_scan(&mask);552553view = textures->views[i];554assert(view);555556tex = (struct si_texture *)view->texture;557558si_decompress_color_texture(sctx, tex, view->u.tex.first_level, view->u.tex.last_level,559false);560}561}562563static void si_decompress_image_color_textures(struct si_context *sctx, struct si_images *images)564{565unsigned i;566unsigned mask = images->needs_color_decompress_mask;567568while (mask) {569const struct pipe_image_view *view;570struct si_texture *tex;571572i = u_bit_scan(&mask);573574view = &images->views[i];575assert(view->resource->target != PIPE_BUFFER);576577tex = (struct si_texture *)view->resource;578579si_decompress_color_texture(sctx, tex, view->u.tex.level, view->u.tex.level,580view->access & PIPE_IMAGE_ACCESS_WRITE);581}582}583584static void si_check_render_feedback_texture(struct si_context *sctx, struct si_texture *tex,585unsigned first_level, unsigned last_level,586unsigned first_layer, unsigned last_layer)587{588bool render_feedback = false;589590if (!vi_dcc_enabled(tex, first_level))591return;592593for (unsigned j = 0; j < sctx->framebuffer.state.nr_cbufs; ++j) {594struct si_surface *surf;595596if (!sctx->framebuffer.state.cbufs[j])597continue;598599surf = (struct si_surface *)sctx->framebuffer.state.cbufs[j];600601if (tex == (struct si_texture *)surf->base.texture && surf->base.u.tex.level >= first_level &&602surf->base.u.tex.level <= last_level && surf->base.u.tex.first_layer <= last_layer &&603surf->base.u.tex.last_layer >= first_layer) {604render_feedback = true;605break;606}607}608609if (render_feedback)610si_texture_disable_dcc(sctx, tex);611}612613static void si_check_render_feedback_textures(struct si_context *sctx, struct si_samplers *textures,614uint32_t in_use_mask)615{616uint32_t mask = textures->enabled_mask & in_use_mask;617618while (mask) {619const struct pipe_sampler_view *view;620struct si_texture *tex;621622unsigned i = u_bit_scan(&mask);623624view = textures->views[i];625if (view->texture->target == PIPE_BUFFER)626continue;627628tex = (struct si_texture *)view->texture;629630si_check_render_feedback_texture(sctx, tex, view->u.tex.first_level, view->u.tex.last_level,631view->u.tex.first_layer, view->u.tex.last_layer);632}633}634635static void si_check_render_feedback_images(struct si_context *sctx, struct si_images *images,636uint32_t in_use_mask)637{638uint32_t mask = images->enabled_mask & in_use_mask;639640while (mask) {641const struct pipe_image_view *view;642struct si_texture *tex;643644unsigned i = u_bit_scan(&mask);645646view = &images->views[i];647if (view->resource->target == PIPE_BUFFER)648continue;649650tex = (struct si_texture *)view->resource;651652si_check_render_feedback_texture(sctx, tex, view->u.tex.level, view->u.tex.level,653view->u.tex.first_layer, view->u.tex.last_layer);654}655}656657static void si_check_render_feedback_resident_textures(struct si_context *sctx)658{659util_dynarray_foreach (&sctx->resident_tex_handles, struct si_texture_handle *, tex_handle) {660struct pipe_sampler_view *view;661struct si_texture *tex;662663view = (*tex_handle)->view;664if (view->texture->target == PIPE_BUFFER)665continue;666667tex = (struct si_texture *)view->texture;668669si_check_render_feedback_texture(sctx, tex, view->u.tex.first_level, view->u.tex.last_level,670view->u.tex.first_layer, view->u.tex.last_layer);671}672}673674static void si_check_render_feedback_resident_images(struct si_context *sctx)675{676util_dynarray_foreach (&sctx->resident_img_handles, struct si_image_handle *, img_handle) {677struct pipe_image_view *view;678struct si_texture *tex;679680view = &(*img_handle)->view;681if (view->resource->target == PIPE_BUFFER)682continue;683684tex = (struct si_texture *)view->resource;685686si_check_render_feedback_texture(sctx, tex, view->u.tex.level, view->u.tex.level,687view->u.tex.first_layer, view->u.tex.last_layer);688}689}690691static void si_check_render_feedback(struct si_context *sctx)692{693if (!sctx->need_check_render_feedback)694return;695696/* There is no render feedback if color writes are disabled.697* (e.g. a pixel shader with image stores)698*/699if (!si_get_total_colormask(sctx))700return;701702for (int i = 0; i < SI_NUM_GRAPHICS_SHADERS; ++i) {703if (!sctx->shaders[i].cso)704continue;705706struct si_shader_info *info = &sctx->shaders[i].cso->info;707si_check_render_feedback_images(sctx, &sctx->images[i],708u_bit_consecutive(0, info->base.num_images));709si_check_render_feedback_textures(sctx, &sctx->samplers[i],710info->base.textures_used[0]);711}712713si_check_render_feedback_resident_images(sctx);714si_check_render_feedback_resident_textures(sctx);715716sctx->need_check_render_feedback = false;717}718719static void si_decompress_resident_textures(struct si_context *sctx)720{721util_dynarray_foreach (&sctx->resident_tex_needs_color_decompress, struct si_texture_handle *,722tex_handle) {723struct pipe_sampler_view *view = (*tex_handle)->view;724struct si_texture *tex = (struct si_texture *)view->texture;725726si_decompress_color_texture(sctx, tex, view->u.tex.first_level, view->u.tex.last_level,727false);728}729730util_dynarray_foreach (&sctx->resident_tex_needs_depth_decompress, struct si_texture_handle *,731tex_handle) {732struct pipe_sampler_view *view = (*tex_handle)->view;733struct si_sampler_view *sview = (struct si_sampler_view *)view;734struct si_texture *tex = (struct si_texture *)view->texture;735736si_decompress_depth(sctx, tex, sview->is_stencil_sampler ? PIPE_MASK_S : PIPE_MASK_Z,737view->u.tex.first_level, view->u.tex.last_level, 0,738util_max_layer(&tex->buffer.b.b, view->u.tex.first_level));739}740}741742static void si_decompress_resident_images(struct si_context *sctx)743{744util_dynarray_foreach (&sctx->resident_img_needs_color_decompress, struct si_image_handle *,745img_handle) {746struct pipe_image_view *view = &(*img_handle)->view;747struct si_texture *tex = (struct si_texture *)view->resource;748749si_decompress_color_texture(sctx, tex, view->u.tex.level, view->u.tex.level,750view->access & PIPE_IMAGE_ACCESS_WRITE);751}752}753754void si_decompress_textures(struct si_context *sctx, unsigned shader_mask)755{756unsigned compressed_colortex_counter, mask;757758if (sctx->blitter_running)759return;760761/* Update the compressed_colortex_mask if necessary. */762compressed_colortex_counter = p_atomic_read(&sctx->screen->compressed_colortex_counter);763if (compressed_colortex_counter != sctx->last_compressed_colortex_counter) {764sctx->last_compressed_colortex_counter = compressed_colortex_counter;765si_update_needs_color_decompress_masks(sctx);766}767768/* Decompress color & depth textures if needed. */769mask = sctx->shader_needs_decompress_mask & shader_mask;770while (mask) {771unsigned i = u_bit_scan(&mask);772773if (sctx->samplers[i].needs_depth_decompress_mask) {774si_decompress_sampler_depth_textures(sctx, &sctx->samplers[i]);775}776if (sctx->samplers[i].needs_color_decompress_mask) {777si_decompress_sampler_color_textures(sctx, &sctx->samplers[i]);778}779if (sctx->images[i].needs_color_decompress_mask) {780si_decompress_image_color_textures(sctx, &sctx->images[i]);781}782}783784if (shader_mask & u_bit_consecutive(0, SI_NUM_GRAPHICS_SHADERS)) {785if (sctx->uses_bindless_samplers)786si_decompress_resident_textures(sctx);787if (sctx->uses_bindless_images)788si_decompress_resident_images(sctx);789790if (sctx->ps_uses_fbfetch) {791struct pipe_surface *cb0 = sctx->framebuffer.state.cbufs[0];792si_decompress_color_texture(sctx, (struct si_texture *)cb0->texture,793cb0->u.tex.first_layer, cb0->u.tex.last_layer, false);794}795796si_check_render_feedback(sctx);797} else if (shader_mask & (1 << PIPE_SHADER_COMPUTE)) {798if (sctx->cs_shader_state.program->sel.info.uses_bindless_samplers)799si_decompress_resident_textures(sctx);800if (sctx->cs_shader_state.program->sel.info.uses_bindless_images)801si_decompress_resident_images(sctx);802}803}804805/* Helper for decompressing a portion of a color or depth resource before806* blitting if any decompression is needed.807* The driver doesn't decompress resources automatically while u_blitter is808* rendering. */809void si_decompress_subresource(struct pipe_context *ctx, struct pipe_resource *tex, unsigned planes,810unsigned level, unsigned first_layer, unsigned last_layer)811{812struct si_context *sctx = (struct si_context *)ctx;813struct si_texture *stex = (struct si_texture *)tex;814815if (stex->db_compatible) {816planes &= PIPE_MASK_Z | PIPE_MASK_S;817818if (!stex->surface.has_stencil)819planes &= ~PIPE_MASK_S;820821/* If we've rendered into the framebuffer and it's a blitting822* source, make sure the decompression pass is invoked823* by dirtying the framebuffer.824*/825if (sctx->framebuffer.state.zsbuf && sctx->framebuffer.state.zsbuf->u.tex.level == level &&826sctx->framebuffer.state.zsbuf->texture == tex)827si_update_fb_dirtiness_after_rendering(sctx);828829si_decompress_depth(sctx, stex, planes, level, level, first_layer, last_layer);830} else if (stex->surface.fmask_size || stex->cmask_buffer ||831vi_dcc_enabled(stex, level)) {832/* If we've rendered into the framebuffer and it's a blitting833* source, make sure the decompression pass is invoked834* by dirtying the framebuffer.835*/836for (unsigned i = 0; i < sctx->framebuffer.state.nr_cbufs; i++) {837if (sctx->framebuffer.state.cbufs[i] &&838sctx->framebuffer.state.cbufs[i]->u.tex.level == level &&839sctx->framebuffer.state.cbufs[i]->texture == tex) {840si_update_fb_dirtiness_after_rendering(sctx);841break;842}843}844845si_blit_decompress_color(sctx, stex, level, level, first_layer, last_layer, false, false);846}847}848849struct texture_orig_info {850unsigned format;851unsigned width0;852unsigned height0;853unsigned npix_x;854unsigned npix_y;855unsigned npix0_x;856unsigned npix0_y;857};858859static void si_use_compute_copy_for_float_formats(struct si_context *sctx,860struct pipe_resource *texture,861unsigned level) {862struct si_texture *tex = (struct si_texture *)texture;863864/* If we are uploading into FP16 or R11G11B10_FLOAT via a blit, CB clobbers NaNs,865* so in order to preserve them exactly, we have to use the compute blit.866* The compute blit is used only when the destination doesn't have DCC, so867* disable it here, which is kinda a hack.868* If we are uploading into 32-bit floats with DCC via a blit, NaNs will also get869* lost so we need to disable DCC as well.870*871* This makes KHR-GL45.texture_view.view_classes pass on gfx9.872*/873if (vi_dcc_enabled(tex, level) &&874util_format_is_float(texture->format) &&875sctx->chip_class < GFX10) {876si_texture_disable_dcc(sctx, tex);877}878}879880void si_resource_copy_region(struct pipe_context *ctx, struct pipe_resource *dst,881unsigned dst_level, unsigned dstx, unsigned dsty, unsigned dstz,882struct pipe_resource *src, unsigned src_level,883const struct pipe_box *src_box)884{885struct si_context *sctx = (struct si_context *)ctx;886struct si_texture *ssrc = (struct si_texture *)src;887struct si_texture *sdst = (struct si_texture *)dst;888struct pipe_surface *dst_view, dst_templ;889struct pipe_sampler_view src_templ, *src_view;890unsigned dst_width, dst_height, src_width0, src_height0;891unsigned dst_width0, dst_height0, src_force_level = 0;892struct pipe_box sbox, dstbox;893894/* Handle buffers first. */895if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {896si_copy_buffer(sctx, dst, src, dstx, src_box->x, src_box->width, SI_OP_SYNC_BEFORE_AFTER);897return;898}899900si_use_compute_copy_for_float_formats(sctx, dst, dst_level);901902if (!util_format_is_compressed(src->format) && !util_format_is_compressed(dst->format) &&903!util_format_is_depth_or_stencil(src->format) && src->nr_samples <= 1 &&904/* DCC compression from image store is enabled for GFX10+. */905(!vi_dcc_enabled(sdst, dst_level) || sctx->chip_class >= GFX10) &&906!(dst->target != src->target &&907(src->target == PIPE_TEXTURE_1D_ARRAY || dst->target == PIPE_TEXTURE_1D_ARRAY))) {908si_compute_copy_image(sctx, dst, dst_level, src, src_level, dstx, dsty, dstz,909src_box, false, SI_OP_SYNC_BEFORE_AFTER);910return;911}912913assert(u_max_sample(dst) == u_max_sample(src));914915/* The driver doesn't decompress resources automatically while916* u_blitter is rendering. */917si_decompress_subresource(ctx, src, PIPE_MASK_RGBAZS, src_level, src_box->z,918src_box->z + src_box->depth - 1);919920dst_width = u_minify(dst->width0, dst_level);921dst_height = u_minify(dst->height0, dst_level);922dst_width0 = dst->width0;923dst_height0 = dst->height0;924src_width0 = src->width0;925src_height0 = src->height0;926927util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);928util_blitter_default_src_texture(sctx->blitter, &src_templ, src, src_level);929930if (util_format_is_compressed(src->format) || util_format_is_compressed(dst->format)) {931unsigned blocksize = ssrc->surface.bpe;932933if (blocksize == 8)934src_templ.format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */935else936src_templ.format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */937dst_templ.format = src_templ.format;938939dst_width = util_format_get_nblocksx(dst->format, dst_width);940dst_height = util_format_get_nblocksy(dst->format, dst_height);941dst_width0 = util_format_get_nblocksx(dst->format, dst_width0);942dst_height0 = util_format_get_nblocksy(dst->format, dst_height0);943src_width0 = util_format_get_nblocksx(src->format, src_width0);944src_height0 = util_format_get_nblocksy(src->format, src_height0);945946dstx = util_format_get_nblocksx(dst->format, dstx);947dsty = util_format_get_nblocksy(dst->format, dsty);948949sbox.x = util_format_get_nblocksx(src->format, src_box->x);950sbox.y = util_format_get_nblocksy(src->format, src_box->y);951sbox.z = src_box->z;952sbox.width = util_format_get_nblocksx(src->format, src_box->width);953sbox.height = util_format_get_nblocksy(src->format, src_box->height);954sbox.depth = src_box->depth;955src_box = &sbox;956957src_force_level = src_level;958} else if (!util_blitter_is_copy_supported(sctx->blitter, dst, src)) {959if (util_format_is_subsampled_422(src->format)) {960src_templ.format = PIPE_FORMAT_R8G8B8A8_UINT;961dst_templ.format = PIPE_FORMAT_R8G8B8A8_UINT;962963dst_width = util_format_get_nblocksx(dst->format, dst_width);964dst_width0 = util_format_get_nblocksx(dst->format, dst_width0);965src_width0 = util_format_get_nblocksx(src->format, src_width0);966967dstx = util_format_get_nblocksx(dst->format, dstx);968969sbox = *src_box;970sbox.x = util_format_get_nblocksx(src->format, src_box->x);971sbox.width = util_format_get_nblocksx(src->format, src_box->width);972src_box = &sbox;973} else {974unsigned blocksize = ssrc->surface.bpe;975976switch (blocksize) {977case 1:978dst_templ.format = PIPE_FORMAT_R8_UNORM;979src_templ.format = PIPE_FORMAT_R8_UNORM;980break;981case 2:982dst_templ.format = PIPE_FORMAT_R8G8_UNORM;983src_templ.format = PIPE_FORMAT_R8G8_UNORM;984break;985case 4:986dst_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;987src_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;988break;989case 8:990dst_templ.format = PIPE_FORMAT_R16G16B16A16_UINT;991src_templ.format = PIPE_FORMAT_R16G16B16A16_UINT;992break;993case 16:994dst_templ.format = PIPE_FORMAT_R32G32B32A32_UINT;995src_templ.format = PIPE_FORMAT_R32G32B32A32_UINT;996break;997default:998fprintf(stderr, "Unhandled format %s with blocksize %u\n",999util_format_short_name(src->format), blocksize);1000assert(0);1001}1002}1003}10041005/* SNORM8 blitting has precision issues on some chips. Use the SINT1006* equivalent instead, which doesn't force DCC decompression.1007*/1008if (util_format_is_snorm8(dst_templ.format)) {1009dst_templ.format = src_templ.format = util_format_snorm8_to_sint8(dst_templ.format);1010}10111012vi_disable_dcc_if_incompatible_format(sctx, dst, dst_level, dst_templ.format);1013vi_disable_dcc_if_incompatible_format(sctx, src, src_level, src_templ.format);10141015/* Initialize the surface. */1016dst_view = si_create_surface_custom(ctx, dst, &dst_templ, dst_width0, dst_height0, dst_width,1017dst_height);10181019/* Initialize the sampler view. */1020src_view =1021si_create_sampler_view_custom(ctx, src, &src_templ, src_width0, src_height0, src_force_level);10221023u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height), abs(src_box->depth),1024&dstbox);10251026/* Copy. */1027si_blitter_begin(sctx, SI_COPY);1028util_blitter_blit_generic(sctx->blitter, dst_view, &dstbox, src_view, src_box, src_width0,1029src_height0, PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL, false);1030si_blitter_end(sctx);10311032pipe_surface_reference(&dst_view, NULL);1033pipe_sampler_view_reference(&src_view, NULL);1034}10351036static void si_do_CB_resolve(struct si_context *sctx, const struct pipe_blit_info *info,1037struct pipe_resource *dst, unsigned dst_level, unsigned dst_z,1038enum pipe_format format)1039{1040/* Required before and after CB_RESOLVE. */1041sctx->flags |= SI_CONTEXT_FLUSH_AND_INV_CB;10421043si_blitter_begin(1044sctx, SI_COLOR_RESOLVE | (info->render_condition_enable ? 0 : SI_DISABLE_RENDER_COND));1045util_blitter_custom_resolve_color(sctx->blitter, dst, dst_level, dst_z, info->src.resource,1046info->src.box.z, ~0, sctx->custom_blend_resolve, format);1047si_blitter_end(sctx);10481049/* Flush caches for possible texturing. */1050si_make_CB_shader_coherent(sctx, 1, false, true /* no DCC */);1051}10521053static bool resolve_formats_compatible(enum pipe_format src, enum pipe_format dst,1054bool src_swaps_rgb_to_bgr, bool *need_rgb_to_bgr)1055{1056*need_rgb_to_bgr = false;10571058if (src_swaps_rgb_to_bgr) {1059/* We must only check the swapped format. */1060enum pipe_format swapped_src = util_format_rgb_to_bgr(src);1061assert(swapped_src);1062return util_is_format_compatible(util_format_description(swapped_src),1063util_format_description(dst));1064}10651066if (util_is_format_compatible(util_format_description(src), util_format_description(dst)))1067return true;10681069enum pipe_format swapped_src = util_format_rgb_to_bgr(src);1070*need_rgb_to_bgr = util_is_format_compatible(util_format_description(swapped_src),1071util_format_description(dst));1072return *need_rgb_to_bgr;1073}10741075static bool do_hardware_msaa_resolve(struct pipe_context *ctx, const struct pipe_blit_info *info)1076{1077struct si_context *sctx = (struct si_context *)ctx;1078struct si_texture *src = (struct si_texture *)info->src.resource;1079struct si_texture *dst = (struct si_texture *)info->dst.resource;1080ASSERTED struct si_texture *stmp;1081unsigned dst_width = u_minify(info->dst.resource->width0, info->dst.level);1082unsigned dst_height = u_minify(info->dst.resource->height0, info->dst.level);1083enum pipe_format format = info->src.format;1084struct pipe_resource *tmp, templ;1085struct pipe_blit_info blit;10861087/* Check basic requirements for hw resolve. */1088if (!(info->src.resource->nr_samples > 1 && info->dst.resource->nr_samples <= 1 &&1089!util_format_is_pure_integer(format) && !util_format_is_depth_or_stencil(format) &&1090util_max_layer(info->src.resource, 0) == 0))1091return false;10921093/* Hardware MSAA resolve doesn't work if SPI format = NORM16_ABGR and1094* the format is R16G16. Use R16A16, which does work.1095*/1096if (format == PIPE_FORMAT_R16G16_UNORM)1097format = PIPE_FORMAT_R16A16_UNORM;1098if (format == PIPE_FORMAT_R16G16_SNORM)1099format = PIPE_FORMAT_R16A16_SNORM;11001101bool need_rgb_to_bgr = false;11021103/* Check the remaining requirements for hw resolve. */1104if (util_max_layer(info->dst.resource, info->dst.level) == 0 && !info->scissor_enable &&1105(info->mask & PIPE_MASK_RGBA) == PIPE_MASK_RGBA &&1106resolve_formats_compatible(info->src.format, info->dst.format,1107src->swap_rgb_to_bgr, &need_rgb_to_bgr) &&1108dst_width == info->src.resource->width0 && dst_height == info->src.resource->height0 &&1109info->dst.box.x == 0 && info->dst.box.y == 0 && info->dst.box.width == dst_width &&1110info->dst.box.height == dst_height && info->dst.box.depth == 1 && info->src.box.x == 0 &&1111info->src.box.y == 0 && info->src.box.width == dst_width &&1112info->src.box.height == dst_height && info->src.box.depth == 1 && !dst->surface.is_linear &&1113(!dst->cmask_buffer || !dst->dirty_level_mask)) { /* dst cannot be fast-cleared */1114/* Check the remaining constraints. */1115if (src->surface.micro_tile_mode != dst->surface.micro_tile_mode ||1116need_rgb_to_bgr) {1117/* The next fast clear will switch to this mode to1118* get direct hw resolve next time if the mode is1119* different now.1120*1121* TODO-GFX10: This does not work in GFX10 because MSAA1122* is restricted to 64KB_R_X and 64KB_Z_X swizzle modes.1123* In some cases we could change the swizzle of the1124* destination texture instead, but the more general1125* solution is to implement compute shader resolve.1126*/1127if (src->surface.micro_tile_mode != dst->surface.micro_tile_mode)1128src->last_msaa_resolve_target_micro_mode = dst->surface.micro_tile_mode;1129if (need_rgb_to_bgr)1130src->swap_rgb_to_bgr_on_next_clear = true;11311132goto resolve_to_temp;1133}11341135/* Resolving into a surface with DCC is unsupported. Since1136* it's being overwritten anyway, clear it to uncompressed.1137* This is still the fastest codepath even with this clear.1138*/1139if (vi_dcc_enabled(dst, info->dst.level)) {1140struct si_clear_info clear_info;11411142if (!vi_dcc_get_clear_info(sctx, dst, info->dst.level, DCC_UNCOMPRESSED, &clear_info))1143goto resolve_to_temp;11441145si_execute_clears(sctx, &clear_info, 1, SI_CLEAR_TYPE_DCC);1146dst->dirty_level_mask &= ~(1 << info->dst.level);1147}11481149/* Resolve directly from src to dst. */1150si_do_CB_resolve(sctx, info, info->dst.resource, info->dst.level, info->dst.box.z, format);1151return true;1152}11531154resolve_to_temp:1155/* Shader-based resolve is VERY SLOW. Instead, resolve into1156* a temporary texture and blit.1157*/1158memset(&templ, 0, sizeof(templ));1159templ.target = PIPE_TEXTURE_2D;1160templ.format = info->src.resource->format;1161templ.width0 = info->src.resource->width0;1162templ.height0 = info->src.resource->height0;1163templ.depth0 = 1;1164templ.array_size = 1;1165templ.usage = PIPE_USAGE_DEFAULT;1166templ.flags = SI_RESOURCE_FLAG_FORCE_MSAA_TILING | SI_RESOURCE_FLAG_FORCE_MICRO_TILE_MODE |1167SI_RESOURCE_FLAG_MICRO_TILE_MODE_SET(src->surface.micro_tile_mode) |1168SI_RESOURCE_FLAG_DISABLE_DCC | SI_RESOURCE_FLAG_DRIVER_INTERNAL;11691170/* The src and dst microtile modes must be the same. */1171if (sctx->chip_class <= GFX8 && src->surface.micro_tile_mode == RADEON_MICRO_MODE_DISPLAY)1172templ.bind = PIPE_BIND_SCANOUT;1173else1174templ.bind = 0;11751176tmp = ctx->screen->resource_create(ctx->screen, &templ);1177if (!tmp)1178return false;1179stmp = (struct si_texture *)tmp;1180/* Match the channel order of src. */1181stmp->swap_rgb_to_bgr = src->swap_rgb_to_bgr;11821183assert(!stmp->surface.is_linear);1184assert(src->surface.micro_tile_mode == stmp->surface.micro_tile_mode);11851186/* resolve */1187si_do_CB_resolve(sctx, info, tmp, 0, 0, format);11881189/* blit */1190blit = *info;1191blit.src.resource = tmp;1192blit.src.box.z = 0;11931194si_blitter_begin(sctx, SI_BLIT | (info->render_condition_enable ? 0 : SI_DISABLE_RENDER_COND));1195util_blitter_blit(sctx->blitter, &blit);1196si_blitter_end(sctx);11971198pipe_resource_reference(&tmp, NULL);1199return true;1200}12011202static void si_blit(struct pipe_context *ctx, const struct pipe_blit_info *info)1203{1204struct si_context *sctx = (struct si_context *)ctx;12051206if (do_hardware_msaa_resolve(ctx, info)) {1207return;1208}12091210if (unlikely(sctx->thread_trace_enabled))1211sctx->sqtt_next_event = EventCmdCopyImage;12121213/* Using compute for copying to a linear texture in GTT is much faster than1214* going through RBs (render backends). This improves DRI PRIME performance.1215*/1216if (util_can_blit_via_copy_region(info, false)) {1217si_resource_copy_region(ctx, info->dst.resource, info->dst.level,1218info->dst.box.x, info->dst.box.y, info->dst.box.z,1219info->src.resource, info->src.level, &info->src.box);1220return;1221}12221223assert(util_blitter_is_blit_supported(sctx->blitter, info));12241225/* The driver doesn't decompress resources automatically while1226* u_blitter is rendering. */1227vi_disable_dcc_if_incompatible_format(sctx, info->src.resource, info->src.level,1228info->src.format);1229vi_disable_dcc_if_incompatible_format(sctx, info->dst.resource, info->dst.level,1230info->dst.format);1231si_decompress_subresource(ctx, info->src.resource, PIPE_MASK_RGBAZS, info->src.level,1232info->src.box.z, info->src.box.z + info->src.box.depth - 1);12331234if (unlikely(sctx->thread_trace_enabled))1235sctx->sqtt_next_event = EventCmdBlitImage;12361237si_blitter_begin(sctx, SI_BLIT | (info->render_condition_enable ? 0 : SI_DISABLE_RENDER_COND));1238util_blitter_blit(sctx->blitter, info);1239si_blitter_end(sctx);1240}12411242static bool si_generate_mipmap(struct pipe_context *ctx, struct pipe_resource *tex,1243enum pipe_format format, unsigned base_level, unsigned last_level,1244unsigned first_layer, unsigned last_layer)1245{1246struct si_context *sctx = (struct si_context *)ctx;1247struct si_texture *stex = (struct si_texture *)tex;12481249if (!util_blitter_is_copy_supported(sctx->blitter, tex, tex))1250return false;12511252/* The driver doesn't decompress resources automatically while1253* u_blitter is rendering. */1254vi_disable_dcc_if_incompatible_format(sctx, tex, base_level, format);1255si_decompress_subresource(ctx, tex, PIPE_MASK_RGBAZS, base_level, first_layer, last_layer);12561257/* Clear dirty_level_mask for the levels that will be overwritten. */1258assert(base_level < last_level);1259stex->dirty_level_mask &= ~u_bit_consecutive(base_level + 1, last_level - base_level);12601261sctx->generate_mipmap_for_depth = stex->is_depth;12621263si_blitter_begin(sctx, SI_BLIT | SI_DISABLE_RENDER_COND);1264util_blitter_generate_mipmap(sctx->blitter, tex, format, base_level, last_level, first_layer,1265last_layer);1266si_blitter_end(sctx);12671268sctx->generate_mipmap_for_depth = false;1269return true;1270}12711272static void si_flush_resource(struct pipe_context *ctx, struct pipe_resource *res)1273{1274struct si_context *sctx = (struct si_context *)ctx;1275struct si_texture *tex = (struct si_texture *)res;12761277assert(res->target != PIPE_BUFFER);12781279if (!tex->is_depth && (tex->cmask_buffer || vi_dcc_enabled(tex, 0))) {1280si_blit_decompress_color(sctx, tex, 0, res->last_level, 0, util_max_layer(res, 0),1281false, false);12821283if (tex->surface.display_dcc_offset && tex->displayable_dcc_dirty) {1284si_retile_dcc(sctx, tex);1285tex->displayable_dcc_dirty = false;1286}1287}1288}12891290void si_flush_implicit_resources(struct si_context *sctx)1291{1292hash_table_foreach(sctx->dirty_implicit_resources, entry) {1293si_flush_resource(&sctx->b, entry->data);1294pipe_resource_reference((struct pipe_resource **)&entry->data, NULL);1295}1296_mesa_hash_table_clear(sctx->dirty_implicit_resources, NULL);1297}12981299void si_decompress_dcc(struct si_context *sctx, struct si_texture *tex)1300{1301assert(!tex->is_depth);13021303/* If graphics is disabled, we can't decompress DCC, but it shouldn't1304* be compressed either. The caller should simply discard it.1305*/1306if (!tex->surface.meta_offset || !sctx->has_graphics)1307return;13081309if (sctx->chip_class == GFX8 || tex->buffer.b.b.nr_storage_samples >= 2) {1310si_blit_decompress_color(sctx, tex, 0, tex->buffer.b.b.last_level, 0,1311util_max_layer(&tex->buffer.b.b, 0), true, false);1312} else {1313struct pipe_resource *ptex = &tex->buffer.b.b;1314assert(ptex->nr_storage_samples <= 1);13151316/* DCC decompression using a compute shader. */1317for (unsigned level = 0; level < tex->surface.num_meta_levels; level++) {1318struct pipe_box box;13191320u_box_3d(0, 0, 0, u_minify(ptex->width0, level),1321u_minify(ptex->height0, level),1322util_num_layers(ptex, level), &box);1323si_compute_copy_image(sctx, ptex, level, ptex, level, 0, 0, 0, &box, true,1324/* Sync before the first copy and after the last copy */1325(level == 0 ? SI_OP_SYNC_BEFORE : 0) |1326(level == tex->surface.num_meta_levels - 1 ? SI_OP_SYNC_AFTER : 0));1327}13281329/* Now clear DCC metadata to uncompressed.1330*1331* This uses SI_COMPUTE_CLEAR_METHOD to avoid a failure when running this1332* deqp caselist on gfx10:1333* dEQP-GLES31.functional.image_load_store.2d.format_reinterpret.rgba32f_rgba32ui1334* dEQP-GLES31.functional.image_load_store.2d.format_reinterpret.rgba32f_rgba32i1335*/1336uint32_t clear_value = DCC_UNCOMPRESSED;1337si_clear_buffer(sctx, ptex, tex->surface.meta_offset,1338tex->surface.meta_size, &clear_value, 4, SI_OP_SYNC_AFTER,1339SI_COHERENCY_CB_META, SI_COMPUTE_CLEAR_METHOD);1340si_mark_display_dcc_dirty(sctx, tex);13411342/* Clearing DCC metadata requires flushing L2 and invalidating L2 metadata to make1343* the metadata visible to L2 caches. This is because clear_buffer uses plain stores1344* that can go to different L2 channels than where L2 metadata caches expect them.1345* This is not done for fast clears because plain stores are visible to CB/DB. Only1346* L2 metadata caches have the problem.1347*/1348sctx->flags |= SI_CONTEXT_WB_L2 | SI_CONTEXT_INV_L2_METADATA;1349}1350}13511352void si_init_blit_functions(struct si_context *sctx)1353{1354sctx->b.resource_copy_region = si_resource_copy_region;13551356if (sctx->has_graphics) {1357sctx->b.blit = si_blit;1358sctx->b.flush_resource = si_flush_resource;1359sctx->b.generate_mipmap = si_generate_mipmap;1360}1361}136213631364