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