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