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