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