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