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 = isv->res;
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 iris_emit_pipe_control_flush(batch,
556 "hiz op: pre-flush",
557 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
558 PIPE_CONTROL_DEPTH_STALL |
559 PIPE_CONTROL_CS_STALL);
560
561 assert(isl_aux_usage_has_hiz(res->aux.usage) && res->aux.bo);
562
563 iris_batch_maybe_flush(batch, 1500);
564
565 iris_batch_sync_region_start(batch);
566
567 struct blorp_surf surf;
568 iris_blorp_surf_for_resource(&batch->screen->isl_dev, &surf,
569 &res->base, res->aux.usage, level, true);
570
571 struct blorp_batch blorp_batch;
572 enum blorp_batch_flags flags = 0;
573 flags |= update_clear_depth ? 0 : BLORP_BATCH_NO_UPDATE_CLEAR_COLOR;
574 blorp_batch_init(&ice->blorp, &blorp_batch, batch, flags);
575 blorp_hiz_op(&blorp_batch, &surf, level, start_layer, num_layers, op);
576 blorp_batch_finish(&blorp_batch);
577
578 /* The following stalls and flushes are only documented to be required
579 * for HiZ clear operations. However, they also seem to be required for
580 * resolve operations.
581 *
582 * From the Broadwell PRM, volume 7, "Depth Buffer Clear":
583 *
584 * "Depth buffer clear pass using any of the methods (WM_STATE,
585 * 3DSTATE_WM or 3DSTATE_WM_HZ_OP) must be followed by a
586 * PIPE_CONTROL command with DEPTH_STALL bit and Depth FLUSH bits
587 * "set" before starting to render. DepthStall and DepthFlush are
588 * not needed between consecutive depth clear passes nor is it
589 * required if the depth clear pass was done with
590 * 'full_surf_clear' bit set in the 3DSTATE_WM_HZ_OP."
591 *
592 * TODO: Such as the spec says, this could be conditional.
593 */
594 iris_emit_pipe_control_flush(batch,
595 "hiz op: post flush",
596 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
597 PIPE_CONTROL_DEPTH_STALL);
598
599 iris_batch_sync_region_end(batch);
600 }
601
602 static bool
level_has_aux(const struct iris_resource * res,uint32_t level)603 level_has_aux(const struct iris_resource *res, uint32_t level)
604 {
605 return isl_aux_usage_has_hiz(res->aux.usage) ?
606 iris_resource_level_has_hiz(res, level) :
607 res->aux.usage != ISL_AUX_USAGE_NONE;
608 }
609
610 /**
611 * Does the resource's slice have hiz enabled?
612 */
613 bool
iris_resource_level_has_hiz(const struct iris_resource * res,uint32_t level)614 iris_resource_level_has_hiz(const struct iris_resource *res, uint32_t level)
615 {
616 iris_resource_check_level_layer(res, level, 0);
617 return res->aux.has_hiz & 1 << level;
618 }
619
620 /** \brief Assert that the level and layer are valid for the resource. */
621 void
iris_resource_check_level_layer(UNUSED const struct iris_resource * res,UNUSED uint32_t level,UNUSED uint32_t layer)622 iris_resource_check_level_layer(UNUSED const struct iris_resource *res,
623 UNUSED uint32_t level, UNUSED uint32_t layer)
624 {
625 assert(level < res->surf.levels);
626 assert(layer < util_num_layers(&res->base, level));
627 }
628
629 static inline uint32_t
miptree_level_range_length(const struct iris_resource * res,uint32_t start_level,uint32_t num_levels)630 miptree_level_range_length(const struct iris_resource *res,
631 uint32_t start_level, uint32_t num_levels)
632 {
633 assert(start_level < res->surf.levels);
634
635 if (num_levels == INTEL_REMAINING_LAYERS)
636 num_levels = res->surf.levels;
637
638 /* Check for overflow */
639 assert(start_level + num_levels >= start_level);
640 assert(start_level + num_levels <= res->surf.levels);
641
642 return num_levels;
643 }
644
645 static inline uint32_t
miptree_layer_range_length(const struct iris_resource * res,uint32_t level,uint32_t start_layer,uint32_t num_layers)646 miptree_layer_range_length(const struct iris_resource *res, uint32_t level,
647 uint32_t start_layer, uint32_t num_layers)
648 {
649 assert(level <= res->base.last_level);
650
651 const uint32_t total_num_layers = iris_get_num_logical_layers(res, level);
652 assert(start_layer < total_num_layers);
653 if (num_layers == INTEL_REMAINING_LAYERS)
654 num_layers = total_num_layers - start_layer;
655 /* Check for overflow */
656 assert(start_layer + num_layers >= start_layer);
657 assert(start_layer + num_layers <= total_num_layers);
658
659 return num_layers;
660 }
661
662 bool
iris_has_invalid_primary(const struct iris_resource * res,unsigned start_level,unsigned num_levels,unsigned start_layer,unsigned num_layers)663 iris_has_invalid_primary(const struct iris_resource *res,
664 unsigned start_level, unsigned num_levels,
665 unsigned start_layer, unsigned num_layers)
666 {
667 if (!res->aux.bo)
668 return false;
669
670 /* Clamp the level range to fit the resource */
671 num_levels = miptree_level_range_length(res, start_level, num_levels);
672
673 for (uint32_t l = 0; l < num_levels; l++) {
674 const uint32_t level = start_level + l;
675 if (!level_has_aux(res, level))
676 continue;
677
678 const uint32_t level_layers =
679 miptree_layer_range_length(res, level, start_layer, num_layers);
680 for (unsigned a = 0; a < level_layers; a++) {
681 enum isl_aux_state aux_state =
682 iris_resource_get_aux_state(res, level, start_layer + a);
683 if (!isl_aux_state_has_valid_primary(aux_state))
684 return true;
685 }
686 }
687
688 return false;
689 }
690
691 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)692 iris_resource_prepare_access(struct iris_context *ice,
693 struct iris_resource *res,
694 uint32_t start_level, uint32_t num_levels,
695 uint32_t start_layer, uint32_t num_layers,
696 enum isl_aux_usage aux_usage,
697 bool fast_clear_supported)
698 {
699 /* We can't do resolves on the compute engine, so awkwardly, we have to
700 * do them on the render batch...
701 */
702 struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
703
704 const uint32_t clamped_levels =
705 miptree_level_range_length(res, start_level, num_levels);
706 for (uint32_t l = 0; l < clamped_levels; l++) {
707 const uint32_t level = start_level + l;
708 if (!level_has_aux(res, level))
709 continue;
710
711 const uint32_t level_layers =
712 miptree_layer_range_length(res, level, start_layer, num_layers);
713 for (uint32_t a = 0; a < level_layers; a++) {
714 const uint32_t layer = start_layer + a;
715 const enum isl_aux_state aux_state =
716 iris_resource_get_aux_state(res, level, layer);
717 const enum isl_aux_op aux_op =
718 isl_aux_prepare_access(aux_state, aux_usage, fast_clear_supported);
719
720 if (aux_op == ISL_AUX_OP_NONE) {
721 /* Nothing to do here. */
722 } else if (isl_aux_usage_has_mcs(res->aux.usage)) {
723 assert(aux_op == ISL_AUX_OP_PARTIAL_RESOLVE);
724 iris_mcs_partial_resolve(ice, batch, res, layer, 1);
725 } else if (isl_aux_usage_has_hiz(res->aux.usage)) {
726 iris_hiz_exec(ice, batch, res, level, layer, 1, aux_op, false);
727 } else {
728 assert(isl_aux_usage_has_ccs(res->aux.usage));
729 iris_resolve_color(ice, batch, res, level, layer, aux_op);
730 }
731
732 const enum isl_aux_state new_state =
733 isl_aux_state_transition_aux_op(aux_state, res->aux.usage, aux_op);
734 iris_resource_set_aux_state(ice, res, level, layer, 1, new_state);
735 }
736 }
737 }
738
739 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)740 iris_resource_finish_write(struct iris_context *ice,
741 struct iris_resource *res, uint32_t level,
742 uint32_t start_layer, uint32_t num_layers,
743 enum isl_aux_usage aux_usage)
744 {
745 if (!level_has_aux(res, level))
746 return;
747
748 const uint32_t level_layers =
749 miptree_layer_range_length(res, level, start_layer, num_layers);
750
751 for (uint32_t a = 0; a < level_layers; a++) {
752 const uint32_t layer = start_layer + a;
753 const enum isl_aux_state aux_state =
754 iris_resource_get_aux_state(res, level, layer);
755 const enum isl_aux_state new_aux_state =
756 isl_aux_state_transition_write(aux_state, aux_usage, false);
757 iris_resource_set_aux_state(ice, res, level, layer, 1, new_aux_state);
758 }
759 }
760
761 enum isl_aux_state
iris_resource_get_aux_state(const struct iris_resource * res,uint32_t level,uint32_t layer)762 iris_resource_get_aux_state(const struct iris_resource *res,
763 uint32_t level, uint32_t layer)
764 {
765 iris_resource_check_level_layer(res, level, layer);
766
767 if (res->surf.usage & ISL_SURF_USAGE_DEPTH_BIT) {
768 assert(iris_resource_level_has_hiz(res, level));
769 } else {
770 assert(res->surf.samples == 1 ||
771 res->surf.msaa_layout == ISL_MSAA_LAYOUT_ARRAY);
772 }
773
774 return res->aux.state[level][layer];
775 }
776
777 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)778 iris_resource_set_aux_state(struct iris_context *ice,
779 struct iris_resource *res, uint32_t level,
780 uint32_t start_layer, uint32_t num_layers,
781 enum isl_aux_state aux_state)
782 {
783 num_layers = miptree_layer_range_length(res, level, start_layer, num_layers);
784
785 if (res->surf.usage & ISL_SURF_USAGE_DEPTH_BIT) {
786 assert(iris_resource_level_has_hiz(res, level));
787 } else {
788 assert(res->surf.samples == 1 ||
789 res->surf.msaa_layout == ISL_MSAA_LAYOUT_ARRAY);
790 }
791
792 for (unsigned a = 0; a < num_layers; a++) {
793 if (res->aux.state[level][start_layer + a] != aux_state) {
794 res->aux.state[level][start_layer + a] = aux_state;
795 /* XXX: Need to track which bindings to make dirty */
796 ice->state.dirty |= IRIS_DIRTY_RENDER_BUFFER |
797 IRIS_DIRTY_RENDER_RESOLVES_AND_FLUSHES |
798 IRIS_DIRTY_COMPUTE_RESOLVES_AND_FLUSHES;
799 ice->state.stage_dirty |= IRIS_ALL_STAGE_DIRTY_BINDINGS;
800 }
801 }
802
803 if (res->mod_info && !res->mod_info->supports_clear_color) {
804 assert(res->mod_info->aux_usage != ISL_AUX_USAGE_NONE);
805 if (aux_state == ISL_AUX_STATE_CLEAR ||
806 aux_state == ISL_AUX_STATE_COMPRESSED_CLEAR ||
807 aux_state == ISL_AUX_STATE_PARTIAL_CLEAR) {
808 iris_mark_dirty_dmabuf(ice, &res->base);
809 }
810 }
811 }
812
813 enum isl_aux_usage
iris_resource_texture_aux_usage(struct iris_context * ice,const struct iris_resource * res,enum isl_format view_format)814 iris_resource_texture_aux_usage(struct iris_context *ice,
815 const struct iris_resource *res,
816 enum isl_format view_format)
817 {
818 struct iris_screen *screen = (void *) ice->ctx.screen;
819 struct gen_device_info *devinfo = &screen->devinfo;
820
821 switch (res->aux.usage) {
822 case ISL_AUX_USAGE_HIZ:
823 if (iris_sample_with_depth_aux(devinfo, res))
824 return ISL_AUX_USAGE_HIZ;
825 break;
826
827 case ISL_AUX_USAGE_HIZ_CCS:
828 assert(!iris_sample_with_depth_aux(devinfo, res));
829 return ISL_AUX_USAGE_NONE;
830
831 case ISL_AUX_USAGE_HIZ_CCS_WT:
832 if (iris_sample_with_depth_aux(devinfo, res))
833 return ISL_AUX_USAGE_HIZ_CCS_WT;
834 break;
835
836 case ISL_AUX_USAGE_MCS:
837 case ISL_AUX_USAGE_MCS_CCS:
838 case ISL_AUX_USAGE_STC_CCS:
839 case ISL_AUX_USAGE_MC:
840 return res->aux.usage;
841
842 case ISL_AUX_USAGE_CCS_E:
843 case ISL_AUX_USAGE_GEN12_CCS_E:
844 /* If we don't have any unresolved color, report an aux usage of
845 * ISL_AUX_USAGE_NONE. This way, texturing won't even look at the
846 * aux surface and we can save some bandwidth.
847 */
848 if (!iris_has_invalid_primary(res, 0, INTEL_REMAINING_LEVELS,
849 0, INTEL_REMAINING_LAYERS))
850 return ISL_AUX_USAGE_NONE;
851
852 /* On Gen9 color buffers may be compressed by the hardware (lossless
853 * compression). There are, however, format restrictions and care needs
854 * to be taken that the sampler engine is capable for re-interpreting a
855 * buffer with format different the buffer was originally written with.
856 *
857 * For example, SRGB formats are not compressible and the sampler engine
858 * isn't capable of treating RGBA_UNORM as SRGB_ALPHA. In such a case
859 * the underlying color buffer needs to be resolved so that the sampling
860 * surface can be sampled as non-compressed (i.e., without the auxiliary
861 * MCS buffer being set).
862 */
863 if (isl_formats_are_ccs_e_compatible(devinfo, res->surf.format,
864 view_format))
865 return res->aux.usage;
866 break;
867
868 default:
869 break;
870 }
871
872 return ISL_AUX_USAGE_NONE;
873 }
874
875 enum isl_aux_usage
iris_image_view_aux_usage(struct iris_context * ice,const struct pipe_image_view * pview,const struct shader_info * info)876 iris_image_view_aux_usage(struct iris_context *ice,
877 const struct pipe_image_view *pview,
878 const struct shader_info *info)
879 {
880 if (!info)
881 return ISL_AUX_USAGE_NONE;
882
883 struct iris_resource *res = (void *) pview->resource;
884
885 enum isl_format view_format = iris_image_view_get_format(ice, pview);
886 enum isl_aux_usage aux_usage =
887 iris_resource_texture_aux_usage(ice, res, view_format);
888
889 bool uses_atomic_load_store =
890 ice->shaders.uncompiled[info->stage]->uses_atomic_load_store;
891
892 if (aux_usage == ISL_AUX_USAGE_GEN12_CCS_E && !uses_atomic_load_store)
893 return ISL_AUX_USAGE_GEN12_CCS_E;
894
895 return ISL_AUX_USAGE_NONE;
896 }
897
898 static bool
isl_formats_are_fast_clear_compatible(enum isl_format a,enum isl_format b)899 isl_formats_are_fast_clear_compatible(enum isl_format a, enum isl_format b)
900 {
901 /* On gen8 and earlier, the hardware was only capable of handling 0/1 clear
902 * values so sRGB curve application was a no-op for all fast-clearable
903 * formats.
904 *
905 * On gen9+, the hardware supports arbitrary clear values. For sRGB clear
906 * values, the hardware interprets the floats, not as what would be
907 * returned from the sampler (or written by the shader), but as being
908 * between format conversion and sRGB curve application. This means that
909 * we can switch between sRGB and UNORM without having to whack the clear
910 * color.
911 */
912 return isl_format_srgb_to_linear(a) == isl_format_srgb_to_linear(b);
913 }
914
915 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)916 iris_resource_prepare_texture(struct iris_context *ice,
917 struct iris_resource *res,
918 enum isl_format view_format,
919 uint32_t start_level, uint32_t num_levels,
920 uint32_t start_layer, uint32_t num_layers)
921 {
922 enum isl_aux_usage aux_usage =
923 iris_resource_texture_aux_usage(ice, res, view_format);
924
925 bool clear_supported = isl_aux_usage_has_fast_clears(aux_usage);
926
927 /* Clear color is specified as ints or floats and the conversion is done by
928 * the sampler. If we have a texture view, we would have to perform the
929 * clear color conversion manually. Just disable clear color.
930 */
931 if (!isl_formats_are_fast_clear_compatible(res->surf.format, view_format))
932 clear_supported = false;
933
934 iris_resource_prepare_access(ice, res, start_level, num_levels,
935 start_layer, num_layers,
936 aux_usage, clear_supported);
937 }
938
939 /* Whether or not rendering a color value with either format results in the
940 * same pixel. This can return false negatives.
941 */
942 bool
iris_render_formats_color_compatible(enum isl_format a,enum isl_format b,union isl_color_value color)943 iris_render_formats_color_compatible(enum isl_format a, enum isl_format b,
944 union isl_color_value color)
945 {
946 if (a == b)
947 return true;
948
949 /* A difference in color space doesn't matter for 0/1 values. */
950 if (isl_format_srgb_to_linear(a) == isl_format_srgb_to_linear(b) &&
951 isl_color_value_is_zero_one(color, a)) {
952 return true;
953 }
954
955 return false;
956 }
957
958 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)959 iris_resource_render_aux_usage(struct iris_context *ice,
960 struct iris_resource *res,
961 enum isl_format render_format,
962 bool draw_aux_disabled)
963 {
964 struct iris_screen *screen = (void *) ice->ctx.screen;
965 struct gen_device_info *devinfo = &screen->devinfo;
966
967 if (draw_aux_disabled)
968 return ISL_AUX_USAGE_NONE;
969
970 switch (res->aux.usage) {
971 case ISL_AUX_USAGE_MCS:
972 case ISL_AUX_USAGE_MCS_CCS:
973 return res->aux.usage;
974
975 case ISL_AUX_USAGE_CCS_D:
976 case ISL_AUX_USAGE_CCS_E:
977 case ISL_AUX_USAGE_GEN12_CCS_E:
978 /* Disable CCS for some cases of texture-view rendering. On gen12, HW
979 * may convert some subregions of shader output to fast-cleared blocks
980 * if CCS is enabled and the shader output matches the clear color.
981 * Existing fast-cleared blocks are correctly interpreted by the clear
982 * color and the resource format (see can_fast_clear_color). To avoid
983 * gaining new fast-cleared blocks that can't be interpreted by the
984 * resource format (and to avoid misinterpreting existing ones), shut
985 * off CCS when the interpretation of the clear color differs between
986 * the render_format and the resource format.
987 */
988 if (!iris_render_formats_color_compatible(render_format,
989 res->surf.format,
990 res->aux.clear_color)) {
991 return ISL_AUX_USAGE_NONE;
992 }
993
994 if (res->aux.usage == ISL_AUX_USAGE_CCS_D)
995 return ISL_AUX_USAGE_CCS_D;
996
997 if (isl_formats_are_ccs_e_compatible(devinfo, res->surf.format,
998 render_format)) {
999 return res->aux.usage;
1000 }
1001 /* fallthrough */
1002
1003 default:
1004 return ISL_AUX_USAGE_NONE;
1005 }
1006 }
1007
1008 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)1009 iris_resource_prepare_render(struct iris_context *ice,
1010 struct iris_batch *batch,
1011 struct iris_resource *res, uint32_t level,
1012 uint32_t start_layer, uint32_t layer_count,
1013 enum isl_aux_usage aux_usage)
1014 {
1015 iris_resource_prepare_access(ice, res, level, 1, start_layer,
1016 layer_count, aux_usage,
1017 isl_aux_usage_has_fast_clears(aux_usage));
1018 }
1019
1020 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)1021 iris_resource_finish_render(struct iris_context *ice,
1022 struct iris_resource *res, uint32_t level,
1023 uint32_t start_layer, uint32_t layer_count,
1024 enum isl_aux_usage aux_usage)
1025 {
1026 iris_resource_finish_write(ice, res, level, start_layer, layer_count,
1027 aux_usage);
1028 }
1029
1030 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)1031 iris_resource_prepare_depth(struct iris_context *ice,
1032 struct iris_batch *batch,
1033 struct iris_resource *res, uint32_t level,
1034 uint32_t start_layer, uint32_t layer_count)
1035 {
1036 iris_resource_prepare_access(ice, res, level, 1, start_layer,
1037 layer_count, res->aux.usage, !!res->aux.bo);
1038 }
1039
1040 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)1041 iris_resource_finish_depth(struct iris_context *ice,
1042 struct iris_resource *res, uint32_t level,
1043 uint32_t start_layer, uint32_t layer_count,
1044 bool depth_written)
1045 {
1046 if (depth_written) {
1047 iris_resource_finish_write(ice, res, level, start_layer, layer_count,
1048 res->aux.usage);
1049 }
1050 }
1051