2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 * Copyright 2018 Advanced Micro Devices, Inc.
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:
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
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.
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"
38 #include "state_tracker/drm_driver.h"
40 #include "amd/addrlib/inc/addrinterface.h"
42 static enum radeon_surf_mode
43 si_choose_tiling(struct si_screen
*sscreen
,
44 const struct pipe_resource
*templ
, bool tc_compatible_htile
);
47 bool si_prepare_for_dma_blit(struct si_context
*sctx
,
48 struct si_texture
*dst
,
49 unsigned dst_level
, unsigned dstx
,
50 unsigned dsty
, unsigned dstz
,
51 struct si_texture
*src
,
53 const struct pipe_box
*src_box
)
58 if (dst
->surface
.bpe
!= src
->surface
.bpe
)
61 /* MSAA: Blits don't exist in the real world. */
62 if (src
->buffer
.b
.b
.nr_samples
> 1 ||
63 dst
->buffer
.b
.b
.nr_samples
> 1)
66 /* Depth-stencil surfaces:
67 * When dst is linear, the DB->CB copy preserves HTILE.
68 * When dst is tiled, the 3D path must be used to update HTILE.
70 if (src
->is_depth
|| dst
->is_depth
)
74 * src: Use the 3D path. DCC decompression is expensive.
75 * dst: Use the 3D path to compress the pixels with DCC.
77 if (vi_dcc_enabled(src
, src_level
) ||
78 vi_dcc_enabled(dst
, dst_level
))
82 * src: Both texture and SDMA paths need decompression. Use SDMA.
83 * dst: If overwriting the whole texture, discard CMASK and use
84 * SDMA. Otherwise, use the 3D path.
86 if (dst
->cmask_buffer
&& dst
->dirty_level_mask
& (1 << dst_level
)) {
87 /* The CMASK clear is only enabled for the first level. */
88 assert(dst_level
== 0);
89 if (!util_texrange_covers_whole_level(&dst
->buffer
.b
.b
, dst_level
,
90 dstx
, dsty
, dstz
, src_box
->width
,
91 src_box
->height
, src_box
->depth
))
94 si_texture_discard_cmask(sctx
->screen
, dst
);
97 /* All requirements are met. Prepare textures for SDMA. */
98 if (src
->cmask_buffer
&& src
->dirty_level_mask
& (1 << src_level
))
99 sctx
->b
.flush_resource(&sctx
->b
, &src
->buffer
.b
.b
);
101 assert(!(src
->dirty_level_mask
& (1 << src_level
)));
102 assert(!(dst
->dirty_level_mask
& (1 << dst_level
)));
107 /* Same as resource_copy_region, except that both upsampling and downsampling are allowed. */
108 static void si_copy_region_with_blit(struct pipe_context
*pipe
,
109 struct pipe_resource
*dst
,
111 unsigned dstx
, unsigned dsty
, unsigned dstz
,
112 struct pipe_resource
*src
,
114 const struct pipe_box
*src_box
)
116 struct pipe_blit_info blit
;
118 memset(&blit
, 0, sizeof(blit
));
119 blit
.src
.resource
= src
;
120 blit
.src
.format
= src
->format
;
121 blit
.src
.level
= src_level
;
122 blit
.src
.box
= *src_box
;
123 blit
.dst
.resource
= dst
;
124 blit
.dst
.format
= dst
->format
;
125 blit
.dst
.level
= dst_level
;
126 blit
.dst
.box
.x
= dstx
;
127 blit
.dst
.box
.y
= dsty
;
128 blit
.dst
.box
.z
= dstz
;
129 blit
.dst
.box
.width
= src_box
->width
;
130 blit
.dst
.box
.height
= src_box
->height
;
131 blit
.dst
.box
.depth
= src_box
->depth
;
132 blit
.mask
= util_format_get_mask(dst
->format
);
133 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
136 pipe
->blit(pipe
, &blit
);
140 /* Copy from a full GPU texture to a transfer's staging one. */
141 static void si_copy_to_staging_texture(struct pipe_context
*ctx
, struct si_transfer
*stransfer
)
143 struct si_context
*sctx
= (struct si_context
*)ctx
;
144 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)stransfer
;
145 struct pipe_resource
*dst
= &stransfer
->staging
->b
.b
;
146 struct pipe_resource
*src
= transfer
->resource
;
148 if (src
->nr_samples
> 1 || ((struct si_texture
*)src
)->is_depth
) {
149 si_copy_region_with_blit(ctx
, dst
, 0, 0, 0, 0,
150 src
, transfer
->level
, &transfer
->box
);
154 sctx
->dma_copy(ctx
, dst
, 0, 0, 0, 0, src
, transfer
->level
,
158 /* Copy from a transfer's staging texture to a full GPU one. */
159 static void si_copy_from_staging_texture(struct pipe_context
*ctx
, struct si_transfer
*stransfer
)
161 struct si_context
*sctx
= (struct si_context
*)ctx
;
162 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)stransfer
;
163 struct pipe_resource
*dst
= transfer
->resource
;
164 struct pipe_resource
*src
= &stransfer
->staging
->b
.b
;
165 struct pipe_box sbox
;
167 u_box_3d(0, 0, 0, transfer
->box
.width
, transfer
->box
.height
, transfer
->box
.depth
, &sbox
);
169 if (dst
->nr_samples
> 1 || ((struct si_texture
*)dst
)->is_depth
) {
170 si_copy_region_with_blit(ctx
, dst
, transfer
->level
,
171 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
176 if (util_format_is_compressed(dst
->format
)) {
177 sbox
.width
= util_format_get_nblocksx(dst
->format
, sbox
.width
);
178 sbox
.height
= util_format_get_nblocksx(dst
->format
, sbox
.height
);
181 sctx
->dma_copy(ctx
, dst
, transfer
->level
,
182 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
186 static unsigned si_texture_get_offset(struct si_screen
*sscreen
,
187 struct si_texture
*tex
, unsigned level
,
188 const struct pipe_box
*box
,
190 unsigned *layer_stride
)
192 if (sscreen
->info
.chip_class
>= GFX9
) {
193 *stride
= tex
->surface
.u
.gfx9
.surf_pitch
* tex
->surface
.bpe
;
194 *layer_stride
= tex
->surface
.u
.gfx9
.surf_slice_size
;
199 /* Each texture is an array of slices. Each slice is an array
200 * of mipmap levels. */
201 return box
->z
* tex
->surface
.u
.gfx9
.surf_slice_size
+
202 tex
->surface
.u
.gfx9
.offset
[level
] +
203 (box
->y
/ tex
->surface
.blk_h
*
204 tex
->surface
.u
.gfx9
.surf_pitch
+
205 box
->x
/ tex
->surface
.blk_w
) * tex
->surface
.bpe
;
207 *stride
= tex
->surface
.u
.legacy
.level
[level
].nblk_x
*
209 assert((uint64_t)tex
->surface
.u
.legacy
.level
[level
].slice_size_dw
* 4 <= UINT_MAX
);
210 *layer_stride
= (uint64_t)tex
->surface
.u
.legacy
.level
[level
].slice_size_dw
* 4;
213 return tex
->surface
.u
.legacy
.level
[level
].offset
;
215 /* Each texture is an array of mipmap levels. Each level is
216 * an array of slices. */
217 return tex
->surface
.u
.legacy
.level
[level
].offset
+
218 box
->z
* (uint64_t)tex
->surface
.u
.legacy
.level
[level
].slice_size_dw
* 4 +
219 (box
->y
/ tex
->surface
.blk_h
*
220 tex
->surface
.u
.legacy
.level
[level
].nblk_x
+
221 box
->x
/ tex
->surface
.blk_w
) * tex
->surface
.bpe
;
225 static int si_init_surface(struct si_screen
*sscreen
,
226 struct radeon_surf
*surface
,
227 const struct pipe_resource
*ptex
,
228 enum radeon_surf_mode array_mode
,
229 unsigned pitch_in_bytes_override
,
233 bool is_flushed_depth
,
234 bool tc_compatible_htile
)
236 const struct util_format_description
*desc
=
237 util_format_description(ptex
->format
);
238 bool is_depth
, is_stencil
;
240 unsigned i
, bpe
, flags
= 0;
242 is_depth
= util_format_has_depth(desc
);
243 is_stencil
= util_format_has_stencil(desc
);
245 if (!is_flushed_depth
&&
246 ptex
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
) {
247 bpe
= 4; /* stencil is allocated separately */
249 bpe
= util_format_get_blocksize(ptex
->format
);
250 assert(util_is_power_of_two_or_zero(bpe
));
253 if (!is_flushed_depth
&& is_depth
) {
254 flags
|= RADEON_SURF_ZBUFFER
;
256 if (tc_compatible_htile
&&
257 (sscreen
->info
.chip_class
>= GFX9
||
258 array_mode
== RADEON_SURF_MODE_2D
)) {
259 /* TC-compatible HTILE only supports Z32_FLOAT.
260 * GFX9 also supports Z16_UNORM.
261 * On GFX8, promote Z16 to Z32. DB->CB copies will convert
262 * the format for transfers.
264 if (sscreen
->info
.chip_class
== GFX8
)
267 flags
|= RADEON_SURF_TC_COMPATIBLE_HTILE
;
271 flags
|= RADEON_SURF_SBUFFER
;
274 if (sscreen
->info
.chip_class
>= GFX8
&&
275 (ptex
->flags
& SI_RESOURCE_FLAG_DISABLE_DCC
||
276 ptex
->format
== PIPE_FORMAT_R9G9B9E5_FLOAT
||
277 (ptex
->nr_samples
>= 2 && !sscreen
->dcc_msaa_allowed
)))
278 flags
|= RADEON_SURF_DISABLE_DCC
;
280 /* Stoney: 128bpp MSAA textures randomly fail piglit tests with DCC. */
281 if (sscreen
->info
.family
== CHIP_STONEY
&&
282 bpe
== 16 && ptex
->nr_samples
>= 2)
283 flags
|= RADEON_SURF_DISABLE_DCC
;
285 /* GFX8: DCC clear for 4x and 8x MSAA array textures unimplemented. */
286 if (sscreen
->info
.chip_class
== GFX8
&&
287 ptex
->nr_storage_samples
>= 4 &&
288 ptex
->array_size
> 1)
289 flags
|= RADEON_SURF_DISABLE_DCC
;
291 /* GFX9: DCC clear for 4x and 8x MSAA textures unimplemented. */
292 if (sscreen
->info
.chip_class
== GFX9
&&
293 ptex
->nr_storage_samples
>= 4)
294 flags
|= RADEON_SURF_DISABLE_DCC
;
296 /* TODO: GFX10: DCC causes corruption with MSAA. */
297 if (sscreen
->info
.chip_class
>= GFX10
&&
298 ptex
->nr_storage_samples
>= 2)
299 flags
|= RADEON_SURF_DISABLE_DCC
;
301 if (ptex
->bind
& PIPE_BIND_SCANOUT
|| is_scanout
) {
302 /* This should catch bugs in gallium users setting incorrect flags. */
303 assert(ptex
->nr_samples
<= 1 &&
304 ptex
->array_size
== 1 &&
306 ptex
->last_level
== 0 &&
307 !(flags
& RADEON_SURF_Z_OR_SBUFFER
));
309 flags
|= RADEON_SURF_SCANOUT
;
312 if (ptex
->bind
& PIPE_BIND_SHARED
)
313 flags
|= RADEON_SURF_SHAREABLE
;
315 flags
|= RADEON_SURF_IMPORTED
| RADEON_SURF_SHAREABLE
;
316 if (!(ptex
->flags
& SI_RESOURCE_FLAG_FORCE_MSAA_TILING
))
317 flags
|= RADEON_SURF_OPTIMIZE_FOR_SPACE
;
319 if (sscreen
->info
.chip_class
>= GFX10
&&
320 (ptex
->flags
& SI_RESOURCE_FLAG_FORCE_MSAA_TILING
)) {
321 flags
|= RADEON_SURF_FORCE_SWIZZLE_MODE
;
322 surface
->u
.gfx9
.surf
.swizzle_mode
= ADDR_SW_64KB_R_X
;
325 r
= sscreen
->ws
->surface_init(sscreen
->ws
, ptex
, flags
, bpe
,
326 array_mode
, surface
);
331 unsigned pitch
= pitch_in_bytes_override
/ bpe
;
333 if (sscreen
->info
.chip_class
>= GFX9
) {
335 surface
->u
.gfx9
.surf_pitch
= pitch
;
336 surface
->u
.gfx9
.surf_slice_size
=
337 (uint64_t)pitch
* surface
->u
.gfx9
.surf_height
* bpe
;
339 surface
->u
.gfx9
.surf_offset
= offset
;
342 surface
->u
.legacy
.level
[0].nblk_x
= pitch
;
343 surface
->u
.legacy
.level
[0].slice_size_dw
=
344 ((uint64_t)pitch
* surface
->u
.legacy
.level
[0].nblk_y
* bpe
) / 4;
347 for (i
= 0; i
< ARRAY_SIZE(surface
->u
.legacy
.level
); ++i
)
348 surface
->u
.legacy
.level
[i
].offset
+= offset
;
354 static void si_get_display_metadata(struct si_screen
*sscreen
,
355 struct radeon_surf
*surf
,
356 struct radeon_bo_metadata
*metadata
,
357 enum radeon_surf_mode
*array_mode
,
360 if (sscreen
->info
.chip_class
>= GFX9
) {
361 if (metadata
->u
.gfx9
.swizzle_mode
> 0)
362 *array_mode
= RADEON_SURF_MODE_2D
;
364 *array_mode
= RADEON_SURF_MODE_LINEAR_ALIGNED
;
366 *is_scanout
= metadata
->u
.gfx9
.swizzle_mode
== 0 ||
367 metadata
->u
.gfx9
.swizzle_mode
% 4 == 2;
369 surf
->u
.gfx9
.surf
.swizzle_mode
= metadata
->u
.gfx9
.swizzle_mode
;
371 if (metadata
->u
.gfx9
.dcc_offset_256B
) {
372 surf
->u
.gfx9
.display_dcc_pitch_max
= metadata
->u
.gfx9
.dcc_pitch_max
;
373 assert(metadata
->u
.gfx9
.dcc_independent_64B
== 1);
376 surf
->u
.legacy
.pipe_config
= metadata
->u
.legacy
.pipe_config
;
377 surf
->u
.legacy
.bankw
= metadata
->u
.legacy
.bankw
;
378 surf
->u
.legacy
.bankh
= metadata
->u
.legacy
.bankh
;
379 surf
->u
.legacy
.tile_split
= metadata
->u
.legacy
.tile_split
;
380 surf
->u
.legacy
.mtilea
= metadata
->u
.legacy
.mtilea
;
381 surf
->u
.legacy
.num_banks
= metadata
->u
.legacy
.num_banks
;
383 if (metadata
->u
.legacy
.macrotile
== RADEON_LAYOUT_TILED
)
384 *array_mode
= RADEON_SURF_MODE_2D
;
385 else if (metadata
->u
.legacy
.microtile
== RADEON_LAYOUT_TILED
)
386 *array_mode
= RADEON_SURF_MODE_1D
;
388 *array_mode
= RADEON_SURF_MODE_LINEAR_ALIGNED
;
390 *is_scanout
= metadata
->u
.legacy
.scanout
;
394 void si_eliminate_fast_color_clear(struct si_context
*sctx
,
395 struct si_texture
*tex
)
397 struct si_screen
*sscreen
= sctx
->screen
;
398 struct pipe_context
*ctx
= &sctx
->b
;
400 if (ctx
== sscreen
->aux_context
)
401 mtx_lock(&sscreen
->aux_context_lock
);
403 unsigned n
= sctx
->num_decompress_calls
;
404 ctx
->flush_resource(ctx
, &tex
->buffer
.b
.b
);
406 /* Flush only if any fast clear elimination took place. */
407 if (n
!= sctx
->num_decompress_calls
)
408 ctx
->flush(ctx
, NULL
, 0);
410 if (ctx
== sscreen
->aux_context
)
411 mtx_unlock(&sscreen
->aux_context_lock
);
414 void si_texture_discard_cmask(struct si_screen
*sscreen
,
415 struct si_texture
*tex
)
417 if (!tex
->cmask_buffer
)
420 assert(tex
->buffer
.b
.b
.nr_samples
<= 1);
423 tex
->cmask_base_address_reg
= tex
->buffer
.gpu_address
>> 8;
424 tex
->dirty_level_mask
= 0;
426 tex
->cb_color_info
&= ~S_028C70_FAST_CLEAR(1);
428 if (tex
->cmask_buffer
!= &tex
->buffer
)
429 si_resource_reference(&tex
->cmask_buffer
, NULL
);
431 tex
->cmask_buffer
= NULL
;
433 /* Notify all contexts about the change. */
434 p_atomic_inc(&sscreen
->dirty_tex_counter
);
435 p_atomic_inc(&sscreen
->compressed_colortex_counter
);
438 static bool si_can_disable_dcc(struct si_texture
*tex
)
440 /* We can't disable DCC if it can be written by another process. */
441 return tex
->dcc_offset
&&
442 (!tex
->buffer
.b
.is_shared
||
443 !(tex
->buffer
.external_usage
& PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE
));
446 static bool si_texture_discard_dcc(struct si_screen
*sscreen
,
447 struct si_texture
*tex
)
449 if (!si_can_disable_dcc(tex
)) {
450 assert(tex
->display_dcc_offset
== 0);
454 assert(tex
->dcc_separate_buffer
== NULL
);
458 tex
->display_dcc_offset
= 0;
459 tex
->dcc_retile_map_offset
= 0;
461 /* Notify all contexts about the change. */
462 p_atomic_inc(&sscreen
->dirty_tex_counter
);
467 * Disable DCC for the texture. (first decompress, then discard metadata).
469 * There is unresolved multi-context synchronization issue between
470 * screen::aux_context and the current context. If applications do this with
471 * multiple contexts, it's already undefined behavior for them and we don't
472 * have to worry about that. The scenario is:
474 * If context 1 disables DCC and context 2 has queued commands that write
475 * to the texture via CB with DCC enabled, and the order of operations is
477 * context 2 queues draw calls rendering to the texture, but doesn't flush
478 * context 1 disables DCC and flushes
479 * context 1 & 2 reset descriptors and FB state
480 * context 2 flushes (new compressed tiles written by the draw calls)
481 * context 1 & 2 read garbage, because DCC is disabled, yet there are
484 * \param sctx the current context if you have one, or sscreen->aux_context
487 bool si_texture_disable_dcc(struct si_context
*sctx
,
488 struct si_texture
*tex
)
490 struct si_screen
*sscreen
= sctx
->screen
;
492 if (!sctx
->has_graphics
)
493 return si_texture_discard_dcc(sscreen
, tex
);
495 if (!si_can_disable_dcc(tex
))
498 if (&sctx
->b
== sscreen
->aux_context
)
499 mtx_lock(&sscreen
->aux_context_lock
);
501 /* Decompress DCC. */
502 si_decompress_dcc(sctx
, tex
);
503 sctx
->b
.flush(&sctx
->b
, NULL
, 0);
505 if (&sctx
->b
== sscreen
->aux_context
)
506 mtx_unlock(&sscreen
->aux_context_lock
);
508 return si_texture_discard_dcc(sscreen
, tex
);
511 static void si_reallocate_texture_inplace(struct si_context
*sctx
,
512 struct si_texture
*tex
,
513 unsigned new_bind_flag
,
514 bool invalidate_storage
)
516 struct pipe_screen
*screen
= sctx
->b
.screen
;
517 struct si_texture
*new_tex
;
518 struct pipe_resource templ
= tex
->buffer
.b
.b
;
521 templ
.bind
|= new_bind_flag
;
523 if (tex
->buffer
.b
.is_shared
)
526 if (new_bind_flag
== PIPE_BIND_LINEAR
) {
527 if (tex
->surface
.is_linear
)
530 /* This fails with MSAA, depth, and compressed textures. */
531 if (si_choose_tiling(sctx
->screen
, &templ
, false) !=
532 RADEON_SURF_MODE_LINEAR_ALIGNED
)
536 new_tex
= (struct si_texture
*)screen
->resource_create(screen
, &templ
);
540 /* Copy the pixels to the new texture. */
541 if (!invalidate_storage
) {
542 for (i
= 0; i
<= templ
.last_level
; i
++) {
546 u_minify(templ
.width0
, i
), u_minify(templ
.height0
, i
),
547 util_num_layers(&templ
, i
), &box
);
549 sctx
->dma_copy(&sctx
->b
, &new_tex
->buffer
.b
.b
, i
, 0, 0, 0,
550 &tex
->buffer
.b
.b
, i
, &box
);
554 if (new_bind_flag
== PIPE_BIND_LINEAR
) {
555 si_texture_discard_cmask(sctx
->screen
, tex
);
556 si_texture_discard_dcc(sctx
->screen
, tex
);
559 /* Replace the structure fields of tex. */
560 tex
->buffer
.b
.b
.bind
= templ
.bind
;
561 pb_reference(&tex
->buffer
.buf
, new_tex
->buffer
.buf
);
562 tex
->buffer
.gpu_address
= new_tex
->buffer
.gpu_address
;
563 tex
->buffer
.vram_usage
= new_tex
->buffer
.vram_usage
;
564 tex
->buffer
.gart_usage
= new_tex
->buffer
.gart_usage
;
565 tex
->buffer
.bo_size
= new_tex
->buffer
.bo_size
;
566 tex
->buffer
.bo_alignment
= new_tex
->buffer
.bo_alignment
;
567 tex
->buffer
.domains
= new_tex
->buffer
.domains
;
568 tex
->buffer
.flags
= new_tex
->buffer
.flags
;
570 tex
->surface
= new_tex
->surface
;
571 tex
->size
= new_tex
->size
;
572 si_texture_reference(&tex
->flushed_depth_texture
,
573 new_tex
->flushed_depth_texture
);
575 tex
->fmask_offset
= new_tex
->fmask_offset
;
576 tex
->cmask_offset
= new_tex
->cmask_offset
;
577 tex
->cmask_base_address_reg
= new_tex
->cmask_base_address_reg
;
579 if (tex
->cmask_buffer
== &tex
->buffer
)
580 tex
->cmask_buffer
= NULL
;
582 si_resource_reference(&tex
->cmask_buffer
, NULL
);
584 if (new_tex
->cmask_buffer
== &new_tex
->buffer
)
585 tex
->cmask_buffer
= &tex
->buffer
;
587 si_resource_reference(&tex
->cmask_buffer
, new_tex
->cmask_buffer
);
589 tex
->dcc_offset
= new_tex
->dcc_offset
;
590 tex
->cb_color_info
= new_tex
->cb_color_info
;
591 memcpy(tex
->color_clear_value
, new_tex
->color_clear_value
,
592 sizeof(tex
->color_clear_value
));
593 tex
->last_msaa_resolve_target_micro_mode
= new_tex
->last_msaa_resolve_target_micro_mode
;
595 tex
->htile_offset
= new_tex
->htile_offset
;
596 tex
->depth_clear_value
= new_tex
->depth_clear_value
;
597 tex
->dirty_level_mask
= new_tex
->dirty_level_mask
;
598 tex
->stencil_dirty_level_mask
= new_tex
->stencil_dirty_level_mask
;
599 tex
->db_render_format
= new_tex
->db_render_format
;
600 tex
->stencil_clear_value
= new_tex
->stencil_clear_value
;
601 tex
->tc_compatible_htile
= new_tex
->tc_compatible_htile
;
602 tex
->depth_cleared
= new_tex
->depth_cleared
;
603 tex
->stencil_cleared
= new_tex
->stencil_cleared
;
604 tex
->upgraded_depth
= new_tex
->upgraded_depth
;
605 tex
->db_compatible
= new_tex
->db_compatible
;
606 tex
->can_sample_z
= new_tex
->can_sample_z
;
607 tex
->can_sample_s
= new_tex
->can_sample_s
;
609 tex
->separate_dcc_dirty
= new_tex
->separate_dcc_dirty
;
610 tex
->dcc_gather_statistics
= new_tex
->dcc_gather_statistics
;
611 si_resource_reference(&tex
->dcc_separate_buffer
,
612 new_tex
->dcc_separate_buffer
);
613 si_resource_reference(&tex
->last_dcc_separate_buffer
,
614 new_tex
->last_dcc_separate_buffer
);
616 if (new_bind_flag
== PIPE_BIND_LINEAR
) {
617 assert(!tex
->htile_offset
);
618 assert(!tex
->cmask_buffer
);
619 assert(!tex
->surface
.fmask_size
);
620 assert(!tex
->dcc_offset
);
621 assert(!tex
->is_depth
);
624 si_texture_reference(&new_tex
, NULL
);
626 p_atomic_inc(&sctx
->screen
->dirty_tex_counter
);
629 static uint32_t si_get_bo_metadata_word1(struct si_screen
*sscreen
)
631 return (ATI_VENDOR_ID
<< 16) | sscreen
->info
.pci_id
;
634 static void si_set_tex_bo_metadata(struct si_screen
*sscreen
,
635 struct si_texture
*tex
)
637 struct radeon_surf
*surface
= &tex
->surface
;
638 struct pipe_resource
*res
= &tex
->buffer
.b
.b
;
639 struct radeon_bo_metadata md
;
641 memset(&md
, 0, sizeof(md
));
643 if (sscreen
->info
.chip_class
>= GFX9
) {
644 md
.u
.gfx9
.swizzle_mode
= surface
->u
.gfx9
.surf
.swizzle_mode
;
646 if (tex
->dcc_offset
&& !tex
->dcc_separate_buffer
) {
647 uint64_t dcc_offset
=
648 tex
->display_dcc_offset
? tex
->display_dcc_offset
651 assert((dcc_offset
>> 8) != 0 && (dcc_offset
>> 8) < (1 << 24));
652 md
.u
.gfx9
.dcc_offset_256B
= dcc_offset
>> 8;
653 md
.u
.gfx9
.dcc_pitch_max
= tex
->surface
.u
.gfx9
.display_dcc_pitch_max
;
654 md
.u
.gfx9
.dcc_independent_64B
= 1;
657 md
.u
.legacy
.microtile
= surface
->u
.legacy
.level
[0].mode
>= RADEON_SURF_MODE_1D
?
658 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
;
659 md
.u
.legacy
.macrotile
= surface
->u
.legacy
.level
[0].mode
>= RADEON_SURF_MODE_2D
?
660 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
;
661 md
.u
.legacy
.pipe_config
= surface
->u
.legacy
.pipe_config
;
662 md
.u
.legacy
.bankw
= surface
->u
.legacy
.bankw
;
663 md
.u
.legacy
.bankh
= surface
->u
.legacy
.bankh
;
664 md
.u
.legacy
.tile_split
= surface
->u
.legacy
.tile_split
;
665 md
.u
.legacy
.mtilea
= surface
->u
.legacy
.mtilea
;
666 md
.u
.legacy
.num_banks
= surface
->u
.legacy
.num_banks
;
667 md
.u
.legacy
.stride
= surface
->u
.legacy
.level
[0].nblk_x
* surface
->bpe
;
668 md
.u
.legacy
.scanout
= (surface
->flags
& RADEON_SURF_SCANOUT
) != 0;
671 assert(tex
->dcc_separate_buffer
== NULL
);
672 assert(tex
->surface
.fmask_size
== 0);
674 /* Metadata image format format version 1:
675 * [0] = 1 (metadata format identifier)
676 * [1] = (VENDOR_ID << 16) | PCI_ID
677 * [2:9] = image descriptor for the whole resource
678 * [2] is always 0, because the base address is cleared
679 * [9] is the DCC offset bits [39:8] from the beginning of
681 * [10:10+LAST_LEVEL] = mipmap level offset bits [39:8] for each level
684 md
.metadata
[0] = 1; /* metadata image format version 1 */
686 /* TILE_MODE_INDEX is ambiguous without a PCI ID. */
687 md
.metadata
[1] = si_get_bo_metadata_word1(sscreen
);
689 static const unsigned char swizzle
[] = {
695 bool is_array
= util_texture_is_array(res
->target
);
698 sscreen
->make_texture_descriptor(sscreen
, tex
, true,
699 res
->target
, res
->format
,
700 swizzle
, 0, res
->last_level
, 0,
701 is_array
? res
->array_size
- 1 : 0,
702 res
->width0
, res
->height0
, res
->depth0
,
705 si_set_mutable_tex_desc_fields(sscreen
, tex
, &tex
->surface
.u
.legacy
.level
[0],
706 0, 0, tex
->surface
.blk_w
, false, desc
);
708 /* Clear the base address and set the relative DCC offset. */
710 desc
[1] &= C_008F14_BASE_ADDRESS_HI
;
712 switch (sscreen
->info
.chip_class
) {
717 desc
[7] = tex
->dcc_offset
>> 8;
720 desc
[7] = tex
->dcc_offset
>> 8;
721 desc
[5] &= C_008F24_META_DATA_ADDRESS
;
722 desc
[5] |= S_008F24_META_DATA_ADDRESS(tex
->dcc_offset
>> 40);
725 desc
[6] &= C_00A018_META_DATA_ADDRESS_LO
;
726 desc
[6] |= S_00A018_META_DATA_ADDRESS_LO(tex
->dcc_offset
>> 8);
727 desc
[7] = tex
->dcc_offset
>> 16;
734 /* Dwords [2:9] contain the image descriptor. */
735 memcpy(&md
.metadata
[2], desc
, sizeof(desc
));
736 md
.size_metadata
= 10 * 4;
738 /* Dwords [10:..] contain the mipmap level offsets. */
739 if (sscreen
->info
.chip_class
<= GFX8
) {
740 for (unsigned i
= 0; i
<= res
->last_level
; i
++)
741 md
.metadata
[10+i
] = tex
->surface
.u
.legacy
.level
[i
].offset
>> 8;
743 md
.size_metadata
+= (1 + res
->last_level
) * 4;
746 sscreen
->ws
->buffer_set_metadata(tex
->buffer
.buf
, &md
);
749 static bool si_read_tex_bo_metadata(struct si_screen
*sscreen
,
750 struct si_texture
*tex
,
751 struct radeon_bo_metadata
*md
)
753 uint32_t *desc
= &md
->metadata
[2];
755 if (md
->size_metadata
< 10 * 4 || /* at least 2(header) + 8(desc) dwords */
756 md
->metadata
[0] == 0 || /* invalid version number */
757 md
->metadata
[1] != si_get_bo_metadata_word1(sscreen
)) /* invalid PCI ID */ {
758 /* Don't report an error if the texture comes from an incompatible driver,
759 * but this might not work.
764 /* Validate that sample counts and the number of mipmap levels match. */
765 unsigned last_level
= G_008F1C_LAST_LEVEL(desc
[3]);
766 unsigned type
= G_008F1C_TYPE(desc
[3]);
768 if (type
== V_008F1C_SQ_RSRC_IMG_2D_MSAA
||
769 type
== V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY
) {
770 unsigned log_samples
=
771 util_logbase2(MAX2(1, tex
->buffer
.b
.b
.nr_storage_samples
));
773 if (last_level
!= log_samples
) {
774 fprintf(stderr
, "radeonsi: invalid MSAA texture import, "
775 "metadata has log2(samples) = %u, the caller set %u\n",
776 last_level
, log_samples
);
780 if (last_level
!= tex
->buffer
.b
.b
.last_level
) {
781 fprintf(stderr
, "radeonsi: invalid mipmapped texture import, "
782 "metadata has last_level = %u, the caller set %u\n",
783 last_level
, tex
->buffer
.b
.b
.last_level
);
788 if (sscreen
->info
.chip_class
>= GFX8
&&
789 G_008F28_COMPRESSION_EN(desc
[6])) {
790 /* Read DCC information.
792 * Some state trackers don't set the SCANOUT flag when
793 * importing displayable images, which affects PIPE_ALIGNED
794 * and RB_ALIGNED, so we need to recover them here.
796 switch (sscreen
->info
.chip_class
) {
798 tex
->dcc_offset
= (uint64_t)desc
[7] << 8;
803 ((uint64_t)desc
[7] << 8) |
804 ((uint64_t)G_008F24_META_DATA_ADDRESS(desc
[5]) << 40);
805 tex
->surface
.u
.gfx9
.dcc
.pipe_aligned
=
806 G_008F24_META_PIPE_ALIGNED(desc
[5]);
807 tex
->surface
.u
.gfx9
.dcc
.rb_aligned
=
808 G_008F24_META_RB_ALIGNED(desc
[5]);
810 /* If DCC is unaligned, this can only be a displayable image. */
811 if (!tex
->surface
.u
.gfx9
.dcc
.pipe_aligned
&&
812 !tex
->surface
.u
.gfx9
.dcc
.rb_aligned
)
813 tex
->surface
.is_displayable
= true;
818 ((uint64_t)G_00A018_META_DATA_ADDRESS_LO(desc
[6]) << 8) |
819 ((uint64_t)desc
[7] << 16);
820 tex
->surface
.u
.gfx9
.dcc
.pipe_aligned
=
821 G_00A018_META_PIPE_ALIGNED(desc
[6]);
829 /* Disable DCC. dcc_offset is always set by texture_from_handle
830 * and must be cleared here.
838 static bool si_has_displayable_dcc(struct si_texture
*tex
)
840 struct si_screen
*sscreen
= (struct si_screen
*)tex
->buffer
.b
.b
.screen
;
842 if (sscreen
->info
.chip_class
<= GFX8
)
845 /* This needs a cache flush before scanout.
846 * (it can't be scanned out and rendered to simultaneously)
848 if (sscreen
->info
.use_display_dcc_unaligned
&&
850 !tex
->surface
.u
.gfx9
.dcc
.pipe_aligned
&&
851 !tex
->surface
.u
.gfx9
.dcc
.rb_aligned
)
854 /* This needs an explicit flush (flush_resource). */
855 if (sscreen
->info
.use_display_dcc_with_retile_blit
&&
856 tex
->display_dcc_offset
)
862 static void si_texture_get_info(struct pipe_screen
* screen
,
863 struct pipe_resource
*resource
,
867 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
868 struct si_texture
*tex
= (struct si_texture
*)resource
;
872 if (!sscreen
|| !tex
)
875 if (resource
->target
!= PIPE_BUFFER
) {
876 if (sscreen
->info
.chip_class
>= GFX9
) {
877 offset
= tex
->surface
.u
.gfx9
.surf_offset
;
878 stride
= tex
->surface
.u
.gfx9
.surf_pitch
*
881 offset
= tex
->surface
.u
.legacy
.level
[0].offset
;
882 stride
= tex
->surface
.u
.legacy
.level
[0].nblk_x
*
894 static bool si_texture_get_handle(struct pipe_screen
* screen
,
895 struct pipe_context
*ctx
,
896 struct pipe_resource
*resource
,
897 struct winsys_handle
*whandle
,
900 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
901 struct si_context
*sctx
;
902 struct si_resource
*res
= si_resource(resource
);
903 struct si_texture
*tex
= (struct si_texture
*)resource
;
904 bool update_metadata
= false;
905 unsigned stride
, offset
, slice_size
;
908 ctx
= threaded_context_unwrap_sync(ctx
);
909 sctx
= (struct si_context
*)(ctx
? ctx
: sscreen
->aux_context
);
911 if (resource
->target
!= PIPE_BUFFER
) {
912 /* This is not supported now, but it might be required for OpenCL
913 * interop in the future.
915 if (resource
->nr_samples
> 1 || tex
->is_depth
)
918 /* Move a suballocated texture into a non-suballocated allocation. */
919 if (sscreen
->ws
->buffer_is_suballocated(res
->buf
) ||
920 tex
->surface
.tile_swizzle
||
921 (tex
->buffer
.flags
& RADEON_FLAG_NO_INTERPROCESS_SHARING
&&
922 sscreen
->info
.has_local_buffers
&&
923 whandle
->type
!= WINSYS_HANDLE_TYPE_KMS
)) {
924 assert(!res
->b
.is_shared
);
925 si_reallocate_texture_inplace(sctx
, tex
,
926 PIPE_BIND_SHARED
, false);
928 assert(res
->b
.b
.bind
& PIPE_BIND_SHARED
);
929 assert(res
->flags
& RADEON_FLAG_NO_SUBALLOC
);
930 assert(!(res
->flags
& RADEON_FLAG_NO_INTERPROCESS_SHARING
));
931 assert(tex
->surface
.tile_swizzle
== 0);
934 /* Since shader image stores don't support DCC on GFX8,
935 * disable it for external clients that want write
938 if ((usage
& PIPE_HANDLE_USAGE_SHADER_WRITE
&& tex
->dcc_offset
) ||
939 /* Displayable DCC requires an explicit flush. */
940 (!(usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
) &&
941 si_has_displayable_dcc(tex
))) {
942 if (si_texture_disable_dcc(sctx
, tex
)) {
943 update_metadata
= true;
944 /* si_texture_disable_dcc flushes the context */
949 if (!(usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
) &&
950 (tex
->cmask_buffer
|| tex
->dcc_offset
)) {
951 /* Eliminate fast clear (both CMASK and DCC) */
952 si_eliminate_fast_color_clear(sctx
, tex
);
953 /* eliminate_fast_color_clear flushes the context */
956 /* Disable CMASK if flush_resource isn't going
959 if (tex
->cmask_buffer
)
960 si_texture_discard_cmask(sscreen
, tex
);
964 if (!res
->b
.is_shared
|| update_metadata
)
965 si_set_tex_bo_metadata(sscreen
, tex
);
967 if (sscreen
->info
.chip_class
>= GFX9
) {
968 slice_size
= tex
->surface
.u
.gfx9
.surf_slice_size
;
970 slice_size
= (uint64_t)tex
->surface
.u
.legacy
.level
[0].slice_size_dw
* 4;
973 /* Buffer exports are for the OpenCL interop. */
974 /* Move a suballocated buffer into a non-suballocated allocation. */
975 if (sscreen
->ws
->buffer_is_suballocated(res
->buf
) ||
976 /* A DMABUF export always fails if the BO is local. */
977 (tex
->buffer
.flags
& RADEON_FLAG_NO_INTERPROCESS_SHARING
&&
978 sscreen
->info
.has_local_buffers
)) {
979 assert(!res
->b
.is_shared
);
981 /* Allocate a new buffer with PIPE_BIND_SHARED. */
982 struct pipe_resource templ
= res
->b
.b
;
983 templ
.bind
|= PIPE_BIND_SHARED
;
985 struct pipe_resource
*newb
=
986 screen
->resource_create(screen
, &templ
);
990 /* Copy the old buffer contents to the new one. */
992 u_box_1d(0, newb
->width0
, &box
);
993 sctx
->b
.resource_copy_region(&sctx
->b
, newb
, 0, 0, 0, 0,
996 /* Move the new buffer storage to the old pipe_resource. */
997 si_replace_buffer_storage(&sctx
->b
, &res
->b
.b
, newb
);
998 pipe_resource_reference(&newb
, NULL
);
1000 assert(res
->b
.b
.bind
& PIPE_BIND_SHARED
);
1001 assert(res
->flags
& RADEON_FLAG_NO_SUBALLOC
);
1008 si_texture_get_info(screen
, resource
, &stride
, &offset
);
1011 sctx
->b
.flush(&sctx
->b
, NULL
, 0);
1013 if (res
->b
.is_shared
) {
1014 /* USAGE_EXPLICIT_FLUSH must be cleared if at least one user
1017 res
->external_usage
|= usage
& ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
;
1018 if (!(usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
))
1019 res
->external_usage
&= ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
;
1021 res
->b
.is_shared
= true;
1022 res
->external_usage
= usage
;
1025 return sscreen
->ws
->buffer_get_handle(sscreen
->ws
, res
->buf
, stride
,
1026 offset
, slice_size
, whandle
);
1029 static void si_texture_destroy(struct pipe_screen
*screen
,
1030 struct pipe_resource
*ptex
)
1032 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
1033 struct si_texture
*tex
= (struct si_texture
*)ptex
;
1034 struct si_resource
*resource
= &tex
->buffer
;
1036 if (sscreen
->info
.chip_class
>= GFX9
)
1037 free(tex
->surface
.u
.gfx9
.dcc_retile_map
);
1039 si_texture_reference(&tex
->flushed_depth_texture
, NULL
);
1041 if (tex
->cmask_buffer
!= &tex
->buffer
) {
1042 si_resource_reference(&tex
->cmask_buffer
, NULL
);
1044 pb_reference(&resource
->buf
, NULL
);
1045 si_resource_reference(&tex
->dcc_separate_buffer
, NULL
);
1046 si_resource_reference(&tex
->last_dcc_separate_buffer
, NULL
);
1050 static const struct u_resource_vtbl si_texture_vtbl
;
1052 static void si_texture_get_htile_size(struct si_screen
*sscreen
,
1053 struct si_texture
*tex
)
1055 unsigned cl_width
, cl_height
, width
, height
;
1056 unsigned slice_elements
, slice_bytes
, pipe_interleave_bytes
, base_align
;
1057 unsigned num_pipes
= sscreen
->info
.num_tile_pipes
;
1059 assert(sscreen
->info
.chip_class
<= GFX8
);
1061 tex
->surface
.htile_size
= 0;
1063 if (tex
->surface
.u
.legacy
.level
[0].mode
== RADEON_SURF_MODE_1D
&&
1064 !sscreen
->info
.htile_cmask_support_1d_tiling
)
1067 /* Overalign HTILE on P2 configs to work around GPU hangs in
1068 * piglit/depthstencil-render-miplevels 585.
1070 * This has been confirmed to help Kabini & Stoney, where the hangs
1071 * are always reproducible. I think I have seen the test hang
1072 * on Carrizo too, though it was very rare there.
1074 if (sscreen
->info
.chip_class
>= GFX7
&& num_pipes
< 4)
1077 switch (num_pipes
) {
1103 width
= align(tex
->surface
.u
.legacy
.level
[0].nblk_x
, cl_width
* 8);
1104 height
= align(tex
->surface
.u
.legacy
.level
[0].nblk_y
, cl_height
* 8);
1106 slice_elements
= (width
* height
) / (8 * 8);
1107 slice_bytes
= slice_elements
* 4;
1109 pipe_interleave_bytes
= sscreen
->info
.pipe_interleave_bytes
;
1110 base_align
= num_pipes
* pipe_interleave_bytes
;
1112 tex
->surface
.htile_alignment
= base_align
;
1113 tex
->surface
.htile_size
=
1114 util_num_layers(&tex
->buffer
.b
.b
, 0) *
1115 align(slice_bytes
, base_align
);
1118 static void si_texture_allocate_htile(struct si_screen
*sscreen
,
1119 struct si_texture
*tex
)
1121 if (sscreen
->info
.chip_class
<= GFX8
&& !tex
->tc_compatible_htile
)
1122 si_texture_get_htile_size(sscreen
, tex
);
1124 if (!tex
->surface
.htile_size
)
1127 tex
->htile_offset
= align(tex
->size
, tex
->surface
.htile_alignment
);
1128 tex
->size
= tex
->htile_offset
+ tex
->surface
.htile_size
;
1131 void si_print_texture_info(struct si_screen
*sscreen
,
1132 struct si_texture
*tex
, struct u_log_context
*log
)
1136 /* Common parameters. */
1137 u_log_printf(log
, " Info: npix_x=%u, npix_y=%u, npix_z=%u, blk_w=%u, "
1138 "blk_h=%u, array_size=%u, last_level=%u, "
1139 "bpe=%u, nsamples=%u, flags=0x%x, %s\n",
1140 tex
->buffer
.b
.b
.width0
, tex
->buffer
.b
.b
.height0
,
1141 tex
->buffer
.b
.b
.depth0
, tex
->surface
.blk_w
,
1143 tex
->buffer
.b
.b
.array_size
, tex
->buffer
.b
.b
.last_level
,
1144 tex
->surface
.bpe
, tex
->buffer
.b
.b
.nr_samples
,
1145 tex
->surface
.flags
, util_format_short_name(tex
->buffer
.b
.b
.format
));
1147 if (sscreen
->info
.chip_class
>= GFX9
) {
1148 u_log_printf(log
, " Surf: size=%"PRIu64
", slice_size=%"PRIu64
", "
1149 "alignment=%u, swmode=%u, epitch=%u, pitch=%u\n",
1150 tex
->surface
.surf_size
,
1151 tex
->surface
.u
.gfx9
.surf_slice_size
,
1152 tex
->surface
.surf_alignment
,
1153 tex
->surface
.u
.gfx9
.surf
.swizzle_mode
,
1154 tex
->surface
.u
.gfx9
.surf
.epitch
,
1155 tex
->surface
.u
.gfx9
.surf_pitch
);
1157 if (tex
->fmask_offset
) {
1158 u_log_printf(log
, " FMASK: offset=%"PRIu64
", size=%"PRIu64
", "
1159 "alignment=%u, swmode=%u, epitch=%u\n",
1161 tex
->surface
.fmask_size
,
1162 tex
->surface
.fmask_alignment
,
1163 tex
->surface
.u
.gfx9
.fmask
.swizzle_mode
,
1164 tex
->surface
.u
.gfx9
.fmask
.epitch
);
1167 if (tex
->cmask_buffer
) {
1168 u_log_printf(log
, " CMask: offset=%"PRIu64
", size=%u, "
1169 "alignment=%u, rb_aligned=%u, pipe_aligned=%u\n",
1171 tex
->surface
.cmask_size
,
1172 tex
->surface
.cmask_alignment
,
1173 tex
->surface
.u
.gfx9
.cmask
.rb_aligned
,
1174 tex
->surface
.u
.gfx9
.cmask
.pipe_aligned
);
1177 if (tex
->htile_offset
) {
1178 u_log_printf(log
, " HTile: offset=%"PRIu64
", size=%u, alignment=%u, "
1179 "rb_aligned=%u, pipe_aligned=%u\n",
1181 tex
->surface
.htile_size
,
1182 tex
->surface
.htile_alignment
,
1183 tex
->surface
.u
.gfx9
.htile
.rb_aligned
,
1184 tex
->surface
.u
.gfx9
.htile
.pipe_aligned
);
1187 if (tex
->dcc_offset
) {
1188 u_log_printf(log
, " DCC: offset=%"PRIu64
", size=%u, "
1189 "alignment=%u, pitch_max=%u, num_dcc_levels=%u\n",
1190 tex
->dcc_offset
, tex
->surface
.dcc_size
,
1191 tex
->surface
.dcc_alignment
,
1192 tex
->surface
.u
.gfx9
.display_dcc_pitch_max
,
1193 tex
->surface
.num_dcc_levels
);
1196 if (tex
->surface
.u
.gfx9
.stencil_offset
) {
1197 u_log_printf(log
, " Stencil: offset=%"PRIu64
", swmode=%u, epitch=%u\n",
1198 tex
->surface
.u
.gfx9
.stencil_offset
,
1199 tex
->surface
.u
.gfx9
.stencil
.swizzle_mode
,
1200 tex
->surface
.u
.gfx9
.stencil
.epitch
);
1205 u_log_printf(log
, " Layout: size=%"PRIu64
", alignment=%u, bankw=%u, "
1206 "bankh=%u, nbanks=%u, mtilea=%u, tilesplit=%u, pipeconfig=%u, scanout=%u\n",
1207 tex
->surface
.surf_size
, tex
->surface
.surf_alignment
, tex
->surface
.u
.legacy
.bankw
,
1208 tex
->surface
.u
.legacy
.bankh
, tex
->surface
.u
.legacy
.num_banks
, tex
->surface
.u
.legacy
.mtilea
,
1209 tex
->surface
.u
.legacy
.tile_split
, tex
->surface
.u
.legacy
.pipe_config
,
1210 (tex
->surface
.flags
& RADEON_SURF_SCANOUT
) != 0);
1212 if (tex
->fmask_offset
)
1213 u_log_printf(log
, " FMask: offset=%"PRIu64
", size=%"PRIu64
", alignment=%u, pitch_in_pixels=%u, "
1214 "bankh=%u, slice_tile_max=%u, tile_mode_index=%u\n",
1215 tex
->fmask_offset
, tex
->surface
.fmask_size
, tex
->surface
.fmask_alignment
,
1216 tex
->surface
.u
.legacy
.fmask
.pitch_in_pixels
,
1217 tex
->surface
.u
.legacy
.fmask
.bankh
,
1218 tex
->surface
.u
.legacy
.fmask
.slice_tile_max
,
1219 tex
->surface
.u
.legacy
.fmask
.tiling_index
);
1221 if (tex
->cmask_buffer
)
1222 u_log_printf(log
, " CMask: offset=%"PRIu64
", size=%u, alignment=%u, "
1223 "slice_tile_max=%u\n",
1224 tex
->cmask_offset
, tex
->surface
.cmask_size
, tex
->surface
.cmask_alignment
,
1225 tex
->surface
.u
.legacy
.cmask_slice_tile_max
);
1227 if (tex
->htile_offset
)
1228 u_log_printf(log
, " HTile: offset=%"PRIu64
", size=%u, "
1229 "alignment=%u, TC_compatible = %u\n",
1230 tex
->htile_offset
, tex
->surface
.htile_size
,
1231 tex
->surface
.htile_alignment
,
1232 tex
->tc_compatible_htile
);
1234 if (tex
->dcc_offset
) {
1235 u_log_printf(log
, " DCC: offset=%"PRIu64
", size=%u, alignment=%u\n",
1236 tex
->dcc_offset
, tex
->surface
.dcc_size
,
1237 tex
->surface
.dcc_alignment
);
1238 for (i
= 0; i
<= tex
->buffer
.b
.b
.last_level
; i
++)
1239 u_log_printf(log
, " DCCLevel[%i]: enabled=%u, offset=%u, "
1240 "fast_clear_size=%u\n",
1241 i
, i
< tex
->surface
.num_dcc_levels
,
1242 tex
->surface
.u
.legacy
.level
[i
].dcc_offset
,
1243 tex
->surface
.u
.legacy
.level
[i
].dcc_fast_clear_size
);
1246 for (i
= 0; i
<= tex
->buffer
.b
.b
.last_level
; i
++)
1247 u_log_printf(log
, " Level[%i]: offset=%"PRIu64
", slice_size=%"PRIu64
", "
1248 "npix_x=%u, npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
1249 "mode=%u, tiling_index = %u\n",
1250 i
, tex
->surface
.u
.legacy
.level
[i
].offset
,
1251 (uint64_t)tex
->surface
.u
.legacy
.level
[i
].slice_size_dw
* 4,
1252 u_minify(tex
->buffer
.b
.b
.width0
, i
),
1253 u_minify(tex
->buffer
.b
.b
.height0
, i
),
1254 u_minify(tex
->buffer
.b
.b
.depth0
, i
),
1255 tex
->surface
.u
.legacy
.level
[i
].nblk_x
,
1256 tex
->surface
.u
.legacy
.level
[i
].nblk_y
,
1257 tex
->surface
.u
.legacy
.level
[i
].mode
,
1258 tex
->surface
.u
.legacy
.tiling_index
[i
]);
1260 if (tex
->surface
.has_stencil
) {
1261 u_log_printf(log
, " StencilLayout: tilesplit=%u\n",
1262 tex
->surface
.u
.legacy
.stencil_tile_split
);
1263 for (i
= 0; i
<= tex
->buffer
.b
.b
.last_level
; i
++) {
1264 u_log_printf(log
, " StencilLevel[%i]: offset=%"PRIu64
", "
1265 "slice_size=%"PRIu64
", npix_x=%u, "
1266 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
1267 "mode=%u, tiling_index = %u\n",
1268 i
, tex
->surface
.u
.legacy
.stencil_level
[i
].offset
,
1269 (uint64_t)tex
->surface
.u
.legacy
.stencil_level
[i
].slice_size_dw
* 4,
1270 u_minify(tex
->buffer
.b
.b
.width0
, i
),
1271 u_minify(tex
->buffer
.b
.b
.height0
, i
),
1272 u_minify(tex
->buffer
.b
.b
.depth0
, i
),
1273 tex
->surface
.u
.legacy
.stencil_level
[i
].nblk_x
,
1274 tex
->surface
.u
.legacy
.stencil_level
[i
].nblk_y
,
1275 tex
->surface
.u
.legacy
.stencil_level
[i
].mode
,
1276 tex
->surface
.u
.legacy
.stencil_tiling_index
[i
]);
1281 /* Common processing for si_texture_create and si_texture_from_handle */
1282 static struct si_texture
*
1283 si_texture_create_object(struct pipe_screen
*screen
,
1284 const struct pipe_resource
*base
,
1285 struct pb_buffer
*buf
,
1286 struct radeon_surf
*surface
)
1288 struct si_texture
*tex
;
1289 struct si_resource
*resource
;
1290 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
1292 tex
= CALLOC_STRUCT(si_texture
);
1296 resource
= &tex
->buffer
;
1297 resource
->b
.b
= *base
;
1298 resource
->b
.b
.next
= NULL
;
1299 resource
->b
.vtbl
= &si_texture_vtbl
;
1300 pipe_reference_init(&resource
->b
.b
.reference
, 1);
1301 resource
->b
.b
.screen
= screen
;
1303 /* don't include stencil-only formats which we don't support for rendering */
1304 tex
->is_depth
= util_format_has_depth(util_format_description(tex
->buffer
.b
.b
.format
));
1306 tex
->surface
= *surface
;
1307 tex
->size
= tex
->surface
.surf_size
;
1309 tex
->tc_compatible_htile
= tex
->surface
.htile_size
!= 0 &&
1310 (tex
->surface
.flags
&
1311 RADEON_SURF_TC_COMPATIBLE_HTILE
);
1313 /* TC-compatible HTILE:
1314 * - GFX8 only supports Z32_FLOAT.
1315 * - GFX9 only supports Z32_FLOAT and Z16_UNORM. */
1316 if (tex
->tc_compatible_htile
) {
1317 if (sscreen
->info
.chip_class
>= GFX9
&&
1318 base
->format
== PIPE_FORMAT_Z16_UNORM
)
1319 tex
->db_render_format
= base
->format
;
1321 tex
->db_render_format
= PIPE_FORMAT_Z32_FLOAT
;
1322 tex
->upgraded_depth
= base
->format
!= PIPE_FORMAT_Z32_FLOAT
&&
1323 base
->format
!= PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
;
1326 tex
->db_render_format
= base
->format
;
1329 /* Applies to GCN. */
1330 tex
->last_msaa_resolve_target_micro_mode
= tex
->surface
.micro_tile_mode
;
1332 /* Disable separate DCC at the beginning. DRI2 doesn't reuse buffers
1333 * between frames, so the only thing that can enable separate DCC
1334 * with DRI2 is multiple slow clears within a frame.
1336 tex
->ps_draw_ratio
= 0;
1338 if (tex
->is_depth
) {
1339 if (sscreen
->info
.chip_class
>= GFX9
) {
1340 tex
->can_sample_z
= true;
1341 tex
->can_sample_s
= true;
1343 /* Stencil texturing with HTILE doesn't work
1344 * with mipmapping on Navi10-14. */
1345 if ((sscreen
->info
.family
== CHIP_NAVI10
||
1346 sscreen
->info
.family
== CHIP_NAVI12
||
1347 sscreen
->info
.family
== CHIP_NAVI14
) &&
1348 base
->last_level
> 0)
1349 tex
->htile_stencil_disabled
= true;
1351 tex
->can_sample_z
= !tex
->surface
.u
.legacy
.depth_adjusted
;
1352 tex
->can_sample_s
= !tex
->surface
.u
.legacy
.stencil_adjusted
;
1355 if (!(base
->flags
& (SI_RESOURCE_FLAG_TRANSFER
|
1356 SI_RESOURCE_FLAG_FLUSHED_DEPTH
))) {
1357 tex
->db_compatible
= true;
1359 if (!(sscreen
->debug_flags
& DBG(NO_HYPERZ
)))
1360 si_texture_allocate_htile(sscreen
, tex
);
1363 if (base
->nr_samples
> 1 &&
1365 !(sscreen
->debug_flags
& DBG(NO_FMASK
))) {
1366 /* Allocate FMASK. */
1367 tex
->fmask_offset
= align64(tex
->size
,
1368 tex
->surface
.fmask_alignment
);
1369 tex
->size
= tex
->fmask_offset
+ tex
->surface
.fmask_size
;
1371 /* Allocate CMASK. */
1372 tex
->cmask_offset
= align64(tex
->size
, tex
->surface
.cmask_alignment
);
1373 tex
->size
= tex
->cmask_offset
+ tex
->surface
.cmask_size
;
1374 tex
->cb_color_info
|= S_028C70_FAST_CLEAR(1);
1375 tex
->cmask_buffer
= &tex
->buffer
;
1377 if (!tex
->surface
.fmask_size
|| !tex
->surface
.cmask_size
)
1381 /* Shared textures must always set up DCC here.
1382 * If it's not present, it will be disabled by
1383 * si_get_opaque_metadata later.
1385 if (tex
->surface
.dcc_size
&&
1386 (buf
|| !(sscreen
->debug_flags
& DBG(NO_DCC
))) &&
1387 (sscreen
->info
.use_display_dcc_unaligned
||
1388 sscreen
->info
.use_display_dcc_with_retile_blit
||
1389 !(tex
->surface
.flags
& RADEON_SURF_SCANOUT
))) {
1390 /* Add space for the DCC buffer. */
1391 tex
->dcc_offset
= align64(tex
->size
, tex
->surface
.dcc_alignment
);
1392 tex
->size
= tex
->dcc_offset
+ tex
->surface
.dcc_size
;
1394 if (sscreen
->info
.chip_class
>= GFX9
&&
1395 tex
->surface
.u
.gfx9
.dcc_retile_num_elements
) {
1396 /* Add space for the displayable DCC buffer. */
1397 tex
->display_dcc_offset
=
1398 align64(tex
->size
, tex
->surface
.u
.gfx9
.display_dcc_alignment
);
1399 tex
->size
= tex
->display_dcc_offset
+
1400 tex
->surface
.u
.gfx9
.display_dcc_size
;
1402 /* Add space for the DCC retile buffer. (16-bit or 32-bit elements) */
1403 tex
->dcc_retile_map_offset
=
1404 align64(tex
->size
, sscreen
->info
.tcc_cache_line_size
);
1406 if (tex
->surface
.u
.gfx9
.dcc_retile_use_uint16
) {
1407 tex
->size
= tex
->dcc_retile_map_offset
+
1408 tex
->surface
.u
.gfx9
.dcc_retile_num_elements
* 2;
1410 tex
->size
= tex
->dcc_retile_map_offset
+
1411 tex
->surface
.u
.gfx9
.dcc_retile_num_elements
* 4;
1417 /* Now create the backing buffer. */
1419 si_init_resource_fields(sscreen
, resource
, tex
->size
,
1420 tex
->surface
.surf_alignment
);
1422 if (!si_alloc_resource(sscreen
, resource
))
1425 resource
->buf
= buf
;
1426 resource
->gpu_address
= sscreen
->ws
->buffer_get_virtual_address(resource
->buf
);
1427 resource
->bo_size
= buf
->size
;
1428 resource
->bo_alignment
= buf
->alignment
;
1429 resource
->domains
= sscreen
->ws
->buffer_get_initial_domain(resource
->buf
);
1430 if (resource
->domains
& RADEON_DOMAIN_VRAM
)
1431 resource
->vram_usage
= buf
->size
;
1432 else if (resource
->domains
& RADEON_DOMAIN_GTT
)
1433 resource
->gart_usage
= buf
->size
;
1436 if (tex
->cmask_buffer
) {
1437 /* Initialize the cmask to 0xCC (= compressed state). */
1438 si_screen_clear_buffer(sscreen
, &tex
->cmask_buffer
->b
.b
,
1439 tex
->cmask_offset
, tex
->surface
.cmask_size
,
1442 if (tex
->htile_offset
) {
1443 uint32_t clear_value
= 0;
1445 if (sscreen
->info
.chip_class
>= GFX9
|| tex
->tc_compatible_htile
)
1446 clear_value
= 0x0000030F;
1448 si_screen_clear_buffer(sscreen
, &tex
->buffer
.b
.b
,
1450 tex
->surface
.htile_size
,
1454 /* Initialize DCC only if the texture is not being imported. */
1455 if (!buf
&& tex
->dcc_offset
) {
1456 /* Clear DCC to black for all tiles with DCC enabled.
1458 * This fixes corruption in 3DMark Slingshot Extreme, which
1459 * uses uninitialized textures, causing corruption.
1461 if (tex
->surface
.num_dcc_levels
== tex
->buffer
.b
.b
.last_level
+ 1 &&
1462 tex
->buffer
.b
.b
.nr_samples
<= 2) {
1463 /* Simple case - all tiles have DCC enabled. */
1464 si_screen_clear_buffer(sscreen
, &tex
->buffer
.b
.b
,
1466 tex
->surface
.dcc_size
,
1467 DCC_CLEAR_COLOR_0000
);
1468 } else if (sscreen
->info
.chip_class
>= GFX9
) {
1469 /* Clear to uncompressed. Clearing this to black is complicated. */
1470 si_screen_clear_buffer(sscreen
, &tex
->buffer
.b
.b
,
1472 tex
->surface
.dcc_size
,
1475 /* GFX8: Initialize mipmap levels and multisamples separately. */
1476 if (tex
->buffer
.b
.b
.nr_samples
>= 2) {
1477 /* Clearing this to black is complicated. */
1478 si_screen_clear_buffer(sscreen
, &tex
->buffer
.b
.b
,
1480 tex
->surface
.dcc_size
,
1483 /* Clear the enabled mipmap levels to black. */
1486 for (unsigned i
= 0; i
< tex
->surface
.num_dcc_levels
; i
++) {
1487 if (!tex
->surface
.u
.legacy
.level
[i
].dcc_fast_clear_size
)
1490 size
= tex
->surface
.u
.legacy
.level
[i
].dcc_offset
+
1491 tex
->surface
.u
.legacy
.level
[i
].dcc_fast_clear_size
;
1494 /* Mipmap levels with DCC. */
1496 si_screen_clear_buffer(sscreen
, &tex
->buffer
.b
.b
,
1497 tex
->dcc_offset
, size
,
1498 DCC_CLEAR_COLOR_0000
);
1500 /* Mipmap levels without DCC. */
1501 if (size
!= tex
->surface
.dcc_size
) {
1502 si_screen_clear_buffer(sscreen
, &tex
->buffer
.b
.b
,
1503 tex
->dcc_offset
+ size
,
1504 tex
->surface
.dcc_size
- size
,
1510 /* Upload the DCC retile map. */
1511 if (tex
->dcc_retile_map_offset
) {
1512 /* Use a staging buffer for the upload, because
1513 * the buffer backing the texture is unmappable.
1515 bool use_uint16
= tex
->surface
.u
.gfx9
.dcc_retile_use_uint16
;
1516 unsigned num_elements
= tex
->surface
.u
.gfx9
.dcc_retile_num_elements
;
1517 struct si_resource
*buf
=
1518 si_aligned_buffer_create(screen
, 0, PIPE_USAGE_STREAM
,
1519 num_elements
* (use_uint16
? 2 : 4),
1520 sscreen
->info
.tcc_cache_line_size
);
1521 uint32_t *ui
= (uint32_t*)sscreen
->ws
->buffer_map(buf
->buf
, NULL
,
1522 PIPE_TRANSFER_WRITE
);
1523 uint16_t *us
= (uint16_t*)ui
;
1525 /* Upload the retile map into a staging buffer. */
1527 for (unsigned i
= 0; i
< num_elements
; i
++)
1528 us
[i
] = tex
->surface
.u
.gfx9
.dcc_retile_map
[i
];
1530 for (unsigned i
= 0; i
< num_elements
; i
++)
1531 ui
[i
] = tex
->surface
.u
.gfx9
.dcc_retile_map
[i
];
1534 /* Copy the staging buffer to the buffer backing the texture. */
1535 struct si_context
*sctx
= (struct si_context
*)sscreen
->aux_context
;
1536 struct pipe_box box
;
1537 u_box_1d(0, buf
->b
.b
.width0
, &box
);
1539 assert(tex
->dcc_retile_map_offset
<= UINT_MAX
);
1540 mtx_lock(&sscreen
->aux_context_lock
);
1541 sctx
->dma_copy(&sctx
->b
, &tex
->buffer
.b
.b
, 0,
1542 tex
->dcc_retile_map_offset
, 0, 0,
1543 &buf
->b
.b
, 0, &box
);
1544 sscreen
->aux_context
->flush(sscreen
->aux_context
, NULL
, 0);
1545 mtx_unlock(&sscreen
->aux_context_lock
);
1547 si_resource_reference(&buf
, NULL
);
1551 /* Initialize the CMASK base register value. */
1552 tex
->cmask_base_address_reg
=
1553 (tex
->buffer
.gpu_address
+ tex
->cmask_offset
) >> 8;
1555 if (sscreen
->debug_flags
& DBG(VM
)) {
1556 fprintf(stderr
, "VM start=0x%"PRIX64
" end=0x%"PRIX64
" | Texture %ix%ix%i, %i levels, %i samples, %s\n",
1557 tex
->buffer
.gpu_address
,
1558 tex
->buffer
.gpu_address
+ tex
->buffer
.buf
->size
,
1559 base
->width0
, base
->height0
, util_num_layers(base
, 0), base
->last_level
+1,
1560 base
->nr_samples
? base
->nr_samples
: 1, util_format_short_name(base
->format
));
1563 if (sscreen
->debug_flags
& DBG(TEX
)) {
1565 struct u_log_context log
;
1566 u_log_context_init(&log
);
1567 si_print_texture_info(sscreen
, tex
, &log
);
1568 u_log_new_page_print(&log
, stdout
);
1570 u_log_context_destroy(&log
);
1577 if (sscreen
->info
.chip_class
>= GFX9
)
1578 free(surface
->u
.gfx9
.dcc_retile_map
);
1582 static enum radeon_surf_mode
1583 si_choose_tiling(struct si_screen
*sscreen
,
1584 const struct pipe_resource
*templ
, bool tc_compatible_htile
)
1586 const struct util_format_description
*desc
= util_format_description(templ
->format
);
1587 bool force_tiling
= templ
->flags
& SI_RESOURCE_FLAG_FORCE_MSAA_TILING
;
1588 bool is_depth_stencil
= util_format_is_depth_or_stencil(templ
->format
) &&
1589 !(templ
->flags
& SI_RESOURCE_FLAG_FLUSHED_DEPTH
);
1591 /* MSAA resources must be 2D tiled. */
1592 if (templ
->nr_samples
> 1)
1593 return RADEON_SURF_MODE_2D
;
1595 /* Transfer resources should be linear. */
1596 if (templ
->flags
& SI_RESOURCE_FLAG_TRANSFER
)
1597 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1599 /* Avoid Z/S decompress blits by forcing TC-compatible HTILE on GFX8,
1600 * which requires 2D tiling.
1602 if (sscreen
->info
.chip_class
== GFX8
&& tc_compatible_htile
)
1603 return RADEON_SURF_MODE_2D
;
1605 /* Handle common candidates for the linear mode.
1606 * Compressed textures and DB surfaces must always be tiled.
1608 if (!force_tiling
&&
1609 !is_depth_stencil
&&
1610 !util_format_is_compressed(templ
->format
)) {
1611 if (sscreen
->debug_flags
& DBG(NO_TILING
))
1612 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1614 /* Tiling doesn't work with the 422 (SUBSAMPLED) formats. */
1615 if (desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
)
1616 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1618 /* Cursors are linear on AMD GCN.
1619 * (XXX double-check, maybe also use RADEON_SURF_SCANOUT) */
1620 if (templ
->bind
& PIPE_BIND_CURSOR
)
1621 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1623 if (templ
->bind
& PIPE_BIND_LINEAR
)
1624 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1626 /* Textures with a very small height are recommended to be linear. */
1627 if (templ
->target
== PIPE_TEXTURE_1D
||
1628 templ
->target
== PIPE_TEXTURE_1D_ARRAY
||
1629 /* Only very thin and long 2D textures should benefit from
1630 * linear_aligned. */
1631 (templ
->width0
> 8 && templ
->height0
<= 2))
1632 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1634 /* Textures likely to be mapped often. */
1635 if (templ
->usage
== PIPE_USAGE_STAGING
||
1636 templ
->usage
== PIPE_USAGE_STREAM
)
1637 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1640 /* Make small textures 1D tiled. */
1641 if (templ
->width0
<= 16 || templ
->height0
<= 16 ||
1642 (sscreen
->debug_flags
& DBG(NO_2D_TILING
)))
1643 return RADEON_SURF_MODE_1D
;
1645 /* The allocator will switch to 1D if needed. */
1646 return RADEON_SURF_MODE_2D
;
1649 struct pipe_resource
*si_texture_create(struct pipe_screen
*screen
,
1650 const struct pipe_resource
*templ
)
1652 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
1653 bool is_zs
= util_format_is_depth_or_stencil(templ
->format
);
1655 if (templ
->nr_samples
>= 2) {
1656 /* This is hackish (overwriting the const pipe_resource template),
1657 * but should be harmless and state trackers can also see
1658 * the overriden number of samples in the created pipe_resource.
1660 if (is_zs
&& sscreen
->eqaa_force_z_samples
) {
1661 ((struct pipe_resource
*)templ
)->nr_samples
=
1662 ((struct pipe_resource
*)templ
)->nr_storage_samples
=
1663 sscreen
->eqaa_force_z_samples
;
1664 } else if (!is_zs
&& sscreen
->eqaa_force_color_samples
) {
1665 ((struct pipe_resource
*)templ
)->nr_samples
=
1666 sscreen
->eqaa_force_coverage_samples
;
1667 ((struct pipe_resource
*)templ
)->nr_storage_samples
=
1668 sscreen
->eqaa_force_color_samples
;
1672 struct radeon_surf surface
= {0};
1673 bool is_flushed_depth
= templ
->flags
& SI_RESOURCE_FLAG_FLUSHED_DEPTH
;
1674 bool tc_compatible_htile
=
1675 sscreen
->info
.chip_class
>= GFX8
&&
1676 /* There are issues with TC-compatible HTILE on Tonga (and
1677 * Iceland is the same design), and documented bug workarounds
1678 * don't help. For example, this fails:
1679 * piglit/bin/tex-miplevel-selection 'texture()' 2DShadow -auto
1681 sscreen
->info
.family
!= CHIP_TONGA
&&
1682 sscreen
->info
.family
!= CHIP_ICELAND
&&
1683 (templ
->flags
& PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY
) &&
1684 !(sscreen
->debug_flags
& DBG(NO_HYPERZ
)) &&
1685 !is_flushed_depth
&&
1686 templ
->nr_samples
<= 1 && /* TC-compat HTILE is less efficient with MSAA */
1690 r
= si_init_surface(sscreen
, &surface
, templ
,
1691 si_choose_tiling(sscreen
, templ
, tc_compatible_htile
),
1692 0, 0, false, false, is_flushed_depth
,
1693 tc_compatible_htile
);
1698 return (struct pipe_resource
*)
1699 si_texture_create_object(screen
, templ
, NULL
, &surface
);
1702 static struct pipe_resource
*si_texture_from_winsys_buffer(struct si_screen
*sscreen
,
1703 const struct pipe_resource
*templ
,
1704 struct pb_buffer
*buf
,
1710 enum radeon_surf_mode array_mode
;
1711 struct radeon_surf surface
= {};
1712 struct radeon_bo_metadata metadata
= {};
1713 struct si_texture
*tex
;
1718 sscreen
->ws
->buffer_get_metadata(buf
, &metadata
);
1719 si_get_display_metadata(sscreen
, &surface
, &metadata
,
1720 &array_mode
, &is_scanout
);
1723 * The bo metadata is unset for un-dedicated images. So we fall
1724 * back to linear. See answer to question 5 of the
1725 * VK_KHX_external_memory spec for some details.
1727 * It is possible that this case isn't going to work if the
1728 * surface pitch isn't correctly aligned by default.
1730 * In order to support it correctly we require multi-image
1731 * metadata to be syncrhonized between radv and radeonsi. The
1732 * semantics of associating multiple image metadata to a memory
1733 * object on the vulkan export side are not concretely defined
1736 * All the use cases we are aware of at the moment for memory
1737 * objects use dedicated allocations. So lets keep the initial
1738 * implementation simple.
1740 * A possible alternative is to attempt to reconstruct the
1741 * tiling information when the TexParameter TEXTURE_TILING_EXT
1744 array_mode
= RADEON_SURF_MODE_LINEAR_ALIGNED
;
1748 r
= si_init_surface(sscreen
, &surface
, templ
,
1749 array_mode
, stride
, offset
, true, is_scanout
,
1754 tex
= si_texture_create_object(&sscreen
->b
, templ
, buf
, &surface
);
1758 tex
->buffer
.b
.is_shared
= true;
1759 tex
->buffer
.external_usage
= usage
;
1761 if (!si_read_tex_bo_metadata(sscreen
, tex
, &metadata
)) {
1762 si_texture_reference(&tex
, NULL
);
1766 /* Displayable DCC requires an explicit flush. */
1768 !(usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
) &&
1769 si_has_displayable_dcc(tex
)) {
1770 /* TODO: do we need to decompress DCC? */
1771 if (si_texture_discard_dcc(sscreen
, tex
)) {
1772 /* Update BO metadata after disabling DCC. */
1773 si_set_tex_bo_metadata(sscreen
, tex
);
1777 assert(tex
->surface
.tile_swizzle
== 0);
1778 return &tex
->buffer
.b
.b
;
1781 static struct pipe_resource
*si_texture_from_handle(struct pipe_screen
*screen
,
1782 const struct pipe_resource
*templ
,
1783 struct winsys_handle
*whandle
,
1786 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
1787 struct pb_buffer
*buf
= NULL
;
1788 unsigned stride
= 0, offset
= 0;
1790 /* Support only 2D textures without mipmaps */
1791 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
&&
1792 templ
->target
!= PIPE_TEXTURE_2D_ARRAY
) ||
1793 templ
->last_level
!= 0)
1796 buf
= sscreen
->ws
->buffer_from_handle(sscreen
->ws
, whandle
,
1797 sscreen
->info
.max_alignment
,
1802 return si_texture_from_winsys_buffer(sscreen
, templ
, buf
, stride
,
1803 offset
, usage
, true);
1806 bool si_init_flushed_depth_texture(struct pipe_context
*ctx
,
1807 struct pipe_resource
*texture
)
1809 struct si_texture
*tex
= (struct si_texture
*)texture
;
1810 struct pipe_resource resource
;
1811 enum pipe_format pipe_format
= texture
->format
;
1813 assert(!tex
->flushed_depth_texture
);
1815 if (!tex
->can_sample_z
&& tex
->can_sample_s
) {
1816 switch (pipe_format
) {
1817 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1818 /* Save memory by not allocating the S plane. */
1819 pipe_format
= PIPE_FORMAT_Z32_FLOAT
;
1821 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1822 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1823 /* Save memory bandwidth by not copying the
1824 * stencil part during flush.
1826 * This potentially increases memory bandwidth
1827 * if an application uses both Z and S texturing
1828 * simultaneously (a flushed Z24S8 texture
1829 * would be stored compactly), but how often
1830 * does that really happen?
1832 pipe_format
= PIPE_FORMAT_Z24X8_UNORM
;
1836 } else if (!tex
->can_sample_s
&& tex
->can_sample_z
) {
1837 assert(util_format_has_stencil(util_format_description(pipe_format
)));
1839 /* DB->CB copies to an 8bpp surface don't work. */
1840 pipe_format
= PIPE_FORMAT_X24S8_UINT
;
1843 memset(&resource
, 0, sizeof(resource
));
1844 resource
.target
= texture
->target
;
1845 resource
.format
= pipe_format
;
1846 resource
.width0
= texture
->width0
;
1847 resource
.height0
= texture
->height0
;
1848 resource
.depth0
= texture
->depth0
;
1849 resource
.array_size
= texture
->array_size
;
1850 resource
.last_level
= texture
->last_level
;
1851 resource
.nr_samples
= texture
->nr_samples
;
1852 resource
.usage
= PIPE_USAGE_DEFAULT
;
1853 resource
.bind
= texture
->bind
& ~PIPE_BIND_DEPTH_STENCIL
;
1854 resource
.flags
= texture
->flags
| SI_RESOURCE_FLAG_FLUSHED_DEPTH
;
1856 tex
->flushed_depth_texture
= (struct si_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
1857 if (!tex
->flushed_depth_texture
) {
1858 PRINT_ERR("failed to create temporary texture to hold flushed depth\n");
1865 * Initialize the pipe_resource descriptor to be of the same size as the box,
1866 * which is supposed to hold a subregion of the texture "orig" at the given
1869 static void si_init_temp_resource_from_box(struct pipe_resource
*res
,
1870 struct pipe_resource
*orig
,
1871 const struct pipe_box
*box
,
1872 unsigned level
, unsigned flags
)
1874 memset(res
, 0, sizeof(*res
));
1875 res
->format
= orig
->format
;
1876 res
->width0
= box
->width
;
1877 res
->height0
= box
->height
;
1879 res
->array_size
= 1;
1880 res
->usage
= flags
& SI_RESOURCE_FLAG_TRANSFER
? PIPE_USAGE_STAGING
: PIPE_USAGE_DEFAULT
;
1883 if (flags
& SI_RESOURCE_FLAG_TRANSFER
&&
1884 util_format_is_compressed(orig
->format
)) {
1885 /* Transfer resources are allocated with linear tiling, which is
1886 * not supported for compressed formats.
1888 unsigned blocksize
=
1889 util_format_get_blocksize(orig
->format
);
1891 if (blocksize
== 8) {
1892 res
->format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1894 assert(blocksize
== 16);
1895 res
->format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1898 res
->width0
= util_format_get_nblocksx(orig
->format
, box
->width
);
1899 res
->height0
= util_format_get_nblocksy(orig
->format
, box
->height
);
1902 /* We must set the correct texture target and dimensions for a 3D box. */
1903 if (box
->depth
> 1 && util_max_layer(orig
, level
) > 0) {
1904 res
->target
= PIPE_TEXTURE_2D_ARRAY
;
1905 res
->array_size
= box
->depth
;
1907 res
->target
= PIPE_TEXTURE_2D
;
1911 static bool si_can_invalidate_texture(struct si_screen
*sscreen
,
1912 struct si_texture
*tex
,
1913 unsigned transfer_usage
,
1914 const struct pipe_box
*box
)
1916 return !tex
->buffer
.b
.is_shared
&&
1917 !(transfer_usage
& PIPE_TRANSFER_READ
) &&
1918 tex
->buffer
.b
.b
.last_level
== 0 &&
1919 util_texrange_covers_whole_level(&tex
->buffer
.b
.b
, 0,
1920 box
->x
, box
->y
, box
->z
,
1921 box
->width
, box
->height
,
1925 static void si_texture_invalidate_storage(struct si_context
*sctx
,
1926 struct si_texture
*tex
)
1928 struct si_screen
*sscreen
= sctx
->screen
;
1930 /* There is no point in discarding depth and tiled buffers. */
1931 assert(!tex
->is_depth
);
1932 assert(tex
->surface
.is_linear
);
1934 /* Reallocate the buffer in the same pipe_resource. */
1935 si_alloc_resource(sscreen
, &tex
->buffer
);
1937 /* Initialize the CMASK base address (needed even without CMASK). */
1938 tex
->cmask_base_address_reg
=
1939 (tex
->buffer
.gpu_address
+ tex
->cmask_offset
) >> 8;
1941 p_atomic_inc(&sscreen
->dirty_tex_counter
);
1943 sctx
->num_alloc_tex_transfer_bytes
+= tex
->size
;
1946 static void *si_texture_transfer_map(struct pipe_context
*ctx
,
1947 struct pipe_resource
*texture
,
1950 const struct pipe_box
*box
,
1951 struct pipe_transfer
**ptransfer
)
1953 struct si_context
*sctx
= (struct si_context
*)ctx
;
1954 struct si_texture
*tex
= (struct si_texture
*)texture
;
1955 struct si_transfer
*trans
;
1956 struct si_resource
*buf
;
1957 unsigned offset
= 0;
1959 bool use_staging_texture
= false;
1961 assert(!(texture
->flags
& SI_RESOURCE_FLAG_TRANSFER
));
1962 assert(box
->width
&& box
->height
&& box
->depth
);
1964 if (tex
->is_depth
) {
1965 /* Depth textures use staging unconditionally. */
1966 use_staging_texture
= true;
1968 /* Degrade the tile mode if we get too many transfers on APUs.
1969 * On dGPUs, the staging texture is always faster.
1970 * Only count uploads that are at least 4x4 pixels large.
1972 if (!sctx
->screen
->info
.has_dedicated_vram
&&
1974 box
->width
>= 4 && box
->height
>= 4 &&
1975 p_atomic_inc_return(&tex
->num_level0_transfers
) == 10) {
1976 bool can_invalidate
=
1977 si_can_invalidate_texture(sctx
->screen
, tex
,
1980 si_reallocate_texture_inplace(sctx
, tex
,
1985 /* Tiled textures need to be converted into a linear texture for CPU
1986 * access. The staging texture is always linear and is placed in GART.
1988 * Reading from VRAM or GTT WC is slow, always use the staging
1989 * texture in this case.
1991 * Use the staging texture for uploads if the underlying BO
1994 if (!tex
->surface
.is_linear
)
1995 use_staging_texture
= true;
1996 else if (usage
& PIPE_TRANSFER_READ
)
1997 use_staging_texture
=
1998 tex
->buffer
.domains
& RADEON_DOMAIN_VRAM
||
1999 tex
->buffer
.flags
& RADEON_FLAG_GTT_WC
;
2000 /* Write & linear only: */
2001 else if (si_rings_is_buffer_referenced(sctx
, tex
->buffer
.buf
,
2002 RADEON_USAGE_READWRITE
) ||
2003 !sctx
->ws
->buffer_wait(tex
->buffer
.buf
, 0,
2004 RADEON_USAGE_READWRITE
)) {
2006 if (si_can_invalidate_texture(sctx
->screen
, tex
,
2008 si_texture_invalidate_storage(sctx
, tex
);
2010 use_staging_texture
= true;
2014 trans
= CALLOC_STRUCT(si_transfer
);
2017 pipe_resource_reference(&trans
->b
.b
.resource
, texture
);
2018 trans
->b
.b
.level
= level
;
2019 trans
->b
.b
.usage
= usage
;
2020 trans
->b
.b
.box
= *box
;
2022 if (use_staging_texture
) {
2023 struct pipe_resource resource
;
2024 struct si_texture
*staging
;
2026 si_init_temp_resource_from_box(&resource
, texture
, box
, level
,
2027 SI_RESOURCE_FLAG_TRANSFER
);
2028 resource
.usage
= (usage
& PIPE_TRANSFER_READ
) ?
2029 PIPE_USAGE_STAGING
: PIPE_USAGE_STREAM
;
2031 /* Since depth-stencil textures don't support linear tiling,
2032 * blit from ZS to color and vice versa. u_blitter will do
2033 * the packing for these formats.
2036 resource
.format
= util_blitter_get_color_format_for_zs(resource
.format
);
2038 /* Create the temporary texture. */
2039 staging
= (struct si_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
2041 PRINT_ERR("failed to create temporary texture to hold untiled copy\n");
2044 trans
->staging
= &staging
->buffer
;
2046 /* Just get the strides. */
2047 si_texture_get_offset(sctx
->screen
, staging
, 0, NULL
,
2049 &trans
->b
.b
.layer_stride
);
2051 if (usage
& PIPE_TRANSFER_READ
)
2052 si_copy_to_staging_texture(ctx
, trans
);
2054 usage
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
2056 buf
= trans
->staging
;
2058 /* the resource is mapped directly */
2059 offset
= si_texture_get_offset(sctx
->screen
, tex
, level
, box
,
2061 &trans
->b
.b
.layer_stride
);
2065 /* Always unmap texture CPU mappings on 32-bit architectures, so that
2066 * we don't run out of the CPU address space.
2068 if (sizeof(void*) == 4)
2069 usage
|= RADEON_TRANSFER_TEMPORARY
;
2071 if (!(map
= si_buffer_map_sync_with_rings(sctx
, buf
, usage
)))
2074 *ptransfer
= &trans
->b
.b
;
2075 return map
+ offset
;
2078 si_resource_reference(&trans
->staging
, NULL
);
2079 pipe_resource_reference(&trans
->b
.b
.resource
, NULL
);
2084 static void si_texture_transfer_unmap(struct pipe_context
*ctx
,
2085 struct pipe_transfer
* transfer
)
2087 struct si_context
*sctx
= (struct si_context
*)ctx
;
2088 struct si_transfer
*stransfer
= (struct si_transfer
*)transfer
;
2089 struct pipe_resource
*texture
= transfer
->resource
;
2090 struct si_texture
*tex
= (struct si_texture
*)texture
;
2092 /* Always unmap texture CPU mappings on 32-bit architectures, so that
2093 * we don't run out of the CPU address space.
2095 if (sizeof(void*) == 4) {
2096 struct si_resource
*buf
=
2097 stransfer
->staging
? stransfer
->staging
: &tex
->buffer
;
2099 sctx
->ws
->buffer_unmap(buf
->buf
);
2102 if ((transfer
->usage
& PIPE_TRANSFER_WRITE
) && stransfer
->staging
)
2103 si_copy_from_staging_texture(ctx
, stransfer
);
2105 if (stransfer
->staging
) {
2106 sctx
->num_alloc_tex_transfer_bytes
+= stransfer
->staging
->buf
->size
;
2107 si_resource_reference(&stransfer
->staging
, NULL
);
2110 /* Heuristic for {upload, draw, upload, draw, ..}:
2112 * Flush the gfx IB if we've allocated too much texture storage.
2114 * The idea is that we don't want to build IBs that use too much
2115 * memory and put pressure on the kernel memory manager and we also
2116 * want to make temporary and invalidated buffers go idle ASAP to
2117 * decrease the total memory usage or make them reusable. The memory
2118 * usage will be slightly higher than given here because of the buffer
2119 * cache in the winsys.
2121 * The result is that the kernel memory manager is never a bottleneck.
2123 if (sctx
->num_alloc_tex_transfer_bytes
> sctx
->screen
->info
.gart_size
/ 4) {
2124 si_flush_gfx_cs(sctx
, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW
, NULL
);
2125 sctx
->num_alloc_tex_transfer_bytes
= 0;
2128 pipe_resource_reference(&transfer
->resource
, NULL
);
2132 static const struct u_resource_vtbl si_texture_vtbl
=
2134 NULL
, /* get_handle */
2135 si_texture_destroy
, /* resource_destroy */
2136 si_texture_transfer_map
, /* transfer_map */
2137 u_default_transfer_flush_region
, /* transfer_flush_region */
2138 si_texture_transfer_unmap
, /* transfer_unmap */
2141 /* Return if it's allowed to reinterpret one format as another with DCC enabled.
2143 bool vi_dcc_formats_compatible(struct si_screen
*sscreen
,
2144 enum pipe_format format1
,
2145 enum pipe_format format2
)
2147 const struct util_format_description
*desc1
, *desc2
;
2149 /* No format change - exit early. */
2150 if (format1
== format2
)
2153 format1
= si_simplify_cb_format(format1
);
2154 format2
= si_simplify_cb_format(format2
);
2156 /* Check again after format adjustments. */
2157 if (format1
== format2
)
2160 desc1
= util_format_description(format1
);
2161 desc2
= util_format_description(format2
);
2163 if (desc1
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
||
2164 desc2
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
2167 /* Float and non-float are totally incompatible. */
2168 if ((desc1
->channel
[0].type
== UTIL_FORMAT_TYPE_FLOAT
) !=
2169 (desc2
->channel
[0].type
== UTIL_FORMAT_TYPE_FLOAT
))
2172 /* Channel sizes must match across DCC formats.
2173 * Comparing just the first 2 channels should be enough.
2175 if (desc1
->channel
[0].size
!= desc2
->channel
[0].size
||
2176 (desc1
->nr_channels
>= 2 &&
2177 desc1
->channel
[1].size
!= desc2
->channel
[1].size
))
2180 /* Everything below is not needed if the driver never uses the DCC
2181 * clear code with the value of 1.
2184 /* If the clear values are all 1 or all 0, this constraint can be
2186 if (vi_alpha_is_on_msb(sscreen
, format1
) != vi_alpha_is_on_msb(sscreen
, format2
))
2189 /* Channel types must match if the clear value of 1 is used.
2190 * The type categories are only float, signed, unsigned.
2191 * NORM and INT are always compatible.
2193 if (desc1
->channel
[0].type
!= desc2
->channel
[0].type
||
2194 (desc1
->nr_channels
>= 2 &&
2195 desc1
->channel
[1].type
!= desc2
->channel
[1].type
))
2201 bool vi_dcc_formats_are_incompatible(struct pipe_resource
*tex
,
2203 enum pipe_format view_format
)
2205 struct si_texture
*stex
= (struct si_texture
*)tex
;
2207 return vi_dcc_enabled(stex
, level
) &&
2208 !vi_dcc_formats_compatible((struct si_screen
*)tex
->screen
,
2209 tex
->format
, view_format
);
2212 /* This can't be merged with the above function, because
2213 * vi_dcc_formats_compatible should be called only when DCC is enabled. */
2214 void vi_disable_dcc_if_incompatible_format(struct si_context
*sctx
,
2215 struct pipe_resource
*tex
,
2217 enum pipe_format view_format
)
2219 struct si_texture
*stex
= (struct si_texture
*)tex
;
2221 if (vi_dcc_formats_are_incompatible(tex
, level
, view_format
))
2222 if (!si_texture_disable_dcc(sctx
, stex
))
2223 si_decompress_dcc(sctx
, stex
);
2226 struct pipe_surface
*si_create_surface_custom(struct pipe_context
*pipe
,
2227 struct pipe_resource
*texture
,
2228 const struct pipe_surface
*templ
,
2229 unsigned width0
, unsigned height0
,
2230 unsigned width
, unsigned height
)
2232 struct si_surface
*surface
= CALLOC_STRUCT(si_surface
);
2237 assert(templ
->u
.tex
.first_layer
<= util_max_layer(texture
, templ
->u
.tex
.level
));
2238 assert(templ
->u
.tex
.last_layer
<= util_max_layer(texture
, templ
->u
.tex
.level
));
2240 pipe_reference_init(&surface
->base
.reference
, 1);
2241 pipe_resource_reference(&surface
->base
.texture
, texture
);
2242 surface
->base
.context
= pipe
;
2243 surface
->base
.format
= templ
->format
;
2244 surface
->base
.width
= width
;
2245 surface
->base
.height
= height
;
2246 surface
->base
.u
= templ
->u
;
2248 surface
->width0
= width0
;
2249 surface
->height0
= height0
;
2251 surface
->dcc_incompatible
=
2252 texture
->target
!= PIPE_BUFFER
&&
2253 vi_dcc_formats_are_incompatible(texture
, templ
->u
.tex
.level
,
2255 return &surface
->base
;
2258 static struct pipe_surface
*si_create_surface(struct pipe_context
*pipe
,
2259 struct pipe_resource
*tex
,
2260 const struct pipe_surface
*templ
)
2262 unsigned level
= templ
->u
.tex
.level
;
2263 unsigned width
= u_minify(tex
->width0
, level
);
2264 unsigned height
= u_minify(tex
->height0
, level
);
2265 unsigned width0
= tex
->width0
;
2266 unsigned height0
= tex
->height0
;
2268 if (tex
->target
!= PIPE_BUFFER
&& templ
->format
!= tex
->format
) {
2269 const struct util_format_description
*tex_desc
2270 = util_format_description(tex
->format
);
2271 const struct util_format_description
*templ_desc
2272 = util_format_description(templ
->format
);
2274 assert(tex_desc
->block
.bits
== templ_desc
->block
.bits
);
2276 /* Adjust size of surface if and only if the block width or
2277 * height is changed. */
2278 if (tex_desc
->block
.width
!= templ_desc
->block
.width
||
2279 tex_desc
->block
.height
!= templ_desc
->block
.height
) {
2280 unsigned nblks_x
= util_format_get_nblocksx(tex
->format
, width
);
2281 unsigned nblks_y
= util_format_get_nblocksy(tex
->format
, height
);
2283 width
= nblks_x
* templ_desc
->block
.width
;
2284 height
= nblks_y
* templ_desc
->block
.height
;
2286 width0
= util_format_get_nblocksx(tex
->format
, width0
);
2287 height0
= util_format_get_nblocksy(tex
->format
, height0
);
2291 return si_create_surface_custom(pipe
, tex
, templ
,
2296 static void si_surface_destroy(struct pipe_context
*pipe
,
2297 struct pipe_surface
*surface
)
2299 pipe_resource_reference(&surface
->texture
, NULL
);
2303 unsigned si_translate_colorswap(enum pipe_format format
, bool do_endian_swap
)
2305 const struct util_format_description
*desc
= util_format_description(format
);
2307 #define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == PIPE_SWIZZLE_##swz)
2309 if (format
== PIPE_FORMAT_R11G11B10_FLOAT
) /* isn't plain */
2310 return V_028C70_SWAP_STD
;
2312 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
2315 switch (desc
->nr_channels
) {
2317 if (HAS_SWIZZLE(0,X
))
2318 return V_028C70_SWAP_STD
; /* X___ */
2319 else if (HAS_SWIZZLE(3,X
))
2320 return V_028C70_SWAP_ALT_REV
; /* ___X */
2323 if ((HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(1,Y
)) ||
2324 (HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(1,NONE
)) ||
2325 (HAS_SWIZZLE(0,NONE
) && HAS_SWIZZLE(1,Y
)))
2326 return V_028C70_SWAP_STD
; /* XY__ */
2327 else if ((HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(1,X
)) ||
2328 (HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(1,NONE
)) ||
2329 (HAS_SWIZZLE(0,NONE
) && HAS_SWIZZLE(1,X
)))
2331 return (do_endian_swap
? V_028C70_SWAP_STD
: V_028C70_SWAP_STD_REV
);
2332 else if (HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(3,Y
))
2333 return V_028C70_SWAP_ALT
; /* X__Y */
2334 else if (HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(3,X
))
2335 return V_028C70_SWAP_ALT_REV
; /* Y__X */
2338 if (HAS_SWIZZLE(0,X
))
2339 return (do_endian_swap
? V_028C70_SWAP_STD_REV
: V_028C70_SWAP_STD
);
2340 else if (HAS_SWIZZLE(0,Z
))
2341 return V_028C70_SWAP_STD_REV
; /* ZYX */
2344 /* check the middle channels, the 1st and 4th channel can be NONE */
2345 if (HAS_SWIZZLE(1,Y
) && HAS_SWIZZLE(2,Z
)) {
2346 return V_028C70_SWAP_STD
; /* XYZW */
2347 } else if (HAS_SWIZZLE(1,Z
) && HAS_SWIZZLE(2,Y
)) {
2348 return V_028C70_SWAP_STD_REV
; /* WZYX */
2349 } else if (HAS_SWIZZLE(1,Y
) && HAS_SWIZZLE(2,X
)) {
2350 return V_028C70_SWAP_ALT
; /* ZYXW */
2351 } else if (HAS_SWIZZLE(1,Z
) && HAS_SWIZZLE(2,W
)) {
2354 return V_028C70_SWAP_ALT_REV
;
2356 return (do_endian_swap
? V_028C70_SWAP_ALT
: V_028C70_SWAP_ALT_REV
);
2363 /* PIPELINE_STAT-BASED DCC ENABLEMENT FOR DISPLAYABLE SURFACES */
2365 static void vi_dcc_clean_up_context_slot(struct si_context
*sctx
,
2370 if (sctx
->dcc_stats
[slot
].query_active
)
2371 vi_separate_dcc_stop_query(sctx
,
2372 sctx
->dcc_stats
[slot
].tex
);
2374 for (i
= 0; i
< ARRAY_SIZE(sctx
->dcc_stats
[slot
].ps_stats
); i
++)
2375 if (sctx
->dcc_stats
[slot
].ps_stats
[i
]) {
2376 sctx
->b
.destroy_query(&sctx
->b
,
2377 sctx
->dcc_stats
[slot
].ps_stats
[i
]);
2378 sctx
->dcc_stats
[slot
].ps_stats
[i
] = NULL
;
2381 si_texture_reference(&sctx
->dcc_stats
[slot
].tex
, NULL
);
2385 * Return the per-context slot where DCC statistics queries for the texture live.
2387 static unsigned vi_get_context_dcc_stats_index(struct si_context
*sctx
,
2388 struct si_texture
*tex
)
2390 int i
, empty_slot
= -1;
2392 /* Remove zombie textures (textures kept alive by this array only). */
2393 for (i
= 0; i
< ARRAY_SIZE(sctx
->dcc_stats
); i
++)
2394 if (sctx
->dcc_stats
[i
].tex
&&
2395 sctx
->dcc_stats
[i
].tex
->buffer
.b
.b
.reference
.count
== 1)
2396 vi_dcc_clean_up_context_slot(sctx
, i
);
2398 /* Find the texture. */
2399 for (i
= 0; i
< ARRAY_SIZE(sctx
->dcc_stats
); i
++) {
2400 /* Return if found. */
2401 if (sctx
->dcc_stats
[i
].tex
== tex
) {
2402 sctx
->dcc_stats
[i
].last_use_timestamp
= os_time_get();
2406 /* Record the first seen empty slot. */
2407 if (empty_slot
== -1 && !sctx
->dcc_stats
[i
].tex
)
2411 /* Not found. Remove the oldest member to make space in the array. */
2412 if (empty_slot
== -1) {
2413 int oldest_slot
= 0;
2415 /* Find the oldest slot. */
2416 for (i
= 1; i
< ARRAY_SIZE(sctx
->dcc_stats
); i
++)
2417 if (sctx
->dcc_stats
[oldest_slot
].last_use_timestamp
>
2418 sctx
->dcc_stats
[i
].last_use_timestamp
)
2421 /* Clean up the oldest slot. */
2422 vi_dcc_clean_up_context_slot(sctx
, oldest_slot
);
2423 empty_slot
= oldest_slot
;
2426 /* Add the texture to the new slot. */
2427 si_texture_reference(&sctx
->dcc_stats
[empty_slot
].tex
, tex
);
2428 sctx
->dcc_stats
[empty_slot
].last_use_timestamp
= os_time_get();
2432 static struct pipe_query
*
2433 vi_create_resuming_pipestats_query(struct si_context
*sctx
)
2435 struct si_query_hw
*query
= (struct si_query_hw
*)
2436 sctx
->b
.create_query(&sctx
->b
, PIPE_QUERY_PIPELINE_STATISTICS
, 0);
2438 query
->flags
|= SI_QUERY_HW_FLAG_BEGIN_RESUMES
;
2439 return (struct pipe_query
*)query
;
2443 * Called when binding a color buffer.
2445 void vi_separate_dcc_start_query(struct si_context
*sctx
,
2446 struct si_texture
*tex
)
2448 unsigned i
= vi_get_context_dcc_stats_index(sctx
, tex
);
2450 assert(!sctx
->dcc_stats
[i
].query_active
);
2452 if (!sctx
->dcc_stats
[i
].ps_stats
[0])
2453 sctx
->dcc_stats
[i
].ps_stats
[0] = vi_create_resuming_pipestats_query(sctx
);
2455 /* begin or resume the query */
2456 sctx
->b
.begin_query(&sctx
->b
, sctx
->dcc_stats
[i
].ps_stats
[0]);
2457 sctx
->dcc_stats
[i
].query_active
= true;
2461 * Called when unbinding a color buffer.
2463 void vi_separate_dcc_stop_query(struct si_context
*sctx
,
2464 struct si_texture
*tex
)
2466 unsigned i
= vi_get_context_dcc_stats_index(sctx
, tex
);
2468 assert(sctx
->dcc_stats
[i
].query_active
);
2469 assert(sctx
->dcc_stats
[i
].ps_stats
[0]);
2471 /* pause or end the query */
2472 sctx
->b
.end_query(&sctx
->b
, sctx
->dcc_stats
[i
].ps_stats
[0]);
2473 sctx
->dcc_stats
[i
].query_active
= false;
2476 static bool vi_should_enable_separate_dcc(struct si_texture
*tex
)
2478 /* The minimum number of fullscreen draws per frame that is required
2480 return tex
->ps_draw_ratio
+ tex
->num_slow_clears
>= 5;
2483 /* Called by fast clear. */
2484 void vi_separate_dcc_try_enable(struct si_context
*sctx
,
2485 struct si_texture
*tex
)
2487 /* The intent is to use this with shared displayable back buffers,
2488 * but it's not strictly limited only to them.
2490 if (!tex
->buffer
.b
.is_shared
||
2491 !(tex
->buffer
.external_usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
) ||
2492 tex
->buffer
.b
.b
.target
!= PIPE_TEXTURE_2D
||
2493 tex
->buffer
.b
.b
.last_level
> 0 ||
2494 !tex
->surface
.dcc_size
||
2495 sctx
->screen
->debug_flags
& DBG(NO_DCC
) ||
2496 sctx
->screen
->debug_flags
& DBG(NO_DCC_FB
))
2499 assert(sctx
->chip_class
>= GFX8
);
2501 if (tex
->dcc_offset
)
2502 return; /* already enabled */
2504 /* Enable the DCC stat gathering. */
2505 if (!tex
->dcc_gather_statistics
) {
2506 tex
->dcc_gather_statistics
= true;
2507 vi_separate_dcc_start_query(sctx
, tex
);
2510 if (!vi_should_enable_separate_dcc(tex
))
2511 return; /* stats show that DCC decompression is too expensive */
2513 assert(tex
->surface
.num_dcc_levels
);
2514 assert(!tex
->dcc_separate_buffer
);
2516 si_texture_discard_cmask(sctx
->screen
, tex
);
2518 /* Get a DCC buffer. */
2519 if (tex
->last_dcc_separate_buffer
) {
2520 assert(tex
->dcc_gather_statistics
);
2521 assert(!tex
->dcc_separate_buffer
);
2522 tex
->dcc_separate_buffer
= tex
->last_dcc_separate_buffer
;
2523 tex
->last_dcc_separate_buffer
= NULL
;
2525 tex
->dcc_separate_buffer
=
2526 si_aligned_buffer_create(sctx
->b
.screen
,
2527 SI_RESOURCE_FLAG_UNMAPPABLE
,
2529 tex
->surface
.dcc_size
,
2530 tex
->surface
.dcc_alignment
);
2531 if (!tex
->dcc_separate_buffer
)
2535 /* dcc_offset is the absolute GPUVM address. */
2536 tex
->dcc_offset
= tex
->dcc_separate_buffer
->gpu_address
;
2538 /* no need to flag anything since this is called by fast clear that
2539 * flags framebuffer state
2544 * Called by pipe_context::flush_resource, the place where DCC decompression
2547 void vi_separate_dcc_process_and_reset_stats(struct pipe_context
*ctx
,
2548 struct si_texture
*tex
)
2550 struct si_context
*sctx
= (struct si_context
*)ctx
;
2551 struct pipe_query
*tmp
;
2552 unsigned i
= vi_get_context_dcc_stats_index(sctx
, tex
);
2553 bool query_active
= sctx
->dcc_stats
[i
].query_active
;
2554 bool disable
= false;
2556 if (sctx
->dcc_stats
[i
].ps_stats
[2]) {
2557 union pipe_query_result result
;
2559 /* Read the results. */
2560 struct pipe_query
*query
= sctx
->dcc_stats
[i
].ps_stats
[2];
2561 ctx
->get_query_result(ctx
, query
,
2563 si_query_buffer_reset(sctx
, &((struct si_query_hw
*)query
)->buffer
);
2565 /* Compute the approximate number of fullscreen draws. */
2566 tex
->ps_draw_ratio
=
2567 result
.pipeline_statistics
.ps_invocations
/
2568 (tex
->buffer
.b
.b
.width0
* tex
->buffer
.b
.b
.height0
);
2569 sctx
->last_tex_ps_draw_ratio
= tex
->ps_draw_ratio
;
2571 disable
= tex
->dcc_separate_buffer
&&
2572 !vi_should_enable_separate_dcc(tex
);
2575 tex
->num_slow_clears
= 0;
2577 /* stop the statistics query for ps_stats[0] */
2579 vi_separate_dcc_stop_query(sctx
, tex
);
2581 /* Move the queries in the queue by one. */
2582 tmp
= sctx
->dcc_stats
[i
].ps_stats
[2];
2583 sctx
->dcc_stats
[i
].ps_stats
[2] = sctx
->dcc_stats
[i
].ps_stats
[1];
2584 sctx
->dcc_stats
[i
].ps_stats
[1] = sctx
->dcc_stats
[i
].ps_stats
[0];
2585 sctx
->dcc_stats
[i
].ps_stats
[0] = tmp
;
2587 /* create and start a new query as ps_stats[0] */
2589 vi_separate_dcc_start_query(sctx
, tex
);
2592 assert(!tex
->last_dcc_separate_buffer
);
2593 tex
->last_dcc_separate_buffer
= tex
->dcc_separate_buffer
;
2594 tex
->dcc_separate_buffer
= NULL
;
2595 tex
->dcc_offset
= 0;
2596 /* no need to flag anything since this is called after
2597 * decompression that re-sets framebuffer state
2602 static struct pipe_memory_object
*
2603 si_memobj_from_handle(struct pipe_screen
*screen
,
2604 struct winsys_handle
*whandle
,
2607 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
2608 struct si_memory_object
*memobj
= CALLOC_STRUCT(si_memory_object
);
2609 struct pb_buffer
*buf
= NULL
;
2610 uint32_t stride
, offset
;
2615 buf
= sscreen
->ws
->buffer_from_handle(sscreen
->ws
, whandle
,
2616 sscreen
->info
.max_alignment
,
2623 memobj
->b
.dedicated
= dedicated
;
2625 memobj
->stride
= stride
;
2627 return (struct pipe_memory_object
*)memobj
;
2632 si_memobj_destroy(struct pipe_screen
*screen
,
2633 struct pipe_memory_object
*_memobj
)
2635 struct si_memory_object
*memobj
= (struct si_memory_object
*)_memobj
;
2637 pb_reference(&memobj
->buf
, NULL
);
2641 static struct pipe_resource
*
2642 si_texture_from_memobj(struct pipe_screen
*screen
,
2643 const struct pipe_resource
*templ
,
2644 struct pipe_memory_object
*_memobj
,
2647 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
2648 struct si_memory_object
*memobj
= (struct si_memory_object
*)_memobj
;
2649 struct pipe_resource
*tex
=
2650 si_texture_from_winsys_buffer(sscreen
, templ
, memobj
->buf
,
2651 memobj
->stride
, offset
,
2652 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE
|
2653 PIPE_HANDLE_USAGE_SHADER_WRITE
,
2654 memobj
->b
.dedicated
);
2658 /* si_texture_from_winsys_buffer doesn't increment refcount of
2659 * memobj->buf, so increment it here.
2661 struct pb_buffer
*buf
= NULL
;
2662 pb_reference(&buf
, memobj
->buf
);
2666 static bool si_check_resource_capability(struct pipe_screen
*screen
,
2667 struct pipe_resource
*resource
,
2670 struct si_texture
*tex
= (struct si_texture
*)resource
;
2672 /* Buffers only support the linear flag. */
2673 if (resource
->target
== PIPE_BUFFER
)
2674 return (bind
& ~PIPE_BIND_LINEAR
) == 0;
2676 if (bind
& PIPE_BIND_LINEAR
&& !tex
->surface
.is_linear
)
2679 if (bind
& PIPE_BIND_SCANOUT
&& !tex
->surface
.is_displayable
)
2682 /* TODO: PIPE_BIND_CURSOR - do we care? */
2686 void si_init_screen_texture_functions(struct si_screen
*sscreen
)
2688 sscreen
->b
.resource_from_handle
= si_texture_from_handle
;
2689 sscreen
->b
.resource_get_handle
= si_texture_get_handle
;
2690 sscreen
->b
.resource_get_info
= si_texture_get_info
;
2691 sscreen
->b
.resource_from_memobj
= si_texture_from_memobj
;
2692 sscreen
->b
.memobj_create_from_handle
= si_memobj_from_handle
;
2693 sscreen
->b
.memobj_destroy
= si_memobj_destroy
;
2694 sscreen
->b
.check_resource_capability
= si_check_resource_capability
;
2697 void si_init_context_texture_functions(struct si_context
*sctx
)
2699 sctx
->b
.create_surface
= si_create_surface
;
2700 sctx
->b
.surface_destroy
= si_surface_destroy
;