1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 * Copyright 2015 Advanced Micro Devices, Inc.
4 *
5 * SPDX-License-Identifier: MIT
6 */
7
8 #include "si_pipe.h"
9 #include "util/format/u_format.h"
10 #include "util/u_log.h"
11 #include "util/u_surface.h"
12 #include "util/hash_table.h"
13
14 enum
15 {
16 SI_COPY =
17 SI_SAVE_FRAMEBUFFER | SI_SAVE_TEXTURES | SI_SAVE_FRAGMENT_STATE | SI_DISABLE_RENDER_COND,
18
19 SI_BLIT = SI_SAVE_FRAMEBUFFER | SI_SAVE_TEXTURES | SI_SAVE_FRAGMENT_STATE,
20
21 SI_DECOMPRESS = SI_SAVE_FRAMEBUFFER | SI_SAVE_FRAGMENT_STATE | SI_DISABLE_RENDER_COND,
22
23 SI_COLOR_RESOLVE = SI_SAVE_FRAMEBUFFER | SI_SAVE_FRAGMENT_STATE
24 };
25
si_blitter_begin(struct si_context * sctx,enum si_blitter_op op)26 void si_blitter_begin(struct si_context *sctx, enum si_blitter_op op)
27 {
28 util_blitter_save_vertex_shader(sctx->blitter, sctx->shader.vs.cso);
29 util_blitter_save_tessctrl_shader(sctx->blitter, sctx->shader.tcs.cso);
30 util_blitter_save_tesseval_shader(sctx->blitter, sctx->shader.tes.cso);
31 util_blitter_save_geometry_shader(sctx->blitter, sctx->shader.gs.cso);
32 util_blitter_save_so_targets(sctx->blitter, sctx->streamout.num_targets,
33 (struct pipe_stream_output_target **)sctx->streamout.targets);
34 util_blitter_save_rasterizer(sctx->blitter, sctx->queued.named.rasterizer);
35
36 if (op & SI_SAVE_FRAGMENT_STATE) {
37 struct pipe_constant_buffer fs_cb = {};
38 si_get_pipe_constant_buffer(sctx, PIPE_SHADER_FRAGMENT, 0, &fs_cb);
39
40 if (op & SI_SAVE_FRAGMENT_CONSTANT)
41 util_blitter_save_fragment_constant_buffer_slot(sctx->blitter, &fs_cb);
42
43 pipe_resource_reference(&fs_cb.buffer, NULL);
44 util_blitter_save_blend(sctx->blitter, sctx->queued.named.blend);
45 util_blitter_save_depth_stencil_alpha(sctx->blitter, sctx->queued.named.dsa);
46 util_blitter_save_stencil_ref(sctx->blitter, &sctx->stencil_ref.state);
47 util_blitter_save_fragment_shader(sctx->blitter, sctx->shader.ps.cso);
48 util_blitter_save_sample_mask(sctx->blitter, sctx->sample_mask, sctx->ps_iter_samples);
49 util_blitter_save_scissor(sctx->blitter, &sctx->scissors[0]);
50 util_blitter_save_window_rectangles(sctx->blitter, sctx->window_rectangles_include,
51 sctx->num_window_rectangles, sctx->window_rectangles);
52 }
53
54 if (op & SI_SAVE_FRAMEBUFFER)
55 util_blitter_save_framebuffer(sctx->blitter, &sctx->framebuffer.state);
56
57 if (op & SI_SAVE_TEXTURES) {
58 util_blitter_save_fragment_sampler_states(
59 sctx->blitter, 2, (void **)sctx->samplers[PIPE_SHADER_FRAGMENT].sampler_states);
60
61 util_blitter_save_fragment_sampler_views(sctx->blitter, 2,
62 sctx->samplers[PIPE_SHADER_FRAGMENT].views);
63 }
64
65 if (op & SI_DISABLE_RENDER_COND)
66 sctx->render_cond_enabled = false;
67
68 if (sctx->screen->dpbb_allowed) {
69 sctx->dpbb_force_off = true;
70 si_mark_atom_dirty(sctx, &sctx->atoms.s.dpbb_state);
71 }
72
73 /* Force-disable fbfetch because there are unsolvable recursion problems with u_blitter. */
74 si_force_disable_ps_colorbuf0_slot(sctx);
75
76 sctx->blitter_running = true;
77 }
78
si_blitter_end(struct si_context * sctx)79 void si_blitter_end(struct si_context *sctx)
80 {
81 sctx->blitter_running = false;
82
83 if (sctx->screen->dpbb_allowed) {
84 sctx->dpbb_force_off = false;
85 si_mark_atom_dirty(sctx, &sctx->atoms.s.dpbb_state);
86 }
87
88 sctx->render_cond_enabled = sctx->render_cond;
89
90 /* Restore shader pointers because the VS blit shader changed all
91 * non-global VS user SGPRs. */
92 sctx->shader_pointers_dirty |= SI_DESCS_SHADER_MASK(VERTEX);
93
94 if (sctx->gfx_level >= GFX11)
95 sctx->gs_attribute_ring_pointer_dirty = true;
96
97 /* Reset SI_SGPR_SMALL_PRIM_CULL_INFO: */
98 if (sctx->screen->use_ngg_culling)
99 si_mark_atom_dirty(sctx, &sctx->atoms.s.ngg_cull_state);
100
101 sctx->vertex_buffers_dirty = sctx->num_vertex_elements > 0;
102 si_mark_atom_dirty(sctx, &sctx->atoms.s.gfx_shader_pointers);
103
104 /* We force-disabled fbfetch for u_blitter, so recompute the state. */
105 si_update_ps_colorbuf0_slot(sctx);
106 }
107
u_max_sample(struct pipe_resource * r)108 static unsigned u_max_sample(struct pipe_resource *r)
109 {
110 return r->nr_samples ? r->nr_samples - 1 : 0;
111 }
112
si_blit_dbcb_copy(struct si_context * sctx,struct si_texture * src,struct si_texture * dst,unsigned planes,unsigned level_mask,unsigned first_layer,unsigned last_layer,unsigned first_sample,unsigned last_sample)113 static unsigned si_blit_dbcb_copy(struct si_context *sctx, struct si_texture *src,
114 struct si_texture *dst, unsigned planes, unsigned level_mask,
115 unsigned first_layer, unsigned last_layer, unsigned first_sample,
116 unsigned last_sample)
117 {
118 struct pipe_surface surf_tmpl = {{0}};
119 unsigned layer, sample, checked_last_layer, max_layer;
120 unsigned fully_copied_levels = 0;
121
122 assert(sctx->gfx_level < GFX11);
123
124 if (planes & PIPE_MASK_Z)
125 sctx->dbcb_depth_copy_enabled = true;
126 if (planes & PIPE_MASK_S)
127 sctx->dbcb_stencil_copy_enabled = true;
128 si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);
129
130 assert(sctx->dbcb_depth_copy_enabled || sctx->dbcb_stencil_copy_enabled);
131
132 sctx->decompression_enabled = true;
133
134 while (level_mask) {
135 unsigned level = u_bit_scan(&level_mask);
136
137 /* The smaller the mipmap level, the less layers there are
138 * as far as 3D textures are concerned. */
139 max_layer = util_max_layer(&src->buffer.b.b, level);
140 checked_last_layer = MIN2(last_layer, max_layer);
141
142 surf_tmpl.u.tex.level = level;
143
144 for (layer = first_layer; layer <= checked_last_layer; layer++) {
145 struct pipe_surface *zsurf, *cbsurf;
146
147 surf_tmpl.format = src->buffer.b.b.format;
148 surf_tmpl.u.tex.first_layer = layer;
149 surf_tmpl.u.tex.last_layer = layer;
150
151 zsurf = sctx->b.create_surface(&sctx->b, &src->buffer.b.b, &surf_tmpl);
152
153 surf_tmpl.format = dst->buffer.b.b.format;
154 cbsurf = sctx->b.create_surface(&sctx->b, &dst->buffer.b.b, &surf_tmpl);
155
156 for (sample = first_sample; sample <= last_sample; sample++) {
157 if (sample != sctx->dbcb_copy_sample) {
158 sctx->dbcb_copy_sample = sample;
159 si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);
160 }
161
162 si_blitter_begin(sctx, SI_DECOMPRESS);
163 util_blitter_custom_depth_stencil(sctx->blitter, zsurf, cbsurf, 1 << sample,
164 sctx->custom_dsa_flush, 1.0f);
165 si_blitter_end(sctx);
166 }
167
168 pipe_surface_reference(&zsurf, NULL);
169 pipe_surface_reference(&cbsurf, NULL);
170 }
171
172 if (first_layer == 0 && last_layer >= max_layer && first_sample == 0 &&
173 last_sample >= u_max_sample(&src->buffer.b.b))
174 fully_copied_levels |= 1u << level;
175 }
176
177 sctx->decompression_enabled = false;
178 sctx->dbcb_depth_copy_enabled = false;
179 sctx->dbcb_stencil_copy_enabled = false;
180 si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);
181
182 return fully_copied_levels;
183 }
184
185 /* Helper function for si_blit_decompress_zs_in_place.
186 */
si_blit_decompress_zs_planes_in_place(struct si_context * sctx,struct si_texture * texture,unsigned planes,unsigned level_mask,unsigned first_layer,unsigned last_layer)187 static void si_blit_decompress_zs_planes_in_place(struct si_context *sctx,
188 struct si_texture *texture, unsigned planes,
189 unsigned level_mask, unsigned first_layer,
190 unsigned last_layer)
191 {
192 struct pipe_surface *zsurf, surf_tmpl = {{0}};
193 unsigned layer, max_layer, checked_last_layer;
194 unsigned fully_decompressed_mask = 0;
195
196 if (!level_mask)
197 return;
198
199 if (planes & PIPE_MASK_S)
200 sctx->db_flush_stencil_inplace = true;
201 if (planes & PIPE_MASK_Z)
202 sctx->db_flush_depth_inplace = true;
203 si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);
204
205 surf_tmpl.format = texture->buffer.b.b.format;
206
207 sctx->decompression_enabled = true;
208
209 while (level_mask) {
210 unsigned level = u_bit_scan(&level_mask);
211
212 surf_tmpl.u.tex.level = level;
213
214 /* The smaller the mipmap level, the less layers there are
215 * as far as 3D textures are concerned. */
216 max_layer = util_max_layer(&texture->buffer.b.b, level);
217 checked_last_layer = MIN2(last_layer, max_layer);
218
219 for (layer = first_layer; layer <= checked_last_layer; layer++) {
220 surf_tmpl.u.tex.first_layer = layer;
221 surf_tmpl.u.tex.last_layer = layer;
222
223 zsurf = sctx->b.create_surface(&sctx->b, &texture->buffer.b.b, &surf_tmpl);
224
225 si_blitter_begin(sctx, SI_DECOMPRESS);
226 util_blitter_custom_depth_stencil(sctx->blitter, zsurf, NULL, ~0, sctx->custom_dsa_flush,
227 1.0f);
228 si_blitter_end(sctx);
229
230 pipe_surface_reference(&zsurf, NULL);
231 }
232
233 /* The texture will always be dirty if some layers aren't flushed.
234 * I don't think this case occurs often though. */
235 if (first_layer == 0 && last_layer >= max_layer) {
236 fully_decompressed_mask |= 1u << level;
237 }
238 }
239
240 if (planes & PIPE_MASK_Z)
241 texture->dirty_level_mask &= ~fully_decompressed_mask;
242 if (planes & PIPE_MASK_S)
243 texture->stencil_dirty_level_mask &= ~fully_decompressed_mask;
244
245 sctx->decompression_enabled = false;
246 sctx->db_flush_depth_inplace = false;
247 sctx->db_flush_stencil_inplace = false;
248 si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);
249 }
250
251 /* Helper function of si_flush_depth_texture: decompress the given levels
252 * of Z and/or S planes in place.
253 */
si_blit_decompress_zs_in_place(struct si_context * sctx,struct si_texture * texture,unsigned levels_z,unsigned levels_s,unsigned first_layer,unsigned last_layer)254 static void si_blit_decompress_zs_in_place(struct si_context *sctx, struct si_texture *texture,
255 unsigned levels_z, unsigned levels_s,
256 unsigned first_layer, unsigned last_layer)
257 {
258 unsigned both = levels_z & levels_s;
259
260 /* First, do combined Z & S decompresses for levels that need it. */
261 if (both) {
262 si_blit_decompress_zs_planes_in_place(sctx, texture, PIPE_MASK_Z | PIPE_MASK_S, both,
263 first_layer, last_layer);
264 levels_z &= ~both;
265 levels_s &= ~both;
266 }
267
268 /* Now do separate Z and S decompresses. */
269 if (levels_z) {
270 si_blit_decompress_zs_planes_in_place(sctx, texture, PIPE_MASK_Z, levels_z, first_layer,
271 last_layer);
272 }
273
274 if (levels_s) {
275 si_blit_decompress_zs_planes_in_place(sctx, texture, PIPE_MASK_S, levels_s, first_layer,
276 last_layer);
277 }
278 }
279
si_decompress_depth(struct si_context * sctx,struct si_texture * tex,unsigned required_planes,unsigned first_level,unsigned last_level,unsigned first_layer,unsigned last_layer)280 static void si_decompress_depth(struct si_context *sctx, struct si_texture *tex,
281 unsigned required_planes, unsigned first_level, unsigned last_level,
282 unsigned first_layer, unsigned last_layer)
283 {
284 unsigned inplace_planes = 0;
285 unsigned copy_planes = 0;
286 unsigned level_mask = u_bit_consecutive(first_level, last_level - first_level + 1);
287 unsigned levels_z = 0;
288 unsigned levels_s = 0;
289
290 if (required_planes & PIPE_MASK_Z) {
291 levels_z = level_mask & tex->dirty_level_mask;
292
293 if (levels_z) {
294 if (si_can_sample_zs(tex, false))
295 inplace_planes |= PIPE_MASK_Z;
296 else
297 copy_planes |= PIPE_MASK_Z;
298 }
299 }
300 if (required_planes & PIPE_MASK_S) {
301 levels_s = level_mask & tex->stencil_dirty_level_mask;
302
303 if (levels_s) {
304 if (si_can_sample_zs(tex, true))
305 inplace_planes |= PIPE_MASK_S;
306 else
307 copy_planes |= PIPE_MASK_S;
308 }
309 }
310
311 if (unlikely(sctx->log))
312 u_log_printf(sctx->log,
313 "\n------------------------------------------------\n"
314 "Decompress Depth (levels %u - %u, levels Z: 0x%x S: 0x%x)\n\n",
315 first_level, last_level, levels_z, levels_s);
316
317 /* We may have to allocate the flushed texture here when called from
318 * si_decompress_subresource.
319 */
320 if (copy_planes &&
321 (tex->flushed_depth_texture || si_init_flushed_depth_texture(&sctx->b, &tex->buffer.b.b))) {
322 struct si_texture *dst = tex->flushed_depth_texture;
323 unsigned fully_copied_levels;
324 unsigned levels = 0;
325
326 assert(tex->flushed_depth_texture);
327
328 if (util_format_is_depth_and_stencil(dst->buffer.b.b.format))
329 copy_planes = PIPE_MASK_Z | PIPE_MASK_S;
330
331 if (copy_planes & PIPE_MASK_Z) {
332 levels |= levels_z;
333 levels_z = 0;
334 }
335 if (copy_planes & PIPE_MASK_S) {
336 levels |= levels_s;
337 levels_s = 0;
338 }
339
340 fully_copied_levels = si_blit_dbcb_copy(sctx, tex, dst, copy_planes, levels, first_layer,
341 last_layer, 0, u_max_sample(&tex->buffer.b.b));
342
343 if (copy_planes & PIPE_MASK_Z)
344 tex->dirty_level_mask &= ~fully_copied_levels;
345 if (copy_planes & PIPE_MASK_S)
346 tex->stencil_dirty_level_mask &= ~fully_copied_levels;
347 }
348
349 if (inplace_planes) {
350 bool has_htile = si_htile_enabled(tex, first_level, inplace_planes);
351 bool tc_compat_htile = vi_tc_compat_htile_enabled(tex, first_level, inplace_planes);
352
353 /* Don't decompress if there is no HTILE or when HTILE is
354 * TC-compatible. */
355 if (has_htile && !tc_compat_htile) {
356 si_blit_decompress_zs_in_place(sctx, tex, levels_z, levels_s, first_layer, last_layer);
357 } else {
358 /* This is only a cache flush.
359 *
360 * Only clear the mask that we are flushing, because
361 * si_make_DB_shader_coherent() treats different levels
362 * and depth and stencil differently.
363 */
364 if (inplace_planes & PIPE_MASK_Z)
365 tex->dirty_level_mask &= ~levels_z;
366 if (inplace_planes & PIPE_MASK_S)
367 tex->stencil_dirty_level_mask &= ~levels_s;
368 }
369
370 /* We just had to completely decompress Z/S for texturing. Enable
371 * TC-compatible HTILE on the next clear, so that the decompression
372 * doesn't have to be done for this texture ever again.
373 *
374 * TC-compatible HTILE might slightly reduce Z/S performance, but
375 * the decompression is much worse.
376 */
377 if (has_htile && !tc_compat_htile &&
378 /* We can only transition the whole buffer in one clear, so no mipmapping: */
379 tex->buffer.b.b.last_level == 0 &&
380 tex->surface.flags & RADEON_SURF_TC_COMPATIBLE_HTILE &&
381 (inplace_planes & PIPE_MASK_Z || !tex->htile_stencil_disabled))
382 tex->enable_tc_compatible_htile_next_clear = true;
383
384 /* Only in-place decompression needs to flush DB caches, or
385 * when we don't decompress but TC-compatible planes are dirty.
386 */
387 si_make_DB_shader_coherent(sctx, tex->buffer.b.b.nr_samples, inplace_planes & PIPE_MASK_S,
388 tc_compat_htile);
389 }
390 /* set_framebuffer_state takes care of coherency for single-sample.
391 * The DB->CB copy uses CB for the final writes.
392 */
393 if (copy_planes && tex->buffer.b.b.nr_samples > 1)
394 si_make_CB_shader_coherent(sctx, tex->buffer.b.b.nr_samples, false, true /* no DCC */);
395 }
396
si_decompress_sampler_depth_textures(struct si_context * sctx,struct si_samplers * textures)397 static bool si_decompress_sampler_depth_textures(struct si_context *sctx,
398 struct si_samplers *textures)
399 {
400 unsigned i;
401 unsigned mask = textures->needs_depth_decompress_mask;
402 bool need_flush = false;
403
404 while (mask) {
405 struct pipe_sampler_view *view;
406 struct si_sampler_view *sview;
407 struct si_texture *tex;
408
409 i = u_bit_scan(&mask);
410
411 view = textures->views[i];
412 assert(view);
413 sview = (struct si_sampler_view *)view;
414
415 tex = (struct si_texture *)view->texture;
416 assert(tex->db_compatible);
417
418 si_decompress_depth(sctx, tex, sview->is_stencil_sampler ? PIPE_MASK_S : PIPE_MASK_Z,
419 view->u.tex.first_level, view->u.tex.last_level, 0,
420 util_max_layer(&tex->buffer.b.b, view->u.tex.first_level));
421
422 if (tex->need_flush_after_depth_decompression) {
423 need_flush = true;
424 tex->need_flush_after_depth_decompression = false;
425 }
426 }
427
428 return need_flush;
429 }
430
si_blit_decompress_color(struct si_context * sctx,struct si_texture * tex,unsigned first_level,unsigned last_level,unsigned first_layer,unsigned last_layer,bool need_dcc_decompress,bool need_fmask_expand)431 static void si_blit_decompress_color(struct si_context *sctx, struct si_texture *tex,
432 unsigned first_level, unsigned last_level,
433 unsigned first_layer, unsigned last_layer,
434 bool need_dcc_decompress, bool need_fmask_expand)
435 {
436 void *custom_blend;
437 unsigned layer, checked_last_layer, max_layer;
438 unsigned level_mask = u_bit_consecutive(first_level, last_level - first_level + 1);
439
440 if (!need_dcc_decompress)
441 level_mask &= tex->dirty_level_mask;
442 if (!level_mask)
443 goto expand_fmask;
444
445 /* No color decompression is needed on GFX11. */
446 assert(sctx->gfx_level < GFX11 || need_dcc_decompress);
447
448 if (unlikely(sctx->log))
449 u_log_printf(sctx->log,
450 "\n------------------------------------------------\n"
451 "Decompress Color (levels %u - %u, mask 0x%x)\n\n",
452 first_level, last_level, level_mask);
453
454 if (need_dcc_decompress) {
455 custom_blend = sctx->custom_blend_dcc_decompress;
456
457 /* DCC_DECOMPRESS and ELIMINATE_FAST_CLEAR require MSAA_NUM_SAMPLES=0. */
458 if (sctx->gfx_level >= GFX11) {
459 sctx->gfx11_force_msaa_num_samples_zero = true;
460 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
461 }
462
463 assert(vi_dcc_enabled(tex, first_level));
464
465 /* disable levels without DCC */
466 for (int i = first_level; i <= last_level; i++) {
467 if (!vi_dcc_enabled(tex, i))
468 level_mask &= ~(1 << i);
469 }
470 } else if (tex->surface.fmask_size) {
471 assert(sctx->gfx_level < GFX11);
472 custom_blend = sctx->custom_blend_fmask_decompress;
473 } else {
474 assert(sctx->gfx_level < GFX11);
475 custom_blend = sctx->custom_blend_eliminate_fastclear;
476 }
477
478 sctx->decompression_enabled = true;
479
480 while (level_mask) {
481 unsigned level = u_bit_scan(&level_mask);
482
483 /* The smaller the mipmap level, the less layers there are
484 * as far as 3D textures are concerned. */
485 max_layer = util_max_layer(&tex->buffer.b.b, level);
486 checked_last_layer = MIN2(last_layer, max_layer);
487
488 for (layer = first_layer; layer <= checked_last_layer; layer++) {
489 struct pipe_surface *cbsurf, surf_tmpl;
490
491 surf_tmpl.format = tex->buffer.b.b.format;
492 surf_tmpl.u.tex.level = level;
493 surf_tmpl.u.tex.first_layer = layer;
494 surf_tmpl.u.tex.last_layer = layer;
495 cbsurf = sctx->b.create_surface(&sctx->b, &tex->buffer.b.b, &surf_tmpl);
496
497 /* Required before and after FMASK and DCC_DECOMPRESS. */
498 if (custom_blend == sctx->custom_blend_fmask_decompress ||
499 custom_blend == sctx->custom_blend_dcc_decompress) {
500 sctx->flags |= SI_CONTEXT_FLUSH_AND_INV_CB;
501 si_mark_atom_dirty(sctx, &sctx->atoms.s.cache_flush);
502 }
503
504 si_blitter_begin(sctx, SI_DECOMPRESS);
505 util_blitter_custom_color(sctx->blitter, cbsurf, custom_blend);
506 si_blitter_end(sctx);
507
508 if (custom_blend == sctx->custom_blend_fmask_decompress ||
509 custom_blend == sctx->custom_blend_dcc_decompress) {
510 sctx->flags |= SI_CONTEXT_FLUSH_AND_INV_CB;
511 si_mark_atom_dirty(sctx, &sctx->atoms.s.cache_flush);
512 }
513
514 /* When running FMASK decompression with DCC, we need to run the "eliminate fast clear" pass
515 * separately because FMASK decompression doesn't eliminate DCC fast clear. This makes
516 * render->texture transitions more expensive. It can be disabled by
517 * allow_dcc_msaa_clear_to_reg_for_bpp.
518 *
519 * TODO: When we get here, change the compression to TC-compatible on the next clear
520 * to disable both the FMASK decompression and fast clear elimination passes.
521 */
522 if (sctx->screen->allow_dcc_msaa_clear_to_reg_for_bpp[util_logbase2(tex->surface.bpe)] &&
523 custom_blend == sctx->custom_blend_fmask_decompress &&
524 vi_dcc_enabled(tex, level)) {
525 si_blitter_begin(sctx, SI_DECOMPRESS);
526 util_blitter_custom_color(sctx->blitter, cbsurf, sctx->custom_blend_eliminate_fastclear);
527 si_blitter_end(sctx);
528 }
529
530 pipe_surface_reference(&cbsurf, NULL);
531 }
532
533 /* The texture will always be dirty if some layers aren't flushed.
534 * I don't think this case occurs often though. */
535 if (first_layer == 0 && last_layer >= max_layer) {
536 tex->dirty_level_mask &= ~(1 << level);
537 }
538 }
539
540 sctx->decompression_enabled = false;
541 si_make_CB_shader_coherent(sctx, tex->buffer.b.b.nr_samples, vi_dcc_enabled(tex, first_level),
542 tex->surface.u.gfx9.color.dcc.pipe_aligned);
543
544 /* Restore gfx11_force_msaa_num_samples_zero. */
545 if (sctx->gfx11_force_msaa_num_samples_zero) {
546 sctx->gfx11_force_msaa_num_samples_zero = false;
547 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
548 }
549
550 expand_fmask:
551 if (need_fmask_expand && tex->surface.fmask_offset && !tex->fmask_is_identity) {
552 assert(sctx->gfx_level < GFX11); /* no FMASK on gfx11 */
553 si_compute_expand_fmask(&sctx->b, &tex->buffer.b.b);
554 tex->fmask_is_identity = true;
555 }
556 }
557
si_decompress_color_texture(struct si_context * sctx,struct si_texture * tex,unsigned first_level,unsigned last_level,bool need_fmask_expand)558 static void si_decompress_color_texture(struct si_context *sctx, struct si_texture *tex,
559 unsigned first_level, unsigned last_level,
560 bool need_fmask_expand)
561 {
562 assert(sctx->gfx_level < GFX11);
563
564 /* CMASK or DCC can be discarded and we can still end up here. */
565 if (!tex->cmask_buffer && !tex->surface.fmask_size &&
566 !vi_dcc_enabled(tex, first_level))
567 return;
568
569 si_blit_decompress_color(sctx, tex, first_level, last_level, 0,
570 util_max_layer(&tex->buffer.b.b, first_level), false,
571 need_fmask_expand);
572 }
573
si_decompress_sampler_color_textures(struct si_context * sctx,struct si_samplers * textures)574 static void si_decompress_sampler_color_textures(struct si_context *sctx,
575 struct si_samplers *textures)
576 {
577 unsigned i;
578 unsigned mask = textures->needs_color_decompress_mask;
579
580 assert(sctx->gfx_level < GFX11);
581
582 while (mask) {
583 struct pipe_sampler_view *view;
584 struct si_texture *tex;
585
586 i = u_bit_scan(&mask);
587
588 view = textures->views[i];
589 assert(view);
590
591 tex = (struct si_texture *)view->texture;
592
593 si_decompress_color_texture(sctx, tex, view->u.tex.first_level, view->u.tex.last_level,
594 false);
595 }
596 }
597
si_decompress_image_color_textures(struct si_context * sctx,struct si_images * images)598 static void si_decompress_image_color_textures(struct si_context *sctx, struct si_images *images)
599 {
600 unsigned i;
601 unsigned mask = images->needs_color_decompress_mask;
602
603 assert(sctx->gfx_level < GFX11);
604
605 while (mask) {
606 const struct pipe_image_view *view;
607 struct si_texture *tex;
608
609 i = u_bit_scan(&mask);
610
611 view = &images->views[i];
612 assert(view->resource->target != PIPE_BUFFER);
613
614 tex = (struct si_texture *)view->resource;
615
616 si_decompress_color_texture(sctx, tex, view->u.tex.level, view->u.tex.level,
617 view->access & PIPE_IMAGE_ACCESS_WRITE);
618 }
619 }
620
si_check_render_feedback_texture(struct si_context * sctx,struct si_texture * tex,unsigned first_level,unsigned last_level,unsigned first_layer,unsigned last_layer)621 static void si_check_render_feedback_texture(struct si_context *sctx, struct si_texture *tex,
622 unsigned first_level, unsigned last_level,
623 unsigned first_layer, unsigned last_layer)
624 {
625 bool render_feedback = false;
626
627 if (!vi_dcc_enabled(tex, first_level))
628 return;
629
630 for (unsigned j = 0; j < sctx->framebuffer.state.nr_cbufs; ++j) {
631 struct si_surface *surf;
632
633 if (!sctx->framebuffer.state.cbufs[j])
634 continue;
635
636 surf = (struct si_surface *)sctx->framebuffer.state.cbufs[j];
637
638 if (tex == (struct si_texture *)surf->base.texture && surf->base.u.tex.level >= first_level &&
639 surf->base.u.tex.level <= last_level && surf->base.u.tex.first_layer <= last_layer &&
640 surf->base.u.tex.last_layer >= first_layer) {
641 render_feedback = true;
642 break;
643 }
644 }
645
646 if (render_feedback)
647 si_texture_disable_dcc(sctx, tex);
648 }
649
si_check_render_feedback_textures(struct si_context * sctx,struct si_samplers * textures,uint32_t in_use_mask)650 static void si_check_render_feedback_textures(struct si_context *sctx, struct si_samplers *textures,
651 uint32_t in_use_mask)
652 {
653 uint32_t mask = textures->enabled_mask & in_use_mask;
654
655 while (mask) {
656 const struct pipe_sampler_view *view;
657 struct si_texture *tex;
658
659 unsigned i = u_bit_scan(&mask);
660
661 view = textures->views[i];
662 if (view->texture->target == PIPE_BUFFER)
663 continue;
664
665 tex = (struct si_texture *)view->texture;
666
667 si_check_render_feedback_texture(sctx, tex, view->u.tex.first_level, view->u.tex.last_level,
668 view->u.tex.first_layer, view->u.tex.last_layer);
669 }
670 }
671
si_check_render_feedback_images(struct si_context * sctx,struct si_images * images,uint32_t in_use_mask)672 static void si_check_render_feedback_images(struct si_context *sctx, struct si_images *images,
673 uint32_t in_use_mask)
674 {
675 uint32_t mask = images->enabled_mask & in_use_mask;
676
677 while (mask) {
678 const struct pipe_image_view *view;
679 struct si_texture *tex;
680
681 unsigned i = u_bit_scan(&mask);
682
683 view = &images->views[i];
684 if (view->resource->target == PIPE_BUFFER)
685 continue;
686
687 tex = (struct si_texture *)view->resource;
688
689 si_check_render_feedback_texture(sctx, tex, view->u.tex.level, view->u.tex.level,
690 view->u.tex.first_layer, view->u.tex.last_layer);
691 }
692 }
693
si_check_render_feedback_resident_textures(struct si_context * sctx)694 static void si_check_render_feedback_resident_textures(struct si_context *sctx)
695 {
696 util_dynarray_foreach (&sctx->resident_tex_handles, struct si_texture_handle *, tex_handle) {
697 struct pipe_sampler_view *view;
698 struct si_texture *tex;
699
700 view = (*tex_handle)->view;
701 if (view->texture->target == PIPE_BUFFER)
702 continue;
703
704 tex = (struct si_texture *)view->texture;
705
706 si_check_render_feedback_texture(sctx, tex, view->u.tex.first_level, view->u.tex.last_level,
707 view->u.tex.first_layer, view->u.tex.last_layer);
708 }
709 }
710
si_check_render_feedback_resident_images(struct si_context * sctx)711 static void si_check_render_feedback_resident_images(struct si_context *sctx)
712 {
713 util_dynarray_foreach (&sctx->resident_img_handles, struct si_image_handle *, img_handle) {
714 struct pipe_image_view *view;
715 struct si_texture *tex;
716
717 view = &(*img_handle)->view;
718 if (view->resource->target == PIPE_BUFFER)
719 continue;
720
721 tex = (struct si_texture *)view->resource;
722
723 si_check_render_feedback_texture(sctx, tex, view->u.tex.level, view->u.tex.level,
724 view->u.tex.first_layer, view->u.tex.last_layer);
725 }
726 }
727
si_check_render_feedback(struct si_context * sctx)728 static void si_check_render_feedback(struct si_context *sctx)
729 {
730 if (!sctx->need_check_render_feedback)
731 return;
732
733 /* There is no render feedback if color writes are disabled.
734 * (e.g. a pixel shader with image stores)
735 */
736 if (!si_any_colorbuffer_written(sctx))
737 return;
738
739 for (int i = 0; i < SI_NUM_GRAPHICS_SHADERS; ++i) {
740 if (!sctx->shaders[i].cso)
741 continue;
742
743 struct si_shader_info *info = &sctx->shaders[i].cso->info;
744 si_check_render_feedback_images(sctx, &sctx->images[i],
745 u_bit_consecutive(0, info->base.num_images));
746 si_check_render_feedback_textures(sctx, &sctx->samplers[i],
747 info->base.textures_used[0]);
748 }
749
750 si_check_render_feedback_resident_images(sctx);
751 si_check_render_feedback_resident_textures(sctx);
752
753 sctx->need_check_render_feedback = false;
754 }
755
si_decompress_resident_color_textures(struct si_context * sctx)756 static void si_decompress_resident_color_textures(struct si_context *sctx)
757 {
758 assert(sctx->gfx_level < GFX11);
759
760 util_dynarray_foreach (&sctx->resident_tex_needs_color_decompress, struct si_texture_handle *,
761 tex_handle) {
762 struct pipe_sampler_view *view = (*tex_handle)->view;
763 struct si_texture *tex = (struct si_texture *)view->texture;
764
765 si_decompress_color_texture(sctx, tex, view->u.tex.first_level, view->u.tex.last_level,
766 false);
767 }
768 }
769
si_decompress_resident_depth_textures(struct si_context * sctx)770 static void si_decompress_resident_depth_textures(struct si_context *sctx)
771 {
772 util_dynarray_foreach (&sctx->resident_tex_needs_depth_decompress, struct si_texture_handle *,
773 tex_handle) {
774 struct pipe_sampler_view *view = (*tex_handle)->view;
775 struct si_sampler_view *sview = (struct si_sampler_view *)view;
776 struct si_texture *tex = (struct si_texture *)view->texture;
777
778 si_decompress_depth(sctx, tex, sview->is_stencil_sampler ? PIPE_MASK_S : PIPE_MASK_Z,
779 view->u.tex.first_level, view->u.tex.last_level, 0,
780 util_max_layer(&tex->buffer.b.b, view->u.tex.first_level));
781 }
782 }
783
si_decompress_resident_images(struct si_context * sctx)784 static void si_decompress_resident_images(struct si_context *sctx)
785 {
786 assert(sctx->gfx_level < GFX11);
787
788 util_dynarray_foreach (&sctx->resident_img_needs_color_decompress, struct si_image_handle *,
789 img_handle) {
790 struct pipe_image_view *view = &(*img_handle)->view;
791 struct si_texture *tex = (struct si_texture *)view->resource;
792
793 si_decompress_color_texture(sctx, tex, view->u.tex.level, view->u.tex.level,
794 view->access & PIPE_IMAGE_ACCESS_WRITE);
795 }
796 }
797
gfx6_decompress_textures(struct si_context * sctx,unsigned shader_mask)798 void gfx6_decompress_textures(struct si_context *sctx, unsigned shader_mask)
799 {
800 unsigned compressed_colortex_counter, mask;
801 bool need_flush = false;
802
803 if (sctx->blitter_running)
804 return;
805
806 /* Update the compressed_colortex_mask if necessary. */
807 compressed_colortex_counter = p_atomic_read(&sctx->screen->compressed_colortex_counter);
808 if (compressed_colortex_counter != sctx->last_compressed_colortex_counter) {
809 sctx->last_compressed_colortex_counter = compressed_colortex_counter;
810 si_update_needs_color_decompress_masks(sctx);
811 }
812
813 /* Decompress color & depth textures if needed. */
814 mask = sctx->shader_needs_decompress_mask & shader_mask;
815 while (mask) {
816 unsigned i = u_bit_scan(&mask);
817
818 if (sctx->samplers[i].needs_depth_decompress_mask) {
819 need_flush |= si_decompress_sampler_depth_textures(sctx, &sctx->samplers[i]);
820 }
821 if (sctx->samplers[i].needs_color_decompress_mask) {
822 si_decompress_sampler_color_textures(sctx, &sctx->samplers[i]);
823 }
824 if (sctx->images[i].needs_color_decompress_mask) {
825 si_decompress_image_color_textures(sctx, &sctx->images[i]);
826 }
827 }
828
829 if (sctx->gfx_level == GFX10_3 && need_flush) {
830 /* This fixes a corruption with the following sequence:
831 * - fast clear depth
832 * - decompress depth
833 * - draw
834 * (see https://gitlab.freedesktop.org/drm/amd/-/issues/1810#note_1170171)
835 */
836 sctx->b.flush(&sctx->b, NULL, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW);
837 }
838
839 if (shader_mask & u_bit_consecutive(0, SI_NUM_GRAPHICS_SHADERS)) {
840 if (sctx->uses_bindless_samplers) {
841 si_decompress_resident_color_textures(sctx);
842 si_decompress_resident_depth_textures(sctx);
843 }
844 if (sctx->uses_bindless_images)
845 si_decompress_resident_images(sctx);
846
847 if (sctx->ps_uses_fbfetch) {
848 struct pipe_surface *cb0 = sctx->framebuffer.state.cbufs[0];
849 si_decompress_color_texture(sctx, (struct si_texture *)cb0->texture,
850 cb0->u.tex.first_layer, cb0->u.tex.last_layer, false);
851 }
852
853 si_check_render_feedback(sctx);
854 } else if (shader_mask & (1 << PIPE_SHADER_COMPUTE)) {
855 if (sctx->cs_shader_state.program->sel.info.uses_bindless_samplers) {
856 si_decompress_resident_color_textures(sctx);
857 si_decompress_resident_depth_textures(sctx);
858 }
859 if (sctx->cs_shader_state.program->sel.info.uses_bindless_images)
860 si_decompress_resident_images(sctx);
861 }
862 }
863
gfx11_decompress_textures(struct si_context * sctx,unsigned shader_mask)864 void gfx11_decompress_textures(struct si_context *sctx, unsigned shader_mask)
865 {
866 if (sctx->blitter_running)
867 return;
868
869 /* Decompress depth textures if needed. */
870 unsigned mask = sctx->shader_needs_decompress_mask & shader_mask;
871 u_foreach_bit(i, mask) {
872 assert(sctx->samplers[i].needs_depth_decompress_mask);
873 si_decompress_sampler_depth_textures(sctx, &sctx->samplers[i]);
874 }
875
876 /* Decompress bindless depth textures and disable DCC for render feedback. */
877 if (shader_mask & u_bit_consecutive(0, SI_NUM_GRAPHICS_SHADERS)) {
878 if (sctx->uses_bindless_samplers)
879 si_decompress_resident_depth_textures(sctx);
880
881 si_check_render_feedback(sctx);
882 } else if (shader_mask & (1 << PIPE_SHADER_COMPUTE)) {
883 if (sctx->cs_shader_state.program->sel.info.uses_bindless_samplers)
884 si_decompress_resident_depth_textures(sctx);
885 }
886 }
887
888 /* Helper for decompressing a portion of a color or depth resource before
889 * blitting if any decompression is needed.
890 * The driver doesn't decompress resources automatically while u_blitter is
891 * rendering. */
si_decompress_subresource(struct pipe_context * ctx,struct pipe_resource * tex,unsigned planes,unsigned level,unsigned first_layer,unsigned last_layer,bool need_fmask_expand)892 void si_decompress_subresource(struct pipe_context *ctx, struct pipe_resource *tex, unsigned planes,
893 unsigned level, unsigned first_layer, unsigned last_layer,
894 bool need_fmask_expand)
895 {
896 struct si_context *sctx = (struct si_context *)ctx;
897 struct si_texture *stex = (struct si_texture *)tex;
898
899 if (stex->db_compatible) {
900 planes &= PIPE_MASK_Z | PIPE_MASK_S;
901
902 if (!stex->surface.has_stencil)
903 planes &= ~PIPE_MASK_S;
904
905 /* If we've rendered into the framebuffer and it's a blitting
906 * source, make sure the decompression pass is invoked
907 * by dirtying the framebuffer.
908 */
909 if (sctx->framebuffer.state.zsbuf && sctx->framebuffer.state.zsbuf->u.tex.level == level &&
910 sctx->framebuffer.state.zsbuf->texture == tex)
911 si_update_fb_dirtiness_after_rendering(sctx);
912
913 si_decompress_depth(sctx, stex, planes, level, level, first_layer, last_layer);
914 } else if (stex->surface.fmask_size || stex->cmask_buffer ||
915 vi_dcc_enabled(stex, level)) {
916 /* If we've rendered into the framebuffer and it's a blitting
917 * source, make sure the decompression pass is invoked
918 * by dirtying the framebuffer.
919 */
920 for (unsigned i = 0; i < sctx->framebuffer.state.nr_cbufs; i++) {
921 if (sctx->framebuffer.state.cbufs[i] &&
922 sctx->framebuffer.state.cbufs[i]->u.tex.level == level &&
923 sctx->framebuffer.state.cbufs[i]->texture == tex) {
924 si_update_fb_dirtiness_after_rendering(sctx);
925 break;
926 }
927 }
928
929 si_blit_decompress_color(sctx, stex, level, level, first_layer, last_layer, false,
930 need_fmask_expand);
931 }
932 }
933
934 struct texture_orig_info {
935 unsigned format;
936 unsigned width0;
937 unsigned height0;
938 unsigned npix_x;
939 unsigned npix_y;
940 unsigned npix0_x;
941 unsigned npix0_y;
942 };
943
si_resource_copy_region(struct pipe_context * ctx,struct pipe_resource * dst,unsigned dst_level,unsigned dstx,unsigned dsty,unsigned dstz,struct pipe_resource * src,unsigned src_level,const struct pipe_box * src_box)944 void si_resource_copy_region(struct pipe_context *ctx, struct pipe_resource *dst,
945 unsigned dst_level, unsigned dstx, unsigned dsty, unsigned dstz,
946 struct pipe_resource *src, unsigned src_level,
947 const struct pipe_box *src_box)
948 {
949 struct si_context *sctx = (struct si_context *)ctx;
950 struct si_texture *ssrc = (struct si_texture *)src;
951 struct pipe_surface *dst_view, dst_templ;
952 struct pipe_sampler_view src_templ, *src_view;
953 struct pipe_box dstbox;
954
955 /* Handle buffers first. */
956 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
957 si_copy_buffer(sctx, dst, src, dstx, src_box->x, src_box->width, SI_OP_SYNC_BEFORE_AFTER);
958 return;
959 }
960
961 if (si_compute_copy_image(sctx, dst, dst_level, src, src_level, dstx, dsty, dstz,
962 src_box, SI_OP_SYNC_BEFORE_AFTER))
963 return;
964
965 /* If the blitter isn't available fail here instead of crashing. */
966 if (!sctx->blitter) {
967 fprintf(stderr, "si_resource_copy_region failed src_format: %s dst_format: %s\n",
968 util_format_name(src->format), util_format_name(dst->format));
969 return;
970 }
971
972 assert(u_max_sample(dst) == u_max_sample(src));
973
974 /* The driver doesn't decompress resources automatically while
975 * u_blitter is rendering. */
976 si_decompress_subresource(ctx, src, PIPE_MASK_RGBAZS, src_level, src_box->z,
977 src_box->z + src_box->depth - 1, false);
978
979 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
980 util_blitter_default_src_texture(sctx->blitter, &src_templ, src, src_level);
981
982 assert(!util_format_is_compressed(src->format) && !util_format_is_compressed(dst->format));
983 assert(!util_format_is_subsampled_422(src->format));
984
985 if (!util_blitter_is_copy_supported(sctx->blitter, dst, src)) {
986 switch (ssrc->surface.bpe) {
987 case 1:
988 dst_templ.format = PIPE_FORMAT_R8_UNORM;
989 src_templ.format = PIPE_FORMAT_R8_UNORM;
990 break;
991 case 2:
992 dst_templ.format = PIPE_FORMAT_R8G8_UNORM;
993 src_templ.format = PIPE_FORMAT_R8G8_UNORM;
994 break;
995 case 4:
996 dst_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
997 src_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
998 break;
999 case 8:
1000 dst_templ.format = PIPE_FORMAT_R16G16B16A16_UINT;
1001 src_templ.format = PIPE_FORMAT_R16G16B16A16_UINT;
1002 break;
1003 case 16:
1004 dst_templ.format = PIPE_FORMAT_R32G32B32A32_UINT;
1005 src_templ.format = PIPE_FORMAT_R32G32B32A32_UINT;
1006 break;
1007 default:
1008 fprintf(stderr, "Unhandled format %s with blocksize %u\n",
1009 util_format_short_name(src->format), ssrc->surface.bpe);
1010 assert(0);
1011 }
1012 }
1013
1014 /* SNORM blitting has precision issues on some chips. Use the SINT
1015 * equivalent instead, which doesn't force DCC decompression.
1016 */
1017 if (util_format_is_snorm(dst_templ.format)) {
1018 dst_templ.format = src_templ.format = util_format_snorm_to_sint(dst_templ.format);
1019 }
1020
1021 vi_disable_dcc_if_incompatible_format(sctx, dst, dst_level, dst_templ.format);
1022 vi_disable_dcc_if_incompatible_format(sctx, src, src_level, src_templ.format);
1023
1024 /* Initialize the surface. */
1025 dst_view = ctx->create_surface(ctx, dst, &dst_templ);
1026
1027 /* Initialize the sampler view. */
1028 src_view = ctx->create_sampler_view(ctx, src, &src_templ);
1029
1030 u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height), abs(src_box->depth),
1031 &dstbox);
1032
1033 /* Copy. */
1034 si_blitter_begin(sctx, SI_COPY);
1035 util_blitter_blit_generic(sctx->blitter, dst_view, &dstbox, src_view, src_box, src->width0,
1036 src->height0, PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,
1037 false, false, 0);
1038 si_blitter_end(sctx);
1039
1040 pipe_surface_reference(&dst_view, NULL);
1041 pipe_sampler_view_reference(&src_view, NULL);
1042 }
1043
si_do_CB_resolve(struct si_context * sctx,const struct pipe_blit_info * info,struct pipe_resource * dst,unsigned dst_level,unsigned dst_z,enum pipe_format format)1044 static void si_do_CB_resolve(struct si_context *sctx, const struct pipe_blit_info *info,
1045 struct pipe_resource *dst, unsigned dst_level, unsigned dst_z,
1046 enum pipe_format format)
1047 {
1048 /* Required before and after CB_RESOLVE. */
1049 sctx->flags |= SI_CONTEXT_FLUSH_AND_INV_CB;
1050 si_mark_atom_dirty(sctx, &sctx->atoms.s.cache_flush);
1051
1052 si_blitter_begin(
1053 sctx, SI_COLOR_RESOLVE | (info->render_condition_enable ? 0 : SI_DISABLE_RENDER_COND));
1054 util_blitter_custom_resolve_color(sctx->blitter, dst, dst_level, dst_z, info->src.resource,
1055 info->src.box.z, ~0, sctx->custom_blend_resolve, format);
1056 si_blitter_end(sctx);
1057
1058 /* Flush caches for possible texturing. */
1059 si_make_CB_shader_coherent(sctx, 1, false, true /* no DCC */);
1060 }
1061
resolve_formats_compatible(enum pipe_format src,enum pipe_format dst,bool src_swaps_rgb_to_bgr,bool * need_rgb_to_bgr)1062 static bool resolve_formats_compatible(enum pipe_format src, enum pipe_format dst,
1063 bool src_swaps_rgb_to_bgr, bool *need_rgb_to_bgr)
1064 {
1065 *need_rgb_to_bgr = false;
1066
1067 if (src_swaps_rgb_to_bgr) {
1068 /* We must only check the swapped format. */
1069 enum pipe_format swapped_src = util_format_rgb_to_bgr(src);
1070 assert(swapped_src);
1071 return util_is_format_compatible(util_format_description(swapped_src),
1072 util_format_description(dst));
1073 }
1074
1075 if (util_is_format_compatible(util_format_description(src), util_format_description(dst)))
1076 return true;
1077
1078 enum pipe_format swapped_src = util_format_rgb_to_bgr(src);
1079 *need_rgb_to_bgr = util_is_format_compatible(util_format_description(swapped_src),
1080 util_format_description(dst));
1081 return *need_rgb_to_bgr;
1082 }
1083
si_msaa_resolve_blit_via_CB(struct pipe_context * ctx,const struct pipe_blit_info * info)1084 bool si_msaa_resolve_blit_via_CB(struct pipe_context *ctx, const struct pipe_blit_info *info)
1085 {
1086 struct si_context *sctx = (struct si_context *)ctx;
1087
1088 /* Gfx11 doesn't have CB_RESOLVE. */
1089 if (sctx->gfx_level >= GFX11)
1090 return false;
1091
1092 struct si_texture *src = (struct si_texture *)info->src.resource;
1093 struct si_texture *dst = (struct si_texture *)info->dst.resource;
1094 ASSERTED struct si_texture *stmp;
1095 unsigned dst_width = u_minify(info->dst.resource->width0, info->dst.level);
1096 unsigned dst_height = u_minify(info->dst.resource->height0, info->dst.level);
1097 enum pipe_format format = info->src.format;
1098 struct pipe_resource *tmp, templ;
1099 struct pipe_blit_info blit;
1100
1101 /* Check basic requirements for hw resolve. */
1102 if (!(info->src.resource->nr_samples > 1 && info->dst.resource->nr_samples <= 1 &&
1103 !util_format_is_pure_integer(format) && !util_format_is_depth_or_stencil(format) &&
1104 util_max_layer(info->src.resource, 0) == 0))
1105 return false;
1106
1107 /* Hardware MSAA resolve doesn't work if SPI format = NORM16_ABGR and
1108 * the format is R16G16. Use R16A16, which does work.
1109 */
1110 if (format == PIPE_FORMAT_R16G16_UNORM)
1111 format = PIPE_FORMAT_R16A16_UNORM;
1112 if (format == PIPE_FORMAT_R16G16_SNORM)
1113 format = PIPE_FORMAT_R16A16_SNORM;
1114
1115 bool need_rgb_to_bgr = false;
1116
1117 /* Check the remaining requirements for hw resolve. */
1118 if (util_max_layer(info->dst.resource, info->dst.level) == 0 && !info->scissor_enable &&
1119 (info->mask & PIPE_MASK_RGBA) == PIPE_MASK_RGBA &&
1120 resolve_formats_compatible(info->src.format, info->dst.format,
1121 src->swap_rgb_to_bgr, &need_rgb_to_bgr) &&
1122 dst_width == info->src.resource->width0 && dst_height == info->src.resource->height0 &&
1123 info->dst.box.x == 0 && info->dst.box.y == 0 && info->dst.box.width == dst_width &&
1124 info->dst.box.height == dst_height && info->dst.box.depth == 1 && info->src.box.x == 0 &&
1125 info->src.box.y == 0 && info->src.box.width == dst_width &&
1126 info->src.box.height == dst_height && info->src.box.depth == 1 && !dst->surface.is_linear &&
1127 (!dst->cmask_buffer || !dst->dirty_level_mask)) { /* dst cannot be fast-cleared */
1128 /* Check the remaining constraints. */
1129 if (src->surface.micro_tile_mode != dst->surface.micro_tile_mode ||
1130 need_rgb_to_bgr) {
1131 /* The next fast clear will switch to this mode to
1132 * get direct hw resolve next time if the mode is
1133 * different now.
1134 *
1135 * TODO-GFX10: This does not work in GFX10 because MSAA
1136 * is restricted to 64KB_R_X and 64KB_Z_X swizzle modes.
1137 * In some cases we could change the swizzle of the
1138 * destination texture instead, but the more general
1139 * solution is to implement compute shader resolve.
1140 */
1141 if (src->surface.micro_tile_mode != dst->surface.micro_tile_mode)
1142 src->last_msaa_resolve_target_micro_mode = dst->surface.micro_tile_mode;
1143 if (need_rgb_to_bgr)
1144 src->swap_rgb_to_bgr_on_next_clear = true;
1145
1146 goto resolve_to_temp;
1147 }
1148
1149 /* Resolving into a surface with DCC is unsupported. Since
1150 * it's being overwritten anyway, clear it to uncompressed.
1151 * This is still the fastest codepath even with this clear.
1152 */
1153 if (vi_dcc_enabled(dst, info->dst.level)) {
1154 struct si_clear_info clear_info;
1155
1156 if (!vi_dcc_get_clear_info(sctx, dst, info->dst.level, DCC_UNCOMPRESSED, &clear_info))
1157 goto resolve_to_temp;
1158
1159 si_execute_clears(sctx, &clear_info, 1, SI_CLEAR_TYPE_DCC);
1160 dst->dirty_level_mask &= ~(1 << info->dst.level);
1161 }
1162
1163 /* Resolve directly from src to dst. */
1164 si_do_CB_resolve(sctx, info, info->dst.resource, info->dst.level, info->dst.box.z, format);
1165 return true;
1166 }
1167
1168 resolve_to_temp:
1169 /* Shader-based resolve is VERY SLOW. Instead, resolve into
1170 * a temporary texture and blit.
1171 */
1172 memset(&templ, 0, sizeof(templ));
1173 templ.target = PIPE_TEXTURE_2D;
1174 templ.format = info->src.resource->format;
1175 templ.width0 = info->src.resource->width0;
1176 templ.height0 = info->src.resource->height0;
1177 templ.depth0 = 1;
1178 templ.array_size = 1;
1179 templ.usage = PIPE_USAGE_DEFAULT;
1180 templ.flags = SI_RESOURCE_FLAG_FORCE_MSAA_TILING | SI_RESOURCE_FLAG_FORCE_MICRO_TILE_MODE |
1181 SI_RESOURCE_FLAG_MICRO_TILE_MODE_SET(src->surface.micro_tile_mode) |
1182 SI_RESOURCE_FLAG_DISABLE_DCC | SI_RESOURCE_FLAG_DRIVER_INTERNAL;
1183
1184 /* The src and dst microtile modes must be the same. */
1185 if (sctx->gfx_level <= GFX8 && src->surface.micro_tile_mode == RADEON_MICRO_MODE_DISPLAY)
1186 templ.bind = PIPE_BIND_SCANOUT;
1187 else
1188 templ.bind = 0;
1189
1190 tmp = ctx->screen->resource_create(ctx->screen, &templ);
1191 if (!tmp)
1192 return false;
1193 stmp = (struct si_texture *)tmp;
1194 /* Match the channel order of src. */
1195 stmp->swap_rgb_to_bgr = src->swap_rgb_to_bgr;
1196
1197 assert(!stmp->surface.is_linear);
1198 assert(src->surface.micro_tile_mode == stmp->surface.micro_tile_mode);
1199
1200 /* resolve */
1201 si_do_CB_resolve(sctx, info, tmp, 0, 0, format);
1202
1203 /* blit */
1204 blit = *info;
1205 blit.src.resource = tmp;
1206 blit.src.box.z = 0;
1207
1208 ctx->blit(ctx, &blit);
1209
1210 pipe_resource_reference(&tmp, NULL);
1211 return true;
1212 }
1213
si_blit(struct pipe_context * ctx,const struct pipe_blit_info * info)1214 static void si_blit(struct pipe_context *ctx, const struct pipe_blit_info *info)
1215 {
1216 struct si_context *sctx = (struct si_context *)ctx;
1217 struct si_texture *sdst = (struct si_texture *)info->dst.resource;
1218
1219 if (sctx->gfx_level >= GFX7 &&
1220 (info->dst.resource->bind & PIPE_BIND_PRIME_BLIT_DST) && sdst->surface.is_linear &&
1221 /* Use SDMA or async compute when copying to a DRI_PRIME imported linear surface. */
1222 info->dst.box.x == 0 && info->dst.box.y == 0 && info->dst.box.z == 0 &&
1223 info->src.box.x == 0 && info->src.box.y == 0 && info->src.box.z == 0 &&
1224 info->dst.level == 0 && info->src.level == 0 &&
1225 info->src.box.width == info->dst.resource->width0 &&
1226 info->src.box.height == info->dst.resource->height0 &&
1227 info->src.box.depth == 1 &&
1228 util_can_blit_via_copy_region(info, true, sctx->render_cond != NULL)) {
1229 struct si_texture *ssrc = (struct si_texture *)info->src.resource;
1230
1231 /* Try SDMA first... */
1232 if (si_sdma_copy_image(sctx, sdst, ssrc))
1233 return;
1234
1235 /* ... and use async compute as the fallback. */
1236 struct si_screen *sscreen = sctx->screen;
1237
1238 simple_mtx_lock(&sscreen->async_compute_context_lock);
1239 if (!sscreen->async_compute_context)
1240 si_init_aux_async_compute_ctx(sscreen);
1241
1242 if (sscreen->async_compute_context) {
1243 si_compute_copy_image((struct si_context*)sctx->screen->async_compute_context,
1244 info->dst.resource, 0, info->src.resource, 0, 0, 0, 0,
1245 &info->src.box, 0);
1246 si_flush_gfx_cs((struct si_context*)sctx->screen->async_compute_context, 0, NULL);
1247 simple_mtx_unlock(&sscreen->async_compute_context_lock);
1248 return;
1249 }
1250
1251 simple_mtx_unlock(&sscreen->async_compute_context_lock);
1252 }
1253
1254 if (unlikely(sctx->sqtt_enabled))
1255 sctx->sqtt_next_event = EventCmdResolveImage;
1256
1257 if (si_msaa_resolve_blit_via_CB(ctx, info))
1258 return;
1259
1260 if (unlikely(sctx->sqtt_enabled))
1261 sctx->sqtt_next_event = EventCmdCopyImage;
1262
1263 /* Using compute for copying to a linear texture in GTT is much faster than
1264 * going through RBs (render backends). This improves DRI PRIME performance.
1265 */
1266 if (util_can_blit_via_copy_region(info, false, sctx->render_cond != NULL)) {
1267 si_resource_copy_region(ctx, info->dst.resource, info->dst.level,
1268 info->dst.box.x, info->dst.box.y, info->dst.box.z,
1269 info->src.resource, info->src.level, &info->src.box);
1270 return;
1271 }
1272
1273 if (si_compute_blit(sctx, info, false))
1274 return;
1275
1276 si_gfx_blit(ctx, info);
1277 }
1278
si_gfx_blit(struct pipe_context * ctx,const struct pipe_blit_info * info)1279 void si_gfx_blit(struct pipe_context *ctx, const struct pipe_blit_info *info)
1280 {
1281 struct si_context *sctx = (struct si_context *)ctx;
1282
1283 assert(util_blitter_is_blit_supported(sctx->blitter, info));
1284
1285 /* The driver doesn't decompress resources automatically while
1286 * u_blitter is rendering. */
1287 vi_disable_dcc_if_incompatible_format(sctx, info->src.resource, info->src.level,
1288 info->src.format);
1289 vi_disable_dcc_if_incompatible_format(sctx, info->dst.resource, info->dst.level,
1290 info->dst.format);
1291 si_decompress_subresource(ctx, info->src.resource, PIPE_MASK_RGBAZS, info->src.level,
1292 info->src.box.z, info->src.box.z + info->src.box.depth - 1,
1293 false);
1294
1295 if (unlikely(sctx->sqtt_enabled))
1296 sctx->sqtt_next_event = EventCmdBlitImage;
1297
1298 si_blitter_begin(sctx, SI_BLIT | (info->render_condition_enable ? 0 : SI_DISABLE_RENDER_COND));
1299 util_blitter_blit(sctx->blitter, info);
1300 si_blitter_end(sctx);
1301 }
1302
si_generate_mipmap(struct pipe_context * ctx,struct pipe_resource * tex,enum pipe_format format,unsigned base_level,unsigned last_level,unsigned first_layer,unsigned last_layer)1303 static bool si_generate_mipmap(struct pipe_context *ctx, struct pipe_resource *tex,
1304 enum pipe_format format, unsigned base_level, unsigned last_level,
1305 unsigned first_layer, unsigned last_layer)
1306 {
1307 struct si_context *sctx = (struct si_context *)ctx;
1308 struct si_texture *stex = (struct si_texture *)tex;
1309
1310 if (!util_blitter_is_copy_supported(sctx->blitter, tex, tex))
1311 return false;
1312
1313 /* The driver doesn't decompress resources automatically while
1314 * u_blitter is rendering. */
1315 vi_disable_dcc_if_incompatible_format(sctx, tex, base_level, format);
1316 si_decompress_subresource(ctx, tex, PIPE_MASK_RGBAZS, base_level, first_layer, last_layer,
1317 false);
1318
1319 /* Clear dirty_level_mask for the levels that will be overwritten. */
1320 assert(base_level < last_level);
1321 stex->dirty_level_mask &= ~u_bit_consecutive(base_level + 1, last_level - base_level);
1322
1323 sctx->generate_mipmap_for_depth = stex->is_depth;
1324
1325 si_blitter_begin(sctx, SI_BLIT | SI_DISABLE_RENDER_COND);
1326 util_blitter_generate_mipmap(sctx->blitter, tex, format, base_level, last_level, first_layer,
1327 last_layer);
1328 si_blitter_end(sctx);
1329
1330 sctx->generate_mipmap_for_depth = false;
1331 return true;
1332 }
1333
si_flush_resource(struct pipe_context * ctx,struct pipe_resource * res)1334 static void si_flush_resource(struct pipe_context *ctx, struct pipe_resource *res)
1335 {
1336 struct si_context *sctx = (struct si_context *)ctx;
1337 struct si_texture *tex = (struct si_texture *)res;
1338
1339 if (res->target == PIPE_BUFFER)
1340 return;
1341
1342 if (!tex->is_depth && (tex->cmask_buffer || vi_dcc_enabled(tex, 0))) {
1343 si_blit_decompress_color(sctx, tex, 0, res->last_level, 0, util_max_layer(res, 0),
1344 false, false);
1345
1346 if (tex->surface.display_dcc_offset && tex->displayable_dcc_dirty) {
1347 si_retile_dcc(sctx, tex);
1348 tex->displayable_dcc_dirty = false;
1349 }
1350 }
1351 }
1352
si_flush_implicit_resources(struct si_context * sctx)1353 void si_flush_implicit_resources(struct si_context *sctx)
1354 {
1355 hash_table_foreach(sctx->dirty_implicit_resources, entry) {
1356 si_flush_resource(&sctx->b, entry->data);
1357 pipe_resource_reference((struct pipe_resource **)&entry->data, NULL);
1358 }
1359 _mesa_hash_table_clear(sctx->dirty_implicit_resources, NULL);
1360 }
1361
si_decompress_dcc(struct si_context * sctx,struct si_texture * tex)1362 void si_decompress_dcc(struct si_context *sctx, struct si_texture *tex)
1363 {
1364 assert(!tex->is_depth);
1365
1366 /* If graphics is disabled, we can't decompress DCC, but it shouldn't
1367 * be compressed either. The caller should simply discard it.
1368 * If blitter is running, we can't decompress DCC either because it
1369 * will cause a blitter recursion.
1370 */
1371 if (!tex->surface.meta_offset || !sctx->has_graphics || sctx->blitter_running)
1372 return;
1373
1374 si_blit_decompress_color(sctx, tex, 0, tex->buffer.b.b.last_level, 0,
1375 util_max_layer(&tex->buffer.b.b, 0), true, false);
1376 }
1377
si_init_blit_functions(struct si_context * sctx)1378 void si_init_blit_functions(struct si_context *sctx)
1379 {
1380 sctx->b.resource_copy_region = si_resource_copy_region;
1381
1382 if (sctx->has_graphics) {
1383 sctx->b.blit = si_blit;
1384 sctx->b.flush_resource = si_flush_resource;
1385 sctx->b.generate_mipmap = si_generate_mipmap;
1386 }
1387 }
1388