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