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