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