Path: blob/21.2-virgl/src/gallium/drivers/iris/iris_resolve.c
4565 views
/*1* Copyright © 2017 Intel Corporation2*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, sublicense,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 shall be included11* in all copies or substantial portions of the Software.12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS14* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER17* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING18* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER19* DEALINGS IN THE SOFTWARE.20*/2122/**23* @file iris_resolve.c24*25* This file handles resolve tracking for main and auxiliary surfaces.26*27* It also handles our cache tracking. We have sets for the render cache,28* depth cache, and so on. If a BO is in a cache's set, then it may have29* data in that cache. The helpers take care of emitting flushes for30* render-to-texture, format reinterpretation issues, and other situations.31*/3233#include "util/hash_table.h"34#include "util/set.h"35#include "iris_context.h"36#include "compiler/nir/nir.h"3738/**39* Disable auxiliary buffers if a renderbuffer is also bound as a texture40* or shader image. This causes a self-dependency, where both rendering41* and sampling may concurrently read or write the CCS buffer, causing42* incorrect pixels.43*/44static bool45disable_rb_aux_buffer(struct iris_context *ice,46bool *draw_aux_buffer_disabled,47struct iris_resource *tex_res,48unsigned min_level, unsigned num_levels,49const char *usage)50{51struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;52bool found = false;5354/* We only need to worry about color compression and fast clears. */55if (tex_res->aux.usage != ISL_AUX_USAGE_CCS_D &&56tex_res->aux.usage != ISL_AUX_USAGE_CCS_E &&57tex_res->aux.usage != ISL_AUX_USAGE_GFX12_CCS_E)58return false;5960for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {61struct iris_surface *surf = (void *) cso_fb->cbufs[i];62if (!surf)63continue;6465struct iris_resource *rb_res = (void *) surf->base.texture;6667if (rb_res->bo == tex_res->bo &&68surf->base.u.tex.level >= min_level &&69surf->base.u.tex.level < min_level + num_levels) {70found = draw_aux_buffer_disabled[i] = true;71}72}7374if (found) {75perf_debug(&ice->dbg,76"Disabling CCS because a renderbuffer is also bound %s.\n",77usage);78}7980return found;81}8283static void84resolve_sampler_views(struct iris_context *ice,85struct iris_batch *batch,86struct iris_shader_state *shs,87const struct shader_info *info,88bool *draw_aux_buffer_disabled,89bool consider_framebuffer)90{91uint32_t views = info ? (shs->bound_sampler_views & info->textures_used[0]) : 0;9293while (views) {94const int i = u_bit_scan(&views);95struct iris_sampler_view *isv = shs->textures[i];9697if (isv->res->base.b.target != PIPE_BUFFER) {98if (consider_framebuffer) {99disable_rb_aux_buffer(ice, draw_aux_buffer_disabled, isv->res,100isv->view.base_level, isv->view.levels,101"for sampling");102}103104iris_resource_prepare_texture(ice, isv->res, isv->view.format,105isv->view.base_level, isv->view.levels,106isv->view.base_array_layer,107isv->view.array_len);108}109110iris_emit_buffer_barrier_for(batch, isv->res->bo,111IRIS_DOMAIN_OTHER_READ);112}113}114115static void116resolve_image_views(struct iris_context *ice,117struct iris_batch *batch,118struct iris_shader_state *shs,119const struct shader_info *info,120bool *draw_aux_buffer_disabled,121bool consider_framebuffer)122{123uint32_t views = info ? (shs->bound_image_views & info->images_used) : 0;124125while (views) {126const int i = u_bit_scan(&views);127struct pipe_image_view *pview = &shs->image[i].base;128struct iris_resource *res = (void *) pview->resource;129130if (res->base.b.target != PIPE_BUFFER) {131if (consider_framebuffer) {132disable_rb_aux_buffer(ice, draw_aux_buffer_disabled,133res, pview->u.tex.level, 1,134"as a shader image");135}136137unsigned num_layers =138pview->u.tex.last_layer - pview->u.tex.first_layer + 1;139140enum isl_aux_usage aux_usage =141iris_image_view_aux_usage(ice, pview, info);142143iris_resource_prepare_access(ice, res,144pview->u.tex.level, 1,145pview->u.tex.first_layer, num_layers,146aux_usage, false);147}148149iris_emit_buffer_barrier_for(batch, res->bo, IRIS_DOMAIN_OTHER_READ);150}151}152153154/**155* \brief Resolve buffers before drawing.156*157* Resolve the depth buffer's HiZ buffer, resolve the depth buffer of each158* enabled depth texture, and flush the render cache for any dirty textures.159*/160void161iris_predraw_resolve_inputs(struct iris_context *ice,162struct iris_batch *batch,163bool *draw_aux_buffer_disabled,164gl_shader_stage stage,165bool consider_framebuffer)166{167struct iris_shader_state *shs = &ice->state.shaders[stage];168const struct shader_info *info = iris_get_shader_info(ice, stage);169170uint64_t stage_dirty = (IRIS_STAGE_DIRTY_BINDINGS_VS << stage) |171(consider_framebuffer ? IRIS_STAGE_DIRTY_BINDINGS_FS : 0);172173if (ice->state.stage_dirty & stage_dirty) {174resolve_sampler_views(ice, batch, shs, info, draw_aux_buffer_disabled,175consider_framebuffer);176resolve_image_views(ice, batch, shs, info, draw_aux_buffer_disabled,177consider_framebuffer);178}179}180181void182iris_predraw_resolve_framebuffer(struct iris_context *ice,183struct iris_batch *batch,184bool *draw_aux_buffer_disabled)185{186struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;187struct iris_screen *screen = (void *) ice->ctx.screen;188struct intel_device_info *devinfo = &screen->devinfo;189struct iris_uncompiled_shader *ish =190ice->shaders.uncompiled[MESA_SHADER_FRAGMENT];191const nir_shader *nir = ish->nir;192193if (ice->state.dirty & IRIS_DIRTY_DEPTH_BUFFER) {194struct pipe_surface *zs_surf = cso_fb->zsbuf;195196if (zs_surf) {197struct iris_resource *z_res, *s_res;198iris_get_depth_stencil_resources(zs_surf->texture, &z_res, &s_res);199unsigned num_layers =200zs_surf->u.tex.last_layer - zs_surf->u.tex.first_layer + 1;201202if (z_res) {203iris_resource_prepare_render(ice, z_res, zs_surf->u.tex.level,204zs_surf->u.tex.first_layer,205num_layers, ice->state.hiz_usage);206iris_emit_buffer_barrier_for(batch, z_res->bo,207IRIS_DOMAIN_DEPTH_WRITE);208}209210if (s_res) {211iris_emit_buffer_barrier_for(batch, s_res->bo,212IRIS_DOMAIN_DEPTH_WRITE);213}214}215}216217if (devinfo->ver == 8 && nir->info.outputs_read != 0) {218for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {219if (cso_fb->cbufs[i]) {220struct iris_surface *surf = (void *) cso_fb->cbufs[i];221struct iris_resource *res = (void *) cso_fb->cbufs[i]->texture;222223iris_resource_prepare_texture(ice, res, surf->view.format,224surf->view.base_level, 1,225surf->view.base_array_layer,226surf->view.array_len);227}228}229}230231if (ice->state.stage_dirty & IRIS_STAGE_DIRTY_BINDINGS_FS) {232for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {233struct iris_surface *surf = (void *) cso_fb->cbufs[i];234if (!surf)235continue;236237struct iris_resource *res = (void *) surf->base.texture;238239enum isl_aux_usage aux_usage =240iris_resource_render_aux_usage(ice, res, surf->view.base_level,241surf->view.format,242draw_aux_buffer_disabled[i]);243244if (ice->state.draw_aux_usage[i] != aux_usage) {245ice->state.draw_aux_usage[i] = aux_usage;246/* XXX: Need to track which bindings to make dirty */247ice->state.dirty |= IRIS_DIRTY_RENDER_BUFFER;248ice->state.stage_dirty |= IRIS_ALL_STAGE_DIRTY_BINDINGS;249}250251iris_resource_prepare_render(ice, res, surf->view.base_level,252surf->view.base_array_layer,253surf->view.array_len,254aux_usage);255256iris_cache_flush_for_render(batch, res->bo, aux_usage);257}258}259}260261/**262* \brief Call this after drawing to mark which buffers need resolving263*264* If the depth buffer was written to and if it has an accompanying HiZ265* buffer, then mark that it needs a depth resolve.266*267* If the color buffer is a multisample window system buffer, then268* mark that it needs a downsample.269*270* Also mark any render targets which will be textured as needing a render271* cache flush.272*/273void274iris_postdraw_update_resolve_tracking(struct iris_context *ice,275struct iris_batch *batch)276{277struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;278279// XXX: front buffer drawing?280281bool may_have_resolved_depth =282ice->state.dirty & (IRIS_DIRTY_DEPTH_BUFFER |283IRIS_DIRTY_WM_DEPTH_STENCIL);284285struct pipe_surface *zs_surf = cso_fb->zsbuf;286if (zs_surf) {287struct iris_resource *z_res, *s_res;288iris_get_depth_stencil_resources(zs_surf->texture, &z_res, &s_res);289unsigned num_layers =290zs_surf->u.tex.last_layer - zs_surf->u.tex.first_layer + 1;291292if (z_res) {293if (may_have_resolved_depth && ice->state.depth_writes_enabled) {294iris_resource_finish_render(ice, z_res, zs_surf->u.tex.level,295zs_surf->u.tex.first_layer,296num_layers, ice->state.hiz_usage);297}298}299300if (s_res) {301if (may_have_resolved_depth && ice->state.stencil_writes_enabled) {302iris_resource_finish_write(ice, s_res, zs_surf->u.tex.level,303zs_surf->u.tex.first_layer, num_layers,304s_res->aux.usage);305}306}307}308309bool may_have_resolved_color =310ice->state.stage_dirty & IRIS_STAGE_DIRTY_BINDINGS_FS;311312for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {313struct iris_surface *surf = (void *) cso_fb->cbufs[i];314if (!surf)315continue;316317struct iris_resource *res = (void *) surf->base.texture;318enum isl_aux_usage aux_usage = ice->state.draw_aux_usage[i];319320if (may_have_resolved_color) {321union pipe_surface_desc *desc = &surf->base.u;322unsigned num_layers =323desc->tex.last_layer - desc->tex.first_layer + 1;324iris_resource_finish_render(ice, res, desc->tex.level,325desc->tex.first_layer, num_layers,326aux_usage);327}328}329}330331void332iris_cache_flush_for_render(struct iris_batch *batch,333struct iris_bo *bo,334enum isl_aux_usage aux_usage)335{336iris_emit_buffer_barrier_for(batch, bo, IRIS_DOMAIN_RENDER_WRITE);337338/* Check to see if this bo has been used by a previous rendering operation339* but with a different aux usage. If it has, flush the render cache so we340* ensure that it's only in there with one aux usage at a time.341*342* Even though it's not obvious, this can easily happen in practice.343* Suppose a client is blending on a surface with sRGB encode enabled on344* gfx9. This implies that you get AUX_USAGE_CCS_D at best. If the client345* then disables sRGB decode and continues blending we will flip on346* AUX_USAGE_CCS_E without doing any sort of resolve in-between (this is347* perfectly valid since CCS_E is a subset of CCS_D). However, this means348* that we have fragments in-flight which are rendering with UNORM+CCS_E349* and other fragments in-flight with SRGB+CCS_D on the same surface at the350* same time and the pixel scoreboard and color blender are trying to sort351* it all out. This ends badly (i.e. GPU hangs).352*353* There are comments in various docs which indicate that the render cache354* isn't 100% resilient to format changes. However, to date, we have never355* observed GPU hangs or even corruption to be associated with switching the356* format, only the aux usage. So we let that slide for now.357*/358void *v_aux_usage = (void *) (uintptr_t) aux_usage;359struct hash_entry *entry =360_mesa_hash_table_search_pre_hashed(batch->cache.render, bo->hash, bo);361if (!entry) {362_mesa_hash_table_insert_pre_hashed(batch->cache.render, bo->hash, bo,363v_aux_usage);364} else if (entry->data != v_aux_usage) {365iris_emit_pipe_control_flush(batch,366"cache tracker: aux usage mismatch",367PIPE_CONTROL_RENDER_TARGET_FLUSH |368PIPE_CONTROL_TILE_CACHE_FLUSH |369PIPE_CONTROL_CS_STALL);370entry->data = v_aux_usage;371}372}373374static void375iris_resolve_color(struct iris_context *ice,376struct iris_batch *batch,377struct iris_resource *res,378unsigned level, unsigned layer,379enum isl_aux_op resolve_op)380{381//DBG("%s to mt %p level %u layer %u\n", __FUNCTION__, mt, level, layer);382383struct blorp_surf surf;384iris_blorp_surf_for_resource(&batch->screen->isl_dev, &surf,385&res->base.b, res->aux.usage, level, true);386387iris_batch_maybe_flush(batch, 1500);388389/* Ivybridge PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":390*391* "Any transition from any value in {Clear, Render, Resolve} to a392* different value in {Clear, Render, Resolve} requires end of pipe393* synchronization."394*395* In other words, fast clear ops are not properly synchronized with396* other drawing. We need to use a PIPE_CONTROL to ensure that the397* contents of the previous draw hit the render target before we resolve398* and again afterwards to ensure that the resolve is complete before we399* do any more regular drawing.400*/401iris_emit_end_of_pipe_sync(batch, "color resolve: pre-flush",402PIPE_CONTROL_RENDER_TARGET_FLUSH);403404iris_batch_sync_region_start(batch);405struct blorp_batch blorp_batch;406blorp_batch_init(&ice->blorp, &blorp_batch, batch, 0);407blorp_ccs_resolve(&blorp_batch, &surf, level, layer, 1, res->surf.format,408resolve_op);409blorp_batch_finish(&blorp_batch);410411/* See comment above */412iris_emit_end_of_pipe_sync(batch, "color resolve: post-flush",413PIPE_CONTROL_RENDER_TARGET_FLUSH);414iris_batch_sync_region_end(batch);415}416417static void418iris_mcs_partial_resolve(struct iris_context *ice,419struct iris_batch *batch,420struct iris_resource *res,421uint32_t start_layer,422uint32_t num_layers)423{424//DBG("%s to mt %p layers %u-%u\n", __FUNCTION__, mt,425//start_layer, start_layer + num_layers - 1);426427assert(isl_aux_usage_has_mcs(res->aux.usage));428429iris_batch_maybe_flush(batch, 1500);430431struct blorp_surf surf;432iris_blorp_surf_for_resource(&batch->screen->isl_dev, &surf,433&res->base.b, res->aux.usage, 0, true);434iris_emit_buffer_barrier_for(batch, res->bo, IRIS_DOMAIN_RENDER_WRITE);435436struct blorp_batch blorp_batch;437iris_batch_sync_region_start(batch);438blorp_batch_init(&ice->blorp, &blorp_batch, batch, 0);439blorp_mcs_partial_resolve(&blorp_batch, &surf, res->surf.format,440start_layer, num_layers);441blorp_batch_finish(&blorp_batch);442iris_batch_sync_region_end(batch);443}444445bool446iris_sample_with_depth_aux(const struct intel_device_info *devinfo,447const struct iris_resource *res)448{449switch (res->aux.usage) {450case ISL_AUX_USAGE_HIZ:451if (devinfo->has_sample_with_hiz)452break;453return false;454case ISL_AUX_USAGE_HIZ_CCS:455return false;456case ISL_AUX_USAGE_HIZ_CCS_WT:457break;458default:459return false;460}461462for (unsigned level = 0; level < res->surf.levels; ++level) {463if (!iris_resource_level_has_hiz(res, level))464return false;465}466467/* From the BDW PRM (Volume 2d: Command Reference: Structures468* RENDER_SURFACE_STATE.AuxiliarySurfaceMode):469*470* "If this field is set to AUX_HIZ, Number of Multisamples must be471* MULTISAMPLECOUNT_1, and Surface Type cannot be SURFTYPE_3D.472*473* There is no such blurb for 1D textures, but there is sufficient evidence474* that this is broken on SKL+.475*/476return res->surf.samples == 1 && res->surf.dim == ISL_SURF_DIM_2D;477}478479/**480* Perform a HiZ or depth resolve operation.481*482* For an overview of HiZ ops, see the following sections of the Sandy Bridge483* PRM, Volume 1, Part 2:484* - 7.5.3.1 Depth Buffer Clear485* - 7.5.3.2 Depth Buffer Resolve486* - 7.5.3.3 Hierarchical Depth Buffer Resolve487*/488void489iris_hiz_exec(struct iris_context *ice,490struct iris_batch *batch,491struct iris_resource *res,492unsigned int level, unsigned int start_layer,493unsigned int num_layers, enum isl_aux_op op,494bool update_clear_depth)495{496assert(iris_resource_level_has_hiz(res, level));497assert(op != ISL_AUX_OP_NONE);498UNUSED const char *name = NULL;499500iris_batch_maybe_flush(batch, 1500);501502switch (op) {503case ISL_AUX_OP_FULL_RESOLVE:504name = "depth resolve";505break;506case ISL_AUX_OP_AMBIGUATE:507name = "hiz ambiguate";508break;509case ISL_AUX_OP_FAST_CLEAR:510name = "depth clear";511break;512case ISL_AUX_OP_PARTIAL_RESOLVE:513case ISL_AUX_OP_NONE:514unreachable("Invalid HiZ op");515}516517//DBG("%s %s to mt %p level %d layers %d-%d\n",518//__func__, name, mt, level, start_layer, start_layer + num_layers - 1);519520/* The following stalls and flushes are only documented to be required521* for HiZ clear operations. However, they also seem to be required for522* resolve operations.523*524* From the Ivybridge PRM, volume 2, "Depth Buffer Clear":525*526* "If other rendering operations have preceded this clear, a527* PIPE_CONTROL with depth cache flush enabled, Depth Stall bit528* enabled must be issued before the rectangle primitive used for529* the depth buffer clear operation."530*531* Same applies for Gfx8 and Gfx9.532*/533iris_emit_pipe_control_flush(batch,534"hiz op: pre-flush",535PIPE_CONTROL_DEPTH_CACHE_FLUSH |536PIPE_CONTROL_DEPTH_STALL |537PIPE_CONTROL_CS_STALL);538539iris_batch_sync_region_start(batch);540541struct blorp_surf surf;542iris_blorp_surf_for_resource(&batch->screen->isl_dev, &surf,543&res->base.b, res->aux.usage, level, true);544545struct blorp_batch blorp_batch;546enum blorp_batch_flags flags = 0;547flags |= update_clear_depth ? 0 : BLORP_BATCH_NO_UPDATE_CLEAR_COLOR;548blorp_batch_init(&ice->blorp, &blorp_batch, batch, flags);549blorp_hiz_op(&blorp_batch, &surf, level, start_layer, num_layers, op);550blorp_batch_finish(&blorp_batch);551552/* The following stalls and flushes are only documented to be required553* for HiZ clear operations. However, they also seem to be required for554* resolve operations.555*556* From the Broadwell PRM, volume 7, "Depth Buffer Clear":557*558* "Depth buffer clear pass using any of the methods (WM_STATE,559* 3DSTATE_WM or 3DSTATE_WM_HZ_OP) must be followed by a560* PIPE_CONTROL command with DEPTH_STALL bit and Depth FLUSH bits561* "set" before starting to render. DepthStall and DepthFlush are562* not needed between consecutive depth clear passes nor is it563* required if the depth clear pass was done with564* 'full_surf_clear' bit set in the 3DSTATE_WM_HZ_OP."565*566* TODO: Such as the spec says, this could be conditional.567*/568iris_emit_pipe_control_flush(batch,569"hiz op: post flush",570PIPE_CONTROL_DEPTH_CACHE_FLUSH |571PIPE_CONTROL_DEPTH_STALL);572573iris_batch_sync_region_end(batch);574}575576/**577* Does the resource's slice have hiz enabled?578*/579bool580iris_resource_level_has_hiz(const struct iris_resource *res, uint32_t level)581{582iris_resource_check_level_layer(res, level, 0);583584if (!isl_aux_usage_has_hiz(res->aux.usage))585return false;586587/* Disable HiZ for LOD > 0 unless the width/height are 8x4 aligned.588* For LOD == 0, we can grow the dimensions to make it work.589*/590if (level > 0) {591if (u_minify(res->base.b.width0, level) & 7)592return false;593594if (u_minify(res->base.b.height0, level) & 3)595return false;596}597598return true;599}600601/** \brief Assert that the level and layer are valid for the resource. */602void603iris_resource_check_level_layer(UNUSED const struct iris_resource *res,604UNUSED uint32_t level, UNUSED uint32_t layer)605{606assert(level < res->surf.levels);607assert(layer < util_num_layers(&res->base.b, level));608}609610static inline uint32_t611miptree_level_range_length(const struct iris_resource *res,612uint32_t start_level, uint32_t num_levels)613{614assert(start_level < res->surf.levels);615616if (num_levels == INTEL_REMAINING_LAYERS)617num_levels = res->surf.levels;618619/* Check for overflow */620assert(start_level + num_levels >= start_level);621assert(start_level + num_levels <= res->surf.levels);622623return num_levels;624}625626static inline uint32_t627miptree_layer_range_length(const struct iris_resource *res, uint32_t level,628uint32_t start_layer, uint32_t num_layers)629{630assert(level <= res->base.b.last_level);631632const uint32_t total_num_layers = iris_get_num_logical_layers(res, level);633assert(start_layer < total_num_layers);634if (num_layers == INTEL_REMAINING_LAYERS)635num_layers = total_num_layers - start_layer;636/* Check for overflow */637assert(start_layer + num_layers >= start_layer);638assert(start_layer + num_layers <= total_num_layers);639640return num_layers;641}642643bool644iris_has_invalid_primary(const struct iris_resource *res,645unsigned start_level, unsigned num_levels,646unsigned start_layer, unsigned num_layers)647{648if (!res->aux.bo)649return false;650651/* Clamp the level range to fit the resource */652num_levels = miptree_level_range_length(res, start_level, num_levels);653654for (uint32_t l = 0; l < num_levels; l++) {655const uint32_t level = start_level + l;656const uint32_t level_layers =657miptree_layer_range_length(res, level, start_layer, num_layers);658for (unsigned a = 0; a < level_layers; a++) {659enum isl_aux_state aux_state =660iris_resource_get_aux_state(res, level, start_layer + a);661if (!isl_aux_state_has_valid_primary(aux_state))662return true;663}664}665666return false;667}668669void670iris_resource_prepare_access(struct iris_context *ice,671struct iris_resource *res,672uint32_t start_level, uint32_t num_levels,673uint32_t start_layer, uint32_t num_layers,674enum isl_aux_usage aux_usage,675bool fast_clear_supported)676{677if (!res->aux.bo)678return;679680/* We can't do resolves on the compute engine, so awkwardly, we have to681* do them on the render batch...682*/683struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];684685const uint32_t clamped_levels =686miptree_level_range_length(res, start_level, num_levels);687for (uint32_t l = 0; l < clamped_levels; l++) {688const uint32_t level = start_level + l;689const uint32_t level_layers =690miptree_layer_range_length(res, level, start_layer, num_layers);691for (uint32_t a = 0; a < level_layers; a++) {692const uint32_t layer = start_layer + a;693const enum isl_aux_state aux_state =694iris_resource_get_aux_state(res, level, layer);695const enum isl_aux_op aux_op =696isl_aux_prepare_access(aux_state, aux_usage, fast_clear_supported);697698/* Prepare the aux buffer for a conditional or unconditional access.699* A conditional access is handled by assuming that the access will700* not evaluate to a no-op. If the access does in fact occur, the aux701* will be in the required state. If it does not, no data is lost702* because the aux_op performed is lossless.703*/704if (aux_op == ISL_AUX_OP_NONE) {705/* Nothing to do here. */706} else if (isl_aux_usage_has_mcs(res->aux.usage)) {707assert(aux_op == ISL_AUX_OP_PARTIAL_RESOLVE);708iris_mcs_partial_resolve(ice, batch, res, layer, 1);709} else if (isl_aux_usage_has_hiz(res->aux.usage)) {710iris_hiz_exec(ice, batch, res, level, layer, 1, aux_op, false);711} else if (res->aux.usage == ISL_AUX_USAGE_STC_CCS) {712unreachable("iris doesn't resolve STC_CCS resources");713} else {714assert(isl_aux_usage_has_ccs(res->aux.usage));715iris_resolve_color(ice, batch, res, level, layer, aux_op);716}717718const enum isl_aux_state new_state =719isl_aux_state_transition_aux_op(aux_state, res->aux.usage, aux_op);720iris_resource_set_aux_state(ice, res, level, layer, 1, new_state);721}722}723}724725void726iris_resource_finish_write(struct iris_context *ice,727struct iris_resource *res, uint32_t level,728uint32_t start_layer, uint32_t num_layers,729enum isl_aux_usage aux_usage)730{731if (!res->aux.bo)732return;733734const uint32_t level_layers =735miptree_layer_range_length(res, level, start_layer, num_layers);736737for (uint32_t a = 0; a < level_layers; a++) {738const uint32_t layer = start_layer + a;739const enum isl_aux_state aux_state =740iris_resource_get_aux_state(res, level, layer);741742/* Transition the aux state for a conditional or unconditional write. A743* conditional write is handled by assuming that the write applies to744* only part of the render target. This prevents the new state from745* losing the types of compression that might exist in the current state746* (e.g. CLEAR). If the write evaluates to a no-op, the state will still747* be able to communicate when resolves are necessary (but it may748* falsely communicate this as well).749*/750const enum isl_aux_state new_aux_state =751isl_aux_state_transition_write(aux_state, aux_usage, false);752753iris_resource_set_aux_state(ice, res, level, layer, 1, new_aux_state);754}755}756757enum isl_aux_state758iris_resource_get_aux_state(const struct iris_resource *res,759uint32_t level, uint32_t layer)760{761iris_resource_check_level_layer(res, level, layer);762763if (res->surf.usage & ISL_SURF_USAGE_DEPTH_BIT) {764assert(isl_aux_usage_has_hiz(res->aux.usage));765} else {766assert(res->surf.samples == 1 ||767res->surf.msaa_layout == ISL_MSAA_LAYOUT_ARRAY);768}769770return res->aux.state[level][layer];771}772773void774iris_resource_set_aux_state(struct iris_context *ice,775struct iris_resource *res, uint32_t level,776uint32_t start_layer, uint32_t num_layers,777enum isl_aux_state aux_state)778{779num_layers = miptree_layer_range_length(res, level, start_layer, num_layers);780781if (res->surf.usage & ISL_SURF_USAGE_DEPTH_BIT) {782assert(iris_resource_level_has_hiz(res, level) ||783!isl_aux_state_has_valid_aux(aux_state));784} else {785assert(res->surf.samples == 1 ||786res->surf.msaa_layout == ISL_MSAA_LAYOUT_ARRAY);787}788789for (unsigned a = 0; a < num_layers; a++) {790if (res->aux.state[level][start_layer + a] != aux_state) {791res->aux.state[level][start_layer + a] = aux_state;792/* XXX: Need to track which bindings to make dirty */793ice->state.dirty |= IRIS_DIRTY_RENDER_BUFFER |794IRIS_DIRTY_RENDER_RESOLVES_AND_FLUSHES |795IRIS_DIRTY_COMPUTE_RESOLVES_AND_FLUSHES;796ice->state.stage_dirty |= IRIS_ALL_STAGE_DIRTY_BINDINGS;797}798}799800if (res->mod_info && !res->mod_info->supports_clear_color) {801assert(res->mod_info->aux_usage != ISL_AUX_USAGE_NONE);802if (aux_state == ISL_AUX_STATE_CLEAR ||803aux_state == ISL_AUX_STATE_COMPRESSED_CLEAR ||804aux_state == ISL_AUX_STATE_PARTIAL_CLEAR) {805iris_mark_dirty_dmabuf(ice, &res->base.b);806}807}808}809810enum isl_aux_usage811iris_resource_texture_aux_usage(struct iris_context *ice,812const struct iris_resource *res,813enum isl_format view_format)814{815struct iris_screen *screen = (void *) ice->ctx.screen;816struct intel_device_info *devinfo = &screen->devinfo;817818switch (res->aux.usage) {819case ISL_AUX_USAGE_HIZ:820case ISL_AUX_USAGE_HIZ_CCS:821case ISL_AUX_USAGE_HIZ_CCS_WT:822assert(res->surf.format == view_format);823return util_last_bit(res->aux.sampler_usages) - 1;824825case ISL_AUX_USAGE_MCS:826case ISL_AUX_USAGE_MCS_CCS:827case ISL_AUX_USAGE_STC_CCS:828case ISL_AUX_USAGE_MC:829return res->aux.usage;830831case ISL_AUX_USAGE_CCS_E:832case ISL_AUX_USAGE_GFX12_CCS_E:833/* If we don't have any unresolved color, report an aux usage of834* ISL_AUX_USAGE_NONE. This way, texturing won't even look at the835* aux surface and we can save some bandwidth.836*/837if (!iris_has_invalid_primary(res, 0, INTEL_REMAINING_LEVELS,8380, INTEL_REMAINING_LAYERS))839return ISL_AUX_USAGE_NONE;840841/* On Gfx9 color buffers may be compressed by the hardware (lossless842* compression). There are, however, format restrictions and care needs843* to be taken that the sampler engine is capable for re-interpreting a844* buffer with format different the buffer was originally written with.845*846* For example, SRGB formats are not compressible and the sampler engine847* isn't capable of treating RGBA_UNORM as SRGB_ALPHA. In such a case848* the underlying color buffer needs to be resolved so that the sampling849* surface can be sampled as non-compressed (i.e., without the auxiliary850* MCS buffer being set).851*/852if (isl_formats_are_ccs_e_compatible(devinfo, res->surf.format,853view_format))854return res->aux.usage;855break;856857default:858break;859}860861return ISL_AUX_USAGE_NONE;862}863864enum isl_aux_usage865iris_image_view_aux_usage(struct iris_context *ice,866const struct pipe_image_view *pview,867const struct shader_info *info)868{869if (!info)870return ISL_AUX_USAGE_NONE;871872struct iris_resource *res = (void *) pview->resource;873874enum isl_format view_format = iris_image_view_get_format(ice, pview);875enum isl_aux_usage aux_usage =876iris_resource_texture_aux_usage(ice, res, view_format);877878bool uses_atomic_load_store =879ice->shaders.uncompiled[info->stage]->uses_atomic_load_store;880881if (aux_usage == ISL_AUX_USAGE_GFX12_CCS_E && !uses_atomic_load_store)882return ISL_AUX_USAGE_GFX12_CCS_E;883884return ISL_AUX_USAGE_NONE;885}886887bool888iris_can_sample_mcs_with_clear(const struct intel_device_info *devinfo,889const struct iris_resource *res)890{891assert(isl_aux_usage_has_mcs(res->aux.usage));892893/* On TGL, the sampler has an issue with some 8 and 16bpp MSAA fast clears.894* See HSD 1707282275, wa_14013111325. Due to the use of895* format-reinterpretation, a simplified workaround is implemented.896*/897if (devinfo->ver >= 12 &&898isl_format_get_layout(res->surf.format)->bpb <= 16) {899return false;900}901902return true;903}904905static bool906isl_formats_are_fast_clear_compatible(enum isl_format a, enum isl_format b)907{908/* On gfx8 and earlier, the hardware was only capable of handling 0/1 clear909* values so sRGB curve application was a no-op for all fast-clearable910* formats.911*912* On gfx9+, the hardware supports arbitrary clear values. For sRGB clear913* values, the hardware interprets the floats, not as what would be914* returned from the sampler (or written by the shader), but as being915* between format conversion and sRGB curve application. This means that916* we can switch between sRGB and UNORM without having to whack the clear917* color.918*/919return isl_format_srgb_to_linear(a) == isl_format_srgb_to_linear(b);920}921922void923iris_resource_prepare_texture(struct iris_context *ice,924struct iris_resource *res,925enum isl_format view_format,926uint32_t start_level, uint32_t num_levels,927uint32_t start_layer, uint32_t num_layers)928{929const struct iris_screen *screen = (void *) ice->ctx.screen;930const struct intel_device_info *devinfo = &screen->devinfo;931932enum isl_aux_usage aux_usage =933iris_resource_texture_aux_usage(ice, res, view_format);934935bool clear_supported = isl_aux_usage_has_fast_clears(aux_usage);936937/* Clear color is specified as ints or floats and the conversion is done by938* the sampler. If we have a texture view, we would have to perform the939* clear color conversion manually. Just disable clear color.940*/941if (!isl_formats_are_fast_clear_compatible(res->surf.format, view_format))942clear_supported = false;943944if (isl_aux_usage_has_mcs(aux_usage) &&945!iris_can_sample_mcs_with_clear(devinfo, res)) {946clear_supported = false;947}948949iris_resource_prepare_access(ice, res, start_level, num_levels,950start_layer, num_layers,951aux_usage, clear_supported);952}953954/* Whether or not rendering a color value with either format results in the955* same pixel. This can return false negatives.956*/957bool958iris_render_formats_color_compatible(enum isl_format a, enum isl_format b,959union isl_color_value color)960{961if (a == b)962return true;963964/* A difference in color space doesn't matter for 0/1 values. */965if (isl_format_srgb_to_linear(a) == isl_format_srgb_to_linear(b) &&966isl_color_value_is_zero_one(color, a)) {967return true;968}969970return false;971}972973enum isl_aux_usage974iris_resource_render_aux_usage(struct iris_context *ice,975struct iris_resource *res, uint32_t level,976enum isl_format render_format,977bool draw_aux_disabled)978{979struct iris_screen *screen = (void *) ice->ctx.screen;980struct intel_device_info *devinfo = &screen->devinfo;981982if (draw_aux_disabled)983return ISL_AUX_USAGE_NONE;984985switch (res->aux.usage) {986case ISL_AUX_USAGE_HIZ:987case ISL_AUX_USAGE_HIZ_CCS:988case ISL_AUX_USAGE_HIZ_CCS_WT:989assert(render_format == res->surf.format);990return iris_resource_level_has_hiz(res, level) ?991res->aux.usage : ISL_AUX_USAGE_NONE;992993case ISL_AUX_USAGE_STC_CCS:994assert(render_format == res->surf.format);995return res->aux.usage;996997case ISL_AUX_USAGE_MCS:998case ISL_AUX_USAGE_MCS_CCS:999return res->aux.usage;10001001case ISL_AUX_USAGE_CCS_D:1002case ISL_AUX_USAGE_CCS_E:1003case ISL_AUX_USAGE_GFX12_CCS_E:1004/* Disable CCS for some cases of texture-view rendering. On gfx12, HW1005* may convert some subregions of shader output to fast-cleared blocks1006* if CCS is enabled and the shader output matches the clear color.1007* Existing fast-cleared blocks are correctly interpreted by the clear1008* color and the resource format (see can_fast_clear_color). To avoid1009* gaining new fast-cleared blocks that can't be interpreted by the1010* resource format (and to avoid misinterpreting existing ones), shut1011* off CCS when the interpretation of the clear color differs between1012* the render_format and the resource format.1013*/1014if (!iris_render_formats_color_compatible(render_format,1015res->surf.format,1016res->aux.clear_color)) {1017return ISL_AUX_USAGE_NONE;1018}10191020if (res->aux.usage == ISL_AUX_USAGE_CCS_D)1021return ISL_AUX_USAGE_CCS_D;10221023if (isl_formats_are_ccs_e_compatible(devinfo, res->surf.format,1024render_format)) {1025return res->aux.usage;1026}1027FALLTHROUGH;10281029default:1030return ISL_AUX_USAGE_NONE;1031}1032}10331034void1035iris_resource_prepare_render(struct iris_context *ice,1036struct iris_resource *res, uint32_t level,1037uint32_t start_layer, uint32_t layer_count,1038enum isl_aux_usage aux_usage)1039{1040iris_resource_prepare_access(ice, res, level, 1, start_layer,1041layer_count, aux_usage,1042isl_aux_usage_has_fast_clears(aux_usage));1043}10441045void1046iris_resource_finish_render(struct iris_context *ice,1047struct iris_resource *res, uint32_t level,1048uint32_t start_layer, uint32_t layer_count,1049enum isl_aux_usage aux_usage)1050{1051iris_resource_finish_write(ice, res, level, start_layer, layer_count,1052aux_usage);1053}105410551056