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