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  * or shader image.  This causes a self-dependency, where both rendering
42  * and sampling may concurrently read or write the CCS buffer, causing
43  * incorrect pixels.
44  */
45 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)46 disable_rb_aux_buffer(struct iris_context *ice,
47                       bool *draw_aux_buffer_disabled,
48                       struct iris_resource *tex_res,
49                       unsigned min_level, unsigned num_levels,
50                       const char *usage)
51 {
52    struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
53    bool found = false;
54 
55    /* We only need to worry about color compression and fast clears. */
56    if (tex_res->aux.usage != ISL_AUX_USAGE_CCS_D &&
57        tex_res->aux.usage != ISL_AUX_USAGE_CCS_E &&
58        tex_res->aux.usage != ISL_AUX_USAGE_GEN12_CCS_E)
59       return false;
60 
61    for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {
62       struct iris_surface *surf = (void *) cso_fb->cbufs[i];
63       if (!surf)
64          continue;
65 
66       struct iris_resource *rb_res = (void *) surf->base.texture;
67 
68       if (rb_res->bo == tex_res->bo &&
69           surf->base.u.tex.level >= min_level &&
70           surf->base.u.tex.level < min_level + num_levels) {
71          found = draw_aux_buffer_disabled[i] = true;
72       }
73    }
74 
75    if (found) {
76       perf_debug(&ice->dbg,
77                  "Disabling CCS because a renderbuffer is also bound %s.\n",
78                  usage);
79    }
80 
81    return found;
82 }
83 
84 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)85 resolve_sampler_views(struct iris_context *ice,
86                       struct iris_batch *batch,
87                       struct iris_shader_state *shs,
88                       const struct shader_info *info,
89                       bool *draw_aux_buffer_disabled,
90                       bool consider_framebuffer)
91 {
92    uint32_t views = info ? (shs->bound_sampler_views & info->textures_used) : 0;
93 
94    while (views) {
95       const int i = u_bit_scan(&views);
96       struct iris_sampler_view *isv = shs->textures[i];
97       struct iris_resource *res = (void *) isv->base.texture;
98 
99       if (res->base.target != PIPE_BUFFER) {
100          if (consider_framebuffer) {
101             disable_rb_aux_buffer(ice, draw_aux_buffer_disabled,
102                                   res, isv->view.base_level, isv->view.levels,
103                                   "for sampling");
104          }
105 
106          iris_resource_prepare_texture(ice, res, isv->view.format,
107                                        isv->view.base_level, isv->view.levels,
108                                        isv->view.base_array_layer,
109                                        isv->view.array_len);
110       }
111 
112       iris_emit_buffer_barrier_for(batch, res->bo, IRIS_DOMAIN_OTHER_READ);
113    }
114 }
115 
116 static void
resolve_image_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)117 resolve_image_views(struct iris_context *ice,
118                     struct iris_batch *batch,
119                     struct iris_shader_state *shs,
120                     const struct shader_info *info,
121                     bool *draw_aux_buffer_disabled,
122                     bool consider_framebuffer)
123 {
124    uint32_t views = info ? (shs->bound_image_views & info->images_used) : 0;
125 
126    while (views) {
127       const int i = u_bit_scan(&views);
128       struct pipe_image_view *pview = &shs->image[i].base;
129       struct iris_resource *res = (void *) pview->resource;
130 
131       if (res->base.target != PIPE_BUFFER) {
132          if (consider_framebuffer) {
133             disable_rb_aux_buffer(ice, draw_aux_buffer_disabled,
134                                   res, pview->u.tex.level, 1,
135                                   "as a shader image");
136          }
137 
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          iris_resource_prepare_access(ice, res,
145                                       pview->u.tex.level, 1,
146                                       pview->u.tex.first_layer, num_layers,
147                                       aux_usage, false);
148       }
149 
150       iris_emit_buffer_barrier_for(batch, res->bo, IRIS_DOMAIN_OTHER_READ);
151    }
152 }
153 
154 
155 /**
156  * \brief Resolve buffers before drawing.
157  *
158  * Resolve the depth buffer's HiZ buffer, resolve the depth buffer of each
159  * enabled depth texture, and flush the render cache for any dirty textures.
160  */
161 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)162 iris_predraw_resolve_inputs(struct iris_context *ice,
163                             struct iris_batch *batch,
164                             bool *draw_aux_buffer_disabled,
165                             gl_shader_stage stage,
166                             bool consider_framebuffer)
167 {
168    struct iris_shader_state *shs = &ice->state.shaders[stage];
169    const struct shader_info *info = iris_get_shader_info(ice, stage);
170 
171    uint64_t stage_dirty = (IRIS_STAGE_DIRTY_BINDINGS_VS << stage) |
172       (consider_framebuffer ? IRIS_STAGE_DIRTY_BINDINGS_FS : 0);
173 
174    if (ice->state.stage_dirty & stage_dirty) {
175       resolve_sampler_views(ice, batch, shs, info, draw_aux_buffer_disabled,
176                             consider_framebuffer);
177       resolve_image_views(ice, batch, shs, info, draw_aux_buffer_disabled,
178                           consider_framebuffer);
179    }
180 }
181 
182 void
iris_predraw_resolve_framebuffer(struct iris_context * ice,struct iris_batch * batch,bool * draw_aux_buffer_disabled)183 iris_predraw_resolve_framebuffer(struct iris_context *ice,
184                                  struct iris_batch *batch,
185                                  bool *draw_aux_buffer_disabled)
186 {
187    struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
188    struct iris_screen *screen = (void *) ice->ctx.screen;
189    struct gen_device_info *devinfo = &screen->devinfo;
190    struct iris_uncompiled_shader *ish =
191       ice->shaders.uncompiled[MESA_SHADER_FRAGMENT];
192    const nir_shader *nir = ish->nir;
193 
194    if (ice->state.dirty & IRIS_DIRTY_DEPTH_BUFFER) {
195       struct pipe_surface *zs_surf = cso_fb->zsbuf;
196 
197       if (zs_surf) {
198          struct iris_resource *z_res, *s_res;
199          iris_get_depth_stencil_resources(zs_surf->texture, &z_res, &s_res);
200          unsigned num_layers =
201             zs_surf->u.tex.last_layer - zs_surf->u.tex.first_layer + 1;
202 
203          if (z_res) {
204             iris_resource_prepare_depth(ice, batch, z_res,
205                                         zs_surf->u.tex.level,
206                                         zs_surf->u.tex.first_layer,
207                                         num_layers);
208             iris_emit_buffer_barrier_for(batch, z_res->bo,
209                                          IRIS_DOMAIN_DEPTH_WRITE);
210          }
211 
212          if (s_res) {
213             iris_emit_buffer_barrier_for(batch, s_res->bo,
214                                          IRIS_DOMAIN_DEPTH_WRITE);
215          }
216       }
217    }
218 
219    if (devinfo->gen == 8 && nir->info.outputs_read != 0) {
220       for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {
221          if (cso_fb->cbufs[i]) {
222             struct iris_surface *surf = (void *) cso_fb->cbufs[i];
223             struct iris_resource *res = (void *) cso_fb->cbufs[i]->texture;
224 
225             iris_resource_prepare_texture(ice, res, surf->view.format,
226                                           surf->view.base_level, 1,
227                                           surf->view.base_array_layer,
228                                           surf->view.array_len);
229          }
230       }
231    }
232 
233    if (ice->state.stage_dirty & IRIS_STAGE_DIRTY_BINDINGS_FS) {
234       for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {
235          struct iris_surface *surf = (void *) cso_fb->cbufs[i];
236          if (!surf)
237             continue;
238 
239          struct iris_resource *res = (void *) surf->base.texture;
240 
241          enum isl_aux_usage aux_usage =
242             iris_resource_render_aux_usage(ice, res, surf->view.format,
243                                            draw_aux_buffer_disabled[i]);
244 
245          if (ice->state.draw_aux_usage[i] != aux_usage) {
246             ice->state.draw_aux_usage[i] = aux_usage;
247             /* XXX: Need to track which bindings to make dirty */
248             ice->state.dirty |= IRIS_DIRTY_RENDER_BUFFER;
249             ice->state.stage_dirty |= IRIS_ALL_STAGE_DIRTY_BINDINGS;
250          }
251 
252          iris_resource_prepare_render(ice, batch, res, surf->view.base_level,
253                                       surf->view.base_array_layer,
254                                       surf->view.array_len,
255                                       aux_usage);
256 
257          iris_cache_flush_for_render(batch, res->bo, surf->view.format,
258                                      aux_usage);
259       }
260    }
261 }
262 
263 /**
264  * \brief Call this after drawing to mark which buffers need resolving
265  *
266  * If the depth buffer was written to and if it has an accompanying HiZ
267  * buffer, then mark that it needs a depth resolve.
268  *
269  * If the color buffer is a multisample window system buffer, then
270  * mark that it needs a downsample.
271  *
272  * Also mark any render targets which will be textured as needing a render
273  * cache flush.
274  */
275 void
iris_postdraw_update_resolve_tracking(struct iris_context * ice,struct iris_batch * batch)276 iris_postdraw_update_resolve_tracking(struct iris_context *ice,
277                                       struct iris_batch *batch)
278 {
279    struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
280 
281    // XXX: front buffer drawing?
282 
283    bool may_have_resolved_depth =
284       ice->state.dirty & (IRIS_DIRTY_DEPTH_BUFFER |
285                           IRIS_DIRTY_WM_DEPTH_STENCIL);
286 
287    struct pipe_surface *zs_surf = cso_fb->zsbuf;
288    if (zs_surf) {
289       struct iris_resource *z_res, *s_res;
290       iris_get_depth_stencil_resources(zs_surf->texture, &z_res, &s_res);
291       unsigned num_layers =
292          zs_surf->u.tex.last_layer - zs_surf->u.tex.first_layer + 1;
293 
294       if (z_res) {
295          if (may_have_resolved_depth) {
296             iris_resource_finish_depth(ice, z_res, zs_surf->u.tex.level,
297                                        zs_surf->u.tex.first_layer, num_layers,
298                                        ice->state.depth_writes_enabled);
299          }
300       }
301 
302       if (s_res) {
303          if (may_have_resolved_depth && ice->state.stencil_writes_enabled) {
304             iris_resource_finish_write(ice, s_res, zs_surf->u.tex.level,
305                                        zs_surf->u.tex.first_layer, num_layers,
306                                        s_res->aux.usage);
307          }
308       }
309    }
310 
311    bool may_have_resolved_color =
312       ice->state.stage_dirty & IRIS_STAGE_DIRTY_BINDINGS_FS;
313 
314    for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {
315       struct iris_surface *surf = (void *) cso_fb->cbufs[i];
316       if (!surf)
317          continue;
318 
319       struct iris_resource *res = (void *) surf->base.texture;
320       enum isl_aux_usage aux_usage = ice->state.draw_aux_usage[i];
321 
322       if (may_have_resolved_color) {
323          union pipe_surface_desc *desc = &surf->base.u;
324          unsigned num_layers =
325             desc->tex.last_layer - desc->tex.first_layer + 1;
326          iris_resource_finish_render(ice, res, desc->tex.level,
327                                      desc->tex.first_layer, num_layers,
328                                      aux_usage);
329       }
330    }
331 }
332 
333 static void *
format_aux_tuple(enum isl_format format,enum isl_aux_usage aux_usage)334 format_aux_tuple(enum isl_format format, enum isl_aux_usage aux_usage)
335 {
336    return (void *)(uintptr_t)((uint32_t)format << 8 | aux_usage);
337 }
338 
339 void
iris_cache_flush_for_render(struct iris_batch * batch,struct iris_bo * bo,enum isl_format format,enum isl_aux_usage aux_usage)340 iris_cache_flush_for_render(struct iris_batch *batch,
341                             struct iris_bo *bo,
342                             enum isl_format format,
343                             enum isl_aux_usage aux_usage)
344 {
345    iris_emit_buffer_barrier_for(batch, bo, IRIS_DOMAIN_RENDER_WRITE);
346 
347    /* Check to see if this bo has been used by a previous rendering operation
348     * but with a different format or aux usage.  If it has, flush the render
349     * cache so we ensure that it's only in there with one format or aux usage
350     * at a time.
351     *
352     * Even though it's not obvious, this can easily happen in practice.
353     * Suppose a client is blending on a surface with sRGB encode enabled on
354     * gen9.  This implies that you get AUX_USAGE_CCS_D at best.  If the client
355     * then disables sRGB decode and continues blending we will flip on
356     * AUX_USAGE_CCS_E without doing any sort of resolve in-between (this is
357     * perfectly valid since CCS_E is a subset of CCS_D).  However, this means
358     * that we have fragments in-flight which are rendering with UNORM+CCS_E
359     * and other fragments in-flight with SRGB+CCS_D on the same surface at the
360     * same time and the pixel scoreboard and color blender are trying to sort
361     * it all out.  This ends badly (i.e. GPU hangs).
362     *
363     * To date, we have never observed GPU hangs or even corruption to be
364     * associated with switching the format, only the aux usage.  However,
365     * there are comments in various docs which indicate that the render cache
366     * isn't 100% resilient to format changes.  We may as well be conservative
367     * and flush on format changes too.  We can always relax this later if we
368     * find it to be a performance problem.
369     */
370    struct hash_entry *entry =
371       _mesa_hash_table_search_pre_hashed(batch->cache.render, bo->hash, bo);
372    if (!entry) {
373       _mesa_hash_table_insert_pre_hashed(batch->cache.render, bo->hash, bo,
374                                          format_aux_tuple(format, aux_usage));
375    } else if (entry->data != format_aux_tuple(format, aux_usage)) {
376       iris_emit_pipe_control_flush(batch,
377                                    "cache tracker: render format mismatch",
378                                    PIPE_CONTROL_RENDER_TARGET_FLUSH |
379                                    PIPE_CONTROL_CS_STALL);
380       entry->data = format_aux_tuple(format, aux_usage);
381    }
382 }
383 
384 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)385 iris_resolve_color(struct iris_context *ice,
386                    struct iris_batch *batch,
387                    struct iris_resource *res,
388                    unsigned level, unsigned layer,
389                    enum isl_aux_op resolve_op)
390 {
391    //DBG("%s to mt %p level %u layer %u\n", __FUNCTION__, mt, level, layer);
392 
393    struct blorp_surf surf;
394    iris_blorp_surf_for_resource(&batch->screen->isl_dev, &surf,
395                                 &res->base, res->aux.usage, level, true);
396 
397    iris_batch_maybe_flush(batch, 1500);
398 
399    /* Ivybridge PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":
400     *
401     *    "Any transition from any value in {Clear, Render, Resolve} to a
402     *     different value in {Clear, Render, Resolve} requires end of pipe
403     *     synchronization."
404     *
405     * In other words, fast clear ops are not properly synchronized with
406     * other drawing.  We need to use a PIPE_CONTROL to ensure that the
407     * contents of the previous draw hit the render target before we resolve
408     * and again afterwards to ensure that the resolve is complete before we
409     * do any more regular drawing.
410     */
411    iris_emit_end_of_pipe_sync(batch, "color resolve: pre-flush",
412                               PIPE_CONTROL_RENDER_TARGET_FLUSH);
413 
414    iris_batch_sync_region_start(batch);
415    struct blorp_batch blorp_batch;
416    blorp_batch_init(&ice->blorp, &blorp_batch, batch, 0);
417    /* On Gen >= 12, Stencil buffer with lossless compression needs to be
418     * resolve with WM_HZ_OP packet.
419     */
420    if (res->aux.usage == ISL_AUX_USAGE_STC_CCS) {
421       blorp_hiz_stencil_op(&blorp_batch, &surf, level, layer,
422                            1, resolve_op);
423    } else {
424       blorp_ccs_resolve(&blorp_batch, &surf, level, layer, 1,
425                         res->surf.format, resolve_op);
426    }
427    blorp_batch_finish(&blorp_batch);
428 
429    /* See comment above */
430    iris_emit_end_of_pipe_sync(batch, "color resolve: post-flush",
431                               PIPE_CONTROL_RENDER_TARGET_FLUSH);
432    iris_batch_sync_region_end(batch);
433 }
434 
435 static void
iris_mcs_partial_resolve(struct iris_context * ice,struct iris_batch * batch,struct iris_resource * res,uint32_t start_layer,uint32_t num_layers)436 iris_mcs_partial_resolve(struct iris_context *ice,
437                          struct iris_batch *batch,
438                          struct iris_resource *res,
439                          uint32_t start_layer,
440                          uint32_t num_layers)
441 {
442    //DBG("%s to mt %p layers %u-%u\n", __FUNCTION__, mt,
443        //start_layer, start_layer + num_layers - 1);
444 
445    assert(isl_aux_usage_has_mcs(res->aux.usage));
446 
447    struct blorp_surf surf;
448    iris_blorp_surf_for_resource(&batch->screen->isl_dev, &surf,
449                                 &res->base, res->aux.usage, 0, true);
450    iris_emit_buffer_barrier_for(batch, res->bo, IRIS_DOMAIN_RENDER_WRITE);
451 
452    struct blorp_batch blorp_batch;
453    iris_batch_sync_region_start(batch);
454    blorp_batch_init(&ice->blorp, &blorp_batch, batch, 0);
455    blorp_mcs_partial_resolve(&blorp_batch, &surf, res->surf.format,
456                              start_layer, num_layers);
457    blorp_batch_finish(&blorp_batch);
458    iris_batch_sync_region_end(batch);
459 }
460 
461 bool
iris_sample_with_depth_aux(const struct gen_device_info * devinfo,const struct iris_resource * res)462 iris_sample_with_depth_aux(const struct gen_device_info *devinfo,
463                            const struct iris_resource *res)
464 {
465    switch (res->aux.usage) {
466    case ISL_AUX_USAGE_HIZ:
467       if (devinfo->has_sample_with_hiz)
468          break;
469       return false;
470    case ISL_AUX_USAGE_HIZ_CCS:
471       return false;
472    case ISL_AUX_USAGE_HIZ_CCS_WT:
473       break;
474    default:
475       return false;
476    }
477 
478    /* It seems the hardware won't fallback to the depth buffer if some of the
479     * mipmap levels aren't available in the HiZ buffer. So we need all levels
480     * of the texture to be HiZ enabled.
481     */
482    for (unsigned level = 0; level < res->surf.levels; ++level) {
483       if (!iris_resource_level_has_hiz(res, level))
484          return false;
485    }
486 
487    /* If compressed multisampling is enabled, then we use it for the auxiliary
488     * buffer instead.
489     *
490     * From the BDW PRM (Volume 2d: Command Reference: Structures
491     *                   RENDER_SURFACE_STATE.AuxiliarySurfaceMode):
492     *
493     *  "If this field is set to AUX_HIZ, Number of Multisamples must be
494     *   MULTISAMPLECOUNT_1, and Surface Type cannot be SURFTYPE_3D.
495     *
496     * There is no such blurb for 1D textures, but there is sufficient evidence
497     * that this is broken on SKL+.
498     */
499    // XXX: i965 disables this for arrays too, is that reasonable?
500    return res->surf.samples == 1 && res->surf.dim == ISL_SURF_DIM_2D;
501 }
502 
503 /**
504  * Perform a HiZ or depth resolve operation.
505  *
506  * For an overview of HiZ ops, see the following sections of the Sandy Bridge
507  * PRM, Volume 1, Part 2:
508  *   - 7.5.3.1 Depth Buffer Clear
509  *   - 7.5.3.2 Depth Buffer Resolve
510  *   - 7.5.3.3 Hierarchical Depth Buffer Resolve
511  */
512 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)513 iris_hiz_exec(struct iris_context *ice,
514               struct iris_batch *batch,
515               struct iris_resource *res,
516               unsigned int level, unsigned int start_layer,
517               unsigned int num_layers, enum isl_aux_op op,
518               bool update_clear_depth)
519 {
520    assert(iris_resource_level_has_hiz(res, level));
521    assert(op != ISL_AUX_OP_NONE);
522    UNUSED const char *name = NULL;
523 
524    switch (op) {
525    case ISL_AUX_OP_FULL_RESOLVE:
526       name = "depth resolve";
527       break;
528    case ISL_AUX_OP_AMBIGUATE:
529       name = "hiz ambiguate";
530       break;
531    case ISL_AUX_OP_FAST_CLEAR:
532       name = "depth clear";
533       break;
534    case ISL_AUX_OP_PARTIAL_RESOLVE:
535    case ISL_AUX_OP_NONE:
536       unreachable("Invalid HiZ op");
537    }
538 
539    //DBG("%s %s to mt %p level %d layers %d-%d\n",
540        //__func__, name, mt, level, start_layer, start_layer + num_layers - 1);
541 
542    /* The following stalls and flushes are only documented to be required
543     * for HiZ clear operations.  However, they also seem to be required for
544     * resolve operations.
545     *
546     * From the Ivybridge PRM, volume 2, "Depth Buffer Clear":
547     *
548     *   "If other rendering operations have preceded this clear, a
549     *    PIPE_CONTROL with depth cache flush enabled, Depth Stall bit
550     *    enabled must be issued before the rectangle primitive used for
551     *    the depth buffer clear operation."
552     *
553     * Same applies for Gen8 and Gen9.
554     *
555     * In addition, from the Ivybridge PRM, volume 2, 1.10.4.1
556     * PIPE_CONTROL, Depth Cache Flush Enable:
557     *
558     *   "This bit must not be set when Depth Stall Enable bit is set in
559     *    this packet."
560     *
561     * This is confirmed to hold for real, Haswell gets immediate gpu hangs.
562     *
563     * Therefore issue two pipe control flushes, one for cache flush and
564     * another for depth stall.
565     */
566    iris_emit_pipe_control_flush(batch,
567                                 "hiz op: pre-flushes (1/2)",
568                                 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
569                                 PIPE_CONTROL_CS_STALL);
570 
571    iris_emit_pipe_control_flush(batch, "hiz op: pre-flushes (2/2)",
572                                 PIPE_CONTROL_DEPTH_STALL);
573 
574    assert(isl_aux_usage_has_hiz(res->aux.usage) && res->aux.bo);
575 
576    iris_batch_maybe_flush(batch, 1500);
577 
578    iris_batch_sync_region_start(batch);
579 
580    struct blorp_surf surf;
581    iris_blorp_surf_for_resource(&batch->screen->isl_dev, &surf,
582                                 &res->base, res->aux.usage, level, true);
583 
584    struct blorp_batch blorp_batch;
585    enum blorp_batch_flags flags = 0;
586    flags |= update_clear_depth ? 0 : BLORP_BATCH_NO_UPDATE_CLEAR_COLOR;
587    blorp_batch_init(&ice->blorp, &blorp_batch, batch, flags);
588    blorp_hiz_op(&blorp_batch, &surf, level, start_layer, num_layers, op);
589    blorp_batch_finish(&blorp_batch);
590 
591    /* The following stalls and flushes are only documented to be required
592     * for HiZ clear operations.  However, they also seem to be required for
593     * resolve operations.
594     *
595     * From the Broadwell PRM, volume 7, "Depth Buffer Clear":
596     *
597     *    "Depth buffer clear pass using any of the methods (WM_STATE,
598     *     3DSTATE_WM or 3DSTATE_WM_HZ_OP) must be followed by a
599     *     PIPE_CONTROL command with DEPTH_STALL bit and Depth FLUSH bits
600     *     "set" before starting to render.  DepthStall and DepthFlush are
601     *     not needed between consecutive depth clear passes nor is it
602     *     required if the depth clear pass was done with
603     *     'full_surf_clear' bit set in the 3DSTATE_WM_HZ_OP."
604     *
605     * TODO: Such as the spec says, this could be conditional.
606     */
607    iris_emit_pipe_control_flush(batch,
608                                 "hiz op: post flush",
609                                 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
610                                 PIPE_CONTROL_DEPTH_STALL);
611 
612    iris_batch_sync_region_end(batch);
613 }
614 
615 static bool
level_has_aux(const struct iris_resource * res,uint32_t level)616 level_has_aux(const struct iris_resource *res, uint32_t level)
617 {
618    return isl_aux_usage_has_hiz(res->aux.usage) ?
619           iris_resource_level_has_hiz(res, level) :
620           res->aux.usage != ISL_AUX_USAGE_NONE;
621 }
622 
623 /**
624  * Does the resource's slice have hiz enabled?
625  */
626 bool
iris_resource_level_has_hiz(const struct iris_resource * res,uint32_t level)627 iris_resource_level_has_hiz(const struct iris_resource *res, uint32_t level)
628 {
629    iris_resource_check_level_layer(res, level, 0);
630    return res->aux.has_hiz & 1 << level;
631 }
632 
633 /** \brief Assert that the level and layer are valid for the resource. */
634 void
iris_resource_check_level_layer(UNUSED const struct iris_resource * res,UNUSED uint32_t level,UNUSED uint32_t layer)635 iris_resource_check_level_layer(UNUSED const struct iris_resource *res,
636                                 UNUSED uint32_t level, UNUSED uint32_t layer)
637 {
638    assert(level < res->surf.levels);
639    assert(layer < util_num_layers(&res->base, level));
640 }
641 
642 static inline uint32_t
miptree_level_range_length(const struct iris_resource * res,uint32_t start_level,uint32_t num_levels)643 miptree_level_range_length(const struct iris_resource *res,
644                            uint32_t start_level, uint32_t num_levels)
645 {
646    assert(start_level < res->surf.levels);
647 
648    if (num_levels == INTEL_REMAINING_LAYERS)
649       num_levels = res->surf.levels;
650 
651    /* Check for overflow */
652    assert(start_level + num_levels >= start_level);
653    assert(start_level + num_levels <= res->surf.levels);
654 
655    return num_levels;
656 }
657 
658 static inline uint32_t
miptree_layer_range_length(const struct iris_resource * res,uint32_t level,uint32_t start_layer,uint32_t num_layers)659 miptree_layer_range_length(const struct iris_resource *res, uint32_t level,
660                            uint32_t start_layer, uint32_t num_layers)
661 {
662    assert(level <= res->base.last_level);
663 
664    const uint32_t total_num_layers = iris_get_num_logical_layers(res, level);
665    assert(start_layer < total_num_layers);
666    if (num_layers == INTEL_REMAINING_LAYERS)
667       num_layers = total_num_layers - start_layer;
668    /* Check for overflow */
669    assert(start_layer + num_layers >= start_layer);
670    assert(start_layer + num_layers <= total_num_layers);
671 
672    return num_layers;
673 }
674 
675 bool
iris_has_color_unresolved(const struct iris_resource * res,unsigned start_level,unsigned num_levels,unsigned start_layer,unsigned num_layers)676 iris_has_color_unresolved(const struct iris_resource *res,
677                           unsigned start_level, unsigned num_levels,
678                           unsigned start_layer, unsigned num_layers)
679 {
680    if (!res->aux.bo)
681       return false;
682 
683    /* Clamp the level range to fit the resource */
684    num_levels = miptree_level_range_length(res, start_level, num_levels);
685 
686    for (uint32_t l = 0; l < num_levels; l++) {
687       const uint32_t level = start_level + l;
688       const uint32_t level_layers =
689          miptree_layer_range_length(res, level, start_layer, num_layers);
690       for (unsigned a = 0; a < level_layers; a++) {
691          enum isl_aux_state aux_state =
692             iris_resource_get_aux_state(res, level, start_layer + a);
693          assert(aux_state != ISL_AUX_STATE_AUX_INVALID);
694          if (aux_state != ISL_AUX_STATE_PASS_THROUGH)
695             return true;
696       }
697    }
698 
699    return false;
700 }
701 
702 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)703 iris_resource_prepare_access(struct iris_context *ice,
704                              struct iris_resource *res,
705                              uint32_t start_level, uint32_t num_levels,
706                              uint32_t start_layer, uint32_t num_layers,
707                              enum isl_aux_usage aux_usage,
708                              bool fast_clear_supported)
709 {
710    /* We can't do resolves on the compute engine, so awkwardly, we have to
711     * do them on the render batch...
712     */
713    struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
714 
715    const uint32_t clamped_levels =
716       miptree_level_range_length(res, start_level, num_levels);
717    for (uint32_t l = 0; l < clamped_levels; l++) {
718       const uint32_t level = start_level + l;
719       if (!level_has_aux(res, level))
720          continue;
721 
722       const uint32_t level_layers =
723          miptree_layer_range_length(res, level, start_layer, num_layers);
724       for (uint32_t a = 0; a < level_layers; a++) {
725          const uint32_t layer = start_layer + a;
726          const enum isl_aux_state aux_state =
727             iris_resource_get_aux_state(res, level, layer);
728          const enum isl_aux_op aux_op =
729             isl_aux_prepare_access(aux_state, aux_usage, fast_clear_supported);
730 
731          if (aux_op == ISL_AUX_OP_NONE) {
732             /* Nothing to do here. */
733          } else if (isl_aux_usage_has_mcs(res->aux.usage)) {
734             assert(aux_op == ISL_AUX_OP_PARTIAL_RESOLVE);
735             iris_mcs_partial_resolve(ice, batch, res, layer, 1);
736          } else if (isl_aux_usage_has_hiz(res->aux.usage)) {
737             iris_hiz_exec(ice, batch, res, level, layer, 1, aux_op, false);
738          } else {
739             assert(isl_aux_usage_has_ccs(res->aux.usage));
740             iris_resolve_color(ice, batch, res, level, layer, aux_op);
741          }
742 
743          const enum isl_aux_state new_state =
744             isl_aux_state_transition_aux_op(aux_state, res->aux.usage, aux_op);
745          iris_resource_set_aux_state(ice, res, level, layer, 1, new_state);
746       }
747    }
748 }
749 
750 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)751 iris_resource_finish_write(struct iris_context *ice,
752                            struct iris_resource *res, uint32_t level,
753                            uint32_t start_layer, uint32_t num_layers,
754                            enum isl_aux_usage aux_usage)
755 {
756    if (!level_has_aux(res, level))
757       return;
758 
759    const uint32_t level_layers =
760       miptree_layer_range_length(res, level, start_layer, num_layers);
761 
762    for (uint32_t a = 0; a < level_layers; a++) {
763       const uint32_t layer = start_layer + a;
764       const enum isl_aux_state aux_state =
765          iris_resource_get_aux_state(res, level, layer);
766       const enum isl_aux_state new_aux_state =
767          isl_aux_state_transition_write(aux_state, aux_usage, false);
768       iris_resource_set_aux_state(ice, res, level, layer, 1, new_aux_state);
769    }
770 }
771 
772 enum isl_aux_state
iris_resource_get_aux_state(const struct iris_resource * res,uint32_t level,uint32_t layer)773 iris_resource_get_aux_state(const struct iris_resource *res,
774                             uint32_t level, uint32_t layer)
775 {
776    iris_resource_check_level_layer(res, level, layer);
777 
778    if (res->surf.usage & ISL_SURF_USAGE_DEPTH_BIT) {
779       assert(iris_resource_level_has_hiz(res, level));
780    } else {
781       assert(res->surf.samples == 1 ||
782              res->surf.msaa_layout == ISL_MSAA_LAYOUT_ARRAY);
783    }
784 
785    return res->aux.state[level][layer];
786 }
787 
788 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)789 iris_resource_set_aux_state(struct iris_context *ice,
790                             struct iris_resource *res, uint32_t level,
791                             uint32_t start_layer, uint32_t num_layers,
792                             enum isl_aux_state aux_state)
793 {
794    num_layers = miptree_layer_range_length(res, level, start_layer, num_layers);
795 
796    if (res->surf.usage & ISL_SURF_USAGE_DEPTH_BIT) {
797       assert(iris_resource_level_has_hiz(res, level));
798    } else {
799       assert(res->surf.samples == 1 ||
800              res->surf.msaa_layout == ISL_MSAA_LAYOUT_ARRAY);
801    }
802 
803    for (unsigned a = 0; a < num_layers; a++) {
804       if (res->aux.state[level][start_layer + a] != aux_state) {
805          res->aux.state[level][start_layer + a] = aux_state;
806          /* XXX: Need to track which bindings to make dirty */
807          ice->state.dirty |= IRIS_DIRTY_RENDER_BUFFER;
808          ice->state.stage_dirty |= IRIS_ALL_STAGE_DIRTY_BINDINGS;
809       }
810    }
811 
812    if (res->mod_info && !res->mod_info->supports_clear_color) {
813       assert(res->mod_info->aux_usage != ISL_AUX_USAGE_NONE);
814       if (aux_state == ISL_AUX_STATE_CLEAR ||
815           aux_state == ISL_AUX_STATE_COMPRESSED_CLEAR ||
816           aux_state == ISL_AUX_STATE_PARTIAL_CLEAR) {
817          iris_mark_dirty_dmabuf(ice, &res->base);
818       }
819    }
820 }
821 
822 enum isl_aux_usage
iris_resource_texture_aux_usage(struct iris_context * ice,const struct iris_resource * res,enum isl_format view_format)823 iris_resource_texture_aux_usage(struct iris_context *ice,
824                                 const struct iris_resource *res,
825                                 enum isl_format view_format)
826 {
827    struct iris_screen *screen = (void *) ice->ctx.screen;
828    struct gen_device_info *devinfo = &screen->devinfo;
829 
830    switch (res->aux.usage) {
831    case ISL_AUX_USAGE_HIZ:
832       if (iris_sample_with_depth_aux(devinfo, res))
833          return ISL_AUX_USAGE_HIZ;
834       break;
835 
836    case ISL_AUX_USAGE_HIZ_CCS:
837       assert(!iris_sample_with_depth_aux(devinfo, res));
838       return ISL_AUX_USAGE_NONE;
839 
840    case ISL_AUX_USAGE_HIZ_CCS_WT:
841       if (iris_sample_with_depth_aux(devinfo, res))
842          return ISL_AUX_USAGE_HIZ_CCS_WT;
843       break;
844 
845    case ISL_AUX_USAGE_MCS:
846    case ISL_AUX_USAGE_MCS_CCS:
847    case ISL_AUX_USAGE_STC_CCS:
848       return res->aux.usage;
849 
850    case ISL_AUX_USAGE_CCS_E:
851    case ISL_AUX_USAGE_GEN12_CCS_E:
852       /* If we don't have any unresolved color, report an aux usage of
853        * ISL_AUX_USAGE_NONE.  This way, texturing won't even look at the
854        * aux surface and we can save some bandwidth.
855        */
856       if (!iris_has_color_unresolved(res, 0, INTEL_REMAINING_LEVELS,
857                                      0, INTEL_REMAINING_LAYERS))
858          return ISL_AUX_USAGE_NONE;
859 
860       /* On Gen9 color buffers may be compressed by the hardware (lossless
861        * compression). There are, however, format restrictions and care needs
862        * to be taken that the sampler engine is capable for re-interpreting a
863        * buffer with format different the buffer was originally written with.
864        *
865        * For example, SRGB formats are not compressible and the sampler engine
866        * isn't capable of treating RGBA_UNORM as SRGB_ALPHA. In such a case
867        * the underlying color buffer needs to be resolved so that the sampling
868        * surface can be sampled as non-compressed (i.e., without the auxiliary
869        * MCS buffer being set).
870        */
871       if (isl_formats_are_ccs_e_compatible(devinfo, res->surf.format,
872                                            view_format))
873          return res->aux.usage;
874       break;
875 
876    default:
877       break;
878    }
879 
880    return ISL_AUX_USAGE_NONE;
881 }
882 
883 enum isl_aux_usage
iris_image_view_aux_usage(struct iris_context * ice,const struct pipe_image_view * pview,const struct shader_info * info)884 iris_image_view_aux_usage(struct iris_context *ice,
885                           const struct pipe_image_view *pview,
886                           const struct shader_info *info)
887 {
888    if (!info)
889       return ISL_AUX_USAGE_NONE;
890 
891    struct iris_resource *res = (void *) pview->resource;
892 
893    enum isl_format view_format = iris_image_view_get_format(ice, pview);
894    enum isl_aux_usage aux_usage =
895       iris_resource_texture_aux_usage(ice, res, view_format);
896 
897    bool uses_atomic_load_store =
898       ice->shaders.uncompiled[info->stage]->uses_atomic_load_store;
899 
900    if (aux_usage == ISL_AUX_USAGE_GEN12_CCS_E && !uses_atomic_load_store)
901       return ISL_AUX_USAGE_GEN12_CCS_E;
902 
903    return ISL_AUX_USAGE_NONE;
904 }
905 
906 static bool
isl_formats_are_fast_clear_compatible(enum isl_format a,enum isl_format b)907 isl_formats_are_fast_clear_compatible(enum isl_format a, enum isl_format b)
908 {
909    /* On gen8 and earlier, the hardware was only capable of handling 0/1 clear
910     * values so sRGB curve application was a no-op for all fast-clearable
911     * formats.
912     *
913     * On gen9+, the hardware supports arbitrary clear values.  For sRGB clear
914     * values, the hardware interprets the floats, not as what would be
915     * returned from the sampler (or written by the shader), but as being
916     * between format conversion and sRGB curve application.  This means that
917     * we can switch between sRGB and UNORM without having to whack the clear
918     * color.
919     */
920    return isl_format_srgb_to_linear(a) == isl_format_srgb_to_linear(b);
921 }
922 
923 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)924 iris_resource_prepare_texture(struct iris_context *ice,
925                               struct iris_resource *res,
926                               enum isl_format view_format,
927                               uint32_t start_level, uint32_t num_levels,
928                               uint32_t start_layer, uint32_t num_layers)
929 {
930    enum isl_aux_usage aux_usage =
931       iris_resource_texture_aux_usage(ice, res, view_format);
932 
933    bool clear_supported = isl_aux_usage_has_fast_clears(aux_usage);
934 
935    /* Clear color is specified as ints or floats and the conversion is done by
936     * the sampler.  If we have a texture view, we would have to perform the
937     * clear color conversion manually.  Just disable clear color.
938     */
939    if (!isl_formats_are_fast_clear_compatible(res->surf.format, view_format))
940       clear_supported = false;
941 
942    iris_resource_prepare_access(ice, res, start_level, num_levels,
943                                 start_layer, num_layers,
944                                 aux_usage, clear_supported);
945 }
946 
947 /* Whether or not rendering a color value with either format results in the
948  * same pixel. This can return false negatives.
949  */
950 bool
iris_render_formats_color_compatible(enum isl_format a,enum isl_format b,union isl_color_value color)951 iris_render_formats_color_compatible(enum isl_format a, enum isl_format b,
952                                      union isl_color_value color)
953 {
954    if (a == b)
955       return true;
956 
957    /* A difference in color space doesn't matter for 0/1 values. */
958    if (isl_format_srgb_to_linear(a) == isl_format_srgb_to_linear(b) &&
959        isl_color_value_is_zero_one(color, a)) {
960       return true;
961    }
962 
963    return false;
964 }
965 
966 enum isl_aux_usage
iris_resource_render_aux_usage(struct iris_context * ice,struct iris_resource * res,enum isl_format render_format,bool draw_aux_disabled)967 iris_resource_render_aux_usage(struct iris_context *ice,
968                                struct iris_resource *res,
969                                enum isl_format render_format,
970                                bool draw_aux_disabled)
971 {
972    struct iris_screen *screen = (void *) ice->ctx.screen;
973    struct gen_device_info *devinfo = &screen->devinfo;
974 
975    if (draw_aux_disabled)
976       return ISL_AUX_USAGE_NONE;
977 
978    switch (res->aux.usage) {
979    case ISL_AUX_USAGE_MCS:
980    case ISL_AUX_USAGE_MCS_CCS:
981       return res->aux.usage;
982 
983    case ISL_AUX_USAGE_CCS_D:
984    case ISL_AUX_USAGE_CCS_E:
985    case ISL_AUX_USAGE_GEN12_CCS_E:
986       /* Disable CCS for some cases of texture-view rendering. On gen12, HW
987        * may convert some subregions of shader output to fast-cleared blocks
988        * if CCS is enabled and the shader output matches the clear color.
989        * Existing fast-cleared blocks are correctly interpreted by the clear
990        * color and the resource format (see can_fast_clear_color). To avoid
991        * gaining new fast-cleared blocks that can't be interpreted by the
992        * resource format (and to avoid misinterpreting existing ones), shut
993        * off CCS when the interpretation of the clear color differs between
994        * the render_format and the resource format.
995        */
996       if (!iris_render_formats_color_compatible(render_format,
997                                                 res->surf.format,
998                                                 res->aux.clear_color)) {
999          return ISL_AUX_USAGE_NONE;
1000       }
1001 
1002       if (res->aux.usage == ISL_AUX_USAGE_CCS_D)
1003          return ISL_AUX_USAGE_CCS_D;
1004 
1005       if (isl_formats_are_ccs_e_compatible(devinfo, res->surf.format,
1006                                            render_format)) {
1007          return res->aux.usage;
1008       }
1009       /* fallthrough */
1010 
1011    default:
1012       return ISL_AUX_USAGE_NONE;
1013    }
1014 }
1015 
1016 void
iris_resource_prepare_render(struct iris_context * ice,struct iris_batch * batch,struct iris_resource * res,uint32_t level,uint32_t start_layer,uint32_t layer_count,enum isl_aux_usage aux_usage)1017 iris_resource_prepare_render(struct iris_context *ice,
1018                              struct iris_batch *batch,
1019                              struct iris_resource *res, uint32_t level,
1020                              uint32_t start_layer, uint32_t layer_count,
1021                              enum isl_aux_usage aux_usage)
1022 {
1023    iris_resource_prepare_access(ice, res, level, 1, start_layer,
1024                                 layer_count, aux_usage,
1025                                 isl_aux_usage_has_fast_clears(aux_usage));
1026 }
1027 
1028 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)1029 iris_resource_finish_render(struct iris_context *ice,
1030                             struct iris_resource *res, uint32_t level,
1031                             uint32_t start_layer, uint32_t layer_count,
1032                             enum isl_aux_usage aux_usage)
1033 {
1034    iris_resource_finish_write(ice, res, level, start_layer, layer_count,
1035                               aux_usage);
1036 }
1037 
1038 void
iris_resource_prepare_depth(struct iris_context * ice,struct iris_batch * batch,struct iris_resource * res,uint32_t level,uint32_t start_layer,uint32_t layer_count)1039 iris_resource_prepare_depth(struct iris_context *ice,
1040                             struct iris_batch *batch,
1041                             struct iris_resource *res, uint32_t level,
1042                             uint32_t start_layer, uint32_t layer_count)
1043 {
1044    iris_resource_prepare_access(ice, res, level, 1, start_layer,
1045                                 layer_count, res->aux.usage, !!res->aux.bo);
1046 }
1047 
1048 void
iris_resource_finish_depth(struct iris_context * ice,struct iris_resource * res,uint32_t level,uint32_t start_layer,uint32_t layer_count,bool depth_written)1049 iris_resource_finish_depth(struct iris_context *ice,
1050                            struct iris_resource *res, uint32_t level,
1051                            uint32_t start_layer, uint32_t layer_count,
1052                            bool depth_written)
1053 {
1054    if (depth_written) {
1055       iris_resource_finish_write(ice, res, level, start_layer, layer_count,
1056                                  res->aux.usage);
1057    }
1058 }
1059