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 "radeonsi/si_pipe.h"
27 #include "radeonsi/si_query.h"
28 #include "util/u_format.h"
29 #include "util/u_log.h"
30 #include "util/u_memory.h"
31 #include "util/u_pack_color.h"
32 #include "util/u_resource.h"
33 #include "util/u_surface.h"
34 #include "util/u_transfer.h"
35 #include "util/os_time.h"
38 #include "state_tracker/drm_driver.h"
39 #include "amd/common/sid.h"
41 static enum radeon_surf_mode
42 si_choose_tiling(struct si_screen
*sscreen
,
43 const struct pipe_resource
*templ
, bool tc_compatible_htile
);
46 bool si_prepare_for_dma_blit(struct si_context
*sctx
,
47 struct si_texture
*dst
,
48 unsigned dst_level
, unsigned dstx
,
49 unsigned dsty
, unsigned dstz
,
50 struct si_texture
*src
,
52 const struct pipe_box
*src_box
)
57 if (dst
->surface
.bpe
!= src
->surface
.bpe
)
60 /* MSAA: Blits don't exist in the real world. */
61 if (src
->buffer
.b
.b
.nr_samples
> 1 ||
62 dst
->buffer
.b
.b
.nr_samples
> 1)
65 /* Depth-stencil surfaces:
66 * When dst is linear, the DB->CB copy preserves HTILE.
67 * When dst is tiled, the 3D path must be used to update HTILE.
69 if (src
->is_depth
|| dst
->is_depth
)
73 * src: Use the 3D path. DCC decompression is expensive.
74 * dst: Use the 3D path to compress the pixels with DCC.
76 if (vi_dcc_enabled(src
, src_level
) ||
77 vi_dcc_enabled(dst
, dst_level
))
81 * src: Both texture and SDMA paths need decompression. Use SDMA.
82 * dst: If overwriting the whole texture, discard CMASK and use
83 * SDMA. Otherwise, use the 3D path.
85 if (dst
->cmask_buffer
&& dst
->dirty_level_mask
& (1 << dst_level
)) {
86 /* The CMASK clear is only enabled for the first level. */
87 assert(dst_level
== 0);
88 if (!util_texrange_covers_whole_level(&dst
->buffer
.b
.b
, dst_level
,
89 dstx
, dsty
, dstz
, src_box
->width
,
90 src_box
->height
, src_box
->depth
))
93 si_texture_discard_cmask(sctx
->screen
, dst
);
96 /* All requirements are met. Prepare textures for SDMA. */
97 if (src
->cmask_buffer
&& src
->dirty_level_mask
& (1 << src_level
))
98 sctx
->b
.flush_resource(&sctx
->b
, &src
->buffer
.b
.b
);
100 assert(!(src
->dirty_level_mask
& (1 << src_level
)));
101 assert(!(dst
->dirty_level_mask
& (1 << dst_level
)));
106 /* Same as resource_copy_region, except that both upsampling and downsampling are allowed. */
107 static void si_copy_region_with_blit(struct pipe_context
*pipe
,
108 struct pipe_resource
*dst
,
110 unsigned dstx
, unsigned dsty
, unsigned dstz
,
111 struct pipe_resource
*src
,
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(src
->format
) &
132 util_format_get_mask(dst
->format
);
133 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
136 pipe
->blit(pipe
, &blit
);
140 /* Copy from a full GPU texture to a transfer's staging one. */
141 static void si_copy_to_staging_texture(struct pipe_context
*ctx
, struct si_transfer
*stransfer
)
143 struct si_context
*sctx
= (struct si_context
*)ctx
;
144 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)stransfer
;
145 struct pipe_resource
*dst
= &stransfer
->staging
->b
.b
;
146 struct pipe_resource
*src
= transfer
->resource
;
148 if (src
->nr_samples
> 1) {
149 si_copy_region_with_blit(ctx
, dst
, 0, 0, 0, 0,
150 src
, transfer
->level
, &transfer
->box
);
154 sctx
->dma_copy(ctx
, dst
, 0, 0, 0, 0, src
, transfer
->level
,
158 /* Copy from a transfer's staging texture to a full GPU one. */
159 static void si_copy_from_staging_texture(struct pipe_context
*ctx
, struct si_transfer
*stransfer
)
161 struct si_context
*sctx
= (struct si_context
*)ctx
;
162 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)stransfer
;
163 struct pipe_resource
*dst
= transfer
->resource
;
164 struct pipe_resource
*src
= &stransfer
->staging
->b
.b
;
165 struct pipe_box sbox
;
167 u_box_3d(0, 0, 0, transfer
->box
.width
, transfer
->box
.height
, transfer
->box
.depth
, &sbox
);
169 if (dst
->nr_samples
> 1) {
170 si_copy_region_with_blit(ctx
, dst
, transfer
->level
,
171 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
176 sctx
->dma_copy(ctx
, dst
, transfer
->level
,
177 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
181 static unsigned si_texture_get_offset(struct si_screen
*sscreen
,
182 struct si_texture
*tex
, unsigned level
,
183 const struct pipe_box
*box
,
185 unsigned *layer_stride
)
187 if (sscreen
->info
.chip_class
>= GFX9
) {
188 *stride
= tex
->surface
.u
.gfx9
.surf_pitch
* tex
->surface
.bpe
;
189 *layer_stride
= tex
->surface
.u
.gfx9
.surf_slice_size
;
194 /* Each texture is an array of slices. Each slice is an array
195 * of mipmap levels. */
196 return box
->z
* tex
->surface
.u
.gfx9
.surf_slice_size
+
197 tex
->surface
.u
.gfx9
.offset
[level
] +
198 (box
->y
/ tex
->surface
.blk_h
*
199 tex
->surface
.u
.gfx9
.surf_pitch
+
200 box
->x
/ tex
->surface
.blk_w
) * tex
->surface
.bpe
;
202 *stride
= tex
->surface
.u
.legacy
.level
[level
].nblk_x
*
204 assert((uint64_t)tex
->surface
.u
.legacy
.level
[level
].slice_size_dw
* 4 <= UINT_MAX
);
205 *layer_stride
= (uint64_t)tex
->surface
.u
.legacy
.level
[level
].slice_size_dw
* 4;
208 return tex
->surface
.u
.legacy
.level
[level
].offset
;
210 /* Each texture is an array of mipmap levels. Each level is
211 * an array of slices. */
212 return tex
->surface
.u
.legacy
.level
[level
].offset
+
213 box
->z
* (uint64_t)tex
->surface
.u
.legacy
.level
[level
].slice_size_dw
* 4 +
214 (box
->y
/ tex
->surface
.blk_h
*
215 tex
->surface
.u
.legacy
.level
[level
].nblk_x
+
216 box
->x
/ tex
->surface
.blk_w
) * tex
->surface
.bpe
;
220 static int si_init_surface(struct si_screen
*sscreen
,
221 struct radeon_surf
*surface
,
222 const struct pipe_resource
*ptex
,
223 enum radeon_surf_mode array_mode
,
224 unsigned pitch_in_bytes_override
,
228 bool is_flushed_depth
,
229 bool tc_compatible_htile
)
231 const struct util_format_description
*desc
=
232 util_format_description(ptex
->format
);
233 bool is_depth
, is_stencil
;
235 unsigned i
, bpe
, flags
= 0;
237 is_depth
= util_format_has_depth(desc
);
238 is_stencil
= util_format_has_stencil(desc
);
240 if (!is_flushed_depth
&&
241 ptex
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
) {
242 bpe
= 4; /* stencil is allocated separately */
244 bpe
= util_format_get_blocksize(ptex
->format
);
245 assert(util_is_power_of_two_or_zero(bpe
));
248 if (!is_flushed_depth
&& is_depth
) {
249 flags
|= RADEON_SURF_ZBUFFER
;
251 if (tc_compatible_htile
&&
252 (sscreen
->info
.chip_class
>= GFX9
||
253 array_mode
== RADEON_SURF_MODE_2D
)) {
254 /* TC-compatible HTILE only supports Z32_FLOAT.
255 * GFX9 also supports Z16_UNORM.
256 * On VI, promote Z16 to Z32. DB->CB copies will convert
257 * the format for transfers.
259 if (sscreen
->info
.chip_class
== VI
)
262 flags
|= RADEON_SURF_TC_COMPATIBLE_HTILE
;
266 flags
|= RADEON_SURF_SBUFFER
;
269 if (sscreen
->info
.chip_class
>= VI
&&
270 (ptex
->flags
& SI_RESOURCE_FLAG_DISABLE_DCC
||
271 ptex
->format
== PIPE_FORMAT_R9G9B9E5_FLOAT
||
272 (ptex
->nr_samples
>= 2 && !sscreen
->dcc_msaa_allowed
)))
273 flags
|= RADEON_SURF_DISABLE_DCC
;
275 /* Stoney: 128bpp MSAA textures randomly fail piglit tests with DCC. */
276 if (sscreen
->info
.family
== CHIP_STONEY
&&
277 bpe
== 16 && ptex
->nr_samples
>= 2)
278 flags
|= RADEON_SURF_DISABLE_DCC
;
280 /* VI: DCC clear for 4x and 8x MSAA array textures unimplemented. */
281 if (sscreen
->info
.chip_class
== VI
&&
282 ptex
->nr_storage_samples
>= 4 &&
283 ptex
->array_size
> 1)
284 flags
|= RADEON_SURF_DISABLE_DCC
;
286 /* GFX9: DCC clear for 4x and 8x MSAA textures unimplemented. */
287 if (sscreen
->info
.chip_class
>= GFX9
&&
288 ptex
->nr_storage_samples
>= 4)
289 flags
|= RADEON_SURF_DISABLE_DCC
;
291 if (ptex
->bind
& PIPE_BIND_SCANOUT
|| is_scanout
) {
292 /* This should catch bugs in gallium users setting incorrect flags. */
293 assert(ptex
->nr_samples
<= 1 &&
294 ptex
->array_size
== 1 &&
296 ptex
->last_level
== 0 &&
297 !(flags
& RADEON_SURF_Z_OR_SBUFFER
));
299 flags
|= RADEON_SURF_SCANOUT
;
302 if (ptex
->bind
& PIPE_BIND_SHARED
)
303 flags
|= RADEON_SURF_SHAREABLE
;
305 flags
|= RADEON_SURF_IMPORTED
| RADEON_SURF_SHAREABLE
;
306 if (!(ptex
->flags
& SI_RESOURCE_FLAG_FORCE_MSAA_TILING
))
307 flags
|= RADEON_SURF_OPTIMIZE_FOR_SPACE
;
309 r
= sscreen
->ws
->surface_init(sscreen
->ws
, ptex
, flags
, bpe
,
310 array_mode
, surface
);
315 unsigned pitch
= pitch_in_bytes_override
/ bpe
;
317 if (sscreen
->info
.chip_class
>= GFX9
) {
319 surface
->u
.gfx9
.surf_pitch
= pitch
;
320 surface
->u
.gfx9
.surf_slice_size
=
321 (uint64_t)pitch
* surface
->u
.gfx9
.surf_height
* bpe
;
323 surface
->u
.gfx9
.surf_offset
= offset
;
326 surface
->u
.legacy
.level
[0].nblk_x
= pitch
;
327 surface
->u
.legacy
.level
[0].slice_size_dw
=
328 ((uint64_t)pitch
* surface
->u
.legacy
.level
[0].nblk_y
* bpe
) / 4;
331 for (i
= 0; i
< ARRAY_SIZE(surface
->u
.legacy
.level
); ++i
)
332 surface
->u
.legacy
.level
[i
].offset
+= offset
;
338 static void si_texture_init_metadata(struct si_screen
*sscreen
,
339 struct si_texture
*tex
,
340 struct radeon_bo_metadata
*metadata
)
342 struct radeon_surf
*surface
= &tex
->surface
;
344 memset(metadata
, 0, sizeof(*metadata
));
346 if (sscreen
->info
.chip_class
>= GFX9
) {
347 metadata
->u
.gfx9
.swizzle_mode
= surface
->u
.gfx9
.surf
.swizzle_mode
;
349 metadata
->u
.legacy
.microtile
= surface
->u
.legacy
.level
[0].mode
>= RADEON_SURF_MODE_1D
?
350 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
;
351 metadata
->u
.legacy
.macrotile
= surface
->u
.legacy
.level
[0].mode
>= RADEON_SURF_MODE_2D
?
352 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
;
353 metadata
->u
.legacy
.pipe_config
= surface
->u
.legacy
.pipe_config
;
354 metadata
->u
.legacy
.bankw
= surface
->u
.legacy
.bankw
;
355 metadata
->u
.legacy
.bankh
= surface
->u
.legacy
.bankh
;
356 metadata
->u
.legacy
.tile_split
= surface
->u
.legacy
.tile_split
;
357 metadata
->u
.legacy
.mtilea
= surface
->u
.legacy
.mtilea
;
358 metadata
->u
.legacy
.num_banks
= surface
->u
.legacy
.num_banks
;
359 metadata
->u
.legacy
.stride
= surface
->u
.legacy
.level
[0].nblk_x
* surface
->bpe
;
360 metadata
->u
.legacy
.scanout
= (surface
->flags
& RADEON_SURF_SCANOUT
) != 0;
364 static void si_surface_import_metadata(struct si_screen
*sscreen
,
365 struct radeon_surf
*surf
,
366 struct radeon_bo_metadata
*metadata
,
367 enum radeon_surf_mode
*array_mode
,
370 if (sscreen
->info
.chip_class
>= GFX9
) {
371 if (metadata
->u
.gfx9
.swizzle_mode
> 0)
372 *array_mode
= RADEON_SURF_MODE_2D
;
374 *array_mode
= RADEON_SURF_MODE_LINEAR_ALIGNED
;
376 *is_scanout
= metadata
->u
.gfx9
.swizzle_mode
== 0 ||
377 metadata
->u
.gfx9
.swizzle_mode
% 4 == 2;
379 surf
->u
.gfx9
.surf
.swizzle_mode
= metadata
->u
.gfx9
.swizzle_mode
;
381 surf
->u
.legacy
.pipe_config
= metadata
->u
.legacy
.pipe_config
;
382 surf
->u
.legacy
.bankw
= metadata
->u
.legacy
.bankw
;
383 surf
->u
.legacy
.bankh
= metadata
->u
.legacy
.bankh
;
384 surf
->u
.legacy
.tile_split
= metadata
->u
.legacy
.tile_split
;
385 surf
->u
.legacy
.mtilea
= metadata
->u
.legacy
.mtilea
;
386 surf
->u
.legacy
.num_banks
= metadata
->u
.legacy
.num_banks
;
388 if (metadata
->u
.legacy
.macrotile
== RADEON_LAYOUT_TILED
)
389 *array_mode
= RADEON_SURF_MODE_2D
;
390 else if (metadata
->u
.legacy
.microtile
== RADEON_LAYOUT_TILED
)
391 *array_mode
= RADEON_SURF_MODE_1D
;
393 *array_mode
= RADEON_SURF_MODE_LINEAR_ALIGNED
;
395 *is_scanout
= metadata
->u
.legacy
.scanout
;
399 void si_eliminate_fast_color_clear(struct si_context
*sctx
,
400 struct si_texture
*tex
)
402 struct si_screen
*sscreen
= sctx
->screen
;
403 struct pipe_context
*ctx
= &sctx
->b
;
405 if (ctx
== sscreen
->aux_context
)
406 mtx_lock(&sscreen
->aux_context_lock
);
408 unsigned n
= sctx
->num_decompress_calls
;
409 ctx
->flush_resource(ctx
, &tex
->buffer
.b
.b
);
411 /* Flush only if any fast clear elimination took place. */
412 if (n
!= sctx
->num_decompress_calls
)
413 ctx
->flush(ctx
, NULL
, 0);
415 if (ctx
== sscreen
->aux_context
)
416 mtx_unlock(&sscreen
->aux_context_lock
);
419 void si_texture_discard_cmask(struct si_screen
*sscreen
,
420 struct si_texture
*tex
)
422 if (!tex
->cmask_buffer
)
425 assert(tex
->buffer
.b
.b
.nr_samples
<= 1);
428 tex
->cmask_base_address_reg
= tex
->buffer
.gpu_address
>> 8;
429 tex
->dirty_level_mask
= 0;
431 tex
->cb_color_info
&= ~S_028C70_FAST_CLEAR(1);
433 if (tex
->cmask_buffer
!= &tex
->buffer
)
434 r600_resource_reference(&tex
->cmask_buffer
, NULL
);
436 tex
->cmask_buffer
= NULL
;
438 /* Notify all contexts about the change. */
439 p_atomic_inc(&sscreen
->dirty_tex_counter
);
440 p_atomic_inc(&sscreen
->compressed_colortex_counter
);
443 static bool si_can_disable_dcc(struct si_texture
*tex
)
445 /* We can't disable DCC if it can be written by another process. */
446 return tex
->dcc_offset
&&
447 (!tex
->buffer
.b
.is_shared
||
448 !(tex
->buffer
.external_usage
& PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE
));
451 static bool si_texture_discard_dcc(struct si_screen
*sscreen
,
452 struct si_texture
*tex
)
454 if (!si_can_disable_dcc(tex
))
457 assert(tex
->dcc_separate_buffer
== NULL
);
462 /* Notify all contexts about the change. */
463 p_atomic_inc(&sscreen
->dirty_tex_counter
);
468 * Disable DCC for the texture. (first decompress, then discard metadata).
470 * There is unresolved multi-context synchronization issue between
471 * screen::aux_context and the current context. If applications do this with
472 * multiple contexts, it's already undefined behavior for them and we don't
473 * have to worry about that. The scenario is:
475 * If context 1 disables DCC and context 2 has queued commands that write
476 * to the texture via CB with DCC enabled, and the order of operations is
478 * context 2 queues draw calls rendering to the texture, but doesn't flush
479 * context 1 disables DCC and flushes
480 * context 1 & 2 reset descriptors and FB state
481 * context 2 flushes (new compressed tiles written by the draw calls)
482 * context 1 & 2 read garbage, because DCC is disabled, yet there are
485 * \param sctx the current context if you have one, or sscreen->aux_context
488 bool si_texture_disable_dcc(struct si_context
*sctx
,
489 struct si_texture
*tex
)
491 struct si_screen
*sscreen
= sctx
->screen
;
493 if (!si_can_disable_dcc(tex
))
496 if (&sctx
->b
== sscreen
->aux_context
)
497 mtx_lock(&sscreen
->aux_context_lock
);
499 /* Decompress DCC. */
500 si_decompress_dcc(sctx
, tex
);
501 sctx
->b
.flush(&sctx
->b
, NULL
, 0);
503 if (&sctx
->b
== sscreen
->aux_context
)
504 mtx_unlock(&sscreen
->aux_context_lock
);
506 return si_texture_discard_dcc(sscreen
, tex
);
509 static void si_reallocate_texture_inplace(struct si_context
*sctx
,
510 struct si_texture
*tex
,
511 unsigned new_bind_flag
,
512 bool invalidate_storage
)
514 struct pipe_screen
*screen
= sctx
->b
.screen
;
515 struct si_texture
*new_tex
;
516 struct pipe_resource templ
= tex
->buffer
.b
.b
;
519 templ
.bind
|= new_bind_flag
;
521 if (tex
->buffer
.b
.is_shared
)
524 if (new_bind_flag
== PIPE_BIND_LINEAR
) {
525 if (tex
->surface
.is_linear
)
528 /* This fails with MSAA, depth, and compressed textures. */
529 if (si_choose_tiling(sctx
->screen
, &templ
, false) !=
530 RADEON_SURF_MODE_LINEAR_ALIGNED
)
534 new_tex
= (struct si_texture
*)screen
->resource_create(screen
, &templ
);
538 /* Copy the pixels to the new texture. */
539 if (!invalidate_storage
) {
540 for (i
= 0; i
<= templ
.last_level
; i
++) {
544 u_minify(templ
.width0
, i
), u_minify(templ
.height0
, i
),
545 util_num_layers(&templ
, i
), &box
);
547 sctx
->dma_copy(&sctx
->b
, &new_tex
->buffer
.b
.b
, i
, 0, 0, 0,
548 &tex
->buffer
.b
.b
, i
, &box
);
552 if (new_bind_flag
== PIPE_BIND_LINEAR
) {
553 si_texture_discard_cmask(sctx
->screen
, tex
);
554 si_texture_discard_dcc(sctx
->screen
, tex
);
557 /* Replace the structure fields of tex. */
558 tex
->buffer
.b
.b
.bind
= templ
.bind
;
559 pb_reference(&tex
->buffer
.buf
, new_tex
->buffer
.buf
);
560 tex
->buffer
.gpu_address
= new_tex
->buffer
.gpu_address
;
561 tex
->buffer
.vram_usage
= new_tex
->buffer
.vram_usage
;
562 tex
->buffer
.gart_usage
= new_tex
->buffer
.gart_usage
;
563 tex
->buffer
.bo_size
= new_tex
->buffer
.bo_size
;
564 tex
->buffer
.bo_alignment
= new_tex
->buffer
.bo_alignment
;
565 tex
->buffer
.domains
= new_tex
->buffer
.domains
;
566 tex
->buffer
.flags
= new_tex
->buffer
.flags
;
568 tex
->surface
= new_tex
->surface
;
569 tex
->size
= new_tex
->size
;
570 si_texture_reference(&tex
->flushed_depth_texture
,
571 new_tex
->flushed_depth_texture
);
573 tex
->fmask_offset
= new_tex
->fmask_offset
;
574 tex
->cmask_offset
= new_tex
->cmask_offset
;
575 tex
->cmask_base_address_reg
= new_tex
->cmask_base_address_reg
;
577 if (tex
->cmask_buffer
== &tex
->buffer
)
578 tex
->cmask_buffer
= NULL
;
580 r600_resource_reference(&tex
->cmask_buffer
, NULL
);
582 if (new_tex
->cmask_buffer
== &new_tex
->buffer
)
583 tex
->cmask_buffer
= &tex
->buffer
;
585 r600_resource_reference(&tex
->cmask_buffer
, new_tex
->cmask_buffer
);
587 tex
->dcc_offset
= new_tex
->dcc_offset
;
588 tex
->cb_color_info
= new_tex
->cb_color_info
;
589 memcpy(tex
->color_clear_value
, new_tex
->color_clear_value
,
590 sizeof(tex
->color_clear_value
));
591 tex
->last_msaa_resolve_target_micro_mode
= new_tex
->last_msaa_resolve_target_micro_mode
;
593 tex
->htile_offset
= new_tex
->htile_offset
;
594 tex
->depth_clear_value
= new_tex
->depth_clear_value
;
595 tex
->dirty_level_mask
= new_tex
->dirty_level_mask
;
596 tex
->stencil_dirty_level_mask
= new_tex
->stencil_dirty_level_mask
;
597 tex
->db_render_format
= new_tex
->db_render_format
;
598 tex
->stencil_clear_value
= new_tex
->stencil_clear_value
;
599 tex
->tc_compatible_htile
= new_tex
->tc_compatible_htile
;
600 tex
->depth_cleared
= new_tex
->depth_cleared
;
601 tex
->stencil_cleared
= new_tex
->stencil_cleared
;
602 tex
->upgraded_depth
= new_tex
->upgraded_depth
;
603 tex
->db_compatible
= new_tex
->db_compatible
;
604 tex
->can_sample_z
= new_tex
->can_sample_z
;
605 tex
->can_sample_s
= new_tex
->can_sample_s
;
607 tex
->separate_dcc_dirty
= new_tex
->separate_dcc_dirty
;
608 tex
->dcc_gather_statistics
= new_tex
->dcc_gather_statistics
;
609 r600_resource_reference(&tex
->dcc_separate_buffer
,
610 new_tex
->dcc_separate_buffer
);
611 r600_resource_reference(&tex
->last_dcc_separate_buffer
,
612 new_tex
->last_dcc_separate_buffer
);
614 if (new_bind_flag
== PIPE_BIND_LINEAR
) {
615 assert(!tex
->htile_offset
);
616 assert(!tex
->cmask_buffer
);
617 assert(!tex
->surface
.fmask_size
);
618 assert(!tex
->dcc_offset
);
619 assert(!tex
->is_depth
);
622 si_texture_reference(&new_tex
, NULL
);
624 p_atomic_inc(&sctx
->screen
->dirty_tex_counter
);
627 static uint32_t si_get_bo_metadata_word1(struct si_screen
*sscreen
)
629 return (ATI_VENDOR_ID
<< 16) | sscreen
->info
.pci_id
;
632 static void si_query_opaque_metadata(struct si_screen
*sscreen
,
633 struct si_texture
*tex
,
634 struct radeon_bo_metadata
*md
)
636 struct pipe_resource
*res
= &tex
->buffer
.b
.b
;
637 static const unsigned char swizzle
[] = {
644 bool is_array
= util_texture_is_array(res
->target
);
646 if (!sscreen
->info
.has_bo_metadata
)
649 assert(tex
->dcc_separate_buffer
== NULL
);
650 assert(tex
->surface
.fmask_size
== 0);
652 /* Metadata image format format version 1:
653 * [0] = 1 (metadata format identifier)
654 * [1] = (VENDOR_ID << 16) | PCI_ID
655 * [2:9] = image descriptor for the whole resource
656 * [2] is always 0, because the base address is cleared
657 * [9] is the DCC offset bits [39:8] from the beginning of
659 * [10:10+LAST_LEVEL] = mipmap level offset bits [39:8] for each level
662 md
->metadata
[0] = 1; /* metadata image format version 1 */
664 /* TILE_MODE_INDEX is ambiguous without a PCI ID. */
665 md
->metadata
[1] = si_get_bo_metadata_word1(sscreen
);
667 si_make_texture_descriptor(sscreen
, tex
, true,
668 res
->target
, res
->format
,
669 swizzle
, 0, res
->last_level
, 0,
670 is_array
? res
->array_size
- 1 : 0,
671 res
->width0
, res
->height0
, res
->depth0
,
674 si_set_mutable_tex_desc_fields(sscreen
, tex
, &tex
->surface
.u
.legacy
.level
[0],
675 0, 0, tex
->surface
.blk_w
, false, desc
);
677 /* Clear the base address and set the relative DCC offset. */
679 desc
[1] &= C_008F14_BASE_ADDRESS_HI
;
680 desc
[7] = tex
->dcc_offset
>> 8;
682 /* Dwords [2:9] contain the image descriptor. */
683 memcpy(&md
->metadata
[2], desc
, sizeof(desc
));
684 md
->size_metadata
= 10 * 4;
686 /* Dwords [10:..] contain the mipmap level offsets. */
687 if (sscreen
->info
.chip_class
<= VI
) {
688 for (i
= 0; i
<= res
->last_level
; i
++)
689 md
->metadata
[10+i
] = tex
->surface
.u
.legacy
.level
[i
].offset
>> 8;
691 md
->size_metadata
+= (1 + res
->last_level
) * 4;
695 static void si_apply_opaque_metadata(struct si_screen
*sscreen
,
696 struct si_texture
*tex
,
697 struct radeon_bo_metadata
*md
)
699 uint32_t *desc
= &md
->metadata
[2];
701 if (sscreen
->info
.chip_class
< VI
)
704 /* Return if DCC is enabled. The texture should be set up with it
707 if (md
->size_metadata
>= 10 * 4 && /* at least 2(header) + 8(desc) dwords */
708 md
->metadata
[0] != 0 &&
709 md
->metadata
[1] == si_get_bo_metadata_word1(sscreen
) &&
710 G_008F28_COMPRESSION_EN(desc
[6])) {
711 tex
->dcc_offset
= (uint64_t)desc
[7] << 8;
715 /* Disable DCC. These are always set by texture_from_handle and must
721 static boolean
si_texture_get_handle(struct pipe_screen
* screen
,
722 struct pipe_context
*ctx
,
723 struct pipe_resource
*resource
,
724 struct winsys_handle
*whandle
,
727 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
728 struct si_context
*sctx
;
729 struct r600_resource
*res
= r600_resource(resource
);
730 struct si_texture
*tex
= (struct si_texture
*)resource
;
731 struct radeon_bo_metadata metadata
;
732 bool update_metadata
= false;
733 unsigned stride
, offset
, slice_size
;
736 ctx
= threaded_context_unwrap_sync(ctx
);
737 sctx
= (struct si_context
*)(ctx
? ctx
: sscreen
->aux_context
);
739 if (resource
->target
!= PIPE_BUFFER
) {
740 /* This is not supported now, but it might be required for OpenCL
741 * interop in the future.
743 if (resource
->nr_samples
> 1 || tex
->is_depth
)
746 /* Move a suballocated texture into a non-suballocated allocation. */
747 if (sscreen
->ws
->buffer_is_suballocated(res
->buf
) ||
748 tex
->surface
.tile_swizzle
||
749 (tex
->buffer
.flags
& RADEON_FLAG_NO_INTERPROCESS_SHARING
&&
750 sscreen
->info
.has_local_buffers
&&
751 whandle
->type
!= WINSYS_HANDLE_TYPE_KMS
)) {
752 assert(!res
->b
.is_shared
);
753 si_reallocate_texture_inplace(sctx
, tex
,
754 PIPE_BIND_SHARED
, false);
756 assert(res
->b
.b
.bind
& PIPE_BIND_SHARED
);
757 assert(res
->flags
& RADEON_FLAG_NO_SUBALLOC
);
758 assert(!(res
->flags
& RADEON_FLAG_NO_INTERPROCESS_SHARING
));
759 assert(tex
->surface
.tile_swizzle
== 0);
762 /* Since shader image stores don't support DCC on VI,
763 * disable it for external clients that want write
766 if (usage
& PIPE_HANDLE_USAGE_SHADER_WRITE
&& tex
->dcc_offset
) {
767 if (si_texture_disable_dcc(sctx
, tex
)) {
768 update_metadata
= true;
769 /* si_texture_disable_dcc flushes the context */
774 if (!(usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
) &&
775 (tex
->cmask_buffer
|| tex
->dcc_offset
)) {
776 /* Eliminate fast clear (both CMASK and DCC) */
777 si_eliminate_fast_color_clear(sctx
, tex
);
778 /* eliminate_fast_color_clear flushes the context */
781 /* Disable CMASK if flush_resource isn't going
784 if (tex
->cmask_buffer
)
785 si_texture_discard_cmask(sscreen
, tex
);
789 if (!res
->b
.is_shared
|| update_metadata
) {
790 si_texture_init_metadata(sscreen
, tex
, &metadata
);
791 si_query_opaque_metadata(sscreen
, tex
, &metadata
);
793 sscreen
->ws
->buffer_set_metadata(res
->buf
, &metadata
);
796 if (sscreen
->info
.chip_class
>= GFX9
) {
797 offset
= tex
->surface
.u
.gfx9
.surf_offset
;
798 stride
= tex
->surface
.u
.gfx9
.surf_pitch
*
800 slice_size
= tex
->surface
.u
.gfx9
.surf_slice_size
;
802 offset
= tex
->surface
.u
.legacy
.level
[0].offset
;
803 stride
= tex
->surface
.u
.legacy
.level
[0].nblk_x
*
805 slice_size
= (uint64_t)tex
->surface
.u
.legacy
.level
[0].slice_size_dw
* 4;
808 /* Buffer exports are for the OpenCL interop. */
809 /* Move a suballocated buffer into a non-suballocated allocation. */
810 if (sscreen
->ws
->buffer_is_suballocated(res
->buf
) ||
811 /* A DMABUF export always fails if the BO is local. */
812 (tex
->buffer
.flags
& RADEON_FLAG_NO_INTERPROCESS_SHARING
&&
813 sscreen
->info
.has_local_buffers
)) {
814 assert(!res
->b
.is_shared
);
816 /* Allocate a new buffer with PIPE_BIND_SHARED. */
817 struct pipe_resource templ
= res
->b
.b
;
818 templ
.bind
|= PIPE_BIND_SHARED
;
820 struct pipe_resource
*newb
=
821 screen
->resource_create(screen
, &templ
);
825 /* Copy the old buffer contents to the new one. */
827 u_box_1d(0, newb
->width0
, &box
);
828 sctx
->b
.resource_copy_region(&sctx
->b
, newb
, 0, 0, 0, 0,
831 /* Move the new buffer storage to the old pipe_resource. */
832 si_replace_buffer_storage(&sctx
->b
, &res
->b
.b
, newb
);
833 pipe_resource_reference(&newb
, NULL
);
835 assert(res
->b
.b
.bind
& PIPE_BIND_SHARED
);
836 assert(res
->flags
& RADEON_FLAG_NO_SUBALLOC
);
846 sctx
->b
.flush(&sctx
->b
, NULL
, 0);
848 if (res
->b
.is_shared
) {
849 /* USAGE_EXPLICIT_FLUSH must be cleared if at least one user
852 res
->external_usage
|= usage
& ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
;
853 if (!(usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
))
854 res
->external_usage
&= ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
;
856 res
->b
.is_shared
= true;
857 res
->external_usage
= usage
;
860 return sscreen
->ws
->buffer_get_handle(res
->buf
, stride
, offset
,
861 slice_size
, whandle
);
864 static void si_texture_destroy(struct pipe_screen
*screen
,
865 struct pipe_resource
*ptex
)
867 struct si_texture
*tex
= (struct si_texture
*)ptex
;
868 struct r600_resource
*resource
= &tex
->buffer
;
870 si_texture_reference(&tex
->flushed_depth_texture
, NULL
);
872 if (tex
->cmask_buffer
!= &tex
->buffer
) {
873 r600_resource_reference(&tex
->cmask_buffer
, NULL
);
875 pb_reference(&resource
->buf
, NULL
);
876 r600_resource_reference(&tex
->dcc_separate_buffer
, NULL
);
877 r600_resource_reference(&tex
->last_dcc_separate_buffer
, NULL
);
881 static const struct u_resource_vtbl si_texture_vtbl
;
883 static void si_texture_get_htile_size(struct si_screen
*sscreen
,
884 struct si_texture
*tex
)
886 unsigned cl_width
, cl_height
, width
, height
;
887 unsigned slice_elements
, slice_bytes
, pipe_interleave_bytes
, base_align
;
888 unsigned num_pipes
= sscreen
->info
.num_tile_pipes
;
890 assert(sscreen
->info
.chip_class
<= VI
);
892 tex
->surface
.htile_size
= 0;
894 if (tex
->surface
.u
.legacy
.level
[0].mode
== RADEON_SURF_MODE_1D
&&
895 !sscreen
->info
.htile_cmask_support_1d_tiling
)
898 /* Overalign HTILE on P2 configs to work around GPU hangs in
899 * piglit/depthstencil-render-miplevels 585.
901 * This has been confirmed to help Kabini & Stoney, where the hangs
902 * are always reproducible. I think I have seen the test hang
903 * on Carrizo too, though it was very rare there.
905 if (sscreen
->info
.chip_class
>= CIK
&& num_pipes
< 4)
934 width
= align(tex
->surface
.u
.legacy
.level
[0].nblk_x
, cl_width
* 8);
935 height
= align(tex
->surface
.u
.legacy
.level
[0].nblk_y
, cl_height
* 8);
937 slice_elements
= (width
* height
) / (8 * 8);
938 slice_bytes
= slice_elements
* 4;
940 pipe_interleave_bytes
= sscreen
->info
.pipe_interleave_bytes
;
941 base_align
= num_pipes
* pipe_interleave_bytes
;
943 tex
->surface
.htile_alignment
= base_align
;
944 tex
->surface
.htile_size
=
945 util_num_layers(&tex
->buffer
.b
.b
, 0) *
946 align(slice_bytes
, base_align
);
949 static void si_texture_allocate_htile(struct si_screen
*sscreen
,
950 struct si_texture
*tex
)
952 if (sscreen
->info
.chip_class
<= VI
&& !tex
->tc_compatible_htile
)
953 si_texture_get_htile_size(sscreen
, tex
);
955 if (!tex
->surface
.htile_size
)
958 tex
->htile_offset
= align(tex
->size
, tex
->surface
.htile_alignment
);
959 tex
->size
= tex
->htile_offset
+ tex
->surface
.htile_size
;
962 void si_print_texture_info(struct si_screen
*sscreen
,
963 struct si_texture
*tex
, struct u_log_context
*log
)
967 /* Common parameters. */
968 u_log_printf(log
, " Info: npix_x=%u, npix_y=%u, npix_z=%u, blk_w=%u, "
969 "blk_h=%u, array_size=%u, last_level=%u, "
970 "bpe=%u, nsamples=%u, flags=0x%x, %s\n",
971 tex
->buffer
.b
.b
.width0
, tex
->buffer
.b
.b
.height0
,
972 tex
->buffer
.b
.b
.depth0
, tex
->surface
.blk_w
,
974 tex
->buffer
.b
.b
.array_size
, tex
->buffer
.b
.b
.last_level
,
975 tex
->surface
.bpe
, tex
->buffer
.b
.b
.nr_samples
,
976 tex
->surface
.flags
, util_format_short_name(tex
->buffer
.b
.b
.format
));
978 if (sscreen
->info
.chip_class
>= GFX9
) {
979 u_log_printf(log
, " Surf: size=%"PRIu64
", slice_size=%"PRIu64
", "
980 "alignment=%u, swmode=%u, epitch=%u, pitch=%u\n",
981 tex
->surface
.surf_size
,
982 tex
->surface
.u
.gfx9
.surf_slice_size
,
983 tex
->surface
.surf_alignment
,
984 tex
->surface
.u
.gfx9
.surf
.swizzle_mode
,
985 tex
->surface
.u
.gfx9
.surf
.epitch
,
986 tex
->surface
.u
.gfx9
.surf_pitch
);
988 if (tex
->surface
.fmask_size
) {
989 u_log_printf(log
, " FMASK: offset=%"PRIu64
", size=%"PRIu64
", "
990 "alignment=%u, swmode=%u, epitch=%u\n",
992 tex
->surface
.fmask_size
,
993 tex
->surface
.fmask_alignment
,
994 tex
->surface
.u
.gfx9
.fmask
.swizzle_mode
,
995 tex
->surface
.u
.gfx9
.fmask
.epitch
);
998 if (tex
->cmask_buffer
) {
999 u_log_printf(log
, " CMask: offset=%"PRIu64
", size=%u, "
1000 "alignment=%u, rb_aligned=%u, pipe_aligned=%u\n",
1002 tex
->surface
.cmask_size
,
1003 tex
->surface
.cmask_alignment
,
1004 tex
->surface
.u
.gfx9
.cmask
.rb_aligned
,
1005 tex
->surface
.u
.gfx9
.cmask
.pipe_aligned
);
1008 if (tex
->htile_offset
) {
1009 u_log_printf(log
, " HTile: offset=%"PRIu64
", size=%u, alignment=%u, "
1010 "rb_aligned=%u, pipe_aligned=%u\n",
1012 tex
->surface
.htile_size
,
1013 tex
->surface
.htile_alignment
,
1014 tex
->surface
.u
.gfx9
.htile
.rb_aligned
,
1015 tex
->surface
.u
.gfx9
.htile
.pipe_aligned
);
1018 if (tex
->dcc_offset
) {
1019 u_log_printf(log
, " DCC: offset=%"PRIu64
", size=%u, "
1020 "alignment=%u, pitch_max=%u, num_dcc_levels=%u\n",
1021 tex
->dcc_offset
, tex
->surface
.dcc_size
,
1022 tex
->surface
.dcc_alignment
,
1023 tex
->surface
.u
.gfx9
.dcc_pitch_max
,
1024 tex
->surface
.num_dcc_levels
);
1027 if (tex
->surface
.u
.gfx9
.stencil_offset
) {
1028 u_log_printf(log
, " Stencil: offset=%"PRIu64
", swmode=%u, epitch=%u\n",
1029 tex
->surface
.u
.gfx9
.stencil_offset
,
1030 tex
->surface
.u
.gfx9
.stencil
.swizzle_mode
,
1031 tex
->surface
.u
.gfx9
.stencil
.epitch
);
1036 u_log_printf(log
, " Layout: size=%"PRIu64
", alignment=%u, bankw=%u, "
1037 "bankh=%u, nbanks=%u, mtilea=%u, tilesplit=%u, pipeconfig=%u, scanout=%u\n",
1038 tex
->surface
.surf_size
, tex
->surface
.surf_alignment
, tex
->surface
.u
.legacy
.bankw
,
1039 tex
->surface
.u
.legacy
.bankh
, tex
->surface
.u
.legacy
.num_banks
, tex
->surface
.u
.legacy
.mtilea
,
1040 tex
->surface
.u
.legacy
.tile_split
, tex
->surface
.u
.legacy
.pipe_config
,
1041 (tex
->surface
.flags
& RADEON_SURF_SCANOUT
) != 0);
1043 if (tex
->surface
.fmask_size
)
1044 u_log_printf(log
, " FMask: offset=%"PRIu64
", size=%"PRIu64
", alignment=%u, pitch_in_pixels=%u, "
1045 "bankh=%u, slice_tile_max=%u, tile_mode_index=%u\n",
1046 tex
->fmask_offset
, tex
->surface
.fmask_size
, tex
->surface
.fmask_alignment
,
1047 tex
->surface
.u
.legacy
.fmask
.pitch_in_pixels
,
1048 tex
->surface
.u
.legacy
.fmask
.bankh
,
1049 tex
->surface
.u
.legacy
.fmask
.slice_tile_max
,
1050 tex
->surface
.u
.legacy
.fmask
.tiling_index
);
1052 if (tex
->cmask_buffer
)
1053 u_log_printf(log
, " CMask: offset=%"PRIu64
", size=%u, alignment=%u, "
1054 "slice_tile_max=%u\n",
1055 tex
->cmask_offset
, tex
->surface
.cmask_size
, tex
->surface
.cmask_alignment
,
1056 tex
->surface
.u
.legacy
.cmask_slice_tile_max
);
1058 if (tex
->htile_offset
)
1059 u_log_printf(log
, " HTile: offset=%"PRIu64
", size=%u, "
1060 "alignment=%u, TC_compatible = %u\n",
1061 tex
->htile_offset
, tex
->surface
.htile_size
,
1062 tex
->surface
.htile_alignment
,
1063 tex
->tc_compatible_htile
);
1065 if (tex
->dcc_offset
) {
1066 u_log_printf(log
, " DCC: offset=%"PRIu64
", size=%u, alignment=%u\n",
1067 tex
->dcc_offset
, tex
->surface
.dcc_size
,
1068 tex
->surface
.dcc_alignment
);
1069 for (i
= 0; i
<= tex
->buffer
.b
.b
.last_level
; i
++)
1070 u_log_printf(log
, " DCCLevel[%i]: enabled=%u, offset=%u, "
1071 "fast_clear_size=%u\n",
1072 i
, i
< tex
->surface
.num_dcc_levels
,
1073 tex
->surface
.u
.legacy
.level
[i
].dcc_offset
,
1074 tex
->surface
.u
.legacy
.level
[i
].dcc_fast_clear_size
);
1077 for (i
= 0; i
<= tex
->buffer
.b
.b
.last_level
; i
++)
1078 u_log_printf(log
, " Level[%i]: offset=%"PRIu64
", slice_size=%"PRIu64
", "
1079 "npix_x=%u, npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
1080 "mode=%u, tiling_index = %u\n",
1081 i
, tex
->surface
.u
.legacy
.level
[i
].offset
,
1082 (uint64_t)tex
->surface
.u
.legacy
.level
[i
].slice_size_dw
* 4,
1083 u_minify(tex
->buffer
.b
.b
.width0
, i
),
1084 u_minify(tex
->buffer
.b
.b
.height0
, i
),
1085 u_minify(tex
->buffer
.b
.b
.depth0
, i
),
1086 tex
->surface
.u
.legacy
.level
[i
].nblk_x
,
1087 tex
->surface
.u
.legacy
.level
[i
].nblk_y
,
1088 tex
->surface
.u
.legacy
.level
[i
].mode
,
1089 tex
->surface
.u
.legacy
.tiling_index
[i
]);
1091 if (tex
->surface
.has_stencil
) {
1092 u_log_printf(log
, " StencilLayout: tilesplit=%u\n",
1093 tex
->surface
.u
.legacy
.stencil_tile_split
);
1094 for (i
= 0; i
<= tex
->buffer
.b
.b
.last_level
; i
++) {
1095 u_log_printf(log
, " StencilLevel[%i]: offset=%"PRIu64
", "
1096 "slice_size=%"PRIu64
", npix_x=%u, "
1097 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
1098 "mode=%u, tiling_index = %u\n",
1099 i
, tex
->surface
.u
.legacy
.stencil_level
[i
].offset
,
1100 (uint64_t)tex
->surface
.u
.legacy
.stencil_level
[i
].slice_size_dw
* 4,
1101 u_minify(tex
->buffer
.b
.b
.width0
, i
),
1102 u_minify(tex
->buffer
.b
.b
.height0
, i
),
1103 u_minify(tex
->buffer
.b
.b
.depth0
, i
),
1104 tex
->surface
.u
.legacy
.stencil_level
[i
].nblk_x
,
1105 tex
->surface
.u
.legacy
.stencil_level
[i
].nblk_y
,
1106 tex
->surface
.u
.legacy
.stencil_level
[i
].mode
,
1107 tex
->surface
.u
.legacy
.stencil_tiling_index
[i
]);
1112 /* Common processing for si_texture_create and si_texture_from_handle */
1113 static struct si_texture
*
1114 si_texture_create_object(struct pipe_screen
*screen
,
1115 const struct pipe_resource
*base
,
1116 struct pb_buffer
*buf
,
1117 struct radeon_surf
*surface
)
1119 struct si_texture
*tex
;
1120 struct r600_resource
*resource
;
1121 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
1123 tex
= CALLOC_STRUCT(si_texture
);
1127 resource
= &tex
->buffer
;
1128 resource
->b
.b
= *base
;
1129 resource
->b
.b
.next
= NULL
;
1130 resource
->b
.vtbl
= &si_texture_vtbl
;
1131 pipe_reference_init(&resource
->b
.b
.reference
, 1);
1132 resource
->b
.b
.screen
= screen
;
1134 /* don't include stencil-only formats which we don't support for rendering */
1135 tex
->is_depth
= util_format_has_depth(util_format_description(tex
->buffer
.b
.b
.format
));
1137 tex
->surface
= *surface
;
1138 tex
->size
= tex
->surface
.surf_size
;
1140 tex
->tc_compatible_htile
= tex
->surface
.htile_size
!= 0 &&
1141 (tex
->surface
.flags
&
1142 RADEON_SURF_TC_COMPATIBLE_HTILE
);
1144 /* TC-compatible HTILE:
1145 * - VI only supports Z32_FLOAT.
1146 * - GFX9 only supports Z32_FLOAT and Z16_UNORM. */
1147 if (tex
->tc_compatible_htile
) {
1148 if (sscreen
->info
.chip_class
>= GFX9
&&
1149 base
->format
== PIPE_FORMAT_Z16_UNORM
)
1150 tex
->db_render_format
= base
->format
;
1152 tex
->db_render_format
= PIPE_FORMAT_Z32_FLOAT
;
1153 tex
->upgraded_depth
= base
->format
!= PIPE_FORMAT_Z32_FLOAT
&&
1154 base
->format
!= PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
;
1157 tex
->db_render_format
= base
->format
;
1160 /* Applies to GCN. */
1161 tex
->last_msaa_resolve_target_micro_mode
= tex
->surface
.micro_tile_mode
;
1163 /* Disable separate DCC at the beginning. DRI2 doesn't reuse buffers
1164 * between frames, so the only thing that can enable separate DCC
1165 * with DRI2 is multiple slow clears within a frame.
1167 tex
->ps_draw_ratio
= 0;
1169 if (tex
->is_depth
) {
1170 if (sscreen
->info
.chip_class
>= GFX9
) {
1171 tex
->can_sample_z
= true;
1172 tex
->can_sample_s
= true;
1174 tex
->can_sample_z
= !tex
->surface
.u
.legacy
.depth_adjusted
;
1175 tex
->can_sample_s
= !tex
->surface
.u
.legacy
.stencil_adjusted
;
1178 if (!(base
->flags
& (SI_RESOURCE_FLAG_TRANSFER
|
1179 SI_RESOURCE_FLAG_FLUSHED_DEPTH
))) {
1180 tex
->db_compatible
= true;
1182 if (!(sscreen
->debug_flags
& DBG(NO_HYPERZ
)))
1183 si_texture_allocate_htile(sscreen
, tex
);
1186 if (base
->nr_samples
> 1 &&
1188 !(sscreen
->debug_flags
& DBG(NO_FMASK
))) {
1189 /* Allocate FMASK. */
1190 tex
->fmask_offset
= align64(tex
->size
,
1191 tex
->surface
.fmask_alignment
);
1192 tex
->size
= tex
->fmask_offset
+ tex
->surface
.fmask_size
;
1194 /* Allocate CMASK. */
1195 tex
->cmask_offset
= align64(tex
->size
, tex
->surface
.cmask_alignment
);
1196 tex
->size
= tex
->cmask_offset
+ tex
->surface
.cmask_size
;
1197 tex
->cb_color_info
|= S_028C70_FAST_CLEAR(1);
1198 tex
->cmask_buffer
= &tex
->buffer
;
1200 if (!tex
->surface
.fmask_size
|| !tex
->surface
.cmask_size
) {
1206 /* Shared textures must always set up DCC here.
1207 * If it's not present, it will be disabled by
1208 * apply_opaque_metadata later.
1210 if (tex
->surface
.dcc_size
&&
1211 (buf
|| !(sscreen
->debug_flags
& DBG(NO_DCC
))) &&
1212 !(tex
->surface
.flags
& RADEON_SURF_SCANOUT
)) {
1213 /* Reserve space for the DCC buffer. */
1214 tex
->dcc_offset
= align64(tex
->size
, tex
->surface
.dcc_alignment
);
1215 tex
->size
= tex
->dcc_offset
+ tex
->surface
.dcc_size
;
1219 /* Now create the backing buffer. */
1221 si_init_resource_fields(sscreen
, resource
, tex
->size
,
1222 tex
->surface
.surf_alignment
);
1224 if (!si_alloc_resource(sscreen
, resource
)) {
1229 resource
->buf
= buf
;
1230 resource
->gpu_address
= sscreen
->ws
->buffer_get_virtual_address(resource
->buf
);
1231 resource
->bo_size
= buf
->size
;
1232 resource
->bo_alignment
= buf
->alignment
;
1233 resource
->domains
= sscreen
->ws
->buffer_get_initial_domain(resource
->buf
);
1234 if (resource
->domains
& RADEON_DOMAIN_VRAM
)
1235 resource
->vram_usage
= buf
->size
;
1236 else if (resource
->domains
& RADEON_DOMAIN_GTT
)
1237 resource
->gart_usage
= buf
->size
;
1240 if (tex
->cmask_buffer
) {
1241 /* Initialize the cmask to 0xCC (= compressed state). */
1242 si_screen_clear_buffer(sscreen
, &tex
->cmask_buffer
->b
.b
,
1243 tex
->cmask_offset
, tex
->surface
.cmask_size
,
1246 if (tex
->htile_offset
) {
1247 uint32_t clear_value
= 0;
1249 if (sscreen
->info
.chip_class
>= GFX9
|| tex
->tc_compatible_htile
)
1250 clear_value
= 0x0000030F;
1252 si_screen_clear_buffer(sscreen
, &tex
->buffer
.b
.b
,
1254 tex
->surface
.htile_size
,
1258 /* Initialize DCC only if the texture is not being imported. */
1259 if (!buf
&& tex
->dcc_offset
) {
1260 si_screen_clear_buffer(sscreen
, &tex
->buffer
.b
.b
,
1262 tex
->surface
.dcc_size
,
1266 /* Initialize the CMASK base register value. */
1267 tex
->cmask_base_address_reg
=
1268 (tex
->buffer
.gpu_address
+ tex
->cmask_offset
) >> 8;
1270 if (sscreen
->debug_flags
& DBG(VM
)) {
1271 fprintf(stderr
, "VM start=0x%"PRIX64
" end=0x%"PRIX64
" | Texture %ix%ix%i, %i levels, %i samples, %s\n",
1272 tex
->buffer
.gpu_address
,
1273 tex
->buffer
.gpu_address
+ tex
->buffer
.buf
->size
,
1274 base
->width0
, base
->height0
, util_num_layers(base
, 0), base
->last_level
+1,
1275 base
->nr_samples
? base
->nr_samples
: 1, util_format_short_name(base
->format
));
1278 if (sscreen
->debug_flags
& DBG(TEX
)) {
1280 struct u_log_context log
;
1281 u_log_context_init(&log
);
1282 si_print_texture_info(sscreen
, tex
, &log
);
1283 u_log_new_page_print(&log
, stdout
);
1285 u_log_context_destroy(&log
);
1291 static enum radeon_surf_mode
1292 si_choose_tiling(struct si_screen
*sscreen
,
1293 const struct pipe_resource
*templ
, bool tc_compatible_htile
)
1295 const struct util_format_description
*desc
= util_format_description(templ
->format
);
1296 bool force_tiling
= templ
->flags
& SI_RESOURCE_FLAG_FORCE_MSAA_TILING
;
1297 bool is_depth_stencil
= util_format_is_depth_or_stencil(templ
->format
) &&
1298 !(templ
->flags
& SI_RESOURCE_FLAG_FLUSHED_DEPTH
);
1300 /* MSAA resources must be 2D tiled. */
1301 if (templ
->nr_samples
> 1)
1302 return RADEON_SURF_MODE_2D
;
1304 /* Transfer resources should be linear. */
1305 if (templ
->flags
& SI_RESOURCE_FLAG_TRANSFER
)
1306 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1308 /* Avoid Z/S decompress blits by forcing TC-compatible HTILE on VI,
1309 * which requires 2D tiling.
1311 if (sscreen
->info
.chip_class
== VI
&& tc_compatible_htile
)
1312 return RADEON_SURF_MODE_2D
;
1314 /* Handle common candidates for the linear mode.
1315 * Compressed textures and DB surfaces must always be tiled.
1317 if (!force_tiling
&&
1318 !is_depth_stencil
&&
1319 !util_format_is_compressed(templ
->format
)) {
1320 if (sscreen
->debug_flags
& DBG(NO_TILING
))
1321 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1323 /* Tiling doesn't work with the 422 (SUBSAMPLED) formats. */
1324 if (desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
)
1325 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1327 /* Cursors are linear on SI.
1328 * (XXX double-check, maybe also use RADEON_SURF_SCANOUT) */
1329 if (templ
->bind
& PIPE_BIND_CURSOR
)
1330 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1332 if (templ
->bind
& PIPE_BIND_LINEAR
)
1333 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1335 /* Textures with a very small height are recommended to be linear. */
1336 if (templ
->target
== PIPE_TEXTURE_1D
||
1337 templ
->target
== PIPE_TEXTURE_1D_ARRAY
||
1338 /* Only very thin and long 2D textures should benefit from
1339 * linear_aligned. */
1340 (templ
->width0
> 8 && templ
->height0
<= 2))
1341 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1343 /* Textures likely to be mapped often. */
1344 if (templ
->usage
== PIPE_USAGE_STAGING
||
1345 templ
->usage
== PIPE_USAGE_STREAM
)
1346 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1349 /* Make small textures 1D tiled. */
1350 if (templ
->width0
<= 16 || templ
->height0
<= 16 ||
1351 (sscreen
->debug_flags
& DBG(NO_2D_TILING
)))
1352 return RADEON_SURF_MODE_1D
;
1354 /* The allocator will switch to 1D if needed. */
1355 return RADEON_SURF_MODE_2D
;
1358 struct pipe_resource
*si_texture_create(struct pipe_screen
*screen
,
1359 const struct pipe_resource
*templ
)
1361 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
1362 bool is_zs
= util_format_is_depth_or_stencil(templ
->format
);
1364 if (templ
->nr_samples
>= 2) {
1365 /* This is hackish (overwriting the const pipe_resource template),
1366 * but should be harmless and state trackers can also see
1367 * the overriden number of samples in the created pipe_resource.
1369 if (is_zs
&& sscreen
->eqaa_force_z_samples
) {
1370 ((struct pipe_resource
*)templ
)->nr_samples
=
1371 ((struct pipe_resource
*)templ
)->nr_storage_samples
=
1372 sscreen
->eqaa_force_z_samples
;
1373 } else if (!is_zs
&& sscreen
->eqaa_force_color_samples
) {
1374 ((struct pipe_resource
*)templ
)->nr_samples
=
1375 sscreen
->eqaa_force_coverage_samples
;
1376 ((struct pipe_resource
*)templ
)->nr_storage_samples
=
1377 sscreen
->eqaa_force_color_samples
;
1381 struct radeon_surf surface
= {0};
1382 bool is_flushed_depth
= templ
->flags
& SI_RESOURCE_FLAG_FLUSHED_DEPTH
;
1383 bool tc_compatible_htile
=
1384 sscreen
->info
.chip_class
>= VI
&&
1385 /* There are issues with TC-compatible HTILE on Tonga (and
1386 * Iceland is the same design), and documented bug workarounds
1387 * don't help. For example, this fails:
1388 * piglit/bin/tex-miplevel-selection 'texture()' 2DShadow -auto
1390 sscreen
->info
.family
!= CHIP_TONGA
&&
1391 sscreen
->info
.family
!= CHIP_ICELAND
&&
1392 (templ
->flags
& PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY
) &&
1393 !(sscreen
->debug_flags
& DBG(NO_HYPERZ
)) &&
1394 !is_flushed_depth
&&
1395 templ
->nr_samples
<= 1 && /* TC-compat HTILE is less efficient with MSAA */
1399 r
= si_init_surface(sscreen
, &surface
, templ
,
1400 si_choose_tiling(sscreen
, templ
, tc_compatible_htile
),
1401 0, 0, false, false, is_flushed_depth
,
1402 tc_compatible_htile
);
1407 return (struct pipe_resource
*)
1408 si_texture_create_object(screen
, templ
, NULL
, &surface
);
1411 static struct pipe_resource
*si_texture_from_winsys_buffer(struct si_screen
*sscreen
,
1412 const struct pipe_resource
*templ
,
1413 struct pb_buffer
*buf
,
1419 enum radeon_surf_mode array_mode
;
1420 struct radeon_surf surface
= {};
1421 struct radeon_bo_metadata metadata
= {};
1422 struct si_texture
*tex
;
1427 sscreen
->ws
->buffer_get_metadata(buf
, &metadata
);
1428 si_surface_import_metadata(sscreen
, &surface
, &metadata
,
1429 &array_mode
, &is_scanout
);
1432 * The bo metadata is unset for un-dedicated images. So we fall
1433 * back to linear. See answer to question 5 of the
1434 * VK_KHX_external_memory spec for some details.
1436 * It is possible that this case isn't going to work if the
1437 * surface pitch isn't correctly aligned by default.
1439 * In order to support it correctly we require multi-image
1440 * metadata to be syncrhonized between radv and radeonsi. The
1441 * semantics of associating multiple image metadata to a memory
1442 * object on the vulkan export side are not concretely defined
1445 * All the use cases we are aware of at the moment for memory
1446 * objects use dedicated allocations. So lets keep the initial
1447 * implementation simple.
1449 * A possible alternative is to attempt to reconstruct the
1450 * tiling information when the TexParameter TEXTURE_TILING_EXT
1453 array_mode
= RADEON_SURF_MODE_LINEAR_ALIGNED
;
1457 r
= si_init_surface(sscreen
, &surface
, templ
,
1458 array_mode
, stride
, offset
, true, is_scanout
,
1463 tex
= si_texture_create_object(&sscreen
->b
, templ
, buf
, &surface
);
1467 tex
->buffer
.b
.is_shared
= true;
1468 tex
->buffer
.external_usage
= usage
;
1470 si_apply_opaque_metadata(sscreen
, tex
, &metadata
);
1472 assert(tex
->surface
.tile_swizzle
== 0);
1473 return &tex
->buffer
.b
.b
;
1476 static struct pipe_resource
*si_texture_from_handle(struct pipe_screen
*screen
,
1477 const struct pipe_resource
*templ
,
1478 struct winsys_handle
*whandle
,
1481 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
1482 struct pb_buffer
*buf
= NULL
;
1483 unsigned stride
= 0, offset
= 0;
1485 /* Support only 2D textures without mipmaps */
1486 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
) ||
1487 templ
->depth0
!= 1 || templ
->last_level
!= 0)
1490 buf
= sscreen
->ws
->buffer_from_handle(sscreen
->ws
, whandle
,
1491 sscreen
->info
.max_alignment
,
1496 return si_texture_from_winsys_buffer(sscreen
, templ
, buf
, stride
,
1497 offset
, usage
, true);
1500 bool si_init_flushed_depth_texture(struct pipe_context
*ctx
,
1501 struct pipe_resource
*texture
,
1502 struct si_texture
**staging
)
1504 struct si_texture
*tex
= (struct si_texture
*)texture
;
1505 struct pipe_resource resource
;
1506 struct si_texture
**flushed_depth_texture
= staging
?
1507 staging
: &tex
->flushed_depth_texture
;
1508 enum pipe_format pipe_format
= texture
->format
;
1511 if (tex
->flushed_depth_texture
)
1512 return true; /* it's ready */
1514 if (!tex
->can_sample_z
&& tex
->can_sample_s
) {
1515 switch (pipe_format
) {
1516 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1517 /* Save memory by not allocating the S plane. */
1518 pipe_format
= PIPE_FORMAT_Z32_FLOAT
;
1520 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1521 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1522 /* Save memory bandwidth by not copying the
1523 * stencil part during flush.
1525 * This potentially increases memory bandwidth
1526 * if an application uses both Z and S texturing
1527 * simultaneously (a flushed Z24S8 texture
1528 * would be stored compactly), but how often
1529 * does that really happen?
1531 pipe_format
= PIPE_FORMAT_Z24X8_UNORM
;
1535 } else if (!tex
->can_sample_s
&& tex
->can_sample_z
) {
1536 assert(util_format_has_stencil(util_format_description(pipe_format
)));
1538 /* DB->CB copies to an 8bpp surface don't work. */
1539 pipe_format
= PIPE_FORMAT_X24S8_UINT
;
1543 memset(&resource
, 0, sizeof(resource
));
1544 resource
.target
= texture
->target
;
1545 resource
.format
= pipe_format
;
1546 resource
.width0
= texture
->width0
;
1547 resource
.height0
= texture
->height0
;
1548 resource
.depth0
= texture
->depth0
;
1549 resource
.array_size
= texture
->array_size
;
1550 resource
.last_level
= texture
->last_level
;
1551 resource
.nr_samples
= texture
->nr_samples
;
1552 resource
.usage
= staging
? PIPE_USAGE_STAGING
: PIPE_USAGE_DEFAULT
;
1553 resource
.bind
= texture
->bind
& ~PIPE_BIND_DEPTH_STENCIL
;
1554 resource
.flags
= texture
->flags
| SI_RESOURCE_FLAG_FLUSHED_DEPTH
;
1557 resource
.flags
|= SI_RESOURCE_FLAG_TRANSFER
;
1559 *flushed_depth_texture
= (struct si_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
1560 if (*flushed_depth_texture
== NULL
) {
1561 PRINT_ERR("failed to create temporary texture to hold flushed depth\n");
1568 * Initialize the pipe_resource descriptor to be of the same size as the box,
1569 * which is supposed to hold a subregion of the texture "orig" at the given
1572 static void si_init_temp_resource_from_box(struct pipe_resource
*res
,
1573 struct pipe_resource
*orig
,
1574 const struct pipe_box
*box
,
1575 unsigned level
, unsigned flags
)
1577 memset(res
, 0, sizeof(*res
));
1578 res
->format
= orig
->format
;
1579 res
->width0
= box
->width
;
1580 res
->height0
= box
->height
;
1582 res
->array_size
= 1;
1583 res
->usage
= flags
& SI_RESOURCE_FLAG_TRANSFER
? PIPE_USAGE_STAGING
: PIPE_USAGE_DEFAULT
;
1586 /* We must set the correct texture target and dimensions for a 3D box. */
1587 if (box
->depth
> 1 && util_max_layer(orig
, level
) > 0) {
1588 res
->target
= PIPE_TEXTURE_2D_ARRAY
;
1589 res
->array_size
= box
->depth
;
1591 res
->target
= PIPE_TEXTURE_2D
;
1595 static bool si_can_invalidate_texture(struct si_screen
*sscreen
,
1596 struct si_texture
*tex
,
1597 unsigned transfer_usage
,
1598 const struct pipe_box
*box
)
1600 return !tex
->buffer
.b
.is_shared
&&
1601 !(transfer_usage
& PIPE_TRANSFER_READ
) &&
1602 tex
->buffer
.b
.b
.last_level
== 0 &&
1603 util_texrange_covers_whole_level(&tex
->buffer
.b
.b
, 0,
1604 box
->x
, box
->y
, box
->z
,
1605 box
->width
, box
->height
,
1609 static void si_texture_invalidate_storage(struct si_context
*sctx
,
1610 struct si_texture
*tex
)
1612 struct si_screen
*sscreen
= sctx
->screen
;
1614 /* There is no point in discarding depth and tiled buffers. */
1615 assert(!tex
->is_depth
);
1616 assert(tex
->surface
.is_linear
);
1618 /* Reallocate the buffer in the same pipe_resource. */
1619 si_alloc_resource(sscreen
, &tex
->buffer
);
1621 /* Initialize the CMASK base address (needed even without CMASK). */
1622 tex
->cmask_base_address_reg
=
1623 (tex
->buffer
.gpu_address
+ tex
->cmask_offset
) >> 8;
1625 p_atomic_inc(&sscreen
->dirty_tex_counter
);
1627 sctx
->num_alloc_tex_transfer_bytes
+= tex
->size
;
1630 static void *si_texture_transfer_map(struct pipe_context
*ctx
,
1631 struct pipe_resource
*texture
,
1634 const struct pipe_box
*box
,
1635 struct pipe_transfer
**ptransfer
)
1637 struct si_context
*sctx
= (struct si_context
*)ctx
;
1638 struct si_texture
*tex
= (struct si_texture
*)texture
;
1639 struct si_transfer
*trans
;
1640 struct r600_resource
*buf
;
1641 unsigned offset
= 0;
1643 bool use_staging_texture
= false;
1645 assert(!(texture
->flags
& SI_RESOURCE_FLAG_TRANSFER
));
1646 assert(box
->width
&& box
->height
&& box
->depth
);
1648 /* Depth textures use staging unconditionally. */
1649 if (!tex
->is_depth
) {
1650 /* Degrade the tile mode if we get too many transfers on APUs.
1651 * On dGPUs, the staging texture is always faster.
1652 * Only count uploads that are at least 4x4 pixels large.
1654 if (!sctx
->screen
->info
.has_dedicated_vram
&&
1656 box
->width
>= 4 && box
->height
>= 4 &&
1657 p_atomic_inc_return(&tex
->num_level0_transfers
) == 10) {
1658 bool can_invalidate
=
1659 si_can_invalidate_texture(sctx
->screen
, tex
,
1662 si_reallocate_texture_inplace(sctx
, tex
,
1667 /* Tiled textures need to be converted into a linear texture for CPU
1668 * access. The staging texture is always linear and is placed in GART.
1670 * Reading from VRAM or GTT WC is slow, always use the staging
1671 * texture in this case.
1673 * Use the staging texture for uploads if the underlying BO
1676 if (!tex
->surface
.is_linear
)
1677 use_staging_texture
= true;
1678 else if (usage
& PIPE_TRANSFER_READ
)
1679 use_staging_texture
=
1680 tex
->buffer
.domains
& RADEON_DOMAIN_VRAM
||
1681 tex
->buffer
.flags
& RADEON_FLAG_GTT_WC
;
1682 /* Write & linear only: */
1683 else if (si_rings_is_buffer_referenced(sctx
, tex
->buffer
.buf
,
1684 RADEON_USAGE_READWRITE
) ||
1685 !sctx
->ws
->buffer_wait(tex
->buffer
.buf
, 0,
1686 RADEON_USAGE_READWRITE
)) {
1688 if (si_can_invalidate_texture(sctx
->screen
, tex
,
1690 si_texture_invalidate_storage(sctx
, tex
);
1692 use_staging_texture
= true;
1696 trans
= CALLOC_STRUCT(si_transfer
);
1699 pipe_resource_reference(&trans
->b
.b
.resource
, texture
);
1700 trans
->b
.b
.level
= level
;
1701 trans
->b
.b
.usage
= usage
;
1702 trans
->b
.b
.box
= *box
;
1704 if (tex
->is_depth
) {
1705 struct si_texture
*staging_depth
;
1707 if (tex
->buffer
.b
.b
.nr_samples
> 1) {
1708 /* MSAA depth buffers need to be converted to single sample buffers.
1710 * Mapping MSAA depth buffers can occur if ReadPixels is called
1711 * with a multisample GLX visual.
1713 * First downsample the depth buffer to a temporary texture,
1714 * then decompress the temporary one to staging.
1716 * Only the region being mapped is transfered.
1718 struct pipe_resource resource
;
1720 si_init_temp_resource_from_box(&resource
, texture
, box
, level
, 0);
1722 if (!si_init_flushed_depth_texture(ctx
, &resource
, &staging_depth
)) {
1723 PRINT_ERR("failed to create temporary texture to hold untiled copy\n");
1727 if (usage
& PIPE_TRANSFER_READ
) {
1728 struct pipe_resource
*temp
= ctx
->screen
->resource_create(ctx
->screen
, &resource
);
1730 PRINT_ERR("failed to create a temporary depth texture\n");
1734 si_copy_region_with_blit(ctx
, temp
, 0, 0, 0, 0, texture
, level
, box
);
1735 si_blit_decompress_depth(ctx
, (struct si_texture
*)temp
, staging_depth
,
1736 0, 0, 0, box
->depth
, 0, 0);
1737 pipe_resource_reference(&temp
, NULL
);
1740 /* Just get the strides. */
1741 si_texture_get_offset(sctx
->screen
, staging_depth
, level
, NULL
,
1743 &trans
->b
.b
.layer_stride
);
1745 /* XXX: only readback the rectangle which is being mapped? */
1746 /* XXX: when discard is true, no need to read back from depth texture */
1747 if (!si_init_flushed_depth_texture(ctx
, texture
, &staging_depth
)) {
1748 PRINT_ERR("failed to create temporary texture to hold untiled copy\n");
1752 si_blit_decompress_depth(ctx
, tex
, staging_depth
,
1754 box
->z
, box
->z
+ box
->depth
- 1,
1757 offset
= si_texture_get_offset(sctx
->screen
, staging_depth
,
1760 &trans
->b
.b
.layer_stride
);
1763 trans
->staging
= &staging_depth
->buffer
;
1764 buf
= trans
->staging
;
1765 } else if (use_staging_texture
) {
1766 struct pipe_resource resource
;
1767 struct si_texture
*staging
;
1769 si_init_temp_resource_from_box(&resource
, texture
, box
, level
,
1770 SI_RESOURCE_FLAG_TRANSFER
);
1771 resource
.usage
= (usage
& PIPE_TRANSFER_READ
) ?
1772 PIPE_USAGE_STAGING
: PIPE_USAGE_STREAM
;
1774 /* Create the temporary texture. */
1775 staging
= (struct si_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
1777 PRINT_ERR("failed to create temporary texture to hold untiled copy\n");
1780 trans
->staging
= &staging
->buffer
;
1782 /* Just get the strides. */
1783 si_texture_get_offset(sctx
->screen
, staging
, 0, NULL
,
1785 &trans
->b
.b
.layer_stride
);
1787 if (usage
& PIPE_TRANSFER_READ
)
1788 si_copy_to_staging_texture(ctx
, trans
);
1790 usage
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
1792 buf
= trans
->staging
;
1794 /* the resource is mapped directly */
1795 offset
= si_texture_get_offset(sctx
->screen
, tex
, level
, box
,
1797 &trans
->b
.b
.layer_stride
);
1801 /* Always unmap texture CPU mappings on 32-bit architectures, so that
1802 * we don't run out of the CPU address space.
1804 if (sizeof(void*) == 4)
1805 usage
|= RADEON_TRANSFER_TEMPORARY
;
1807 if (!(map
= si_buffer_map_sync_with_rings(sctx
, buf
, usage
)))
1810 *ptransfer
= &trans
->b
.b
;
1811 return map
+ offset
;
1814 r600_resource_reference(&trans
->staging
, NULL
);
1815 pipe_resource_reference(&trans
->b
.b
.resource
, NULL
);
1820 static void si_texture_transfer_unmap(struct pipe_context
*ctx
,
1821 struct pipe_transfer
* transfer
)
1823 struct si_context
*sctx
= (struct si_context
*)ctx
;
1824 struct si_transfer
*stransfer
= (struct si_transfer
*)transfer
;
1825 struct pipe_resource
*texture
= transfer
->resource
;
1826 struct si_texture
*tex
= (struct si_texture
*)texture
;
1828 /* Always unmap texture CPU mappings on 32-bit architectures, so that
1829 * we don't run out of the CPU address space.
1831 if (sizeof(void*) == 4) {
1832 struct r600_resource
*buf
=
1833 stransfer
->staging
? stransfer
->staging
: &tex
->buffer
;
1835 sctx
->ws
->buffer_unmap(buf
->buf
);
1838 if ((transfer
->usage
& PIPE_TRANSFER_WRITE
) && stransfer
->staging
) {
1839 if (tex
->is_depth
&& tex
->buffer
.b
.b
.nr_samples
<= 1) {
1840 ctx
->resource_copy_region(ctx
, texture
, transfer
->level
,
1841 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
1842 &stransfer
->staging
->b
.b
, transfer
->level
,
1845 si_copy_from_staging_texture(ctx
, stransfer
);
1849 if (stransfer
->staging
) {
1850 sctx
->num_alloc_tex_transfer_bytes
+= stransfer
->staging
->buf
->size
;
1851 r600_resource_reference(&stransfer
->staging
, NULL
);
1854 /* Heuristic for {upload, draw, upload, draw, ..}:
1856 * Flush the gfx IB if we've allocated too much texture storage.
1858 * The idea is that we don't want to build IBs that use too much
1859 * memory and put pressure on the kernel memory manager and we also
1860 * want to make temporary and invalidated buffers go idle ASAP to
1861 * decrease the total memory usage or make them reusable. The memory
1862 * usage will be slightly higher than given here because of the buffer
1863 * cache in the winsys.
1865 * The result is that the kernel memory manager is never a bottleneck.
1867 if (sctx
->num_alloc_tex_transfer_bytes
> sctx
->screen
->info
.gart_size
/ 4) {
1868 si_flush_gfx_cs(sctx
, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW
, NULL
);
1869 sctx
->num_alloc_tex_transfer_bytes
= 0;
1872 pipe_resource_reference(&transfer
->resource
, NULL
);
1876 static const struct u_resource_vtbl si_texture_vtbl
=
1878 NULL
, /* get_handle */
1879 si_texture_destroy
, /* resource_destroy */
1880 si_texture_transfer_map
, /* transfer_map */
1881 u_default_transfer_flush_region
, /* transfer_flush_region */
1882 si_texture_transfer_unmap
, /* transfer_unmap */
1885 /* Return if it's allowed to reinterpret one format as another with DCC enabled.
1887 bool vi_dcc_formats_compatible(enum pipe_format format1
,
1888 enum pipe_format format2
)
1890 const struct util_format_description
*desc1
, *desc2
;
1892 /* No format change - exit early. */
1893 if (format1
== format2
)
1896 format1
= si_simplify_cb_format(format1
);
1897 format2
= si_simplify_cb_format(format2
);
1899 /* Check again after format adjustments. */
1900 if (format1
== format2
)
1903 desc1
= util_format_description(format1
);
1904 desc2
= util_format_description(format2
);
1906 if (desc1
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
||
1907 desc2
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1910 /* Float and non-float are totally incompatible. */
1911 if ((desc1
->channel
[0].type
== UTIL_FORMAT_TYPE_FLOAT
) !=
1912 (desc2
->channel
[0].type
== UTIL_FORMAT_TYPE_FLOAT
))
1915 /* Channel sizes must match across DCC formats.
1916 * Comparing just the first 2 channels should be enough.
1918 if (desc1
->channel
[0].size
!= desc2
->channel
[0].size
||
1919 (desc1
->nr_channels
>= 2 &&
1920 desc1
->channel
[1].size
!= desc2
->channel
[1].size
))
1923 /* Everything below is not needed if the driver never uses the DCC
1924 * clear code with the value of 1.
1927 /* If the clear values are all 1 or all 0, this constraint can be
1929 if (vi_alpha_is_on_msb(format1
) != vi_alpha_is_on_msb(format2
))
1932 /* Channel types must match if the clear value of 1 is used.
1933 * The type categories are only float, signed, unsigned.
1934 * NORM and INT are always compatible.
1936 if (desc1
->channel
[0].type
!= desc2
->channel
[0].type
||
1937 (desc1
->nr_channels
>= 2 &&
1938 desc1
->channel
[1].type
!= desc2
->channel
[1].type
))
1944 bool vi_dcc_formats_are_incompatible(struct pipe_resource
*tex
,
1946 enum pipe_format view_format
)
1948 struct si_texture
*stex
= (struct si_texture
*)tex
;
1950 return vi_dcc_enabled(stex
, level
) &&
1951 !vi_dcc_formats_compatible(tex
->format
, view_format
);
1954 /* This can't be merged with the above function, because
1955 * vi_dcc_formats_compatible should be called only when DCC is enabled. */
1956 void vi_disable_dcc_if_incompatible_format(struct si_context
*sctx
,
1957 struct pipe_resource
*tex
,
1959 enum pipe_format view_format
)
1961 struct si_texture
*stex
= (struct si_texture
*)tex
;
1963 if (vi_dcc_formats_are_incompatible(tex
, level
, view_format
))
1964 if (!si_texture_disable_dcc(sctx
, stex
))
1965 si_decompress_dcc(sctx
, stex
);
1968 struct pipe_surface
*si_create_surface_custom(struct pipe_context
*pipe
,
1969 struct pipe_resource
*texture
,
1970 const struct pipe_surface
*templ
,
1971 unsigned width0
, unsigned height0
,
1972 unsigned width
, unsigned height
)
1974 struct si_surface
*surface
= CALLOC_STRUCT(si_surface
);
1979 assert(templ
->u
.tex
.first_layer
<= util_max_layer(texture
, templ
->u
.tex
.level
));
1980 assert(templ
->u
.tex
.last_layer
<= util_max_layer(texture
, templ
->u
.tex
.level
));
1982 pipe_reference_init(&surface
->base
.reference
, 1);
1983 pipe_resource_reference(&surface
->base
.texture
, texture
);
1984 surface
->base
.context
= pipe
;
1985 surface
->base
.format
= templ
->format
;
1986 surface
->base
.width
= width
;
1987 surface
->base
.height
= height
;
1988 surface
->base
.u
= templ
->u
;
1990 surface
->width0
= width0
;
1991 surface
->height0
= height0
;
1993 surface
->dcc_incompatible
=
1994 texture
->target
!= PIPE_BUFFER
&&
1995 vi_dcc_formats_are_incompatible(texture
, templ
->u
.tex
.level
,
1997 return &surface
->base
;
2000 static struct pipe_surface
*si_create_surface(struct pipe_context
*pipe
,
2001 struct pipe_resource
*tex
,
2002 const struct pipe_surface
*templ
)
2004 unsigned level
= templ
->u
.tex
.level
;
2005 unsigned width
= u_minify(tex
->width0
, level
);
2006 unsigned height
= u_minify(tex
->height0
, level
);
2007 unsigned width0
= tex
->width0
;
2008 unsigned height0
= tex
->height0
;
2010 if (tex
->target
!= PIPE_BUFFER
&& templ
->format
!= tex
->format
) {
2011 const struct util_format_description
*tex_desc
2012 = util_format_description(tex
->format
);
2013 const struct util_format_description
*templ_desc
2014 = util_format_description(templ
->format
);
2016 assert(tex_desc
->block
.bits
== templ_desc
->block
.bits
);
2018 /* Adjust size of surface if and only if the block width or
2019 * height is changed. */
2020 if (tex_desc
->block
.width
!= templ_desc
->block
.width
||
2021 tex_desc
->block
.height
!= templ_desc
->block
.height
) {
2022 unsigned nblks_x
= util_format_get_nblocksx(tex
->format
, width
);
2023 unsigned nblks_y
= util_format_get_nblocksy(tex
->format
, height
);
2025 width
= nblks_x
* templ_desc
->block
.width
;
2026 height
= nblks_y
* templ_desc
->block
.height
;
2028 width0
= util_format_get_nblocksx(tex
->format
, width0
);
2029 height0
= util_format_get_nblocksy(tex
->format
, height0
);
2033 return si_create_surface_custom(pipe
, tex
, templ
,
2038 static void si_surface_destroy(struct pipe_context
*pipe
,
2039 struct pipe_surface
*surface
)
2041 pipe_resource_reference(&surface
->texture
, NULL
);
2045 unsigned si_translate_colorswap(enum pipe_format format
, bool do_endian_swap
)
2047 const struct util_format_description
*desc
= util_format_description(format
);
2049 #define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == PIPE_SWIZZLE_##swz)
2051 if (format
== PIPE_FORMAT_R11G11B10_FLOAT
) /* isn't plain */
2052 return V_028C70_SWAP_STD
;
2054 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
2057 switch (desc
->nr_channels
) {
2059 if (HAS_SWIZZLE(0,X
))
2060 return V_028C70_SWAP_STD
; /* X___ */
2061 else if (HAS_SWIZZLE(3,X
))
2062 return V_028C70_SWAP_ALT_REV
; /* ___X */
2065 if ((HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(1,Y
)) ||
2066 (HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(1,NONE
)) ||
2067 (HAS_SWIZZLE(0,NONE
) && HAS_SWIZZLE(1,Y
)))
2068 return V_028C70_SWAP_STD
; /* XY__ */
2069 else if ((HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(1,X
)) ||
2070 (HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(1,NONE
)) ||
2071 (HAS_SWIZZLE(0,NONE
) && HAS_SWIZZLE(1,X
)))
2073 return (do_endian_swap
? V_028C70_SWAP_STD
: V_028C70_SWAP_STD_REV
);
2074 else if (HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(3,Y
))
2075 return V_028C70_SWAP_ALT
; /* X__Y */
2076 else if (HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(3,X
))
2077 return V_028C70_SWAP_ALT_REV
; /* Y__X */
2080 if (HAS_SWIZZLE(0,X
))
2081 return (do_endian_swap
? V_028C70_SWAP_STD_REV
: V_028C70_SWAP_STD
);
2082 else if (HAS_SWIZZLE(0,Z
))
2083 return V_028C70_SWAP_STD_REV
; /* ZYX */
2086 /* check the middle channels, the 1st and 4th channel can be NONE */
2087 if (HAS_SWIZZLE(1,Y
) && HAS_SWIZZLE(2,Z
)) {
2088 return V_028C70_SWAP_STD
; /* XYZW */
2089 } else if (HAS_SWIZZLE(1,Z
) && HAS_SWIZZLE(2,Y
)) {
2090 return V_028C70_SWAP_STD_REV
; /* WZYX */
2091 } else if (HAS_SWIZZLE(1,Y
) && HAS_SWIZZLE(2,X
)) {
2092 return V_028C70_SWAP_ALT
; /* ZYXW */
2093 } else if (HAS_SWIZZLE(1,Z
) && HAS_SWIZZLE(2,W
)) {
2096 return V_028C70_SWAP_ALT_REV
;
2098 return (do_endian_swap
? V_028C70_SWAP_ALT
: V_028C70_SWAP_ALT_REV
);
2105 /* PIPELINE_STAT-BASED DCC ENABLEMENT FOR DISPLAYABLE SURFACES */
2107 static void vi_dcc_clean_up_context_slot(struct si_context
*sctx
,
2112 if (sctx
->dcc_stats
[slot
].query_active
)
2113 vi_separate_dcc_stop_query(sctx
,
2114 sctx
->dcc_stats
[slot
].tex
);
2116 for (i
= 0; i
< ARRAY_SIZE(sctx
->dcc_stats
[slot
].ps_stats
); i
++)
2117 if (sctx
->dcc_stats
[slot
].ps_stats
[i
]) {
2118 sctx
->b
.destroy_query(&sctx
->b
,
2119 sctx
->dcc_stats
[slot
].ps_stats
[i
]);
2120 sctx
->dcc_stats
[slot
].ps_stats
[i
] = NULL
;
2123 si_texture_reference(&sctx
->dcc_stats
[slot
].tex
, NULL
);
2127 * Return the per-context slot where DCC statistics queries for the texture live.
2129 static unsigned vi_get_context_dcc_stats_index(struct si_context
*sctx
,
2130 struct si_texture
*tex
)
2132 int i
, empty_slot
= -1;
2134 /* Remove zombie textures (textures kept alive by this array only). */
2135 for (i
= 0; i
< ARRAY_SIZE(sctx
->dcc_stats
); i
++)
2136 if (sctx
->dcc_stats
[i
].tex
&&
2137 sctx
->dcc_stats
[i
].tex
->buffer
.b
.b
.reference
.count
== 1)
2138 vi_dcc_clean_up_context_slot(sctx
, i
);
2140 /* Find the texture. */
2141 for (i
= 0; i
< ARRAY_SIZE(sctx
->dcc_stats
); i
++) {
2142 /* Return if found. */
2143 if (sctx
->dcc_stats
[i
].tex
== tex
) {
2144 sctx
->dcc_stats
[i
].last_use_timestamp
= os_time_get();
2148 /* Record the first seen empty slot. */
2149 if (empty_slot
== -1 && !sctx
->dcc_stats
[i
].tex
)
2153 /* Not found. Remove the oldest member to make space in the array. */
2154 if (empty_slot
== -1) {
2155 int oldest_slot
= 0;
2157 /* Find the oldest slot. */
2158 for (i
= 1; i
< ARRAY_SIZE(sctx
->dcc_stats
); i
++)
2159 if (sctx
->dcc_stats
[oldest_slot
].last_use_timestamp
>
2160 sctx
->dcc_stats
[i
].last_use_timestamp
)
2163 /* Clean up the oldest slot. */
2164 vi_dcc_clean_up_context_slot(sctx
, oldest_slot
);
2165 empty_slot
= oldest_slot
;
2168 /* Add the texture to the new slot. */
2169 si_texture_reference(&sctx
->dcc_stats
[empty_slot
].tex
, tex
);
2170 sctx
->dcc_stats
[empty_slot
].last_use_timestamp
= os_time_get();
2174 static struct pipe_query
*
2175 vi_create_resuming_pipestats_query(struct si_context
*sctx
)
2177 struct si_query_hw
*query
= (struct si_query_hw
*)
2178 sctx
->b
.create_query(&sctx
->b
, PIPE_QUERY_PIPELINE_STATISTICS
, 0);
2180 query
->flags
|= SI_QUERY_HW_FLAG_BEGIN_RESUMES
;
2181 return (struct pipe_query
*)query
;
2185 * Called when binding a color buffer.
2187 void vi_separate_dcc_start_query(struct si_context
*sctx
,
2188 struct si_texture
*tex
)
2190 unsigned i
= vi_get_context_dcc_stats_index(sctx
, tex
);
2192 assert(!sctx
->dcc_stats
[i
].query_active
);
2194 if (!sctx
->dcc_stats
[i
].ps_stats
[0])
2195 sctx
->dcc_stats
[i
].ps_stats
[0] = vi_create_resuming_pipestats_query(sctx
);
2197 /* begin or resume the query */
2198 sctx
->b
.begin_query(&sctx
->b
, sctx
->dcc_stats
[i
].ps_stats
[0]);
2199 sctx
->dcc_stats
[i
].query_active
= true;
2203 * Called when unbinding a color buffer.
2205 void vi_separate_dcc_stop_query(struct si_context
*sctx
,
2206 struct si_texture
*tex
)
2208 unsigned i
= vi_get_context_dcc_stats_index(sctx
, tex
);
2210 assert(sctx
->dcc_stats
[i
].query_active
);
2211 assert(sctx
->dcc_stats
[i
].ps_stats
[0]);
2213 /* pause or end the query */
2214 sctx
->b
.end_query(&sctx
->b
, sctx
->dcc_stats
[i
].ps_stats
[0]);
2215 sctx
->dcc_stats
[i
].query_active
= false;
2218 static bool vi_should_enable_separate_dcc(struct si_texture
*tex
)
2220 /* The minimum number of fullscreen draws per frame that is required
2222 return tex
->ps_draw_ratio
+ tex
->num_slow_clears
>= 5;
2225 /* Called by fast clear. */
2226 void vi_separate_dcc_try_enable(struct si_context
*sctx
,
2227 struct si_texture
*tex
)
2229 /* The intent is to use this with shared displayable back buffers,
2230 * but it's not strictly limited only to them.
2232 if (!tex
->buffer
.b
.is_shared
||
2233 !(tex
->buffer
.external_usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
) ||
2234 tex
->buffer
.b
.b
.target
!= PIPE_TEXTURE_2D
||
2235 tex
->buffer
.b
.b
.last_level
> 0 ||
2236 !tex
->surface
.dcc_size
||
2237 sctx
->screen
->debug_flags
& DBG(NO_DCC
) ||
2238 sctx
->screen
->debug_flags
& DBG(NO_DCC_FB
))
2241 assert(sctx
->chip_class
>= VI
);
2243 if (tex
->dcc_offset
)
2244 return; /* already enabled */
2246 /* Enable the DCC stat gathering. */
2247 if (!tex
->dcc_gather_statistics
) {
2248 tex
->dcc_gather_statistics
= true;
2249 vi_separate_dcc_start_query(sctx
, tex
);
2252 if (!vi_should_enable_separate_dcc(tex
))
2253 return; /* stats show that DCC decompression is too expensive */
2255 assert(tex
->surface
.num_dcc_levels
);
2256 assert(!tex
->dcc_separate_buffer
);
2258 si_texture_discard_cmask(sctx
->screen
, tex
);
2260 /* Get a DCC buffer. */
2261 if (tex
->last_dcc_separate_buffer
) {
2262 assert(tex
->dcc_gather_statistics
);
2263 assert(!tex
->dcc_separate_buffer
);
2264 tex
->dcc_separate_buffer
= tex
->last_dcc_separate_buffer
;
2265 tex
->last_dcc_separate_buffer
= NULL
;
2267 tex
->dcc_separate_buffer
=
2268 si_aligned_buffer_create(sctx
->b
.screen
,
2269 SI_RESOURCE_FLAG_UNMAPPABLE
,
2271 tex
->surface
.dcc_size
,
2272 tex
->surface
.dcc_alignment
);
2273 if (!tex
->dcc_separate_buffer
)
2277 /* dcc_offset is the absolute GPUVM address. */
2278 tex
->dcc_offset
= tex
->dcc_separate_buffer
->gpu_address
;
2280 /* no need to flag anything since this is called by fast clear that
2281 * flags framebuffer state
2286 * Called by pipe_context::flush_resource, the place where DCC decompression
2289 void vi_separate_dcc_process_and_reset_stats(struct pipe_context
*ctx
,
2290 struct si_texture
*tex
)
2292 struct si_context
*sctx
= (struct si_context
*)ctx
;
2293 struct pipe_query
*tmp
;
2294 unsigned i
= vi_get_context_dcc_stats_index(sctx
, tex
);
2295 bool query_active
= sctx
->dcc_stats
[i
].query_active
;
2296 bool disable
= false;
2298 if (sctx
->dcc_stats
[i
].ps_stats
[2]) {
2299 union pipe_query_result result
;
2301 /* Read the results. */
2302 struct pipe_query
*query
= sctx
->dcc_stats
[i
].ps_stats
[2];
2303 ctx
->get_query_result(ctx
, query
,
2305 si_query_buffer_reset(sctx
, &((struct si_query_hw
*)query
)->buffer
);
2307 /* Compute the approximate number of fullscreen draws. */
2308 tex
->ps_draw_ratio
=
2309 result
.pipeline_statistics
.ps_invocations
/
2310 (tex
->buffer
.b
.b
.width0
* tex
->buffer
.b
.b
.height0
);
2311 sctx
->last_tex_ps_draw_ratio
= tex
->ps_draw_ratio
;
2313 disable
= tex
->dcc_separate_buffer
&&
2314 !vi_should_enable_separate_dcc(tex
);
2317 tex
->num_slow_clears
= 0;
2319 /* stop the statistics query for ps_stats[0] */
2321 vi_separate_dcc_stop_query(sctx
, tex
);
2323 /* Move the queries in the queue by one. */
2324 tmp
= sctx
->dcc_stats
[i
].ps_stats
[2];
2325 sctx
->dcc_stats
[i
].ps_stats
[2] = sctx
->dcc_stats
[i
].ps_stats
[1];
2326 sctx
->dcc_stats
[i
].ps_stats
[1] = sctx
->dcc_stats
[i
].ps_stats
[0];
2327 sctx
->dcc_stats
[i
].ps_stats
[0] = tmp
;
2329 /* create and start a new query as ps_stats[0] */
2331 vi_separate_dcc_start_query(sctx
, tex
);
2334 assert(!tex
->last_dcc_separate_buffer
);
2335 tex
->last_dcc_separate_buffer
= tex
->dcc_separate_buffer
;
2336 tex
->dcc_separate_buffer
= NULL
;
2337 tex
->dcc_offset
= 0;
2338 /* no need to flag anything since this is called after
2339 * decompression that re-sets framebuffer state
2344 static struct pipe_memory_object
*
2345 si_memobj_from_handle(struct pipe_screen
*screen
,
2346 struct winsys_handle
*whandle
,
2349 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
2350 struct si_memory_object
*memobj
= CALLOC_STRUCT(si_memory_object
);
2351 struct pb_buffer
*buf
= NULL
;
2352 uint32_t stride
, offset
;
2357 buf
= sscreen
->ws
->buffer_from_handle(sscreen
->ws
, whandle
,
2358 sscreen
->info
.max_alignment
,
2365 memobj
->b
.dedicated
= dedicated
;
2367 memobj
->stride
= stride
;
2369 return (struct pipe_memory_object
*)memobj
;
2374 si_memobj_destroy(struct pipe_screen
*screen
,
2375 struct pipe_memory_object
*_memobj
)
2377 struct si_memory_object
*memobj
= (struct si_memory_object
*)_memobj
;
2379 pb_reference(&memobj
->buf
, NULL
);
2383 static struct pipe_resource
*
2384 si_texture_from_memobj(struct pipe_screen
*screen
,
2385 const struct pipe_resource
*templ
,
2386 struct pipe_memory_object
*_memobj
,
2389 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
2390 struct si_memory_object
*memobj
= (struct si_memory_object
*)_memobj
;
2391 struct pipe_resource
*tex
=
2392 si_texture_from_winsys_buffer(sscreen
, templ
, memobj
->buf
,
2393 memobj
->stride
, offset
,
2394 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE
|
2395 PIPE_HANDLE_USAGE_SHADER_WRITE
,
2396 memobj
->b
.dedicated
);
2400 /* si_texture_from_winsys_buffer doesn't increment refcount of
2401 * memobj->buf, so increment it here.
2403 struct pb_buffer
*buf
= NULL
;
2404 pb_reference(&buf
, memobj
->buf
);
2408 static bool si_check_resource_capability(struct pipe_screen
*screen
,
2409 struct pipe_resource
*resource
,
2412 struct si_texture
*tex
= (struct si_texture
*)resource
;
2414 /* Buffers only support the linear flag. */
2415 if (resource
->target
== PIPE_BUFFER
)
2416 return (bind
& ~PIPE_BIND_LINEAR
) == 0;
2418 if (bind
& PIPE_BIND_LINEAR
&& !tex
->surface
.is_linear
)
2421 if (bind
& PIPE_BIND_SCANOUT
&& !tex
->surface
.is_displayable
)
2424 /* TODO: PIPE_BIND_CURSOR - do we care? */
2428 void si_init_screen_texture_functions(struct si_screen
*sscreen
)
2430 sscreen
->b
.resource_from_handle
= si_texture_from_handle
;
2431 sscreen
->b
.resource_get_handle
= si_texture_get_handle
;
2432 sscreen
->b
.resource_from_memobj
= si_texture_from_memobj
;
2433 sscreen
->b
.memobj_create_from_handle
= si_memobj_from_handle
;
2434 sscreen
->b
.memobj_destroy
= si_memobj_destroy
;
2435 sscreen
->b
.check_resource_capability
= si_check_resource_capability
;
2438 void si_init_context_texture_functions(struct si_context
*sctx
)
2440 sctx
->b
.create_surface
= si_create_surface
;
2441 sctx
->b
.surface_destroy
= si_surface_destroy
;