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