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