1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 * Copyright 2018 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 "drm-uapi/drm_fourcc.h"
27 #include "si_pipe.h"
28 #include "si_query.h"
29 #include "sid.h"
30 #include "frontend/drm_driver.h"
31 #include "util/format/u_format.h"
32 #include "util/os_time.h"
33 #include "util/u_log.h"
34 #include "util/u_memory.h"
35 #include "util/u_pack_color.h"
36 #include "util/u_resource.h"
37 #include "util/u_surface.h"
38 #include "util/u_transfer.h"
39
40 #include <errno.h>
41 #include <inttypes.h>
42
43 #include "amd/addrlib/inc/addrinterface.h"
44
45 static enum radeon_surf_mode si_choose_tiling(struct si_screen *sscreen,
46 const struct pipe_resource *templ,
47 bool tc_compatible_htile);
48
si_prepare_for_dma_blit(struct si_context * sctx,struct si_texture * dst,unsigned dst_level,unsigned dstx,unsigned dsty,unsigned dstz,struct si_texture * src,unsigned src_level,const struct pipe_box * src_box)49 bool si_prepare_for_dma_blit(struct si_context *sctx, struct si_texture *dst, unsigned dst_level,
50 unsigned dstx, unsigned dsty, unsigned dstz, struct si_texture *src,
51 unsigned src_level, const struct pipe_box *src_box)
52 {
53 if (!sctx->sdma_cs)
54 return false;
55
56 if (dst->surface.bpe != src->surface.bpe)
57 return false;
58
59 /* MSAA: Blits don't exist in the real world. */
60 if (src->buffer.b.b.nr_samples > 1 || dst->buffer.b.b.nr_samples > 1)
61 return false;
62
63 /* Depth-stencil surfaces:
64 * When dst is linear, the DB->CB copy preserves HTILE.
65 * When dst is tiled, the 3D path must be used to update HTILE.
66 */
67 if (src->is_depth || dst->is_depth)
68 return false;
69
70 /* DCC as:
71 * src: Use the 3D path. DCC decompression is expensive.
72 * dst: Use the 3D path to compress the pixels with DCC.
73 */
74 if (vi_dcc_enabled(src, src_level) || vi_dcc_enabled(dst, dst_level))
75 return false;
76
77 /* TMZ: mixing encrypted and non-encrypted buffer in a single command
78 * doesn't seem supported.
79 */
80 if ((src->buffer.flags & RADEON_FLAG_ENCRYPTED) !=
81 (dst->buffer.flags & RADEON_FLAG_ENCRYPTED))
82 return false;
83
84 /* CMASK as:
85 * src: Both texture and SDMA paths need decompression. Use SDMA.
86 * dst: If overwriting the whole texture, discard CMASK and use
87 * SDMA. Otherwise, use the 3D path.
88 */
89 if (dst->cmask_buffer && dst->dirty_level_mask & (1 << dst_level)) {
90 /* The CMASK clear is only enabled for the first level. */
91 assert(dst_level == 0);
92 if (!util_texrange_covers_whole_level(&dst->buffer.b.b, dst_level, dstx, dsty, dstz,
93 src_box->width, src_box->height, src_box->depth))
94 return false;
95
96 si_texture_discard_cmask(sctx->screen, dst);
97 }
98
99 /* All requirements are met. Prepare textures for SDMA. */
100 if (src->cmask_buffer && src->dirty_level_mask & (1 << src_level))
101 sctx->b.flush_resource(&sctx->b, &src->buffer.b.b);
102
103 assert(!(src->dirty_level_mask & (1 << src_level)));
104 assert(!(dst->dirty_level_mask & (1 << dst_level)));
105
106 return true;
107 }
108
109 /* Same as resource_copy_region, except that both upsampling and downsampling are allowed. */
si_copy_region_with_blit(struct pipe_context * pipe,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)110 static void si_copy_region_with_blit(struct pipe_context *pipe, struct pipe_resource *dst,
111 unsigned dst_level, unsigned dstx, unsigned dsty,
112 unsigned dstz, struct pipe_resource *src, unsigned src_level,
113 const struct pipe_box *src_box)
114 {
115 struct pipe_blit_info blit;
116
117 memset(&blit, 0, sizeof(blit));
118 blit.src.resource = src;
119 blit.src.format = src->format;
120 blit.src.level = src_level;
121 blit.src.box = *src_box;
122 blit.dst.resource = dst;
123 blit.dst.format = dst->format;
124 blit.dst.level = dst_level;
125 blit.dst.box.x = dstx;
126 blit.dst.box.y = dsty;
127 blit.dst.box.z = dstz;
128 blit.dst.box.width = src_box->width;
129 blit.dst.box.height = src_box->height;
130 blit.dst.box.depth = src_box->depth;
131 blit.mask = util_format_get_mask(dst->format);
132 blit.filter = PIPE_TEX_FILTER_NEAREST;
133
134 if (blit.mask) {
135 pipe->blit(pipe, &blit);
136 }
137 }
138
139 /* Copy from a full GPU texture to a transfer's staging one. */
si_copy_to_staging_texture(struct pipe_context * ctx,struct si_transfer * stransfer)140 static void si_copy_to_staging_texture(struct pipe_context *ctx, struct si_transfer *stransfer)
141 {
142 struct si_context *sctx = (struct si_context *)ctx;
143 struct pipe_transfer *transfer = (struct pipe_transfer *)stransfer;
144 struct pipe_resource *dst = &stransfer->staging->b.b;
145 struct pipe_resource *src = transfer->resource;
146
147 if (src->nr_samples > 1 || ((struct si_texture *)src)->is_depth) {
148 si_copy_region_with_blit(ctx, dst, 0, 0, 0, 0, src, transfer->level, &transfer->box);
149 return;
150 }
151
152 sctx->dma_copy(ctx, dst, 0, 0, 0, 0, src, transfer->level, &transfer->box);
153 }
154
155 /* Copy from a transfer's staging texture to a full GPU one. */
si_copy_from_staging_texture(struct pipe_context * ctx,struct si_transfer * stransfer)156 static void si_copy_from_staging_texture(struct pipe_context *ctx, struct si_transfer *stransfer)
157 {
158 struct si_context *sctx = (struct si_context *)ctx;
159 struct pipe_transfer *transfer = (struct pipe_transfer *)stransfer;
160 struct pipe_resource *dst = transfer->resource;
161 struct pipe_resource *src = &stransfer->staging->b.b;
162 struct pipe_box sbox;
163
164 u_box_3d(0, 0, 0, transfer->box.width, transfer->box.height, transfer->box.depth, &sbox);
165
166 if (dst->nr_samples > 1 || ((struct si_texture *)dst)->is_depth) {
167 si_copy_region_with_blit(ctx, dst, transfer->level, transfer->box.x, transfer->box.y,
168 transfer->box.z, src, 0, &sbox);
169 return;
170 }
171
172 if (util_format_is_compressed(dst->format)) {
173 sbox.width = util_format_get_nblocksx(dst->format, sbox.width);
174 sbox.height = util_format_get_nblocksx(dst->format, sbox.height);
175 }
176
177 sctx->dma_copy(ctx, dst, transfer->level, transfer->box.x, transfer->box.y, transfer->box.z, src,
178 0, &sbox);
179 }
180
si_texture_get_offset(struct si_screen * sscreen,struct si_texture * tex,unsigned level,const struct pipe_box * box,unsigned * stride,unsigned * layer_stride)181 static unsigned si_texture_get_offset(struct si_screen *sscreen, struct si_texture *tex,
182 unsigned level, const struct pipe_box *box, unsigned *stride,
183 unsigned *layer_stride)
184 {
185 if (sscreen->info.chip_class >= GFX9) {
186 *stride = tex->surface.u.gfx9.surf_pitch * tex->surface.bpe;
187 *layer_stride = tex->surface.u.gfx9.surf_slice_size;
188
189 if (!box)
190 return 0;
191
192 /* Each texture is an array of slices. Each slice is an array
193 * of mipmap levels. */
194 return tex->surface.u.gfx9.surf_offset + box->z * tex->surface.u.gfx9.surf_slice_size +
195 tex->surface.u.gfx9.offset[level] +
196 (box->y / tex->surface.blk_h * tex->surface.u.gfx9.surf_pitch +
197 box->x / tex->surface.blk_w) *
198 tex->surface.bpe;
199 } else {
200 *stride = tex->surface.u.legacy.level[level].nblk_x * tex->surface.bpe;
201 assert((uint64_t)tex->surface.u.legacy.level[level].slice_size_dw * 4 <= UINT_MAX);
202 *layer_stride = (uint64_t)tex->surface.u.legacy.level[level].slice_size_dw * 4;
203
204 if (!box)
205 return tex->surface.u.legacy.level[level].offset;
206
207 /* Each texture is an array of mipmap levels. Each level is
208 * an array of slices. */
209 return tex->surface.u.legacy.level[level].offset +
210 box->z * (uint64_t)tex->surface.u.legacy.level[level].slice_size_dw * 4 +
211 (box->y / tex->surface.blk_h * tex->surface.u.legacy.level[level].nblk_x +
212 box->x / tex->surface.blk_w) *
213 tex->surface.bpe;
214 }
215 }
216
si_init_surface(struct si_screen * sscreen,struct radeon_surf * surface,const struct pipe_resource * ptex,enum radeon_surf_mode array_mode,bool is_imported,bool is_scanout,bool is_flushed_depth,bool tc_compatible_htile)217 static int si_init_surface(struct si_screen *sscreen, struct radeon_surf *surface,
218 const struct pipe_resource *ptex, enum radeon_surf_mode array_mode,
219 bool is_imported, bool is_scanout, bool is_flushed_depth,
220 bool tc_compatible_htile)
221 {
222 const struct util_format_description *desc = util_format_description(ptex->format);
223 bool is_depth, is_stencil;
224 int r;
225 unsigned bpe, flags = 0;
226
227 is_depth = util_format_has_depth(desc);
228 is_stencil = util_format_has_stencil(desc);
229
230 if (!is_flushed_depth && ptex->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT) {
231 bpe = 4; /* stencil is allocated separately */
232 } else {
233 bpe = util_format_get_blocksize(ptex->format);
234 assert(util_is_power_of_two_or_zero(bpe));
235 }
236
237 if (!is_flushed_depth && is_depth) {
238 flags |= RADEON_SURF_ZBUFFER;
239
240 if ((sscreen->debug_flags & DBG(NO_HYPERZ)) ||
241 (ptex->bind & PIPE_BIND_SHARED) || is_imported) {
242 flags |= RADEON_SURF_NO_HTILE;
243 } else if (tc_compatible_htile &&
244 (sscreen->info.chip_class >= GFX9 || array_mode == RADEON_SURF_MODE_2D)) {
245 /* TC-compatible HTILE only supports Z32_FLOAT.
246 * GFX9 also supports Z16_UNORM.
247 * On GFX8, promote Z16 to Z32. DB->CB copies will convert
248 * the format for transfers.
249 */
250 if (sscreen->info.chip_class == GFX8)
251 bpe = 4;
252
253 flags |= RADEON_SURF_TC_COMPATIBLE_HTILE;
254 }
255
256 if (is_stencil)
257 flags |= RADEON_SURF_SBUFFER;
258 }
259
260 if (sscreen->info.chip_class >= GFX8 &&
261 (ptex->flags & SI_RESOURCE_FLAG_DISABLE_DCC ||
262 (sscreen->info.chip_class < GFX10_3 &&
263 ptex->format == PIPE_FORMAT_R9G9B9E5_FLOAT) ||
264 (ptex->nr_samples >= 2 && !sscreen->dcc_msaa_allowed)))
265 flags |= RADEON_SURF_DISABLE_DCC;
266
267 /* Stoney: 128bpp MSAA textures randomly fail piglit tests with DCC. */
268 if (sscreen->info.family == CHIP_STONEY && bpe == 16 && ptex->nr_samples >= 2)
269 flags |= RADEON_SURF_DISABLE_DCC;
270
271 /* GFX8: DCC clear for 4x and 8x MSAA array textures unimplemented. */
272 if (sscreen->info.chip_class == GFX8 && ptex->nr_storage_samples >= 4 && ptex->array_size > 1)
273 flags |= RADEON_SURF_DISABLE_DCC;
274
275 /* GFX9: DCC clear for 4x and 8x MSAA textures unimplemented. */
276 if (sscreen->info.chip_class == GFX9 &&
277 (ptex->nr_storage_samples >= 4 ||
278 (sscreen->info.family == CHIP_RAVEN && ptex->nr_storage_samples >= 2 && bpe < 4)))
279 flags |= RADEON_SURF_DISABLE_DCC;
280
281 /* TODO: GFX10: DCC causes corruption with MSAA. */
282 if (sscreen->info.chip_class >= GFX10 && ptex->nr_storage_samples >= 2)
283 flags |= RADEON_SURF_DISABLE_DCC;
284
285 /* Shared textures must always set up DCC.
286 * If it's not present, it will be disabled by
287 * si_get_opaque_metadata later.
288 */
289 if (!is_imported && (sscreen->debug_flags & DBG(NO_DCC)))
290 flags |= RADEON_SURF_DISABLE_DCC;
291
292 if (is_scanout) {
293 /* This should catch bugs in gallium users setting incorrect flags. */
294 assert(ptex->nr_samples <= 1 && ptex->array_size == 1 && ptex->depth0 == 1 &&
295 ptex->last_level == 0 && !(flags & RADEON_SURF_Z_OR_SBUFFER));
296
297 flags |= RADEON_SURF_SCANOUT;
298 }
299
300 if (ptex->bind & PIPE_BIND_SHARED)
301 flags |= RADEON_SURF_SHAREABLE;
302 if (is_imported)
303 flags |= RADEON_SURF_IMPORTED | RADEON_SURF_SHAREABLE;
304 if (sscreen->debug_flags & DBG(NO_FMASK))
305 flags |= RADEON_SURF_NO_FMASK;
306
307 if (sscreen->info.chip_class == GFX9 && (ptex->flags & SI_RESOURCE_FLAG_FORCE_MICRO_TILE_MODE)) {
308 flags |= RADEON_SURF_FORCE_MICRO_TILE_MODE;
309 surface->micro_tile_mode = SI_RESOURCE_FLAG_MICRO_TILE_MODE_GET(ptex->flags);
310 }
311
312 if (ptex->flags & SI_RESOURCE_FLAG_FORCE_MSAA_TILING) {
313 flags |= RADEON_SURF_FORCE_SWIZZLE_MODE;
314
315 if (sscreen->info.chip_class >= GFX10)
316 surface->u.gfx9.surf.swizzle_mode = ADDR_SW_64KB_R_X;
317 }
318
319 r = sscreen->ws->surface_init(sscreen->ws, ptex, flags, bpe, array_mode, surface);
320 if (r) {
321 return r;
322 }
323
324 return 0;
325 }
326
si_eliminate_fast_color_clear(struct si_context * sctx,struct si_texture * tex,bool * ctx_flushed)327 void si_eliminate_fast_color_clear(struct si_context *sctx, struct si_texture *tex,
328 bool *ctx_flushed)
329 {
330 struct si_screen *sscreen = sctx->screen;
331 struct pipe_context *ctx = &sctx->b;
332
333 if (ctx == sscreen->aux_context)
334 simple_mtx_lock(&sscreen->aux_context_lock);
335
336 unsigned n = sctx->num_decompress_calls;
337 ctx->flush_resource(ctx, &tex->buffer.b.b);
338
339 /* Flush only if any fast clear elimination took place. */
340 bool flushed = false;
341 if (n != sctx->num_decompress_calls)
342 {
343 ctx->flush(ctx, NULL, 0);
344 flushed = true;
345 }
346 if (ctx_flushed)
347 *ctx_flushed = flushed;
348
349 if (ctx == sscreen->aux_context)
350 simple_mtx_unlock(&sscreen->aux_context_lock);
351 }
352
si_texture_discard_cmask(struct si_screen * sscreen,struct si_texture * tex)353 void si_texture_discard_cmask(struct si_screen *sscreen, struct si_texture *tex)
354 {
355 if (!tex->cmask_buffer)
356 return;
357
358 assert(tex->buffer.b.b.nr_samples <= 1);
359
360 /* Disable CMASK. */
361 tex->cmask_base_address_reg = tex->buffer.gpu_address >> 8;
362 tex->dirty_level_mask = 0;
363
364 tex->cb_color_info &= ~S_028C70_FAST_CLEAR(1);
365
366 if (tex->cmask_buffer != &tex->buffer)
367 si_resource_reference(&tex->cmask_buffer, NULL);
368
369 tex->cmask_buffer = NULL;
370
371 /* Notify all contexts about the change. */
372 p_atomic_inc(&sscreen->dirty_tex_counter);
373 p_atomic_inc(&sscreen->compressed_colortex_counter);
374 }
375
si_can_disable_dcc(struct si_texture * tex)376 static bool si_can_disable_dcc(struct si_texture *tex)
377 {
378 /* We can't disable DCC if it can be written by another process. */
379 return tex->surface.dcc_offset &&
380 (!tex->buffer.b.is_shared ||
381 !(tex->buffer.external_usage & PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE));
382 }
383
si_texture_discard_dcc(struct si_screen * sscreen,struct si_texture * tex)384 static bool si_texture_discard_dcc(struct si_screen *sscreen, struct si_texture *tex)
385 {
386 if (!si_can_disable_dcc(tex))
387 return false;
388
389 assert(tex->dcc_separate_buffer == NULL);
390
391 /* Disable DCC. */
392 ac_surface_zero_dcc_fields(&tex->surface);
393
394 /* Notify all contexts about the change. */
395 p_atomic_inc(&sscreen->dirty_tex_counter);
396 return true;
397 }
398
399 /**
400 * Disable DCC for the texture. (first decompress, then discard metadata).
401 *
402 * There is unresolved multi-context synchronization issue between
403 * screen::aux_context and the current context. If applications do this with
404 * multiple contexts, it's already undefined behavior for them and we don't
405 * have to worry about that. The scenario is:
406 *
407 * If context 1 disables DCC and context 2 has queued commands that write
408 * to the texture via CB with DCC enabled, and the order of operations is
409 * as follows:
410 * context 2 queues draw calls rendering to the texture, but doesn't flush
411 * context 1 disables DCC and flushes
412 * context 1 & 2 reset descriptors and FB state
413 * context 2 flushes (new compressed tiles written by the draw calls)
414 * context 1 & 2 read garbage, because DCC is disabled, yet there are
415 * compressed tiled
416 *
417 * \param sctx the current context if you have one, or sscreen->aux_context
418 * if you don't.
419 */
si_texture_disable_dcc(struct si_context * sctx,struct si_texture * tex)420 bool si_texture_disable_dcc(struct si_context *sctx, struct si_texture *tex)
421 {
422 struct si_screen *sscreen = sctx->screen;
423
424 if (!sctx->has_graphics)
425 return si_texture_discard_dcc(sscreen, tex);
426
427 if (!si_can_disable_dcc(tex))
428 return false;
429
430 if (&sctx->b == sscreen->aux_context)
431 simple_mtx_lock(&sscreen->aux_context_lock);
432
433 /* Decompress DCC. */
434 si_decompress_dcc(sctx, tex);
435 sctx->b.flush(&sctx->b, NULL, 0);
436
437 if (&sctx->b == sscreen->aux_context)
438 simple_mtx_unlock(&sscreen->aux_context_lock);
439
440 return si_texture_discard_dcc(sscreen, tex);
441 }
442
si_reallocate_texture_inplace(struct si_context * sctx,struct si_texture * tex,unsigned new_bind_flag,bool invalidate_storage)443 static void si_reallocate_texture_inplace(struct si_context *sctx, struct si_texture *tex,
444 unsigned new_bind_flag, bool invalidate_storage)
445 {
446 struct pipe_screen *screen = sctx->b.screen;
447 struct si_texture *new_tex;
448 struct pipe_resource templ = tex->buffer.b.b;
449 unsigned i;
450
451 templ.bind |= new_bind_flag;
452
453 if (tex->buffer.b.is_shared || tex->num_planes > 1)
454 return;
455
456 if (new_bind_flag == PIPE_BIND_LINEAR) {
457 if (tex->surface.is_linear)
458 return;
459
460 /* This fails with MSAA, depth, and compressed textures. */
461 if (si_choose_tiling(sctx->screen, &templ, false) != RADEON_SURF_MODE_LINEAR_ALIGNED)
462 return;
463 }
464
465 new_tex = (struct si_texture *)screen->resource_create(screen, &templ);
466 if (!new_tex)
467 return;
468
469 /* Copy the pixels to the new texture. */
470 if (!invalidate_storage) {
471 for (i = 0; i <= templ.last_level; i++) {
472 struct pipe_box box;
473
474 u_box_3d(0, 0, 0, u_minify(templ.width0, i), u_minify(templ.height0, i),
475 util_num_layers(&templ, i), &box);
476
477 sctx->dma_copy(&sctx->b, &new_tex->buffer.b.b, i, 0, 0, 0, &tex->buffer.b.b, i, &box);
478 }
479 }
480
481 if (new_bind_flag == PIPE_BIND_LINEAR) {
482 si_texture_discard_cmask(sctx->screen, tex);
483 si_texture_discard_dcc(sctx->screen, tex);
484 }
485
486 /* Replace the structure fields of tex. */
487 tex->buffer.b.b.bind = templ.bind;
488 pb_reference(&tex->buffer.buf, new_tex->buffer.buf);
489 tex->buffer.gpu_address = new_tex->buffer.gpu_address;
490 tex->buffer.vram_usage = new_tex->buffer.vram_usage;
491 tex->buffer.gart_usage = new_tex->buffer.gart_usage;
492 tex->buffer.bo_size = new_tex->buffer.bo_size;
493 tex->buffer.bo_alignment = new_tex->buffer.bo_alignment;
494 tex->buffer.domains = new_tex->buffer.domains;
495 tex->buffer.flags = new_tex->buffer.flags;
496
497 tex->surface = new_tex->surface;
498 si_texture_reference(&tex->flushed_depth_texture, new_tex->flushed_depth_texture);
499
500 tex->surface.fmask_offset = new_tex->surface.fmask_offset;
501 tex->surface.cmask_offset = new_tex->surface.cmask_offset;
502 tex->cmask_base_address_reg = new_tex->cmask_base_address_reg;
503
504 if (tex->cmask_buffer == &tex->buffer)
505 tex->cmask_buffer = NULL;
506 else
507 si_resource_reference(&tex->cmask_buffer, NULL);
508
509 if (new_tex->cmask_buffer == &new_tex->buffer)
510 tex->cmask_buffer = &tex->buffer;
511 else
512 si_resource_reference(&tex->cmask_buffer, new_tex->cmask_buffer);
513
514 tex->surface.dcc_offset = new_tex->surface.dcc_offset;
515 tex->cb_color_info = new_tex->cb_color_info;
516 memcpy(tex->color_clear_value, new_tex->color_clear_value, sizeof(tex->color_clear_value));
517 tex->last_msaa_resolve_target_micro_mode = new_tex->last_msaa_resolve_target_micro_mode;
518
519 tex->surface.htile_offset = new_tex->surface.htile_offset;
520 tex->depth_clear_value = new_tex->depth_clear_value;
521 tex->dirty_level_mask = new_tex->dirty_level_mask;
522 tex->stencil_dirty_level_mask = new_tex->stencil_dirty_level_mask;
523 tex->db_render_format = new_tex->db_render_format;
524 tex->stencil_clear_value = new_tex->stencil_clear_value;
525 tex->tc_compatible_htile = new_tex->tc_compatible_htile;
526 tex->depth_cleared = new_tex->depth_cleared;
527 tex->stencil_cleared = new_tex->stencil_cleared;
528 tex->upgraded_depth = new_tex->upgraded_depth;
529 tex->db_compatible = new_tex->db_compatible;
530 tex->can_sample_z = new_tex->can_sample_z;
531 tex->can_sample_s = new_tex->can_sample_s;
532
533 tex->separate_dcc_dirty = new_tex->separate_dcc_dirty;
534 tex->displayable_dcc_dirty = new_tex->displayable_dcc_dirty;
535 tex->dcc_gather_statistics = new_tex->dcc_gather_statistics;
536 si_resource_reference(&tex->dcc_separate_buffer, new_tex->dcc_separate_buffer);
537 si_resource_reference(&tex->last_dcc_separate_buffer, new_tex->last_dcc_separate_buffer);
538 si_resource_reference(&tex->dcc_retile_buffer, new_tex->dcc_retile_buffer);
539
540 if (new_bind_flag == PIPE_BIND_LINEAR) {
541 assert(!tex->surface.htile_offset);
542 assert(!tex->cmask_buffer);
543 assert(!tex->surface.fmask_size);
544 assert(!tex->surface.dcc_offset);
545 assert(!tex->is_depth);
546 }
547
548 si_texture_reference(&new_tex, NULL);
549
550 p_atomic_inc(&sctx->screen->dirty_tex_counter);
551 }
552
si_set_tex_bo_metadata(struct si_screen * sscreen,struct si_texture * tex)553 static void si_set_tex_bo_metadata(struct si_screen *sscreen, struct si_texture *tex)
554 {
555 struct pipe_resource *res = &tex->buffer.b.b;
556 struct radeon_bo_metadata md;
557
558 memset(&md, 0, sizeof(md));
559
560 assert(tex->dcc_separate_buffer == NULL);
561 assert(tex->surface.fmask_size == 0);
562
563 static const unsigned char swizzle[] = {PIPE_SWIZZLE_X, PIPE_SWIZZLE_Y, PIPE_SWIZZLE_Z,
564 PIPE_SWIZZLE_W};
565 bool is_array = util_texture_is_array(res->target);
566 uint32_t desc[8];
567
568 sscreen->make_texture_descriptor(sscreen, tex, true, res->target, res->format, swizzle, 0,
569 res->last_level, 0, is_array ? res->array_size - 1 : 0,
570 res->width0, res->height0, res->depth0, desc, NULL);
571 si_set_mutable_tex_desc_fields(sscreen, tex, &tex->surface.u.legacy.level[0], 0, 0,
572 tex->surface.blk_w, false, false, desc);
573
574 ac_surface_get_umd_metadata(&sscreen->info, &tex->surface,
575 tex->buffer.b.b.last_level + 1,
576 desc, &md.size_metadata, md.metadata);
577 sscreen->ws->buffer_set_metadata(tex->buffer.buf, &md, &tex->surface);
578 }
579
si_has_displayable_dcc(struct si_texture * tex)580 static bool si_has_displayable_dcc(struct si_texture *tex)
581 {
582 struct si_screen *sscreen = (struct si_screen *)tex->buffer.b.b.screen;
583
584 if (sscreen->info.chip_class <= GFX8)
585 return false;
586
587 return tex->surface.is_displayable && tex->surface.dcc_offset;
588 }
589
si_resource_get_param(struct pipe_screen * screen,struct pipe_context * context,struct pipe_resource * resource,unsigned plane,unsigned layer,unsigned level,enum pipe_resource_param param,unsigned handle_usage,uint64_t * value)590 static bool si_resource_get_param(struct pipe_screen *screen, struct pipe_context *context,
591 struct pipe_resource *resource, unsigned plane, unsigned layer,
592 unsigned level,
593 enum pipe_resource_param param, unsigned handle_usage,
594 uint64_t *value)
595 {
596 for (unsigned i = 0; i < plane; i++)
597 resource = resource->next;
598
599 struct si_screen *sscreen = (struct si_screen *)screen;
600 struct si_texture *tex = (struct si_texture *)resource;
601 struct winsys_handle whandle;
602
603 switch (param) {
604 case PIPE_RESOURCE_PARAM_NPLANES:
605 *value = resource->target == PIPE_BUFFER ? 1 : tex->num_planes;
606 return true;
607
608 case PIPE_RESOURCE_PARAM_STRIDE:
609 if (resource->target == PIPE_BUFFER)
610 *value = 0;
611 else if (sscreen->info.chip_class >= GFX9)
612 *value = tex->surface.u.gfx9.surf_pitch * tex->surface.bpe;
613 else
614 *value = tex->surface.u.legacy.level[0].nblk_x * tex->surface.bpe;
615 return true;
616
617 case PIPE_RESOURCE_PARAM_OFFSET:
618 if (resource->target == PIPE_BUFFER)
619 *value = 0;
620 else if (sscreen->info.chip_class >= GFX9)
621 *value = tex->surface.u.gfx9.surf_offset + layer * tex->surface.u.gfx9.surf_slice_size;
622 else
623 *value = tex->surface.u.legacy.level[0].offset +
624 layer * (uint64_t)tex->surface.u.legacy.level[0].slice_size_dw * 4;
625 return true;
626
627 case PIPE_RESOURCE_PARAM_MODIFIER:
628 *value = DRM_FORMAT_MOD_INVALID;
629 return true;
630
631 case PIPE_RESOURCE_PARAM_HANDLE_TYPE_SHARED:
632 case PIPE_RESOURCE_PARAM_HANDLE_TYPE_KMS:
633 case PIPE_RESOURCE_PARAM_HANDLE_TYPE_FD:
634 memset(&whandle, 0, sizeof(whandle));
635
636 if (param == PIPE_RESOURCE_PARAM_HANDLE_TYPE_SHARED)
637 whandle.type = WINSYS_HANDLE_TYPE_SHARED;
638 else if (param == PIPE_RESOURCE_PARAM_HANDLE_TYPE_KMS)
639 whandle.type = WINSYS_HANDLE_TYPE_KMS;
640 else if (param == PIPE_RESOURCE_PARAM_HANDLE_TYPE_FD)
641 whandle.type = WINSYS_HANDLE_TYPE_FD;
642
643 if (!screen->resource_get_handle(screen, context, resource, &whandle, handle_usage))
644 return false;
645
646 *value = whandle.handle;
647 return true;
648 case PIPE_RESOURCE_PARAM_LAYER_STRIDE:
649 break;
650 }
651 return false;
652 }
653
si_texture_get_info(struct pipe_screen * screen,struct pipe_resource * resource,unsigned * pstride,unsigned * poffset)654 static void si_texture_get_info(struct pipe_screen *screen, struct pipe_resource *resource,
655 unsigned *pstride, unsigned *poffset)
656 {
657 uint64_t value;
658
659 if (pstride) {
660 si_resource_get_param(screen, NULL, resource, 0, 0, 0, PIPE_RESOURCE_PARAM_STRIDE, 0, &value);
661 *pstride = value;
662 }
663
664 if (poffset) {
665 si_resource_get_param(screen, NULL, resource, 0, 0, 0, PIPE_RESOURCE_PARAM_OFFSET, 0, &value);
666 *poffset = value;
667 }
668 }
669
si_texture_get_handle(struct pipe_screen * screen,struct pipe_context * ctx,struct pipe_resource * resource,struct winsys_handle * whandle,unsigned usage)670 static bool si_texture_get_handle(struct pipe_screen *screen, struct pipe_context *ctx,
671 struct pipe_resource *resource, struct winsys_handle *whandle,
672 unsigned usage)
673 {
674 struct si_screen *sscreen = (struct si_screen *)screen;
675 struct si_context *sctx;
676 struct si_resource *res = si_resource(resource);
677 struct si_texture *tex = (struct si_texture *)resource;
678 bool update_metadata = false;
679 unsigned stride, offset, slice_size;
680 bool flush = false;
681
682 ctx = threaded_context_unwrap_sync(ctx);
683 sctx = (struct si_context *)(ctx ? ctx : sscreen->aux_context);
684
685 if (resource->target != PIPE_BUFFER) {
686 /* Individual planes are chained pipe_resource instances. */
687 for (unsigned i = 0; i < whandle->plane; i++) {
688 resource = resource->next;
689 res = si_resource(resource);
690 tex = (struct si_texture *)resource;
691 }
692
693 /* This is not supported now, but it might be required for OpenCL
694 * interop in the future.
695 */
696 if (resource->nr_samples > 1 || tex->is_depth)
697 return false;
698
699 /* Move a suballocated texture into a non-suballocated allocation. */
700 if (sscreen->ws->buffer_is_suballocated(res->buf) || tex->surface.tile_swizzle ||
701 (tex->buffer.flags & RADEON_FLAG_NO_INTERPROCESS_SHARING &&
702 sscreen->info.has_local_buffers)) {
703 assert(!res->b.is_shared);
704 si_reallocate_texture_inplace(sctx, tex, PIPE_BIND_SHARED, false);
705 flush = true;
706 assert(res->b.b.bind & PIPE_BIND_SHARED);
707 assert(res->flags & RADEON_FLAG_NO_SUBALLOC);
708 assert(!(res->flags & RADEON_FLAG_NO_INTERPROCESS_SHARING));
709 assert(tex->surface.tile_swizzle == 0);
710 }
711
712 /* Since shader image stores don't support DCC on GFX8,
713 * disable it for external clients that want write
714 * access.
715 */
716 if ((usage & PIPE_HANDLE_USAGE_SHADER_WRITE && tex->surface.dcc_offset) ||
717 /* Displayable DCC requires an explicit flush. */
718 (!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) && si_has_displayable_dcc(tex))) {
719 if (si_texture_disable_dcc(sctx, tex)) {
720 update_metadata = true;
721 /* si_texture_disable_dcc flushes the context */
722 flush = false;
723 }
724 }
725
726 if (!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) &&
727 (tex->cmask_buffer || tex->surface.dcc_offset)) {
728 /* Eliminate fast clear (both CMASK and DCC) */
729 bool flushed;
730 si_eliminate_fast_color_clear(sctx, tex, &flushed);
731 /* eliminate_fast_color_clear sometimes flushes the context */
732 if (flushed)
733 flush = false;
734
735 /* Disable CMASK if flush_resource isn't going
736 * to be called.
737 */
738 if (tex->cmask_buffer)
739 si_texture_discard_cmask(sscreen, tex);
740 }
741
742 /* Set metadata. */
743 if ((!res->b.is_shared || update_metadata) && whandle->offset == 0)
744 si_set_tex_bo_metadata(sscreen, tex);
745
746 if (sscreen->info.chip_class >= GFX9) {
747 slice_size = tex->surface.u.gfx9.surf_slice_size;
748 } else {
749 slice_size = (uint64_t)tex->surface.u.legacy.level[0].slice_size_dw * 4;
750 }
751 } else {
752 /* Buffer exports are for the OpenCL interop. */
753 /* Move a suballocated buffer into a non-suballocated allocation. */
754 if (sscreen->ws->buffer_is_suballocated(res->buf) ||
755 /* A DMABUF export always fails if the BO is local. */
756 (tex->buffer.flags & RADEON_FLAG_NO_INTERPROCESS_SHARING &&
757 sscreen->info.has_local_buffers)) {
758 assert(!res->b.is_shared);
759
760 /* Allocate a new buffer with PIPE_BIND_SHARED. */
761 struct pipe_resource templ = res->b.b;
762 templ.bind |= PIPE_BIND_SHARED;
763
764 struct pipe_resource *newb = screen->resource_create(screen, &templ);
765 if (!newb)
766 return false;
767
768 /* Copy the old buffer contents to the new one. */
769 struct pipe_box box;
770 u_box_1d(0, newb->width0, &box);
771 sctx->b.resource_copy_region(&sctx->b, newb, 0, 0, 0, 0, &res->b.b, 0, &box);
772 flush = true;
773 /* Move the new buffer storage to the old pipe_resource. */
774 si_replace_buffer_storage(&sctx->b, &res->b.b, newb);
775 pipe_resource_reference(&newb, NULL);
776
777 assert(res->b.b.bind & PIPE_BIND_SHARED);
778 assert(res->flags & RADEON_FLAG_NO_SUBALLOC);
779 }
780
781 /* Buffers */
782 slice_size = 0;
783 }
784
785 si_texture_get_info(screen, resource, &stride, &offset);
786
787 if (flush)
788 sctx->b.flush(&sctx->b, NULL, 0);
789
790 if (res->b.is_shared) {
791 /* USAGE_EXPLICIT_FLUSH must be cleared if at least one user
792 * doesn't set it.
793 */
794 res->external_usage |= usage & ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH;
795 if (!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH))
796 res->external_usage &= ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH;
797 } else {
798 res->b.is_shared = true;
799 res->external_usage = usage;
800 }
801
802 whandle->stride = stride;
803 whandle->offset = offset + slice_size * whandle->layer;
804
805 return sscreen->ws->buffer_get_handle(sscreen->ws, res->buf, whandle);
806 }
807
si_texture_destroy(struct pipe_screen * screen,struct pipe_resource * ptex)808 static void si_texture_destroy(struct pipe_screen *screen, struct pipe_resource *ptex)
809 {
810 struct si_texture *tex = (struct si_texture *)ptex;
811 struct si_resource *resource = &tex->buffer;
812
813 si_texture_reference(&tex->flushed_depth_texture, NULL);
814
815 if (tex->cmask_buffer != &tex->buffer) {
816 si_resource_reference(&tex->cmask_buffer, NULL);
817 }
818 pb_reference(&resource->buf, NULL);
819 si_resource_reference(&tex->dcc_separate_buffer, NULL);
820 si_resource_reference(&tex->last_dcc_separate_buffer, NULL);
821 si_resource_reference(&tex->dcc_retile_buffer, NULL);
822 FREE(tex);
823 }
824
825 static const struct u_resource_vtbl si_texture_vtbl;
826
si_print_texture_info(struct si_screen * sscreen,struct si_texture * tex,struct u_log_context * log)827 void si_print_texture_info(struct si_screen *sscreen, struct si_texture *tex,
828 struct u_log_context *log)
829 {
830 int i;
831
832 /* Common parameters. */
833 u_log_printf(log,
834 " Info: npix_x=%u, npix_y=%u, npix_z=%u, blk_w=%u, "
835 "blk_h=%u, array_size=%u, last_level=%u, "
836 "bpe=%u, nsamples=%u, flags=0x%x, %s\n",
837 tex->buffer.b.b.width0, tex->buffer.b.b.height0, tex->buffer.b.b.depth0,
838 tex->surface.blk_w, tex->surface.blk_h, tex->buffer.b.b.array_size,
839 tex->buffer.b.b.last_level, tex->surface.bpe, tex->buffer.b.b.nr_samples,
840 tex->surface.flags, util_format_short_name(tex->buffer.b.b.format));
841
842 if (sscreen->info.chip_class >= GFX9) {
843 u_log_printf(log,
844 " Surf: size=%" PRIu64 ", slice_size=%" PRIu64 ", "
845 "alignment=%u, swmode=%u, epitch=%u, pitch=%u\n",
846 tex->surface.surf_size, tex->surface.u.gfx9.surf_slice_size,
847 tex->surface.surf_alignment, tex->surface.u.gfx9.surf.swizzle_mode,
848 tex->surface.u.gfx9.surf.epitch, tex->surface.u.gfx9.surf_pitch);
849
850 if (tex->surface.fmask_offset) {
851 u_log_printf(log,
852 " FMASK: offset=%" PRIu64 ", size=%" PRIu64 ", "
853 "alignment=%u, swmode=%u, epitch=%u\n",
854 tex->surface.fmask_offset, tex->surface.fmask_size,
855 tex->surface.fmask_alignment, tex->surface.u.gfx9.fmask.swizzle_mode,
856 tex->surface.u.gfx9.fmask.epitch);
857 }
858
859 if (tex->cmask_buffer) {
860 u_log_printf(log,
861 " CMask: offset=%" PRIu64 ", size=%u, "
862 "alignment=%u\n",
863 tex->surface.cmask_offset, tex->surface.cmask_size,
864 tex->surface.cmask_alignment);
865 }
866
867 if (tex->surface.htile_offset) {
868 u_log_printf(log,
869 " HTile: offset=%" PRIu64 ", size=%u, alignment=%u\n",
870 tex->surface.htile_offset, tex->surface.htile_size,
871 tex->surface.htile_alignment);
872 }
873
874 if (tex->surface.dcc_offset) {
875 u_log_printf(log,
876 " DCC: offset=%" PRIu64 ", size=%u, "
877 "alignment=%u, pitch_max=%u, num_dcc_levels=%u\n",
878 tex->surface.dcc_offset, tex->surface.dcc_size, tex->surface.dcc_alignment,
879 tex->surface.u.gfx9.display_dcc_pitch_max, tex->surface.num_dcc_levels);
880 }
881
882 if (tex->surface.u.gfx9.stencil_offset) {
883 u_log_printf(log, " Stencil: offset=%" PRIu64 ", swmode=%u, epitch=%u\n",
884 tex->surface.u.gfx9.stencil_offset, tex->surface.u.gfx9.stencil.swizzle_mode,
885 tex->surface.u.gfx9.stencil.epitch);
886 }
887 return;
888 }
889
890 u_log_printf(log,
891 " Layout: size=%" PRIu64 ", alignment=%u, bankw=%u, "
892 "bankh=%u, nbanks=%u, mtilea=%u, tilesplit=%u, pipeconfig=%u, scanout=%u\n",
893 tex->surface.surf_size, tex->surface.surf_alignment, tex->surface.u.legacy.bankw,
894 tex->surface.u.legacy.bankh, tex->surface.u.legacy.num_banks,
895 tex->surface.u.legacy.mtilea, tex->surface.u.legacy.tile_split,
896 tex->surface.u.legacy.pipe_config, (tex->surface.flags & RADEON_SURF_SCANOUT) != 0);
897
898 if (tex->surface.fmask_offset)
899 u_log_printf(
900 log,
901 " FMask: offset=%" PRIu64 ", size=%" PRIu64 ", alignment=%u, pitch_in_pixels=%u, "
902 "bankh=%u, slice_tile_max=%u, tile_mode_index=%u\n",
903 tex->surface.fmask_offset, tex->surface.fmask_size, tex->surface.fmask_alignment,
904 tex->surface.u.legacy.fmask.pitch_in_pixels, tex->surface.u.legacy.fmask.bankh,
905 tex->surface.u.legacy.fmask.slice_tile_max, tex->surface.u.legacy.fmask.tiling_index);
906
907 if (tex->cmask_buffer)
908 u_log_printf(log,
909 " CMask: offset=%" PRIu64 ", size=%u, alignment=%u, "
910 "slice_tile_max=%u\n",
911 tex->surface.cmask_offset, tex->surface.cmask_size, tex->surface.cmask_alignment,
912 tex->surface.u.legacy.cmask_slice_tile_max);
913
914 if (tex->surface.htile_offset)
915 u_log_printf(log,
916 " HTile: offset=%" PRIu64 ", size=%u, "
917 "alignment=%u, TC_compatible = %u\n",
918 tex->surface.htile_offset, tex->surface.htile_size, tex->surface.htile_alignment,
919 tex->tc_compatible_htile);
920
921 if (tex->surface.dcc_offset) {
922 u_log_printf(log, " DCC: offset=%" PRIu64 ", size=%u, alignment=%u\n",
923 tex->surface.dcc_offset, tex->surface.dcc_size, tex->surface.dcc_alignment);
924 for (i = 0; i <= tex->buffer.b.b.last_level; i++)
925 u_log_printf(log,
926 " DCCLevel[%i]: enabled=%u, offset=%u, "
927 "fast_clear_size=%u\n",
928 i, i < tex->surface.num_dcc_levels, tex->surface.u.legacy.level[i].dcc_offset,
929 tex->surface.u.legacy.level[i].dcc_fast_clear_size);
930 }
931
932 for (i = 0; i <= tex->buffer.b.b.last_level; i++)
933 u_log_printf(log,
934 " Level[%i]: offset=%" PRIu64 ", slice_size=%" PRIu64 ", "
935 "npix_x=%u, npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
936 "mode=%u, tiling_index = %u\n",
937 i, tex->surface.u.legacy.level[i].offset,
938 (uint64_t)tex->surface.u.legacy.level[i].slice_size_dw * 4,
939 u_minify(tex->buffer.b.b.width0, i), u_minify(tex->buffer.b.b.height0, i),
940 u_minify(tex->buffer.b.b.depth0, i), tex->surface.u.legacy.level[i].nblk_x,
941 tex->surface.u.legacy.level[i].nblk_y, tex->surface.u.legacy.level[i].mode,
942 tex->surface.u.legacy.tiling_index[i]);
943
944 if (tex->surface.has_stencil) {
945 u_log_printf(log, " StencilLayout: tilesplit=%u\n",
946 tex->surface.u.legacy.stencil_tile_split);
947 for (i = 0; i <= tex->buffer.b.b.last_level; i++) {
948 u_log_printf(log,
949 " StencilLevel[%i]: offset=%" PRIu64 ", "
950 "slice_size=%" PRIu64 ", npix_x=%u, "
951 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
952 "mode=%u, tiling_index = %u\n",
953 i, tex->surface.u.legacy.stencil_level[i].offset,
954 (uint64_t)tex->surface.u.legacy.stencil_level[i].slice_size_dw * 4,
955 u_minify(tex->buffer.b.b.width0, i), u_minify(tex->buffer.b.b.height0, i),
956 u_minify(tex->buffer.b.b.depth0, i),
957 tex->surface.u.legacy.stencil_level[i].nblk_x,
958 tex->surface.u.legacy.stencil_level[i].nblk_y,
959 tex->surface.u.legacy.stencil_level[i].mode,
960 tex->surface.u.legacy.stencil_tiling_index[i]);
961 }
962 }
963 }
964
965 /**
966 * Common function for si_texture_create and si_texture_from_handle.
967 *
968 * \param screen screen
969 * \param base resource template
970 * \param surface radeon_surf
971 * \param plane0 if a non-zero plane is being created, this is the first plane
972 * \param imported_buf from si_texture_from_handle
973 * \param offset offset for non-zero planes or imported buffers
974 * \param alloc_size the size to allocate if plane0 != NULL
975 * \param alignment alignment for the allocation
976 */
si_texture_create_object(struct pipe_screen * screen,const struct pipe_resource * base,const struct radeon_surf * surface,const struct si_texture * plane0,struct pb_buffer * imported_buf,uint64_t offset,unsigned pitch_in_bytes,uint64_t alloc_size,unsigned alignment)977 static struct si_texture *si_texture_create_object(struct pipe_screen *screen,
978 const struct pipe_resource *base,
979 const struct radeon_surf *surface,
980 const struct si_texture *plane0,
981 struct pb_buffer *imported_buf,
982 uint64_t offset, unsigned pitch_in_bytes,
983 uint64_t alloc_size, unsigned alignment)
984 {
985 struct si_texture *tex;
986 struct si_resource *resource;
987 struct si_screen *sscreen = (struct si_screen *)screen;
988
989 tex = CALLOC_STRUCT(si_texture);
990 if (!tex)
991 goto error;
992
993 resource = &tex->buffer;
994 resource->b.b = *base;
995 resource->b.vtbl = &si_texture_vtbl;
996 pipe_reference_init(&resource->b.b.reference, 1);
997 resource->b.b.screen = screen;
998
999 /* don't include stencil-only formats which we don't support for rendering */
1000 tex->is_depth = util_format_has_depth(util_format_description(tex->buffer.b.b.format));
1001 tex->surface = *surface;
1002
1003 /* On GFX8, HTILE uses different tiling depending on the TC_COMPATIBLE_HTILE
1004 * setting, so we have to enable it if we enabled it at allocation.
1005 *
1006 * GFX9 and later use the same tiling for both, so TC-compatible HTILE can be
1007 * enabled on demand.
1008 */
1009 tex->tc_compatible_htile = sscreen->info.chip_class == GFX8 &&
1010 tex->surface.flags & RADEON_SURF_TC_COMPATIBLE_HTILE;
1011
1012 /* TC-compatible HTILE:
1013 * - GFX8 only supports Z32_FLOAT.
1014 * - GFX9 only supports Z32_FLOAT and Z16_UNORM. */
1015 if (tex->surface.flags & RADEON_SURF_TC_COMPATIBLE_HTILE) {
1016 if (sscreen->info.chip_class >= GFX9 && base->format == PIPE_FORMAT_Z16_UNORM)
1017 tex->db_render_format = base->format;
1018 else {
1019 tex->db_render_format = PIPE_FORMAT_Z32_FLOAT;
1020 tex->upgraded_depth = base->format != PIPE_FORMAT_Z32_FLOAT &&
1021 base->format != PIPE_FORMAT_Z32_FLOAT_S8X24_UINT;
1022 }
1023 } else {
1024 tex->db_render_format = base->format;
1025 }
1026
1027 /* Applies to GCN. */
1028 tex->last_msaa_resolve_target_micro_mode = tex->surface.micro_tile_mode;
1029
1030 /* Disable separate DCC at the beginning. DRI2 doesn't reuse buffers
1031 * between frames, so the only thing that can enable separate DCC
1032 * with DRI2 is multiple slow clears within a frame.
1033 */
1034 tex->ps_draw_ratio = 0;
1035
1036 ac_surface_override_offset_stride(&sscreen->info, &tex->surface,
1037 tex->buffer.b.b.last_level + 1,
1038 offset, pitch_in_bytes / tex->surface.bpe);
1039
1040 if (tex->is_depth) {
1041 if (sscreen->info.chip_class >= GFX9) {
1042 tex->can_sample_z = true;
1043 tex->can_sample_s = true;
1044
1045 /* Stencil texturing with HTILE doesn't work
1046 * with mipmapping on Navi10-14. */
1047 if (sscreen->info.chip_class == GFX10 && base->last_level > 0)
1048 tex->htile_stencil_disabled = true;
1049 } else {
1050 tex->can_sample_z = !tex->surface.u.legacy.depth_adjusted;
1051 tex->can_sample_s = !tex->surface.u.legacy.stencil_adjusted;
1052 }
1053
1054 tex->db_compatible = surface->flags & RADEON_SURF_ZBUFFER;
1055 } else {
1056 if (tex->surface.cmask_offset) {
1057 tex->cb_color_info |= S_028C70_FAST_CLEAR(1);
1058 tex->cmask_buffer = &tex->buffer;
1059 }
1060 }
1061
1062 if (plane0) {
1063 /* The buffer is shared with the first plane. */
1064 resource->bo_size = plane0->buffer.bo_size;
1065 resource->bo_alignment = plane0->buffer.bo_alignment;
1066 resource->flags = plane0->buffer.flags;
1067 resource->domains = plane0->buffer.domains;
1068 resource->vram_usage = plane0->buffer.vram_usage;
1069 resource->gart_usage = plane0->buffer.gart_usage;
1070
1071 pb_reference(&resource->buf, plane0->buffer.buf);
1072 resource->gpu_address = plane0->buffer.gpu_address;
1073 } else if (!(surface->flags & RADEON_SURF_IMPORTED)) {
1074 /* Create the backing buffer. */
1075 si_init_resource_fields(sscreen, resource, alloc_size, alignment);
1076
1077 if (!si_alloc_resource(sscreen, resource))
1078 goto error;
1079 } else {
1080 resource->buf = imported_buf;
1081 resource->gpu_address = sscreen->ws->buffer_get_virtual_address(resource->buf);
1082 resource->bo_size = imported_buf->size;
1083 resource->bo_alignment = imported_buf->alignment;
1084 resource->domains = sscreen->ws->buffer_get_initial_domain(resource->buf);
1085 if (resource->domains & RADEON_DOMAIN_VRAM)
1086 resource->vram_usage = resource->bo_size;
1087 else if (resource->domains & RADEON_DOMAIN_GTT)
1088 resource->gart_usage = resource->bo_size;
1089 if (sscreen->ws->buffer_get_flags)
1090 resource->flags = sscreen->ws->buffer_get_flags(resource->buf);
1091 }
1092
1093 if (tex->cmask_buffer) {
1094 /* Initialize the cmask to 0xCC (= compressed state). */
1095 si_screen_clear_buffer(sscreen, &tex->cmask_buffer->b.b, tex->surface.cmask_offset,
1096 tex->surface.cmask_size, 0xCCCCCCCC);
1097 }
1098 if (tex->surface.htile_offset) {
1099 uint32_t clear_value = 0;
1100
1101 if (sscreen->info.chip_class >= GFX9 || tex->tc_compatible_htile)
1102 clear_value = 0x0000030F;
1103
1104 si_screen_clear_buffer(sscreen, &tex->buffer.b.b, tex->surface.htile_offset,
1105 tex->surface.htile_size, clear_value);
1106 }
1107
1108 /* Initialize DCC only if the texture is not being imported. */
1109 if (!(surface->flags & RADEON_SURF_IMPORTED) && tex->surface.dcc_offset) {
1110 /* Clear DCC to black for all tiles with DCC enabled.
1111 *
1112 * This fixes corruption in 3DMark Slingshot Extreme, which
1113 * uses uninitialized textures, causing corruption.
1114 */
1115 if (tex->surface.num_dcc_levels == tex->buffer.b.b.last_level + 1 &&
1116 tex->buffer.b.b.nr_samples <= 2) {
1117 /* Simple case - all tiles have DCC enabled. */
1118 si_screen_clear_buffer(sscreen, &tex->buffer.b.b, tex->surface.dcc_offset,
1119 tex->surface.dcc_size, DCC_CLEAR_COLOR_0000);
1120 } else if (sscreen->info.chip_class >= GFX9) {
1121 /* Clear to uncompressed. Clearing this to black is complicated. */
1122 si_screen_clear_buffer(sscreen, &tex->buffer.b.b, tex->surface.dcc_offset,
1123 tex->surface.dcc_size, DCC_UNCOMPRESSED);
1124 } else {
1125 /* GFX8: Initialize mipmap levels and multisamples separately. */
1126 if (tex->buffer.b.b.nr_samples >= 2) {
1127 /* Clearing this to black is complicated. */
1128 si_screen_clear_buffer(sscreen, &tex->buffer.b.b, tex->surface.dcc_offset,
1129 tex->surface.dcc_size, DCC_UNCOMPRESSED);
1130 } else {
1131 /* Clear the enabled mipmap levels to black. */
1132 unsigned size = 0;
1133
1134 for (unsigned i = 0; i < tex->surface.num_dcc_levels; i++) {
1135 if (!tex->surface.u.legacy.level[i].dcc_fast_clear_size)
1136 break;
1137
1138 size = tex->surface.u.legacy.level[i].dcc_offset +
1139 tex->surface.u.legacy.level[i].dcc_fast_clear_size;
1140 }
1141
1142 /* Mipmap levels with DCC. */
1143 if (size) {
1144 si_screen_clear_buffer(sscreen, &tex->buffer.b.b, tex->surface.dcc_offset, size,
1145 DCC_CLEAR_COLOR_0000);
1146 }
1147 /* Mipmap levels without DCC. */
1148 if (size != tex->surface.dcc_size) {
1149 si_screen_clear_buffer(sscreen, &tex->buffer.b.b, tex->surface.dcc_offset + size,
1150 tex->surface.dcc_size - size, DCC_UNCOMPRESSED);
1151 }
1152 }
1153 }
1154 }
1155
1156 /* Initialize displayable DCC that requires the retile blit. */
1157 if (tex->surface.display_dcc_offset) {
1158 if (!(surface->flags & RADEON_SURF_IMPORTED)) {
1159 /* Uninitialized DCC can hang the display hw.
1160 * Clear to white to indicate that. */
1161 si_screen_clear_buffer(sscreen, &tex->buffer.b.b, tex->surface.display_dcc_offset,
1162 tex->surface.u.gfx9.display_dcc_size, DCC_CLEAR_COLOR_1111);
1163 }
1164
1165 /* Upload the DCC retile map.
1166 * Use a staging buffer for the upload, because
1167 * the buffer backing the texture is unmappable.
1168 */
1169 bool use_uint16 = tex->surface.u.gfx9.dcc_retile_use_uint16;
1170 unsigned num_elements = tex->surface.u.gfx9.dcc_retile_num_elements;
1171 unsigned dcc_retile_map_size = num_elements * (use_uint16 ? 2 : 4);
1172
1173 tex->dcc_retile_buffer = si_aligned_buffer_create(screen,
1174 SI_RESOURCE_FLAG_DRIVER_INTERNAL, PIPE_USAGE_DEFAULT,
1175 dcc_retile_map_size,
1176 sscreen->info.tcc_cache_line_size);
1177 struct si_resource *buf = si_aligned_buffer_create(screen,
1178 SI_RESOURCE_FLAG_DRIVER_INTERNAL, PIPE_USAGE_STREAM,
1179 dcc_retile_map_size,
1180 sscreen->info.tcc_cache_line_size);
1181 void *map = sscreen->ws->buffer_map(buf->buf, NULL, PIPE_MAP_WRITE);
1182
1183 /* Upload the retile map into the staging buffer. */
1184 memcpy(map, tex->surface.u.gfx9.dcc_retile_map, dcc_retile_map_size);
1185
1186 /* Copy the staging buffer to the buffer backing the texture. */
1187 struct si_context *sctx = (struct si_context *)sscreen->aux_context;
1188
1189 simple_mtx_lock(&sscreen->aux_context_lock);
1190 si_sdma_copy_buffer(sctx, &tex->dcc_retile_buffer->b.b, &buf->b.b, 0,
1191 0, buf->b.b.width0);
1192 sscreen->aux_context->flush(sscreen->aux_context, NULL, 0);
1193 simple_mtx_unlock(&sscreen->aux_context_lock);
1194
1195 si_resource_reference(&buf, NULL);
1196 }
1197
1198 /* Initialize the CMASK base register value. */
1199 tex->cmask_base_address_reg = (tex->buffer.gpu_address + tex->surface.cmask_offset) >> 8;
1200
1201 if (sscreen->debug_flags & DBG(VM)) {
1202 fprintf(stderr,
1203 "VM start=0x%" PRIX64 " end=0x%" PRIX64
1204 " | Texture %ix%ix%i, %i levels, %i samples, %s\n",
1205 tex->buffer.gpu_address, tex->buffer.gpu_address + tex->buffer.buf->size,
1206 base->width0, base->height0, util_num_layers(base, 0), base->last_level + 1,
1207 base->nr_samples ? base->nr_samples : 1, util_format_short_name(base->format));
1208 }
1209
1210 if (sscreen->debug_flags & DBG(TEX)) {
1211 puts("Texture:");
1212 struct u_log_context log;
1213 u_log_context_init(&log);
1214 si_print_texture_info(sscreen, tex, &log);
1215 u_log_new_page_print(&log, stdout);
1216 fflush(stdout);
1217 u_log_context_destroy(&log);
1218 }
1219
1220 return tex;
1221
1222 error:
1223 FREE(tex);
1224 return NULL;
1225 }
1226
si_choose_tiling(struct si_screen * sscreen,const struct pipe_resource * templ,bool tc_compatible_htile)1227 static enum radeon_surf_mode si_choose_tiling(struct si_screen *sscreen,
1228 const struct pipe_resource *templ,
1229 bool tc_compatible_htile)
1230 {
1231 const struct util_format_description *desc = util_format_description(templ->format);
1232 bool force_tiling = templ->flags & SI_RESOURCE_FLAG_FORCE_MSAA_TILING;
1233 bool is_depth_stencil = util_format_is_depth_or_stencil(templ->format) &&
1234 !(templ->flags & SI_RESOURCE_FLAG_FLUSHED_DEPTH);
1235
1236 /* MSAA resources must be 2D tiled. */
1237 if (templ->nr_samples > 1)
1238 return RADEON_SURF_MODE_2D;
1239
1240 /* Transfer resources should be linear. */
1241 if (templ->flags & SI_RESOURCE_FLAG_FORCE_LINEAR)
1242 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1243
1244 /* Avoid Z/S decompress blits by forcing TC-compatible HTILE on GFX8,
1245 * which requires 2D tiling.
1246 */
1247 if (sscreen->info.chip_class == GFX8 && tc_compatible_htile)
1248 return RADEON_SURF_MODE_2D;
1249
1250 /* Handle common candidates for the linear mode.
1251 * Compressed textures and DB surfaces must always be tiled.
1252 */
1253 if (!force_tiling && !is_depth_stencil && !util_format_is_compressed(templ->format)) {
1254 if (sscreen->debug_flags & DBG(NO_TILING))
1255 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1256
1257 /* Tiling doesn't work with the 422 (SUBSAMPLED) formats. */
1258 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED)
1259 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1260
1261 /* Cursors are linear on AMD GCN.
1262 * (XXX double-check, maybe also use RADEON_SURF_SCANOUT) */
1263 if (templ->bind & PIPE_BIND_CURSOR)
1264 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1265
1266 if (templ->bind & PIPE_BIND_LINEAR)
1267 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1268
1269 /* Textures with a very small height are recommended to be linear. */
1270 if (templ->target == PIPE_TEXTURE_1D || templ->target == PIPE_TEXTURE_1D_ARRAY ||
1271 /* Only very thin and long 2D textures should benefit from
1272 * linear_aligned. */
1273 templ->height0 <= 2)
1274 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1275
1276 /* Textures likely to be mapped often. */
1277 if (templ->usage == PIPE_USAGE_STAGING || templ->usage == PIPE_USAGE_STREAM)
1278 return RADEON_SURF_MODE_LINEAR_ALIGNED;
1279 }
1280
1281 /* Make small textures 1D tiled. */
1282 if (templ->width0 <= 16 || templ->height0 <= 16 || (sscreen->debug_flags & DBG(NO_2D_TILING)))
1283 return RADEON_SURF_MODE_1D;
1284
1285 /* The allocator will switch to 1D if needed. */
1286 return RADEON_SURF_MODE_2D;
1287 }
1288
si_texture_create(struct pipe_screen * screen,const struct pipe_resource * templ)1289 struct pipe_resource *si_texture_create(struct pipe_screen *screen,
1290 const struct pipe_resource *templ)
1291 {
1292 struct si_screen *sscreen = (struct si_screen *)screen;
1293 bool is_zs = util_format_is_depth_or_stencil(templ->format);
1294
1295 if (templ->nr_samples >= 2) {
1296 /* This is hackish (overwriting the const pipe_resource template),
1297 * but should be harmless and gallium frontends can also see
1298 * the overriden number of samples in the created pipe_resource.
1299 */
1300 if (is_zs && sscreen->eqaa_force_z_samples) {
1301 ((struct pipe_resource *)templ)->nr_samples =
1302 ((struct pipe_resource *)templ)->nr_storage_samples = sscreen->eqaa_force_z_samples;
1303 } else if (!is_zs && sscreen->eqaa_force_color_samples) {
1304 ((struct pipe_resource *)templ)->nr_samples = sscreen->eqaa_force_coverage_samples;
1305 ((struct pipe_resource *)templ)->nr_storage_samples = sscreen->eqaa_force_color_samples;
1306 }
1307 }
1308
1309 bool is_flushed_depth = templ->flags & SI_RESOURCE_FLAG_FLUSHED_DEPTH ||
1310 templ->flags & SI_RESOURCE_FLAG_FORCE_LINEAR;
1311 bool tc_compatible_htile =
1312 sscreen->info.chip_class >= GFX8 &&
1313 /* There are issues with TC-compatible HTILE on Tonga (and
1314 * Iceland is the same design), and documented bug workarounds
1315 * don't help. For example, this fails:
1316 * piglit/bin/tex-miplevel-selection 'texture()' 2DShadow -auto
1317 */
1318 sscreen->info.family != CHIP_TONGA && sscreen->info.family != CHIP_ICELAND &&
1319 (templ->flags & PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY) &&
1320 !(sscreen->debug_flags & DBG(NO_HYPERZ)) && !is_flushed_depth &&
1321 templ->nr_samples <= 1 && /* TC-compat HTILE is less efficient with MSAA */
1322 is_zs;
1323 enum radeon_surf_mode tile_mode = si_choose_tiling(sscreen, templ, tc_compatible_htile);
1324
1325 /* This allocates textures with multiple planes like NV12 in 1 buffer. */
1326 enum
1327 {
1328 SI_TEXTURE_MAX_PLANES = 3
1329 };
1330 struct radeon_surf surface[SI_TEXTURE_MAX_PLANES] = {};
1331 struct pipe_resource plane_templ[SI_TEXTURE_MAX_PLANES];
1332 uint64_t plane_offset[SI_TEXTURE_MAX_PLANES] = {};
1333 uint64_t total_size = 0;
1334 unsigned max_alignment = 0;
1335 unsigned num_planes = util_format_get_num_planes(templ->format);
1336 assert(num_planes <= SI_TEXTURE_MAX_PLANES);
1337
1338 /* Compute texture or plane layouts and offsets. */
1339 for (unsigned i = 0; i < num_planes; i++) {
1340 plane_templ[i] = *templ;
1341 plane_templ[i].format = util_format_get_plane_format(templ->format, i);
1342 plane_templ[i].width0 = util_format_get_plane_width(templ->format, i, templ->width0);
1343 plane_templ[i].height0 = util_format_get_plane_height(templ->format, i, templ->height0);
1344
1345 /* Multi-plane allocations need PIPE_BIND_SHARED, because we can't
1346 * reallocate the storage to add PIPE_BIND_SHARED, because it's
1347 * shared by 3 pipe_resources.
1348 */
1349 if (num_planes > 1)
1350 plane_templ[i].bind |= PIPE_BIND_SHARED;
1351
1352 if (si_init_surface(sscreen, &surface[i], &plane_templ[i], tile_mode, false,
1353 plane_templ[i].bind & PIPE_BIND_SCANOUT, is_flushed_depth,
1354 tc_compatible_htile))
1355 return NULL;
1356
1357 plane_offset[i] = align64(total_size, surface[i].surf_alignment);
1358 total_size = plane_offset[i] + surface[i].total_size;
1359 max_alignment = MAX2(max_alignment, surface[i].surf_alignment);
1360 }
1361
1362 struct si_texture *plane0 = NULL, *last_plane = NULL;
1363
1364 for (unsigned i = 0; i < num_planes; i++) {
1365 struct si_texture *tex =
1366 si_texture_create_object(screen, &plane_templ[i], &surface[i], plane0, NULL,
1367 plane_offset[i], 0, total_size, max_alignment);
1368 if (!tex) {
1369 si_texture_reference(&plane0, NULL);
1370 return NULL;
1371 }
1372
1373 tex->plane_index = i;
1374 tex->num_planes = num_planes;
1375
1376 if (!plane0) {
1377 plane0 = last_plane = tex;
1378 } else {
1379 last_plane->buffer.b.b.next = &tex->buffer.b.b;
1380 last_plane = tex;
1381 }
1382 }
1383
1384 return (struct pipe_resource *)plane0;
1385 }
1386
si_texture_from_winsys_buffer(struct si_screen * sscreen,const struct pipe_resource * templ,struct pb_buffer * buf,unsigned stride,uint64_t offset,unsigned usage,bool dedicated)1387 static struct pipe_resource *si_texture_from_winsys_buffer(struct si_screen *sscreen,
1388 const struct pipe_resource *templ,
1389 struct pb_buffer *buf, unsigned stride,
1390 uint64_t offset, unsigned usage,
1391 bool dedicated)
1392 {
1393 struct radeon_surf surface = {};
1394 struct radeon_bo_metadata metadata = {};
1395 struct si_texture *tex;
1396 int r;
1397
1398 /* Ignore metadata for non-zero planes. */
1399 if (offset != 0)
1400 dedicated = false;
1401
1402 if (dedicated) {
1403 sscreen->ws->buffer_get_metadata(buf, &metadata, &surface);
1404 } else {
1405 /**
1406 * The bo metadata is unset for un-dedicated images. So we fall
1407 * back to linear. See answer to question 5 of the
1408 * VK_KHX_external_memory spec for some details.
1409 *
1410 * It is possible that this case isn't going to work if the
1411 * surface pitch isn't correctly aligned by default.
1412 *
1413 * In order to support it correctly we require multi-image
1414 * metadata to be syncrhonized between radv and radeonsi. The
1415 * semantics of associating multiple image metadata to a memory
1416 * object on the vulkan export side are not concretely defined
1417 * either.
1418 *
1419 * All the use cases we are aware of at the moment for memory
1420 * objects use dedicated allocations. So lets keep the initial
1421 * implementation simple.
1422 *
1423 * A possible alternative is to attempt to reconstruct the
1424 * tiling information when the TexParameter TEXTURE_TILING_EXT
1425 * is set.
1426 */
1427 metadata.mode = RADEON_SURF_MODE_LINEAR_ALIGNED;
1428 }
1429
1430 r = si_init_surface(sscreen, &surface, templ, metadata.mode, true,
1431 surface.flags & RADEON_SURF_SCANOUT, false, false);
1432 if (r)
1433 return NULL;
1434
1435 tex = si_texture_create_object(&sscreen->b, templ, &surface, NULL, buf,
1436 offset, stride, 0, 0);
1437 if (!tex)
1438 return NULL;
1439
1440 tex->buffer.b.is_shared = true;
1441 tex->buffer.external_usage = usage;
1442 tex->num_planes = 1;
1443 if (tex->buffer.flags & RADEON_FLAG_ENCRYPTED)
1444 tex->buffer.b.b.bind |= PIPE_BIND_PROTECTED;
1445
1446 /* Account for multiple planes with lowered yuv import. */
1447 struct pipe_resource *next_plane = tex->buffer.b.b.next;
1448 while(next_plane) {
1449 struct si_texture *next_tex = (struct si_texture *)next_plane;
1450 ++next_tex->num_planes;
1451 ++tex->num_planes;
1452 next_plane = next_plane->next;
1453 }
1454
1455 if (!ac_surface_set_umd_metadata(&sscreen->info, &tex->surface,
1456 tex->buffer.b.b.nr_storage_samples,
1457 tex->buffer.b.b.last_level + 1,
1458 metadata.size_metadata,
1459 metadata.metadata)) {
1460 si_texture_reference(&tex, NULL);
1461 return NULL;
1462 }
1463
1464 /* Displayable DCC requires an explicit flush. */
1465 if (dedicated && offset == 0 && !(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) &&
1466 si_has_displayable_dcc(tex)) {
1467 /* TODO: do we need to decompress DCC? */
1468 if (si_texture_discard_dcc(sscreen, tex)) {
1469 /* Update BO metadata after disabling DCC. */
1470 si_set_tex_bo_metadata(sscreen, tex);
1471 }
1472 }
1473
1474 assert(tex->surface.tile_swizzle == 0);
1475 return &tex->buffer.b.b;
1476 }
1477
si_texture_from_handle(struct pipe_screen * screen,const struct pipe_resource * templ,struct winsys_handle * whandle,unsigned usage)1478 static struct pipe_resource *si_texture_from_handle(struct pipe_screen *screen,
1479 const struct pipe_resource *templ,
1480 struct winsys_handle *whandle, unsigned usage)
1481 {
1482 struct si_screen *sscreen = (struct si_screen *)screen;
1483 struct pb_buffer *buf = NULL;
1484
1485 /* Support only 2D textures without mipmaps */
1486 if ((templ->target != PIPE_TEXTURE_2D && templ->target != PIPE_TEXTURE_RECT &&
1487 templ->target != PIPE_TEXTURE_2D_ARRAY) ||
1488 templ->last_level != 0)
1489 return NULL;
1490
1491 buf = sscreen->ws->buffer_from_handle(sscreen->ws, whandle, sscreen->info.max_alignment);
1492 if (!buf)
1493 return NULL;
1494
1495 return si_texture_from_winsys_buffer(sscreen, templ, buf, whandle->stride, whandle->offset,
1496 usage, true);
1497 }
1498
si_init_flushed_depth_texture(struct pipe_context * ctx,struct pipe_resource * texture)1499 bool si_init_flushed_depth_texture(struct pipe_context *ctx, struct pipe_resource *texture)
1500 {
1501 struct si_texture *tex = (struct si_texture *)texture;
1502 struct pipe_resource resource;
1503 enum pipe_format pipe_format = texture->format;
1504
1505 assert(!tex->flushed_depth_texture);
1506
1507 if (!tex->can_sample_z && tex->can_sample_s) {
1508 switch (pipe_format) {
1509 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1510 /* Save memory by not allocating the S plane. */
1511 pipe_format = PIPE_FORMAT_Z32_FLOAT;
1512 break;
1513 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1514 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1515 /* Save memory bandwidth by not copying the
1516 * stencil part during flush.
1517 *
1518 * This potentially increases memory bandwidth
1519 * if an application uses both Z and S texturing
1520 * simultaneously (a flushed Z24S8 texture
1521 * would be stored compactly), but how often
1522 * does that really happen?
1523 */
1524 pipe_format = PIPE_FORMAT_Z24X8_UNORM;
1525 break;
1526 default:;
1527 }
1528 } else if (!tex->can_sample_s && tex->can_sample_z) {
1529 assert(util_format_has_stencil(util_format_description(pipe_format)));
1530
1531 /* DB->CB copies to an 8bpp surface don't work. */
1532 pipe_format = PIPE_FORMAT_X24S8_UINT;
1533 }
1534
1535 memset(&resource, 0, sizeof(resource));
1536 resource.target = texture->target;
1537 resource.format = pipe_format;
1538 resource.width0 = texture->width0;
1539 resource.height0 = texture->height0;
1540 resource.depth0 = texture->depth0;
1541 resource.array_size = texture->array_size;
1542 resource.last_level = texture->last_level;
1543 resource.nr_samples = texture->nr_samples;
1544 resource.usage = PIPE_USAGE_DEFAULT;
1545 resource.bind = texture->bind & ~PIPE_BIND_DEPTH_STENCIL;
1546 resource.flags = texture->flags | SI_RESOURCE_FLAG_FLUSHED_DEPTH;
1547
1548 tex->flushed_depth_texture =
1549 (struct si_texture *)ctx->screen->resource_create(ctx->screen, &resource);
1550 if (!tex->flushed_depth_texture) {
1551 PRINT_ERR("failed to create temporary texture to hold flushed depth\n");
1552 return false;
1553 }
1554 return true;
1555 }
1556
1557 /**
1558 * Initialize the pipe_resource descriptor to be of the same size as the box,
1559 * which is supposed to hold a subregion of the texture "orig" at the given
1560 * mipmap level.
1561 */
si_init_temp_resource_from_box(struct pipe_resource * res,struct pipe_resource * orig,const struct pipe_box * box,unsigned level,unsigned usage,unsigned flags)1562 static void si_init_temp_resource_from_box(struct pipe_resource *res, struct pipe_resource *orig,
1563 const struct pipe_box *box, unsigned level,
1564 unsigned usage, unsigned flags)
1565 {
1566 memset(res, 0, sizeof(*res));
1567 res->format = orig->format;
1568 res->width0 = box->width;
1569 res->height0 = box->height;
1570 res->depth0 = 1;
1571 res->array_size = 1;
1572 res->usage = usage;
1573 res->flags = flags;
1574
1575 if (flags & SI_RESOURCE_FLAG_FORCE_LINEAR && util_format_is_compressed(orig->format)) {
1576 /* Transfer resources are allocated with linear tiling, which is
1577 * not supported for compressed formats.
1578 */
1579 unsigned blocksize = util_format_get_blocksize(orig->format);
1580
1581 if (blocksize == 8) {
1582 res->format = PIPE_FORMAT_R16G16B16A16_UINT;
1583 } else {
1584 assert(blocksize == 16);
1585 res->format = PIPE_FORMAT_R32G32B32A32_UINT;
1586 }
1587
1588 res->width0 = util_format_get_nblocksx(orig->format, box->width);
1589 res->height0 = util_format_get_nblocksy(orig->format, box->height);
1590 }
1591
1592 /* We must set the correct texture target and dimensions for a 3D box. */
1593 if (box->depth > 1 && util_max_layer(orig, level) > 0) {
1594 res->target = PIPE_TEXTURE_2D_ARRAY;
1595 res->array_size = box->depth;
1596 } else {
1597 res->target = PIPE_TEXTURE_2D;
1598 }
1599 }
1600
si_can_invalidate_texture(struct si_screen * sscreen,struct si_texture * tex,unsigned transfer_usage,const struct pipe_box * box)1601 static bool si_can_invalidate_texture(struct si_screen *sscreen, struct si_texture *tex,
1602 unsigned transfer_usage, const struct pipe_box *box)
1603 {
1604 return !tex->buffer.b.is_shared && !(tex->surface.flags & RADEON_SURF_IMPORTED) &&
1605 !(transfer_usage & PIPE_MAP_READ) && tex->buffer.b.b.last_level == 0 &&
1606 util_texrange_covers_whole_level(&tex->buffer.b.b, 0, box->x, box->y, box->z, box->width,
1607 box->height, box->depth);
1608 }
1609
si_texture_invalidate_storage(struct si_context * sctx,struct si_texture * tex)1610 static void si_texture_invalidate_storage(struct si_context *sctx, struct si_texture *tex)
1611 {
1612 struct si_screen *sscreen = sctx->screen;
1613
1614 /* There is no point in discarding depth and tiled buffers. */
1615 assert(!tex->is_depth);
1616 assert(tex->surface.is_linear);
1617
1618 /* Reallocate the buffer in the same pipe_resource. */
1619 si_alloc_resource(sscreen, &tex->buffer);
1620
1621 /* Initialize the CMASK base address (needed even without CMASK). */
1622 tex->cmask_base_address_reg = (tex->buffer.gpu_address + tex->surface.cmask_offset) >> 8;
1623
1624 p_atomic_inc(&sscreen->dirty_tex_counter);
1625
1626 sctx->num_alloc_tex_transfer_bytes += tex->surface.total_size;
1627 }
1628
si_texture_transfer_map(struct pipe_context * ctx,struct pipe_resource * texture,unsigned level,unsigned usage,const struct pipe_box * box,struct pipe_transfer ** ptransfer)1629 static void *si_texture_transfer_map(struct pipe_context *ctx, struct pipe_resource *texture,
1630 unsigned level, unsigned usage, const struct pipe_box *box,
1631 struct pipe_transfer **ptransfer)
1632 {
1633 struct si_context *sctx = (struct si_context *)ctx;
1634 struct si_texture *tex = (struct si_texture *)texture;
1635 struct si_transfer *trans;
1636 struct si_resource *buf;
1637 unsigned offset = 0;
1638 char *map;
1639 bool use_staging_texture = false;
1640
1641 assert(!(texture->flags & SI_RESOURCE_FLAG_FORCE_LINEAR));
1642 assert(box->width && box->height && box->depth);
1643
1644 if (tex->buffer.flags & RADEON_FLAG_ENCRYPTED)
1645 return NULL;
1646
1647 if (tex->is_depth) {
1648 /* Depth textures use staging unconditionally. */
1649 use_staging_texture = true;
1650 } else {
1651 /* Degrade the tile mode if we get too many transfers on APUs.
1652 * On dGPUs, the staging texture is always faster.
1653 * Only count uploads that are at least 4x4 pixels large.
1654 */
1655 if (!sctx->screen->info.has_dedicated_vram && level == 0 && box->width >= 4 &&
1656 box->height >= 4 && p_atomic_inc_return(&tex->num_level0_transfers) == 10) {
1657 bool can_invalidate = si_can_invalidate_texture(sctx->screen, tex, usage, box);
1658
1659 si_reallocate_texture_inplace(sctx, tex, PIPE_BIND_LINEAR, can_invalidate);
1660 }
1661
1662 /* Tiled textures need to be converted into a linear texture for CPU
1663 * access. The staging texture is always linear and is placed in GART.
1664 *
1665 * Reading from VRAM or GTT WC is slow, always use the staging
1666 * texture in this case.
1667 *
1668 * Use the staging texture for uploads if the underlying BO
1669 * is busy.
1670 */
1671 if (!tex->surface.is_linear || (tex->buffer.flags & RADEON_FLAG_ENCRYPTED))
1672 use_staging_texture = true;
1673 else if (usage & PIPE_MAP_READ)
1674 use_staging_texture =
1675 tex->buffer.domains & RADEON_DOMAIN_VRAM || tex->buffer.flags & RADEON_FLAG_GTT_WC;
1676 /* Write & linear only: */
1677 else if (si_rings_is_buffer_referenced(sctx, tex->buffer.buf, RADEON_USAGE_READWRITE) ||
1678 !sctx->ws->buffer_wait(tex->buffer.buf, 0, RADEON_USAGE_READWRITE)) {
1679 /* It's busy. */
1680 if (si_can_invalidate_texture(sctx->screen, tex, usage, box))
1681 si_texture_invalidate_storage(sctx, tex);
1682 else
1683 use_staging_texture = true;
1684 }
1685 }
1686
1687 trans = CALLOC_STRUCT(si_transfer);
1688 if (!trans)
1689 return NULL;
1690 pipe_resource_reference(&trans->b.b.resource, texture);
1691 trans->b.b.level = level;
1692 trans->b.b.usage = usage;
1693 trans->b.b.box = *box;
1694
1695 if (use_staging_texture) {
1696 struct pipe_resource resource;
1697 struct si_texture *staging;
1698 unsigned bo_usage = usage & PIPE_MAP_READ ? PIPE_USAGE_STAGING : PIPE_USAGE_STREAM;
1699 unsigned bo_flags = SI_RESOURCE_FLAG_FORCE_LINEAR | SI_RESOURCE_FLAG_DRIVER_INTERNAL;
1700
1701 /* The pixel shader has a bad access pattern for linear textures.
1702 * If a pixel shader is used to blit to/from staging, don't disable caches.
1703 *
1704 * MSAA, depth/stencil textures, and compressed textures use the pixel shader
1705 * to blit.
1706 */
1707 if (texture->nr_samples <= 1 &&
1708 !tex->is_depth &&
1709 !util_format_is_compressed(texture->format) &&
1710 /* Texture uploads with DCC use the pixel shader to blit */
1711 (!(usage & PIPE_MAP_WRITE) || !vi_dcc_enabled(tex, level)))
1712 bo_flags |= SI_RESOURCE_FLAG_UNCACHED;
1713
1714 si_init_temp_resource_from_box(&resource, texture, box, level, bo_usage,
1715 bo_flags);
1716
1717 /* Since depth-stencil textures don't support linear tiling,
1718 * blit from ZS to color and vice versa. u_blitter will do
1719 * the packing for these formats.
1720 */
1721 if (tex->is_depth)
1722 resource.format = util_blitter_get_color_format_for_zs(resource.format);
1723
1724 /* Create the temporary texture. */
1725 staging = (struct si_texture *)ctx->screen->resource_create(ctx->screen, &resource);
1726 if (!staging) {
1727 PRINT_ERR("failed to create temporary texture to hold untiled copy\n");
1728 goto fail_trans;
1729 }
1730 trans->staging = &staging->buffer;
1731
1732 /* Just get the strides. */
1733 si_texture_get_offset(sctx->screen, staging, 0, NULL, &trans->b.b.stride,
1734 &trans->b.b.layer_stride);
1735
1736 if (usage & PIPE_MAP_READ)
1737 si_copy_to_staging_texture(ctx, trans);
1738 else
1739 usage |= PIPE_MAP_UNSYNCHRONIZED;
1740
1741 buf = trans->staging;
1742 } else {
1743 /* the resource is mapped directly */
1744 offset = si_texture_get_offset(sctx->screen, tex, level, box, &trans->b.b.stride,
1745 &trans->b.b.layer_stride);
1746 buf = &tex->buffer;
1747 }
1748
1749 /* Always unmap texture CPU mappings on 32-bit architectures, so that
1750 * we don't run out of the CPU address space.
1751 */
1752 if (sizeof(void *) == 4)
1753 usage |= RADEON_MAP_TEMPORARY;
1754
1755 if (!(map = si_buffer_map_sync_with_rings(sctx, buf, usage)))
1756 goto fail_trans;
1757
1758 *ptransfer = &trans->b.b;
1759 return map + offset;
1760
1761 fail_trans:
1762 si_resource_reference(&trans->staging, NULL);
1763 pipe_resource_reference(&trans->b.b.resource, NULL);
1764 FREE(trans);
1765 return NULL;
1766 }
1767
si_texture_transfer_unmap(struct pipe_context * ctx,struct pipe_transfer * transfer)1768 static void si_texture_transfer_unmap(struct pipe_context *ctx, struct pipe_transfer *transfer)
1769 {
1770 struct si_context *sctx = (struct si_context *)ctx;
1771 struct si_transfer *stransfer = (struct si_transfer *)transfer;
1772 struct pipe_resource *texture = transfer->resource;
1773 struct si_texture *tex = (struct si_texture *)texture;
1774
1775 /* Always unmap texture CPU mappings on 32-bit architectures, so that
1776 * we don't run out of the CPU address space.
1777 */
1778 if (sizeof(void *) == 4) {
1779 struct si_resource *buf = stransfer->staging ? stransfer->staging : &tex->buffer;
1780
1781 sctx->ws->buffer_unmap(buf->buf);
1782 }
1783
1784 if ((transfer->usage & PIPE_MAP_WRITE) && stransfer->staging)
1785 si_copy_from_staging_texture(ctx, stransfer);
1786
1787 if (stransfer->staging) {
1788 sctx->num_alloc_tex_transfer_bytes += stransfer->staging->buf->size;
1789 si_resource_reference(&stransfer->staging, NULL);
1790 }
1791
1792 /* Heuristic for {upload, draw, upload, draw, ..}:
1793 *
1794 * Flush the gfx IB if we've allocated too much texture storage.
1795 *
1796 * The idea is that we don't want to build IBs that use too much
1797 * memory and put pressure on the kernel memory manager and we also
1798 * want to make temporary and invalidated buffers go idle ASAP to
1799 * decrease the total memory usage or make them reusable. The memory
1800 * usage will be slightly higher than given here because of the buffer
1801 * cache in the winsys.
1802 *
1803 * The result is that the kernel memory manager is never a bottleneck.
1804 */
1805 if (sctx->num_alloc_tex_transfer_bytes > sctx->screen->info.gart_size / 4) {
1806 si_flush_gfx_cs(sctx, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW, NULL);
1807 sctx->num_alloc_tex_transfer_bytes = 0;
1808 }
1809
1810 pipe_resource_reference(&transfer->resource, NULL);
1811 FREE(transfer);
1812 }
1813
1814 static const struct u_resource_vtbl si_texture_vtbl = {
1815 NULL, /* get_handle */
1816 si_texture_destroy, /* resource_destroy */
1817 si_texture_transfer_map, /* transfer_map */
1818 u_default_transfer_flush_region, /* transfer_flush_region */
1819 si_texture_transfer_unmap, /* transfer_unmap */
1820 };
1821
1822 /* Return if it's allowed to reinterpret one format as another with DCC enabled.
1823 */
vi_dcc_formats_compatible(struct si_screen * sscreen,enum pipe_format format1,enum pipe_format format2)1824 bool vi_dcc_formats_compatible(struct si_screen *sscreen, enum pipe_format format1,
1825 enum pipe_format format2)
1826 {
1827 const struct util_format_description *desc1, *desc2;
1828
1829 /* No format change - exit early. */
1830 if (format1 == format2)
1831 return true;
1832
1833 format1 = si_simplify_cb_format(format1);
1834 format2 = si_simplify_cb_format(format2);
1835
1836 /* Check again after format adjustments. */
1837 if (format1 == format2)
1838 return true;
1839
1840 desc1 = util_format_description(format1);
1841 desc2 = util_format_description(format2);
1842
1843 if (desc1->layout != UTIL_FORMAT_LAYOUT_PLAIN || desc2->layout != UTIL_FORMAT_LAYOUT_PLAIN)
1844 return false;
1845
1846 /* Float and non-float are totally incompatible. */
1847 if ((desc1->channel[0].type == UTIL_FORMAT_TYPE_FLOAT) !=
1848 (desc2->channel[0].type == UTIL_FORMAT_TYPE_FLOAT))
1849 return false;
1850
1851 /* Channel sizes must match across DCC formats.
1852 * Comparing just the first 2 channels should be enough.
1853 */
1854 if (desc1->channel[0].size != desc2->channel[0].size ||
1855 (desc1->nr_channels >= 2 && desc1->channel[1].size != desc2->channel[1].size))
1856 return false;
1857
1858 /* Everything below is not needed if the driver never uses the DCC
1859 * clear code with the value of 1.
1860 */
1861
1862 /* If the clear values are all 1 or all 0, this constraint can be
1863 * ignored. */
1864 if (vi_alpha_is_on_msb(sscreen, format1) != vi_alpha_is_on_msb(sscreen, format2))
1865 return false;
1866
1867 /* Channel types must match if the clear value of 1 is used.
1868 * The type categories are only float, signed, unsigned.
1869 * NORM and INT are always compatible.
1870 */
1871 if (desc1->channel[0].type != desc2->channel[0].type ||
1872 (desc1->nr_channels >= 2 && desc1->channel[1].type != desc2->channel[1].type))
1873 return false;
1874
1875 return true;
1876 }
1877
vi_dcc_formats_are_incompatible(struct pipe_resource * tex,unsigned level,enum pipe_format view_format)1878 bool vi_dcc_formats_are_incompatible(struct pipe_resource *tex, unsigned level,
1879 enum pipe_format view_format)
1880 {
1881 struct si_texture *stex = (struct si_texture *)tex;
1882
1883 return vi_dcc_enabled(stex, level) &&
1884 !vi_dcc_formats_compatible((struct si_screen *)tex->screen, tex->format, view_format);
1885 }
1886
1887 /* This can't be merged with the above function, because
1888 * vi_dcc_formats_compatible should be called only when DCC is enabled. */
vi_disable_dcc_if_incompatible_format(struct si_context * sctx,struct pipe_resource * tex,unsigned level,enum pipe_format view_format)1889 void vi_disable_dcc_if_incompatible_format(struct si_context *sctx, struct pipe_resource *tex,
1890 unsigned level, enum pipe_format view_format)
1891 {
1892 struct si_texture *stex = (struct si_texture *)tex;
1893
1894 if (vi_dcc_formats_are_incompatible(tex, level, view_format))
1895 if (!si_texture_disable_dcc(sctx, stex))
1896 si_decompress_dcc(sctx, stex);
1897 }
1898
si_create_surface_custom(struct pipe_context * pipe,struct pipe_resource * texture,const struct pipe_surface * templ,unsigned width0,unsigned height0,unsigned width,unsigned height)1899 struct pipe_surface *si_create_surface_custom(struct pipe_context *pipe,
1900 struct pipe_resource *texture,
1901 const struct pipe_surface *templ, unsigned width0,
1902 unsigned height0, unsigned width, unsigned height)
1903 {
1904 struct si_surface *surface = CALLOC_STRUCT(si_surface);
1905
1906 if (!surface)
1907 return NULL;
1908
1909 assert(templ->u.tex.first_layer <= util_max_layer(texture, templ->u.tex.level));
1910 assert(templ->u.tex.last_layer <= util_max_layer(texture, templ->u.tex.level));
1911
1912 pipe_reference_init(&surface->base.reference, 1);
1913 pipe_resource_reference(&surface->base.texture, texture);
1914 surface->base.context = pipe;
1915 surface->base.format = templ->format;
1916 surface->base.width = width;
1917 surface->base.height = height;
1918 surface->base.u = templ->u;
1919
1920 surface->width0 = width0;
1921 surface->height0 = height0;
1922
1923 surface->dcc_incompatible =
1924 texture->target != PIPE_BUFFER &&
1925 vi_dcc_formats_are_incompatible(texture, templ->u.tex.level, templ->format);
1926 return &surface->base;
1927 }
1928
si_create_surface(struct pipe_context * pipe,struct pipe_resource * tex,const struct pipe_surface * templ)1929 static struct pipe_surface *si_create_surface(struct pipe_context *pipe, struct pipe_resource *tex,
1930 const struct pipe_surface *templ)
1931 {
1932 unsigned level = templ->u.tex.level;
1933 unsigned width = u_minify(tex->width0, level);
1934 unsigned height = u_minify(tex->height0, level);
1935 unsigned width0 = tex->width0;
1936 unsigned height0 = tex->height0;
1937
1938 if (tex->target != PIPE_BUFFER && templ->format != tex->format) {
1939 const struct util_format_description *tex_desc = util_format_description(tex->format);
1940 const struct util_format_description *templ_desc = util_format_description(templ->format);
1941
1942 assert(tex_desc->block.bits == templ_desc->block.bits);
1943
1944 /* Adjust size of surface if and only if the block width or
1945 * height is changed. */
1946 if (tex_desc->block.width != templ_desc->block.width ||
1947 tex_desc->block.height != templ_desc->block.height) {
1948 unsigned nblks_x = util_format_get_nblocksx(tex->format, width);
1949 unsigned nblks_y = util_format_get_nblocksy(tex->format, height);
1950
1951 width = nblks_x * templ_desc->block.width;
1952 height = nblks_y * templ_desc->block.height;
1953
1954 width0 = util_format_get_nblocksx(tex->format, width0);
1955 height0 = util_format_get_nblocksy(tex->format, height0);
1956 }
1957 }
1958
1959 return si_create_surface_custom(pipe, tex, templ, width0, height0, width, height);
1960 }
1961
si_surface_destroy(struct pipe_context * pipe,struct pipe_surface * surface)1962 static void si_surface_destroy(struct pipe_context *pipe, struct pipe_surface *surface)
1963 {
1964 pipe_resource_reference(&surface->texture, NULL);
1965 FREE(surface);
1966 }
1967
si_translate_colorswap(enum pipe_format format,bool do_endian_swap)1968 unsigned si_translate_colorswap(enum pipe_format format, bool do_endian_swap)
1969 {
1970 const struct util_format_description *desc = util_format_description(format);
1971
1972 #define HAS_SWIZZLE(chan, swz) (desc->swizzle[chan] == PIPE_SWIZZLE_##swz)
1973
1974 if (format == PIPE_FORMAT_R11G11B10_FLOAT) /* isn't plain */
1975 return V_028C70_SWAP_STD;
1976
1977 if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
1978 return ~0U;
1979
1980 switch (desc->nr_channels) {
1981 case 1:
1982 if (HAS_SWIZZLE(0, X))
1983 return V_028C70_SWAP_STD; /* X___ */
1984 else if (HAS_SWIZZLE(3, X))
1985 return V_028C70_SWAP_ALT_REV; /* ___X */
1986 break;
1987 case 2:
1988 if ((HAS_SWIZZLE(0, X) && HAS_SWIZZLE(1, Y)) || (HAS_SWIZZLE(0, X) && HAS_SWIZZLE(1, NONE)) ||
1989 (HAS_SWIZZLE(0, NONE) && HAS_SWIZZLE(1, Y)))
1990 return V_028C70_SWAP_STD; /* XY__ */
1991 else if ((HAS_SWIZZLE(0, Y) && HAS_SWIZZLE(1, X)) ||
1992 (HAS_SWIZZLE(0, Y) && HAS_SWIZZLE(1, NONE)) ||
1993 (HAS_SWIZZLE(0, NONE) && HAS_SWIZZLE(1, X)))
1994 /* YX__ */
1995 return (do_endian_swap ? V_028C70_SWAP_STD : V_028C70_SWAP_STD_REV);
1996 else if (HAS_SWIZZLE(0, X) && HAS_SWIZZLE(3, Y))
1997 return V_028C70_SWAP_ALT; /* X__Y */
1998 else if (HAS_SWIZZLE(0, Y) && HAS_SWIZZLE(3, X))
1999 return V_028C70_SWAP_ALT_REV; /* Y__X */
2000 break;
2001 case 3:
2002 if (HAS_SWIZZLE(0, X))
2003 return (do_endian_swap ? V_028C70_SWAP_STD_REV : V_028C70_SWAP_STD);
2004 else if (HAS_SWIZZLE(0, Z))
2005 return V_028C70_SWAP_STD_REV; /* ZYX */
2006 break;
2007 case 4:
2008 /* check the middle channels, the 1st and 4th channel can be NONE */
2009 if (HAS_SWIZZLE(1, Y) && HAS_SWIZZLE(2, Z)) {
2010 return V_028C70_SWAP_STD; /* XYZW */
2011 } else if (HAS_SWIZZLE(1, Z) && HAS_SWIZZLE(2, Y)) {
2012 return V_028C70_SWAP_STD_REV; /* WZYX */
2013 } else if (HAS_SWIZZLE(1, Y) && HAS_SWIZZLE(2, X)) {
2014 return V_028C70_SWAP_ALT; /* ZYXW */
2015 } else if (HAS_SWIZZLE(1, Z) && HAS_SWIZZLE(2, W)) {
2016 /* YZWX */
2017 if (desc->is_array)
2018 return V_028C70_SWAP_ALT_REV;
2019 else
2020 return (do_endian_swap ? V_028C70_SWAP_ALT : V_028C70_SWAP_ALT_REV);
2021 }
2022 break;
2023 }
2024 return ~0U;
2025 }
2026
2027 /* PIPELINE_STAT-BASED DCC ENABLEMENT FOR DISPLAYABLE SURFACES */
2028
vi_dcc_clean_up_context_slot(struct si_context * sctx,int slot)2029 static void vi_dcc_clean_up_context_slot(struct si_context *sctx, int slot)
2030 {
2031 int i;
2032
2033 if (sctx->dcc_stats[slot].query_active)
2034 vi_separate_dcc_stop_query(sctx, sctx->dcc_stats[slot].tex);
2035
2036 for (i = 0; i < ARRAY_SIZE(sctx->dcc_stats[slot].ps_stats); i++)
2037 if (sctx->dcc_stats[slot].ps_stats[i]) {
2038 sctx->b.destroy_query(&sctx->b, sctx->dcc_stats[slot].ps_stats[i]);
2039 sctx->dcc_stats[slot].ps_stats[i] = NULL;
2040 }
2041
2042 si_texture_reference(&sctx->dcc_stats[slot].tex, NULL);
2043 }
2044
2045 /**
2046 * Return the per-context slot where DCC statistics queries for the texture live.
2047 */
vi_get_context_dcc_stats_index(struct si_context * sctx,struct si_texture * tex)2048 static unsigned vi_get_context_dcc_stats_index(struct si_context *sctx, struct si_texture *tex)
2049 {
2050 int i, empty_slot = -1;
2051
2052 /* Remove zombie textures (textures kept alive by this array only). */
2053 for (i = 0; i < ARRAY_SIZE(sctx->dcc_stats); i++)
2054 if (sctx->dcc_stats[i].tex && sctx->dcc_stats[i].tex->buffer.b.b.reference.count == 1)
2055 vi_dcc_clean_up_context_slot(sctx, i);
2056
2057 /* Find the texture. */
2058 for (i = 0; i < ARRAY_SIZE(sctx->dcc_stats); i++) {
2059 /* Return if found. */
2060 if (sctx->dcc_stats[i].tex == tex) {
2061 sctx->dcc_stats[i].last_use_timestamp = os_time_get();
2062 return i;
2063 }
2064
2065 /* Record the first seen empty slot. */
2066 if (empty_slot == -1 && !sctx->dcc_stats[i].tex)
2067 empty_slot = i;
2068 }
2069
2070 /* Not found. Remove the oldest member to make space in the array. */
2071 if (empty_slot == -1) {
2072 int oldest_slot = 0;
2073
2074 /* Find the oldest slot. */
2075 for (i = 1; i < ARRAY_SIZE(sctx->dcc_stats); i++)
2076 if (sctx->dcc_stats[oldest_slot].last_use_timestamp >
2077 sctx->dcc_stats[i].last_use_timestamp)
2078 oldest_slot = i;
2079
2080 /* Clean up the oldest slot. */
2081 vi_dcc_clean_up_context_slot(sctx, oldest_slot);
2082 empty_slot = oldest_slot;
2083 }
2084
2085 /* Add the texture to the new slot. */
2086 si_texture_reference(&sctx->dcc_stats[empty_slot].tex, tex);
2087 sctx->dcc_stats[empty_slot].last_use_timestamp = os_time_get();
2088 return empty_slot;
2089 }
2090
vi_create_resuming_pipestats_query(struct si_context * sctx)2091 static struct pipe_query *vi_create_resuming_pipestats_query(struct si_context *sctx)
2092 {
2093 struct si_query_hw *query =
2094 (struct si_query_hw *)sctx->b.create_query(&sctx->b, PIPE_QUERY_PIPELINE_STATISTICS, 0);
2095
2096 query->flags |= SI_QUERY_HW_FLAG_BEGIN_RESUMES;
2097 return (struct pipe_query *)query;
2098 }
2099
2100 /**
2101 * Called when binding a color buffer.
2102 */
vi_separate_dcc_start_query(struct si_context * sctx,struct si_texture * tex)2103 void vi_separate_dcc_start_query(struct si_context *sctx, struct si_texture *tex)
2104 {
2105 unsigned i = vi_get_context_dcc_stats_index(sctx, tex);
2106
2107 assert(!sctx->dcc_stats[i].query_active);
2108
2109 if (!sctx->dcc_stats[i].ps_stats[0])
2110 sctx->dcc_stats[i].ps_stats[0] = vi_create_resuming_pipestats_query(sctx);
2111
2112 /* begin or resume the query */
2113 sctx->b.begin_query(&sctx->b, sctx->dcc_stats[i].ps_stats[0]);
2114 sctx->dcc_stats[i].query_active = true;
2115 }
2116
2117 /**
2118 * Called when unbinding a color buffer.
2119 */
vi_separate_dcc_stop_query(struct si_context * sctx,struct si_texture * tex)2120 void vi_separate_dcc_stop_query(struct si_context *sctx, struct si_texture *tex)
2121 {
2122 unsigned i = vi_get_context_dcc_stats_index(sctx, tex);
2123
2124 assert(sctx->dcc_stats[i].query_active);
2125 assert(sctx->dcc_stats[i].ps_stats[0]);
2126
2127 /* pause or end the query */
2128 sctx->b.end_query(&sctx->b, sctx->dcc_stats[i].ps_stats[0]);
2129 sctx->dcc_stats[i].query_active = false;
2130 }
2131
vi_should_enable_separate_dcc(struct si_texture * tex)2132 static bool vi_should_enable_separate_dcc(struct si_texture *tex)
2133 {
2134 /* The minimum number of fullscreen draws per frame that is required
2135 * to enable DCC. */
2136 return tex->ps_draw_ratio + tex->num_slow_clears >= 5;
2137 }
2138
2139 /* Called by fast clear. */
vi_separate_dcc_try_enable(struct si_context * sctx,struct si_texture * tex)2140 void vi_separate_dcc_try_enable(struct si_context *sctx, struct si_texture *tex)
2141 {
2142 /* The intent is to use this with shared displayable back buffers,
2143 * but it's not strictly limited only to them.
2144 */
2145 if (!tex->buffer.b.is_shared ||
2146 !(tex->buffer.external_usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) ||
2147 tex->buffer.b.b.target != PIPE_TEXTURE_2D || tex->buffer.b.b.last_level > 0 ||
2148 !tex->surface.dcc_size || sctx->screen->debug_flags & DBG(NO_DCC) ||
2149 sctx->screen->debug_flags & DBG(NO_DCC_FB))
2150 return;
2151
2152 assert(sctx->chip_class >= GFX8);
2153
2154 if (tex->surface.dcc_offset)
2155 return; /* already enabled */
2156
2157 /* Enable the DCC stat gathering. */
2158 if (!tex->dcc_gather_statistics) {
2159 tex->dcc_gather_statistics = true;
2160 vi_separate_dcc_start_query(sctx, tex);
2161 }
2162
2163 if (!vi_should_enable_separate_dcc(tex))
2164 return; /* stats show that DCC decompression is too expensive */
2165
2166 assert(tex->surface.num_dcc_levels);
2167 assert(!tex->dcc_separate_buffer);
2168
2169 si_texture_discard_cmask(sctx->screen, tex);
2170
2171 /* Get a DCC buffer. */
2172 if (tex->last_dcc_separate_buffer) {
2173 assert(tex->dcc_gather_statistics);
2174 assert(!tex->dcc_separate_buffer);
2175 tex->dcc_separate_buffer = tex->last_dcc_separate_buffer;
2176 tex->last_dcc_separate_buffer = NULL;
2177 } else {
2178 tex->dcc_separate_buffer =
2179 si_aligned_buffer_create(sctx->b.screen, SI_RESOURCE_FLAG_UNMAPPABLE, PIPE_USAGE_DEFAULT,
2180 tex->surface.dcc_size, tex->surface.dcc_alignment);
2181 if (!tex->dcc_separate_buffer)
2182 return;
2183 }
2184
2185 /* dcc_offset is the absolute GPUVM address. */
2186 tex->surface.dcc_offset = tex->dcc_separate_buffer->gpu_address;
2187
2188 /* no need to flag anything since this is called by fast clear that
2189 * flags framebuffer state
2190 */
2191 }
2192
2193 /**
2194 * Called by pipe_context::flush_resource, the place where DCC decompression
2195 * takes place.
2196 */
vi_separate_dcc_process_and_reset_stats(struct pipe_context * ctx,struct si_texture * tex)2197 void vi_separate_dcc_process_and_reset_stats(struct pipe_context *ctx, struct si_texture *tex)
2198 {
2199 struct si_context *sctx = (struct si_context *)ctx;
2200 struct pipe_query *tmp;
2201 unsigned i = vi_get_context_dcc_stats_index(sctx, tex);
2202 bool query_active = sctx->dcc_stats[i].query_active;
2203 bool disable = false;
2204
2205 if (sctx->dcc_stats[i].ps_stats[2]) {
2206 union pipe_query_result result;
2207
2208 /* Read the results. */
2209 struct pipe_query *query = sctx->dcc_stats[i].ps_stats[2];
2210 ctx->get_query_result(ctx, query, true, &result);
2211 si_query_buffer_reset(sctx, &((struct si_query_hw *)query)->buffer);
2212
2213 /* Compute the approximate number of fullscreen draws. */
2214 tex->ps_draw_ratio = result.pipeline_statistics.ps_invocations /
2215 (tex->buffer.b.b.width0 * tex->buffer.b.b.height0);
2216 sctx->last_tex_ps_draw_ratio = tex->ps_draw_ratio;
2217
2218 disable = tex->dcc_separate_buffer && !vi_should_enable_separate_dcc(tex);
2219 }
2220
2221 tex->num_slow_clears = 0;
2222
2223 /* stop the statistics query for ps_stats[0] */
2224 if (query_active)
2225 vi_separate_dcc_stop_query(sctx, tex);
2226
2227 /* Move the queries in the queue by one. */
2228 tmp = sctx->dcc_stats[i].ps_stats[2];
2229 sctx->dcc_stats[i].ps_stats[2] = sctx->dcc_stats[i].ps_stats[1];
2230 sctx->dcc_stats[i].ps_stats[1] = sctx->dcc_stats[i].ps_stats[0];
2231 sctx->dcc_stats[i].ps_stats[0] = tmp;
2232
2233 /* create and start a new query as ps_stats[0] */
2234 if (query_active)
2235 vi_separate_dcc_start_query(sctx, tex);
2236
2237 if (disable) {
2238 assert(!tex->last_dcc_separate_buffer);
2239 tex->last_dcc_separate_buffer = tex->dcc_separate_buffer;
2240 tex->dcc_separate_buffer = NULL;
2241 tex->surface.dcc_offset = 0;
2242 /* no need to flag anything since this is called after
2243 * decompression that re-sets framebuffer state
2244 */
2245 }
2246 }
2247
2248 static struct pipe_memory_object *
si_memobj_from_handle(struct pipe_screen * screen,struct winsys_handle * whandle,bool dedicated)2249 si_memobj_from_handle(struct pipe_screen *screen, struct winsys_handle *whandle, bool dedicated)
2250 {
2251 struct si_screen *sscreen = (struct si_screen *)screen;
2252 struct si_memory_object *memobj = CALLOC_STRUCT(si_memory_object);
2253 struct pb_buffer *buf = NULL;
2254
2255 if (!memobj)
2256 return NULL;
2257
2258 buf = sscreen->ws->buffer_from_handle(sscreen->ws, whandle, sscreen->info.max_alignment);
2259 if (!buf) {
2260 free(memobj);
2261 return NULL;
2262 }
2263
2264 memobj->b.dedicated = dedicated;
2265 memobj->buf = buf;
2266 memobj->stride = whandle->stride;
2267
2268 return (struct pipe_memory_object *)memobj;
2269 }
2270
si_memobj_destroy(struct pipe_screen * screen,struct pipe_memory_object * _memobj)2271 static void si_memobj_destroy(struct pipe_screen *screen, struct pipe_memory_object *_memobj)
2272 {
2273 struct si_memory_object *memobj = (struct si_memory_object *)_memobj;
2274
2275 pb_reference(&memobj->buf, NULL);
2276 free(memobj);
2277 }
2278
si_resource_from_memobj(struct pipe_screen * screen,const struct pipe_resource * templ,struct pipe_memory_object * _memobj,uint64_t offset)2279 static struct pipe_resource *si_resource_from_memobj(struct pipe_screen *screen,
2280 const struct pipe_resource *templ,
2281 struct pipe_memory_object *_memobj,
2282 uint64_t offset)
2283 {
2284 struct si_screen *sscreen = (struct si_screen *)screen;
2285 struct si_memory_object *memobj = (struct si_memory_object *)_memobj;
2286 struct pipe_resource *res;
2287
2288 if (templ->target == PIPE_BUFFER)
2289 res = si_buffer_from_winsys_buffer(screen, templ, memobj->buf,
2290 memobj->b.dedicated);
2291 else
2292 res = si_texture_from_winsys_buffer(sscreen, templ, memobj->buf,
2293 memobj->stride,
2294 offset,
2295 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE | PIPE_HANDLE_USAGE_SHADER_WRITE,
2296 memobj->b.dedicated);
2297
2298 if (!res)
2299 return NULL;
2300
2301 /* si_texture_from_winsys_buffer doesn't increment refcount of
2302 * memobj->buf, so increment it here.
2303 */
2304 struct pb_buffer *buf = NULL;
2305 pb_reference(&buf, memobj->buf);
2306 return res;
2307 }
2308
si_check_resource_capability(struct pipe_screen * screen,struct pipe_resource * resource,unsigned bind)2309 static bool si_check_resource_capability(struct pipe_screen *screen, struct pipe_resource *resource,
2310 unsigned bind)
2311 {
2312 struct si_texture *tex = (struct si_texture *)resource;
2313
2314 /* Buffers only support the linear flag. */
2315 if (resource->target == PIPE_BUFFER)
2316 return (bind & ~PIPE_BIND_LINEAR) == 0;
2317
2318 if (bind & PIPE_BIND_LINEAR && !tex->surface.is_linear)
2319 return false;
2320
2321 if (bind & PIPE_BIND_SCANOUT && !tex->surface.is_displayable)
2322 return false;
2323
2324 /* TODO: PIPE_BIND_CURSOR - do we care? */
2325 return true;
2326 }
2327
si_init_screen_texture_functions(struct si_screen * sscreen)2328 void si_init_screen_texture_functions(struct si_screen *sscreen)
2329 {
2330 sscreen->b.resource_from_handle = si_texture_from_handle;
2331 sscreen->b.resource_get_handle = si_texture_get_handle;
2332 sscreen->b.resource_get_param = si_resource_get_param;
2333 sscreen->b.resource_get_info = si_texture_get_info;
2334 sscreen->b.resource_from_memobj = si_resource_from_memobj;
2335 sscreen->b.memobj_create_from_handle = si_memobj_from_handle;
2336 sscreen->b.memobj_destroy = si_memobj_destroy;
2337 sscreen->b.check_resource_capability = si_check_resource_capability;
2338 }
2339
si_init_context_texture_functions(struct si_context * sctx)2340 void si_init_context_texture_functions(struct si_context *sctx)
2341 {
2342 sctx->b.create_surface = si_create_surface;
2343 sctx->b.surface_destroy = si_surface_destroy;
2344 }
2345