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.
26 #include "drm-uapi/drm_fourcc.h"
30 #include "frontend/drm_driver.h"
31 #include "util/format/u_format.h"
32 #include "util/os_time.h"
33 #include "util/u_log.h"
34 #include "util/u_memory.h"
35 #include "util/u_pack_color.h"
36 #include "util/u_resource.h"
37 #include "util/u_surface.h"
38 #include "util/u_transfer.h"
43 #include "amd/addrlib/inc/addrinterface.h"
45 static enum radeon_surf_mode
si_choose_tiling(struct si_screen
*sscreen
,
46 const struct pipe_resource
*templ
,
47 bool tc_compatible_htile
);
49 bool si_prepare_for_dma_blit(struct si_context
*sctx
, struct si_texture
*dst
, unsigned dst_level
,
50 unsigned dstx
, unsigned dsty
, unsigned dstz
, struct si_texture
*src
,
51 unsigned src_level
, const struct pipe_box
*src_box
)
56 if (dst
->surface
.bpe
!= src
->surface
.bpe
)
59 /* MSAA: Blits don't exist in the real world. */
60 if (src
->buffer
.b
.b
.nr_samples
> 1 || dst
->buffer
.b
.b
.nr_samples
> 1)
63 /* Depth-stencil surfaces:
64 * When dst is linear, the DB->CB copy preserves HTILE.
65 * When dst is tiled, the 3D path must be used to update HTILE.
67 if (src
->is_depth
|| dst
->is_depth
)
71 * src: Use the 3D path. DCC decompression is expensive.
72 * dst: Use the 3D path to compress the pixels with DCC.
74 if (vi_dcc_enabled(src
, src_level
) || vi_dcc_enabled(dst
, dst_level
))
77 /* TMZ: mixing encrypted and non-encrypted buffer in a single command
78 * doesn't seem supported.
80 if ((src
->buffer
.flags
& RADEON_FLAG_ENCRYPTED
) !=
81 (dst
->buffer
.flags
& RADEON_FLAG_ENCRYPTED
))
85 * src: Both texture and SDMA paths need decompression. Use SDMA.
86 * dst: If overwriting the whole texture, discard CMASK and use
87 * SDMA. Otherwise, use the 3D path.
89 if (dst
->cmask_buffer
&& dst
->dirty_level_mask
& (1 << dst_level
)) {
90 /* The CMASK clear is only enabled for the first level. */
91 assert(dst_level
== 0);
92 if (!util_texrange_covers_whole_level(&dst
->buffer
.b
.b
, dst_level
, dstx
, dsty
, dstz
,
93 src_box
->width
, src_box
->height
, src_box
->depth
))
96 si_texture_discard_cmask(sctx
->screen
, dst
);
99 /* All requirements are met. Prepare textures for SDMA. */
100 if (src
->cmask_buffer
&& src
->dirty_level_mask
& (1 << src_level
))
101 sctx
->b
.flush_resource(&sctx
->b
, &src
->buffer
.b
.b
);
103 assert(!(src
->dirty_level_mask
& (1 << src_level
)));
104 assert(!(dst
->dirty_level_mask
& (1 << dst_level
)));
109 /* Same as resource_copy_region, except that both upsampling and downsampling are allowed. */
110 static void si_copy_region_with_blit(struct pipe_context
*pipe
, struct pipe_resource
*dst
,
111 unsigned dst_level
, unsigned dstx
, unsigned dsty
,
112 unsigned dstz
, struct pipe_resource
*src
, unsigned src_level
,
113 const struct pipe_box
*src_box
)
115 struct pipe_blit_info blit
;
117 memset(&blit
, 0, sizeof(blit
));
118 blit
.src
.resource
= src
;
119 blit
.src
.format
= src
->format
;
120 blit
.src
.level
= src_level
;
121 blit
.src
.box
= *src_box
;
122 blit
.dst
.resource
= dst
;
123 blit
.dst
.format
= dst
->format
;
124 blit
.dst
.level
= dst_level
;
125 blit
.dst
.box
.x
= dstx
;
126 blit
.dst
.box
.y
= dsty
;
127 blit
.dst
.box
.z
= dstz
;
128 blit
.dst
.box
.width
= src_box
->width
;
129 blit
.dst
.box
.height
= src_box
->height
;
130 blit
.dst
.box
.depth
= src_box
->depth
;
131 blit
.mask
= util_format_get_mask(dst
->format
);
132 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
135 pipe
->blit(pipe
, &blit
);
139 /* Copy from a full GPU texture to a transfer's staging one. */
140 static void si_copy_to_staging_texture(struct pipe_context
*ctx
, struct si_transfer
*stransfer
)
142 struct si_context
*sctx
= (struct si_context
*)ctx
;
143 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)stransfer
;
144 struct pipe_resource
*dst
= &stransfer
->staging
->b
.b
;
145 struct pipe_resource
*src
= transfer
->resource
;
147 if (src
->nr_samples
> 1 || ((struct si_texture
*)src
)->is_depth
) {
148 si_copy_region_with_blit(ctx
, dst
, 0, 0, 0, 0, src
, transfer
->level
, &transfer
->box
);
152 sctx
->dma_copy(ctx
, dst
, 0, 0, 0, 0, src
, transfer
->level
, &transfer
->box
);
155 /* Copy from a transfer's staging texture to a full GPU one. */
156 static void si_copy_from_staging_texture(struct pipe_context
*ctx
, struct si_transfer
*stransfer
)
158 struct si_context
*sctx
= (struct si_context
*)ctx
;
159 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)stransfer
;
160 struct pipe_resource
*dst
= transfer
->resource
;
161 struct pipe_resource
*src
= &stransfer
->staging
->b
.b
;
162 struct pipe_box sbox
;
164 u_box_3d(0, 0, 0, transfer
->box
.width
, transfer
->box
.height
, transfer
->box
.depth
, &sbox
);
166 if (dst
->nr_samples
> 1 || ((struct si_texture
*)dst
)->is_depth
) {
167 si_copy_region_with_blit(ctx
, dst
, transfer
->level
, transfer
->box
.x
, transfer
->box
.y
,
168 transfer
->box
.z
, src
, 0, &sbox
);
172 if (util_format_is_compressed(dst
->format
)) {
173 sbox
.width
= util_format_get_nblocksx(dst
->format
, sbox
.width
);
174 sbox
.height
= util_format_get_nblocksx(dst
->format
, sbox
.height
);
177 sctx
->dma_copy(ctx
, dst
, transfer
->level
, transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
, src
,
181 static unsigned si_texture_get_offset(struct si_screen
*sscreen
, struct si_texture
*tex
,
182 unsigned level
, const struct pipe_box
*box
, unsigned *stride
,
183 unsigned *layer_stride
)
185 if (sscreen
->info
.chip_class
>= GFX9
) {
186 *stride
= tex
->surface
.u
.gfx9
.surf_pitch
* tex
->surface
.bpe
;
187 *layer_stride
= tex
->surface
.u
.gfx9
.surf_slice_size
;
192 /* Each texture is an array of slices. Each slice is an array
193 * of mipmap levels. */
194 return tex
->surface
.u
.gfx9
.surf_offset
+ box
->z
* tex
->surface
.u
.gfx9
.surf_slice_size
+
195 tex
->surface
.u
.gfx9
.offset
[level
] +
196 (box
->y
/ tex
->surface
.blk_h
* tex
->surface
.u
.gfx9
.surf_pitch
+
197 box
->x
/ tex
->surface
.blk_w
) *
200 *stride
= tex
->surface
.u
.legacy
.level
[level
].nblk_x
* tex
->surface
.bpe
;
201 assert((uint64_t)tex
->surface
.u
.legacy
.level
[level
].slice_size_dw
* 4 <= UINT_MAX
);
202 *layer_stride
= (uint64_t)tex
->surface
.u
.legacy
.level
[level
].slice_size_dw
* 4;
205 return tex
->surface
.u
.legacy
.level
[level
].offset
;
207 /* Each texture is an array of mipmap levels. Each level is
208 * an array of slices. */
209 return tex
->surface
.u
.legacy
.level
[level
].offset
+
210 box
->z
* (uint64_t)tex
->surface
.u
.legacy
.level
[level
].slice_size_dw
* 4 +
211 (box
->y
/ tex
->surface
.blk_h
* tex
->surface
.u
.legacy
.level
[level
].nblk_x
+
212 box
->x
/ tex
->surface
.blk_w
) *
217 static int si_init_surface(struct si_screen
*sscreen
, struct radeon_surf
*surface
,
218 const struct pipe_resource
*ptex
, enum radeon_surf_mode array_mode
,
219 bool is_imported
, bool is_scanout
, bool is_flushed_depth
,
220 bool tc_compatible_htile
)
222 const struct util_format_description
*desc
= util_format_description(ptex
->format
);
223 bool is_depth
, is_stencil
;
225 unsigned bpe
, flags
= 0;
227 is_depth
= util_format_has_depth(desc
);
228 is_stencil
= util_format_has_stencil(desc
);
230 if (!is_flushed_depth
&& ptex
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
) {
231 bpe
= 4; /* stencil is allocated separately */
233 bpe
= util_format_get_blocksize(ptex
->format
);
234 assert(util_is_power_of_two_or_zero(bpe
));
237 if (!is_flushed_depth
&& is_depth
) {
238 flags
|= RADEON_SURF_ZBUFFER
;
240 if (sscreen
->debug_flags
& DBG(NO_HYPERZ
)) {
241 flags
|= RADEON_SURF_NO_HTILE
;
242 } else if (tc_compatible_htile
&&
243 (sscreen
->info
.chip_class
>= GFX9
|| array_mode
== RADEON_SURF_MODE_2D
)) {
244 /* TC-compatible HTILE only supports Z32_FLOAT.
245 * GFX9 also supports Z16_UNORM.
246 * On GFX8, promote Z16 to Z32. DB->CB copies will convert
247 * the format for transfers.
249 if (sscreen
->info
.chip_class
== GFX8
)
252 flags
|= RADEON_SURF_TC_COMPATIBLE_HTILE
;
256 flags
|= RADEON_SURF_SBUFFER
;
259 if (sscreen
->info
.chip_class
>= GFX8
&&
260 (ptex
->flags
& SI_RESOURCE_FLAG_DISABLE_DCC
||
261 (sscreen
->info
.chip_class
< GFX10_3
&&
262 ptex
->format
== PIPE_FORMAT_R9G9B9E5_FLOAT
) ||
263 (ptex
->nr_samples
>= 2 && !sscreen
->dcc_msaa_allowed
)))
264 flags
|= RADEON_SURF_DISABLE_DCC
;
266 /* Stoney: 128bpp MSAA textures randomly fail piglit tests with DCC. */
267 if (sscreen
->info
.family
== CHIP_STONEY
&& bpe
== 16 && ptex
->nr_samples
>= 2)
268 flags
|= RADEON_SURF_DISABLE_DCC
;
270 /* GFX8: DCC clear for 4x and 8x MSAA array textures unimplemented. */
271 if (sscreen
->info
.chip_class
== GFX8
&& ptex
->nr_storage_samples
>= 4 && ptex
->array_size
> 1)
272 flags
|= RADEON_SURF_DISABLE_DCC
;
274 /* GFX9: DCC clear for 4x and 8x MSAA textures unimplemented. */
275 if (sscreen
->info
.chip_class
== GFX9
&&
276 (ptex
->nr_storage_samples
>= 4 ||
277 (sscreen
->info
.family
== CHIP_RAVEN
&& ptex
->nr_storage_samples
>= 2 && bpe
< 4)))
278 flags
|= RADEON_SURF_DISABLE_DCC
;
280 /* TODO: GFX10: DCC causes corruption with MSAA. */
281 if (sscreen
->info
.chip_class
>= GFX10
&& ptex
->nr_storage_samples
>= 2)
282 flags
|= RADEON_SURF_DISABLE_DCC
;
284 /* Shared textures must always set up DCC.
285 * If it's not present, it will be disabled by
286 * si_get_opaque_metadata later.
288 if (!is_imported
&& (sscreen
->debug_flags
& DBG(NO_DCC
)))
289 flags
|= RADEON_SURF_DISABLE_DCC
;
292 /* This should catch bugs in gallium users setting incorrect flags. */
293 assert(ptex
->nr_samples
<= 1 && ptex
->array_size
== 1 && ptex
->depth0
== 1 &&
294 ptex
->last_level
== 0 && !(flags
& RADEON_SURF_Z_OR_SBUFFER
));
296 flags
|= RADEON_SURF_SCANOUT
;
299 if (ptex
->bind
& PIPE_BIND_SHARED
)
300 flags
|= RADEON_SURF_SHAREABLE
;
302 flags
|= RADEON_SURF_IMPORTED
| RADEON_SURF_SHAREABLE
;
303 if (sscreen
->debug_flags
& DBG(NO_FMASK
))
304 flags
|= RADEON_SURF_NO_FMASK
;
306 if (sscreen
->info
.chip_class
== GFX9
&& (ptex
->flags
& SI_RESOURCE_FLAG_FORCE_MICRO_TILE_MODE
)) {
307 flags
|= RADEON_SURF_FORCE_MICRO_TILE_MODE
;
308 surface
->micro_tile_mode
= SI_RESOURCE_FLAG_MICRO_TILE_MODE_GET(ptex
->flags
);
311 if (ptex
->flags
& SI_RESOURCE_FLAG_FORCE_MSAA_TILING
) {
312 flags
|= RADEON_SURF_FORCE_SWIZZLE_MODE
;
314 if (sscreen
->info
.chip_class
>= GFX10
)
315 surface
->u
.gfx9
.surf
.swizzle_mode
= ADDR_SW_64KB_R_X
;
318 r
= sscreen
->ws
->surface_init(sscreen
->ws
, ptex
, flags
, bpe
, array_mode
, surface
);
326 void si_eliminate_fast_color_clear(struct si_context
*sctx
, struct si_texture
*tex
,
329 struct si_screen
*sscreen
= sctx
->screen
;
330 struct pipe_context
*ctx
= &sctx
->b
;
332 if (ctx
== sscreen
->aux_context
)
333 simple_mtx_lock(&sscreen
->aux_context_lock
);
335 unsigned n
= sctx
->num_decompress_calls
;
336 ctx
->flush_resource(ctx
, &tex
->buffer
.b
.b
);
338 /* Flush only if any fast clear elimination took place. */
339 bool flushed
= false;
340 if (n
!= sctx
->num_decompress_calls
)
342 ctx
->flush(ctx
, NULL
, 0);
346 *ctx_flushed
= flushed
;
348 if (ctx
== sscreen
->aux_context
)
349 simple_mtx_unlock(&sscreen
->aux_context_lock
);
352 void si_texture_discard_cmask(struct si_screen
*sscreen
, struct si_texture
*tex
)
354 if (!tex
->cmask_buffer
)
357 assert(tex
->buffer
.b
.b
.nr_samples
<= 1);
360 tex
->cmask_base_address_reg
= tex
->buffer
.gpu_address
>> 8;
361 tex
->dirty_level_mask
= 0;
363 tex
->cb_color_info
&= ~S_028C70_FAST_CLEAR(1);
365 if (tex
->cmask_buffer
!= &tex
->buffer
)
366 si_resource_reference(&tex
->cmask_buffer
, NULL
);
368 tex
->cmask_buffer
= NULL
;
370 /* Notify all contexts about the change. */
371 p_atomic_inc(&sscreen
->dirty_tex_counter
);
372 p_atomic_inc(&sscreen
->compressed_colortex_counter
);
375 static bool si_can_disable_dcc(struct si_texture
*tex
)
377 /* We can't disable DCC if it can be written by another process. */
378 return tex
->surface
.dcc_offset
&&
379 (!tex
->buffer
.b
.is_shared
||
380 !(tex
->buffer
.external_usage
& PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE
));
383 static bool si_texture_discard_dcc(struct si_screen
*sscreen
, struct si_texture
*tex
)
385 if (!si_can_disable_dcc(tex
))
388 assert(tex
->dcc_separate_buffer
== NULL
);
391 ac_surface_zero_dcc_fields(&tex
->surface
);
393 /* Notify all contexts about the change. */
394 p_atomic_inc(&sscreen
->dirty_tex_counter
);
399 * Disable DCC for the texture. (first decompress, then discard metadata).
401 * There is unresolved multi-context synchronization issue between
402 * screen::aux_context and the current context. If applications do this with
403 * multiple contexts, it's already undefined behavior for them and we don't
404 * have to worry about that. The scenario is:
406 * If context 1 disables DCC and context 2 has queued commands that write
407 * to the texture via CB with DCC enabled, and the order of operations is
409 * context 2 queues draw calls rendering to the texture, but doesn't flush
410 * context 1 disables DCC and flushes
411 * context 1 & 2 reset descriptors and FB state
412 * context 2 flushes (new compressed tiles written by the draw calls)
413 * context 1 & 2 read garbage, because DCC is disabled, yet there are
416 * \param sctx the current context if you have one, or sscreen->aux_context
419 bool si_texture_disable_dcc(struct si_context
*sctx
, struct si_texture
*tex
)
421 struct si_screen
*sscreen
= sctx
->screen
;
423 if (!sctx
->has_graphics
)
424 return si_texture_discard_dcc(sscreen
, tex
);
426 if (!si_can_disable_dcc(tex
))
429 if (&sctx
->b
== sscreen
->aux_context
)
430 simple_mtx_lock(&sscreen
->aux_context_lock
);
432 /* Decompress DCC. */
433 si_decompress_dcc(sctx
, tex
);
434 sctx
->b
.flush(&sctx
->b
, NULL
, 0);
436 if (&sctx
->b
== sscreen
->aux_context
)
437 simple_mtx_unlock(&sscreen
->aux_context_lock
);
439 return si_texture_discard_dcc(sscreen
, tex
);
442 static void si_reallocate_texture_inplace(struct si_context
*sctx
, struct si_texture
*tex
,
443 unsigned new_bind_flag
, bool invalidate_storage
)
445 struct pipe_screen
*screen
= sctx
->b
.screen
;
446 struct si_texture
*new_tex
;
447 struct pipe_resource templ
= tex
->buffer
.b
.b
;
450 templ
.bind
|= new_bind_flag
;
452 if (tex
->buffer
.b
.is_shared
|| tex
->num_planes
> 1)
455 if (new_bind_flag
== PIPE_BIND_LINEAR
) {
456 if (tex
->surface
.is_linear
)
459 /* This fails with MSAA, depth, and compressed textures. */
460 if (si_choose_tiling(sctx
->screen
, &templ
, false) != RADEON_SURF_MODE_LINEAR_ALIGNED
)
464 new_tex
= (struct si_texture
*)screen
->resource_create(screen
, &templ
);
468 /* Copy the pixels to the new texture. */
469 if (!invalidate_storage
) {
470 for (i
= 0; i
<= templ
.last_level
; i
++) {
473 u_box_3d(0, 0, 0, u_minify(templ
.width0
, i
), u_minify(templ
.height0
, i
),
474 util_num_layers(&templ
, i
), &box
);
476 sctx
->dma_copy(&sctx
->b
, &new_tex
->buffer
.b
.b
, i
, 0, 0, 0, &tex
->buffer
.b
.b
, i
, &box
);
480 if (new_bind_flag
== PIPE_BIND_LINEAR
) {
481 si_texture_discard_cmask(sctx
->screen
, tex
);
482 si_texture_discard_dcc(sctx
->screen
, tex
);
485 /* Replace the structure fields of tex. */
486 tex
->buffer
.b
.b
.bind
= templ
.bind
;
487 pb_reference(&tex
->buffer
.buf
, new_tex
->buffer
.buf
);
488 tex
->buffer
.gpu_address
= new_tex
->buffer
.gpu_address
;
489 tex
->buffer
.vram_usage
= new_tex
->buffer
.vram_usage
;
490 tex
->buffer
.gart_usage
= new_tex
->buffer
.gart_usage
;
491 tex
->buffer
.bo_size
= new_tex
->buffer
.bo_size
;
492 tex
->buffer
.bo_alignment
= new_tex
->buffer
.bo_alignment
;
493 tex
->buffer
.domains
= new_tex
->buffer
.domains
;
494 tex
->buffer
.flags
= new_tex
->buffer
.flags
;
496 tex
->surface
= new_tex
->surface
;
497 si_texture_reference(&tex
->flushed_depth_texture
, new_tex
->flushed_depth_texture
);
499 tex
->surface
.fmask_offset
= new_tex
->surface
.fmask_offset
;
500 tex
->surface
.cmask_offset
= new_tex
->surface
.cmask_offset
;
501 tex
->cmask_base_address_reg
= new_tex
->cmask_base_address_reg
;
503 if (tex
->cmask_buffer
== &tex
->buffer
)
504 tex
->cmask_buffer
= NULL
;
506 si_resource_reference(&tex
->cmask_buffer
, NULL
);
508 if (new_tex
->cmask_buffer
== &new_tex
->buffer
)
509 tex
->cmask_buffer
= &tex
->buffer
;
511 si_resource_reference(&tex
->cmask_buffer
, new_tex
->cmask_buffer
);
513 tex
->surface
.dcc_offset
= new_tex
->surface
.dcc_offset
;
514 tex
->cb_color_info
= new_tex
->cb_color_info
;
515 memcpy(tex
->color_clear_value
, new_tex
->color_clear_value
, sizeof(tex
->color_clear_value
));
516 tex
->last_msaa_resolve_target_micro_mode
= new_tex
->last_msaa_resolve_target_micro_mode
;
518 tex
->surface
.htile_offset
= new_tex
->surface
.htile_offset
;
519 tex
->depth_clear_value
= new_tex
->depth_clear_value
;
520 tex
->dirty_level_mask
= new_tex
->dirty_level_mask
;
521 tex
->stencil_dirty_level_mask
= new_tex
->stencil_dirty_level_mask
;
522 tex
->db_render_format
= new_tex
->db_render_format
;
523 tex
->stencil_clear_value
= new_tex
->stencil_clear_value
;
524 tex
->tc_compatible_htile
= new_tex
->tc_compatible_htile
;
525 tex
->depth_cleared
= new_tex
->depth_cleared
;
526 tex
->stencil_cleared
= new_tex
->stencil_cleared
;
527 tex
->upgraded_depth
= new_tex
->upgraded_depth
;
528 tex
->db_compatible
= new_tex
->db_compatible
;
529 tex
->can_sample_z
= new_tex
->can_sample_z
;
530 tex
->can_sample_s
= new_tex
->can_sample_s
;
532 tex
->separate_dcc_dirty
= new_tex
->separate_dcc_dirty
;
533 tex
->displayable_dcc_dirty
= new_tex
->displayable_dcc_dirty
;
534 tex
->dcc_gather_statistics
= new_tex
->dcc_gather_statistics
;
535 si_resource_reference(&tex
->dcc_separate_buffer
, new_tex
->dcc_separate_buffer
);
536 si_resource_reference(&tex
->last_dcc_separate_buffer
, new_tex
->last_dcc_separate_buffer
);
538 if (new_bind_flag
== PIPE_BIND_LINEAR
) {
539 assert(!tex
->surface
.htile_offset
);
540 assert(!tex
->cmask_buffer
);
541 assert(!tex
->surface
.fmask_size
);
542 assert(!tex
->surface
.dcc_offset
);
543 assert(!tex
->is_depth
);
546 si_texture_reference(&new_tex
, NULL
);
548 p_atomic_inc(&sctx
->screen
->dirty_tex_counter
);
551 static void si_set_tex_bo_metadata(struct si_screen
*sscreen
, struct si_texture
*tex
)
553 struct pipe_resource
*res
= &tex
->buffer
.b
.b
;
554 struct radeon_bo_metadata md
;
556 memset(&md
, 0, sizeof(md
));
558 assert(tex
->dcc_separate_buffer
== NULL
);
559 assert(tex
->surface
.fmask_size
== 0);
561 static const unsigned char swizzle
[] = {PIPE_SWIZZLE_X
, PIPE_SWIZZLE_Y
, PIPE_SWIZZLE_Z
,
563 bool is_array
= util_texture_is_array(res
->target
);
566 sscreen
->make_texture_descriptor(sscreen
, tex
, true, res
->target
, res
->format
, swizzle
, 0,
567 res
->last_level
, 0, is_array
? res
->array_size
- 1 : 0,
568 res
->width0
, res
->height0
, res
->depth0
, desc
, NULL
);
569 si_set_mutable_tex_desc_fields(sscreen
, tex
, &tex
->surface
.u
.legacy
.level
[0], 0, 0,
570 tex
->surface
.blk_w
, false, false, desc
);
572 ac_surface_get_umd_metadata(&sscreen
->info
, &tex
->surface
,
573 tex
->buffer
.b
.b
.last_level
+ 1,
574 desc
, &md
.size_metadata
, md
.metadata
);
575 sscreen
->ws
->buffer_set_metadata(tex
->buffer
.buf
, &md
, &tex
->surface
);
578 static bool si_has_displayable_dcc(struct si_texture
*tex
)
580 struct si_screen
*sscreen
= (struct si_screen
*)tex
->buffer
.b
.b
.screen
;
582 if (sscreen
->info
.chip_class
<= GFX8
)
585 return tex
->surface
.is_displayable
&& tex
->surface
.dcc_offset
;
588 static bool si_resource_get_param(struct pipe_screen
*screen
, struct pipe_context
*context
,
589 struct pipe_resource
*resource
, unsigned plane
, unsigned layer
,
590 enum pipe_resource_param param
, unsigned handle_usage
,
593 for (unsigned i
= 0; i
< plane
; i
++)
594 resource
= resource
->next
;
596 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
597 struct si_texture
*tex
= (struct si_texture
*)resource
;
598 struct winsys_handle whandle
;
601 case PIPE_RESOURCE_PARAM_NPLANES
:
602 *value
= resource
->target
== PIPE_BUFFER
? 1 : tex
->num_planes
;
605 case PIPE_RESOURCE_PARAM_STRIDE
:
606 if (resource
->target
== PIPE_BUFFER
)
608 else if (sscreen
->info
.chip_class
>= GFX9
)
609 *value
= tex
->surface
.u
.gfx9
.surf_pitch
* tex
->surface
.bpe
;
611 *value
= tex
->surface
.u
.legacy
.level
[0].nblk_x
* tex
->surface
.bpe
;
614 case PIPE_RESOURCE_PARAM_OFFSET
:
615 if (resource
->target
== PIPE_BUFFER
)
617 else if (sscreen
->info
.chip_class
>= GFX9
)
618 *value
= tex
->surface
.u
.gfx9
.surf_offset
+ layer
* tex
->surface
.u
.gfx9
.surf_slice_size
;
620 *value
= tex
->surface
.u
.legacy
.level
[0].offset
+
621 layer
* (uint64_t)tex
->surface
.u
.legacy
.level
[0].slice_size_dw
* 4;
624 case PIPE_RESOURCE_PARAM_MODIFIER
:
625 *value
= DRM_FORMAT_MOD_INVALID
;
628 case PIPE_RESOURCE_PARAM_HANDLE_TYPE_SHARED
:
629 case PIPE_RESOURCE_PARAM_HANDLE_TYPE_KMS
:
630 case PIPE_RESOURCE_PARAM_HANDLE_TYPE_FD
:
631 memset(&whandle
, 0, sizeof(whandle
));
633 if (param
== PIPE_RESOURCE_PARAM_HANDLE_TYPE_SHARED
)
634 whandle
.type
= WINSYS_HANDLE_TYPE_SHARED
;
635 else if (param
== PIPE_RESOURCE_PARAM_HANDLE_TYPE_KMS
)
636 whandle
.type
= WINSYS_HANDLE_TYPE_KMS
;
637 else if (param
== PIPE_RESOURCE_PARAM_HANDLE_TYPE_FD
)
638 whandle
.type
= WINSYS_HANDLE_TYPE_FD
;
640 if (!screen
->resource_get_handle(screen
, context
, resource
, &whandle
, handle_usage
))
643 *value
= whandle
.handle
;
649 static void si_texture_get_info(struct pipe_screen
*screen
, struct pipe_resource
*resource
,
650 unsigned *pstride
, unsigned *poffset
)
655 si_resource_get_param(screen
, NULL
, resource
, 0, 0, PIPE_RESOURCE_PARAM_STRIDE
, 0, &value
);
660 si_resource_get_param(screen
, NULL
, resource
, 0, 0, PIPE_RESOURCE_PARAM_OFFSET
, 0, &value
);
665 static bool si_texture_get_handle(struct pipe_screen
*screen
, struct pipe_context
*ctx
,
666 struct pipe_resource
*resource
, struct winsys_handle
*whandle
,
669 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
670 struct si_context
*sctx
;
671 struct si_resource
*res
= si_resource(resource
);
672 struct si_texture
*tex
= (struct si_texture
*)resource
;
673 bool update_metadata
= false;
674 unsigned stride
, offset
, slice_size
;
677 ctx
= threaded_context_unwrap_sync(ctx
);
678 sctx
= (struct si_context
*)(ctx
? ctx
: sscreen
->aux_context
);
680 if (resource
->target
!= PIPE_BUFFER
) {
681 /* Individual planes are chained pipe_resource instances. */
682 for (unsigned i
= 0; i
< whandle
->plane
; i
++) {
683 resource
= resource
->next
;
684 res
= si_resource(resource
);
685 tex
= (struct si_texture
*)resource
;
688 /* This is not supported now, but it might be required for OpenCL
689 * interop in the future.
691 if (resource
->nr_samples
> 1 || tex
->is_depth
)
694 /* Move a suballocated texture into a non-suballocated allocation. */
695 if (sscreen
->ws
->buffer_is_suballocated(res
->buf
) || tex
->surface
.tile_swizzle
||
696 (tex
->buffer
.flags
& RADEON_FLAG_NO_INTERPROCESS_SHARING
&&
697 sscreen
->info
.has_local_buffers
)) {
698 assert(!res
->b
.is_shared
);
699 si_reallocate_texture_inplace(sctx
, tex
, PIPE_BIND_SHARED
, false);
701 assert(res
->b
.b
.bind
& PIPE_BIND_SHARED
);
702 assert(res
->flags
& RADEON_FLAG_NO_SUBALLOC
);
703 assert(!(res
->flags
& RADEON_FLAG_NO_INTERPROCESS_SHARING
));
704 assert(tex
->surface
.tile_swizzle
== 0);
707 /* Since shader image stores don't support DCC on GFX8,
708 * disable it for external clients that want write
711 if ((usage
& PIPE_HANDLE_USAGE_SHADER_WRITE
&& tex
->surface
.dcc_offset
) ||
712 /* Displayable DCC requires an explicit flush. */
713 (!(usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
) && si_has_displayable_dcc(tex
))) {
714 if (si_texture_disable_dcc(sctx
, tex
)) {
715 update_metadata
= true;
716 /* si_texture_disable_dcc flushes the context */
721 if (!(usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
) &&
722 (tex
->cmask_buffer
|| tex
->surface
.dcc_offset
)) {
723 /* Eliminate fast clear (both CMASK and DCC) */
725 si_eliminate_fast_color_clear(sctx
, tex
, &flushed
);
726 /* eliminate_fast_color_clear sometimes flushes the context */
730 /* Disable CMASK if flush_resource isn't going
733 if (tex
->cmask_buffer
)
734 si_texture_discard_cmask(sscreen
, tex
);
738 if ((!res
->b
.is_shared
|| update_metadata
) && whandle
->offset
== 0)
739 si_set_tex_bo_metadata(sscreen
, tex
);
741 if (sscreen
->info
.chip_class
>= GFX9
) {
742 slice_size
= tex
->surface
.u
.gfx9
.surf_slice_size
;
744 slice_size
= (uint64_t)tex
->surface
.u
.legacy
.level
[0].slice_size_dw
* 4;
747 /* Buffer exports are for the OpenCL interop. */
748 /* Move a suballocated buffer into a non-suballocated allocation. */
749 if (sscreen
->ws
->buffer_is_suballocated(res
->buf
) ||
750 /* A DMABUF export always fails if the BO is local. */
751 (tex
->buffer
.flags
& RADEON_FLAG_NO_INTERPROCESS_SHARING
&&
752 sscreen
->info
.has_local_buffers
)) {
753 assert(!res
->b
.is_shared
);
755 /* Allocate a new buffer with PIPE_BIND_SHARED. */
756 struct pipe_resource templ
= res
->b
.b
;
757 templ
.bind
|= PIPE_BIND_SHARED
;
759 struct pipe_resource
*newb
= screen
->resource_create(screen
, &templ
);
763 /* Copy the old buffer contents to the new one. */
765 u_box_1d(0, newb
->width0
, &box
);
766 sctx
->b
.resource_copy_region(&sctx
->b
, newb
, 0, 0, 0, 0, &res
->b
.b
, 0, &box
);
768 /* Move the new buffer storage to the old pipe_resource. */
769 si_replace_buffer_storage(&sctx
->b
, &res
->b
.b
, newb
);
770 pipe_resource_reference(&newb
, NULL
);
772 assert(res
->b
.b
.bind
& PIPE_BIND_SHARED
);
773 assert(res
->flags
& RADEON_FLAG_NO_SUBALLOC
);
780 si_texture_get_info(screen
, resource
, &stride
, &offset
);
783 sctx
->b
.flush(&sctx
->b
, NULL
, 0);
785 if (res
->b
.is_shared
) {
786 /* USAGE_EXPLICIT_FLUSH must be cleared if at least one user
789 res
->external_usage
|= usage
& ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
;
790 if (!(usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
))
791 res
->external_usage
&= ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
;
793 res
->b
.is_shared
= true;
794 res
->external_usage
= usage
;
797 whandle
->stride
= stride
;
798 whandle
->offset
= offset
+ slice_size
* whandle
->layer
;
800 return sscreen
->ws
->buffer_get_handle(sscreen
->ws
, res
->buf
, whandle
);
803 static void si_texture_destroy(struct pipe_screen
*screen
, struct pipe_resource
*ptex
)
805 struct si_texture
*tex
= (struct si_texture
*)ptex
;
806 struct si_resource
*resource
= &tex
->buffer
;
808 si_texture_reference(&tex
->flushed_depth_texture
, NULL
);
810 if (tex
->cmask_buffer
!= &tex
->buffer
) {
811 si_resource_reference(&tex
->cmask_buffer
, NULL
);
813 pb_reference(&resource
->buf
, NULL
);
814 si_resource_reference(&tex
->dcc_separate_buffer
, NULL
);
815 si_resource_reference(&tex
->last_dcc_separate_buffer
, NULL
);
819 static const struct u_resource_vtbl si_texture_vtbl
;
821 void si_print_texture_info(struct si_screen
*sscreen
, struct si_texture
*tex
,
822 struct u_log_context
*log
)
826 /* Common parameters. */
828 " Info: npix_x=%u, npix_y=%u, npix_z=%u, blk_w=%u, "
829 "blk_h=%u, array_size=%u, last_level=%u, "
830 "bpe=%u, nsamples=%u, flags=0x%x, %s\n",
831 tex
->buffer
.b
.b
.width0
, tex
->buffer
.b
.b
.height0
, tex
->buffer
.b
.b
.depth0
,
832 tex
->surface
.blk_w
, tex
->surface
.blk_h
, tex
->buffer
.b
.b
.array_size
,
833 tex
->buffer
.b
.b
.last_level
, tex
->surface
.bpe
, tex
->buffer
.b
.b
.nr_samples
,
834 tex
->surface
.flags
, util_format_short_name(tex
->buffer
.b
.b
.format
));
836 if (sscreen
->info
.chip_class
>= GFX9
) {
838 " Surf: size=%" PRIu64
", slice_size=%" PRIu64
", "
839 "alignment=%u, swmode=%u, epitch=%u, pitch=%u\n",
840 tex
->surface
.surf_size
, tex
->surface
.u
.gfx9
.surf_slice_size
,
841 tex
->surface
.surf_alignment
, tex
->surface
.u
.gfx9
.surf
.swizzle_mode
,
842 tex
->surface
.u
.gfx9
.surf
.epitch
, tex
->surface
.u
.gfx9
.surf_pitch
);
844 if (tex
->surface
.fmask_offset
) {
846 " FMASK: offset=%" PRIu64
", size=%" PRIu64
", "
847 "alignment=%u, swmode=%u, epitch=%u\n",
848 tex
->surface
.fmask_offset
, tex
->surface
.fmask_size
,
849 tex
->surface
.fmask_alignment
, tex
->surface
.u
.gfx9
.fmask
.swizzle_mode
,
850 tex
->surface
.u
.gfx9
.fmask
.epitch
);
853 if (tex
->cmask_buffer
) {
855 " CMask: offset=%" PRIu64
", size=%u, "
857 tex
->surface
.cmask_offset
, tex
->surface
.cmask_size
,
858 tex
->surface
.cmask_alignment
);
861 if (tex
->surface
.htile_offset
) {
863 " HTile: offset=%" PRIu64
", size=%u, alignment=%u\n",
864 tex
->surface
.htile_offset
, tex
->surface
.htile_size
,
865 tex
->surface
.htile_alignment
);
868 if (tex
->surface
.dcc_offset
) {
870 " DCC: offset=%" PRIu64
", size=%u, "
871 "alignment=%u, pitch_max=%u, num_dcc_levels=%u\n",
872 tex
->surface
.dcc_offset
, tex
->surface
.dcc_size
, tex
->surface
.dcc_alignment
,
873 tex
->surface
.u
.gfx9
.display_dcc_pitch_max
, tex
->surface
.num_dcc_levels
);
876 if (tex
->surface
.u
.gfx9
.stencil_offset
) {
877 u_log_printf(log
, " Stencil: offset=%" PRIu64
", swmode=%u, epitch=%u\n",
878 tex
->surface
.u
.gfx9
.stencil_offset
, tex
->surface
.u
.gfx9
.stencil
.swizzle_mode
,
879 tex
->surface
.u
.gfx9
.stencil
.epitch
);
885 " Layout: size=%" PRIu64
", alignment=%u, bankw=%u, "
886 "bankh=%u, nbanks=%u, mtilea=%u, tilesplit=%u, pipeconfig=%u, scanout=%u\n",
887 tex
->surface
.surf_size
, tex
->surface
.surf_alignment
, tex
->surface
.u
.legacy
.bankw
,
888 tex
->surface
.u
.legacy
.bankh
, tex
->surface
.u
.legacy
.num_banks
,
889 tex
->surface
.u
.legacy
.mtilea
, tex
->surface
.u
.legacy
.tile_split
,
890 tex
->surface
.u
.legacy
.pipe_config
, (tex
->surface
.flags
& RADEON_SURF_SCANOUT
) != 0);
892 if (tex
->surface
.fmask_offset
)
895 " FMask: offset=%" PRIu64
", size=%" PRIu64
", alignment=%u, pitch_in_pixels=%u, "
896 "bankh=%u, slice_tile_max=%u, tile_mode_index=%u\n",
897 tex
->surface
.fmask_offset
, tex
->surface
.fmask_size
, tex
->surface
.fmask_alignment
,
898 tex
->surface
.u
.legacy
.fmask
.pitch_in_pixels
, tex
->surface
.u
.legacy
.fmask
.bankh
,
899 tex
->surface
.u
.legacy
.fmask
.slice_tile_max
, tex
->surface
.u
.legacy
.fmask
.tiling_index
);
901 if (tex
->cmask_buffer
)
903 " CMask: offset=%" PRIu64
", size=%u, alignment=%u, "
904 "slice_tile_max=%u\n",
905 tex
->surface
.cmask_offset
, tex
->surface
.cmask_size
, tex
->surface
.cmask_alignment
,
906 tex
->surface
.u
.legacy
.cmask_slice_tile_max
);
908 if (tex
->surface
.htile_offset
)
910 " HTile: offset=%" PRIu64
", size=%u, "
911 "alignment=%u, TC_compatible = %u\n",
912 tex
->surface
.htile_offset
, tex
->surface
.htile_size
, tex
->surface
.htile_alignment
,
913 tex
->tc_compatible_htile
);
915 if (tex
->surface
.dcc_offset
) {
916 u_log_printf(log
, " DCC: offset=%" PRIu64
", size=%u, alignment=%u\n",
917 tex
->surface
.dcc_offset
, tex
->surface
.dcc_size
, tex
->surface
.dcc_alignment
);
918 for (i
= 0; i
<= tex
->buffer
.b
.b
.last_level
; i
++)
920 " DCCLevel[%i]: enabled=%u, offset=%u, "
921 "fast_clear_size=%u\n",
922 i
, i
< tex
->surface
.num_dcc_levels
, tex
->surface
.u
.legacy
.level
[i
].dcc_offset
,
923 tex
->surface
.u
.legacy
.level
[i
].dcc_fast_clear_size
);
926 for (i
= 0; i
<= tex
->buffer
.b
.b
.last_level
; i
++)
928 " Level[%i]: offset=%" PRIu64
", slice_size=%" PRIu64
", "
929 "npix_x=%u, npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
930 "mode=%u, tiling_index = %u\n",
931 i
, tex
->surface
.u
.legacy
.level
[i
].offset
,
932 (uint64_t)tex
->surface
.u
.legacy
.level
[i
].slice_size_dw
* 4,
933 u_minify(tex
->buffer
.b
.b
.width0
, i
), u_minify(tex
->buffer
.b
.b
.height0
, i
),
934 u_minify(tex
->buffer
.b
.b
.depth0
, i
), tex
->surface
.u
.legacy
.level
[i
].nblk_x
,
935 tex
->surface
.u
.legacy
.level
[i
].nblk_y
, tex
->surface
.u
.legacy
.level
[i
].mode
,
936 tex
->surface
.u
.legacy
.tiling_index
[i
]);
938 if (tex
->surface
.has_stencil
) {
939 u_log_printf(log
, " StencilLayout: tilesplit=%u\n",
940 tex
->surface
.u
.legacy
.stencil_tile_split
);
941 for (i
= 0; i
<= tex
->buffer
.b
.b
.last_level
; i
++) {
943 " StencilLevel[%i]: offset=%" PRIu64
", "
944 "slice_size=%" PRIu64
", npix_x=%u, "
945 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
946 "mode=%u, tiling_index = %u\n",
947 i
, tex
->surface
.u
.legacy
.stencil_level
[i
].offset
,
948 (uint64_t)tex
->surface
.u
.legacy
.stencil_level
[i
].slice_size_dw
* 4,
949 u_minify(tex
->buffer
.b
.b
.width0
, i
), u_minify(tex
->buffer
.b
.b
.height0
, i
),
950 u_minify(tex
->buffer
.b
.b
.depth0
, i
),
951 tex
->surface
.u
.legacy
.stencil_level
[i
].nblk_x
,
952 tex
->surface
.u
.legacy
.stencil_level
[i
].nblk_y
,
953 tex
->surface
.u
.legacy
.stencil_level
[i
].mode
,
954 tex
->surface
.u
.legacy
.stencil_tiling_index
[i
]);
960 * Common function for si_texture_create and si_texture_from_handle.
962 * \param screen screen
963 * \param base resource template
964 * \param surface radeon_surf
965 * \param plane0 if a non-zero plane is being created, this is the first plane
966 * \param imported_buf from si_texture_from_handle
967 * \param offset offset for non-zero planes or imported buffers
968 * \param alloc_size the size to allocate if plane0 != NULL
969 * \param alignment alignment for the allocation
971 static struct si_texture
*si_texture_create_object(struct pipe_screen
*screen
,
972 const struct pipe_resource
*base
,
973 const struct radeon_surf
*surface
,
974 const struct si_texture
*plane0
,
975 struct pb_buffer
*imported_buf
,
976 uint64_t offset
, unsigned pitch_in_bytes
,
977 uint64_t alloc_size
, unsigned alignment
)
979 struct si_texture
*tex
;
980 struct si_resource
*resource
;
981 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
983 tex
= CALLOC_STRUCT(si_texture
);
987 resource
= &tex
->buffer
;
988 resource
->b
.b
= *base
;
989 resource
->b
.vtbl
= &si_texture_vtbl
;
990 pipe_reference_init(&resource
->b
.b
.reference
, 1);
991 resource
->b
.b
.screen
= screen
;
993 /* don't include stencil-only formats which we don't support for rendering */
994 tex
->is_depth
= util_format_has_depth(util_format_description(tex
->buffer
.b
.b
.format
));
995 tex
->surface
= *surface
;
997 /* On GFX8, HTILE uses different tiling depending on the TC_COMPATIBLE_HTILE
998 * setting, so we have to enable it if we enabled it at allocation.
1000 * GFX9 and later use the same tiling for both, so TC-compatible HTILE can be
1001 * enabled on demand.
1003 tex
->tc_compatible_htile
= sscreen
->info
.chip_class
== GFX8
&&
1004 tex
->surface
.flags
& RADEON_SURF_TC_COMPATIBLE_HTILE
;
1006 /* TC-compatible HTILE:
1007 * - GFX8 only supports Z32_FLOAT.
1008 * - GFX9 only supports Z32_FLOAT and Z16_UNORM. */
1009 if (tex
->surface
.flags
& RADEON_SURF_TC_COMPATIBLE_HTILE
) {
1010 if (sscreen
->info
.chip_class
>= GFX9
&& base
->format
== PIPE_FORMAT_Z16_UNORM
)
1011 tex
->db_render_format
= base
->format
;
1013 tex
->db_render_format
= PIPE_FORMAT_Z32_FLOAT
;
1014 tex
->upgraded_depth
= base
->format
!= PIPE_FORMAT_Z32_FLOAT
&&
1015 base
->format
!= PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
;
1018 tex
->db_render_format
= base
->format
;
1021 /* Applies to GCN. */
1022 tex
->last_msaa_resolve_target_micro_mode
= tex
->surface
.micro_tile_mode
;
1024 /* Disable separate DCC at the beginning. DRI2 doesn't reuse buffers
1025 * between frames, so the only thing that can enable separate DCC
1026 * with DRI2 is multiple slow clears within a frame.
1028 tex
->ps_draw_ratio
= 0;
1030 ac_surface_override_offset_stride(&sscreen
->info
, &tex
->surface
,
1031 tex
->buffer
.b
.b
.last_level
+ 1,
1032 offset
, pitch_in_bytes
/ tex
->surface
.bpe
);
1034 if (tex
->is_depth
) {
1035 if (sscreen
->info
.chip_class
>= GFX9
) {
1036 tex
->can_sample_z
= true;
1037 tex
->can_sample_s
= true;
1039 /* Stencil texturing with HTILE doesn't work
1040 * with mipmapping on Navi10-14. */
1041 if (sscreen
->info
.chip_class
== GFX10
&& base
->last_level
> 0)
1042 tex
->htile_stencil_disabled
= true;
1044 tex
->can_sample_z
= !tex
->surface
.u
.legacy
.depth_adjusted
;
1045 tex
->can_sample_s
= !tex
->surface
.u
.legacy
.stencil_adjusted
;
1048 tex
->db_compatible
= surface
->flags
& RADEON_SURF_ZBUFFER
;
1050 if (tex
->surface
.cmask_offset
) {
1051 tex
->cb_color_info
|= S_028C70_FAST_CLEAR(1);
1052 tex
->cmask_buffer
= &tex
->buffer
;
1057 /* The buffer is shared with the first plane. */
1058 resource
->bo_size
= plane0
->buffer
.bo_size
;
1059 resource
->bo_alignment
= plane0
->buffer
.bo_alignment
;
1060 resource
->flags
= plane0
->buffer
.flags
;
1061 resource
->domains
= plane0
->buffer
.domains
;
1062 resource
->vram_usage
= plane0
->buffer
.vram_usage
;
1063 resource
->gart_usage
= plane0
->buffer
.gart_usage
;
1065 pb_reference(&resource
->buf
, plane0
->buffer
.buf
);
1066 resource
->gpu_address
= plane0
->buffer
.gpu_address
;
1067 } else if (!(surface
->flags
& RADEON_SURF_IMPORTED
)) {
1068 /* Create the backing buffer. */
1069 si_init_resource_fields(sscreen
, resource
, alloc_size
, alignment
);
1071 if (!si_alloc_resource(sscreen
, resource
))
1074 resource
->buf
= imported_buf
;
1075 resource
->gpu_address
= sscreen
->ws
->buffer_get_virtual_address(resource
->buf
);
1076 resource
->bo_size
= imported_buf
->size
;
1077 resource
->bo_alignment
= imported_buf
->alignment
;
1078 resource
->domains
= sscreen
->ws
->buffer_get_initial_domain(resource
->buf
);
1079 if (resource
->domains
& RADEON_DOMAIN_VRAM
)
1080 resource
->vram_usage
= resource
->bo_size
;
1081 else if (resource
->domains
& RADEON_DOMAIN_GTT
)
1082 resource
->gart_usage
= resource
->bo_size
;
1083 if (sscreen
->ws
->buffer_get_flags
)
1084 resource
->flags
= sscreen
->ws
->buffer_get_flags(resource
->buf
);
1087 if (tex
->cmask_buffer
) {
1088 /* Initialize the cmask to 0xCC (= compressed state). */
1089 si_screen_clear_buffer(sscreen
, &tex
->cmask_buffer
->b
.b
, tex
->surface
.cmask_offset
,
1090 tex
->surface
.cmask_size
, 0xCCCCCCCC);
1092 if (tex
->surface
.htile_offset
) {
1093 uint32_t clear_value
= 0;
1095 if (sscreen
->info
.chip_class
>= GFX9
|| tex
->tc_compatible_htile
)
1096 clear_value
= 0x0000030F;
1098 si_screen_clear_buffer(sscreen
, &tex
->buffer
.b
.b
, tex
->surface
.htile_offset
,
1099 tex
->surface
.htile_size
, clear_value
);
1102 /* Initialize DCC only if the texture is not being imported. */
1103 if (!(surface
->flags
& RADEON_SURF_IMPORTED
) && tex
->surface
.dcc_offset
) {
1104 /* Clear DCC to black for all tiles with DCC enabled.
1106 * This fixes corruption in 3DMark Slingshot Extreme, which
1107 * uses uninitialized textures, causing corruption.
1109 if (tex
->surface
.num_dcc_levels
== tex
->buffer
.b
.b
.last_level
+ 1 &&
1110 tex
->buffer
.b
.b
.nr_samples
<= 2) {
1111 /* Simple case - all tiles have DCC enabled. */
1112 si_screen_clear_buffer(sscreen
, &tex
->buffer
.b
.b
, tex
->surface
.dcc_offset
,
1113 tex
->surface
.dcc_size
, DCC_CLEAR_COLOR_0000
);
1114 } else if (sscreen
->info
.chip_class
>= GFX9
) {
1115 /* Clear to uncompressed. Clearing this to black is complicated. */
1116 si_screen_clear_buffer(sscreen
, &tex
->buffer
.b
.b
, tex
->surface
.dcc_offset
,
1117 tex
->surface
.dcc_size
, DCC_UNCOMPRESSED
);
1119 /* GFX8: Initialize mipmap levels and multisamples separately. */
1120 if (tex
->buffer
.b
.b
.nr_samples
>= 2) {
1121 /* Clearing this to black is complicated. */
1122 si_screen_clear_buffer(sscreen
, &tex
->buffer
.b
.b
, tex
->surface
.dcc_offset
,
1123 tex
->surface
.dcc_size
, DCC_UNCOMPRESSED
);
1125 /* Clear the enabled mipmap levels to black. */
1128 for (unsigned i
= 0; i
< tex
->surface
.num_dcc_levels
; i
++) {
1129 if (!tex
->surface
.u
.legacy
.level
[i
].dcc_fast_clear_size
)
1132 size
= tex
->surface
.u
.legacy
.level
[i
].dcc_offset
+
1133 tex
->surface
.u
.legacy
.level
[i
].dcc_fast_clear_size
;
1136 /* Mipmap levels with DCC. */
1138 si_screen_clear_buffer(sscreen
, &tex
->buffer
.b
.b
, tex
->surface
.dcc_offset
, size
,
1139 DCC_CLEAR_COLOR_0000
);
1141 /* Mipmap levels without DCC. */
1142 if (size
!= tex
->surface
.dcc_size
) {
1143 si_screen_clear_buffer(sscreen
, &tex
->buffer
.b
.b
, tex
->surface
.dcc_offset
+ size
,
1144 tex
->surface
.dcc_size
- size
, DCC_UNCOMPRESSED
);
1149 /* Initialize displayable DCC that requires the retile blit. */
1150 if (tex
->surface
.dcc_retile_map_offset
) {
1151 /* Uninitialized DCC can hang the display hw.
1152 * Clear to white to indicate that. */
1153 si_screen_clear_buffer(sscreen
, &tex
->buffer
.b
.b
, tex
->surface
.display_dcc_offset
,
1154 tex
->surface
.u
.gfx9
.display_dcc_size
, DCC_CLEAR_COLOR_1111
);
1156 /* Upload the DCC retile map.
1157 * Use a staging buffer for the upload, because
1158 * the buffer backing the texture is unmappable.
1160 bool use_uint16
= tex
->surface
.u
.gfx9
.dcc_retile_use_uint16
;
1161 unsigned num_elements
= tex
->surface
.u
.gfx9
.dcc_retile_num_elements
;
1162 unsigned dcc_retile_map_size
= num_elements
* (use_uint16
? 2 : 4);
1163 struct si_resource
*buf
= si_aligned_buffer_create(screen
, 0, PIPE_USAGE_STREAM
,
1164 dcc_retile_map_size
,
1165 sscreen
->info
.tcc_cache_line_size
);
1166 void *map
= sscreen
->ws
->buffer_map(buf
->buf
, NULL
, PIPE_TRANSFER_WRITE
);
1168 /* Upload the retile map into the staging buffer. */
1169 memcpy(map
, tex
->surface
.u
.gfx9
.dcc_retile_map
, dcc_retile_map_size
);
1171 /* Copy the staging buffer to the buffer backing the texture. */
1172 struct si_context
*sctx
= (struct si_context
*)sscreen
->aux_context
;
1174 assert(tex
->surface
.dcc_retile_map_offset
<= UINT_MAX
);
1175 simple_mtx_lock(&sscreen
->aux_context_lock
);
1176 si_sdma_copy_buffer(sctx
, &tex
->buffer
.b
.b
, &buf
->b
.b
, tex
->surface
.dcc_retile_map_offset
,
1177 0, buf
->b
.b
.width0
);
1178 sscreen
->aux_context
->flush(sscreen
->aux_context
, NULL
, 0);
1179 simple_mtx_unlock(&sscreen
->aux_context_lock
);
1181 si_resource_reference(&buf
, NULL
);
1185 /* Initialize the CMASK base register value. */
1186 tex
->cmask_base_address_reg
= (tex
->buffer
.gpu_address
+ tex
->surface
.cmask_offset
) >> 8;
1188 if (sscreen
->debug_flags
& DBG(VM
)) {
1190 "VM start=0x%" PRIX64
" end=0x%" PRIX64
1191 " | Texture %ix%ix%i, %i levels, %i samples, %s\n",
1192 tex
->buffer
.gpu_address
, tex
->buffer
.gpu_address
+ tex
->buffer
.buf
->size
,
1193 base
->width0
, base
->height0
, util_num_layers(base
, 0), base
->last_level
+ 1,
1194 base
->nr_samples
? base
->nr_samples
: 1, util_format_short_name(base
->format
));
1197 if (sscreen
->debug_flags
& DBG(TEX
)) {
1199 struct u_log_context log
;
1200 u_log_context_init(&log
);
1201 si_print_texture_info(sscreen
, tex
, &log
);
1202 u_log_new_page_print(&log
, stdout
);
1204 u_log_context_destroy(&log
);
1214 static enum radeon_surf_mode
si_choose_tiling(struct si_screen
*sscreen
,
1215 const struct pipe_resource
*templ
,
1216 bool tc_compatible_htile
)
1218 const struct util_format_description
*desc
= util_format_description(templ
->format
);
1219 bool force_tiling
= templ
->flags
& SI_RESOURCE_FLAG_FORCE_MSAA_TILING
;
1220 bool is_depth_stencil
= util_format_is_depth_or_stencil(templ
->format
) &&
1221 !(templ
->flags
& SI_RESOURCE_FLAG_FLUSHED_DEPTH
);
1223 /* MSAA resources must be 2D tiled. */
1224 if (templ
->nr_samples
> 1)
1225 return RADEON_SURF_MODE_2D
;
1227 /* Transfer resources should be linear. */
1228 if (templ
->flags
& SI_RESOURCE_FLAG_FORCE_LINEAR
)
1229 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1231 /* Avoid Z/S decompress blits by forcing TC-compatible HTILE on GFX8,
1232 * which requires 2D tiling.
1234 if (sscreen
->info
.chip_class
== GFX8
&& tc_compatible_htile
)
1235 return RADEON_SURF_MODE_2D
;
1237 /* Handle common candidates for the linear mode.
1238 * Compressed textures and DB surfaces must always be tiled.
1240 if (!force_tiling
&& !is_depth_stencil
&& !util_format_is_compressed(templ
->format
)) {
1241 if (sscreen
->debug_flags
& DBG(NO_TILING
))
1242 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1244 /* Tiling doesn't work with the 422 (SUBSAMPLED) formats. */
1245 if (desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
)
1246 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1248 /* Cursors are linear on AMD GCN.
1249 * (XXX double-check, maybe also use RADEON_SURF_SCANOUT) */
1250 if (templ
->bind
& PIPE_BIND_CURSOR
)
1251 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1253 if (templ
->bind
& PIPE_BIND_LINEAR
)
1254 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1256 /* Textures with a very small height are recommended to be linear. */
1257 if (templ
->target
== PIPE_TEXTURE_1D
|| templ
->target
== PIPE_TEXTURE_1D_ARRAY
||
1258 /* Only very thin and long 2D textures should benefit from
1259 * linear_aligned. */
1260 (templ
->width0
> 8 && templ
->height0
<= 2))
1261 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1263 /* Textures likely to be mapped often. */
1264 if (templ
->usage
== PIPE_USAGE_STAGING
|| templ
->usage
== PIPE_USAGE_STREAM
)
1265 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1268 /* Make small textures 1D tiled. */
1269 if (templ
->width0
<= 16 || templ
->height0
<= 16 || (sscreen
->debug_flags
& DBG(NO_2D_TILING
)))
1270 return RADEON_SURF_MODE_1D
;
1272 /* The allocator will switch to 1D if needed. */
1273 return RADEON_SURF_MODE_2D
;
1276 struct pipe_resource
*si_texture_create(struct pipe_screen
*screen
,
1277 const struct pipe_resource
*templ
)
1279 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
1280 bool is_zs
= util_format_is_depth_or_stencil(templ
->format
);
1282 if (templ
->nr_samples
>= 2) {
1283 /* This is hackish (overwriting the const pipe_resource template),
1284 * but should be harmless and gallium frontends can also see
1285 * the overriden number of samples in the created pipe_resource.
1287 if (is_zs
&& sscreen
->eqaa_force_z_samples
) {
1288 ((struct pipe_resource
*)templ
)->nr_samples
=
1289 ((struct pipe_resource
*)templ
)->nr_storage_samples
= sscreen
->eqaa_force_z_samples
;
1290 } else if (!is_zs
&& sscreen
->eqaa_force_color_samples
) {
1291 ((struct pipe_resource
*)templ
)->nr_samples
= sscreen
->eqaa_force_coverage_samples
;
1292 ((struct pipe_resource
*)templ
)->nr_storage_samples
= sscreen
->eqaa_force_color_samples
;
1296 bool is_flushed_depth
= templ
->flags
& SI_RESOURCE_FLAG_FLUSHED_DEPTH
||
1297 templ
->flags
& SI_RESOURCE_FLAG_FORCE_LINEAR
;
1298 bool tc_compatible_htile
=
1299 sscreen
->info
.chip_class
>= GFX8
&&
1300 /* There are issues with TC-compatible HTILE on Tonga (and
1301 * Iceland is the same design), and documented bug workarounds
1302 * don't help. For example, this fails:
1303 * piglit/bin/tex-miplevel-selection 'texture()' 2DShadow -auto
1305 sscreen
->info
.family
!= CHIP_TONGA
&& sscreen
->info
.family
!= CHIP_ICELAND
&&
1306 (templ
->flags
& PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY
) &&
1307 !(sscreen
->debug_flags
& DBG(NO_HYPERZ
)) && !is_flushed_depth
&&
1308 templ
->nr_samples
<= 1 && /* TC-compat HTILE is less efficient with MSAA */
1310 enum radeon_surf_mode tile_mode
= si_choose_tiling(sscreen
, templ
, tc_compatible_htile
);
1312 /* This allocates textures with multiple planes like NV12 in 1 buffer. */
1315 SI_TEXTURE_MAX_PLANES
= 3
1317 struct radeon_surf surface
[SI_TEXTURE_MAX_PLANES
] = {};
1318 struct pipe_resource plane_templ
[SI_TEXTURE_MAX_PLANES
];
1319 uint64_t plane_offset
[SI_TEXTURE_MAX_PLANES
] = {};
1320 uint64_t total_size
= 0;
1321 unsigned max_alignment
= 0;
1322 unsigned num_planes
= util_format_get_num_planes(templ
->format
);
1323 assert(num_planes
<= SI_TEXTURE_MAX_PLANES
);
1325 /* Compute texture or plane layouts and offsets. */
1326 for (unsigned i
= 0; i
< num_planes
; i
++) {
1327 plane_templ
[i
] = *templ
;
1328 plane_templ
[i
].format
= util_format_get_plane_format(templ
->format
, i
);
1329 plane_templ
[i
].width0
= util_format_get_plane_width(templ
->format
, i
, templ
->width0
);
1330 plane_templ
[i
].height0
= util_format_get_plane_height(templ
->format
, i
, templ
->height0
);
1332 /* Multi-plane allocations need PIPE_BIND_SHARED, because we can't
1333 * reallocate the storage to add PIPE_BIND_SHARED, because it's
1334 * shared by 3 pipe_resources.
1337 plane_templ
[i
].bind
|= PIPE_BIND_SHARED
;
1339 if (si_init_surface(sscreen
, &surface
[i
], &plane_templ
[i
], tile_mode
, false,
1340 plane_templ
[i
].bind
& PIPE_BIND_SCANOUT
, is_flushed_depth
,
1341 tc_compatible_htile
))
1344 plane_offset
[i
] = align64(total_size
, surface
[i
].surf_alignment
);
1345 total_size
= plane_offset
[i
] + surface
[i
].total_size
;
1346 max_alignment
= MAX2(max_alignment
, surface
[i
].surf_alignment
);
1349 struct si_texture
*plane0
= NULL
, *last_plane
= NULL
;
1351 for (unsigned i
= 0; i
< num_planes
; i
++) {
1352 struct si_texture
*tex
=
1353 si_texture_create_object(screen
, &plane_templ
[i
], &surface
[i
], plane0
, NULL
,
1354 plane_offset
[i
], 0, total_size
, max_alignment
);
1356 si_texture_reference(&plane0
, NULL
);
1360 tex
->plane_index
= i
;
1361 tex
->num_planes
= num_planes
;
1364 plane0
= last_plane
= tex
;
1366 last_plane
->buffer
.b
.b
.next
= &tex
->buffer
.b
.b
;
1371 return (struct pipe_resource
*)plane0
;
1374 static struct pipe_resource
*si_texture_from_winsys_buffer(struct si_screen
*sscreen
,
1375 const struct pipe_resource
*templ
,
1376 struct pb_buffer
*buf
, unsigned stride
,
1377 uint64_t offset
, unsigned usage
,
1380 struct radeon_surf surface
= {};
1381 struct radeon_bo_metadata metadata
= {};
1382 struct si_texture
*tex
;
1385 /* Ignore metadata for non-zero planes. */
1390 sscreen
->ws
->buffer_get_metadata(buf
, &metadata
, &surface
);
1393 * The bo metadata is unset for un-dedicated images. So we fall
1394 * back to linear. See answer to question 5 of the
1395 * VK_KHX_external_memory spec for some details.
1397 * It is possible that this case isn't going to work if the
1398 * surface pitch isn't correctly aligned by default.
1400 * In order to support it correctly we require multi-image
1401 * metadata to be syncrhonized between radv and radeonsi. The
1402 * semantics of associating multiple image metadata to a memory
1403 * object on the vulkan export side are not concretely defined
1406 * All the use cases we are aware of at the moment for memory
1407 * objects use dedicated allocations. So lets keep the initial
1408 * implementation simple.
1410 * A possible alternative is to attempt to reconstruct the
1411 * tiling information when the TexParameter TEXTURE_TILING_EXT
1414 metadata
.mode
= RADEON_SURF_MODE_LINEAR_ALIGNED
;
1417 r
= si_init_surface(sscreen
, &surface
, templ
, metadata
.mode
, true,
1418 surface
.flags
& RADEON_SURF_SCANOUT
, false, false);
1422 tex
= si_texture_create_object(&sscreen
->b
, templ
, &surface
, NULL
, buf
,
1423 offset
, stride
, 0, 0);
1427 tex
->buffer
.b
.is_shared
= true;
1428 tex
->buffer
.external_usage
= usage
;
1429 tex
->num_planes
= 1;
1431 /* Account for multiple planes with lowered yuv import. */
1432 struct pipe_resource
*next_plane
= tex
->buffer
.b
.b
.next
;
1434 struct si_texture
*next_tex
= (struct si_texture
*)next_plane
;
1435 ++next_tex
->num_planes
;
1437 next_plane
= next_plane
->next
;
1440 if (!ac_surface_set_umd_metadata(&sscreen
->info
, &tex
->surface
,
1441 tex
->buffer
.b
.b
.nr_storage_samples
,
1442 tex
->buffer
.b
.b
.last_level
+ 1,
1443 metadata
.size_metadata
,
1444 metadata
.metadata
)) {
1445 si_texture_reference(&tex
, NULL
);
1449 /* Displayable DCC requires an explicit flush. */
1450 if (dedicated
&& offset
== 0 && !(usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
) &&
1451 si_has_displayable_dcc(tex
)) {
1452 /* TODO: do we need to decompress DCC? */
1453 if (si_texture_discard_dcc(sscreen
, tex
)) {
1454 /* Update BO metadata after disabling DCC. */
1455 si_set_tex_bo_metadata(sscreen
, tex
);
1459 assert(tex
->surface
.tile_swizzle
== 0);
1460 return &tex
->buffer
.b
.b
;
1463 static struct pipe_resource
*si_texture_from_handle(struct pipe_screen
*screen
,
1464 const struct pipe_resource
*templ
,
1465 struct winsys_handle
*whandle
, unsigned usage
)
1467 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
1468 struct pb_buffer
*buf
= NULL
;
1470 /* Support only 2D textures without mipmaps */
1471 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
&&
1472 templ
->target
!= PIPE_TEXTURE_2D_ARRAY
) ||
1473 templ
->last_level
!= 0)
1476 buf
= sscreen
->ws
->buffer_from_handle(sscreen
->ws
, whandle
, sscreen
->info
.max_alignment
);
1480 return si_texture_from_winsys_buffer(sscreen
, templ
, buf
, whandle
->stride
, whandle
->offset
,
1484 bool si_init_flushed_depth_texture(struct pipe_context
*ctx
, struct pipe_resource
*texture
)
1486 struct si_texture
*tex
= (struct si_texture
*)texture
;
1487 struct pipe_resource resource
;
1488 enum pipe_format pipe_format
= texture
->format
;
1490 assert(!tex
->flushed_depth_texture
);
1492 if (!tex
->can_sample_z
&& tex
->can_sample_s
) {
1493 switch (pipe_format
) {
1494 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1495 /* Save memory by not allocating the S plane. */
1496 pipe_format
= PIPE_FORMAT_Z32_FLOAT
;
1498 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1499 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1500 /* Save memory bandwidth by not copying the
1501 * stencil part during flush.
1503 * This potentially increases memory bandwidth
1504 * if an application uses both Z and S texturing
1505 * simultaneously (a flushed Z24S8 texture
1506 * would be stored compactly), but how often
1507 * does that really happen?
1509 pipe_format
= PIPE_FORMAT_Z24X8_UNORM
;
1513 } else if (!tex
->can_sample_s
&& tex
->can_sample_z
) {
1514 assert(util_format_has_stencil(util_format_description(pipe_format
)));
1516 /* DB->CB copies to an 8bpp surface don't work. */
1517 pipe_format
= PIPE_FORMAT_X24S8_UINT
;
1520 memset(&resource
, 0, sizeof(resource
));
1521 resource
.target
= texture
->target
;
1522 resource
.format
= pipe_format
;
1523 resource
.width0
= texture
->width0
;
1524 resource
.height0
= texture
->height0
;
1525 resource
.depth0
= texture
->depth0
;
1526 resource
.array_size
= texture
->array_size
;
1527 resource
.last_level
= texture
->last_level
;
1528 resource
.nr_samples
= texture
->nr_samples
;
1529 resource
.usage
= PIPE_USAGE_DEFAULT
;
1530 resource
.bind
= texture
->bind
& ~PIPE_BIND_DEPTH_STENCIL
;
1531 resource
.flags
= texture
->flags
| SI_RESOURCE_FLAG_FLUSHED_DEPTH
;
1533 tex
->flushed_depth_texture
=
1534 (struct si_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
1535 if (!tex
->flushed_depth_texture
) {
1536 PRINT_ERR("failed to create temporary texture to hold flushed depth\n");
1543 * Initialize the pipe_resource descriptor to be of the same size as the box,
1544 * which is supposed to hold a subregion of the texture "orig" at the given
1547 static void si_init_temp_resource_from_box(struct pipe_resource
*res
, struct pipe_resource
*orig
,
1548 const struct pipe_box
*box
, unsigned level
,
1549 unsigned usage
, unsigned flags
)
1551 memset(res
, 0, sizeof(*res
));
1552 res
->format
= orig
->format
;
1553 res
->width0
= box
->width
;
1554 res
->height0
= box
->height
;
1556 res
->array_size
= 1;
1560 if (flags
& SI_RESOURCE_FLAG_FORCE_LINEAR
&& util_format_is_compressed(orig
->format
)) {
1561 /* Transfer resources are allocated with linear tiling, which is
1562 * not supported for compressed formats.
1564 unsigned blocksize
= util_format_get_blocksize(orig
->format
);
1566 if (blocksize
== 8) {
1567 res
->format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1569 assert(blocksize
== 16);
1570 res
->format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1573 res
->width0
= util_format_get_nblocksx(orig
->format
, box
->width
);
1574 res
->height0
= util_format_get_nblocksy(orig
->format
, box
->height
);
1577 /* We must set the correct texture target and dimensions for a 3D box. */
1578 if (box
->depth
> 1 && util_max_layer(orig
, level
) > 0) {
1579 res
->target
= PIPE_TEXTURE_2D_ARRAY
;
1580 res
->array_size
= box
->depth
;
1582 res
->target
= PIPE_TEXTURE_2D
;
1586 static bool si_can_invalidate_texture(struct si_screen
*sscreen
, struct si_texture
*tex
,
1587 unsigned transfer_usage
, const struct pipe_box
*box
)
1589 return !tex
->buffer
.b
.is_shared
&& !(tex
->surface
.flags
& RADEON_SURF_IMPORTED
) &&
1590 !(transfer_usage
& PIPE_TRANSFER_READ
) && tex
->buffer
.b
.b
.last_level
== 0 &&
1591 util_texrange_covers_whole_level(&tex
->buffer
.b
.b
, 0, box
->x
, box
->y
, box
->z
, box
->width
,
1592 box
->height
, box
->depth
);
1595 static void si_texture_invalidate_storage(struct si_context
*sctx
, struct si_texture
*tex
)
1597 struct si_screen
*sscreen
= sctx
->screen
;
1599 /* There is no point in discarding depth and tiled buffers. */
1600 assert(!tex
->is_depth
);
1601 assert(tex
->surface
.is_linear
);
1603 /* Reallocate the buffer in the same pipe_resource. */
1604 si_alloc_resource(sscreen
, &tex
->buffer
);
1606 /* Initialize the CMASK base address (needed even without CMASK). */
1607 tex
->cmask_base_address_reg
= (tex
->buffer
.gpu_address
+ tex
->surface
.cmask_offset
) >> 8;
1609 p_atomic_inc(&sscreen
->dirty_tex_counter
);
1611 sctx
->num_alloc_tex_transfer_bytes
+= tex
->surface
.total_size
;
1614 static void *si_texture_transfer_map(struct pipe_context
*ctx
, struct pipe_resource
*texture
,
1615 unsigned level
, unsigned usage
, const struct pipe_box
*box
,
1616 struct pipe_transfer
**ptransfer
)
1618 struct si_context
*sctx
= (struct si_context
*)ctx
;
1619 struct si_texture
*tex
= (struct si_texture
*)texture
;
1620 struct si_transfer
*trans
;
1621 struct si_resource
*buf
;
1622 unsigned offset
= 0;
1624 bool use_staging_texture
= false;
1626 assert(!(texture
->flags
& SI_RESOURCE_FLAG_FORCE_LINEAR
));
1627 assert(box
->width
&& box
->height
&& box
->depth
);
1629 if (tex
->is_depth
) {
1630 /* Depth textures use staging unconditionally. */
1631 use_staging_texture
= true;
1633 /* Degrade the tile mode if we get too many transfers on APUs.
1634 * On dGPUs, the staging texture is always faster.
1635 * Only count uploads that are at least 4x4 pixels large.
1637 if (!sctx
->screen
->info
.has_dedicated_vram
&& level
== 0 && box
->width
>= 4 &&
1638 box
->height
>= 4 && p_atomic_inc_return(&tex
->num_level0_transfers
) == 10) {
1639 bool can_invalidate
= si_can_invalidate_texture(sctx
->screen
, tex
, usage
, box
);
1641 si_reallocate_texture_inplace(sctx
, tex
, PIPE_BIND_LINEAR
, can_invalidate
);
1644 /* Tiled textures need to be converted into a linear texture for CPU
1645 * access. The staging texture is always linear and is placed in GART.
1647 * Reading from VRAM or GTT WC is slow, always use the staging
1648 * texture in this case.
1650 * Use the staging texture for uploads if the underlying BO
1653 if (!tex
->surface
.is_linear
|| (tex
->buffer
.flags
& RADEON_FLAG_ENCRYPTED
))
1654 use_staging_texture
= true;
1655 else if (usage
& PIPE_TRANSFER_READ
)
1656 use_staging_texture
=
1657 tex
->buffer
.domains
& RADEON_DOMAIN_VRAM
|| tex
->buffer
.flags
& RADEON_FLAG_GTT_WC
;
1658 /* Write & linear only: */
1659 else if (si_rings_is_buffer_referenced(sctx
, tex
->buffer
.buf
, RADEON_USAGE_READWRITE
) ||
1660 !sctx
->ws
->buffer_wait(tex
->buffer
.buf
, 0, RADEON_USAGE_READWRITE
)) {
1662 if (si_can_invalidate_texture(sctx
->screen
, tex
, usage
, box
))
1663 si_texture_invalidate_storage(sctx
, tex
);
1665 use_staging_texture
= true;
1669 trans
= CALLOC_STRUCT(si_transfer
);
1672 pipe_resource_reference(&trans
->b
.b
.resource
, texture
);
1673 trans
->b
.b
.level
= level
;
1674 trans
->b
.b
.usage
= usage
;
1675 trans
->b
.b
.box
= *box
;
1677 if (use_staging_texture
) {
1678 struct pipe_resource resource
;
1679 struct si_texture
*staging
;
1680 unsigned bo_usage
= usage
& PIPE_TRANSFER_READ
? PIPE_USAGE_STAGING
: PIPE_USAGE_STREAM
;
1681 unsigned bo_flags
= SI_RESOURCE_FLAG_FORCE_LINEAR
;
1683 /* The pixel shader has a bad access pattern for linear textures.
1684 * If a pixel shader is used to blit to/from staging, don't disable caches.
1686 * MSAA, depth/stencil textures, and compressed textures use the pixel shader
1689 if (texture
->nr_samples
<= 1 &&
1691 !util_format_is_compressed(texture
->format
) &&
1692 /* Texture uploads with DCC use the pixel shader to blit */
1693 (!(usage
& PIPE_TRANSFER_WRITE
) || !vi_dcc_enabled(tex
, level
)))
1694 bo_flags
|= SI_RESOURCE_FLAG_UNCACHED
;
1696 si_init_temp_resource_from_box(&resource
, texture
, box
, level
, bo_usage
,
1699 /* Since depth-stencil textures don't support linear tiling,
1700 * blit from ZS to color and vice versa. u_blitter will do
1701 * the packing for these formats.
1704 resource
.format
= util_blitter_get_color_format_for_zs(resource
.format
);
1706 /* Create the temporary texture. */
1707 staging
= (struct si_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
1709 PRINT_ERR("failed to create temporary texture to hold untiled copy\n");
1712 trans
->staging
= &staging
->buffer
;
1714 /* Just get the strides. */
1715 si_texture_get_offset(sctx
->screen
, staging
, 0, NULL
, &trans
->b
.b
.stride
,
1716 &trans
->b
.b
.layer_stride
);
1718 if (usage
& PIPE_TRANSFER_READ
)
1719 si_copy_to_staging_texture(ctx
, trans
);
1721 usage
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
1723 buf
= trans
->staging
;
1725 /* the resource is mapped directly */
1726 offset
= si_texture_get_offset(sctx
->screen
, tex
, level
, box
, &trans
->b
.b
.stride
,
1727 &trans
->b
.b
.layer_stride
);
1731 /* Always unmap texture CPU mappings on 32-bit architectures, so that
1732 * we don't run out of the CPU address space.
1734 if (sizeof(void *) == 4)
1735 usage
|= RADEON_TRANSFER_TEMPORARY
;
1737 if (!(map
= si_buffer_map_sync_with_rings(sctx
, buf
, usage
)))
1740 *ptransfer
= &trans
->b
.b
;
1741 return map
+ offset
;
1744 si_resource_reference(&trans
->staging
, NULL
);
1745 pipe_resource_reference(&trans
->b
.b
.resource
, NULL
);
1750 static void si_texture_transfer_unmap(struct pipe_context
*ctx
, struct pipe_transfer
*transfer
)
1752 struct si_context
*sctx
= (struct si_context
*)ctx
;
1753 struct si_transfer
*stransfer
= (struct si_transfer
*)transfer
;
1754 struct pipe_resource
*texture
= transfer
->resource
;
1755 struct si_texture
*tex
= (struct si_texture
*)texture
;
1757 /* Always unmap texture CPU mappings on 32-bit architectures, so that
1758 * we don't run out of the CPU address space.
1760 if (sizeof(void *) == 4) {
1761 struct si_resource
*buf
= stransfer
->staging
? stransfer
->staging
: &tex
->buffer
;
1763 sctx
->ws
->buffer_unmap(buf
->buf
);
1766 if ((transfer
->usage
& PIPE_TRANSFER_WRITE
) && stransfer
->staging
)
1767 si_copy_from_staging_texture(ctx
, stransfer
);
1769 if (stransfer
->staging
) {
1770 sctx
->num_alloc_tex_transfer_bytes
+= stransfer
->staging
->buf
->size
;
1771 si_resource_reference(&stransfer
->staging
, NULL
);
1774 /* Heuristic for {upload, draw, upload, draw, ..}:
1776 * Flush the gfx IB if we've allocated too much texture storage.
1778 * The idea is that we don't want to build IBs that use too much
1779 * memory and put pressure on the kernel memory manager and we also
1780 * want to make temporary and invalidated buffers go idle ASAP to
1781 * decrease the total memory usage or make them reusable. The memory
1782 * usage will be slightly higher than given here because of the buffer
1783 * cache in the winsys.
1785 * The result is that the kernel memory manager is never a bottleneck.
1787 if (sctx
->num_alloc_tex_transfer_bytes
> sctx
->screen
->info
.gart_size
/ 4) {
1788 si_flush_gfx_cs(sctx
, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW
, NULL
);
1789 sctx
->num_alloc_tex_transfer_bytes
= 0;
1792 pipe_resource_reference(&transfer
->resource
, NULL
);
1796 static const struct u_resource_vtbl si_texture_vtbl
= {
1797 NULL
, /* get_handle */
1798 si_texture_destroy
, /* resource_destroy */
1799 si_texture_transfer_map
, /* transfer_map */
1800 u_default_transfer_flush_region
, /* transfer_flush_region */
1801 si_texture_transfer_unmap
, /* transfer_unmap */
1804 /* Return if it's allowed to reinterpret one format as another with DCC enabled.
1806 bool vi_dcc_formats_compatible(struct si_screen
*sscreen
, enum pipe_format format1
,
1807 enum pipe_format format2
)
1809 const struct util_format_description
*desc1
, *desc2
;
1811 /* No format change - exit early. */
1812 if (format1
== format2
)
1815 format1
= si_simplify_cb_format(format1
);
1816 format2
= si_simplify_cb_format(format2
);
1818 /* Check again after format adjustments. */
1819 if (format1
== format2
)
1822 desc1
= util_format_description(format1
);
1823 desc2
= util_format_description(format2
);
1825 if (desc1
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
|| desc2
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1828 /* Float and non-float are totally incompatible. */
1829 if ((desc1
->channel
[0].type
== UTIL_FORMAT_TYPE_FLOAT
) !=
1830 (desc2
->channel
[0].type
== UTIL_FORMAT_TYPE_FLOAT
))
1833 /* Channel sizes must match across DCC formats.
1834 * Comparing just the first 2 channels should be enough.
1836 if (desc1
->channel
[0].size
!= desc2
->channel
[0].size
||
1837 (desc1
->nr_channels
>= 2 && desc1
->channel
[1].size
!= desc2
->channel
[1].size
))
1840 /* Everything below is not needed if the driver never uses the DCC
1841 * clear code with the value of 1.
1844 /* If the clear values are all 1 or all 0, this constraint can be
1846 if (vi_alpha_is_on_msb(sscreen
, format1
) != vi_alpha_is_on_msb(sscreen
, format2
))
1849 /* Channel types must match if the clear value of 1 is used.
1850 * The type categories are only float, signed, unsigned.
1851 * NORM and INT are always compatible.
1853 if (desc1
->channel
[0].type
!= desc2
->channel
[0].type
||
1854 (desc1
->nr_channels
>= 2 && desc1
->channel
[1].type
!= desc2
->channel
[1].type
))
1860 bool vi_dcc_formats_are_incompatible(struct pipe_resource
*tex
, unsigned level
,
1861 enum pipe_format view_format
)
1863 struct si_texture
*stex
= (struct si_texture
*)tex
;
1865 return vi_dcc_enabled(stex
, level
) &&
1866 !vi_dcc_formats_compatible((struct si_screen
*)tex
->screen
, tex
->format
, view_format
);
1869 /* This can't be merged with the above function, because
1870 * vi_dcc_formats_compatible should be called only when DCC is enabled. */
1871 void vi_disable_dcc_if_incompatible_format(struct si_context
*sctx
, struct pipe_resource
*tex
,
1872 unsigned level
, enum pipe_format view_format
)
1874 struct si_texture
*stex
= (struct si_texture
*)tex
;
1876 if (vi_dcc_formats_are_incompatible(tex
, level
, view_format
))
1877 if (!si_texture_disable_dcc(sctx
, stex
))
1878 si_decompress_dcc(sctx
, stex
);
1881 struct pipe_surface
*si_create_surface_custom(struct pipe_context
*pipe
,
1882 struct pipe_resource
*texture
,
1883 const struct pipe_surface
*templ
, unsigned width0
,
1884 unsigned height0
, unsigned width
, unsigned height
)
1886 struct si_surface
*surface
= CALLOC_STRUCT(si_surface
);
1891 assert(templ
->u
.tex
.first_layer
<= util_max_layer(texture
, templ
->u
.tex
.level
));
1892 assert(templ
->u
.tex
.last_layer
<= util_max_layer(texture
, templ
->u
.tex
.level
));
1894 pipe_reference_init(&surface
->base
.reference
, 1);
1895 pipe_resource_reference(&surface
->base
.texture
, texture
);
1896 surface
->base
.context
= pipe
;
1897 surface
->base
.format
= templ
->format
;
1898 surface
->base
.width
= width
;
1899 surface
->base
.height
= height
;
1900 surface
->base
.u
= templ
->u
;
1902 surface
->width0
= width0
;
1903 surface
->height0
= height0
;
1905 surface
->dcc_incompatible
=
1906 texture
->target
!= PIPE_BUFFER
&&
1907 vi_dcc_formats_are_incompatible(texture
, templ
->u
.tex
.level
, templ
->format
);
1908 return &surface
->base
;
1911 static struct pipe_surface
*si_create_surface(struct pipe_context
*pipe
, struct pipe_resource
*tex
,
1912 const struct pipe_surface
*templ
)
1914 unsigned level
= templ
->u
.tex
.level
;
1915 unsigned width
= u_minify(tex
->width0
, level
);
1916 unsigned height
= u_minify(tex
->height0
, level
);
1917 unsigned width0
= tex
->width0
;
1918 unsigned height0
= tex
->height0
;
1920 if (tex
->target
!= PIPE_BUFFER
&& templ
->format
!= tex
->format
) {
1921 const struct util_format_description
*tex_desc
= util_format_description(tex
->format
);
1922 const struct util_format_description
*templ_desc
= util_format_description(templ
->format
);
1924 assert(tex_desc
->block
.bits
== templ_desc
->block
.bits
);
1926 /* Adjust size of surface if and only if the block width or
1927 * height is changed. */
1928 if (tex_desc
->block
.width
!= templ_desc
->block
.width
||
1929 tex_desc
->block
.height
!= templ_desc
->block
.height
) {
1930 unsigned nblks_x
= util_format_get_nblocksx(tex
->format
, width
);
1931 unsigned nblks_y
= util_format_get_nblocksy(tex
->format
, height
);
1933 width
= nblks_x
* templ_desc
->block
.width
;
1934 height
= nblks_y
* templ_desc
->block
.height
;
1936 width0
= util_format_get_nblocksx(tex
->format
, width0
);
1937 height0
= util_format_get_nblocksy(tex
->format
, height0
);
1941 return si_create_surface_custom(pipe
, tex
, templ
, width0
, height0
, width
, height
);
1944 static void si_surface_destroy(struct pipe_context
*pipe
, struct pipe_surface
*surface
)
1946 pipe_resource_reference(&surface
->texture
, NULL
);
1950 unsigned si_translate_colorswap(enum pipe_format format
, bool do_endian_swap
)
1952 const struct util_format_description
*desc
= util_format_description(format
);
1954 #define HAS_SWIZZLE(chan, swz) (desc->swizzle[chan] == PIPE_SWIZZLE_##swz)
1956 if (format
== PIPE_FORMAT_R11G11B10_FLOAT
) /* isn't plain */
1957 return V_028C70_SWAP_STD
;
1959 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1962 switch (desc
->nr_channels
) {
1964 if (HAS_SWIZZLE(0, X
))
1965 return V_028C70_SWAP_STD
; /* X___ */
1966 else if (HAS_SWIZZLE(3, X
))
1967 return V_028C70_SWAP_ALT_REV
; /* ___X */
1970 if ((HAS_SWIZZLE(0, X
) && HAS_SWIZZLE(1, Y
)) || (HAS_SWIZZLE(0, X
) && HAS_SWIZZLE(1, NONE
)) ||
1971 (HAS_SWIZZLE(0, NONE
) && HAS_SWIZZLE(1, Y
)))
1972 return V_028C70_SWAP_STD
; /* XY__ */
1973 else if ((HAS_SWIZZLE(0, Y
) && HAS_SWIZZLE(1, X
)) ||
1974 (HAS_SWIZZLE(0, Y
) && HAS_SWIZZLE(1, NONE
)) ||
1975 (HAS_SWIZZLE(0, NONE
) && HAS_SWIZZLE(1, X
)))
1977 return (do_endian_swap
? V_028C70_SWAP_STD
: V_028C70_SWAP_STD_REV
);
1978 else if (HAS_SWIZZLE(0, X
) && HAS_SWIZZLE(3, Y
))
1979 return V_028C70_SWAP_ALT
; /* X__Y */
1980 else if (HAS_SWIZZLE(0, Y
) && HAS_SWIZZLE(3, X
))
1981 return V_028C70_SWAP_ALT_REV
; /* Y__X */
1984 if (HAS_SWIZZLE(0, X
))
1985 return (do_endian_swap
? V_028C70_SWAP_STD_REV
: V_028C70_SWAP_STD
);
1986 else if (HAS_SWIZZLE(0, Z
))
1987 return V_028C70_SWAP_STD_REV
; /* ZYX */
1990 /* check the middle channels, the 1st and 4th channel can be NONE */
1991 if (HAS_SWIZZLE(1, Y
) && HAS_SWIZZLE(2, Z
)) {
1992 return V_028C70_SWAP_STD
; /* XYZW */
1993 } else if (HAS_SWIZZLE(1, Z
) && HAS_SWIZZLE(2, Y
)) {
1994 return V_028C70_SWAP_STD_REV
; /* WZYX */
1995 } else if (HAS_SWIZZLE(1, Y
) && HAS_SWIZZLE(2, X
)) {
1996 return V_028C70_SWAP_ALT
; /* ZYXW */
1997 } else if (HAS_SWIZZLE(1, Z
) && HAS_SWIZZLE(2, W
)) {
2000 return V_028C70_SWAP_ALT_REV
;
2002 return (do_endian_swap
? V_028C70_SWAP_ALT
: V_028C70_SWAP_ALT_REV
);
2009 /* PIPELINE_STAT-BASED DCC ENABLEMENT FOR DISPLAYABLE SURFACES */
2011 static void vi_dcc_clean_up_context_slot(struct si_context
*sctx
, int slot
)
2015 if (sctx
->dcc_stats
[slot
].query_active
)
2016 vi_separate_dcc_stop_query(sctx
, sctx
->dcc_stats
[slot
].tex
);
2018 for (i
= 0; i
< ARRAY_SIZE(sctx
->dcc_stats
[slot
].ps_stats
); i
++)
2019 if (sctx
->dcc_stats
[slot
].ps_stats
[i
]) {
2020 sctx
->b
.destroy_query(&sctx
->b
, sctx
->dcc_stats
[slot
].ps_stats
[i
]);
2021 sctx
->dcc_stats
[slot
].ps_stats
[i
] = NULL
;
2024 si_texture_reference(&sctx
->dcc_stats
[slot
].tex
, NULL
);
2028 * Return the per-context slot where DCC statistics queries for the texture live.
2030 static unsigned vi_get_context_dcc_stats_index(struct si_context
*sctx
, struct si_texture
*tex
)
2032 int i
, empty_slot
= -1;
2034 /* Remove zombie textures (textures kept alive by this array only). */
2035 for (i
= 0; i
< ARRAY_SIZE(sctx
->dcc_stats
); i
++)
2036 if (sctx
->dcc_stats
[i
].tex
&& sctx
->dcc_stats
[i
].tex
->buffer
.b
.b
.reference
.count
== 1)
2037 vi_dcc_clean_up_context_slot(sctx
, i
);
2039 /* Find the texture. */
2040 for (i
= 0; i
< ARRAY_SIZE(sctx
->dcc_stats
); i
++) {
2041 /* Return if found. */
2042 if (sctx
->dcc_stats
[i
].tex
== tex
) {
2043 sctx
->dcc_stats
[i
].last_use_timestamp
= os_time_get();
2047 /* Record the first seen empty slot. */
2048 if (empty_slot
== -1 && !sctx
->dcc_stats
[i
].tex
)
2052 /* Not found. Remove the oldest member to make space in the array. */
2053 if (empty_slot
== -1) {
2054 int oldest_slot
= 0;
2056 /* Find the oldest slot. */
2057 for (i
= 1; i
< ARRAY_SIZE(sctx
->dcc_stats
); i
++)
2058 if (sctx
->dcc_stats
[oldest_slot
].last_use_timestamp
>
2059 sctx
->dcc_stats
[i
].last_use_timestamp
)
2062 /* Clean up the oldest slot. */
2063 vi_dcc_clean_up_context_slot(sctx
, oldest_slot
);
2064 empty_slot
= oldest_slot
;
2067 /* Add the texture to the new slot. */
2068 si_texture_reference(&sctx
->dcc_stats
[empty_slot
].tex
, tex
);
2069 sctx
->dcc_stats
[empty_slot
].last_use_timestamp
= os_time_get();
2073 static struct pipe_query
*vi_create_resuming_pipestats_query(struct si_context
*sctx
)
2075 struct si_query_hw
*query
=
2076 (struct si_query_hw
*)sctx
->b
.create_query(&sctx
->b
, PIPE_QUERY_PIPELINE_STATISTICS
, 0);
2078 query
->flags
|= SI_QUERY_HW_FLAG_BEGIN_RESUMES
;
2079 return (struct pipe_query
*)query
;
2083 * Called when binding a color buffer.
2085 void vi_separate_dcc_start_query(struct si_context
*sctx
, struct si_texture
*tex
)
2087 unsigned i
= vi_get_context_dcc_stats_index(sctx
, tex
);
2089 assert(!sctx
->dcc_stats
[i
].query_active
);
2091 if (!sctx
->dcc_stats
[i
].ps_stats
[0])
2092 sctx
->dcc_stats
[i
].ps_stats
[0] = vi_create_resuming_pipestats_query(sctx
);
2094 /* begin or resume the query */
2095 sctx
->b
.begin_query(&sctx
->b
, sctx
->dcc_stats
[i
].ps_stats
[0]);
2096 sctx
->dcc_stats
[i
].query_active
= true;
2100 * Called when unbinding a color buffer.
2102 void vi_separate_dcc_stop_query(struct si_context
*sctx
, struct si_texture
*tex
)
2104 unsigned i
= vi_get_context_dcc_stats_index(sctx
, tex
);
2106 assert(sctx
->dcc_stats
[i
].query_active
);
2107 assert(sctx
->dcc_stats
[i
].ps_stats
[0]);
2109 /* pause or end the query */
2110 sctx
->b
.end_query(&sctx
->b
, sctx
->dcc_stats
[i
].ps_stats
[0]);
2111 sctx
->dcc_stats
[i
].query_active
= false;
2114 static bool vi_should_enable_separate_dcc(struct si_texture
*tex
)
2116 /* The minimum number of fullscreen draws per frame that is required
2118 return tex
->ps_draw_ratio
+ tex
->num_slow_clears
>= 5;
2121 /* Called by fast clear. */
2122 void vi_separate_dcc_try_enable(struct si_context
*sctx
, struct si_texture
*tex
)
2124 /* The intent is to use this with shared displayable back buffers,
2125 * but it's not strictly limited only to them.
2127 if (!tex
->buffer
.b
.is_shared
||
2128 !(tex
->buffer
.external_usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
) ||
2129 tex
->buffer
.b
.b
.target
!= PIPE_TEXTURE_2D
|| tex
->buffer
.b
.b
.last_level
> 0 ||
2130 !tex
->surface
.dcc_size
|| sctx
->screen
->debug_flags
& DBG(NO_DCC
) ||
2131 sctx
->screen
->debug_flags
& DBG(NO_DCC_FB
))
2134 assert(sctx
->chip_class
>= GFX8
);
2136 if (tex
->surface
.dcc_offset
)
2137 return; /* already enabled */
2139 /* Enable the DCC stat gathering. */
2140 if (!tex
->dcc_gather_statistics
) {
2141 tex
->dcc_gather_statistics
= true;
2142 vi_separate_dcc_start_query(sctx
, tex
);
2145 if (!vi_should_enable_separate_dcc(tex
))
2146 return; /* stats show that DCC decompression is too expensive */
2148 assert(tex
->surface
.num_dcc_levels
);
2149 assert(!tex
->dcc_separate_buffer
);
2151 si_texture_discard_cmask(sctx
->screen
, tex
);
2153 /* Get a DCC buffer. */
2154 if (tex
->last_dcc_separate_buffer
) {
2155 assert(tex
->dcc_gather_statistics
);
2156 assert(!tex
->dcc_separate_buffer
);
2157 tex
->dcc_separate_buffer
= tex
->last_dcc_separate_buffer
;
2158 tex
->last_dcc_separate_buffer
= NULL
;
2160 tex
->dcc_separate_buffer
=
2161 si_aligned_buffer_create(sctx
->b
.screen
, SI_RESOURCE_FLAG_UNMAPPABLE
, PIPE_USAGE_DEFAULT
,
2162 tex
->surface
.dcc_size
, tex
->surface
.dcc_alignment
);
2163 if (!tex
->dcc_separate_buffer
)
2167 /* dcc_offset is the absolute GPUVM address. */
2168 tex
->surface
.dcc_offset
= tex
->dcc_separate_buffer
->gpu_address
;
2170 /* no need to flag anything since this is called by fast clear that
2171 * flags framebuffer state
2176 * Called by pipe_context::flush_resource, the place where DCC decompression
2179 void vi_separate_dcc_process_and_reset_stats(struct pipe_context
*ctx
, struct si_texture
*tex
)
2181 struct si_context
*sctx
= (struct si_context
*)ctx
;
2182 struct pipe_query
*tmp
;
2183 unsigned i
= vi_get_context_dcc_stats_index(sctx
, tex
);
2184 bool query_active
= sctx
->dcc_stats
[i
].query_active
;
2185 bool disable
= false;
2187 if (sctx
->dcc_stats
[i
].ps_stats
[2]) {
2188 union pipe_query_result result
;
2190 /* Read the results. */
2191 struct pipe_query
*query
= sctx
->dcc_stats
[i
].ps_stats
[2];
2192 ctx
->get_query_result(ctx
, query
, true, &result
);
2193 si_query_buffer_reset(sctx
, &((struct si_query_hw
*)query
)->buffer
);
2195 /* Compute the approximate number of fullscreen draws. */
2196 tex
->ps_draw_ratio
= result
.pipeline_statistics
.ps_invocations
/
2197 (tex
->buffer
.b
.b
.width0
* tex
->buffer
.b
.b
.height0
);
2198 sctx
->last_tex_ps_draw_ratio
= tex
->ps_draw_ratio
;
2200 disable
= tex
->dcc_separate_buffer
&& !vi_should_enable_separate_dcc(tex
);
2203 tex
->num_slow_clears
= 0;
2205 /* stop the statistics query for ps_stats[0] */
2207 vi_separate_dcc_stop_query(sctx
, tex
);
2209 /* Move the queries in the queue by one. */
2210 tmp
= sctx
->dcc_stats
[i
].ps_stats
[2];
2211 sctx
->dcc_stats
[i
].ps_stats
[2] = sctx
->dcc_stats
[i
].ps_stats
[1];
2212 sctx
->dcc_stats
[i
].ps_stats
[1] = sctx
->dcc_stats
[i
].ps_stats
[0];
2213 sctx
->dcc_stats
[i
].ps_stats
[0] = tmp
;
2215 /* create and start a new query as ps_stats[0] */
2217 vi_separate_dcc_start_query(sctx
, tex
);
2220 assert(!tex
->last_dcc_separate_buffer
);
2221 tex
->last_dcc_separate_buffer
= tex
->dcc_separate_buffer
;
2222 tex
->dcc_separate_buffer
= NULL
;
2223 tex
->surface
.dcc_offset
= 0;
2224 /* no need to flag anything since this is called after
2225 * decompression that re-sets framebuffer state
2230 static struct pipe_memory_object
*
2231 si_memobj_from_handle(struct pipe_screen
*screen
, struct winsys_handle
*whandle
, bool dedicated
)
2233 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
2234 struct si_memory_object
*memobj
= CALLOC_STRUCT(si_memory_object
);
2235 struct pb_buffer
*buf
= NULL
;
2240 buf
= sscreen
->ws
->buffer_from_handle(sscreen
->ws
, whandle
, sscreen
->info
.max_alignment
);
2246 memobj
->b
.dedicated
= dedicated
;
2248 memobj
->stride
= whandle
->stride
;
2250 return (struct pipe_memory_object
*)memobj
;
2253 static void si_memobj_destroy(struct pipe_screen
*screen
, struct pipe_memory_object
*_memobj
)
2255 struct si_memory_object
*memobj
= (struct si_memory_object
*)_memobj
;
2257 pb_reference(&memobj
->buf
, NULL
);
2261 static struct pipe_resource
*si_texture_from_memobj(struct pipe_screen
*screen
,
2262 const struct pipe_resource
*templ
,
2263 struct pipe_memory_object
*_memobj
,
2266 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
2267 struct si_memory_object
*memobj
= (struct si_memory_object
*)_memobj
;
2268 struct pipe_resource
*tex
= si_texture_from_winsys_buffer(
2269 sscreen
, templ
, memobj
->buf
, memobj
->stride
, offset
,
2270 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE
| PIPE_HANDLE_USAGE_SHADER_WRITE
, memobj
->b
.dedicated
);
2274 /* si_texture_from_winsys_buffer doesn't increment refcount of
2275 * memobj->buf, so increment it here.
2277 struct pb_buffer
*buf
= NULL
;
2278 pb_reference(&buf
, memobj
->buf
);
2282 static bool si_check_resource_capability(struct pipe_screen
*screen
, struct pipe_resource
*resource
,
2285 struct si_texture
*tex
= (struct si_texture
*)resource
;
2287 /* Buffers only support the linear flag. */
2288 if (resource
->target
== PIPE_BUFFER
)
2289 return (bind
& ~PIPE_BIND_LINEAR
) == 0;
2291 if (bind
& PIPE_BIND_LINEAR
&& !tex
->surface
.is_linear
)
2294 if (bind
& PIPE_BIND_SCANOUT
&& !tex
->surface
.is_displayable
)
2297 /* TODO: PIPE_BIND_CURSOR - do we care? */
2301 void si_init_screen_texture_functions(struct si_screen
*sscreen
)
2303 sscreen
->b
.resource_from_handle
= si_texture_from_handle
;
2304 sscreen
->b
.resource_get_handle
= si_texture_get_handle
;
2305 sscreen
->b
.resource_get_param
= si_resource_get_param
;
2306 sscreen
->b
.resource_get_info
= si_texture_get_info
;
2307 sscreen
->b
.resource_from_memobj
= si_texture_from_memobj
;
2308 sscreen
->b
.memobj_create_from_handle
= si_memobj_from_handle
;
2309 sscreen
->b
.memobj_destroy
= si_memobj_destroy
;
2310 sscreen
->b
.check_resource_capability
= si_check_resource_capability
;
2313 void si_init_context_texture_functions(struct si_context
*sctx
)
2315 sctx
->b
.create_surface
= si_create_surface
;
2316 sctx
->b
.surface_destroy
= si_surface_destroy
;