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