• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2017 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 /**
24  * @file iris_resolve.c
25  *
26  * This file handles resolve tracking for main and auxiliary surfaces.
27  *
28  * It also handles our cache tracking.  We have sets for the render cache,
29  * depth cache, and so on.  If a BO is in a cache's set, then it may have
30  * data in that cache.  The helpers take care of emitting flushes for
31  * render-to-texture, format reinterpretation issues, and other situations.
32  */
33 
34 #include "util/hash_table.h"
35 #include "util/set.h"
36 #include "iris_context.h"
37 #include "compiler/nir/nir.h"
38 
39 /**
40  * Disable auxiliary buffers if a renderbuffer is also bound as a texture.
41  * This causes a self-dependency, where both rendering and sampling may
42  * concurrently read or write the CCS buffer, causing incorrect pixels.
43  */
44 static bool
disable_rb_aux_buffer(struct iris_context * ice,bool * draw_aux_buffer_disabled,struct iris_resource * tex_res,unsigned min_level,unsigned num_levels,const char * usage)45 disable_rb_aux_buffer(struct iris_context *ice,
46                       bool *draw_aux_buffer_disabled,
47                       struct iris_resource *tex_res,
48                       unsigned min_level, unsigned num_levels,
49                       const char *usage)
50 {
51    struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
52    bool found = false;
53 
54    /* We only need to worry about color compression and fast clears. */
55    if (tex_res->aux.usage != ISL_AUX_USAGE_CCS_D &&
56        tex_res->aux.usage != ISL_AUX_USAGE_CCS_E &&
57        tex_res->aux.usage != ISL_AUX_USAGE_FCV_CCS_E)
58       return false;
59 
60    for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {
61       struct iris_surface *surf = (void *) cso_fb->cbufs[i];
62       if (!surf)
63          continue;
64 
65       struct iris_resource *rb_res = (void *) surf->base.texture;
66 
67       if (rb_res->bo == tex_res->bo &&
68           surf->base.u.tex.level >= min_level &&
69           surf->base.u.tex.level < min_level + num_levels) {
70          found = draw_aux_buffer_disabled[i] = true;
71       }
72    }
73 
74    if (found) {
75       perf_debug(&ice->dbg,
76                  "Disabling CCS because a renderbuffer is also bound %s.\n",
77                  usage);
78    }
79 
80    return found;
81 }
82 
83 static void
resolve_sampler_views(struct iris_context * ice,struct iris_batch * batch,struct iris_shader_state * shs,const struct shader_info * info,bool * draw_aux_buffer_disabled,bool consider_framebuffer)84 resolve_sampler_views(struct iris_context *ice,
85                       struct iris_batch *batch,
86                       struct iris_shader_state *shs,
87                       const struct shader_info *info,
88                       bool *draw_aux_buffer_disabled,
89                       bool consider_framebuffer)
90 {
91    if (info == NULL)
92       return;
93 
94    int i;
95    BITSET_FOREACH_SET(i, shs->bound_sampler_views, IRIS_MAX_TEXTURES) {
96       if (!BITSET_TEST(info->textures_used, i))
97          continue;
98 
99       struct iris_sampler_view *isv = shs->textures[i];
100 
101       if (isv->res->base.b.target != PIPE_BUFFER) {
102          if (consider_framebuffer) {
103             disable_rb_aux_buffer(ice, draw_aux_buffer_disabled, isv->res,
104                                   isv->view.base_level, isv->view.levels,
105                                   "for sampling");
106          }
107 
108          iris_resource_prepare_texture(ice, isv->res, isv->view.format,
109                                        isv->view.base_level, isv->view.levels,
110                                        isv->view.base_array_layer,
111                                        isv->view.array_len);
112       }
113 
114       iris_emit_buffer_barrier_for(batch, isv->res->bo,
115                                    IRIS_DOMAIN_SAMPLER_READ);
116    }
117 }
118 
119 static void
resolve_image_views(struct iris_context * ice,struct iris_batch * batch,struct iris_shader_state * shs,const struct shader_info * info)120 resolve_image_views(struct iris_context *ice,
121                     struct iris_batch *batch,
122                     struct iris_shader_state *shs,
123                     const struct shader_info *info)
124 {
125    if (info == NULL)
126       return;
127 
128    const uint64_t images_used =
129       (info->images_used[0] | ((uint64_t)info->images_used[1]) << 32);
130    uint64_t views = shs->bound_image_views & images_used;
131 
132    while (views) {
133       const int i = u_bit_scan64(&views);
134       struct pipe_image_view *pview = &shs->image[i].base;
135       struct iris_resource *res = (void *) pview->resource;
136 
137       if (res->base.b.target != PIPE_BUFFER) {
138          unsigned num_layers =
139             pview->u.tex.last_layer - pview->u.tex.first_layer + 1;
140 
141          enum isl_aux_usage aux_usage =
142             iris_image_view_aux_usage(ice, pview, info);
143 
144          enum isl_format view_format = iris_image_view_get_format(ice, pview);
145 
146          bool clear_supported = isl_aux_usage_has_fast_clears(aux_usage);
147 
148          if (!iris_render_formats_color_compatible(view_format,
149                                                    res->surf.format,
150                                                    res->aux.clear_color,
151                                                    res->aux.clear_color_unknown))
152             clear_supported = false;
153 
154          iris_resource_prepare_access(ice, res,
155                                       pview->u.tex.level, 1,
156                                       pview->u.tex.first_layer, num_layers,
157                                       aux_usage, clear_supported);
158 
159          shs->image_aux_usage[i] = aux_usage;
160       } else {
161          shs->image_aux_usage[i] = ISL_AUX_USAGE_NONE;
162       }
163 
164       iris_emit_buffer_barrier_for(batch, res->bo, IRIS_DOMAIN_DATA_WRITE);
165    }
166 }
167 
168 /**
169  * \brief Resolve buffers before drawing.
170  *
171  * Resolve the depth buffer's HiZ buffer, resolve the depth buffer of each
172  * enabled depth texture, and flush the render cache for any dirty textures.
173  */
174 void
iris_predraw_resolve_inputs(struct iris_context * ice,struct iris_batch * batch,bool * draw_aux_buffer_disabled,gl_shader_stage stage,bool consider_framebuffer)175 iris_predraw_resolve_inputs(struct iris_context *ice,
176                             struct iris_batch *batch,
177                             bool *draw_aux_buffer_disabled,
178                             gl_shader_stage stage,
179                             bool consider_framebuffer)
180 {
181    struct iris_shader_state *shs = &ice->state.shaders[stage];
182    const struct shader_info *info = iris_get_shader_info(ice, stage);
183 
184    uint64_t stage_dirty = (IRIS_STAGE_DIRTY_BINDINGS_VS << stage) |
185       (consider_framebuffer ? IRIS_STAGE_DIRTY_BINDINGS_FS : 0);
186 
187    if (ice->state.stage_dirty & stage_dirty) {
188       resolve_sampler_views(ice, batch, shs, info, draw_aux_buffer_disabled,
189                             consider_framebuffer);
190       resolve_image_views(ice, batch, shs, info);
191    }
192 }
193 
194 void
iris_predraw_resolve_framebuffer(struct iris_context * ice,struct iris_batch * batch,bool * draw_aux_buffer_disabled)195 iris_predraw_resolve_framebuffer(struct iris_context *ice,
196                                  struct iris_batch *batch,
197                                  bool *draw_aux_buffer_disabled)
198 {
199    struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
200    struct iris_screen *screen = (void *) ice->ctx.screen;
201    const struct intel_device_info *devinfo = screen->devinfo;
202    struct iris_uncompiled_shader *ish =
203       ice->shaders.uncompiled[MESA_SHADER_FRAGMENT];
204    const nir_shader *nir = ish->nir;
205 
206    if (ice->state.dirty & IRIS_DIRTY_DEPTH_BUFFER) {
207       struct pipe_surface *zs_surf = cso_fb->zsbuf;
208 
209       if (zs_surf) {
210          struct iris_resource *z_res, *s_res;
211          iris_get_depth_stencil_resources(zs_surf->texture, &z_res, &s_res);
212          unsigned num_layers =
213             zs_surf->u.tex.last_layer - zs_surf->u.tex.first_layer + 1;
214 
215          if (z_res) {
216             iris_resource_prepare_render(ice, z_res, z_res->surf.format,
217                                          zs_surf->u.tex.level,
218                                          zs_surf->u.tex.first_layer,
219                                          num_layers, ice->state.hiz_usage);
220             iris_emit_buffer_barrier_for(batch, z_res->bo,
221                                          IRIS_DOMAIN_DEPTH_WRITE);
222          }
223 
224          if (s_res) {
225             iris_emit_buffer_barrier_for(batch, s_res->bo,
226                                          IRIS_DOMAIN_DEPTH_WRITE);
227          }
228       }
229    }
230 
231    if (devinfo->ver == 8 && nir->info.outputs_read != 0) {
232       for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {
233          if (cso_fb->cbufs[i]) {
234             struct iris_surface *surf = (void *) cso_fb->cbufs[i];
235             struct iris_resource *res = (void *) cso_fb->cbufs[i]->texture;
236 
237             iris_resource_prepare_texture(ice, res, surf->view.format,
238                                           surf->view.base_level, 1,
239                                           surf->view.base_array_layer,
240                                           surf->view.array_len);
241          }
242       }
243    }
244 
245    if (ice->state.stage_dirty & IRIS_STAGE_DIRTY_BINDINGS_FS) {
246       for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {
247          struct iris_surface *surf = (void *) cso_fb->cbufs[i];
248          if (!surf)
249             continue;
250 
251          struct iris_resource *res = (void *) surf->base.texture;
252 
253          /* Undocumented workaround:
254           *
255           * Disable auxiliary buffer if MSRT is bound as texture.
256           */
257          if (intel_device_info_is_dg2(devinfo) && res->surf.samples > 1 &&
258              nir->info.outputs_read != 0)
259             draw_aux_buffer_disabled[i] = true;
260 
261          enum isl_aux_usage aux_usage =
262             iris_resource_render_aux_usage(ice, res, surf->view.format,
263                                            surf->view.base_level,
264                                            draw_aux_buffer_disabled[i]);
265 
266          if (ice->state.draw_aux_usage[i] != aux_usage) {
267             ice->state.draw_aux_usage[i] = aux_usage;
268             /* XXX: Need to track which bindings to make dirty */
269             ice->state.dirty |= IRIS_DIRTY_RENDER_BUFFER;
270             ice->state.stage_dirty |= IRIS_ALL_STAGE_DIRTY_BINDINGS;
271          }
272 
273          iris_resource_prepare_render(ice, res, surf->view.format,
274                                       surf->view.base_level,
275                                       surf->view.base_array_layer,
276                                       surf->view.array_len,
277                                       aux_usage);
278 
279          iris_emit_buffer_barrier_for(batch, res->bo,
280                                       IRIS_DOMAIN_RENDER_WRITE);
281       }
282    }
283 }
284 
285 void
iris_postdraw_update_image_resolve_tracking(struct iris_context * ice,gl_shader_stage stage)286 iris_postdraw_update_image_resolve_tracking(struct iris_context *ice,
287                                             gl_shader_stage stage)
288 {
289    struct iris_screen *screen = (void *) ice->ctx.screen;
290    ASSERTED const struct intel_device_info *devinfo = screen->devinfo;
291 
292    assert(devinfo->ver >= 12);
293 
294    const struct iris_shader_state *shs = &ice->state.shaders[stage];
295    const struct shader_info *info = iris_get_shader_info(ice, stage);
296 
297    const uint64_t images_used = !info ? 0 :
298       (info->images_used[0] | ((uint64_t)info->images_used[1]) << 32);
299    uint64_t views = shs->bound_image_views & images_used;
300 
301    while (views) {
302       const int i = u_bit_scan64(&views);
303       const struct pipe_image_view *pview = &shs->image[i].base;
304       struct iris_resource *res = (void *) pview->resource;
305 
306       if (pview->shader_access & PIPE_IMAGE_ACCESS_WRITE &&
307           res->base.b.target != PIPE_BUFFER) {
308          unsigned num_layers =
309             pview->u.tex.last_layer - pview->u.tex.first_layer + 1;
310 
311          iris_resource_finish_write(ice, res, pview->u.tex.level,
312                                     pview->u.tex.first_layer, num_layers,
313                                     shs->image_aux_usage[i]);
314       }
315    }
316 }
317 
318 /**
319  * \brief Call this after drawing to mark which buffers need resolving
320  *
321  * If the depth buffer was written to and if it has an accompanying HiZ
322  * buffer, then mark that it needs a depth resolve.
323  *
324  * If the color buffer is a multisample window system buffer, then
325  * mark that it needs a downsample.
326  *
327  * Also mark any render targets which will be textured as needing a render
328  * cache flush.
329  */
330 void
iris_postdraw_update_resolve_tracking(struct iris_context * ice)331 iris_postdraw_update_resolve_tracking(struct iris_context *ice)
332 {
333    struct iris_screen *screen = (void *) ice->ctx.screen;
334    const struct intel_device_info *devinfo = screen->devinfo;
335    struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
336 
337    // XXX: front buffer drawing?
338 
339    bool may_have_resolved_depth =
340       ice->state.dirty & (IRIS_DIRTY_DEPTH_BUFFER |
341                           IRIS_DIRTY_WM_DEPTH_STENCIL);
342 
343    struct pipe_surface *zs_surf = cso_fb->zsbuf;
344    if (zs_surf) {
345       struct iris_resource *z_res, *s_res;
346       iris_get_depth_stencil_resources(zs_surf->texture, &z_res, &s_res);
347       unsigned num_layers =
348          zs_surf->u.tex.last_layer - zs_surf->u.tex.first_layer + 1;
349 
350       if (z_res) {
351          if (may_have_resolved_depth && ice->state.depth_writes_enabled) {
352             iris_resource_finish_render(ice, z_res, zs_surf->u.tex.level,
353                                         zs_surf->u.tex.first_layer,
354                                         num_layers, ice->state.hiz_usage);
355          }
356       }
357 
358       if (s_res) {
359          if (may_have_resolved_depth && ice->state.stencil_writes_enabled) {
360             iris_resource_finish_write(ice, s_res, zs_surf->u.tex.level,
361                                        zs_surf->u.tex.first_layer, num_layers,
362                                        s_res->aux.usage);
363          }
364       }
365    }
366 
367    bool may_have_resolved_color =
368       ice->state.stage_dirty & IRIS_STAGE_DIRTY_BINDINGS_FS;
369 
370    for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {
371       struct iris_surface *surf = (void *) cso_fb->cbufs[i];
372       if (!surf)
373          continue;
374 
375       struct iris_resource *res = (void *) surf->base.texture;
376       enum isl_aux_usage aux_usage = ice->state.draw_aux_usage[i];
377 
378       if (may_have_resolved_color) {
379          union pipe_surface_desc *desc = &surf->base.u;
380          unsigned num_layers =
381             desc->tex.last_layer - desc->tex.first_layer + 1;
382          iris_resource_finish_render(ice, res, desc->tex.level,
383                                      desc->tex.first_layer, num_layers,
384                                      aux_usage);
385       }
386    }
387 
388    if (devinfo->ver >= 12) {
389       for (gl_shader_stage stage = 0; stage < MESA_SHADER_COMPUTE; stage++) {
390          iris_postdraw_update_image_resolve_tracking(ice, stage);
391       }
392    }
393 }
394 
395 static void
flush_previous_aux_mode(struct iris_batch * batch,const struct iris_bo * bo,enum isl_aux_usage aux_usage)396 flush_previous_aux_mode(struct iris_batch *batch,
397                         const struct iris_bo *bo,
398                         enum isl_aux_usage aux_usage)
399 {
400    /* Check to see if this BO has been put into caches by a previous operation
401     * but with a different aux usage.  If it has, flush those caches to ensure
402     * that it's only in there with one aux usage at a time.
403     *
404     * Even though it's not obvious, this could easily happen in practice.
405     * Suppose a client is blending on a surface with sRGB encode enabled on
406     * gfx9.  This implies that you get AUX_USAGE_CCS_D at best.  If the client
407     * then disables sRGB decode and continues blending we could flip on
408     * AUX_USAGE_CCS_E without doing any sort of resolve in-between (this is
409     * perfectly valid since CCS_E is a subset of CCS_D).  However, this means
410     * that we have fragments in-flight which are rendering with UNORM+CCS_E
411     * and other fragments in-flight with SRGB+CCS_D on the same surface at the
412     * same time and the pixel scoreboard and color blender are trying to sort
413     * it all out.  This ends badly (i.e. GPU hangs).
414     *
415     * There are comments in various docs which indicate that the render cache
416     * isn't 100% resilient to format changes.  However, to date, we have never
417     * observed GPU hangs or even corruption to be associated with switching the
418     * format, only the aux usage.  So we let that slide for now.
419     *
420     * We haven't seen issues on gfx12 hardware when switching between
421     * FCV_CCS_E and plain CCS_E. A switch could indicate a transition in
422     * accessing data through a different cache domain. The flushes and
423     * invalidates that come from the cache tracker and memory barrier
424     * functions seem to be enough to handle this. Treat the two as equivalent
425     * to avoid extra cache flushing.
426     */
427    void *v_aux_usage = (void *) (uintptr_t)
428       (aux_usage == ISL_AUX_USAGE_FCV_CCS_E ?
429        ISL_AUX_USAGE_CCS_E : aux_usage);
430 
431    struct hash_entry *entry =
432       _mesa_hash_table_search_pre_hashed(batch->bo_aux_modes, bo->hash, bo);
433    if (!entry) {
434       _mesa_hash_table_insert_pre_hashed(batch->bo_aux_modes, bo->hash, bo,
435                                          v_aux_usage);
436    } else if (entry->data != v_aux_usage) {
437       iris_emit_pipe_control_flush(batch,
438                                    "cache tracker: aux usage mismatch",
439                                    PIPE_CONTROL_RENDER_TARGET_FLUSH |
440                                    PIPE_CONTROL_TILE_CACHE_FLUSH |
441                                    PIPE_CONTROL_CS_STALL);
442       entry->data = v_aux_usage;
443    }
444 }
445 
446 static void
flush_ubos(struct iris_batch * batch,struct iris_shader_state * shs)447 flush_ubos(struct iris_batch *batch,
448             struct iris_shader_state *shs)
449 {
450    uint32_t cbufs = shs->dirty_cbufs & shs->bound_cbufs;
451 
452    while (cbufs) {
453       const int i = u_bit_scan(&cbufs);
454       struct pipe_shader_buffer *cbuf = &shs->constbuf[i];
455       struct iris_resource *res = (void *)cbuf->buffer;
456       iris_emit_buffer_barrier_for(batch, res->bo, IRIS_DOMAIN_PULL_CONSTANT_READ);
457    }
458 
459    shs->dirty_cbufs = 0;
460 }
461 
462 static void
flush_ssbos(struct iris_batch * batch,struct iris_shader_state * shs)463 flush_ssbos(struct iris_batch *batch,
464             struct iris_shader_state *shs)
465 {
466    uint32_t ssbos = shs->bound_ssbos;
467 
468    while (ssbos) {
469       const int i = u_bit_scan(&ssbos);
470       struct pipe_shader_buffer *ssbo = &shs->ssbo[i];
471       struct iris_resource *res = (void *)ssbo->buffer;
472       iris_emit_buffer_barrier_for(batch, res->bo, IRIS_DOMAIN_DATA_WRITE);
473    }
474 }
475 
476 void
iris_predraw_flush_buffers(struct iris_context * ice,struct iris_batch * batch,gl_shader_stage stage)477 iris_predraw_flush_buffers(struct iris_context *ice,
478                            struct iris_batch *batch,
479                            gl_shader_stage stage)
480 {
481    struct iris_shader_state *shs = &ice->state.shaders[stage];
482 
483    if (ice->state.stage_dirty & (IRIS_STAGE_DIRTY_CONSTANTS_VS << stage))
484       flush_ubos(batch, shs);
485 
486    if (ice->state.stage_dirty & (IRIS_STAGE_DIRTY_BINDINGS_VS << stage))
487       flush_ssbos(batch, shs);
488 
489    if (ice->state.streamout_active &&
490        (ice->state.dirty & IRIS_DIRTY_SO_BUFFERS)) {
491       for (int i = 0; i < 4; i++) {
492          struct iris_stream_output_target *tgt = (void *)ice->state.so_target[i];
493          if (tgt) {
494             struct iris_bo *bo = iris_resource_bo(tgt->base.buffer);
495             iris_emit_buffer_barrier_for(batch, bo, IRIS_DOMAIN_OTHER_WRITE);
496          }
497       }
498    }
499 }
500 
501 static void
iris_resolve_color(struct iris_context * ice,struct iris_batch * batch,struct iris_resource * res,unsigned level,unsigned layer,enum isl_aux_op resolve_op)502 iris_resolve_color(struct iris_context *ice,
503                    struct iris_batch *batch,
504                    struct iris_resource *res,
505                    unsigned level, unsigned layer,
506                    enum isl_aux_op resolve_op)
507 {
508    //DBG("%s to mt %p level %u layer %u\n", __func__, mt, level, layer);
509 
510    struct blorp_surf surf;
511    iris_blorp_surf_for_resource(&batch->screen->isl_dev, &surf,
512                                 &res->base.b, res->aux.usage, level, true);
513 
514    iris_batch_maybe_flush(batch, 1500);
515 
516    /* Ivybridge PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":
517     *
518     *    "Any transition from any value in {Clear, Render, Resolve} to a
519     *     different value in {Clear, Render, Resolve} requires end of pipe
520     *     synchronization."
521     *
522     * In other words, fast clear ops are not properly synchronized with
523     * other drawing.  We need to use a PIPE_CONTROL to ensure that the
524     * contents of the previous draw hit the render target before we resolve
525     * and again afterwards to ensure that the resolve is complete before we
526     * do any more regular drawing.
527     */
528    iris_emit_end_of_pipe_sync(batch, "color resolve: pre-flush",
529                               PIPE_CONTROL_RENDER_TARGET_FLUSH);
530 
531    if (intel_needs_workaround(batch->screen->devinfo, 1508744258)) {
532       /* The suggested workaround is:
533        *
534        *    Disable RHWO by setting 0x7010[14] by default except during resolve
535        *    pass.
536        *
537        * We implement global disabling of the RHWO optimization during
538        * iris_init_render_context. We toggle it around the blorp resolve call.
539        */
540       assert(resolve_op == ISL_AUX_OP_FULL_RESOLVE ||
541              resolve_op == ISL_AUX_OP_PARTIAL_RESOLVE);
542       batch->screen->vtbl.disable_rhwo_optimization(batch, false);
543    }
544 
545    iris_batch_sync_region_start(batch);
546    struct blorp_batch blorp_batch;
547    blorp_batch_init(&ice->blorp, &blorp_batch, batch, 0);
548    blorp_ccs_resolve(&blorp_batch, &surf, level, layer, 1, res->surf.format,
549                      resolve_op);
550    blorp_batch_finish(&blorp_batch);
551 
552    /* See comment above */
553    iris_emit_end_of_pipe_sync(batch, "color resolve: post-flush",
554                               PIPE_CONTROL_RENDER_TARGET_FLUSH);
555 
556    if (intel_needs_workaround(batch->screen->devinfo, 1508744258)) {
557       batch->screen->vtbl.disable_rhwo_optimization(batch, true);
558    }
559 
560    iris_batch_sync_region_end(batch);
561 }
562 
563 static void
iris_mcs_exec(struct iris_context * ice,struct iris_batch * batch,struct iris_resource * res,uint32_t start_layer,uint32_t num_layers,enum isl_aux_op op)564 iris_mcs_exec(struct iris_context *ice,
565               struct iris_batch *batch,
566               struct iris_resource *res,
567               uint32_t start_layer,
568               uint32_t num_layers,
569               enum isl_aux_op op)
570 {
571    //DBG("%s to mt %p layers %u-%u\n", __func__, mt,
572        //start_layer, start_layer + num_layers - 1);
573 
574    assert(isl_aux_usage_has_mcs(res->aux.usage));
575 
576    iris_batch_maybe_flush(batch, 1500);
577 
578    struct blorp_surf surf;
579    iris_blorp_surf_for_resource(&batch->screen->isl_dev, &surf,
580                                 &res->base.b, res->aux.usage, 0, true);
581 
582    /* MCS partial resolve will read from the MCS surface. */
583    assert(res->aux.bo == res->bo);
584    iris_emit_buffer_barrier_for(batch, res->bo, IRIS_DOMAIN_SAMPLER_READ);
585    iris_emit_buffer_barrier_for(batch, res->bo, IRIS_DOMAIN_RENDER_WRITE);
586 
587    struct blorp_batch blorp_batch;
588    iris_batch_sync_region_start(batch);
589    blorp_batch_init(&ice->blorp, &blorp_batch, batch, 0);
590 
591    if (op == ISL_AUX_OP_PARTIAL_RESOLVE) {
592       blorp_mcs_partial_resolve(&blorp_batch, &surf, res->surf.format,
593                                 start_layer, num_layers);
594    } else if (op == ISL_AUX_OP_FULL_RESOLVE) {
595       /* Simply copy compressed surface to uncompressed surface in order to do
596        * the full resolve.
597        */
598       struct blorp_surf src_surf, dst_surf;
599       iris_blorp_surf_for_resource(&batch->screen->isl_dev, &src_surf,
600                                    &res->base.b, res->aux.usage, 0, false);
601       iris_blorp_surf_for_resource(&batch->screen->isl_dev, &dst_surf,
602                                    &res->base.b, ISL_AUX_USAGE_NONE, 0, true);
603 
604       blorp_copy(&blorp_batch, &src_surf, 0, 0, &dst_surf, 0, 0,
605                  0, 0, 0, 0, surf.surf->logical_level0_px.width,
606                  surf.surf->logical_level0_px.height);
607    } else {
608       assert(op == ISL_AUX_OP_AMBIGUATE);
609       blorp_mcs_ambiguate(&blorp_batch, &surf, start_layer, num_layers);
610    }
611 
612    blorp_batch_finish(&blorp_batch);
613    iris_batch_sync_region_end(batch);
614 }
615 
616 bool
iris_sample_with_depth_aux(const struct intel_device_info * devinfo,const struct iris_resource * res)617 iris_sample_with_depth_aux(const struct intel_device_info *devinfo,
618                            const struct iris_resource *res)
619 {
620    switch (res->aux.usage) {
621    case ISL_AUX_USAGE_HIZ_CCS_WT:
622       /* Always support sampling with HIZ_CCS_WT.  Although the sampler
623        * doesn't comprehend HiZ, write-through means that the correct data
624        * will be in the CCS, and the sampler can simply rely on that.
625        */
626       return true;
627    case ISL_AUX_USAGE_HIZ_CCS:
628       /* Without write-through, the CCS data may be out of sync with HiZ
629        * and the sampler won't see the correct data.  Skip both.
630        */
631       return false;
632    case ISL_AUX_USAGE_HIZ:
633       /* From the Broadwell PRM (Volume 2d: Command Reference: Structures
634        * RENDER_SURFACE_STATE.AuxiliarySurfaceMode):
635        *
636        *   "If this field is set to AUX_HIZ, Number of Multisamples must be
637        *    MULTISAMPLECOUNT_1, and Surface Type cannot be SURFTYPE_3D.
638        *
639        * There is no such blurb for 1D textures, but there is sufficient
640        * evidence that this is broken on SKL+.
641        */
642       if (!devinfo->has_sample_with_hiz ||
643           res->surf.samples != 1 ||
644           res->surf.dim != ISL_SURF_DIM_2D)
645          return false;
646 
647       /* Make sure that HiZ exists for all necessary miplevels. */
648       for (unsigned level = 0; level < res->surf.levels; ++level) {
649          if (!iris_resource_level_has_hiz(devinfo, res, level))
650             return false;
651       }
652 
653       /* We can sample directly from HiZ in this case. */
654       return true;
655    default:
656       return false;
657    }
658 }
659 
660 /**
661  * Perform a HiZ or depth resolve operation.
662  *
663  * For an overview of HiZ ops, see the following sections of the Sandy Bridge
664  * PRM, Volume 1, Part 2:
665  *   - 7.5.3.1 Depth Buffer Clear
666  *   - 7.5.3.2 Depth Buffer Resolve
667  *   - 7.5.3.3 Hierarchical Depth Buffer Resolve
668  */
669 void
iris_hiz_exec(struct iris_context * ice,struct iris_batch * batch,struct iris_resource * res,unsigned int level,unsigned int start_layer,unsigned int num_layers,enum isl_aux_op op,bool update_clear_depth)670 iris_hiz_exec(struct iris_context *ice,
671               struct iris_batch *batch,
672               struct iris_resource *res,
673               unsigned int level, unsigned int start_layer,
674               unsigned int num_layers, enum isl_aux_op op,
675               bool update_clear_depth)
676 {
677    ASSERTED const struct intel_device_info *devinfo = batch->screen->devinfo;
678 
679    assert(iris_resource_level_has_hiz(devinfo, res, level));
680    assert(op != ISL_AUX_OP_NONE);
681    UNUSED const char *name = NULL;
682 
683    iris_batch_maybe_flush(batch, 1500);
684 
685    switch (op) {
686    case ISL_AUX_OP_FULL_RESOLVE:
687       name = "depth resolve";
688       break;
689    case ISL_AUX_OP_AMBIGUATE:
690       name = "hiz ambiguate";
691       break;
692    case ISL_AUX_OP_FAST_CLEAR:
693       name = "depth clear";
694       break;
695    case ISL_AUX_OP_PARTIAL_RESOLVE:
696    case ISL_AUX_OP_NONE:
697       unreachable("Invalid HiZ op");
698    }
699 
700    //DBG("%s %s to mt %p level %d layers %d-%d\n",
701        //__func__, name, mt, level, start_layer, start_layer + num_layers - 1);
702 
703    /* A data cache flush is not suggested by HW docs, but we found it to fix
704     * a number of failures.
705     */
706    unsigned wa_flush = devinfo->verx10 >= 125 &&
707                        res->aux.usage == ISL_AUX_USAGE_HIZ_CCS ?
708                        PIPE_CONTROL_DATA_CACHE_FLUSH : 0;
709 
710    /* The following stalls and flushes are only documented to be required
711     * for HiZ clear operations.  However, they also seem to be required for
712     * resolve operations.
713     *
714     * From the Ivybridge PRM, volume 2, "Depth Buffer Clear":
715     *
716     *   "If other rendering operations have preceded this clear, a
717     *    PIPE_CONTROL with depth cache flush enabled, Depth Stall bit
718     *    enabled must be issued before the rectangle primitive used for
719     *    the depth buffer clear operation."
720     *
721     * Same applies for Gfx8 and Gfx9.
722     */
723    iris_emit_pipe_control_flush(batch,
724                                 "hiz op: pre-flush",
725                                 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
726                                 wa_flush |
727                                 PIPE_CONTROL_DEPTH_STALL |
728                                 PIPE_CONTROL_CS_STALL);
729 
730    iris_batch_sync_region_start(batch);
731 
732    struct blorp_surf surf;
733    iris_blorp_surf_for_resource(&batch->screen->isl_dev, &surf,
734                                 &res->base.b, res->aux.usage, level, true);
735 
736    struct blorp_batch blorp_batch;
737    enum blorp_batch_flags flags = 0;
738    flags |= update_clear_depth ? 0 : BLORP_BATCH_NO_UPDATE_CLEAR_COLOR;
739    blorp_batch_init(&ice->blorp, &blorp_batch, batch, flags);
740    blorp_hiz_op(&blorp_batch, &surf, level, start_layer, num_layers, op);
741    blorp_batch_finish(&blorp_batch);
742 
743    /* For gfx8-11, the following stalls and flushes are only documented to be
744     * required for HiZ clear operations.  However, they also seem to be
745     * required for resolve operations.
746     *
747     * From the Broadwell PRM, volume 7, "Depth Buffer Clear":
748     *
749     *    "Depth buffer clear pass using any of the methods (WM_STATE,
750     *     3DSTATE_WM or 3DSTATE_WM_HZ_OP) must be followed by a
751     *     PIPE_CONTROL command with DEPTH_STALL bit and Depth FLUSH bits
752     *     "set" before starting to render.  DepthStall and DepthFlush are
753     *     not needed between consecutive depth clear passes nor is it
754     *     required if the depth clear pass was done with
755     *     'full_surf_clear' bit set in the 3DSTATE_WM_HZ_OP."
756     *
757     * TODO: Such as the spec says, this could be conditional.
758     *
759     * From Bspec 46959, a programming note applicable to Gfx12+:
760     *
761     *    " Since HZ_OP has to be sent twice (first time set the clear/resolve
762     *    state and 2nd time to clear the state), and HW internally flushes the
763     *    depth cache on HZ_OP, there is no need to explicitly send a Depth
764     *    Cache flush after Clear or Resolve."
765     */
766    if (devinfo->verx10 < 120) {
767       iris_emit_pipe_control_flush(batch,
768                                    "hiz op: post flush",
769                                    PIPE_CONTROL_DEPTH_CACHE_FLUSH |
770                                    PIPE_CONTROL_DEPTH_STALL);
771    }
772 
773    iris_batch_sync_region_end(batch);
774 }
775 
776 /**
777  * Does the resource's slice have hiz enabled?
778  */
779 bool
iris_resource_level_has_hiz(const struct intel_device_info * devinfo,const struct iris_resource * res,uint32_t level)780 iris_resource_level_has_hiz(const struct intel_device_info *devinfo,
781                             const struct iris_resource *res, uint32_t level)
782 {
783    iris_resource_check_level_layer(res, level, 0);
784 
785    if (!isl_aux_usage_has_hiz(res->aux.usage))
786       return false;
787 
788    /* Disable HiZ for LOD > 0 unless the width/height are 8x4 aligned.
789     * For LOD == 0, we can grow the dimensions to make it work.
790     *
791     * This doesn't appear to be necessary on Gfx11+.  See details here:
792     * https://gitlab.freedesktop.org/mesa/mesa/-/issues/3788
793     */
794    if (devinfo->ver < 11 && level > 0) {
795       if (u_minify(res->base.b.width0, level) & 7)
796          return false;
797 
798       if (u_minify(res->base.b.height0, level) & 3)
799          return false;
800    }
801 
802    return true;
803 }
804 
805 /** \brief Assert that the level and layer are valid for the resource. */
806 void
iris_resource_check_level_layer(UNUSED const struct iris_resource * res,UNUSED uint32_t level,UNUSED uint32_t layer)807 iris_resource_check_level_layer(UNUSED const struct iris_resource *res,
808                                 UNUSED uint32_t level, UNUSED uint32_t layer)
809 {
810    assert(level < res->surf.levels);
811    assert(layer < util_num_layers(&res->base.b, level));
812 }
813 
814 static inline uint32_t
miptree_level_range_length(const struct iris_resource * res,uint32_t start_level,uint32_t num_levels)815 miptree_level_range_length(const struct iris_resource *res,
816                            uint32_t start_level, uint32_t num_levels)
817 {
818    assert(start_level < res->surf.levels);
819 
820    if (num_levels == INTEL_REMAINING_LEVELS)
821       num_levels = res->surf.levels - start_level;
822 
823    /* Check for overflow */
824    assert(start_level + num_levels >= start_level);
825    assert(start_level + num_levels <= res->surf.levels);
826 
827    return num_levels;
828 }
829 
830 static inline uint32_t
miptree_layer_range_length(const struct iris_resource * res,uint32_t level,uint32_t start_layer,uint32_t num_layers)831 miptree_layer_range_length(const struct iris_resource *res, uint32_t level,
832                            uint32_t start_layer, uint32_t num_layers)
833 {
834    assert(level <= res->base.b.last_level);
835 
836    const uint32_t total_num_layers = iris_get_num_logical_layers(res, level);
837    assert(start_layer < total_num_layers);
838    if (num_layers == INTEL_REMAINING_LAYERS)
839       num_layers = total_num_layers - start_layer;
840    /* Check for overflow */
841    assert(start_layer + num_layers >= start_layer);
842    assert(start_layer + num_layers <= total_num_layers);
843 
844    return num_layers;
845 }
846 
847 bool
iris_has_invalid_primary(const struct iris_resource * res,unsigned start_level,unsigned num_levels,unsigned start_layer,unsigned num_layers)848 iris_has_invalid_primary(const struct iris_resource *res,
849                          unsigned start_level, unsigned num_levels,
850                          unsigned start_layer, unsigned num_layers)
851 {
852    if (res->aux.usage == ISL_AUX_USAGE_NONE)
853       return false;
854 
855    /* Clamp the level range to fit the resource */
856    num_levels = miptree_level_range_length(res, start_level, num_levels);
857 
858    for (uint32_t l = 0; l < num_levels; l++) {
859       const uint32_t level = start_level + l;
860       const uint32_t level_layers =
861          miptree_layer_range_length(res, level, start_layer, num_layers);
862       for (unsigned a = 0; a < level_layers; a++) {
863          enum isl_aux_state aux_state =
864             iris_resource_get_aux_state(res, level, start_layer + a);
865          if (!isl_aux_state_has_valid_primary(aux_state))
866             return true;
867       }
868    }
869 
870    return false;
871 }
872 
873 void
iris_resource_prepare_access(struct iris_context * ice,struct iris_resource * res,uint32_t start_level,uint32_t num_levels,uint32_t start_layer,uint32_t num_layers,enum isl_aux_usage aux_usage,bool fast_clear_supported)874 iris_resource_prepare_access(struct iris_context *ice,
875                              struct iris_resource *res,
876                              uint32_t start_level, uint32_t num_levels,
877                              uint32_t start_layer, uint32_t num_layers,
878                              enum isl_aux_usage aux_usage,
879                              bool fast_clear_supported)
880 {
881    if (res->aux.usage == ISL_AUX_USAGE_NONE)
882       return;
883 
884    /* We can't do resolves on the compute engine, so awkwardly, we have to
885     * do them on the render batch...
886     */
887    struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
888 
889    const uint32_t clamped_levels =
890       miptree_level_range_length(res, start_level, num_levels);
891    for (uint32_t l = 0; l < clamped_levels; l++) {
892       const uint32_t level = start_level + l;
893       const uint32_t level_layers =
894          miptree_layer_range_length(res, level, start_layer, num_layers);
895       for (uint32_t a = 0; a < level_layers; a++) {
896          const uint32_t layer = start_layer + a;
897          const enum isl_aux_state aux_state =
898             iris_resource_get_aux_state(res, level, layer);
899          const enum isl_aux_op aux_op =
900             isl_aux_prepare_access(aux_state, aux_usage, fast_clear_supported);
901 
902          /* Prepare the aux buffer for a conditional or unconditional access.
903           * A conditional access is handled by assuming that the access will
904           * not evaluate to a no-op. If the access does in fact occur, the aux
905           * will be in the required state. If it does not, no data is lost
906           * because the aux_op performed is lossless.
907           */
908          if (aux_op == ISL_AUX_OP_NONE) {
909             /* Nothing to do here. */
910          } else if (isl_aux_usage_has_mcs(res->aux.usage)) {
911             iris_mcs_exec(ice, batch, res, layer, 1, aux_op);
912          } else if (isl_aux_usage_has_hiz(res->aux.usage)) {
913             iris_hiz_exec(ice, batch, res, level, layer, 1, aux_op, false);
914          } else if (res->aux.usage == ISL_AUX_USAGE_STC_CCS) {
915             unreachable("iris doesn't resolve STC_CCS resources");
916          } else {
917             assert(isl_aux_usage_has_ccs(res->aux.usage));
918             iris_resolve_color(ice, batch, res, level, layer, aux_op);
919          }
920 
921          const enum isl_aux_state new_state =
922             isl_aux_state_transition_aux_op(aux_state, res->aux.usage, aux_op);
923          iris_resource_set_aux_state(ice, res, level, layer, 1, new_state);
924       }
925    }
926 
927    flush_previous_aux_mode(batch, res->bo, aux_usage);
928 }
929 
930 void
iris_resource_finish_write(struct iris_context * ice,struct iris_resource * res,uint32_t level,uint32_t start_layer,uint32_t num_layers,enum isl_aux_usage aux_usage)931 iris_resource_finish_write(struct iris_context *ice,
932                            struct iris_resource *res, uint32_t level,
933                            uint32_t start_layer, uint32_t num_layers,
934                            enum isl_aux_usage aux_usage)
935 {
936    if (res->aux.usage == ISL_AUX_USAGE_NONE)
937       return;
938 
939    const uint32_t level_layers =
940       miptree_layer_range_length(res, level, start_layer, num_layers);
941 
942    for (uint32_t a = 0; a < level_layers; a++) {
943       const uint32_t layer = start_layer + a;
944       const enum isl_aux_state aux_state =
945          iris_resource_get_aux_state(res, level, layer);
946 
947       /* Transition the aux state for a conditional or unconditional write. A
948        * conditional write is handled by assuming that the write applies to
949        * only part of the render target. This prevents the new state from
950        * losing the types of compression that might exist in the current state
951        * (e.g. CLEAR). If the write evaluates to a no-op, the state will still
952        * be able to communicate when resolves are necessary (but it may
953        * falsely communicate this as well).
954        */
955       const enum isl_aux_state new_aux_state =
956          isl_aux_state_transition_write(aux_state, aux_usage, false);
957 
958       iris_resource_set_aux_state(ice, res, level, layer, 1, new_aux_state);
959    }
960 }
961 
962 enum isl_aux_state
iris_resource_get_aux_state(const struct iris_resource * res,uint32_t level,uint32_t layer)963 iris_resource_get_aux_state(const struct iris_resource *res,
964                             uint32_t level, uint32_t layer)
965 {
966    iris_resource_check_level_layer(res, level, layer);
967 
968    if (res->surf.usage & ISL_SURF_USAGE_DEPTH_BIT) {
969       assert(isl_aux_usage_has_hiz(res->aux.usage));
970    } else {
971       assert(res->surf.samples == 1 ||
972              res->surf.msaa_layout == ISL_MSAA_LAYOUT_ARRAY);
973    }
974 
975    return res->aux.state[level][layer];
976 }
977 
978 void
iris_resource_set_aux_state(struct iris_context * ice,struct iris_resource * res,uint32_t level,uint32_t start_layer,uint32_t num_layers,enum isl_aux_state aux_state)979 iris_resource_set_aux_state(struct iris_context *ice,
980                             struct iris_resource *res, uint32_t level,
981                             uint32_t start_layer, uint32_t num_layers,
982                             enum isl_aux_state aux_state)
983 {
984    struct iris_screen *screen = (void *) ice->ctx.screen;
985    ASSERTED const struct intel_device_info *devinfo = screen->devinfo;
986 
987    num_layers = miptree_layer_range_length(res, level, start_layer, num_layers);
988 
989    if (res->surf.usage & ISL_SURF_USAGE_DEPTH_BIT) {
990       assert(iris_resource_level_has_hiz(devinfo, res, level) ||
991              !isl_aux_state_has_valid_aux(aux_state));
992    } else {
993       assert(res->surf.samples == 1 ||
994              res->surf.msaa_layout == ISL_MSAA_LAYOUT_ARRAY);
995    }
996 
997    for (unsigned a = 0; a < num_layers; a++) {
998       if (res->aux.state[level][start_layer + a] != aux_state) {
999          res->aux.state[level][start_layer + a] = aux_state;
1000          /* XXX: Need to track which bindings to make dirty */
1001          ice->state.dirty |= IRIS_DIRTY_RENDER_BUFFER |
1002                              IRIS_DIRTY_RENDER_RESOLVES_AND_FLUSHES |
1003                              IRIS_DIRTY_COMPUTE_RESOLVES_AND_FLUSHES;
1004          ice->state.stage_dirty |= IRIS_ALL_STAGE_DIRTY_BINDINGS;
1005       }
1006    }
1007 
1008    if (res->mod_info && !res->mod_info->supports_clear_color) {
1009       assert(isl_drm_modifier_has_aux(res->mod_info->modifier));
1010       if (aux_state == ISL_AUX_STATE_CLEAR ||
1011           aux_state == ISL_AUX_STATE_COMPRESSED_CLEAR ||
1012           aux_state == ISL_AUX_STATE_PARTIAL_CLEAR) {
1013          iris_mark_dirty_dmabuf(ice, &res->base.b);
1014       }
1015    }
1016 }
1017 
1018 enum isl_aux_usage
iris_resource_texture_aux_usage(struct iris_context * ice,const struct iris_resource * res,enum isl_format view_format,unsigned start_level,unsigned num_levels)1019 iris_resource_texture_aux_usage(struct iris_context *ice,
1020                                 const struct iris_resource *res,
1021                                 enum isl_format view_format,
1022                                 unsigned start_level,
1023                                 unsigned num_levels)
1024 {
1025    struct iris_screen *screen = (void *) ice->ctx.screen;
1026    const struct intel_device_info *devinfo = screen->devinfo;
1027 
1028    switch (res->aux.usage) {
1029    case ISL_AUX_USAGE_HIZ:
1030    case ISL_AUX_USAGE_HIZ_CCS:
1031    case ISL_AUX_USAGE_HIZ_CCS_WT:
1032       assert(res->surf.format == view_format);
1033       return iris_sample_with_depth_aux(devinfo, res) ?
1034              res->aux.usage : ISL_AUX_USAGE_NONE;
1035 
1036    case ISL_AUX_USAGE_MCS:
1037    case ISL_AUX_USAGE_MCS_CCS:
1038    case ISL_AUX_USAGE_STC_CCS:
1039    case ISL_AUX_USAGE_MC:
1040       return res->aux.usage;
1041 
1042    case ISL_AUX_USAGE_CCS_E:
1043    case ISL_AUX_USAGE_FCV_CCS_E:
1044       /* If we don't have any unresolved color, report an aux usage of
1045        * ISL_AUX_USAGE_NONE.  This way, texturing won't even look at the
1046        * aux surface and we can save some bandwidth.
1047        */
1048       if (!iris_has_invalid_primary(res, start_level, num_levels,
1049                                     0, INTEL_REMAINING_LAYERS))
1050          return ISL_AUX_USAGE_NONE;
1051 
1052       /* On Gfx9 color buffers may be compressed by the hardware (lossless
1053        * compression). There are, however, format restrictions and care needs
1054        * to be taken that the sampler engine is capable for re-interpreting a
1055        * buffer with format different the buffer was originally written with.
1056        *
1057        * For example, SRGB formats are not compressible and the sampler engine
1058        * isn't capable of treating RGBA_UNORM as SRGB_ALPHA. In such a case
1059        * the underlying color buffer needs to be resolved so that the sampling
1060        * surface can be sampled as non-compressed (i.e., without the auxiliary
1061        * MCS buffer being set).
1062        */
1063       if (isl_formats_are_ccs_e_compatible(devinfo, res->surf.format,
1064                                            view_format))
1065          return res->aux.usage;
1066       break;
1067 
1068    default:
1069       break;
1070    }
1071 
1072    return ISL_AUX_USAGE_NONE;
1073 }
1074 
1075 enum isl_aux_usage
iris_image_view_aux_usage(struct iris_context * ice,const struct pipe_image_view * pview,const struct shader_info * info)1076 iris_image_view_aux_usage(struct iris_context *ice,
1077                           const struct pipe_image_view *pview,
1078                           const struct shader_info *info)
1079 {
1080    if (!info)
1081       return ISL_AUX_USAGE_NONE;
1082 
1083    const struct iris_screen *screen = (void *) ice->ctx.screen;
1084    const struct intel_device_info *devinfo = screen->devinfo;
1085    struct iris_resource *res = (void *) pview->resource;
1086 
1087    const unsigned level = res->base.b.target != PIPE_BUFFER ?
1088                           pview->u.tex.level : 0;
1089 
1090    bool uses_atomic_load_store =
1091       ice->shaders.uncompiled[info->stage]->uses_atomic_load_store;
1092 
1093    /* Prior to GFX12, render compression is not supported for images. */
1094    if (devinfo->ver < 12)
1095       return ISL_AUX_USAGE_NONE;
1096 
1097    /* On GFX12, compressed surfaces supports non-atomic operations. GFX12HP and
1098     * further, add support for all the operations.
1099     */
1100    if (devinfo->verx10 < 125 && uses_atomic_load_store)
1101       return ISL_AUX_USAGE_NONE;
1102 
1103    /* If the image is read-only, and doesn't have any unresolved color,
1104     * report ISL_AUX_USAGE_NONE.  Bypassing useless aux can save bandwidth.
1105     */
1106    if (!(pview->access & PIPE_IMAGE_ACCESS_WRITE) &&
1107        !iris_has_invalid_primary(res, level, 1, 0, INTEL_REMAINING_LAYERS))
1108       return ISL_AUX_USAGE_NONE;
1109 
1110    /* The FCV feature is documented to occur on regular render writes. Images
1111     * are written to with the DC data port however.
1112     */
1113    if (res->aux.usage == ISL_AUX_USAGE_FCV_CCS_E)
1114       return ISL_AUX_USAGE_CCS_E;
1115 
1116    return res->aux.usage;
1117 }
1118 
1119 static bool
formats_are_fast_clear_compatible(enum isl_format a,enum isl_format b)1120 formats_are_fast_clear_compatible(enum isl_format a, enum isl_format b)
1121 {
1122    /* On gfx8 and earlier, the hardware was only capable of handling 0/1 clear
1123     * values so sRGB curve application was a no-op for all fast-clearable
1124     * formats.
1125     *
1126     * On gfx9+, the hardware supports arbitrary clear values.  For sRGB clear
1127     * values, the hardware interprets the floats, not as what would be
1128     * returned from the sampler (or written by the shader), but as being
1129     * between format conversion and sRGB curve application.  This means that
1130     * we can switch between sRGB and UNORM without having to whack the clear
1131     * color.
1132     */
1133    return isl_format_srgb_to_linear(a) == isl_format_srgb_to_linear(b);
1134 }
1135 
1136 void
iris_resource_prepare_texture(struct iris_context * ice,struct iris_resource * res,enum isl_format view_format,uint32_t start_level,uint32_t num_levels,uint32_t start_layer,uint32_t num_layers)1137 iris_resource_prepare_texture(struct iris_context *ice,
1138                               struct iris_resource *res,
1139                               enum isl_format view_format,
1140                               uint32_t start_level, uint32_t num_levels,
1141                               uint32_t start_layer, uint32_t num_layers)
1142 {
1143    const struct iris_screen *screen = (void *) ice->ctx.screen;
1144    const struct intel_device_info *devinfo = screen->devinfo;
1145 
1146    enum isl_aux_usage aux_usage =
1147       iris_resource_texture_aux_usage(ice, res, view_format,
1148                                       start_level, num_levels);
1149 
1150    bool clear_supported = isl_aux_usage_has_fast_clears(aux_usage);
1151 
1152    /* On gfx8-9, the clear color is specified as ints or floats and the
1153     * conversion is done by the sampler.  If we have a texture view, we would
1154     * have to perform the clear color conversion manually.  Just disable clear
1155     * color.
1156     */
1157    if (devinfo->ver <= 9 &&
1158        !formats_are_fast_clear_compatible(res->surf.format, view_format)) {
1159       clear_supported = false;
1160    }
1161 
1162    /* On gfx11+, the sampler reads clear values stored in pixel form.  The
1163     * location the sampler reads from is dependent on the bits-per-channel of
1164     * the format.  Specifically, a pixel is read from the Raw Clear Color
1165     * fields if the format is 32bpc.  Otherwise, it's read from the Converted
1166     * Clear Color fields.  To avoid modifying the clear color, disable it if
1167     * the new format points the sampler to an incompatible location.
1168     *
1169     * Note: although hardware looks at the bits-per-channel of the format, we
1170     * only need to check the red channel's size here.  In the scope of formats
1171     * supporting fast-clears, all 32bpc formats have 32-bit red channels and
1172     * vice-versa.
1173     */
1174    if (devinfo->ver >= 11 &&
1175        isl_format_get_layout(res->surf.format)->channels.r.bits != 32 &&
1176        isl_format_get_layout(view_format)->channels.r.bits == 32) {
1177       clear_supported = false;
1178    }
1179 
1180    /* On gfx12.0, the sampler has an issue with some 8 and 16bpp MSAA fast
1181     * clears.  See HSD 1707282275, wa_14013111325.  A simplified workaround is
1182     * implemented, but we could implement something more specific.
1183     */
1184    if (isl_aux_usage_has_mcs(aux_usage) &&
1185        intel_needs_workaround(devinfo, 14013111325) &&
1186        isl_format_get_layout(res->surf.format)->bpb <= 16) {
1187       clear_supported = false;
1188    }
1189 
1190    iris_resource_prepare_access(ice, res, start_level, num_levels,
1191                                 start_layer, num_layers,
1192                                 aux_usage, clear_supported);
1193 }
1194 
1195 /* Whether or not rendering a color value with either format results in the
1196  * same pixel. This can return false negatives.
1197  */
1198 bool
iris_render_formats_color_compatible(enum isl_format a,enum isl_format b,union isl_color_value color,bool clear_color_unknown)1199 iris_render_formats_color_compatible(enum isl_format a, enum isl_format b,
1200                                      union isl_color_value color,
1201                                      bool clear_color_unknown)
1202 {
1203    if (a == b)
1204       return true;
1205 
1206    /* A difference in color space doesn't matter for 0/1 values. */
1207    if (!clear_color_unknown &&
1208        isl_format_srgb_to_linear(a) == isl_format_srgb_to_linear(b) &&
1209        isl_color_value_is_zero_one(color, a)) {
1210       return true;
1211    }
1212 
1213    /* Both formats may interpret the clear color as zero. */
1214    if (!clear_color_unknown &&
1215        isl_color_value_is_zero(color, a) &&
1216        isl_color_value_is_zero(color, b)) {
1217       return true;
1218    }
1219 
1220    return false;
1221 }
1222 
1223 enum isl_aux_usage
iris_resource_render_aux_usage(struct iris_context * ice,struct iris_resource * res,enum isl_format render_format,uint32_t level,bool draw_aux_disabled)1224 iris_resource_render_aux_usage(struct iris_context *ice,
1225                                struct iris_resource *res,
1226                                enum isl_format render_format, uint32_t level,
1227                                bool draw_aux_disabled)
1228 {
1229    struct iris_screen *screen = (void *) ice->ctx.screen;
1230    const struct intel_device_info *devinfo = screen->devinfo;
1231 
1232    if (draw_aux_disabled)
1233       return ISL_AUX_USAGE_NONE;
1234 
1235    switch (res->aux.usage) {
1236    case ISL_AUX_USAGE_HIZ:
1237    case ISL_AUX_USAGE_HIZ_CCS:
1238    case ISL_AUX_USAGE_HIZ_CCS_WT:
1239       assert(render_format == res->surf.format);
1240       return iris_resource_level_has_hiz(devinfo, res, level) ?
1241              res->aux.usage : ISL_AUX_USAGE_NONE;
1242 
1243    case ISL_AUX_USAGE_STC_CCS:
1244       assert(render_format == res->surf.format);
1245       return res->aux.usage;
1246 
1247    case ISL_AUX_USAGE_MCS:
1248    case ISL_AUX_USAGE_MCS_CCS:
1249    case ISL_AUX_USAGE_CCS_D:
1250       return res->aux.usage;
1251 
1252    case ISL_AUX_USAGE_CCS_E:
1253    case ISL_AUX_USAGE_FCV_CCS_E:
1254       if (isl_formats_are_ccs_e_compatible(devinfo, res->surf.format,
1255                                            render_format)) {
1256          return res->aux.usage;
1257       }
1258       FALLTHROUGH;
1259 
1260    default:
1261       return ISL_AUX_USAGE_NONE;
1262    }
1263 }
1264 
1265 void
iris_resource_prepare_render(struct iris_context * ice,struct iris_resource * res,enum isl_format render_format,uint32_t level,uint32_t start_layer,uint32_t layer_count,enum isl_aux_usage aux_usage)1266 iris_resource_prepare_render(struct iris_context *ice,
1267                              struct iris_resource *res,
1268                              enum isl_format render_format, uint32_t level,
1269                              uint32_t start_layer, uint32_t layer_count,
1270                              enum isl_aux_usage aux_usage)
1271 {
1272    /* Replace the resource's clear color with zero if:
1273     *
1274     * - The resource's clear color is incompatible with render_format. This
1275     *   avoids corrupting current fast clear blocks and ensures any fast clear
1276     *   blocks generated as a result of the render will be recoverable.
1277     *
1278     * - The clear color struct is uninitialized and potentially inconsistent
1279     *   with itself. For non-32-bpc formats, the struct consists of different
1280     *   fields for rendering and sampling. If rendering can generate
1281     *   fast-cleared blocks, we want these to agree so that we can avoid
1282     *   partially resolving prior to sampling. Images with modifiers can be
1283     *   ignored. Either we will have already initialized their structs to
1284     *   zero, or they will have already been consistent at the time of import
1285     *   (as defined by drm_fourcc.h)
1286     *
1287     * The only aux usage which requires this process is FCV_CCS_E. Other aux
1288     * usages share a subset of these restrictions and benefit from only some
1289     * of the steps involved with changing the clear color. For now, just keep
1290     * things simple and assume we have the worst case usage of FCV_CCS_E.
1291     */
1292    if (!iris_render_formats_color_compatible(render_format,
1293                                              res->surf.format,
1294                                              res->aux.clear_color,
1295                                              res->aux.clear_color_unknown) ||
1296        (res->aux.clear_color_unknown && !res->mod_info &&
1297         isl_format_get_layout(render_format)->channels.r.bits != 32)) {
1298 
1299       /* Remove references to the clear color with resolves. */
1300       iris_resource_prepare_access(ice, res, 0, INTEL_REMAINING_LEVELS, 0,
1301                                    INTEL_REMAINING_LAYERS, res->aux.usage,
1302                                    false);
1303 
1304       /* The clear color is no longer in use; replace it now. */
1305       const union isl_color_value zero = { .u32 = { 0, } };
1306       iris_resource_set_clear_color(ice, res, zero);
1307 
1308       if (res->aux.clear_color_bo) {
1309          /* Update dwords used for rendering and sampling. */
1310          iris_emit_pipe_control_write(&ice->batches[IRIS_BATCH_RENDER],
1311                                       "zero fast clear color (RG____)",
1312                                       PIPE_CONTROL_WRITE_IMMEDIATE,
1313                                       res->aux.clear_color_bo,
1314                                       res->aux.clear_color_offset, 0);
1315 
1316          iris_emit_pipe_control_write(&ice->batches[IRIS_BATCH_RENDER],
1317                                       "zero fast clear color (__BA__)",
1318                                       PIPE_CONTROL_WRITE_IMMEDIATE,
1319                                       res->aux.clear_color_bo,
1320                                       res->aux.clear_color_offset + 8, 0);
1321 
1322          iris_emit_pipe_control_write(&ice->batches[IRIS_BATCH_RENDER],
1323                                       "zero fast clear color (____PX)",
1324                                       PIPE_CONTROL_WRITE_IMMEDIATE,
1325                                       res->aux.clear_color_bo,
1326                                       res->aux.clear_color_offset + 16, 0);
1327 
1328          iris_emit_pipe_control_flush(&ice->batches[IRIS_BATCH_RENDER],
1329                                       "new clear color affects state cache",
1330                                       PIPE_CONTROL_FLUSH_ENABLE |
1331                                       PIPE_CONTROL_STATE_CACHE_INVALIDATE);
1332       } else {
1333          /* Flag surface states with inline clear colors as dirty. */
1334          ice->state.stage_dirty |= IRIS_ALL_STAGE_DIRTY_BINDINGS;
1335       }
1336    }
1337 
1338    /* Now, do the preparation requested by the caller. Doing this after the
1339     * partial resolves above helps maintain the accuracy of the aux-usage
1340     * tracking that happens within the preparation function.
1341     */
1342    iris_resource_prepare_access(ice, res, level, 1, start_layer,
1343                                 layer_count, aux_usage,
1344                                 isl_aux_usage_has_fast_clears(aux_usage));
1345 }
1346 
1347 void
iris_resource_finish_render(struct iris_context * ice,struct iris_resource * res,uint32_t level,uint32_t start_layer,uint32_t layer_count,enum isl_aux_usage aux_usage)1348 iris_resource_finish_render(struct iris_context *ice,
1349                             struct iris_resource *res, uint32_t level,
1350                             uint32_t start_layer, uint32_t layer_count,
1351                             enum isl_aux_usage aux_usage)
1352 {
1353    iris_resource_finish_write(ice, res, level, start_layer, layer_count,
1354                               aux_usage);
1355 }
1356