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