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"
41 #include "drm-uapi/drm_fourcc.h"
43 static enum radeon_surf_mode
44 si_choose_tiling(struct si_screen
*sscreen
,
45 const struct pipe_resource
*templ
, bool tc_compatible_htile
);
48 bool si_prepare_for_dma_blit(struct si_context
*sctx
,
49 struct si_texture
*dst
,
50 unsigned dst_level
, unsigned dstx
,
51 unsigned dsty
, unsigned dstz
,
52 struct si_texture
*src
,
54 const struct pipe_box
*src_box
)
59 if (dst
->surface
.bpe
!= src
->surface
.bpe
)
62 /* MSAA: Blits don't exist in the real world. */
63 if (src
->buffer
.b
.b
.nr_samples
> 1 ||
64 dst
->buffer
.b
.b
.nr_samples
> 1)
67 /* Depth-stencil surfaces:
68 * When dst is linear, the DB->CB copy preserves HTILE.
69 * When dst is tiled, the 3D path must be used to update HTILE.
71 if (src
->is_depth
|| dst
->is_depth
)
75 * src: Use the 3D path. DCC decompression is expensive.
76 * dst: Use the 3D path to compress the pixels with DCC.
78 if (vi_dcc_enabled(src
, src_level
) ||
79 vi_dcc_enabled(dst
, dst_level
))
83 * src: Both texture and SDMA paths need decompression. Use SDMA.
84 * dst: If overwriting the whole texture, discard CMASK and use
85 * SDMA. Otherwise, use the 3D path.
87 if (dst
->cmask_buffer
&& dst
->dirty_level_mask
& (1 << dst_level
)) {
88 /* The CMASK clear is only enabled for the first level. */
89 assert(dst_level
== 0);
90 if (!util_texrange_covers_whole_level(&dst
->buffer
.b
.b
, dst_level
,
91 dstx
, dsty
, dstz
, src_box
->width
,
92 src_box
->height
, src_box
->depth
))
95 si_texture_discard_cmask(sctx
->screen
, dst
);
98 /* All requirements are met. Prepare textures for SDMA. */
99 if (src
->cmask_buffer
&& src
->dirty_level_mask
& (1 << src_level
))
100 sctx
->b
.flush_resource(&sctx
->b
, &src
->buffer
.b
.b
);
102 assert(!(src
->dirty_level_mask
& (1 << src_level
)));
103 assert(!(dst
->dirty_level_mask
& (1 << dst_level
)));
108 /* Same as resource_copy_region, except that both upsampling and downsampling are allowed. */
109 static void si_copy_region_with_blit(struct pipe_context
*pipe
,
110 struct pipe_resource
*dst
,
112 unsigned dstx
, unsigned dsty
, unsigned dstz
,
113 struct pipe_resource
*src
,
115 const struct pipe_box
*src_box
)
117 struct pipe_blit_info blit
;
119 memset(&blit
, 0, sizeof(blit
));
120 blit
.src
.resource
= src
;
121 blit
.src
.format
= src
->format
;
122 blit
.src
.level
= src_level
;
123 blit
.src
.box
= *src_box
;
124 blit
.dst
.resource
= dst
;
125 blit
.dst
.format
= dst
->format
;
126 blit
.dst
.level
= dst_level
;
127 blit
.dst
.box
.x
= dstx
;
128 blit
.dst
.box
.y
= dsty
;
129 blit
.dst
.box
.z
= dstz
;
130 blit
.dst
.box
.width
= src_box
->width
;
131 blit
.dst
.box
.height
= src_box
->height
;
132 blit
.dst
.box
.depth
= src_box
->depth
;
133 blit
.mask
= util_format_get_mask(dst
->format
);
134 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
137 pipe
->blit(pipe
, &blit
);
141 /* Copy from a full GPU texture to a transfer's staging one. */
142 static void si_copy_to_staging_texture(struct pipe_context
*ctx
, struct si_transfer
*stransfer
)
144 struct si_context
*sctx
= (struct si_context
*)ctx
;
145 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)stransfer
;
146 struct pipe_resource
*dst
= &stransfer
->staging
->b
.b
;
147 struct pipe_resource
*src
= transfer
->resource
;
149 if (src
->nr_samples
> 1 || ((struct si_texture
*)src
)->is_depth
) {
150 si_copy_region_with_blit(ctx
, dst
, 0, 0, 0, 0,
151 src
, transfer
->level
, &transfer
->box
);
155 sctx
->dma_copy(ctx
, dst
, 0, 0, 0, 0, src
, transfer
->level
,
159 /* Copy from a transfer's staging texture to a full GPU one. */
160 static void si_copy_from_staging_texture(struct pipe_context
*ctx
, struct si_transfer
*stransfer
)
162 struct si_context
*sctx
= (struct si_context
*)ctx
;
163 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)stransfer
;
164 struct pipe_resource
*dst
= transfer
->resource
;
165 struct pipe_resource
*src
= &stransfer
->staging
->b
.b
;
166 struct pipe_box sbox
;
168 u_box_3d(0, 0, 0, transfer
->box
.width
, transfer
->box
.height
, transfer
->box
.depth
, &sbox
);
170 if (dst
->nr_samples
> 1 || ((struct si_texture
*)dst
)->is_depth
) {
171 si_copy_region_with_blit(ctx
, dst
, transfer
->level
,
172 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
177 if (util_format_is_compressed(dst
->format
)) {
178 sbox
.width
= util_format_get_nblocksx(dst
->format
, sbox
.width
);
179 sbox
.height
= util_format_get_nblocksx(dst
->format
, sbox
.height
);
182 sctx
->dma_copy(ctx
, dst
, transfer
->level
,
183 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
187 static unsigned si_texture_get_offset(struct si_screen
*sscreen
,
188 struct si_texture
*tex
, unsigned level
,
189 const struct pipe_box
*box
,
191 unsigned *layer_stride
)
193 if (sscreen
->info
.chip_class
>= GFX9
) {
194 *stride
= tex
->surface
.u
.gfx9
.surf_pitch
* tex
->surface
.bpe
;
195 *layer_stride
= tex
->surface
.u
.gfx9
.surf_slice_size
;
200 /* Each texture is an array of slices. Each slice is an array
201 * of mipmap levels. */
202 return box
->z
* tex
->surface
.u
.gfx9
.surf_slice_size
+
203 tex
->surface
.u
.gfx9
.offset
[level
] +
204 (box
->y
/ tex
->surface
.blk_h
*
205 tex
->surface
.u
.gfx9
.surf_pitch
+
206 box
->x
/ tex
->surface
.blk_w
) * tex
->surface
.bpe
;
208 *stride
= tex
->surface
.u
.legacy
.level
[level
].nblk_x
*
210 assert((uint64_t)tex
->surface
.u
.legacy
.level
[level
].slice_size_dw
* 4 <= UINT_MAX
);
211 *layer_stride
= (uint64_t)tex
->surface
.u
.legacy
.level
[level
].slice_size_dw
* 4;
214 return tex
->surface
.u
.legacy
.level
[level
].offset
;
216 /* Each texture is an array of mipmap levels. Each level is
217 * an array of slices. */
218 return tex
->surface
.u
.legacy
.level
[level
].offset
+
219 box
->z
* (uint64_t)tex
->surface
.u
.legacy
.level
[level
].slice_size_dw
* 4 +
220 (box
->y
/ tex
->surface
.blk_h
*
221 tex
->surface
.u
.legacy
.level
[level
].nblk_x
+
222 box
->x
/ tex
->surface
.blk_w
) * tex
->surface
.bpe
;
226 static int si_init_surface(struct si_screen
*sscreen
,
227 struct radeon_surf
*surface
,
228 const struct pipe_resource
*ptex
,
229 enum radeon_surf_mode array_mode
,
230 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 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 (sscreen
->debug_flags
& DBG(NO_HYPERZ
)) {
257 flags
|= RADEON_SURF_NO_HTILE
;
258 } else if (tc_compatible_htile
&&
259 (sscreen
->info
.chip_class
>= GFX9
||
260 array_mode
== RADEON_SURF_MODE_2D
)) {
261 /* TC-compatible HTILE only supports Z32_FLOAT.
262 * GFX9 also supports Z16_UNORM.
263 * On GFX8, promote Z16 to Z32. DB->CB copies will convert
264 * the format for transfers.
266 if (sscreen
->info
.chip_class
== GFX8
)
269 flags
|= RADEON_SURF_TC_COMPATIBLE_HTILE
;
273 flags
|= RADEON_SURF_SBUFFER
;
276 if (sscreen
->info
.chip_class
>= GFX8
&&
277 (ptex
->flags
& SI_RESOURCE_FLAG_DISABLE_DCC
||
278 ptex
->format
== PIPE_FORMAT_R9G9B9E5_FLOAT
||
279 (ptex
->nr_samples
>= 2 && !sscreen
->dcc_msaa_allowed
)))
280 flags
|= RADEON_SURF_DISABLE_DCC
;
282 /* Stoney: 128bpp MSAA textures randomly fail piglit tests with DCC. */
283 if (sscreen
->info
.family
== CHIP_STONEY
&&
284 bpe
== 16 && ptex
->nr_samples
>= 2)
285 flags
|= RADEON_SURF_DISABLE_DCC
;
287 /* GFX8: DCC clear for 4x and 8x MSAA array textures unimplemented. */
288 if (sscreen
->info
.chip_class
== GFX8
&&
289 ptex
->nr_storage_samples
>= 4 &&
290 ptex
->array_size
> 1)
291 flags
|= RADEON_SURF_DISABLE_DCC
;
293 /* GFX9: DCC clear for 4x and 8x MSAA textures unimplemented. */
294 if (sscreen
->info
.chip_class
== GFX9
&&
295 ptex
->nr_storage_samples
>= 4)
296 flags
|= RADEON_SURF_DISABLE_DCC
;
298 /* TODO: GFX10: DCC causes corruption with MSAA. */
299 if (sscreen
->info
.chip_class
>= GFX10
&&
300 ptex
->nr_storage_samples
>= 2)
301 flags
|= RADEON_SURF_DISABLE_DCC
;
303 /* Shared textures must always set up DCC.
304 * If it's not present, it will be disabled by
305 * si_get_opaque_metadata later.
307 if (!is_imported
&& (sscreen
->debug_flags
& DBG(NO_DCC
)))
308 flags
|= RADEON_SURF_DISABLE_DCC
;
310 if (ptex
->bind
& PIPE_BIND_SCANOUT
|| is_scanout
) {
311 /* This should catch bugs in gallium users setting incorrect flags. */
312 assert(ptex
->nr_samples
<= 1 &&
313 ptex
->array_size
== 1 &&
315 ptex
->last_level
== 0 &&
316 !(flags
& RADEON_SURF_Z_OR_SBUFFER
));
318 flags
|= RADEON_SURF_SCANOUT
;
321 if (ptex
->bind
& PIPE_BIND_SHARED
)
322 flags
|= RADEON_SURF_SHAREABLE
;
324 flags
|= RADEON_SURF_IMPORTED
| RADEON_SURF_SHAREABLE
;
325 if (!(ptex
->flags
& SI_RESOURCE_FLAG_FORCE_MSAA_TILING
))
326 flags
|= RADEON_SURF_OPTIMIZE_FOR_SPACE
;
327 if (sscreen
->debug_flags
& DBG(NO_FMASK
))
328 flags
|= RADEON_SURF_NO_FMASK
;
330 if (sscreen
->info
.chip_class
>= GFX10
&&
331 (ptex
->flags
& SI_RESOURCE_FLAG_FORCE_MSAA_TILING
)) {
332 flags
|= RADEON_SURF_FORCE_SWIZZLE_MODE
;
333 surface
->u
.gfx9
.surf
.swizzle_mode
= ADDR_SW_64KB_R_X
;
336 r
= sscreen
->ws
->surface_init(sscreen
->ws
, ptex
, flags
, bpe
,
337 array_mode
, surface
);
342 unsigned pitch
= pitch_in_bytes_override
/ bpe
;
344 if (sscreen
->info
.chip_class
>= GFX9
) {
346 surface
->u
.gfx9
.surf_pitch
= pitch
;
347 if (ptex
->last_level
== 0)
348 surface
->u
.gfx9
.surf
.epitch
= pitch
- 1;
349 surface
->u
.gfx9
.surf_slice_size
=
350 (uint64_t)pitch
* surface
->u
.gfx9
.surf_height
* bpe
;
354 surface
->u
.legacy
.level
[0].nblk_x
= pitch
;
355 surface
->u
.legacy
.level
[0].slice_size_dw
=
356 ((uint64_t)pitch
* surface
->u
.legacy
.level
[0].nblk_y
* bpe
) / 4;
362 static void si_get_display_metadata(struct si_screen
*sscreen
,
363 struct radeon_surf
*surf
,
364 struct radeon_bo_metadata
*metadata
,
365 enum radeon_surf_mode
*array_mode
,
368 if (sscreen
->info
.chip_class
>= GFX9
) {
369 if (metadata
->u
.gfx9
.swizzle_mode
> 0)
370 *array_mode
= RADEON_SURF_MODE_2D
;
372 *array_mode
= RADEON_SURF_MODE_LINEAR_ALIGNED
;
374 *is_scanout
= metadata
->u
.gfx9
.swizzle_mode
== 0 ||
375 metadata
->u
.gfx9
.swizzle_mode
% 4 == 2;
377 surf
->u
.gfx9
.surf
.swizzle_mode
= metadata
->u
.gfx9
.swizzle_mode
;
379 if (metadata
->u
.gfx9
.dcc_offset_256B
) {
380 surf
->u
.gfx9
.display_dcc_pitch_max
= metadata
->u
.gfx9
.dcc_pitch_max
;
381 assert(metadata
->u
.gfx9
.dcc_independent_64B
== 1);
384 surf
->u
.legacy
.pipe_config
= metadata
->u
.legacy
.pipe_config
;
385 surf
->u
.legacy
.bankw
= metadata
->u
.legacy
.bankw
;
386 surf
->u
.legacy
.bankh
= metadata
->u
.legacy
.bankh
;
387 surf
->u
.legacy
.tile_split
= metadata
->u
.legacy
.tile_split
;
388 surf
->u
.legacy
.mtilea
= metadata
->u
.legacy
.mtilea
;
389 surf
->u
.legacy
.num_banks
= metadata
->u
.legacy
.num_banks
;
391 if (metadata
->u
.legacy
.macrotile
== RADEON_LAYOUT_TILED
)
392 *array_mode
= RADEON_SURF_MODE_2D
;
393 else if (metadata
->u
.legacy
.microtile
== RADEON_LAYOUT_TILED
)
394 *array_mode
= RADEON_SURF_MODE_1D
;
396 *array_mode
= RADEON_SURF_MODE_LINEAR_ALIGNED
;
398 *is_scanout
= metadata
->u
.legacy
.scanout
;
402 void si_eliminate_fast_color_clear(struct si_context
*sctx
,
403 struct si_texture
*tex
)
405 struct si_screen
*sscreen
= sctx
->screen
;
406 struct pipe_context
*ctx
= &sctx
->b
;
408 if (ctx
== sscreen
->aux_context
)
409 simple_mtx_lock(&sscreen
->aux_context_lock
);
411 unsigned n
= sctx
->num_decompress_calls
;
412 ctx
->flush_resource(ctx
, &tex
->buffer
.b
.b
);
414 /* Flush only if any fast clear elimination took place. */
415 if (n
!= sctx
->num_decompress_calls
)
416 ctx
->flush(ctx
, NULL
, 0);
418 if (ctx
== sscreen
->aux_context
)
419 simple_mtx_unlock(&sscreen
->aux_context_lock
);
422 void si_texture_discard_cmask(struct si_screen
*sscreen
,
423 struct si_texture
*tex
)
425 if (!tex
->cmask_buffer
)
428 assert(tex
->buffer
.b
.b
.nr_samples
<= 1);
431 tex
->cmask_base_address_reg
= tex
->buffer
.gpu_address
>> 8;
432 tex
->dirty_level_mask
= 0;
434 tex
->cb_color_info
&= ~S_028C70_FAST_CLEAR(1);
436 if (tex
->cmask_buffer
!= &tex
->buffer
)
437 si_resource_reference(&tex
->cmask_buffer
, NULL
);
439 tex
->cmask_buffer
= NULL
;
441 /* Notify all contexts about the change. */
442 p_atomic_inc(&sscreen
->dirty_tex_counter
);
443 p_atomic_inc(&sscreen
->compressed_colortex_counter
);
446 static bool si_can_disable_dcc(struct si_texture
*tex
)
448 /* We can't disable DCC if it can be written by another process. */
449 return tex
->surface
.dcc_offset
&&
450 (!tex
->buffer
.b
.is_shared
||
451 !(tex
->buffer
.external_usage
& PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE
));
454 static void si_texture_zero_dcc_fields(struct si_texture
*tex
)
456 tex
->surface
.dcc_offset
= 0;
457 tex
->surface
.display_dcc_offset
= 0;
458 tex
->surface
.dcc_retile_map_offset
= 0;
461 static bool si_texture_discard_dcc(struct si_screen
*sscreen
,
462 struct si_texture
*tex
)
464 if (!si_can_disable_dcc(tex
)) {
465 assert(tex
->surface
.display_dcc_offset
== 0);
469 assert(tex
->dcc_separate_buffer
== NULL
);
472 si_texture_zero_dcc_fields(tex
);
474 /* Notify all contexts about the change. */
475 p_atomic_inc(&sscreen
->dirty_tex_counter
);
480 * Disable DCC for the texture. (first decompress, then discard metadata).
482 * There is unresolved multi-context synchronization issue between
483 * screen::aux_context and the current context. If applications do this with
484 * multiple contexts, it's already undefined behavior for them and we don't
485 * have to worry about that. The scenario is:
487 * If context 1 disables DCC and context 2 has queued commands that write
488 * to the texture via CB with DCC enabled, and the order of operations is
490 * context 2 queues draw calls rendering to the texture, but doesn't flush
491 * context 1 disables DCC and flushes
492 * context 1 & 2 reset descriptors and FB state
493 * context 2 flushes (new compressed tiles written by the draw calls)
494 * context 1 & 2 read garbage, because DCC is disabled, yet there are
497 * \param sctx the current context if you have one, or sscreen->aux_context
500 bool si_texture_disable_dcc(struct si_context
*sctx
,
501 struct si_texture
*tex
)
503 struct si_screen
*sscreen
= sctx
->screen
;
505 if (!sctx
->has_graphics
)
506 return si_texture_discard_dcc(sscreen
, tex
);
508 if (!si_can_disable_dcc(tex
))
511 if (&sctx
->b
== sscreen
->aux_context
)
512 simple_mtx_lock(&sscreen
->aux_context_lock
);
514 /* Decompress DCC. */
515 si_decompress_dcc(sctx
, tex
);
516 sctx
->b
.flush(&sctx
->b
, NULL
, 0);
518 if (&sctx
->b
== sscreen
->aux_context
)
519 simple_mtx_unlock(&sscreen
->aux_context_lock
);
521 return si_texture_discard_dcc(sscreen
, tex
);
524 static void si_reallocate_texture_inplace(struct si_context
*sctx
,
525 struct si_texture
*tex
,
526 unsigned new_bind_flag
,
527 bool invalidate_storage
)
529 struct pipe_screen
*screen
= sctx
->b
.screen
;
530 struct si_texture
*new_tex
;
531 struct pipe_resource templ
= tex
->buffer
.b
.b
;
534 templ
.bind
|= new_bind_flag
;
536 if (tex
->buffer
.b
.is_shared
|| tex
->num_planes
> 1)
539 if (new_bind_flag
== PIPE_BIND_LINEAR
) {
540 if (tex
->surface
.is_linear
)
543 /* This fails with MSAA, depth, and compressed textures. */
544 if (si_choose_tiling(sctx
->screen
, &templ
, false) !=
545 RADEON_SURF_MODE_LINEAR_ALIGNED
)
549 new_tex
= (struct si_texture
*)screen
->resource_create(screen
, &templ
);
553 /* Copy the pixels to the new texture. */
554 if (!invalidate_storage
) {
555 for (i
= 0; i
<= templ
.last_level
; i
++) {
559 u_minify(templ
.width0
, i
), u_minify(templ
.height0
, i
),
560 util_num_layers(&templ
, i
), &box
);
562 sctx
->dma_copy(&sctx
->b
, &new_tex
->buffer
.b
.b
, i
, 0, 0, 0,
563 &tex
->buffer
.b
.b
, i
, &box
);
567 if (new_bind_flag
== PIPE_BIND_LINEAR
) {
568 si_texture_discard_cmask(sctx
->screen
, tex
);
569 si_texture_discard_dcc(sctx
->screen
, tex
);
572 /* Replace the structure fields of tex. */
573 tex
->buffer
.b
.b
.bind
= templ
.bind
;
574 pb_reference(&tex
->buffer
.buf
, new_tex
->buffer
.buf
);
575 tex
->buffer
.gpu_address
= new_tex
->buffer
.gpu_address
;
576 tex
->buffer
.vram_usage
= new_tex
->buffer
.vram_usage
;
577 tex
->buffer
.gart_usage
= new_tex
->buffer
.gart_usage
;
578 tex
->buffer
.bo_size
= new_tex
->buffer
.bo_size
;
579 tex
->buffer
.bo_alignment
= new_tex
->buffer
.bo_alignment
;
580 tex
->buffer
.domains
= new_tex
->buffer
.domains
;
581 tex
->buffer
.flags
= new_tex
->buffer
.flags
;
583 tex
->surface
= new_tex
->surface
;
584 si_texture_reference(&tex
->flushed_depth_texture
,
585 new_tex
->flushed_depth_texture
);
587 tex
->surface
.fmask_offset
= new_tex
->surface
.fmask_offset
;
588 tex
->surface
.cmask_offset
= new_tex
->surface
.cmask_offset
;
589 tex
->cmask_base_address_reg
= new_tex
->cmask_base_address_reg
;
591 if (tex
->cmask_buffer
== &tex
->buffer
)
592 tex
->cmask_buffer
= NULL
;
594 si_resource_reference(&tex
->cmask_buffer
, NULL
);
596 if (new_tex
->cmask_buffer
== &new_tex
->buffer
)
597 tex
->cmask_buffer
= &tex
->buffer
;
599 si_resource_reference(&tex
->cmask_buffer
, new_tex
->cmask_buffer
);
601 tex
->surface
.dcc_offset
= new_tex
->surface
.dcc_offset
;
602 tex
->cb_color_info
= new_tex
->cb_color_info
;
603 memcpy(tex
->color_clear_value
, new_tex
->color_clear_value
,
604 sizeof(tex
->color_clear_value
));
605 tex
->last_msaa_resolve_target_micro_mode
= new_tex
->last_msaa_resolve_target_micro_mode
;
607 tex
->surface
.htile_offset
= new_tex
->surface
.htile_offset
;
608 tex
->depth_clear_value
= new_tex
->depth_clear_value
;
609 tex
->dirty_level_mask
= new_tex
->dirty_level_mask
;
610 tex
->stencil_dirty_level_mask
= new_tex
->stencil_dirty_level_mask
;
611 tex
->db_render_format
= new_tex
->db_render_format
;
612 tex
->stencil_clear_value
= new_tex
->stencil_clear_value
;
613 tex
->tc_compatible_htile
= new_tex
->tc_compatible_htile
;
614 tex
->depth_cleared
= new_tex
->depth_cleared
;
615 tex
->stencil_cleared
= new_tex
->stencil_cleared
;
616 tex
->upgraded_depth
= new_tex
->upgraded_depth
;
617 tex
->db_compatible
= new_tex
->db_compatible
;
618 tex
->can_sample_z
= new_tex
->can_sample_z
;
619 tex
->can_sample_s
= new_tex
->can_sample_s
;
621 tex
->separate_dcc_dirty
= new_tex
->separate_dcc_dirty
;
622 tex
->dcc_gather_statistics
= new_tex
->dcc_gather_statistics
;
623 si_resource_reference(&tex
->dcc_separate_buffer
,
624 new_tex
->dcc_separate_buffer
);
625 si_resource_reference(&tex
->last_dcc_separate_buffer
,
626 new_tex
->last_dcc_separate_buffer
);
628 if (new_bind_flag
== PIPE_BIND_LINEAR
) {
629 assert(!tex
->surface
.htile_offset
);
630 assert(!tex
->cmask_buffer
);
631 assert(!tex
->surface
.fmask_size
);
632 assert(!tex
->surface
.dcc_offset
);
633 assert(!tex
->is_depth
);
636 si_texture_reference(&new_tex
, NULL
);
638 p_atomic_inc(&sctx
->screen
->dirty_tex_counter
);
641 static uint32_t si_get_bo_metadata_word1(struct si_screen
*sscreen
)
643 return (ATI_VENDOR_ID
<< 16) | sscreen
->info
.pci_id
;
646 static void si_set_tex_bo_metadata(struct si_screen
*sscreen
,
647 struct si_texture
*tex
)
649 struct radeon_surf
*surface
= &tex
->surface
;
650 struct pipe_resource
*res
= &tex
->buffer
.b
.b
;
651 struct radeon_bo_metadata md
;
653 memset(&md
, 0, sizeof(md
));
655 if (sscreen
->info
.chip_class
>= GFX9
) {
656 md
.u
.gfx9
.swizzle_mode
= surface
->u
.gfx9
.surf
.swizzle_mode
;
658 if (tex
->surface
.dcc_offset
&& !tex
->dcc_separate_buffer
) {
659 uint64_t dcc_offset
=
660 tex
->surface
.display_dcc_offset
? tex
->surface
.display_dcc_offset
661 : tex
->surface
.dcc_offset
;
663 assert((dcc_offset
>> 8) != 0 && (dcc_offset
>> 8) < (1 << 24));
664 md
.u
.gfx9
.dcc_offset_256B
= dcc_offset
>> 8;
665 md
.u
.gfx9
.dcc_pitch_max
= tex
->surface
.u
.gfx9
.display_dcc_pitch_max
;
666 md
.u
.gfx9
.dcc_independent_64B
= 1;
669 md
.u
.legacy
.microtile
= surface
->u
.legacy
.level
[0].mode
>= RADEON_SURF_MODE_1D
?
670 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
;
671 md
.u
.legacy
.macrotile
= surface
->u
.legacy
.level
[0].mode
>= RADEON_SURF_MODE_2D
?
672 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
;
673 md
.u
.legacy
.pipe_config
= surface
->u
.legacy
.pipe_config
;
674 md
.u
.legacy
.bankw
= surface
->u
.legacy
.bankw
;
675 md
.u
.legacy
.bankh
= surface
->u
.legacy
.bankh
;
676 md
.u
.legacy
.tile_split
= surface
->u
.legacy
.tile_split
;
677 md
.u
.legacy
.mtilea
= surface
->u
.legacy
.mtilea
;
678 md
.u
.legacy
.num_banks
= surface
->u
.legacy
.num_banks
;
679 md
.u
.legacy
.stride
= surface
->u
.legacy
.level
[0].nblk_x
* surface
->bpe
;
680 md
.u
.legacy
.scanout
= (surface
->flags
& RADEON_SURF_SCANOUT
) != 0;
683 assert(tex
->dcc_separate_buffer
== NULL
);
684 assert(tex
->surface
.fmask_size
== 0);
686 /* Metadata image format format version 1:
687 * [0] = 1 (metadata format identifier)
688 * [1] = (VENDOR_ID << 16) | PCI_ID
689 * [2:9] = image descriptor for the whole resource
690 * [2] is always 0, because the base address is cleared
691 * [9] is the DCC offset bits [39:8] from the beginning of
693 * [10:10+LAST_LEVEL] = mipmap level offset bits [39:8] for each level
696 md
.metadata
[0] = 1; /* metadata image format version 1 */
698 /* TILE_MODE_INDEX is ambiguous without a PCI ID. */
699 md
.metadata
[1] = si_get_bo_metadata_word1(sscreen
);
701 static const unsigned char swizzle
[] = {
707 bool is_array
= util_texture_is_array(res
->target
);
710 sscreen
->make_texture_descriptor(sscreen
, tex
, true,
711 res
->target
, res
->format
,
712 swizzle
, 0, res
->last_level
, 0,
713 is_array
? res
->array_size
- 1 : 0,
714 res
->width0
, res
->height0
, res
->depth0
,
717 si_set_mutable_tex_desc_fields(sscreen
, tex
, &tex
->surface
.u
.legacy
.level
[0],
718 0, 0, tex
->surface
.blk_w
, false, desc
);
720 /* Clear the base address and set the relative DCC offset. */
722 desc
[1] &= C_008F14_BASE_ADDRESS_HI
;
724 switch (sscreen
->info
.chip_class
) {
729 desc
[7] = tex
->surface
.dcc_offset
>> 8;
732 desc
[7] = tex
->surface
.dcc_offset
>> 8;
733 desc
[5] &= C_008F24_META_DATA_ADDRESS
;
734 desc
[5] |= S_008F24_META_DATA_ADDRESS(tex
->surface
.dcc_offset
>> 40);
737 desc
[6] &= C_00A018_META_DATA_ADDRESS_LO
;
738 desc
[6] |= S_00A018_META_DATA_ADDRESS_LO(tex
->surface
.dcc_offset
>> 8);
739 desc
[7] = tex
->surface
.dcc_offset
>> 16;
746 /* Dwords [2:9] contain the image descriptor. */
747 memcpy(&md
.metadata
[2], desc
, sizeof(desc
));
748 md
.size_metadata
= 10 * 4;
750 /* Dwords [10:..] contain the mipmap level offsets. */
751 if (sscreen
->info
.chip_class
<= GFX8
) {
752 for (unsigned i
= 0; i
<= res
->last_level
; i
++)
753 md
.metadata
[10+i
] = tex
->surface
.u
.legacy
.level
[i
].offset
>> 8;
755 md
.size_metadata
+= (1 + res
->last_level
) * 4;
758 sscreen
->ws
->buffer_set_metadata(tex
->buffer
.buf
, &md
);
761 static bool si_read_tex_bo_metadata(struct si_screen
*sscreen
,
762 struct si_texture
*tex
,
764 struct radeon_bo_metadata
*md
)
766 uint32_t *desc
= &md
->metadata
[2];
768 if (offset
|| /* Non-zero planes ignore metadata. */
769 md
->size_metadata
< 10 * 4 || /* at least 2(header) + 8(desc) dwords */
770 md
->metadata
[0] == 0 || /* invalid version number */
771 md
->metadata
[1] != si_get_bo_metadata_word1(sscreen
)) /* invalid PCI ID */ {
772 /* Disable DCC because it might not be enabled. */
773 si_texture_zero_dcc_fields(tex
);
775 /* Don't report an error if the texture comes from an incompatible driver,
776 * but this might not work.
781 /* Validate that sample counts and the number of mipmap levels match. */
782 unsigned last_level
= G_008F1C_LAST_LEVEL(desc
[3]);
783 unsigned type
= G_008F1C_TYPE(desc
[3]);
785 if (type
== V_008F1C_SQ_RSRC_IMG_2D_MSAA
||
786 type
== V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY
) {
787 unsigned log_samples
=
788 util_logbase2(MAX2(1, tex
->buffer
.b
.b
.nr_storage_samples
));
790 if (last_level
!= log_samples
) {
791 fprintf(stderr
, "radeonsi: invalid MSAA texture import, "
792 "metadata has log2(samples) = %u, the caller set %u\n",
793 last_level
, log_samples
);
797 if (last_level
!= tex
->buffer
.b
.b
.last_level
) {
798 fprintf(stderr
, "radeonsi: invalid mipmapped texture import, "
799 "metadata has last_level = %u, the caller set %u\n",
800 last_level
, tex
->buffer
.b
.b
.last_level
);
805 if (sscreen
->info
.chip_class
>= GFX8
&&
806 G_008F28_COMPRESSION_EN(desc
[6])) {
807 /* Read DCC information.
809 * Some state trackers don't set the SCANOUT flag when
810 * importing displayable images, which affects PIPE_ALIGNED
811 * and RB_ALIGNED, so we need to recover them here.
813 switch (sscreen
->info
.chip_class
) {
815 tex
->surface
.dcc_offset
= (uint64_t)desc
[7] << 8;
819 tex
->surface
.dcc_offset
=
820 ((uint64_t)desc
[7] << 8) |
821 ((uint64_t)G_008F24_META_DATA_ADDRESS(desc
[5]) << 40);
822 tex
->surface
.u
.gfx9
.dcc
.pipe_aligned
=
823 G_008F24_META_PIPE_ALIGNED(desc
[5]);
824 tex
->surface
.u
.gfx9
.dcc
.rb_aligned
=
825 G_008F24_META_RB_ALIGNED(desc
[5]);
827 /* If DCC is unaligned, this can only be a displayable image. */
828 if (!tex
->surface
.u
.gfx9
.dcc
.pipe_aligned
&&
829 !tex
->surface
.u
.gfx9
.dcc
.rb_aligned
)
830 tex
->surface
.is_displayable
= true;
834 tex
->surface
.dcc_offset
=
835 ((uint64_t)G_00A018_META_DATA_ADDRESS_LO(desc
[6]) << 8) |
836 ((uint64_t)desc
[7] << 16);
837 tex
->surface
.u
.gfx9
.dcc
.pipe_aligned
=
838 G_00A018_META_PIPE_ALIGNED(desc
[6]);
846 /* Disable DCC. dcc_offset is always set by texture_from_handle
847 * and must be cleared here.
849 si_texture_zero_dcc_fields(tex
);
855 static bool si_has_displayable_dcc(struct si_texture
*tex
)
857 struct si_screen
*sscreen
= (struct si_screen
*)tex
->buffer
.b
.b
.screen
;
859 if (sscreen
->info
.chip_class
<= GFX8
)
862 /* This needs a cache flush before scanout.
863 * (it can't be scanned out and rendered to simultaneously)
865 if (sscreen
->info
.use_display_dcc_unaligned
&&
866 tex
->surface
.dcc_offset
&&
867 !tex
->surface
.u
.gfx9
.dcc
.pipe_aligned
&&
868 !tex
->surface
.u
.gfx9
.dcc
.rb_aligned
)
871 /* This needs an explicit flush (flush_resource). */
872 if (sscreen
->info
.use_display_dcc_with_retile_blit
&&
873 tex
->surface
.display_dcc_offset
)
879 static bool si_resource_get_param(struct pipe_screen
*screen
,
880 struct pipe_context
*context
,
881 struct pipe_resource
*resource
,
884 enum pipe_resource_param param
,
885 unsigned handle_usage
,
888 for (unsigned i
= 0; i
< plane
; i
++)
889 resource
= resource
->next
;
891 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
892 struct si_texture
*tex
= (struct si_texture
*)resource
;
893 struct winsys_handle whandle
;
896 case PIPE_RESOURCE_PARAM_NPLANES
:
897 *value
= resource
->target
== PIPE_BUFFER
? 1 : tex
->num_planes
;
900 case PIPE_RESOURCE_PARAM_STRIDE
:
901 if (resource
->target
== PIPE_BUFFER
)
903 else if (sscreen
->info
.chip_class
>= GFX9
)
904 *value
= tex
->surface
.u
.gfx9
.surf_pitch
* tex
->surface
.bpe
;
906 *value
= tex
->surface
.u
.legacy
.level
[0].nblk_x
* tex
->surface
.bpe
;
909 case PIPE_RESOURCE_PARAM_OFFSET
:
910 if (resource
->target
== PIPE_BUFFER
)
912 else if (sscreen
->info
.chip_class
>= GFX9
)
913 *value
= tex
->surface
.u
.gfx9
.surf_offset
+
914 layer
* tex
->surface
.u
.gfx9
.surf_slice_size
;
916 *value
= tex
->surface
.u
.legacy
.level
[0].offset
+
917 layer
* (uint64_t)tex
->surface
.u
.legacy
.level
[0].slice_size_dw
* 4;
920 case PIPE_RESOURCE_PARAM_MODIFIER
:
921 *value
= DRM_FORMAT_MOD_INVALID
;
924 case PIPE_RESOURCE_PARAM_HANDLE_TYPE_SHARED
:
925 case PIPE_RESOURCE_PARAM_HANDLE_TYPE_KMS
:
926 case PIPE_RESOURCE_PARAM_HANDLE_TYPE_FD
:
927 memset(&whandle
, 0, sizeof(whandle
));
929 if (param
== PIPE_RESOURCE_PARAM_HANDLE_TYPE_SHARED
)
930 whandle
.type
= WINSYS_HANDLE_TYPE_SHARED
;
931 else if (param
== PIPE_RESOURCE_PARAM_HANDLE_TYPE_KMS
)
932 whandle
.type
= WINSYS_HANDLE_TYPE_KMS
;
933 else if (param
== PIPE_RESOURCE_PARAM_HANDLE_TYPE_FD
)
934 whandle
.type
= WINSYS_HANDLE_TYPE_FD
;
936 if (!screen
->resource_get_handle(screen
, context
, resource
,
937 &whandle
, handle_usage
))
940 *value
= whandle
.handle
;
946 static void si_texture_get_info(struct pipe_screen
* screen
,
947 struct pipe_resource
*resource
,
954 si_resource_get_param(screen
, NULL
, resource
, 0, 0,
955 PIPE_RESOURCE_PARAM_STRIDE
, 0, &value
);
960 si_resource_get_param(screen
, NULL
, resource
, 0, 0,
961 PIPE_RESOURCE_PARAM_OFFSET
, 0, &value
);
966 static bool si_texture_get_handle(struct pipe_screen
* screen
,
967 struct pipe_context
*ctx
,
968 struct pipe_resource
*resource
,
969 struct winsys_handle
*whandle
,
972 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
973 struct si_context
*sctx
;
974 struct si_resource
*res
= si_resource(resource
);
975 struct si_texture
*tex
= (struct si_texture
*)resource
;
976 bool update_metadata
= false;
977 unsigned stride
, offset
, slice_size
;
980 ctx
= threaded_context_unwrap_sync(ctx
);
981 sctx
= (struct si_context
*)(ctx
? ctx
: sscreen
->aux_context
);
983 if (resource
->target
!= PIPE_BUFFER
) {
984 /* Individual planes are chained pipe_resource instances. */
985 for (unsigned i
= 0; i
< whandle
->plane
; i
++) {
986 resource
= resource
->next
;
987 res
= si_resource(resource
);
988 tex
= (struct si_texture
*)resource
;
991 /* This is not supported now, but it might be required for OpenCL
992 * interop in the future.
994 if (resource
->nr_samples
> 1 || tex
->is_depth
)
997 /* Move a suballocated texture into a non-suballocated allocation. */
998 if (sscreen
->ws
->buffer_is_suballocated(res
->buf
) ||
999 tex
->surface
.tile_swizzle
||
1000 (tex
->buffer
.flags
& RADEON_FLAG_NO_INTERPROCESS_SHARING
&&
1001 sscreen
->info
.has_local_buffers
)) {
1002 assert(!res
->b
.is_shared
);
1003 si_reallocate_texture_inplace(sctx
, tex
,
1004 PIPE_BIND_SHARED
, false);
1006 assert(res
->b
.b
.bind
& PIPE_BIND_SHARED
);
1007 assert(res
->flags
& RADEON_FLAG_NO_SUBALLOC
);
1008 assert(!(res
->flags
& RADEON_FLAG_NO_INTERPROCESS_SHARING
));
1009 assert(tex
->surface
.tile_swizzle
== 0);
1012 /* Since shader image stores don't support DCC on GFX8,
1013 * disable it for external clients that want write
1016 if ((usage
& PIPE_HANDLE_USAGE_SHADER_WRITE
&& tex
->surface
.dcc_offset
) ||
1017 /* Displayable DCC requires an explicit flush. */
1018 (!(usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
) &&
1019 si_has_displayable_dcc(tex
))) {
1020 if (si_texture_disable_dcc(sctx
, tex
)) {
1021 update_metadata
= true;
1022 /* si_texture_disable_dcc flushes the context */
1027 if (!(usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
) &&
1028 (tex
->cmask_buffer
|| tex
->surface
.dcc_offset
)) {
1029 /* Eliminate fast clear (both CMASK and DCC) */
1030 si_eliminate_fast_color_clear(sctx
, tex
);
1031 /* eliminate_fast_color_clear flushes the context */
1034 /* Disable CMASK if flush_resource isn't going
1037 if (tex
->cmask_buffer
)
1038 si_texture_discard_cmask(sscreen
, tex
);
1042 if ((!res
->b
.is_shared
|| update_metadata
) && whandle
->offset
== 0)
1043 si_set_tex_bo_metadata(sscreen
, tex
);
1045 if (sscreen
->info
.chip_class
>= GFX9
) {
1046 slice_size
= tex
->surface
.u
.gfx9
.surf_slice_size
;
1048 slice_size
= (uint64_t)tex
->surface
.u
.legacy
.level
[0].slice_size_dw
* 4;
1051 /* Buffer exports are for the OpenCL interop. */
1052 /* Move a suballocated buffer into a non-suballocated allocation. */
1053 if (sscreen
->ws
->buffer_is_suballocated(res
->buf
) ||
1054 /* A DMABUF export always fails if the BO is local. */
1055 (tex
->buffer
.flags
& RADEON_FLAG_NO_INTERPROCESS_SHARING
&&
1056 sscreen
->info
.has_local_buffers
)) {
1057 assert(!res
->b
.is_shared
);
1059 /* Allocate a new buffer with PIPE_BIND_SHARED. */
1060 struct pipe_resource templ
= res
->b
.b
;
1061 templ
.bind
|= PIPE_BIND_SHARED
;
1063 struct pipe_resource
*newb
=
1064 screen
->resource_create(screen
, &templ
);
1068 /* Copy the old buffer contents to the new one. */
1069 struct pipe_box box
;
1070 u_box_1d(0, newb
->width0
, &box
);
1071 sctx
->b
.resource_copy_region(&sctx
->b
, newb
, 0, 0, 0, 0,
1072 &res
->b
.b
, 0, &box
);
1074 /* Move the new buffer storage to the old pipe_resource. */
1075 si_replace_buffer_storage(&sctx
->b
, &res
->b
.b
, newb
);
1076 pipe_resource_reference(&newb
, NULL
);
1078 assert(res
->b
.b
.bind
& PIPE_BIND_SHARED
);
1079 assert(res
->flags
& RADEON_FLAG_NO_SUBALLOC
);
1086 si_texture_get_info(screen
, resource
, &stride
, &offset
);
1089 sctx
->b
.flush(&sctx
->b
, NULL
, 0);
1091 if (res
->b
.is_shared
) {
1092 /* USAGE_EXPLICIT_FLUSH must be cleared if at least one user
1095 res
->external_usage
|= usage
& ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
;
1096 if (!(usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
))
1097 res
->external_usage
&= ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
;
1099 res
->b
.is_shared
= true;
1100 res
->external_usage
= usage
;
1103 whandle
->stride
= stride
;
1104 whandle
->offset
= offset
+ slice_size
* whandle
->layer
;
1106 return sscreen
->ws
->buffer_get_handle(sscreen
->ws
, res
->buf
, whandle
);
1109 static void si_texture_destroy(struct pipe_screen
*screen
,
1110 struct pipe_resource
*ptex
)
1112 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
1113 struct si_texture
*tex
= (struct si_texture
*)ptex
;
1114 struct si_resource
*resource
= &tex
->buffer
;
1116 if (sscreen
->info
.chip_class
>= GFX9
)
1117 free(tex
->surface
.u
.gfx9
.dcc_retile_map
);
1119 si_texture_reference(&tex
->flushed_depth_texture
, NULL
);
1121 if (tex
->cmask_buffer
!= &tex
->buffer
) {
1122 si_resource_reference(&tex
->cmask_buffer
, NULL
);
1124 pb_reference(&resource
->buf
, NULL
);
1125 si_resource_reference(&tex
->dcc_separate_buffer
, NULL
);
1126 si_resource_reference(&tex
->last_dcc_separate_buffer
, NULL
);
1130 static const struct u_resource_vtbl si_texture_vtbl
;
1132 void si_print_texture_info(struct si_screen
*sscreen
,
1133 struct si_texture
*tex
, struct u_log_context
*log
)
1137 /* Common parameters. */
1138 u_log_printf(log
, " Info: npix_x=%u, npix_y=%u, npix_z=%u, blk_w=%u, "
1139 "blk_h=%u, array_size=%u, last_level=%u, "
1140 "bpe=%u, nsamples=%u, flags=0x%x, %s\n",
1141 tex
->buffer
.b
.b
.width0
, tex
->buffer
.b
.b
.height0
,
1142 tex
->buffer
.b
.b
.depth0
, tex
->surface
.blk_w
,
1144 tex
->buffer
.b
.b
.array_size
, tex
->buffer
.b
.b
.last_level
,
1145 tex
->surface
.bpe
, tex
->buffer
.b
.b
.nr_samples
,
1146 tex
->surface
.flags
, util_format_short_name(tex
->buffer
.b
.b
.format
));
1148 if (sscreen
->info
.chip_class
>= GFX9
) {
1149 u_log_printf(log
, " Surf: size=%"PRIu64
", slice_size=%"PRIu64
", "
1150 "alignment=%u, swmode=%u, epitch=%u, pitch=%u\n",
1151 tex
->surface
.surf_size
,
1152 tex
->surface
.u
.gfx9
.surf_slice_size
,
1153 tex
->surface
.surf_alignment
,
1154 tex
->surface
.u
.gfx9
.surf
.swizzle_mode
,
1155 tex
->surface
.u
.gfx9
.surf
.epitch
,
1156 tex
->surface
.u
.gfx9
.surf_pitch
);
1158 if (tex
->surface
.fmask_offset
) {
1159 u_log_printf(log
, " FMASK: offset=%"PRIu64
", size=%"PRIu64
", "
1160 "alignment=%u, swmode=%u, epitch=%u\n",
1161 tex
->surface
.fmask_offset
,
1162 tex
->surface
.fmask_size
,
1163 tex
->surface
.fmask_alignment
,
1164 tex
->surface
.u
.gfx9
.fmask
.swizzle_mode
,
1165 tex
->surface
.u
.gfx9
.fmask
.epitch
);
1168 if (tex
->cmask_buffer
) {
1169 u_log_printf(log
, " CMask: offset=%"PRIu64
", size=%u, "
1170 "alignment=%u, rb_aligned=%u, pipe_aligned=%u\n",
1171 tex
->surface
.cmask_offset
,
1172 tex
->surface
.cmask_size
,
1173 tex
->surface
.cmask_alignment
,
1174 tex
->surface
.u
.gfx9
.cmask
.rb_aligned
,
1175 tex
->surface
.u
.gfx9
.cmask
.pipe_aligned
);
1178 if (tex
->surface
.htile_offset
) {
1179 u_log_printf(log
, " HTile: offset=%"PRIu64
", size=%u, alignment=%u, "
1180 "rb_aligned=%u, pipe_aligned=%u\n",
1181 tex
->surface
.htile_offset
,
1182 tex
->surface
.htile_size
,
1183 tex
->surface
.htile_alignment
,
1184 tex
->surface
.u
.gfx9
.htile
.rb_aligned
,
1185 tex
->surface
.u
.gfx9
.htile
.pipe_aligned
);
1188 if (tex
->surface
.dcc_offset
) {
1189 u_log_printf(log
, " DCC: offset=%"PRIu64
", size=%u, "
1190 "alignment=%u, pitch_max=%u, num_dcc_levels=%u\n",
1191 tex
->surface
.dcc_offset
, tex
->surface
.dcc_size
,
1192 tex
->surface
.dcc_alignment
,
1193 tex
->surface
.u
.gfx9
.display_dcc_pitch_max
,
1194 tex
->surface
.num_dcc_levels
);
1197 if (tex
->surface
.u
.gfx9
.stencil_offset
) {
1198 u_log_printf(log
, " Stencil: offset=%"PRIu64
", swmode=%u, epitch=%u\n",
1199 tex
->surface
.u
.gfx9
.stencil_offset
,
1200 tex
->surface
.u
.gfx9
.stencil
.swizzle_mode
,
1201 tex
->surface
.u
.gfx9
.stencil
.epitch
);
1206 u_log_printf(log
, " Layout: size=%"PRIu64
", alignment=%u, bankw=%u, "
1207 "bankh=%u, nbanks=%u, mtilea=%u, tilesplit=%u, pipeconfig=%u, scanout=%u\n",
1208 tex
->surface
.surf_size
, tex
->surface
.surf_alignment
, tex
->surface
.u
.legacy
.bankw
,
1209 tex
->surface
.u
.legacy
.bankh
, tex
->surface
.u
.legacy
.num_banks
, tex
->surface
.u
.legacy
.mtilea
,
1210 tex
->surface
.u
.legacy
.tile_split
, tex
->surface
.u
.legacy
.pipe_config
,
1211 (tex
->surface
.flags
& RADEON_SURF_SCANOUT
) != 0);
1213 if (tex
->surface
.fmask_offset
)
1214 u_log_printf(log
, " FMask: offset=%"PRIu64
", size=%"PRIu64
", alignment=%u, pitch_in_pixels=%u, "
1215 "bankh=%u, slice_tile_max=%u, tile_mode_index=%u\n",
1216 tex
->surface
.fmask_offset
, tex
->surface
.fmask_size
, tex
->surface
.fmask_alignment
,
1217 tex
->surface
.u
.legacy
.fmask
.pitch_in_pixels
,
1218 tex
->surface
.u
.legacy
.fmask
.bankh
,
1219 tex
->surface
.u
.legacy
.fmask
.slice_tile_max
,
1220 tex
->surface
.u
.legacy
.fmask
.tiling_index
);
1222 if (tex
->cmask_buffer
)
1223 u_log_printf(log
, " CMask: offset=%"PRIu64
", size=%u, alignment=%u, "
1224 "slice_tile_max=%u\n",
1225 tex
->surface
.cmask_offset
, tex
->surface
.cmask_size
, tex
->surface
.cmask_alignment
,
1226 tex
->surface
.u
.legacy
.cmask_slice_tile_max
);
1228 if (tex
->surface
.htile_offset
)
1229 u_log_printf(log
, " HTile: offset=%"PRIu64
", size=%u, "
1230 "alignment=%u, TC_compatible = %u\n",
1231 tex
->surface
.htile_offset
, tex
->surface
.htile_size
,
1232 tex
->surface
.htile_alignment
,
1233 tex
->tc_compatible_htile
);
1235 if (tex
->surface
.dcc_offset
) {
1236 u_log_printf(log
, " DCC: offset=%"PRIu64
", size=%u, alignment=%u\n",
1237 tex
->surface
.dcc_offset
, tex
->surface
.dcc_size
,
1238 tex
->surface
.dcc_alignment
);
1239 for (i
= 0; i
<= tex
->buffer
.b
.b
.last_level
; i
++)
1240 u_log_printf(log
, " DCCLevel[%i]: enabled=%u, offset=%u, "
1241 "fast_clear_size=%u\n",
1242 i
, i
< tex
->surface
.num_dcc_levels
,
1243 tex
->surface
.u
.legacy
.level
[i
].dcc_offset
,
1244 tex
->surface
.u
.legacy
.level
[i
].dcc_fast_clear_size
);
1247 for (i
= 0; i
<= tex
->buffer
.b
.b
.last_level
; i
++)
1248 u_log_printf(log
, " Level[%i]: offset=%"PRIu64
", slice_size=%"PRIu64
", "
1249 "npix_x=%u, npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
1250 "mode=%u, tiling_index = %u\n",
1251 i
, tex
->surface
.u
.legacy
.level
[i
].offset
,
1252 (uint64_t)tex
->surface
.u
.legacy
.level
[i
].slice_size_dw
* 4,
1253 u_minify(tex
->buffer
.b
.b
.width0
, i
),
1254 u_minify(tex
->buffer
.b
.b
.height0
, i
),
1255 u_minify(tex
->buffer
.b
.b
.depth0
, i
),
1256 tex
->surface
.u
.legacy
.level
[i
].nblk_x
,
1257 tex
->surface
.u
.legacy
.level
[i
].nblk_y
,
1258 tex
->surface
.u
.legacy
.level
[i
].mode
,
1259 tex
->surface
.u
.legacy
.tiling_index
[i
]);
1261 if (tex
->surface
.has_stencil
) {
1262 u_log_printf(log
, " StencilLayout: tilesplit=%u\n",
1263 tex
->surface
.u
.legacy
.stencil_tile_split
);
1264 for (i
= 0; i
<= tex
->buffer
.b
.b
.last_level
; i
++) {
1265 u_log_printf(log
, " StencilLevel[%i]: offset=%"PRIu64
", "
1266 "slice_size=%"PRIu64
", npix_x=%u, "
1267 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
1268 "mode=%u, tiling_index = %u\n",
1269 i
, tex
->surface
.u
.legacy
.stencil_level
[i
].offset
,
1270 (uint64_t)tex
->surface
.u
.legacy
.stencil_level
[i
].slice_size_dw
* 4,
1271 u_minify(tex
->buffer
.b
.b
.width0
, i
),
1272 u_minify(tex
->buffer
.b
.b
.height0
, i
),
1273 u_minify(tex
->buffer
.b
.b
.depth0
, i
),
1274 tex
->surface
.u
.legacy
.stencil_level
[i
].nblk_x
,
1275 tex
->surface
.u
.legacy
.stencil_level
[i
].nblk_y
,
1276 tex
->surface
.u
.legacy
.stencil_level
[i
].mode
,
1277 tex
->surface
.u
.legacy
.stencil_tiling_index
[i
]);
1283 * Common function for si_texture_create and si_texture_from_handle.
1285 * \param screen screen
1286 * \param base resource template
1287 * \param surface radeon_surf
1288 * \param plane0 if a non-zero plane is being created, this is the first plane
1289 * \param imported_buf from si_texture_from_handle
1290 * \param offset offset for non-zero planes or imported buffers
1291 * \param alloc_size the size to allocate if plane0 != NULL
1292 * \param alignment alignment for the allocation
1294 static struct si_texture
*
1295 si_texture_create_object(struct pipe_screen
*screen
,
1296 const struct pipe_resource
*base
,
1297 const struct radeon_surf
*surface
,
1298 const struct si_texture
*plane0
,
1299 struct pb_buffer
*imported_buf
,
1301 uint64_t alloc_size
,
1304 struct si_texture
*tex
;
1305 struct si_resource
*resource
;
1306 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
1308 tex
= CALLOC_STRUCT(si_texture
);
1312 resource
= &tex
->buffer
;
1313 resource
->b
.b
= *base
;
1314 resource
->b
.b
.next
= NULL
;
1315 resource
->b
.vtbl
= &si_texture_vtbl
;
1316 pipe_reference_init(&resource
->b
.b
.reference
, 1);
1317 resource
->b
.b
.screen
= screen
;
1319 /* don't include stencil-only formats which we don't support for rendering */
1320 tex
->is_depth
= util_format_has_depth(util_format_description(tex
->buffer
.b
.b
.format
));
1321 tex
->surface
= *surface
;
1322 tex
->tc_compatible_htile
= tex
->surface
.htile_size
!= 0 &&
1323 (tex
->surface
.flags
&
1324 RADEON_SURF_TC_COMPATIBLE_HTILE
);
1326 /* TC-compatible HTILE:
1327 * - GFX8 only supports Z32_FLOAT.
1328 * - GFX9 only supports Z32_FLOAT and Z16_UNORM. */
1329 if (tex
->tc_compatible_htile
) {
1330 if (sscreen
->info
.chip_class
>= GFX9
&&
1331 base
->format
== PIPE_FORMAT_Z16_UNORM
)
1332 tex
->db_render_format
= base
->format
;
1334 tex
->db_render_format
= PIPE_FORMAT_Z32_FLOAT
;
1335 tex
->upgraded_depth
= base
->format
!= PIPE_FORMAT_Z32_FLOAT
&&
1336 base
->format
!= PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
;
1339 tex
->db_render_format
= base
->format
;
1342 /* Applies to GCN. */
1343 tex
->last_msaa_resolve_target_micro_mode
= tex
->surface
.micro_tile_mode
;
1345 /* Disable separate DCC at the beginning. DRI2 doesn't reuse buffers
1346 * between frames, so the only thing that can enable separate DCC
1347 * with DRI2 is multiple slow clears within a frame.
1349 tex
->ps_draw_ratio
= 0;
1351 if (sscreen
->info
.chip_class
>= GFX9
) {
1352 tex
->surface
.u
.gfx9
.surf_offset
= offset
;
1354 for (unsigned i
= 0; i
< ARRAY_SIZE(surface
->u
.legacy
.level
); ++i
)
1355 tex
->surface
.u
.legacy
.level
[i
].offset
+= offset
;
1358 if (tex
->is_depth
) {
1359 if (sscreen
->info
.chip_class
>= GFX9
) {
1360 tex
->can_sample_z
= true;
1361 tex
->can_sample_s
= true;
1363 /* Stencil texturing with HTILE doesn't work
1364 * with mipmapping on Navi10-14. */
1365 if ((sscreen
->info
.family
== CHIP_NAVI10
||
1366 sscreen
->info
.family
== CHIP_NAVI12
||
1367 sscreen
->info
.family
== CHIP_NAVI14
) &&
1368 base
->last_level
> 0)
1369 tex
->htile_stencil_disabled
= true;
1371 tex
->can_sample_z
= !tex
->surface
.u
.legacy
.depth_adjusted
;
1372 tex
->can_sample_s
= !tex
->surface
.u
.legacy
.stencil_adjusted
;
1375 tex
->db_compatible
= surface
->flags
& RADEON_SURF_ZBUFFER
;
1377 if (tex
->surface
.cmask_offset
) {
1378 tex
->cb_color_info
|= S_028C70_FAST_CLEAR(1);
1379 tex
->cmask_buffer
= &tex
->buffer
;
1384 /* The buffer is shared with the first plane. */
1385 resource
->bo_size
= plane0
->buffer
.bo_size
;
1386 resource
->bo_alignment
= plane0
->buffer
.bo_alignment
;
1387 resource
->flags
= plane0
->buffer
.flags
;
1388 resource
->domains
= plane0
->buffer
.domains
;
1389 resource
->vram_usage
= plane0
->buffer
.vram_usage
;
1390 resource
->gart_usage
= plane0
->buffer
.gart_usage
;
1392 pb_reference(&resource
->buf
, plane0
->buffer
.buf
);
1393 resource
->gpu_address
= plane0
->buffer
.gpu_address
;
1394 } else if (!(surface
->flags
& RADEON_SURF_IMPORTED
)) {
1395 /* Create the backing buffer. */
1396 si_init_resource_fields(sscreen
, resource
, alloc_size
, alignment
);
1398 if (!si_alloc_resource(sscreen
, resource
))
1401 resource
->buf
= imported_buf
;
1402 resource
->gpu_address
= sscreen
->ws
->buffer_get_virtual_address(resource
->buf
);
1403 resource
->bo_size
= imported_buf
->size
;
1404 resource
->bo_alignment
= imported_buf
->alignment
;
1405 resource
->domains
= sscreen
->ws
->buffer_get_initial_domain(resource
->buf
);
1406 if (resource
->domains
& RADEON_DOMAIN_VRAM
)
1407 resource
->vram_usage
= resource
->bo_size
;
1408 else if (resource
->domains
& RADEON_DOMAIN_GTT
)
1409 resource
->gart_usage
= resource
->bo_size
;
1412 if (tex
->cmask_buffer
) {
1413 /* Initialize the cmask to 0xCC (= compressed state). */
1414 si_screen_clear_buffer(sscreen
, &tex
->cmask_buffer
->b
.b
,
1415 tex
->surface
.cmask_offset
, tex
->surface
.cmask_size
,
1418 if (tex
->surface
.htile_offset
) {
1419 uint32_t clear_value
= 0;
1421 if (sscreen
->info
.chip_class
>= GFX9
|| tex
->tc_compatible_htile
)
1422 clear_value
= 0x0000030F;
1424 si_screen_clear_buffer(sscreen
, &tex
->buffer
.b
.b
,
1425 tex
->surface
.htile_offset
,
1426 tex
->surface
.htile_size
,
1430 /* Initialize DCC only if the texture is not being imported. */
1431 if (!(surface
->flags
& RADEON_SURF_IMPORTED
) && tex
->surface
.dcc_offset
) {
1432 /* Clear DCC to black for all tiles with DCC enabled.
1434 * This fixes corruption in 3DMark Slingshot Extreme, which
1435 * uses uninitialized textures, causing corruption.
1437 if (tex
->surface
.num_dcc_levels
== tex
->buffer
.b
.b
.last_level
+ 1 &&
1438 tex
->buffer
.b
.b
.nr_samples
<= 2) {
1439 /* Simple case - all tiles have DCC enabled. */
1440 si_screen_clear_buffer(sscreen
, &tex
->buffer
.b
.b
,
1441 tex
->surface
.dcc_offset
,
1442 tex
->surface
.dcc_size
,
1443 DCC_CLEAR_COLOR_0000
);
1444 } else if (sscreen
->info
.chip_class
>= GFX9
) {
1445 /* Clear to uncompressed. Clearing this to black is complicated. */
1446 si_screen_clear_buffer(sscreen
, &tex
->buffer
.b
.b
,
1447 tex
->surface
.dcc_offset
,
1448 tex
->surface
.dcc_size
,
1451 /* GFX8: Initialize mipmap levels and multisamples separately. */
1452 if (tex
->buffer
.b
.b
.nr_samples
>= 2) {
1453 /* Clearing this to black is complicated. */
1454 si_screen_clear_buffer(sscreen
, &tex
->buffer
.b
.b
,
1455 tex
->surface
.dcc_offset
,
1456 tex
->surface
.dcc_size
,
1459 /* Clear the enabled mipmap levels to black. */
1462 for (unsigned i
= 0; i
< tex
->surface
.num_dcc_levels
; i
++) {
1463 if (!tex
->surface
.u
.legacy
.level
[i
].dcc_fast_clear_size
)
1466 size
= tex
->surface
.u
.legacy
.level
[i
].dcc_offset
+
1467 tex
->surface
.u
.legacy
.level
[i
].dcc_fast_clear_size
;
1470 /* Mipmap levels with DCC. */
1472 si_screen_clear_buffer(sscreen
, &tex
->buffer
.b
.b
,
1473 tex
->surface
.dcc_offset
, size
,
1474 DCC_CLEAR_COLOR_0000
);
1476 /* Mipmap levels without DCC. */
1477 if (size
!= tex
->surface
.dcc_size
) {
1478 si_screen_clear_buffer(sscreen
, &tex
->buffer
.b
.b
,
1479 tex
->surface
.dcc_offset
+ size
,
1480 tex
->surface
.dcc_size
- size
,
1486 /* Initialize displayable DCC that requires the retile blit. */
1487 if (tex
->surface
.dcc_retile_map_offset
) {
1488 /* Uninitialized DCC can hang the display hw.
1489 * Clear to white to indicate that. */
1490 si_screen_clear_buffer(sscreen
, &tex
->buffer
.b
.b
,
1491 tex
->surface
.display_dcc_offset
,
1492 tex
->surface
.u
.gfx9
.display_dcc_size
,
1493 DCC_CLEAR_COLOR_1111
);
1495 /* Upload the DCC retile map.
1496 * Use a staging buffer for the upload, because
1497 * the buffer backing the texture is unmappable.
1499 bool use_uint16
= tex
->surface
.u
.gfx9
.dcc_retile_use_uint16
;
1500 unsigned num_elements
= tex
->surface
.u
.gfx9
.dcc_retile_num_elements
;
1501 struct si_resource
*buf
=
1502 si_aligned_buffer_create(screen
, 0, PIPE_USAGE_STREAM
,
1503 num_elements
* (use_uint16
? 2 : 4),
1504 sscreen
->info
.tcc_cache_line_size
);
1505 uint32_t *ui
= (uint32_t*)sscreen
->ws
->buffer_map(buf
->buf
, NULL
,
1506 PIPE_TRANSFER_WRITE
);
1507 uint16_t *us
= (uint16_t*)ui
;
1509 /* Upload the retile map into a staging buffer. */
1511 for (unsigned i
= 0; i
< num_elements
; i
++)
1512 us
[i
] = tex
->surface
.u
.gfx9
.dcc_retile_map
[i
];
1514 for (unsigned i
= 0; i
< num_elements
; i
++)
1515 ui
[i
] = tex
->surface
.u
.gfx9
.dcc_retile_map
[i
];
1518 /* Copy the staging buffer to the buffer backing the texture. */
1519 struct si_context
*sctx
= (struct si_context
*)sscreen
->aux_context
;
1520 struct pipe_box box
;
1521 u_box_1d(0, buf
->b
.b
.width0
, &box
);
1523 assert(tex
->surface
.dcc_retile_map_offset
<= UINT_MAX
);
1524 simple_mtx_lock(&sscreen
->aux_context_lock
);
1525 sctx
->dma_copy(&sctx
->b
, &tex
->buffer
.b
.b
, 0,
1526 tex
->surface
.dcc_retile_map_offset
, 0, 0,
1527 &buf
->b
.b
, 0, &box
);
1528 sscreen
->aux_context
->flush(sscreen
->aux_context
, NULL
, 0);
1529 simple_mtx_unlock(&sscreen
->aux_context_lock
);
1531 si_resource_reference(&buf
, NULL
);
1535 /* Initialize the CMASK base register value. */
1536 tex
->cmask_base_address_reg
=
1537 (tex
->buffer
.gpu_address
+ tex
->surface
.cmask_offset
) >> 8;
1539 if (sscreen
->debug_flags
& DBG(VM
)) {
1540 fprintf(stderr
, "VM start=0x%"PRIX64
" end=0x%"PRIX64
" | Texture %ix%ix%i, %i levels, %i samples, %s\n",
1541 tex
->buffer
.gpu_address
,
1542 tex
->buffer
.gpu_address
+ tex
->buffer
.buf
->size
,
1543 base
->width0
, base
->height0
, util_num_layers(base
, 0), base
->last_level
+1,
1544 base
->nr_samples
? base
->nr_samples
: 1, util_format_short_name(base
->format
));
1547 if (sscreen
->debug_flags
& DBG(TEX
)) {
1549 struct u_log_context log
;
1550 u_log_context_init(&log
);
1551 si_print_texture_info(sscreen
, tex
, &log
);
1552 u_log_new_page_print(&log
, stdout
);
1554 u_log_context_destroy(&log
);
1561 if (sscreen
->info
.chip_class
>= GFX9
)
1562 free(surface
->u
.gfx9
.dcc_retile_map
);
1566 static enum radeon_surf_mode
1567 si_choose_tiling(struct si_screen
*sscreen
,
1568 const struct pipe_resource
*templ
, bool tc_compatible_htile
)
1570 const struct util_format_description
*desc
= util_format_description(templ
->format
);
1571 bool force_tiling
= templ
->flags
& SI_RESOURCE_FLAG_FORCE_MSAA_TILING
;
1572 bool is_depth_stencil
= util_format_is_depth_or_stencil(templ
->format
) &&
1573 !(templ
->flags
& SI_RESOURCE_FLAG_FLUSHED_DEPTH
);
1575 /* MSAA resources must be 2D tiled. */
1576 if (templ
->nr_samples
> 1)
1577 return RADEON_SURF_MODE_2D
;
1579 /* Transfer resources should be linear. */
1580 if (templ
->flags
& SI_RESOURCE_FLAG_TRANSFER
)
1581 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1583 /* Avoid Z/S decompress blits by forcing TC-compatible HTILE on GFX8,
1584 * which requires 2D tiling.
1586 if (sscreen
->info
.chip_class
== GFX8
&& tc_compatible_htile
)
1587 return RADEON_SURF_MODE_2D
;
1589 /* Handle common candidates for the linear mode.
1590 * Compressed textures and DB surfaces must always be tiled.
1592 if (!force_tiling
&&
1593 !is_depth_stencil
&&
1594 !util_format_is_compressed(templ
->format
)) {
1595 if (sscreen
->debug_flags
& DBG(NO_TILING
))
1596 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1598 /* Tiling doesn't work with the 422 (SUBSAMPLED) formats. */
1599 if (desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
)
1600 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1602 /* Cursors are linear on AMD GCN.
1603 * (XXX double-check, maybe also use RADEON_SURF_SCANOUT) */
1604 if (templ
->bind
& PIPE_BIND_CURSOR
)
1605 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1607 if (templ
->bind
& PIPE_BIND_LINEAR
)
1608 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1610 /* Textures with a very small height are recommended to be linear. */
1611 if (templ
->target
== PIPE_TEXTURE_1D
||
1612 templ
->target
== PIPE_TEXTURE_1D_ARRAY
||
1613 /* Only very thin and long 2D textures should benefit from
1614 * linear_aligned. */
1615 (templ
->width0
> 8 && templ
->height0
<= 2))
1616 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1618 /* Textures likely to be mapped often. */
1619 if (templ
->usage
== PIPE_USAGE_STAGING
||
1620 templ
->usage
== PIPE_USAGE_STREAM
)
1621 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1624 /* Make small textures 1D tiled. */
1625 if (templ
->width0
<= 16 || templ
->height0
<= 16 ||
1626 (sscreen
->debug_flags
& DBG(NO_2D_TILING
)))
1627 return RADEON_SURF_MODE_1D
;
1629 /* The allocator will switch to 1D if needed. */
1630 return RADEON_SURF_MODE_2D
;
1633 struct pipe_resource
*si_texture_create(struct pipe_screen
*screen
,
1634 const struct pipe_resource
*templ
)
1636 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
1637 bool is_zs
= util_format_is_depth_or_stencil(templ
->format
);
1639 if (templ
->nr_samples
>= 2) {
1640 /* This is hackish (overwriting the const pipe_resource template),
1641 * but should be harmless and state trackers can also see
1642 * the overriden number of samples in the created pipe_resource.
1644 if (is_zs
&& sscreen
->eqaa_force_z_samples
) {
1645 ((struct pipe_resource
*)templ
)->nr_samples
=
1646 ((struct pipe_resource
*)templ
)->nr_storage_samples
=
1647 sscreen
->eqaa_force_z_samples
;
1648 } else if (!is_zs
&& sscreen
->eqaa_force_color_samples
) {
1649 ((struct pipe_resource
*)templ
)->nr_samples
=
1650 sscreen
->eqaa_force_coverage_samples
;
1651 ((struct pipe_resource
*)templ
)->nr_storage_samples
=
1652 sscreen
->eqaa_force_color_samples
;
1656 bool is_flushed_depth
= templ
->flags
& SI_RESOURCE_FLAG_FLUSHED_DEPTH
||
1657 templ
->flags
& SI_RESOURCE_FLAG_TRANSFER
;
1658 bool tc_compatible_htile
=
1659 sscreen
->info
.chip_class
>= GFX8
&&
1660 /* There are issues with TC-compatible HTILE on Tonga (and
1661 * Iceland is the same design), and documented bug workarounds
1662 * don't help. For example, this fails:
1663 * piglit/bin/tex-miplevel-selection 'texture()' 2DShadow -auto
1665 sscreen
->info
.family
!= CHIP_TONGA
&&
1666 sscreen
->info
.family
!= CHIP_ICELAND
&&
1667 (templ
->flags
& PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY
) &&
1668 !(sscreen
->debug_flags
& DBG(NO_HYPERZ
)) &&
1669 !is_flushed_depth
&&
1670 templ
->nr_samples
<= 1 && /* TC-compat HTILE is less efficient with MSAA */
1672 enum radeon_surf_mode tile_mode
= si_choose_tiling(sscreen
, templ
,
1673 tc_compatible_htile
);
1675 /* This allocates textures with multiple planes like NV12 in 1 buffer. */
1676 enum { SI_TEXTURE_MAX_PLANES
= 3 };
1677 struct radeon_surf surface
[SI_TEXTURE_MAX_PLANES
] = {};
1678 struct pipe_resource plane_templ
[SI_TEXTURE_MAX_PLANES
];
1679 uint64_t plane_offset
[SI_TEXTURE_MAX_PLANES
] = {};
1680 uint64_t total_size
= 0;
1681 unsigned max_alignment
= 0;
1682 unsigned num_planes
= util_format_get_num_planes(templ
->format
);
1683 assert(num_planes
<= SI_TEXTURE_MAX_PLANES
);
1685 /* Compute texture or plane layouts and offsets. */
1686 for (unsigned i
= 0; i
< num_planes
; i
++) {
1687 plane_templ
[i
] = *templ
;
1688 plane_templ
[i
].format
= util_format_get_plane_format(templ
->format
, i
);
1689 plane_templ
[i
].width0
= util_format_get_plane_width(templ
->format
, i
, templ
->width0
);
1690 plane_templ
[i
].height0
= util_format_get_plane_height(templ
->format
, i
, templ
->height0
);
1692 /* Multi-plane allocations need PIPE_BIND_SHARED, because we can't
1693 * reallocate the storage to add PIPE_BIND_SHARED, because it's
1694 * shared by 3 pipe_resources.
1697 plane_templ
[i
].bind
|= PIPE_BIND_SHARED
;
1699 if (si_init_surface(sscreen
, &surface
[i
], &plane_templ
[i
],
1700 tile_mode
, 0, false, false,
1701 is_flushed_depth
, tc_compatible_htile
))
1704 plane_offset
[i
] = align64(total_size
, surface
[i
].surf_alignment
);
1705 total_size
= plane_offset
[i
] + surface
[i
].total_size
;
1706 max_alignment
= MAX2(max_alignment
, surface
[i
].surf_alignment
);
1709 struct si_texture
*plane0
= NULL
, *last_plane
= NULL
;
1711 for (unsigned i
= 0; i
< num_planes
; i
++) {
1712 struct si_texture
*tex
=
1713 si_texture_create_object(screen
, &plane_templ
[i
], &surface
[i
],
1714 plane0
, NULL
, plane_offset
[i
],
1715 total_size
, max_alignment
);
1717 si_texture_reference(&plane0
, NULL
);
1721 tex
->plane_index
= i
;
1722 tex
->num_planes
= num_planes
;
1725 plane0
= last_plane
= tex
;
1727 last_plane
->buffer
.b
.b
.next
= &tex
->buffer
.b
.b
;
1730 return (struct pipe_resource
*)plane0
;
1733 static struct pipe_resource
*si_texture_from_winsys_buffer(struct si_screen
*sscreen
,
1734 const struct pipe_resource
*templ
,
1735 struct pb_buffer
*buf
,
1741 enum radeon_surf_mode array_mode
;
1742 struct radeon_surf surface
= {};
1743 struct radeon_bo_metadata metadata
= {};
1744 struct si_texture
*tex
;
1748 /* Ignore metadata for non-zero planes. */
1753 sscreen
->ws
->buffer_get_metadata(buf
, &metadata
);
1754 si_get_display_metadata(sscreen
, &surface
, &metadata
,
1755 &array_mode
, &is_scanout
);
1758 * The bo metadata is unset for un-dedicated images. So we fall
1759 * back to linear. See answer to question 5 of the
1760 * VK_KHX_external_memory spec for some details.
1762 * It is possible that this case isn't going to work if the
1763 * surface pitch isn't correctly aligned by default.
1765 * In order to support it correctly we require multi-image
1766 * metadata to be syncrhonized between radv and radeonsi. The
1767 * semantics of associating multiple image metadata to a memory
1768 * object on the vulkan export side are not concretely defined
1771 * All the use cases we are aware of at the moment for memory
1772 * objects use dedicated allocations. So lets keep the initial
1773 * implementation simple.
1775 * A possible alternative is to attempt to reconstruct the
1776 * tiling information when the TexParameter TEXTURE_TILING_EXT
1779 array_mode
= RADEON_SURF_MODE_LINEAR_ALIGNED
;
1783 r
= si_init_surface(sscreen
, &surface
, templ
,
1784 array_mode
, stride
, true, is_scanout
,
1789 tex
= si_texture_create_object(&sscreen
->b
, templ
, &surface
, NULL
, buf
,
1794 tex
->buffer
.b
.is_shared
= true;
1795 tex
->buffer
.external_usage
= usage
;
1796 tex
->num_planes
= 1;
1798 if (!si_read_tex_bo_metadata(sscreen
, tex
, offset
, &metadata
)) {
1799 si_texture_reference(&tex
, NULL
);
1803 /* Displayable DCC requires an explicit flush. */
1804 if (dedicated
&& offset
== 0 &&
1805 !(usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
) &&
1806 si_has_displayable_dcc(tex
)) {
1807 /* TODO: do we need to decompress DCC? */
1808 if (si_texture_discard_dcc(sscreen
, tex
)) {
1809 /* Update BO metadata after disabling DCC. */
1810 si_set_tex_bo_metadata(sscreen
, tex
);
1814 assert(tex
->surface
.tile_swizzle
== 0);
1815 return &tex
->buffer
.b
.b
;
1818 static struct pipe_resource
*si_texture_from_handle(struct pipe_screen
*screen
,
1819 const struct pipe_resource
*templ
,
1820 struct winsys_handle
*whandle
,
1823 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
1824 struct pb_buffer
*buf
= NULL
;
1826 /* Support only 2D textures without mipmaps */
1827 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
&&
1828 templ
->target
!= PIPE_TEXTURE_2D_ARRAY
) ||
1829 templ
->last_level
!= 0)
1832 buf
= sscreen
->ws
->buffer_from_handle(sscreen
->ws
, whandle
,
1833 sscreen
->info
.max_alignment
);
1837 return si_texture_from_winsys_buffer(sscreen
, templ
, buf
,
1838 whandle
->stride
, whandle
->offset
,
1842 bool si_init_flushed_depth_texture(struct pipe_context
*ctx
,
1843 struct pipe_resource
*texture
)
1845 struct si_texture
*tex
= (struct si_texture
*)texture
;
1846 struct pipe_resource resource
;
1847 enum pipe_format pipe_format
= texture
->format
;
1849 assert(!tex
->flushed_depth_texture
);
1851 if (!tex
->can_sample_z
&& tex
->can_sample_s
) {
1852 switch (pipe_format
) {
1853 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1854 /* Save memory by not allocating the S plane. */
1855 pipe_format
= PIPE_FORMAT_Z32_FLOAT
;
1857 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1858 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1859 /* Save memory bandwidth by not copying the
1860 * stencil part during flush.
1862 * This potentially increases memory bandwidth
1863 * if an application uses both Z and S texturing
1864 * simultaneously (a flushed Z24S8 texture
1865 * would be stored compactly), but how often
1866 * does that really happen?
1868 pipe_format
= PIPE_FORMAT_Z24X8_UNORM
;
1872 } else if (!tex
->can_sample_s
&& tex
->can_sample_z
) {
1873 assert(util_format_has_stencil(util_format_description(pipe_format
)));
1875 /* DB->CB copies to an 8bpp surface don't work. */
1876 pipe_format
= PIPE_FORMAT_X24S8_UINT
;
1879 memset(&resource
, 0, sizeof(resource
));
1880 resource
.target
= texture
->target
;
1881 resource
.format
= pipe_format
;
1882 resource
.width0
= texture
->width0
;
1883 resource
.height0
= texture
->height0
;
1884 resource
.depth0
= texture
->depth0
;
1885 resource
.array_size
= texture
->array_size
;
1886 resource
.last_level
= texture
->last_level
;
1887 resource
.nr_samples
= texture
->nr_samples
;
1888 resource
.usage
= PIPE_USAGE_DEFAULT
;
1889 resource
.bind
= texture
->bind
& ~PIPE_BIND_DEPTH_STENCIL
;
1890 resource
.flags
= texture
->flags
| SI_RESOURCE_FLAG_FLUSHED_DEPTH
;
1892 tex
->flushed_depth_texture
= (struct si_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
1893 if (!tex
->flushed_depth_texture
) {
1894 PRINT_ERR("failed to create temporary texture to hold flushed depth\n");
1901 * Initialize the pipe_resource descriptor to be of the same size as the box,
1902 * which is supposed to hold a subregion of the texture "orig" at the given
1905 static void si_init_temp_resource_from_box(struct pipe_resource
*res
,
1906 struct pipe_resource
*orig
,
1907 const struct pipe_box
*box
,
1908 unsigned level
, unsigned flags
)
1910 memset(res
, 0, sizeof(*res
));
1911 res
->format
= orig
->format
;
1912 res
->width0
= box
->width
;
1913 res
->height0
= box
->height
;
1915 res
->array_size
= 1;
1916 res
->usage
= flags
& SI_RESOURCE_FLAG_TRANSFER
? PIPE_USAGE_STAGING
: PIPE_USAGE_DEFAULT
;
1919 if (flags
& SI_RESOURCE_FLAG_TRANSFER
&&
1920 util_format_is_compressed(orig
->format
)) {
1921 /* Transfer resources are allocated with linear tiling, which is
1922 * not supported for compressed formats.
1924 unsigned blocksize
=
1925 util_format_get_blocksize(orig
->format
);
1927 if (blocksize
== 8) {
1928 res
->format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1930 assert(blocksize
== 16);
1931 res
->format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1934 res
->width0
= util_format_get_nblocksx(orig
->format
, box
->width
);
1935 res
->height0
= util_format_get_nblocksy(orig
->format
, box
->height
);
1938 /* We must set the correct texture target and dimensions for a 3D box. */
1939 if (box
->depth
> 1 && util_max_layer(orig
, level
) > 0) {
1940 res
->target
= PIPE_TEXTURE_2D_ARRAY
;
1941 res
->array_size
= box
->depth
;
1943 res
->target
= PIPE_TEXTURE_2D
;
1947 static bool si_can_invalidate_texture(struct si_screen
*sscreen
,
1948 struct si_texture
*tex
,
1949 unsigned transfer_usage
,
1950 const struct pipe_box
*box
)
1952 return !tex
->buffer
.b
.is_shared
&&
1953 !(tex
->surface
.flags
& RADEON_SURF_IMPORTED
) &&
1954 !(transfer_usage
& PIPE_TRANSFER_READ
) &&
1955 tex
->buffer
.b
.b
.last_level
== 0 &&
1956 util_texrange_covers_whole_level(&tex
->buffer
.b
.b
, 0,
1957 box
->x
, box
->y
, box
->z
,
1958 box
->width
, box
->height
,
1962 static void si_texture_invalidate_storage(struct si_context
*sctx
,
1963 struct si_texture
*tex
)
1965 struct si_screen
*sscreen
= sctx
->screen
;
1967 /* There is no point in discarding depth and tiled buffers. */
1968 assert(!tex
->is_depth
);
1969 assert(tex
->surface
.is_linear
);
1971 /* Reallocate the buffer in the same pipe_resource. */
1972 si_alloc_resource(sscreen
, &tex
->buffer
);
1974 /* Initialize the CMASK base address (needed even without CMASK). */
1975 tex
->cmask_base_address_reg
=
1976 (tex
->buffer
.gpu_address
+ tex
->surface
.cmask_offset
) >> 8;
1978 p_atomic_inc(&sscreen
->dirty_tex_counter
);
1980 sctx
->num_alloc_tex_transfer_bytes
+= tex
->surface
.total_size
;
1983 static void *si_texture_transfer_map(struct pipe_context
*ctx
,
1984 struct pipe_resource
*texture
,
1987 const struct pipe_box
*box
,
1988 struct pipe_transfer
**ptransfer
)
1990 struct si_context
*sctx
= (struct si_context
*)ctx
;
1991 struct si_texture
*tex
= (struct si_texture
*)texture
;
1992 struct si_transfer
*trans
;
1993 struct si_resource
*buf
;
1994 unsigned offset
= 0;
1996 bool use_staging_texture
= false;
1998 assert(!(texture
->flags
& SI_RESOURCE_FLAG_TRANSFER
));
1999 assert(box
->width
&& box
->height
&& box
->depth
);
2001 if (tex
->is_depth
) {
2002 /* Depth textures use staging unconditionally. */
2003 use_staging_texture
= true;
2005 /* Degrade the tile mode if we get too many transfers on APUs.
2006 * On dGPUs, the staging texture is always faster.
2007 * Only count uploads that are at least 4x4 pixels large.
2009 if (!sctx
->screen
->info
.has_dedicated_vram
&&
2011 box
->width
>= 4 && box
->height
>= 4 &&
2012 p_atomic_inc_return(&tex
->num_level0_transfers
) == 10) {
2013 bool can_invalidate
=
2014 si_can_invalidate_texture(sctx
->screen
, tex
,
2017 si_reallocate_texture_inplace(sctx
, tex
,
2022 /* Tiled textures need to be converted into a linear texture for CPU
2023 * access. The staging texture is always linear and is placed in GART.
2025 * Reading from VRAM or GTT WC is slow, always use the staging
2026 * texture in this case.
2028 * Use the staging texture for uploads if the underlying BO
2031 if (!tex
->surface
.is_linear
)
2032 use_staging_texture
= true;
2033 else if (usage
& PIPE_TRANSFER_READ
)
2034 use_staging_texture
=
2035 tex
->buffer
.domains
& RADEON_DOMAIN_VRAM
||
2036 tex
->buffer
.flags
& RADEON_FLAG_GTT_WC
;
2037 /* Write & linear only: */
2038 else if (si_rings_is_buffer_referenced(sctx
, tex
->buffer
.buf
,
2039 RADEON_USAGE_READWRITE
) ||
2040 !sctx
->ws
->buffer_wait(tex
->buffer
.buf
, 0,
2041 RADEON_USAGE_READWRITE
)) {
2043 if (si_can_invalidate_texture(sctx
->screen
, tex
,
2045 si_texture_invalidate_storage(sctx
, tex
);
2047 use_staging_texture
= true;
2051 trans
= CALLOC_STRUCT(si_transfer
);
2054 pipe_resource_reference(&trans
->b
.b
.resource
, texture
);
2055 trans
->b
.b
.level
= level
;
2056 trans
->b
.b
.usage
= usage
;
2057 trans
->b
.b
.box
= *box
;
2059 if (use_staging_texture
) {
2060 struct pipe_resource resource
;
2061 struct si_texture
*staging
;
2063 si_init_temp_resource_from_box(&resource
, texture
, box
, level
,
2064 SI_RESOURCE_FLAG_TRANSFER
);
2065 resource
.usage
= (usage
& PIPE_TRANSFER_READ
) ?
2066 PIPE_USAGE_STAGING
: PIPE_USAGE_STREAM
;
2068 /* Since depth-stencil textures don't support linear tiling,
2069 * blit from ZS to color and vice versa. u_blitter will do
2070 * the packing for these formats.
2073 resource
.format
= util_blitter_get_color_format_for_zs(resource
.format
);
2075 /* Create the temporary texture. */
2076 staging
= (struct si_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
2078 PRINT_ERR("failed to create temporary texture to hold untiled copy\n");
2081 trans
->staging
= &staging
->buffer
;
2083 /* Just get the strides. */
2084 si_texture_get_offset(sctx
->screen
, staging
, 0, NULL
,
2086 &trans
->b
.b
.layer_stride
);
2088 if (usage
& PIPE_TRANSFER_READ
)
2089 si_copy_to_staging_texture(ctx
, trans
);
2091 usage
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
2093 buf
= trans
->staging
;
2095 /* the resource is mapped directly */
2096 offset
= si_texture_get_offset(sctx
->screen
, tex
, level
, box
,
2098 &trans
->b
.b
.layer_stride
);
2102 /* Always unmap texture CPU mappings on 32-bit architectures, so that
2103 * we don't run out of the CPU address space.
2105 if (sizeof(void*) == 4)
2106 usage
|= RADEON_TRANSFER_TEMPORARY
;
2108 if (!(map
= si_buffer_map_sync_with_rings(sctx
, buf
, usage
)))
2111 *ptransfer
= &trans
->b
.b
;
2112 return map
+ offset
;
2115 si_resource_reference(&trans
->staging
, NULL
);
2116 pipe_resource_reference(&trans
->b
.b
.resource
, NULL
);
2121 static void si_texture_transfer_unmap(struct pipe_context
*ctx
,
2122 struct pipe_transfer
* transfer
)
2124 struct si_context
*sctx
= (struct si_context
*)ctx
;
2125 struct si_transfer
*stransfer
= (struct si_transfer
*)transfer
;
2126 struct pipe_resource
*texture
= transfer
->resource
;
2127 struct si_texture
*tex
= (struct si_texture
*)texture
;
2129 /* Always unmap texture CPU mappings on 32-bit architectures, so that
2130 * we don't run out of the CPU address space.
2132 if (sizeof(void*) == 4) {
2133 struct si_resource
*buf
=
2134 stransfer
->staging
? stransfer
->staging
: &tex
->buffer
;
2136 sctx
->ws
->buffer_unmap(buf
->buf
);
2139 if ((transfer
->usage
& PIPE_TRANSFER_WRITE
) && stransfer
->staging
)
2140 si_copy_from_staging_texture(ctx
, stransfer
);
2142 if (stransfer
->staging
) {
2143 sctx
->num_alloc_tex_transfer_bytes
+= stransfer
->staging
->buf
->size
;
2144 si_resource_reference(&stransfer
->staging
, NULL
);
2147 /* Heuristic for {upload, draw, upload, draw, ..}:
2149 * Flush the gfx IB if we've allocated too much texture storage.
2151 * The idea is that we don't want to build IBs that use too much
2152 * memory and put pressure on the kernel memory manager and we also
2153 * want to make temporary and invalidated buffers go idle ASAP to
2154 * decrease the total memory usage or make them reusable. The memory
2155 * usage will be slightly higher than given here because of the buffer
2156 * cache in the winsys.
2158 * The result is that the kernel memory manager is never a bottleneck.
2160 if (sctx
->num_alloc_tex_transfer_bytes
> sctx
->screen
->info
.gart_size
/ 4) {
2161 si_flush_gfx_cs(sctx
, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW
, NULL
);
2162 sctx
->num_alloc_tex_transfer_bytes
= 0;
2165 pipe_resource_reference(&transfer
->resource
, NULL
);
2169 static const struct u_resource_vtbl si_texture_vtbl
=
2171 NULL
, /* get_handle */
2172 si_texture_destroy
, /* resource_destroy */
2173 si_texture_transfer_map
, /* transfer_map */
2174 u_default_transfer_flush_region
, /* transfer_flush_region */
2175 si_texture_transfer_unmap
, /* transfer_unmap */
2178 /* Return if it's allowed to reinterpret one format as another with DCC enabled.
2180 bool vi_dcc_formats_compatible(struct si_screen
*sscreen
,
2181 enum pipe_format format1
,
2182 enum pipe_format format2
)
2184 const struct util_format_description
*desc1
, *desc2
;
2186 /* No format change - exit early. */
2187 if (format1
== format2
)
2190 format1
= si_simplify_cb_format(format1
);
2191 format2
= si_simplify_cb_format(format2
);
2193 /* Check again after format adjustments. */
2194 if (format1
== format2
)
2197 desc1
= util_format_description(format1
);
2198 desc2
= util_format_description(format2
);
2200 if (desc1
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
||
2201 desc2
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
2204 /* Float and non-float are totally incompatible. */
2205 if ((desc1
->channel
[0].type
== UTIL_FORMAT_TYPE_FLOAT
) !=
2206 (desc2
->channel
[0].type
== UTIL_FORMAT_TYPE_FLOAT
))
2209 /* Channel sizes must match across DCC formats.
2210 * Comparing just the first 2 channels should be enough.
2212 if (desc1
->channel
[0].size
!= desc2
->channel
[0].size
||
2213 (desc1
->nr_channels
>= 2 &&
2214 desc1
->channel
[1].size
!= desc2
->channel
[1].size
))
2217 /* Everything below is not needed if the driver never uses the DCC
2218 * clear code with the value of 1.
2221 /* If the clear values are all 1 or all 0, this constraint can be
2223 if (vi_alpha_is_on_msb(sscreen
, format1
) != vi_alpha_is_on_msb(sscreen
, format2
))
2226 /* Channel types must match if the clear value of 1 is used.
2227 * The type categories are only float, signed, unsigned.
2228 * NORM and INT are always compatible.
2230 if (desc1
->channel
[0].type
!= desc2
->channel
[0].type
||
2231 (desc1
->nr_channels
>= 2 &&
2232 desc1
->channel
[1].type
!= desc2
->channel
[1].type
))
2238 bool vi_dcc_formats_are_incompatible(struct pipe_resource
*tex
,
2240 enum pipe_format view_format
)
2242 struct si_texture
*stex
= (struct si_texture
*)tex
;
2244 return vi_dcc_enabled(stex
, level
) &&
2245 !vi_dcc_formats_compatible((struct si_screen
*)tex
->screen
,
2246 tex
->format
, view_format
);
2249 /* This can't be merged with the above function, because
2250 * vi_dcc_formats_compatible should be called only when DCC is enabled. */
2251 void vi_disable_dcc_if_incompatible_format(struct si_context
*sctx
,
2252 struct pipe_resource
*tex
,
2254 enum pipe_format view_format
)
2256 struct si_texture
*stex
= (struct si_texture
*)tex
;
2258 if (vi_dcc_formats_are_incompatible(tex
, level
, view_format
))
2259 if (!si_texture_disable_dcc(sctx
, stex
))
2260 si_decompress_dcc(sctx
, stex
);
2263 struct pipe_surface
*si_create_surface_custom(struct pipe_context
*pipe
,
2264 struct pipe_resource
*texture
,
2265 const struct pipe_surface
*templ
,
2266 unsigned width0
, unsigned height0
,
2267 unsigned width
, unsigned height
)
2269 struct si_surface
*surface
= CALLOC_STRUCT(si_surface
);
2274 assert(templ
->u
.tex
.first_layer
<= util_max_layer(texture
, templ
->u
.tex
.level
));
2275 assert(templ
->u
.tex
.last_layer
<= util_max_layer(texture
, templ
->u
.tex
.level
));
2277 pipe_reference_init(&surface
->base
.reference
, 1);
2278 pipe_resource_reference(&surface
->base
.texture
, texture
);
2279 surface
->base
.context
= pipe
;
2280 surface
->base
.format
= templ
->format
;
2281 surface
->base
.width
= width
;
2282 surface
->base
.height
= height
;
2283 surface
->base
.u
= templ
->u
;
2285 surface
->width0
= width0
;
2286 surface
->height0
= height0
;
2288 surface
->dcc_incompatible
=
2289 texture
->target
!= PIPE_BUFFER
&&
2290 vi_dcc_formats_are_incompatible(texture
, templ
->u
.tex
.level
,
2292 return &surface
->base
;
2295 static struct pipe_surface
*si_create_surface(struct pipe_context
*pipe
,
2296 struct pipe_resource
*tex
,
2297 const struct pipe_surface
*templ
)
2299 unsigned level
= templ
->u
.tex
.level
;
2300 unsigned width
= u_minify(tex
->width0
, level
);
2301 unsigned height
= u_minify(tex
->height0
, level
);
2302 unsigned width0
= tex
->width0
;
2303 unsigned height0
= tex
->height0
;
2305 if (tex
->target
!= PIPE_BUFFER
&& templ
->format
!= tex
->format
) {
2306 const struct util_format_description
*tex_desc
2307 = util_format_description(tex
->format
);
2308 const struct util_format_description
*templ_desc
2309 = util_format_description(templ
->format
);
2311 assert(tex_desc
->block
.bits
== templ_desc
->block
.bits
);
2313 /* Adjust size of surface if and only if the block width or
2314 * height is changed. */
2315 if (tex_desc
->block
.width
!= templ_desc
->block
.width
||
2316 tex_desc
->block
.height
!= templ_desc
->block
.height
) {
2317 unsigned nblks_x
= util_format_get_nblocksx(tex
->format
, width
);
2318 unsigned nblks_y
= util_format_get_nblocksy(tex
->format
, height
);
2320 width
= nblks_x
* templ_desc
->block
.width
;
2321 height
= nblks_y
* templ_desc
->block
.height
;
2323 width0
= util_format_get_nblocksx(tex
->format
, width0
);
2324 height0
= util_format_get_nblocksy(tex
->format
, height0
);
2328 return si_create_surface_custom(pipe
, tex
, templ
,
2333 static void si_surface_destroy(struct pipe_context
*pipe
,
2334 struct pipe_surface
*surface
)
2336 pipe_resource_reference(&surface
->texture
, NULL
);
2340 unsigned si_translate_colorswap(enum pipe_format format
, bool do_endian_swap
)
2342 const struct util_format_description
*desc
= util_format_description(format
);
2344 #define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == PIPE_SWIZZLE_##swz)
2346 if (format
== PIPE_FORMAT_R11G11B10_FLOAT
) /* isn't plain */
2347 return V_028C70_SWAP_STD
;
2349 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
2352 switch (desc
->nr_channels
) {
2354 if (HAS_SWIZZLE(0,X
))
2355 return V_028C70_SWAP_STD
; /* X___ */
2356 else if (HAS_SWIZZLE(3,X
))
2357 return V_028C70_SWAP_ALT_REV
; /* ___X */
2360 if ((HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(1,Y
)) ||
2361 (HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(1,NONE
)) ||
2362 (HAS_SWIZZLE(0,NONE
) && HAS_SWIZZLE(1,Y
)))
2363 return V_028C70_SWAP_STD
; /* XY__ */
2364 else if ((HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(1,X
)) ||
2365 (HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(1,NONE
)) ||
2366 (HAS_SWIZZLE(0,NONE
) && HAS_SWIZZLE(1,X
)))
2368 return (do_endian_swap
? V_028C70_SWAP_STD
: V_028C70_SWAP_STD_REV
);
2369 else if (HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(3,Y
))
2370 return V_028C70_SWAP_ALT
; /* X__Y */
2371 else if (HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(3,X
))
2372 return V_028C70_SWAP_ALT_REV
; /* Y__X */
2375 if (HAS_SWIZZLE(0,X
))
2376 return (do_endian_swap
? V_028C70_SWAP_STD_REV
: V_028C70_SWAP_STD
);
2377 else if (HAS_SWIZZLE(0,Z
))
2378 return V_028C70_SWAP_STD_REV
; /* ZYX */
2381 /* check the middle channels, the 1st and 4th channel can be NONE */
2382 if (HAS_SWIZZLE(1,Y
) && HAS_SWIZZLE(2,Z
)) {
2383 return V_028C70_SWAP_STD
; /* XYZW */
2384 } else if (HAS_SWIZZLE(1,Z
) && HAS_SWIZZLE(2,Y
)) {
2385 return V_028C70_SWAP_STD_REV
; /* WZYX */
2386 } else if (HAS_SWIZZLE(1,Y
) && HAS_SWIZZLE(2,X
)) {
2387 return V_028C70_SWAP_ALT
; /* ZYXW */
2388 } else if (HAS_SWIZZLE(1,Z
) && HAS_SWIZZLE(2,W
)) {
2391 return V_028C70_SWAP_ALT_REV
;
2393 return (do_endian_swap
? V_028C70_SWAP_ALT
: V_028C70_SWAP_ALT_REV
);
2400 /* PIPELINE_STAT-BASED DCC ENABLEMENT FOR DISPLAYABLE SURFACES */
2402 static void vi_dcc_clean_up_context_slot(struct si_context
*sctx
,
2407 if (sctx
->dcc_stats
[slot
].query_active
)
2408 vi_separate_dcc_stop_query(sctx
,
2409 sctx
->dcc_stats
[slot
].tex
);
2411 for (i
= 0; i
< ARRAY_SIZE(sctx
->dcc_stats
[slot
].ps_stats
); i
++)
2412 if (sctx
->dcc_stats
[slot
].ps_stats
[i
]) {
2413 sctx
->b
.destroy_query(&sctx
->b
,
2414 sctx
->dcc_stats
[slot
].ps_stats
[i
]);
2415 sctx
->dcc_stats
[slot
].ps_stats
[i
] = NULL
;
2418 si_texture_reference(&sctx
->dcc_stats
[slot
].tex
, NULL
);
2422 * Return the per-context slot where DCC statistics queries for the texture live.
2424 static unsigned vi_get_context_dcc_stats_index(struct si_context
*sctx
,
2425 struct si_texture
*tex
)
2427 int i
, empty_slot
= -1;
2429 /* Remove zombie textures (textures kept alive by this array only). */
2430 for (i
= 0; i
< ARRAY_SIZE(sctx
->dcc_stats
); i
++)
2431 if (sctx
->dcc_stats
[i
].tex
&&
2432 sctx
->dcc_stats
[i
].tex
->buffer
.b
.b
.reference
.count
== 1)
2433 vi_dcc_clean_up_context_slot(sctx
, i
);
2435 /* Find the texture. */
2436 for (i
= 0; i
< ARRAY_SIZE(sctx
->dcc_stats
); i
++) {
2437 /* Return if found. */
2438 if (sctx
->dcc_stats
[i
].tex
== tex
) {
2439 sctx
->dcc_stats
[i
].last_use_timestamp
= os_time_get();
2443 /* Record the first seen empty slot. */
2444 if (empty_slot
== -1 && !sctx
->dcc_stats
[i
].tex
)
2448 /* Not found. Remove the oldest member to make space in the array. */
2449 if (empty_slot
== -1) {
2450 int oldest_slot
= 0;
2452 /* Find the oldest slot. */
2453 for (i
= 1; i
< ARRAY_SIZE(sctx
->dcc_stats
); i
++)
2454 if (sctx
->dcc_stats
[oldest_slot
].last_use_timestamp
>
2455 sctx
->dcc_stats
[i
].last_use_timestamp
)
2458 /* Clean up the oldest slot. */
2459 vi_dcc_clean_up_context_slot(sctx
, oldest_slot
);
2460 empty_slot
= oldest_slot
;
2463 /* Add the texture to the new slot. */
2464 si_texture_reference(&sctx
->dcc_stats
[empty_slot
].tex
, tex
);
2465 sctx
->dcc_stats
[empty_slot
].last_use_timestamp
= os_time_get();
2469 static struct pipe_query
*
2470 vi_create_resuming_pipestats_query(struct si_context
*sctx
)
2472 struct si_query_hw
*query
= (struct si_query_hw
*)
2473 sctx
->b
.create_query(&sctx
->b
, PIPE_QUERY_PIPELINE_STATISTICS
, 0);
2475 query
->flags
|= SI_QUERY_HW_FLAG_BEGIN_RESUMES
;
2476 return (struct pipe_query
*)query
;
2480 * Called when binding a color buffer.
2482 void vi_separate_dcc_start_query(struct si_context
*sctx
,
2483 struct si_texture
*tex
)
2485 unsigned i
= vi_get_context_dcc_stats_index(sctx
, tex
);
2487 assert(!sctx
->dcc_stats
[i
].query_active
);
2489 if (!sctx
->dcc_stats
[i
].ps_stats
[0])
2490 sctx
->dcc_stats
[i
].ps_stats
[0] = vi_create_resuming_pipestats_query(sctx
);
2492 /* begin or resume the query */
2493 sctx
->b
.begin_query(&sctx
->b
, sctx
->dcc_stats
[i
].ps_stats
[0]);
2494 sctx
->dcc_stats
[i
].query_active
= true;
2498 * Called when unbinding a color buffer.
2500 void vi_separate_dcc_stop_query(struct si_context
*sctx
,
2501 struct si_texture
*tex
)
2503 unsigned i
= vi_get_context_dcc_stats_index(sctx
, tex
);
2505 assert(sctx
->dcc_stats
[i
].query_active
);
2506 assert(sctx
->dcc_stats
[i
].ps_stats
[0]);
2508 /* pause or end the query */
2509 sctx
->b
.end_query(&sctx
->b
, sctx
->dcc_stats
[i
].ps_stats
[0]);
2510 sctx
->dcc_stats
[i
].query_active
= false;
2513 static bool vi_should_enable_separate_dcc(struct si_texture
*tex
)
2515 /* The minimum number of fullscreen draws per frame that is required
2517 return tex
->ps_draw_ratio
+ tex
->num_slow_clears
>= 5;
2520 /* Called by fast clear. */
2521 void vi_separate_dcc_try_enable(struct si_context
*sctx
,
2522 struct si_texture
*tex
)
2524 /* The intent is to use this with shared displayable back buffers,
2525 * but it's not strictly limited only to them.
2527 if (!tex
->buffer
.b
.is_shared
||
2528 !(tex
->buffer
.external_usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
) ||
2529 tex
->buffer
.b
.b
.target
!= PIPE_TEXTURE_2D
||
2530 tex
->buffer
.b
.b
.last_level
> 0 ||
2531 !tex
->surface
.dcc_size
||
2532 sctx
->screen
->debug_flags
& DBG(NO_DCC
) ||
2533 sctx
->screen
->debug_flags
& DBG(NO_DCC_FB
))
2536 assert(sctx
->chip_class
>= GFX8
);
2538 if (tex
->surface
.dcc_offset
)
2539 return; /* already enabled */
2541 /* Enable the DCC stat gathering. */
2542 if (!tex
->dcc_gather_statistics
) {
2543 tex
->dcc_gather_statistics
= true;
2544 vi_separate_dcc_start_query(sctx
, tex
);
2547 if (!vi_should_enable_separate_dcc(tex
))
2548 return; /* stats show that DCC decompression is too expensive */
2550 assert(tex
->surface
.num_dcc_levels
);
2551 assert(!tex
->dcc_separate_buffer
);
2553 si_texture_discard_cmask(sctx
->screen
, tex
);
2555 /* Get a DCC buffer. */
2556 if (tex
->last_dcc_separate_buffer
) {
2557 assert(tex
->dcc_gather_statistics
);
2558 assert(!tex
->dcc_separate_buffer
);
2559 tex
->dcc_separate_buffer
= tex
->last_dcc_separate_buffer
;
2560 tex
->last_dcc_separate_buffer
= NULL
;
2562 tex
->dcc_separate_buffer
=
2563 si_aligned_buffer_create(sctx
->b
.screen
,
2564 SI_RESOURCE_FLAG_UNMAPPABLE
,
2566 tex
->surface
.dcc_size
,
2567 tex
->surface
.dcc_alignment
);
2568 if (!tex
->dcc_separate_buffer
)
2572 /* dcc_offset is the absolute GPUVM address. */
2573 tex
->surface
.dcc_offset
= tex
->dcc_separate_buffer
->gpu_address
;
2575 /* no need to flag anything since this is called by fast clear that
2576 * flags framebuffer state
2581 * Called by pipe_context::flush_resource, the place where DCC decompression
2584 void vi_separate_dcc_process_and_reset_stats(struct pipe_context
*ctx
,
2585 struct si_texture
*tex
)
2587 struct si_context
*sctx
= (struct si_context
*)ctx
;
2588 struct pipe_query
*tmp
;
2589 unsigned i
= vi_get_context_dcc_stats_index(sctx
, tex
);
2590 bool query_active
= sctx
->dcc_stats
[i
].query_active
;
2591 bool disable
= false;
2593 if (sctx
->dcc_stats
[i
].ps_stats
[2]) {
2594 union pipe_query_result result
;
2596 /* Read the results. */
2597 struct pipe_query
*query
= sctx
->dcc_stats
[i
].ps_stats
[2];
2598 ctx
->get_query_result(ctx
, query
,
2600 si_query_buffer_reset(sctx
, &((struct si_query_hw
*)query
)->buffer
);
2602 /* Compute the approximate number of fullscreen draws. */
2603 tex
->ps_draw_ratio
=
2604 result
.pipeline_statistics
.ps_invocations
/
2605 (tex
->buffer
.b
.b
.width0
* tex
->buffer
.b
.b
.height0
);
2606 sctx
->last_tex_ps_draw_ratio
= tex
->ps_draw_ratio
;
2608 disable
= tex
->dcc_separate_buffer
&&
2609 !vi_should_enable_separate_dcc(tex
);
2612 tex
->num_slow_clears
= 0;
2614 /* stop the statistics query for ps_stats[0] */
2616 vi_separate_dcc_stop_query(sctx
, tex
);
2618 /* Move the queries in the queue by one. */
2619 tmp
= sctx
->dcc_stats
[i
].ps_stats
[2];
2620 sctx
->dcc_stats
[i
].ps_stats
[2] = sctx
->dcc_stats
[i
].ps_stats
[1];
2621 sctx
->dcc_stats
[i
].ps_stats
[1] = sctx
->dcc_stats
[i
].ps_stats
[0];
2622 sctx
->dcc_stats
[i
].ps_stats
[0] = tmp
;
2624 /* create and start a new query as ps_stats[0] */
2626 vi_separate_dcc_start_query(sctx
, tex
);
2629 assert(!tex
->last_dcc_separate_buffer
);
2630 tex
->last_dcc_separate_buffer
= tex
->dcc_separate_buffer
;
2631 tex
->dcc_separate_buffer
= NULL
;
2632 tex
->surface
.dcc_offset
= 0;
2633 /* no need to flag anything since this is called after
2634 * decompression that re-sets framebuffer state
2639 static struct pipe_memory_object
*
2640 si_memobj_from_handle(struct pipe_screen
*screen
,
2641 struct winsys_handle
*whandle
,
2644 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
2645 struct si_memory_object
*memobj
= CALLOC_STRUCT(si_memory_object
);
2646 struct pb_buffer
*buf
= NULL
;
2651 buf
= sscreen
->ws
->buffer_from_handle(sscreen
->ws
, whandle
,
2652 sscreen
->info
.max_alignment
);
2658 memobj
->b
.dedicated
= dedicated
;
2660 memobj
->stride
= whandle
->stride
;
2662 return (struct pipe_memory_object
*)memobj
;
2667 si_memobj_destroy(struct pipe_screen
*screen
,
2668 struct pipe_memory_object
*_memobj
)
2670 struct si_memory_object
*memobj
= (struct si_memory_object
*)_memobj
;
2672 pb_reference(&memobj
->buf
, NULL
);
2676 static struct pipe_resource
*
2677 si_texture_from_memobj(struct pipe_screen
*screen
,
2678 const struct pipe_resource
*templ
,
2679 struct pipe_memory_object
*_memobj
,
2682 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
2683 struct si_memory_object
*memobj
= (struct si_memory_object
*)_memobj
;
2684 struct pipe_resource
*tex
=
2685 si_texture_from_winsys_buffer(sscreen
, templ
, memobj
->buf
,
2686 memobj
->stride
, offset
,
2687 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE
|
2688 PIPE_HANDLE_USAGE_SHADER_WRITE
,
2689 memobj
->b
.dedicated
);
2693 /* si_texture_from_winsys_buffer doesn't increment refcount of
2694 * memobj->buf, so increment it here.
2696 struct pb_buffer
*buf
= NULL
;
2697 pb_reference(&buf
, memobj
->buf
);
2701 static bool si_check_resource_capability(struct pipe_screen
*screen
,
2702 struct pipe_resource
*resource
,
2705 struct si_texture
*tex
= (struct si_texture
*)resource
;
2707 /* Buffers only support the linear flag. */
2708 if (resource
->target
== PIPE_BUFFER
)
2709 return (bind
& ~PIPE_BIND_LINEAR
) == 0;
2711 if (bind
& PIPE_BIND_LINEAR
&& !tex
->surface
.is_linear
)
2714 if (bind
& PIPE_BIND_SCANOUT
&& !tex
->surface
.is_displayable
)
2717 /* TODO: PIPE_BIND_CURSOR - do we care? */
2721 void si_init_screen_texture_functions(struct si_screen
*sscreen
)
2723 sscreen
->b
.resource_from_handle
= si_texture_from_handle
;
2724 sscreen
->b
.resource_get_handle
= si_texture_get_handle
;
2725 sscreen
->b
.resource_get_param
= si_resource_get_param
;
2726 sscreen
->b
.resource_get_info
= si_texture_get_info
;
2727 sscreen
->b
.resource_from_memobj
= si_texture_from_memobj
;
2728 sscreen
->b
.memobj_create_from_handle
= si_memobj_from_handle
;
2729 sscreen
->b
.memobj_destroy
= si_memobj_destroy
;
2730 sscreen
->b
.check_resource_capability
= si_check_resource_capability
;
2733 void si_init_context_texture_functions(struct si_context
*sctx
)
2735 sctx
->b
.create_surface
= si_create_surface
;
2736 sctx
->b
.surface_destroy
= si_surface_destroy
;