Path: blob/21.2-virgl/src/gallium/drivers/crocus/crocus_resolve.c
4570 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 crocus_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 "crocus_context.h"36#include "compiler/nir/nir.h"3738#define FILE_DEBUG_FLAG DEBUG_BLORP3940static void41crocus_update_stencil_shadow(struct crocus_context *ice,42struct crocus_resource *res);43/**44* Disable auxiliary buffers if a renderbuffer is also bound as a texture45* or shader image. This causes a self-dependency, where both rendering46* and sampling may concurrently read or write the CCS buffer, causing47* incorrect pixels.48*/49static bool50disable_rb_aux_buffer(struct crocus_context *ice,51bool *draw_aux_buffer_disabled,52struct crocus_resource *tex_res,53unsigned min_level, unsigned num_levels,54const char *usage)55{56struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;57bool found = false;5859/* We only need to worry about fast clears. */60if (tex_res->aux.usage != ISL_AUX_USAGE_CCS_D)61return false;6263for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {64struct crocus_surface *surf = (void *) cso_fb->cbufs[i];65if (!surf)66continue;6768struct crocus_resource *rb_res = (void *) surf->base.texture;6970if (rb_res->bo == tex_res->bo &&71surf->base.u.tex.level >= min_level &&72surf->base.u.tex.level < min_level + num_levels) {73found = draw_aux_buffer_disabled[i] = true;74}75}7677if (found) {78perf_debug(&ice->dbg,79"Disabling CCS because a renderbuffer is also bound %s.\n",80usage);81}8283return found;84}8586static void87resolve_sampler_views(struct crocus_context *ice,88struct crocus_batch *batch,89struct crocus_shader_state *shs,90const struct shader_info *info,91bool *draw_aux_buffer_disabled,92bool consider_framebuffer)93{94uint32_t views = info ? (shs->bound_sampler_views & info->textures_used[0]) : 0;9596while (views) {97const int i = u_bit_scan(&views);98struct crocus_sampler_view *isv = shs->textures[i];99100if (isv->res->base.b.target != PIPE_BUFFER) {101if (consider_framebuffer) {102disable_rb_aux_buffer(ice, draw_aux_buffer_disabled, isv->res,103isv->view.base_level, isv->view.levels,104"for sampling");105}106107crocus_resource_prepare_texture(ice, isv->res, isv->view.format,108isv->view.base_level, isv->view.levels,109isv->view.base_array_layer,110isv->view.array_len);111}112113crocus_cache_flush_for_read(batch, isv->res->bo);114115if (batch->screen->devinfo.ver == 7 &&116(isv->base.format == PIPE_FORMAT_X24S8_UINT ||117isv->base.format == PIPE_FORMAT_X32_S8X24_UINT ||118isv->base.format == PIPE_FORMAT_S8_UINT)) {119struct crocus_resource *zres, *sres;120crocus_get_depth_stencil_resources(&batch->screen->devinfo, isv->base.texture, &zres, &sres);121crocus_update_stencil_shadow(ice, sres);122crocus_cache_flush_for_read(batch, sres->shadow->bo);123}124}125}126127static void128resolve_image_views(struct crocus_context *ice,129struct crocus_batch *batch,130struct crocus_shader_state *shs,131bool *draw_aux_buffer_disabled,132bool consider_framebuffer)133{134/* TODO: Consider images used by program */135uint32_t views = shs->bound_image_views;136137while (views) {138const int i = u_bit_scan(&views);139struct pipe_image_view *pview = &shs->image[i].base;140struct crocus_resource *res = (void *) pview->resource;141142if (res->base.b.target != PIPE_BUFFER) {143if (consider_framebuffer) {144disable_rb_aux_buffer(ice, draw_aux_buffer_disabled,145res, pview->u.tex.level, 1,146"as a shader image");147}148149unsigned num_layers =150pview->u.tex.last_layer - pview->u.tex.first_layer + 1;151152/* The data port doesn't understand any compression */153crocus_resource_prepare_access(ice, res,154pview->u.tex.level, 1,155pview->u.tex.first_layer, num_layers,156ISL_AUX_USAGE_NONE, false);157}158159crocus_cache_flush_for_read(batch, res->bo);160}161}162163static void164crocus_update_align_res(struct crocus_batch *batch,165struct crocus_surface *surf,166bool copy_to_wa)167{168struct crocus_screen *screen = (struct crocus_screen *)batch->screen;169struct pipe_blit_info info = { 0 };170171info.src.resource = copy_to_wa ? surf->base.texture : surf->align_res;172info.src.level = copy_to_wa ? surf->base.u.tex.level : 0;173u_box_2d_zslice(0, 0, copy_to_wa ? surf->base.u.tex.first_layer : 0,174u_minify(surf->base.texture->width0, surf->base.u.tex.level),175u_minify(surf->base.texture->height0, surf->base.u.tex.level), &info.src.box);176info.src.format = surf->base.texture->format;177info.dst.resource = copy_to_wa ? surf->align_res : surf->base.texture;178info.dst.level = copy_to_wa ? 0 : surf->base.u.tex.level;179info.dst.box = info.src.box;180info.dst.box.z = copy_to_wa ? 0 : surf->base.u.tex.first_layer;181info.dst.format = surf->base.texture->format;182info.mask = util_format_is_depth_or_stencil(surf->base.texture->format) ? PIPE_MASK_ZS : PIPE_MASK_RGBA;183info.filter = 0;184if (!screen->vtbl.blit_blt(batch, &info)) {185assert(0);186}187}188189/**190* \brief Resolve buffers before drawing.191*192* Resolve the depth buffer's HiZ buffer, resolve the depth buffer of each193* enabled depth texture, and flush the render cache for any dirty textures.194*/195void196crocus_predraw_resolve_inputs(struct crocus_context *ice,197struct crocus_batch *batch,198bool *draw_aux_buffer_disabled,199gl_shader_stage stage,200bool consider_framebuffer)201{202struct crocus_shader_state *shs = &ice->state.shaders[stage];203const struct shader_info *info = crocus_get_shader_info(ice, stage);204205uint64_t stage_dirty = (CROCUS_STAGE_DIRTY_BINDINGS_VS << stage) |206(consider_framebuffer ? CROCUS_STAGE_DIRTY_BINDINGS_FS : 0);207208if (ice->state.stage_dirty & stage_dirty) {209resolve_sampler_views(ice, batch, shs, info, draw_aux_buffer_disabled,210consider_framebuffer);211resolve_image_views(ice, batch, shs, draw_aux_buffer_disabled,212consider_framebuffer);213}214}215216void217crocus_predraw_resolve_framebuffer(struct crocus_context *ice,218struct crocus_batch *batch,219bool *draw_aux_buffer_disabled)220{221struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;222struct crocus_screen *screen = (void *) ice->ctx.screen;223struct intel_device_info *devinfo = &screen->devinfo;224struct crocus_uncompiled_shader *ish =225ice->shaders.uncompiled[MESA_SHADER_FRAGMENT];226const nir_shader *nir = ish->nir;227228if (ice->state.dirty & CROCUS_DIRTY_DEPTH_BUFFER) {229struct pipe_surface *zs_surf = cso_fb->zsbuf;230231if (zs_surf) {232struct crocus_resource *z_res, *s_res;233crocus_get_depth_stencil_resources(devinfo, zs_surf->texture, &z_res, &s_res);234unsigned num_layers =235zs_surf->u.tex.last_layer - zs_surf->u.tex.first_layer + 1;236237if (z_res) {238crocus_resource_prepare_render(ice, z_res,239zs_surf->u.tex.level,240zs_surf->u.tex.first_layer,241num_layers, ice->state.hiz_usage);242crocus_cache_flush_for_depth(batch, z_res->bo);243244if (((struct crocus_surface *)zs_surf)->align_res) {245crocus_update_align_res(batch, (struct crocus_surface *)zs_surf, true);246}247}248249if (s_res) {250crocus_cache_flush_for_depth(batch, s_res->bo);251}252}253}254255if (nir->info.outputs_read != 0) {256for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {257if (cso_fb->cbufs[i]) {258struct crocus_surface *surf = (void *) cso_fb->cbufs[i];259struct crocus_resource *res = (void *) cso_fb->cbufs[i]->texture;260261crocus_resource_prepare_texture(ice, res, surf->view.format,262surf->view.base_level, 1,263surf->view.base_array_layer,264surf->view.array_len);265}266}267}268269if (ice->state.stage_dirty & CROCUS_STAGE_DIRTY_BINDINGS_FS) {270for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {271struct crocus_surface *surf = (void *) cso_fb->cbufs[i];272if (!surf)273continue;274275struct crocus_resource *res = (void *) surf->base.texture;276277if (surf->align_res)278crocus_update_align_res(batch, surf, true);279280enum isl_aux_usage aux_usage =281crocus_resource_render_aux_usage(ice, res, surf->view.base_level,282surf->view.format,283draw_aux_buffer_disabled[i]);284285if (ice->state.draw_aux_usage[i] != aux_usage) {286ice->state.draw_aux_usage[i] = aux_usage;287/* XXX: Need to track which bindings to make dirty */288ice->state.stage_dirty |= CROCUS_ALL_STAGE_DIRTY_BINDINGS;289}290291crocus_resource_prepare_render(ice, res, surf->view.base_level,292surf->view.base_array_layer,293surf->view.array_len,294aux_usage);295296crocus_cache_flush_for_render(batch, res->bo, surf->view.format,297aux_usage);298}299}300}301302/**303* \brief Call this after drawing to mark which buffers need resolving304*305* If the depth buffer was written to and if it has an accompanying HiZ306* buffer, then mark that it needs a depth resolve.307*308* If the color buffer is a multisample window system buffer, then309* mark that it needs a downsample.310*311* Also mark any render targets which will be textured as needing a render312* cache flush.313*/314void315crocus_postdraw_update_resolve_tracking(struct crocus_context *ice,316struct crocus_batch *batch)317{318struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;319struct crocus_screen *screen = (void *) ice->ctx.screen;320struct intel_device_info *devinfo = &screen->devinfo;321// XXX: front buffer drawing?322323bool may_have_resolved_depth =324ice->state.dirty & (CROCUS_DIRTY_DEPTH_BUFFER |325CROCUS_DIRTY_GEN6_WM_DEPTH_STENCIL);326327struct pipe_surface *zs_surf = cso_fb->zsbuf;328if (zs_surf) {329struct crocus_resource *z_res, *s_res;330crocus_get_depth_stencil_resources(devinfo, zs_surf->texture, &z_res, &s_res);331unsigned num_layers =332zs_surf->u.tex.last_layer - zs_surf->u.tex.first_layer + 1;333334if (z_res) {335if (may_have_resolved_depth && ice->state.depth_writes_enabled) {336crocus_resource_finish_render(ice, z_res, zs_surf->u.tex.level,337zs_surf->u.tex.first_layer, num_layers,338ice->state.hiz_usage);339}340341if (ice->state.depth_writes_enabled)342crocus_depth_cache_add_bo(batch, z_res->bo);343344if (((struct crocus_surface *)zs_surf)->align_res) {345crocus_update_align_res(batch, (struct crocus_surface *)zs_surf, false);346}347}348349if (s_res) {350if (may_have_resolved_depth && ice->state.stencil_writes_enabled) {351crocus_resource_finish_write(ice, s_res, zs_surf->u.tex.level,352zs_surf->u.tex.first_layer, num_layers,353s_res->aux.usage);354}355356if (ice->state.stencil_writes_enabled)357crocus_depth_cache_add_bo(batch, s_res->bo);358}359}360361bool may_have_resolved_color =362ice->state.stage_dirty & CROCUS_STAGE_DIRTY_BINDINGS_FS;363364for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {365struct crocus_surface *surf = (void *) cso_fb->cbufs[i];366if (!surf)367continue;368369if (surf->align_res)370crocus_update_align_res(batch, surf, false);371struct crocus_resource *res = (void *) surf->base.texture;372enum isl_aux_usage aux_usage = ice->state.draw_aux_usage[i];373374crocus_render_cache_add_bo(batch, res->bo, surf->view.format,375aux_usage);376377if (may_have_resolved_color) {378union pipe_surface_desc *desc = &surf->base.u;379unsigned num_layers =380desc->tex.last_layer - desc->tex.first_layer + 1;381crocus_resource_finish_render(ice, res, desc->tex.level,382desc->tex.first_layer, num_layers,383aux_usage);384}385}386}387388/**389* Clear the cache-tracking sets.390*/391void392crocus_cache_sets_clear(struct crocus_batch *batch)393{394hash_table_foreach(batch->cache.render, render_entry)395_mesa_hash_table_remove(batch->cache.render, render_entry);396397set_foreach(batch->cache.depth, depth_entry)398_mesa_set_remove(batch->cache.depth, depth_entry);399}400401/**402* Emits an appropriate flush for a BO if it has been rendered to within the403* same batchbuffer as a read that's about to be emitted.404*405* The GPU has separate, incoherent caches for the render cache and the406* sampler cache, along with other caches. Usually data in the different407* caches don't interact (e.g. we don't render to our driver-generated408* immediate constant data), but for render-to-texture in FBOs we definitely409* do. When a batchbuffer is flushed, the kernel will ensure that everything410* necessary is flushed before another use of that BO, but for reuse from411* different caches within a batchbuffer, it's all our responsibility.412*/413void414crocus_flush_depth_and_render_caches(struct crocus_batch *batch)415{416const struct intel_device_info *devinfo = &batch->screen->devinfo;417if (devinfo->ver >= 6) {418crocus_emit_pipe_control_flush(batch,419"cache tracker: render-to-texture",420PIPE_CONTROL_DEPTH_CACHE_FLUSH |421PIPE_CONTROL_RENDER_TARGET_FLUSH |422PIPE_CONTROL_CS_STALL);423424crocus_emit_pipe_control_flush(batch,425"cache tracker: render-to-texture",426PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |427PIPE_CONTROL_CONST_CACHE_INVALIDATE);428} else {429crocus_emit_mi_flush(batch);430}431432crocus_cache_sets_clear(batch);433}434435void436crocus_cache_flush_for_read(struct crocus_batch *batch,437struct crocus_bo *bo)438{439if (_mesa_hash_table_search_pre_hashed(batch->cache.render, bo->hash, bo) ||440_mesa_set_search_pre_hashed(batch->cache.depth, bo->hash, bo))441crocus_flush_depth_and_render_caches(batch);442}443444static void *445format_aux_tuple(enum isl_format format, enum isl_aux_usage aux_usage)446{447return (void *)(uintptr_t)((uint32_t)format << 8 | aux_usage);448}449450void451crocus_cache_flush_for_render(struct crocus_batch *batch,452struct crocus_bo *bo,453enum isl_format format,454enum isl_aux_usage aux_usage)455{456if (_mesa_set_search_pre_hashed(batch->cache.depth, bo->hash, bo))457crocus_flush_depth_and_render_caches(batch);458459/* Check to see if this bo has been used by a previous rendering operation460* but with a different format or aux usage. If it has, flush the render461* cache so we ensure that it's only in there with one format or aux usage462* at a time.463*464* Even though it's not obvious, this can easily happen in practice.465* Suppose a client is blending on a surface with sRGB encode enabled on466* gen9. This implies that you get AUX_USAGE_CCS_D at best. If the client467* then disables sRGB decode and continues blending we will flip on468* AUX_USAGE_CCS_E without doing any sort of resolve in-between (this is469* perfectly valid since CCS_E is a subset of CCS_D). However, this means470* that we have fragments in-flight which are rendering with UNORM+CCS_E471* and other fragments in-flight with SRGB+CCS_D on the same surface at the472* same time and the pixel scoreboard and color blender are trying to sort473* it all out. This ends badly (i.e. GPU hangs).474*475* To date, we have never observed GPU hangs or even corruption to be476* associated with switching the format, only the aux usage. However,477* there are comments in various docs which indicate that the render cache478* isn't 100% resilient to format changes. We may as well be conservative479* and flush on format changes too. We can always relax this later if we480* find it to be a performance problem.481*/482struct hash_entry *entry =483_mesa_hash_table_search_pre_hashed(batch->cache.render, bo->hash, bo);484if (entry && entry->data != format_aux_tuple(format, aux_usage))485crocus_flush_depth_and_render_caches(batch);486}487488void489crocus_render_cache_add_bo(struct crocus_batch *batch,490struct crocus_bo *bo,491enum isl_format format,492enum isl_aux_usage aux_usage)493{494#ifndef NDEBUG495struct hash_entry *entry =496_mesa_hash_table_search_pre_hashed(batch->cache.render, bo->hash, bo);497if (entry) {498/* Otherwise, someone didn't do a flush_for_render and that would be499* very bad indeed.500*/501assert(entry->data == format_aux_tuple(format, aux_usage));502}503#endif504505_mesa_hash_table_insert_pre_hashed(batch->cache.render, bo->hash, bo,506format_aux_tuple(format, aux_usage));507}508509void510crocus_cache_flush_for_depth(struct crocus_batch *batch,511struct crocus_bo *bo)512{513if (_mesa_hash_table_search_pre_hashed(batch->cache.render, bo->hash, bo))514crocus_flush_depth_and_render_caches(batch);515}516517void518crocus_depth_cache_add_bo(struct crocus_batch *batch, struct crocus_bo *bo)519{520_mesa_set_add_pre_hashed(batch->cache.depth, bo->hash, bo);521}522523static void524crocus_resolve_color(struct crocus_context *ice,525struct crocus_batch *batch,526struct crocus_resource *res,527unsigned level, unsigned layer,528enum isl_aux_op resolve_op)529{530struct crocus_screen *screen = batch->screen;531DBG("%s to res %p level %u layer %u\n", __func__, res, level, layer);532533struct blorp_surf surf;534crocus_blorp_surf_for_resource(&screen->vtbl, &batch->screen->isl_dev, &surf,535&res->base.b, res->aux.usage, level, true);536537crocus_batch_maybe_flush(batch, 1500);538539/* Ivybridge PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":540*541* "Any transition from any value in {Clear, Render, Resolve} to a542* different value in {Clear, Render, Resolve} requires end of pipe543* synchronization."544*545* In other words, fast clear ops are not properly synchronized with546* other drawing. We need to use a PIPE_CONTROL to ensure that the547* contents of the previous draw hit the render target before we resolve548* and again afterwards to ensure that the resolve is complete before we549* do any more regular drawing.550*/551crocus_emit_end_of_pipe_sync(batch, "color resolve: pre-flush",552PIPE_CONTROL_RENDER_TARGET_FLUSH);553554struct blorp_batch blorp_batch;555blorp_batch_init(&ice->blorp, &blorp_batch, batch, 0);556blorp_ccs_resolve(&blorp_batch, &surf, level, layer, 1,557isl_format_srgb_to_linear(res->surf.format),558resolve_op);559blorp_batch_finish(&blorp_batch);560561/* See comment above */562crocus_emit_end_of_pipe_sync(batch, "color resolve: post-flush",563PIPE_CONTROL_RENDER_TARGET_FLUSH);564}565566static void567crocus_mcs_partial_resolve(struct crocus_context *ice,568struct crocus_batch *batch,569struct crocus_resource *res,570uint32_t start_layer,571uint32_t num_layers)572{573struct crocus_screen *screen = batch->screen;574575DBG("%s to res %p layers %u-%u\n", __func__, res,576start_layer, start_layer + num_layers - 1);577578assert(isl_aux_usage_has_mcs(res->aux.usage));579580struct blorp_surf surf;581crocus_blorp_surf_for_resource(&screen->vtbl, &batch->screen->isl_dev, &surf,582&res->base.b, res->aux.usage, 0, true);583584struct blorp_batch blorp_batch;585blorp_batch_init(&ice->blorp, &blorp_batch, batch, 0);586blorp_mcs_partial_resolve(&blorp_batch, &surf,587isl_format_srgb_to_linear(res->surf.format),588start_layer, num_layers);589blorp_batch_finish(&blorp_batch);590}591592/**593* Perform a HiZ or depth resolve operation.594*595* For an overview of HiZ ops, see the following sections of the Sandy Bridge596* PRM, Volume 1, Part 2:597* - 7.5.3.1 Depth Buffer Clear598* - 7.5.3.2 Depth Buffer Resolve599* - 7.5.3.3 Hierarchical Depth Buffer Resolve600*/601void602crocus_hiz_exec(struct crocus_context *ice,603struct crocus_batch *batch,604struct crocus_resource *res,605unsigned int level, unsigned int start_layer,606unsigned int num_layers, enum isl_aux_op op,607bool update_clear_depth)608{609struct crocus_screen *screen = batch->screen;610const struct intel_device_info *devinfo = &batch->screen->devinfo;611assert(crocus_resource_level_has_hiz(res, level));612assert(op != ISL_AUX_OP_NONE);613UNUSED const char *name = NULL;614615switch (op) {616case ISL_AUX_OP_FULL_RESOLVE:617name = "depth resolve";618break;619case ISL_AUX_OP_AMBIGUATE:620name = "hiz ambiguate";621break;622case ISL_AUX_OP_FAST_CLEAR:623name = "depth clear";624break;625case ISL_AUX_OP_PARTIAL_RESOLVE:626case ISL_AUX_OP_NONE:627unreachable("Invalid HiZ op");628}629630DBG("%s %s to res %p level %d layers %d-%d\n",631__func__, name, res, level, start_layer, start_layer + num_layers - 1);632633/* The following stalls and flushes are only documented to be required634* for HiZ clear operations. However, they also seem to be required for635* resolve operations.636*637* From the Ivybridge PRM, volume 2, "Depth Buffer Clear":638*639* "If other rendering operations have preceded this clear, a640* PIPE_CONTROL with depth cache flush enabled, Depth Stall bit641* enabled must be issued before the rectangle primitive used for642* the depth buffer clear operation."643*644* Same applies for Gen8 and Gen9.645*646* In addition, from the Ivybridge PRM, volume 2, 1.10.4.1647* PIPE_CONTROL, Depth Cache Flush Enable:648*649* "This bit must not be set when Depth Stall Enable bit is set in650* this packet."651*652* This is confirmed to hold for real, Haswell gets immediate gpu hangs.653*654* Therefore issue two pipe control flushes, one for cache flush and655* another for depth stall.656*/657if (devinfo->ver == 6) {658/* From the Sandy Bridge PRM, volume 2 part 1, page 313:659*660* "If other rendering operations have preceded this clear, a661* PIPE_CONTROL with write cache flush enabled and Z-inhibit662* disabled must be issued before the rectangle primitive used for663* the depth buffer clear operation.664*/665crocus_emit_pipe_control_flush(batch,666"hiz op: pre-flushes (1)",667PIPE_CONTROL_RENDER_TARGET_FLUSH |668PIPE_CONTROL_DEPTH_CACHE_FLUSH |669PIPE_CONTROL_CS_STALL);670} else if (devinfo->ver >= 7) {671crocus_emit_pipe_control_flush(batch,672"hiz op: pre-flushes (1/2)",673PIPE_CONTROL_DEPTH_CACHE_FLUSH |674PIPE_CONTROL_CS_STALL);675crocus_emit_pipe_control_flush(batch, "hiz op: pre-flushes (2/2)",676PIPE_CONTROL_DEPTH_STALL);677}678679assert(isl_aux_usage_has_hiz(res->aux.usage) && res->aux.bo);680681crocus_batch_maybe_flush(batch, 1500);682683struct blorp_surf surf;684crocus_blorp_surf_for_resource(&screen->vtbl, &batch->screen->isl_dev, &surf,685&res->base.b, res->aux.usage, level, true);686687struct blorp_batch blorp_batch;688enum blorp_batch_flags flags = 0;689flags |= update_clear_depth ? 0 : BLORP_BATCH_NO_UPDATE_CLEAR_COLOR;690blorp_batch_init(&ice->blorp, &blorp_batch, batch, flags);691blorp_hiz_op(&blorp_batch, &surf, level, start_layer, num_layers, op);692blorp_batch_finish(&blorp_batch);693694/* The following stalls and flushes are only documented to be required695* for HiZ clear operations. However, they also seem to be required for696* resolve operations.697*698* From the Broadwell PRM, volume 7, "Depth Buffer Clear":699*700* "Depth buffer clear pass using any of the methods (WM_STATE,701* 3DSTATE_WM or 3DSTATE_WM_HZ_OP) must be followed by a702* PIPE_CONTROL command with DEPTH_STALL bit and Depth FLUSH bits703* "set" before starting to render. DepthStall and DepthFlush are704* not needed between consecutive depth clear passes nor is it705* required if the depth clear pass was done with706* 'full_surf_clear' bit set in the 3DSTATE_WM_HZ_OP."707*708* TODO: Such as the spec says, this could be conditional.709*/710if (devinfo->ver == 6) {711/* From the Sandy Bridge PRM, volume 2 part 1, page 314:712*713* "DevSNB, DevSNB-B{W/A}]: Depth buffer clear pass must be714* followed by a PIPE_CONTROL command with DEPTH_STALL bit set715* and Then followed by Depth FLUSH'716*/717crocus_emit_pipe_control_flush(batch,718"hiz op: post-flushes (1/2)",719PIPE_CONTROL_DEPTH_STALL);720721crocus_emit_pipe_control_flush(batch,722"hiz op: post-flushes (2/2)",723PIPE_CONTROL_DEPTH_CACHE_FLUSH |724PIPE_CONTROL_CS_STALL);725}726}727728/**729* Does the resource's slice have hiz enabled?730*/731bool732crocus_resource_level_has_hiz(const struct crocus_resource *res, uint32_t level)733{734crocus_resource_check_level_layer(res, level, 0);735return res->aux.has_hiz & 1 << level;736}737738static bool739crocus_resource_level_has_aux(const struct crocus_resource *res, uint32_t level)740{741if (isl_aux_usage_has_hiz(res->aux.usage))742return crocus_resource_level_has_hiz(res, level);743else744return level < res->aux.surf.levels;745}746747/** \brief Assert that the level and layer are valid for the resource. */748void749crocus_resource_check_level_layer(UNUSED const struct crocus_resource *res,750UNUSED uint32_t level, UNUSED uint32_t layer)751{752assert(level < res->surf.levels);753assert(layer < util_num_layers(&res->base.b, level));754}755756static inline uint32_t757miptree_level_range_length(const struct crocus_resource *res,758uint32_t start_level, uint32_t num_levels)759{760assert(start_level < res->surf.levels);761762if (num_levels == INTEL_REMAINING_LAYERS)763num_levels = res->surf.levels;764765/* Check for overflow */766assert(start_level + num_levels >= start_level);767assert(start_level + num_levels <= res->surf.levels);768769return num_levels;770}771772static inline uint32_t773miptree_layer_range_length(const struct crocus_resource *res, uint32_t level,774uint32_t start_layer, uint32_t num_layers)775{776assert(level <= res->base.b.last_level);777778const uint32_t total_num_layers = crocus_get_num_logical_layers(res, level);779assert(start_layer < total_num_layers);780if (num_layers == INTEL_REMAINING_LAYERS)781num_layers = total_num_layers - start_layer;782/* Check for overflow */783assert(start_layer + num_layers >= start_layer);784assert(start_layer + num_layers <= total_num_layers);785786return num_layers;787}788789bool790crocus_has_invalid_primary(const struct crocus_resource *res,791unsigned start_level, unsigned num_levels,792unsigned start_layer, unsigned num_layers)793{794if (!res->aux.bo)795return false;796797/* Clamp the level range to fit the resource */798num_levels = miptree_level_range_length(res, start_level, num_levels);799800for (uint32_t l = 0; l < num_levels; l++) {801const uint32_t level = start_level + l;802if (!crocus_resource_level_has_aux(res, level))803continue;804805const uint32_t level_layers =806miptree_layer_range_length(res, level, start_layer, num_layers);807for (unsigned a = 0; a < level_layers; a++) {808enum isl_aux_state aux_state =809crocus_resource_get_aux_state(res, level, start_layer + a);810if (!isl_aux_state_has_valid_primary(aux_state))811return true;812}813}814815return false;816}817818void819crocus_resource_prepare_access(struct crocus_context *ice,820struct crocus_resource *res,821uint32_t start_level, uint32_t num_levels,822uint32_t start_layer, uint32_t num_layers,823enum isl_aux_usage aux_usage,824bool fast_clear_supported)825{826if (!res->aux.bo)827return;828829/* We can't do resolves on the compute engine, so awkwardly, we have to830* do them on the render batch...831*/832struct crocus_batch *batch = &ice->batches[CROCUS_BATCH_RENDER];833834const uint32_t clamped_levels =835miptree_level_range_length(res, start_level, num_levels);836for (uint32_t l = 0; l < clamped_levels; l++) {837const uint32_t level = start_level + l;838if (!crocus_resource_level_has_aux(res, level))839continue;840841const uint32_t level_layers =842miptree_layer_range_length(res, level, start_layer, num_layers);843for (uint32_t a = 0; a < level_layers; a++) {844const uint32_t layer = start_layer + a;845const enum isl_aux_state aux_state =846crocus_resource_get_aux_state(res, level, layer);847const enum isl_aux_op aux_op =848isl_aux_prepare_access(aux_state, aux_usage, fast_clear_supported);849850/* Prepare the aux buffer for a conditional or unconditional access.851* A conditional access is handled by assuming that the access will852* not evaluate to a no-op. If the access does in fact occur, the aux853* will be in the required state. If it does not, no data is lost854* because the aux_op performed is lossless.855*/856if (aux_op == ISL_AUX_OP_NONE) {857/* Nothing to do here. */858} else if (isl_aux_usage_has_mcs(res->aux.usage)) {859assert(aux_op == ISL_AUX_OP_PARTIAL_RESOLVE);860crocus_mcs_partial_resolve(ice, batch, res, layer, 1);861} else if (isl_aux_usage_has_hiz(res->aux.usage)) {862crocus_hiz_exec(ice, batch, res, level, layer, 1, aux_op, false);863} else if (res->aux.usage == ISL_AUX_USAGE_STC_CCS) {864unreachable("crocus doesn't resolve STC_CCS resources");865} else {866assert(isl_aux_usage_has_ccs(res->aux.usage));867crocus_resolve_color(ice, batch, res, level, layer, aux_op);868}869870const enum isl_aux_state new_state =871isl_aux_state_transition_aux_op(aux_state, res->aux.usage, aux_op);872crocus_resource_set_aux_state(ice, res, level, layer, 1, new_state);873}874}875}876877void878crocus_resource_finish_write(struct crocus_context *ice,879struct crocus_resource *res, uint32_t level,880uint32_t start_layer, uint32_t num_layers,881enum isl_aux_usage aux_usage)882{883if (res->base.b.format == PIPE_FORMAT_S8_UINT)884res->shadow_needs_update = true;885886if (!crocus_resource_level_has_aux(res, level))887return;888889const uint32_t level_layers =890miptree_layer_range_length(res, level, start_layer, num_layers);891892for (uint32_t a = 0; a < level_layers; a++) {893const uint32_t layer = start_layer + a;894const enum isl_aux_state aux_state =895crocus_resource_get_aux_state(res, level, layer);896897/* Transition the aux state for a conditional or unconditional write. A898* conditional write is handled by assuming that the write applies to899* only part of the render target. This prevents the new state from900* losing the types of compression that might exist in the current state901* (e.g. CLEAR). If the write evaluates to a no-op, the state will still902* be able to communicate when resolves are necessary (but it may903* falsely communicate this as well).904*/905const enum isl_aux_state new_aux_state =906isl_aux_state_transition_write(aux_state, aux_usage, false);907908crocus_resource_set_aux_state(ice, res, level, layer, 1, new_aux_state);909}910}911912enum isl_aux_state913crocus_resource_get_aux_state(const struct crocus_resource *res,914uint32_t level, uint32_t layer)915{916crocus_resource_check_level_layer(res, level, layer);917assert(crocus_resource_level_has_aux(res, level));918919return res->aux.state[level][layer];920}921922void923crocus_resource_set_aux_state(struct crocus_context *ice,924struct crocus_resource *res, uint32_t level,925uint32_t start_layer, uint32_t num_layers,926enum isl_aux_state aux_state)927{928assert(crocus_resource_level_has_aux(res, level));929930num_layers = miptree_layer_range_length(res, level, start_layer, num_layers);931for (unsigned a = 0; a < num_layers; a++) {932if (res->aux.state[level][start_layer + a] != aux_state) {933res->aux.state[level][start_layer + a] = aux_state;934ice->state.dirty |= CROCUS_DIRTY_RENDER_RESOLVES_AND_FLUSHES |935CROCUS_DIRTY_COMPUTE_RESOLVES_AND_FLUSHES;936/* XXX: Need to track which bindings to make dirty */937ice->state.stage_dirty |= CROCUS_ALL_STAGE_DIRTY_BINDINGS;938}939}940}941942static bool943isl_formats_are_fast_clear_compatible(enum isl_format a, enum isl_format b)944{945/* On gen8 and earlier, the hardware was only capable of handling 0/1 clear946* values so sRGB curve application was a no-op for all fast-clearable947* formats.948*949* On gen9+, the hardware supports arbitrary clear values. For sRGB clear950* values, the hardware interprets the floats, not as what would be951* returned from the sampler (or written by the shader), but as being952* between format conversion and sRGB curve application. This means that953* we can switch between sRGB and UNORM without having to whack the clear954* color.955*/956return isl_format_srgb_to_linear(a) == isl_format_srgb_to_linear(b);957}958959void960crocus_resource_prepare_texture(struct crocus_context *ice,961struct crocus_resource *res,962enum isl_format view_format,963uint32_t start_level, uint32_t num_levels,964uint32_t start_layer, uint32_t num_layers)965{966enum isl_aux_usage aux_usage =967crocus_resource_texture_aux_usage(res);968969bool clear_supported = aux_usage != ISL_AUX_USAGE_NONE;970971/* Clear color is specified as ints or floats and the conversion is done by972* the sampler. If we have a texture view, we would have to perform the973* clear color conversion manually. Just disable clear color.974*/975if (!isl_formats_are_fast_clear_compatible(res->surf.format, view_format))976clear_supported = false;977978crocus_resource_prepare_access(ice, res, start_level, num_levels,979start_layer, num_layers,980aux_usage, clear_supported);981}982983enum isl_aux_usage984crocus_resource_render_aux_usage(struct crocus_context *ice,985struct crocus_resource *res,986uint32_t level,987enum isl_format render_format,988bool draw_aux_disabled)989{990struct crocus_screen *screen = (void *) ice->ctx.screen;991struct intel_device_info *devinfo = &screen->devinfo;992993if (draw_aux_disabled)994return ISL_AUX_USAGE_NONE;995996switch (res->aux.usage) {997case ISL_AUX_USAGE_MCS:998return res->aux.usage;9991000case ISL_AUX_USAGE_CCS_D:1001/* Otherwise, we try to fall back to CCS_D */1002if (isl_format_supports_ccs_d(devinfo, render_format))1003return ISL_AUX_USAGE_CCS_D;10041005return ISL_AUX_USAGE_NONE;10061007case ISL_AUX_USAGE_HIZ:1008assert(render_format == res->surf.format);1009return crocus_resource_level_has_hiz(res, level) ?1010res->aux.usage : ISL_AUX_USAGE_NONE;10111012default:1013return ISL_AUX_USAGE_NONE;1014}1015}10161017void1018crocus_resource_prepare_render(struct crocus_context *ice,1019struct crocus_resource *res, uint32_t level,1020uint32_t start_layer, uint32_t layer_count,1021enum isl_aux_usage aux_usage)1022{1023crocus_resource_prepare_access(ice, res, level, 1, start_layer,1024layer_count, aux_usage,1025aux_usage != ISL_AUX_USAGE_NONE);1026}10271028void1029crocus_resource_finish_render(struct crocus_context *ice,1030struct crocus_resource *res, uint32_t level,1031uint32_t start_layer, uint32_t layer_count,1032enum isl_aux_usage aux_usage)1033{1034crocus_resource_finish_write(ice, res, level, start_layer, layer_count,1035aux_usage);1036}10371038static void1039crocus_update_stencil_shadow(struct crocus_context *ice,1040struct crocus_resource *res)1041{1042struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;1043UNUSED const struct intel_device_info *devinfo = &screen->devinfo;1044assert(devinfo->ver == 7);10451046if (!res->shadow_needs_update)1047return;10481049struct pipe_box box;1050for (unsigned level = 0; level <= res->base.b.last_level; level++) {1051u_box_2d(0, 0,1052u_minify(res->base.b.width0, level),1053u_minify(res->base.b.height0, level), &box);1054const unsigned depth = res->base.b.target == PIPE_TEXTURE_3D ?1055u_minify(res->base.b.depth0, level) : res->base.b.array_size;10561057for (unsigned layer = 0; layer < depth; layer++) {1058box.z = layer;1059ice->ctx.resource_copy_region(&ice->ctx,1060&res->shadow->base.b, level, 0, 0, layer,1061&res->base.b, level, &box);1062}1063}1064res->shadow_needs_update = false;1065}106610671068