2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #include "radeonsi/si_pipe.h"
26 #include "r600_query.h"
27 #include "util/u_format.h"
28 #include "util/u_log.h"
29 #include "util/u_memory.h"
30 #include "util/u_pack_color.h"
31 #include "util/u_resource.h"
32 #include "util/u_surface.h"
33 #include "util/os_time.h"
36 #include "state_tracker/drm_driver.h"
37 #include "amd/common/sid.h"
39 static void r600_texture_discard_cmask(struct r600_common_screen
*rscreen
,
40 struct r600_texture
*rtex
);
41 static enum radeon_surf_mode
42 r600_choose_tiling(struct r600_common_screen
*rscreen
,
43 const struct pipe_resource
*templ
);
46 bool si_prepare_for_dma_blit(struct r600_common_context
*rctx
,
47 struct r600_texture
*rdst
,
48 unsigned dst_level
, unsigned dstx
,
49 unsigned dsty
, unsigned dstz
,
50 struct r600_texture
*rsrc
,
52 const struct pipe_box
*src_box
)
57 if (rdst
->surface
.bpe
!= rsrc
->surface
.bpe
)
60 /* MSAA: Blits don't exist in the real world. */
61 if (rsrc
->resource
.b
.b
.nr_samples
> 1 ||
62 rdst
->resource
.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 (rsrc
->is_depth
|| rdst
->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(rsrc
, src_level
) ||
77 vi_dcc_enabled(rdst
, 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 (rdst
->cmask
.size
&& rdst
->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(&rdst
->resource
.b
.b
, dst_level
,
89 dstx
, dsty
, dstz
, src_box
->width
,
90 src_box
->height
, src_box
->depth
))
93 r600_texture_discard_cmask(rctx
->screen
, rdst
);
96 /* All requirements are met. Prepare textures for SDMA. */
97 if (rsrc
->cmask
.size
&& rsrc
->dirty_level_mask
& (1 << src_level
))
98 rctx
->b
.flush_resource(&rctx
->b
, &rsrc
->resource
.b
.b
);
100 assert(!(rsrc
->dirty_level_mask
& (1 << src_level
)));
101 assert(!(rdst
->dirty_level_mask
& (1 << dst_level
)));
106 /* Same as resource_copy_region, except that both upsampling and downsampling are allowed. */
107 static void r600_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 r600_copy_to_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
143 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
144 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
145 struct pipe_resource
*dst
= &rtransfer
->staging
->b
.b
;
146 struct pipe_resource
*src
= transfer
->resource
;
148 if (src
->nr_samples
> 1) {
149 r600_copy_region_with_blit(ctx
, dst
, 0, 0, 0, 0,
150 src
, transfer
->level
, &transfer
->box
);
154 rctx
->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 r600_copy_from_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
161 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
162 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
163 struct pipe_resource
*dst
= transfer
->resource
;
164 struct pipe_resource
*src
= &rtransfer
->staging
->b
.b
;
165 struct pipe_box sbox
;
167 u_box_3d(0, 0, 0, transfer
->box
.width
, transfer
->box
.height
, transfer
->box
.depth
, &sbox
);
169 if (dst
->nr_samples
> 1) {
170 r600_copy_region_with_blit(ctx
, dst
, transfer
->level
,
171 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
176 rctx
->dma_copy(ctx
, dst
, transfer
->level
,
177 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
181 static unsigned r600_texture_get_offset(struct r600_common_screen
*rscreen
,
182 struct r600_texture
*rtex
, unsigned level
,
183 const struct pipe_box
*box
,
185 unsigned *layer_stride
)
187 if (rscreen
->chip_class
>= GFX9
) {
188 *stride
= rtex
->surface
.u
.gfx9
.surf_pitch
* rtex
->surface
.bpe
;
189 *layer_stride
= rtex
->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
* rtex
->surface
.u
.gfx9
.surf_slice_size
+
197 rtex
->surface
.u
.gfx9
.offset
[level
] +
198 (box
->y
/ rtex
->surface
.blk_h
*
199 rtex
->surface
.u
.gfx9
.surf_pitch
+
200 box
->x
/ rtex
->surface
.blk_w
) * rtex
->surface
.bpe
;
202 *stride
= rtex
->surface
.u
.legacy
.level
[level
].nblk_x
*
204 assert((uint64_t)rtex
->surface
.u
.legacy
.level
[level
].slice_size_dw
* 4 <= UINT_MAX
);
205 *layer_stride
= (uint64_t)rtex
->surface
.u
.legacy
.level
[level
].slice_size_dw
* 4;
208 return rtex
->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 rtex
->surface
.u
.legacy
.level
[level
].offset
+
213 box
->z
* (uint64_t)rtex
->surface
.u
.legacy
.level
[level
].slice_size_dw
* 4 +
214 (box
->y
/ rtex
->surface
.blk_h
*
215 rtex
->surface
.u
.legacy
.level
[level
].nblk_x
+
216 box
->x
/ rtex
->surface
.blk_w
) * rtex
->surface
.bpe
;
220 static int r600_init_surface(struct r600_common_screen
*rscreen
,
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 on evergreen */
244 bpe
= util_format_get_blocksize(ptex
->format
);
245 assert(util_is_power_of_two(bpe
));
248 if (!is_flushed_depth
&& is_depth
) {
249 flags
|= RADEON_SURF_ZBUFFER
;
251 if (tc_compatible_htile
&&
252 (rscreen
->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 (rscreen
->chip_class
== VI
)
262 flags
|= RADEON_SURF_TC_COMPATIBLE_HTILE
;
266 flags
|= RADEON_SURF_SBUFFER
;
269 if (rscreen
->chip_class
>= VI
&&
270 (ptex
->flags
& R600_RESOURCE_FLAG_DISABLE_DCC
||
271 ptex
->format
== PIPE_FORMAT_R9G9B9E5_FLOAT
||
272 /* DCC MSAA array textures are disallowed due to incomplete clear impl. */
273 (ptex
->nr_samples
>= 2 &&
274 (!rscreen
->dcc_msaa_allowed
|| ptex
->array_size
> 1))))
275 flags
|= RADEON_SURF_DISABLE_DCC
;
277 if (ptex
->bind
& PIPE_BIND_SCANOUT
|| is_scanout
) {
278 /* This should catch bugs in gallium users setting incorrect flags. */
279 assert(ptex
->nr_samples
<= 1 &&
280 ptex
->array_size
== 1 &&
282 ptex
->last_level
== 0 &&
283 !(flags
& RADEON_SURF_Z_OR_SBUFFER
));
285 flags
|= RADEON_SURF_SCANOUT
;
288 if (ptex
->bind
& PIPE_BIND_SHARED
)
289 flags
|= RADEON_SURF_SHAREABLE
;
291 flags
|= RADEON_SURF_IMPORTED
| RADEON_SURF_SHAREABLE
;
292 if (!(ptex
->flags
& R600_RESOURCE_FLAG_FORCE_TILING
))
293 flags
|= RADEON_SURF_OPTIMIZE_FOR_SPACE
;
295 r
= rscreen
->ws
->surface_init(rscreen
->ws
, ptex
, flags
, bpe
,
296 array_mode
, surface
);
301 if (rscreen
->chip_class
>= GFX9
) {
302 assert(!pitch_in_bytes_override
||
303 pitch_in_bytes_override
== surface
->u
.gfx9
.surf_pitch
* bpe
);
304 surface
->u
.gfx9
.surf_offset
= offset
;
307 for (i
= 0; i
< ARRAY_SIZE(surface
->u
.legacy
.level
); ++i
)
308 surface
->u
.legacy
.level
[i
].offset
+= offset
;
314 static void r600_texture_init_metadata(struct r600_common_screen
*rscreen
,
315 struct r600_texture
*rtex
,
316 struct radeon_bo_metadata
*metadata
)
318 struct radeon_surf
*surface
= &rtex
->surface
;
320 memset(metadata
, 0, sizeof(*metadata
));
322 if (rscreen
->chip_class
>= GFX9
) {
323 metadata
->u
.gfx9
.swizzle_mode
= surface
->u
.gfx9
.surf
.swizzle_mode
;
325 metadata
->u
.legacy
.microtile
= surface
->u
.legacy
.level
[0].mode
>= RADEON_SURF_MODE_1D
?
326 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
;
327 metadata
->u
.legacy
.macrotile
= surface
->u
.legacy
.level
[0].mode
>= RADEON_SURF_MODE_2D
?
328 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
;
329 metadata
->u
.legacy
.pipe_config
= surface
->u
.legacy
.pipe_config
;
330 metadata
->u
.legacy
.bankw
= surface
->u
.legacy
.bankw
;
331 metadata
->u
.legacy
.bankh
= surface
->u
.legacy
.bankh
;
332 metadata
->u
.legacy
.tile_split
= surface
->u
.legacy
.tile_split
;
333 metadata
->u
.legacy
.mtilea
= surface
->u
.legacy
.mtilea
;
334 metadata
->u
.legacy
.num_banks
= surface
->u
.legacy
.num_banks
;
335 metadata
->u
.legacy
.stride
= surface
->u
.legacy
.level
[0].nblk_x
* surface
->bpe
;
336 metadata
->u
.legacy
.scanout
= (surface
->flags
& RADEON_SURF_SCANOUT
) != 0;
340 static void r600_surface_import_metadata(struct r600_common_screen
*rscreen
,
341 struct radeon_surf
*surf
,
342 struct radeon_bo_metadata
*metadata
,
343 enum radeon_surf_mode
*array_mode
,
346 if (rscreen
->chip_class
>= GFX9
) {
347 if (metadata
->u
.gfx9
.swizzle_mode
> 0)
348 *array_mode
= RADEON_SURF_MODE_2D
;
350 *array_mode
= RADEON_SURF_MODE_LINEAR_ALIGNED
;
352 *is_scanout
= metadata
->u
.gfx9
.swizzle_mode
== 0 ||
353 metadata
->u
.gfx9
.swizzle_mode
% 4 == 2;
355 surf
->u
.gfx9
.surf
.swizzle_mode
= metadata
->u
.gfx9
.swizzle_mode
;
357 surf
->u
.legacy
.pipe_config
= metadata
->u
.legacy
.pipe_config
;
358 surf
->u
.legacy
.bankw
= metadata
->u
.legacy
.bankw
;
359 surf
->u
.legacy
.bankh
= metadata
->u
.legacy
.bankh
;
360 surf
->u
.legacy
.tile_split
= metadata
->u
.legacy
.tile_split
;
361 surf
->u
.legacy
.mtilea
= metadata
->u
.legacy
.mtilea
;
362 surf
->u
.legacy
.num_banks
= metadata
->u
.legacy
.num_banks
;
364 if (metadata
->u
.legacy
.macrotile
== RADEON_LAYOUT_TILED
)
365 *array_mode
= RADEON_SURF_MODE_2D
;
366 else if (metadata
->u
.legacy
.microtile
== RADEON_LAYOUT_TILED
)
367 *array_mode
= RADEON_SURF_MODE_1D
;
369 *array_mode
= RADEON_SURF_MODE_LINEAR_ALIGNED
;
371 *is_scanout
= metadata
->u
.legacy
.scanout
;
375 static void r600_eliminate_fast_color_clear(struct r600_common_context
*rctx
,
376 struct r600_texture
*rtex
)
378 struct r600_common_screen
*rscreen
= rctx
->screen
;
379 struct pipe_context
*ctx
= &rctx
->b
;
381 if (ctx
== rscreen
->aux_context
)
382 mtx_lock(&rscreen
->aux_context_lock
);
384 ctx
->flush_resource(ctx
, &rtex
->resource
.b
.b
);
385 ctx
->flush(ctx
, NULL
, 0);
387 if (ctx
== rscreen
->aux_context
)
388 mtx_unlock(&rscreen
->aux_context_lock
);
391 static void r600_texture_discard_cmask(struct r600_common_screen
*rscreen
,
392 struct r600_texture
*rtex
)
394 if (!rtex
->cmask
.size
)
397 assert(rtex
->resource
.b
.b
.nr_samples
<= 1);
400 memset(&rtex
->cmask
, 0, sizeof(rtex
->cmask
));
401 rtex
->cmask
.base_address_reg
= rtex
->resource
.gpu_address
>> 8;
402 rtex
->dirty_level_mask
= 0;
404 rtex
->cb_color_info
&= ~S_028C70_FAST_CLEAR(1);
406 if (rtex
->cmask_buffer
!= &rtex
->resource
)
407 r600_resource_reference(&rtex
->cmask_buffer
, NULL
);
409 /* Notify all contexts about the change. */
410 p_atomic_inc(&rscreen
->dirty_tex_counter
);
411 p_atomic_inc(&rscreen
->compressed_colortex_counter
);
414 static bool r600_can_disable_dcc(struct r600_texture
*rtex
)
416 /* We can't disable DCC if it can be written by another process. */
417 return rtex
->dcc_offset
&&
418 (!rtex
->resource
.b
.is_shared
||
419 !(rtex
->resource
.external_usage
& PIPE_HANDLE_USAGE_WRITE
));
422 static bool r600_texture_discard_dcc(struct r600_common_screen
*rscreen
,
423 struct r600_texture
*rtex
)
425 if (!r600_can_disable_dcc(rtex
))
428 assert(rtex
->dcc_separate_buffer
== NULL
);
431 rtex
->dcc_offset
= 0;
433 /* Notify all contexts about the change. */
434 p_atomic_inc(&rscreen
->dirty_tex_counter
);
439 * Disable DCC for the texture. (first decompress, then discard metadata).
441 * There is unresolved multi-context synchronization issue between
442 * screen::aux_context and the current context. If applications do this with
443 * multiple contexts, it's already undefined behavior for them and we don't
444 * have to worry about that. The scenario is:
446 * If context 1 disables DCC and context 2 has queued commands that write
447 * to the texture via CB with DCC enabled, and the order of operations is
449 * context 2 queues draw calls rendering to the texture, but doesn't flush
450 * context 1 disables DCC and flushes
451 * context 1 & 2 reset descriptors and FB state
452 * context 2 flushes (new compressed tiles written by the draw calls)
453 * context 1 & 2 read garbage, because DCC is disabled, yet there are
456 * \param rctx the current context if you have one, or rscreen->aux_context
459 bool si_texture_disable_dcc(struct r600_common_context
*rctx
,
460 struct r600_texture
*rtex
)
462 struct r600_common_screen
*rscreen
= rctx
->screen
;
464 if (!r600_can_disable_dcc(rtex
))
467 if (&rctx
->b
== rscreen
->aux_context
)
468 mtx_lock(&rscreen
->aux_context_lock
);
470 /* Decompress DCC. */
471 rctx
->decompress_dcc(&rctx
->b
, rtex
);
472 rctx
->b
.flush(&rctx
->b
, NULL
, 0);
474 if (&rctx
->b
== rscreen
->aux_context
)
475 mtx_unlock(&rscreen
->aux_context_lock
);
477 return r600_texture_discard_dcc(rscreen
, rtex
);
480 static void r600_reallocate_texture_inplace(struct r600_common_context
*rctx
,
481 struct r600_texture
*rtex
,
482 unsigned new_bind_flag
,
483 bool invalidate_storage
)
485 struct pipe_screen
*screen
= rctx
->b
.screen
;
486 struct r600_texture
*new_tex
;
487 struct pipe_resource templ
= rtex
->resource
.b
.b
;
490 templ
.bind
|= new_bind_flag
;
492 if (rtex
->resource
.b
.is_shared
)
495 if (new_bind_flag
== PIPE_BIND_LINEAR
) {
496 if (rtex
->surface
.is_linear
)
499 /* This fails with MSAA, depth, and compressed textures. */
500 if (r600_choose_tiling(rctx
->screen
, &templ
) !=
501 RADEON_SURF_MODE_LINEAR_ALIGNED
)
505 new_tex
= (struct r600_texture
*)screen
->resource_create(screen
, &templ
);
509 /* Copy the pixels to the new texture. */
510 if (!invalidate_storage
) {
511 for (i
= 0; i
<= templ
.last_level
; i
++) {
515 u_minify(templ
.width0
, i
), u_minify(templ
.height0
, i
),
516 util_max_layer(&templ
, i
) + 1, &box
);
518 rctx
->dma_copy(&rctx
->b
, &new_tex
->resource
.b
.b
, i
, 0, 0, 0,
519 &rtex
->resource
.b
.b
, i
, &box
);
523 if (new_bind_flag
== PIPE_BIND_LINEAR
) {
524 r600_texture_discard_cmask(rctx
->screen
, rtex
);
525 r600_texture_discard_dcc(rctx
->screen
, rtex
);
528 /* Replace the structure fields of rtex. */
529 rtex
->resource
.b
.b
.bind
= templ
.bind
;
530 pb_reference(&rtex
->resource
.buf
, new_tex
->resource
.buf
);
531 rtex
->resource
.gpu_address
= new_tex
->resource
.gpu_address
;
532 rtex
->resource
.vram_usage
= new_tex
->resource
.vram_usage
;
533 rtex
->resource
.gart_usage
= new_tex
->resource
.gart_usage
;
534 rtex
->resource
.bo_size
= new_tex
->resource
.bo_size
;
535 rtex
->resource
.bo_alignment
= new_tex
->resource
.bo_alignment
;
536 rtex
->resource
.domains
= new_tex
->resource
.domains
;
537 rtex
->resource
.flags
= new_tex
->resource
.flags
;
538 rtex
->size
= new_tex
->size
;
539 rtex
->db_render_format
= new_tex
->db_render_format
;
540 rtex
->db_compatible
= new_tex
->db_compatible
;
541 rtex
->can_sample_z
= new_tex
->can_sample_z
;
542 rtex
->can_sample_s
= new_tex
->can_sample_s
;
543 rtex
->surface
= new_tex
->surface
;
544 rtex
->fmask
= new_tex
->fmask
;
545 rtex
->cmask
= new_tex
->cmask
;
546 rtex
->cb_color_info
= new_tex
->cb_color_info
;
547 rtex
->last_msaa_resolve_target_micro_mode
= new_tex
->last_msaa_resolve_target_micro_mode
;
548 rtex
->htile_offset
= new_tex
->htile_offset
;
549 rtex
->tc_compatible_htile
= new_tex
->tc_compatible_htile
;
550 rtex
->depth_cleared
= new_tex
->depth_cleared
;
551 rtex
->stencil_cleared
= new_tex
->stencil_cleared
;
552 rtex
->dcc_gather_statistics
= new_tex
->dcc_gather_statistics
;
553 rtex
->framebuffers_bound
= new_tex
->framebuffers_bound
;
555 if (new_bind_flag
== PIPE_BIND_LINEAR
) {
556 assert(!rtex
->htile_offset
);
557 assert(!rtex
->cmask
.size
);
558 assert(!rtex
->fmask
.size
);
559 assert(!rtex
->dcc_offset
);
560 assert(!rtex
->is_depth
);
563 r600_texture_reference(&new_tex
, NULL
);
565 p_atomic_inc(&rctx
->screen
->dirty_tex_counter
);
568 static uint32_t si_get_bo_metadata_word1(struct r600_common_screen
*rscreen
)
570 return (ATI_VENDOR_ID
<< 16) | rscreen
->info
.pci_id
;
573 static void si_query_opaque_metadata(struct r600_common_screen
*rscreen
,
574 struct r600_texture
*rtex
,
575 struct radeon_bo_metadata
*md
)
577 struct si_screen
*sscreen
= (struct si_screen
*)rscreen
;
578 struct pipe_resource
*res
= &rtex
->resource
.b
.b
;
579 static const unsigned char swizzle
[] = {
586 bool is_array
= util_resource_is_array_texture(res
);
588 /* DRM 2.x.x doesn't support this. */
589 if (rscreen
->info
.drm_major
!= 3)
592 assert(rtex
->dcc_separate_buffer
== NULL
);
593 assert(rtex
->fmask
.size
== 0);
595 /* Metadata image format format version 1:
596 * [0] = 1 (metadata format identifier)
597 * [1] = (VENDOR_ID << 16) | PCI_ID
598 * [2:9] = image descriptor for the whole resource
599 * [2] is always 0, because the base address is cleared
600 * [9] is the DCC offset bits [39:8] from the beginning of
602 * [10:10+LAST_LEVEL] = mipmap level offset bits [39:8] for each level
605 md
->metadata
[0] = 1; /* metadata image format version 1 */
607 /* TILE_MODE_INDEX is ambiguous without a PCI ID. */
608 md
->metadata
[1] = si_get_bo_metadata_word1(rscreen
);
610 si_make_texture_descriptor(sscreen
, rtex
, true,
611 res
->target
, res
->format
,
612 swizzle
, 0, res
->last_level
, 0,
613 is_array
? res
->array_size
- 1 : 0,
614 res
->width0
, res
->height0
, res
->depth0
,
617 si_set_mutable_tex_desc_fields(sscreen
, rtex
, &rtex
->surface
.u
.legacy
.level
[0],
618 0, 0, rtex
->surface
.blk_w
, false, desc
);
620 /* Clear the base address and set the relative DCC offset. */
622 desc
[1] &= C_008F14_BASE_ADDRESS_HI
;
623 desc
[7] = rtex
->dcc_offset
>> 8;
625 /* Dwords [2:9] contain the image descriptor. */
626 memcpy(&md
->metadata
[2], desc
, sizeof(desc
));
627 md
->size_metadata
= 10 * 4;
629 /* Dwords [10:..] contain the mipmap level offsets. */
630 if (rscreen
->chip_class
<= VI
) {
631 for (i
= 0; i
<= res
->last_level
; i
++)
632 md
->metadata
[10+i
] = rtex
->surface
.u
.legacy
.level
[i
].offset
>> 8;
634 md
->size_metadata
+= (1 + res
->last_level
) * 4;
638 static void si_apply_opaque_metadata(struct r600_common_screen
*rscreen
,
639 struct r600_texture
*rtex
,
640 struct radeon_bo_metadata
*md
)
642 uint32_t *desc
= &md
->metadata
[2];
644 if (rscreen
->chip_class
< VI
)
647 /* Return if DCC is enabled. The texture should be set up with it
650 if (md
->size_metadata
>= 11 * 4 &&
651 md
->metadata
[0] != 0 &&
652 md
->metadata
[1] == si_get_bo_metadata_word1(rscreen
) &&
653 G_008F28_COMPRESSION_EN(desc
[6])) {
654 rtex
->dcc_offset
= (uint64_t)desc
[7] << 8;
658 /* Disable DCC. These are always set by texture_from_handle and must
661 rtex
->dcc_offset
= 0;
664 static boolean
r600_texture_get_handle(struct pipe_screen
* screen
,
665 struct pipe_context
*ctx
,
666 struct pipe_resource
*resource
,
667 struct winsys_handle
*whandle
,
670 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)screen
;
671 struct r600_common_context
*rctx
;
672 struct r600_resource
*res
= (struct r600_resource
*)resource
;
673 struct r600_texture
*rtex
= (struct r600_texture
*)resource
;
674 struct radeon_bo_metadata metadata
;
675 bool update_metadata
= false;
676 unsigned stride
, offset
, slice_size
;
678 ctx
= threaded_context_unwrap_sync(ctx
);
679 rctx
= (struct r600_common_context
*)(ctx
? ctx
: rscreen
->aux_context
);
681 if (resource
->target
!= PIPE_BUFFER
) {
682 /* This is not supported now, but it might be required for OpenCL
683 * interop in the future.
685 if (resource
->nr_samples
> 1 || rtex
->is_depth
)
688 /* Move a suballocated texture into a non-suballocated allocation. */
689 if (rscreen
->ws
->buffer_is_suballocated(res
->buf
) ||
690 rtex
->surface
.tile_swizzle
||
691 (rtex
->resource
.flags
& RADEON_FLAG_NO_INTERPROCESS_SHARING
&&
692 whandle
->type
!= DRM_API_HANDLE_TYPE_KMS
)) {
693 assert(!res
->b
.is_shared
);
694 r600_reallocate_texture_inplace(rctx
, rtex
,
695 PIPE_BIND_SHARED
, false);
696 rctx
->b
.flush(&rctx
->b
, NULL
, 0);
697 assert(res
->b
.b
.bind
& PIPE_BIND_SHARED
);
698 assert(res
->flags
& RADEON_FLAG_NO_SUBALLOC
);
699 assert(!(res
->flags
& RADEON_FLAG_NO_INTERPROCESS_SHARING
));
700 assert(rtex
->surface
.tile_swizzle
== 0);
703 /* Since shader image stores don't support DCC on VI,
704 * disable it for external clients that want write
707 if (usage
& PIPE_HANDLE_USAGE_WRITE
&& rtex
->dcc_offset
) {
708 if (si_texture_disable_dcc(rctx
, rtex
))
709 update_metadata
= true;
712 if (!(usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
) &&
713 (rtex
->cmask
.size
|| rtex
->dcc_offset
)) {
714 /* Eliminate fast clear (both CMASK and DCC) */
715 r600_eliminate_fast_color_clear(rctx
, rtex
);
717 /* Disable CMASK if flush_resource isn't going
720 if (rtex
->cmask
.size
)
721 r600_texture_discard_cmask(rscreen
, rtex
);
725 if (!res
->b
.is_shared
|| update_metadata
) {
726 r600_texture_init_metadata(rscreen
, rtex
, &metadata
);
727 si_query_opaque_metadata(rscreen
, rtex
, &metadata
);
729 rscreen
->ws
->buffer_set_metadata(res
->buf
, &metadata
);
732 if (rscreen
->chip_class
>= GFX9
) {
733 offset
= rtex
->surface
.u
.gfx9
.surf_offset
;
734 stride
= rtex
->surface
.u
.gfx9
.surf_pitch
*
736 slice_size
= rtex
->surface
.u
.gfx9
.surf_slice_size
;
738 offset
= rtex
->surface
.u
.legacy
.level
[0].offset
;
739 stride
= rtex
->surface
.u
.legacy
.level
[0].nblk_x
*
741 slice_size
= (uint64_t)rtex
->surface
.u
.legacy
.level
[0].slice_size_dw
* 4;
744 /* Move a suballocated buffer into a non-suballocated allocation. */
745 if (rscreen
->ws
->buffer_is_suballocated(res
->buf
)) {
746 assert(!res
->b
.is_shared
);
748 /* Allocate a new buffer with PIPE_BIND_SHARED. */
749 struct pipe_resource templ
= res
->b
.b
;
750 templ
.bind
|= PIPE_BIND_SHARED
;
752 struct pipe_resource
*newb
=
753 screen
->resource_create(screen
, &templ
);
757 /* Copy the old buffer contents to the new one. */
759 u_box_1d(0, newb
->width0
, &box
);
760 rctx
->b
.resource_copy_region(&rctx
->b
, newb
, 0, 0, 0, 0,
762 /* Move the new buffer storage to the old pipe_resource. */
763 si_replace_buffer_storage(&rctx
->b
, &res
->b
.b
, newb
);
764 pipe_resource_reference(&newb
, NULL
);
766 assert(res
->b
.b
.bind
& PIPE_BIND_SHARED
);
767 assert(res
->flags
& RADEON_FLAG_NO_SUBALLOC
);
776 if (res
->b
.is_shared
) {
777 /* USAGE_EXPLICIT_FLUSH must be cleared if at least one user
780 res
->external_usage
|= usage
& ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
;
781 if (!(usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
))
782 res
->external_usage
&= ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
;
784 res
->b
.is_shared
= true;
785 res
->external_usage
= usage
;
788 return rscreen
->ws
->buffer_get_handle(res
->buf
, stride
, offset
,
789 slice_size
, whandle
);
792 static void r600_texture_destroy(struct pipe_screen
*screen
,
793 struct pipe_resource
*ptex
)
795 struct r600_texture
*rtex
= (struct r600_texture
*)ptex
;
796 struct r600_resource
*resource
= &rtex
->resource
;
798 r600_texture_reference(&rtex
->flushed_depth_texture
, NULL
);
800 if (rtex
->cmask_buffer
!= &rtex
->resource
) {
801 r600_resource_reference(&rtex
->cmask_buffer
, NULL
);
803 pb_reference(&resource
->buf
, NULL
);
804 r600_resource_reference(&rtex
->dcc_separate_buffer
, NULL
);
805 r600_resource_reference(&rtex
->last_dcc_separate_buffer
, NULL
);
809 static const struct u_resource_vtbl r600_texture_vtbl
;
811 /* The number of samples can be specified independently of the texture. */
812 void si_texture_get_fmask_info(struct r600_common_screen
*rscreen
,
813 struct r600_texture
*rtex
,
815 struct r600_fmask_info
*out
)
817 /* FMASK is allocated like an ordinary texture. */
818 struct pipe_resource templ
= rtex
->resource
.b
.b
;
819 struct radeon_surf fmask
= {};
822 memset(out
, 0, sizeof(*out
));
824 if (rscreen
->chip_class
>= GFX9
) {
825 out
->alignment
= rtex
->surface
.u
.gfx9
.fmask_alignment
;
826 out
->size
= rtex
->surface
.u
.gfx9
.fmask_size
;
830 templ
.nr_samples
= 1;
831 flags
= rtex
->surface
.flags
| RADEON_SURF_FMASK
;
833 switch (nr_samples
) {
842 R600_ERR("Invalid sample count for FMASK allocation.\n");
846 if (rscreen
->ws
->surface_init(rscreen
->ws
, &templ
, flags
, bpe
,
847 RADEON_SURF_MODE_2D
, &fmask
)) {
848 R600_ERR("Got error in surface_init while allocating FMASK.\n");
852 assert(fmask
.u
.legacy
.level
[0].mode
== RADEON_SURF_MODE_2D
);
854 out
->slice_tile_max
= (fmask
.u
.legacy
.level
[0].nblk_x
* fmask
.u
.legacy
.level
[0].nblk_y
) / 64;
855 if (out
->slice_tile_max
)
856 out
->slice_tile_max
-= 1;
858 out
->tile_mode_index
= fmask
.u
.legacy
.tiling_index
[0];
859 out
->pitch_in_pixels
= fmask
.u
.legacy
.level
[0].nblk_x
;
860 out
->bank_height
= fmask
.u
.legacy
.bankh
;
861 out
->tile_swizzle
= fmask
.tile_swizzle
;
862 out
->alignment
= MAX2(256, fmask
.surf_alignment
);
863 out
->size
= fmask
.surf_size
;
866 static void r600_texture_allocate_fmask(struct r600_common_screen
*rscreen
,
867 struct r600_texture
*rtex
)
869 si_texture_get_fmask_info(rscreen
, rtex
,
870 rtex
->resource
.b
.b
.nr_samples
, &rtex
->fmask
);
872 rtex
->fmask
.offset
= align64(rtex
->size
, rtex
->fmask
.alignment
);
873 rtex
->size
= rtex
->fmask
.offset
+ rtex
->fmask
.size
;
876 void si_texture_get_cmask_info(struct r600_common_screen
*rscreen
,
877 struct r600_texture
*rtex
,
878 struct r600_cmask_info
*out
)
880 unsigned pipe_interleave_bytes
= rscreen
->info
.pipe_interleave_bytes
;
881 unsigned num_pipes
= rscreen
->info
.num_tile_pipes
;
882 unsigned cl_width
, cl_height
;
884 if (rscreen
->chip_class
>= GFX9
) {
885 out
->alignment
= rtex
->surface
.u
.gfx9
.cmask_alignment
;
886 out
->size
= rtex
->surface
.u
.gfx9
.cmask_size
;
903 case 16: /* Hawaii */
912 unsigned base_align
= num_pipes
* pipe_interleave_bytes
;
914 unsigned width
= align(rtex
->resource
.b
.b
.width0
, cl_width
*8);
915 unsigned height
= align(rtex
->resource
.b
.b
.height0
, cl_height
*8);
916 unsigned slice_elements
= (width
* height
) / (8*8);
918 /* Each element of CMASK is a nibble. */
919 unsigned slice_bytes
= slice_elements
/ 2;
921 out
->slice_tile_max
= (width
* height
) / (128*128);
922 if (out
->slice_tile_max
)
923 out
->slice_tile_max
-= 1;
925 out
->alignment
= MAX2(256, base_align
);
926 out
->size
= (util_max_layer(&rtex
->resource
.b
.b
, 0) + 1) *
927 align(slice_bytes
, base_align
);
930 static void r600_texture_allocate_cmask(struct r600_common_screen
*rscreen
,
931 struct r600_texture
*rtex
)
933 si_texture_get_cmask_info(rscreen
, rtex
, &rtex
->cmask
);
935 rtex
->cmask
.offset
= align64(rtex
->size
, rtex
->cmask
.alignment
);
936 rtex
->size
= rtex
->cmask
.offset
+ rtex
->cmask
.size
;
938 rtex
->cb_color_info
|= S_028C70_FAST_CLEAR(1);
941 static void r600_texture_get_htile_size(struct r600_common_screen
*rscreen
,
942 struct r600_texture
*rtex
)
944 unsigned cl_width
, cl_height
, width
, height
;
945 unsigned slice_elements
, slice_bytes
, pipe_interleave_bytes
, base_align
;
946 unsigned num_pipes
= rscreen
->info
.num_tile_pipes
;
948 assert(rscreen
->chip_class
<= VI
);
950 rtex
->surface
.htile_size
= 0;
952 /* HTILE is broken with 1D tiling on old kernels and CIK. */
953 if (rscreen
->chip_class
>= CIK
&&
954 rtex
->surface
.u
.legacy
.level
[0].mode
== RADEON_SURF_MODE_1D
&&
955 rscreen
->info
.drm_major
== 2 && rscreen
->info
.drm_minor
< 38)
958 /* Overalign HTILE on P2 configs to work around GPU hangs in
959 * piglit/depthstencil-render-miplevels 585.
961 * This has been confirmed to help Kabini & Stoney, where the hangs
962 * are always reproducible. I think I have seen the test hang
963 * on Carrizo too, though it was very rare there.
965 if (rscreen
->chip_class
>= CIK
&& num_pipes
< 4)
994 width
= align(rtex
->resource
.b
.b
.width0
, cl_width
* 8);
995 height
= align(rtex
->resource
.b
.b
.height0
, cl_height
* 8);
997 slice_elements
= (width
* height
) / (8 * 8);
998 slice_bytes
= slice_elements
* 4;
1000 pipe_interleave_bytes
= rscreen
->info
.pipe_interleave_bytes
;
1001 base_align
= num_pipes
* pipe_interleave_bytes
;
1003 rtex
->surface
.htile_alignment
= base_align
;
1004 rtex
->surface
.htile_size
=
1005 (util_max_layer(&rtex
->resource
.b
.b
, 0) + 1) *
1006 align(slice_bytes
, base_align
);
1009 static void r600_texture_allocate_htile(struct r600_common_screen
*rscreen
,
1010 struct r600_texture
*rtex
)
1012 if (rscreen
->chip_class
<= VI
&& !rtex
->tc_compatible_htile
)
1013 r600_texture_get_htile_size(rscreen
, rtex
);
1015 if (!rtex
->surface
.htile_size
)
1018 rtex
->htile_offset
= align(rtex
->size
, rtex
->surface
.htile_alignment
);
1019 rtex
->size
= rtex
->htile_offset
+ rtex
->surface
.htile_size
;
1022 void si_print_texture_info(struct r600_common_screen
*rscreen
,
1023 struct r600_texture
*rtex
, struct u_log_context
*log
)
1027 /* Common parameters. */
1028 u_log_printf(log
, " Info: npix_x=%u, npix_y=%u, npix_z=%u, blk_w=%u, "
1029 "blk_h=%u, array_size=%u, last_level=%u, "
1030 "bpe=%u, nsamples=%u, flags=0x%x, %s\n",
1031 rtex
->resource
.b
.b
.width0
, rtex
->resource
.b
.b
.height0
,
1032 rtex
->resource
.b
.b
.depth0
, rtex
->surface
.blk_w
,
1033 rtex
->surface
.blk_h
,
1034 rtex
->resource
.b
.b
.array_size
, rtex
->resource
.b
.b
.last_level
,
1035 rtex
->surface
.bpe
, rtex
->resource
.b
.b
.nr_samples
,
1036 rtex
->surface
.flags
, util_format_short_name(rtex
->resource
.b
.b
.format
));
1038 if (rscreen
->chip_class
>= GFX9
) {
1039 u_log_printf(log
, " Surf: size=%"PRIu64
", slice_size=%"PRIu64
", "
1040 "alignment=%u, swmode=%u, epitch=%u, pitch=%u\n",
1041 rtex
->surface
.surf_size
,
1042 rtex
->surface
.u
.gfx9
.surf_slice_size
,
1043 rtex
->surface
.surf_alignment
,
1044 rtex
->surface
.u
.gfx9
.surf
.swizzle_mode
,
1045 rtex
->surface
.u
.gfx9
.surf
.epitch
,
1046 rtex
->surface
.u
.gfx9
.surf_pitch
);
1048 if (rtex
->fmask
.size
) {
1049 u_log_printf(log
, " FMASK: offset=%"PRIu64
", size=%"PRIu64
", "
1050 "alignment=%u, swmode=%u, epitch=%u\n",
1052 rtex
->surface
.u
.gfx9
.fmask_size
,
1053 rtex
->surface
.u
.gfx9
.fmask_alignment
,
1054 rtex
->surface
.u
.gfx9
.fmask
.swizzle_mode
,
1055 rtex
->surface
.u
.gfx9
.fmask
.epitch
);
1058 if (rtex
->cmask
.size
) {
1059 u_log_printf(log
, " CMask: offset=%"PRIu64
", size=%"PRIu64
", "
1060 "alignment=%u, rb_aligned=%u, pipe_aligned=%u\n",
1062 rtex
->surface
.u
.gfx9
.cmask_size
,
1063 rtex
->surface
.u
.gfx9
.cmask_alignment
,
1064 rtex
->surface
.u
.gfx9
.cmask
.rb_aligned
,
1065 rtex
->surface
.u
.gfx9
.cmask
.pipe_aligned
);
1068 if (rtex
->htile_offset
) {
1069 u_log_printf(log
, " HTile: offset=%"PRIu64
", size=%u, alignment=%u, "
1070 "rb_aligned=%u, pipe_aligned=%u\n",
1072 rtex
->surface
.htile_size
,
1073 rtex
->surface
.htile_alignment
,
1074 rtex
->surface
.u
.gfx9
.htile
.rb_aligned
,
1075 rtex
->surface
.u
.gfx9
.htile
.pipe_aligned
);
1078 if (rtex
->dcc_offset
) {
1079 u_log_printf(log
, " DCC: offset=%"PRIu64
", size=%u, "
1080 "alignment=%u, pitch_max=%u, num_dcc_levels=%u\n",
1081 rtex
->dcc_offset
, rtex
->surface
.dcc_size
,
1082 rtex
->surface
.dcc_alignment
,
1083 rtex
->surface
.u
.gfx9
.dcc_pitch_max
,
1084 rtex
->surface
.num_dcc_levels
);
1087 if (rtex
->surface
.u
.gfx9
.stencil_offset
) {
1088 u_log_printf(log
, " Stencil: offset=%"PRIu64
", swmode=%u, epitch=%u\n",
1089 rtex
->surface
.u
.gfx9
.stencil_offset
,
1090 rtex
->surface
.u
.gfx9
.stencil
.swizzle_mode
,
1091 rtex
->surface
.u
.gfx9
.stencil
.epitch
);
1096 u_log_printf(log
, " Layout: size=%"PRIu64
", alignment=%u, bankw=%u, "
1097 "bankh=%u, nbanks=%u, mtilea=%u, tilesplit=%u, pipeconfig=%u, scanout=%u\n",
1098 rtex
->surface
.surf_size
, rtex
->surface
.surf_alignment
, rtex
->surface
.u
.legacy
.bankw
,
1099 rtex
->surface
.u
.legacy
.bankh
, rtex
->surface
.u
.legacy
.num_banks
, rtex
->surface
.u
.legacy
.mtilea
,
1100 rtex
->surface
.u
.legacy
.tile_split
, rtex
->surface
.u
.legacy
.pipe_config
,
1101 (rtex
->surface
.flags
& RADEON_SURF_SCANOUT
) != 0);
1103 if (rtex
->fmask
.size
)
1104 u_log_printf(log
, " FMask: offset=%"PRIu64
", size=%"PRIu64
", alignment=%u, pitch_in_pixels=%u, "
1105 "bankh=%u, slice_tile_max=%u, tile_mode_index=%u\n",
1106 rtex
->fmask
.offset
, rtex
->fmask
.size
, rtex
->fmask
.alignment
,
1107 rtex
->fmask
.pitch_in_pixels
, rtex
->fmask
.bank_height
,
1108 rtex
->fmask
.slice_tile_max
, rtex
->fmask
.tile_mode_index
);
1110 if (rtex
->cmask
.size
)
1111 u_log_printf(log
, " CMask: offset=%"PRIu64
", size=%"PRIu64
", alignment=%u, "
1112 "slice_tile_max=%u\n",
1113 rtex
->cmask
.offset
, rtex
->cmask
.size
, rtex
->cmask
.alignment
,
1114 rtex
->cmask
.slice_tile_max
);
1116 if (rtex
->htile_offset
)
1117 u_log_printf(log
, " HTile: offset=%"PRIu64
", size=%u, "
1118 "alignment=%u, TC_compatible = %u\n",
1119 rtex
->htile_offset
, rtex
->surface
.htile_size
,
1120 rtex
->surface
.htile_alignment
,
1121 rtex
->tc_compatible_htile
);
1123 if (rtex
->dcc_offset
) {
1124 u_log_printf(log
, " DCC: offset=%"PRIu64
", size=%u, alignment=%u\n",
1125 rtex
->dcc_offset
, rtex
->surface
.dcc_size
,
1126 rtex
->surface
.dcc_alignment
);
1127 for (i
= 0; i
<= rtex
->resource
.b
.b
.last_level
; i
++)
1128 u_log_printf(log
, " DCCLevel[%i]: enabled=%u, offset=%u, "
1129 "fast_clear_size=%u\n",
1130 i
, i
< rtex
->surface
.num_dcc_levels
,
1131 rtex
->surface
.u
.legacy
.level
[i
].dcc_offset
,
1132 rtex
->surface
.u
.legacy
.level
[i
].dcc_fast_clear_size
);
1135 for (i
= 0; i
<= rtex
->resource
.b
.b
.last_level
; i
++)
1136 u_log_printf(log
, " Level[%i]: offset=%"PRIu64
", slice_size=%"PRIu64
", "
1137 "npix_x=%u, npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
1138 "mode=%u, tiling_index = %u\n",
1139 i
, rtex
->surface
.u
.legacy
.level
[i
].offset
,
1140 (uint64_t)rtex
->surface
.u
.legacy
.level
[i
].slice_size_dw
* 4,
1141 u_minify(rtex
->resource
.b
.b
.width0
, i
),
1142 u_minify(rtex
->resource
.b
.b
.height0
, i
),
1143 u_minify(rtex
->resource
.b
.b
.depth0
, i
),
1144 rtex
->surface
.u
.legacy
.level
[i
].nblk_x
,
1145 rtex
->surface
.u
.legacy
.level
[i
].nblk_y
,
1146 rtex
->surface
.u
.legacy
.level
[i
].mode
,
1147 rtex
->surface
.u
.legacy
.tiling_index
[i
]);
1149 if (rtex
->surface
.has_stencil
) {
1150 u_log_printf(log
, " StencilLayout: tilesplit=%u\n",
1151 rtex
->surface
.u
.legacy
.stencil_tile_split
);
1152 for (i
= 0; i
<= rtex
->resource
.b
.b
.last_level
; i
++) {
1153 u_log_printf(log
, " StencilLevel[%i]: offset=%"PRIu64
", "
1154 "slice_size=%"PRIu64
", npix_x=%u, "
1155 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
1156 "mode=%u, tiling_index = %u\n",
1157 i
, rtex
->surface
.u
.legacy
.stencil_level
[i
].offset
,
1158 (uint64_t)rtex
->surface
.u
.legacy
.stencil_level
[i
].slice_size_dw
* 4,
1159 u_minify(rtex
->resource
.b
.b
.width0
, i
),
1160 u_minify(rtex
->resource
.b
.b
.height0
, i
),
1161 u_minify(rtex
->resource
.b
.b
.depth0
, i
),
1162 rtex
->surface
.u
.legacy
.stencil_level
[i
].nblk_x
,
1163 rtex
->surface
.u
.legacy
.stencil_level
[i
].nblk_y
,
1164 rtex
->surface
.u
.legacy
.stencil_level
[i
].mode
,
1165 rtex
->surface
.u
.legacy
.stencil_tiling_index
[i
]);
1170 /* Common processing for r600_texture_create and r600_texture_from_handle */
1171 static struct r600_texture
*
1172 r600_texture_create_object(struct pipe_screen
*screen
,
1173 const struct pipe_resource
*base
,
1174 struct pb_buffer
*buf
,
1175 struct radeon_surf
*surface
)
1177 struct r600_texture
*rtex
;
1178 struct r600_resource
*resource
;
1179 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)screen
;
1181 rtex
= CALLOC_STRUCT(r600_texture
);
1185 resource
= &rtex
->resource
;
1186 resource
->b
.b
= *base
;
1187 resource
->b
.b
.next
= NULL
;
1188 resource
->b
.vtbl
= &r600_texture_vtbl
;
1189 pipe_reference_init(&resource
->b
.b
.reference
, 1);
1190 resource
->b
.b
.screen
= screen
;
1192 /* don't include stencil-only formats which we don't support for rendering */
1193 rtex
->is_depth
= util_format_has_depth(util_format_description(rtex
->resource
.b
.b
.format
));
1195 rtex
->surface
= *surface
;
1196 rtex
->size
= rtex
->surface
.surf_size
;
1198 rtex
->tc_compatible_htile
= rtex
->surface
.htile_size
!= 0 &&
1199 (rtex
->surface
.flags
&
1200 RADEON_SURF_TC_COMPATIBLE_HTILE
);
1202 /* TC-compatible HTILE:
1203 * - VI only supports Z32_FLOAT.
1204 * - GFX9 only supports Z32_FLOAT and Z16_UNORM. */
1205 if (rtex
->tc_compatible_htile
) {
1206 if (rscreen
->chip_class
>= GFX9
&&
1207 base
->format
== PIPE_FORMAT_Z16_UNORM
)
1208 rtex
->db_render_format
= base
->format
;
1210 rtex
->db_render_format
= PIPE_FORMAT_Z32_FLOAT
;
1211 rtex
->upgraded_depth
= base
->format
!= PIPE_FORMAT_Z32_FLOAT
&&
1212 base
->format
!= PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
;
1215 rtex
->db_render_format
= base
->format
;
1218 /* Applies to GCN. */
1219 rtex
->last_msaa_resolve_target_micro_mode
= rtex
->surface
.micro_tile_mode
;
1221 /* Disable separate DCC at the beginning. DRI2 doesn't reuse buffers
1222 * between frames, so the only thing that can enable separate DCC
1223 * with DRI2 is multiple slow clears within a frame.
1225 rtex
->ps_draw_ratio
= 0;
1227 if (rtex
->is_depth
) {
1228 if (rscreen
->chip_class
>= GFX9
) {
1229 rtex
->can_sample_z
= true;
1230 rtex
->can_sample_s
= true;
1232 rtex
->can_sample_z
= !rtex
->surface
.u
.legacy
.depth_adjusted
;
1233 rtex
->can_sample_s
= !rtex
->surface
.u
.legacy
.stencil_adjusted
;
1236 if (!(base
->flags
& (R600_RESOURCE_FLAG_TRANSFER
|
1237 R600_RESOURCE_FLAG_FLUSHED_DEPTH
))) {
1238 rtex
->db_compatible
= true;
1240 if (!(rscreen
->debug_flags
& DBG(NO_HYPERZ
)))
1241 r600_texture_allocate_htile(rscreen
, rtex
);
1244 if (base
->nr_samples
> 1) {
1246 r600_texture_allocate_fmask(rscreen
, rtex
);
1247 r600_texture_allocate_cmask(rscreen
, rtex
);
1248 rtex
->cmask_buffer
= &rtex
->resource
;
1250 if (!rtex
->fmask
.size
|| !rtex
->cmask
.size
) {
1256 /* Shared textures must always set up DCC here.
1257 * If it's not present, it will be disabled by
1258 * apply_opaque_metadata later.
1260 if (rtex
->surface
.dcc_size
&&
1261 (buf
|| !(rscreen
->debug_flags
& DBG(NO_DCC
))) &&
1262 !(rtex
->surface
.flags
& RADEON_SURF_SCANOUT
)) {
1263 /* Reserve space for the DCC buffer. */
1264 rtex
->dcc_offset
= align64(rtex
->size
, rtex
->surface
.dcc_alignment
);
1265 rtex
->size
= rtex
->dcc_offset
+ rtex
->surface
.dcc_size
;
1269 /* Now create the backing buffer. */
1271 si_init_resource_fields(rscreen
, resource
, rtex
->size
,
1272 rtex
->surface
.surf_alignment
);
1274 if (!si_alloc_resource(rscreen
, resource
)) {
1279 resource
->buf
= buf
;
1280 resource
->gpu_address
= rscreen
->ws
->buffer_get_virtual_address(resource
->buf
);
1281 resource
->bo_size
= buf
->size
;
1282 resource
->bo_alignment
= buf
->alignment
;
1283 resource
->domains
= rscreen
->ws
->buffer_get_initial_domain(resource
->buf
);
1284 if (resource
->domains
& RADEON_DOMAIN_VRAM
)
1285 resource
->vram_usage
= buf
->size
;
1286 else if (resource
->domains
& RADEON_DOMAIN_GTT
)
1287 resource
->gart_usage
= buf
->size
;
1290 if (rtex
->cmask
.size
) {
1291 /* Initialize the cmask to 0xCC (= compressed state). */
1292 si_screen_clear_buffer(rscreen
, &rtex
->cmask_buffer
->b
.b
,
1293 rtex
->cmask
.offset
, rtex
->cmask
.size
,
1296 if (rtex
->htile_offset
) {
1297 uint32_t clear_value
= 0;
1299 if (rscreen
->chip_class
>= GFX9
|| rtex
->tc_compatible_htile
)
1300 clear_value
= 0x0000030F;
1302 si_screen_clear_buffer(rscreen
, &rtex
->resource
.b
.b
,
1304 rtex
->surface
.htile_size
,
1308 /* Initialize DCC only if the texture is not being imported. */
1309 if (!buf
&& rtex
->dcc_offset
) {
1310 si_screen_clear_buffer(rscreen
, &rtex
->resource
.b
.b
,
1312 rtex
->surface
.dcc_size
,
1316 /* Initialize the CMASK base register value. */
1317 rtex
->cmask
.base_address_reg
=
1318 (rtex
->resource
.gpu_address
+ rtex
->cmask
.offset
) >> 8;
1320 if (rscreen
->debug_flags
& DBG(VM
)) {
1321 fprintf(stderr
, "VM start=0x%"PRIX64
" end=0x%"PRIX64
" | Texture %ix%ix%i, %i levels, %i samples, %s\n",
1322 rtex
->resource
.gpu_address
,
1323 rtex
->resource
.gpu_address
+ rtex
->resource
.buf
->size
,
1324 base
->width0
, base
->height0
, util_max_layer(base
, 0)+1, base
->last_level
+1,
1325 base
->nr_samples
? base
->nr_samples
: 1, util_format_short_name(base
->format
));
1328 if (rscreen
->debug_flags
& DBG(TEX
)) {
1330 struct u_log_context log
;
1331 u_log_context_init(&log
);
1332 si_print_texture_info(rscreen
, rtex
, &log
);
1333 u_log_new_page_print(&log
, stdout
);
1335 u_log_context_destroy(&log
);
1341 static enum radeon_surf_mode
1342 r600_choose_tiling(struct r600_common_screen
*rscreen
,
1343 const struct pipe_resource
*templ
)
1345 const struct util_format_description
*desc
= util_format_description(templ
->format
);
1346 bool force_tiling
= templ
->flags
& R600_RESOURCE_FLAG_FORCE_TILING
;
1347 bool is_depth_stencil
= util_format_is_depth_or_stencil(templ
->format
) &&
1348 !(templ
->flags
& R600_RESOURCE_FLAG_FLUSHED_DEPTH
);
1350 /* MSAA resources must be 2D tiled. */
1351 if (templ
->nr_samples
> 1)
1352 return RADEON_SURF_MODE_2D
;
1354 /* Transfer resources should be linear. */
1355 if (templ
->flags
& R600_RESOURCE_FLAG_TRANSFER
)
1356 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1358 /* Avoid Z/S decompress blits by forcing TC-compatible HTILE on VI,
1359 * which requires 2D tiling.
1361 if (rscreen
->chip_class
== VI
&&
1363 (templ
->flags
& PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY
))
1364 return RADEON_SURF_MODE_2D
;
1366 /* Handle common candidates for the linear mode.
1367 * Compressed textures and DB surfaces must always be tiled.
1369 if (!force_tiling
&&
1370 !is_depth_stencil
&&
1371 !util_format_is_compressed(templ
->format
)) {
1372 if (rscreen
->debug_flags
& DBG(NO_TILING
))
1373 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1375 /* Tiling doesn't work with the 422 (SUBSAMPLED) formats on R600+. */
1376 if (desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
)
1377 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1379 /* Cursors are linear on SI.
1380 * (XXX double-check, maybe also use RADEON_SURF_SCANOUT) */
1381 if (templ
->bind
& PIPE_BIND_CURSOR
)
1382 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1384 if (templ
->bind
& PIPE_BIND_LINEAR
)
1385 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1387 /* Textures with a very small height are recommended to be linear. */
1388 if (templ
->target
== PIPE_TEXTURE_1D
||
1389 templ
->target
== PIPE_TEXTURE_1D_ARRAY
||
1390 /* Only very thin and long 2D textures should benefit from
1391 * linear_aligned. */
1392 (templ
->width0
> 8 && templ
->height0
<= 2))
1393 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1395 /* Textures likely to be mapped often. */
1396 if (templ
->usage
== PIPE_USAGE_STAGING
||
1397 templ
->usage
== PIPE_USAGE_STREAM
)
1398 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1401 /* Make small textures 1D tiled. */
1402 if (templ
->width0
<= 16 || templ
->height0
<= 16 ||
1403 (rscreen
->debug_flags
& DBG(NO_2D_TILING
)))
1404 return RADEON_SURF_MODE_1D
;
1406 /* The allocator will switch to 1D if needed. */
1407 return RADEON_SURF_MODE_2D
;
1410 struct pipe_resource
*si_texture_create(struct pipe_screen
*screen
,
1411 const struct pipe_resource
*templ
)
1413 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)screen
;
1414 struct radeon_surf surface
= {0};
1415 bool is_flushed_depth
= templ
->flags
& R600_RESOURCE_FLAG_FLUSHED_DEPTH
;
1416 bool tc_compatible_htile
=
1417 rscreen
->chip_class
>= VI
&&
1418 (templ
->flags
& PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY
) &&
1419 !(rscreen
->debug_flags
& DBG(NO_HYPERZ
)) &&
1420 !is_flushed_depth
&&
1421 templ
->nr_samples
<= 1 && /* TC-compat HTILE is less efficient with MSAA */
1422 util_format_is_depth_or_stencil(templ
->format
);
1426 r
= r600_init_surface(rscreen
, &surface
, templ
,
1427 r600_choose_tiling(rscreen
, templ
), 0, 0,
1428 false, false, is_flushed_depth
,
1429 tc_compatible_htile
);
1434 return (struct pipe_resource
*)
1435 r600_texture_create_object(screen
, templ
, NULL
, &surface
);
1438 static struct pipe_resource
*r600_texture_from_handle(struct pipe_screen
*screen
,
1439 const struct pipe_resource
*templ
,
1440 struct winsys_handle
*whandle
,
1443 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)screen
;
1444 struct pb_buffer
*buf
= NULL
;
1445 unsigned stride
= 0, offset
= 0;
1446 enum radeon_surf_mode array_mode
;
1447 struct radeon_surf surface
= {};
1449 struct radeon_bo_metadata metadata
= {};
1450 struct r600_texture
*rtex
;
1453 /* Support only 2D textures without mipmaps */
1454 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
) ||
1455 templ
->depth0
!= 1 || templ
->last_level
!= 0)
1458 buf
= rscreen
->ws
->buffer_from_handle(rscreen
->ws
, whandle
, &stride
, &offset
);
1462 rscreen
->ws
->buffer_get_metadata(buf
, &metadata
);
1463 r600_surface_import_metadata(rscreen
, &surface
, &metadata
,
1464 &array_mode
, &is_scanout
);
1466 r
= r600_init_surface(rscreen
, &surface
, templ
, array_mode
, stride
,
1467 offset
, true, is_scanout
, false, false);
1472 rtex
= r600_texture_create_object(screen
, templ
, buf
, &surface
);
1476 rtex
->resource
.b
.is_shared
= true;
1477 rtex
->resource
.external_usage
= usage
;
1479 si_apply_opaque_metadata(rscreen
, rtex
, &metadata
);
1481 assert(rtex
->surface
.tile_swizzle
== 0);
1482 return &rtex
->resource
.b
.b
;
1485 bool si_init_flushed_depth_texture(struct pipe_context
*ctx
,
1486 struct pipe_resource
*texture
,
1487 struct r600_texture
**staging
)
1489 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
1490 struct pipe_resource resource
;
1491 struct r600_texture
**flushed_depth_texture
= staging
?
1492 staging
: &rtex
->flushed_depth_texture
;
1493 enum pipe_format pipe_format
= texture
->format
;
1496 if (rtex
->flushed_depth_texture
)
1497 return true; /* it's ready */
1499 if (!rtex
->can_sample_z
&& rtex
->can_sample_s
) {
1500 switch (pipe_format
) {
1501 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1502 /* Save memory by not allocating the S plane. */
1503 pipe_format
= PIPE_FORMAT_Z32_FLOAT
;
1505 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1506 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1507 /* Save memory bandwidth by not copying the
1508 * stencil part during flush.
1510 * This potentially increases memory bandwidth
1511 * if an application uses both Z and S texturing
1512 * simultaneously (a flushed Z24S8 texture
1513 * would be stored compactly), but how often
1514 * does that really happen?
1516 pipe_format
= PIPE_FORMAT_Z24X8_UNORM
;
1520 } else if (!rtex
->can_sample_s
&& rtex
->can_sample_z
) {
1521 assert(util_format_has_stencil(util_format_description(pipe_format
)));
1523 /* DB->CB copies to an 8bpp surface don't work. */
1524 pipe_format
= PIPE_FORMAT_X24S8_UINT
;
1528 memset(&resource
, 0, sizeof(resource
));
1529 resource
.target
= texture
->target
;
1530 resource
.format
= pipe_format
;
1531 resource
.width0
= texture
->width0
;
1532 resource
.height0
= texture
->height0
;
1533 resource
.depth0
= texture
->depth0
;
1534 resource
.array_size
= texture
->array_size
;
1535 resource
.last_level
= texture
->last_level
;
1536 resource
.nr_samples
= texture
->nr_samples
;
1537 resource
.usage
= staging
? PIPE_USAGE_STAGING
: PIPE_USAGE_DEFAULT
;
1538 resource
.bind
= texture
->bind
& ~PIPE_BIND_DEPTH_STENCIL
;
1539 resource
.flags
= texture
->flags
| R600_RESOURCE_FLAG_FLUSHED_DEPTH
;
1542 resource
.flags
|= R600_RESOURCE_FLAG_TRANSFER
;
1544 *flushed_depth_texture
= (struct r600_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
1545 if (*flushed_depth_texture
== NULL
) {
1546 R600_ERR("failed to create temporary texture to hold flushed depth\n");
1553 * Initialize the pipe_resource descriptor to be of the same size as the box,
1554 * which is supposed to hold a subregion of the texture "orig" at the given
1557 static void r600_init_temp_resource_from_box(struct pipe_resource
*res
,
1558 struct pipe_resource
*orig
,
1559 const struct pipe_box
*box
,
1560 unsigned level
, unsigned flags
)
1562 memset(res
, 0, sizeof(*res
));
1563 res
->format
= orig
->format
;
1564 res
->width0
= box
->width
;
1565 res
->height0
= box
->height
;
1567 res
->array_size
= 1;
1568 res
->usage
= flags
& R600_RESOURCE_FLAG_TRANSFER
? PIPE_USAGE_STAGING
: PIPE_USAGE_DEFAULT
;
1571 /* We must set the correct texture target and dimensions for a 3D box. */
1572 if (box
->depth
> 1 && util_max_layer(orig
, level
) > 0) {
1573 res
->target
= PIPE_TEXTURE_2D_ARRAY
;
1574 res
->array_size
= box
->depth
;
1576 res
->target
= PIPE_TEXTURE_2D
;
1580 static bool r600_can_invalidate_texture(struct r600_common_screen
*rscreen
,
1581 struct r600_texture
*rtex
,
1582 unsigned transfer_usage
,
1583 const struct pipe_box
*box
)
1585 return !rtex
->resource
.b
.is_shared
&&
1586 !(transfer_usage
& PIPE_TRANSFER_READ
) &&
1587 rtex
->resource
.b
.b
.last_level
== 0 &&
1588 util_texrange_covers_whole_level(&rtex
->resource
.b
.b
, 0,
1589 box
->x
, box
->y
, box
->z
,
1590 box
->width
, box
->height
,
1594 static void r600_texture_invalidate_storage(struct r600_common_context
*rctx
,
1595 struct r600_texture
*rtex
)
1597 struct r600_common_screen
*rscreen
= rctx
->screen
;
1599 /* There is no point in discarding depth and tiled buffers. */
1600 assert(!rtex
->is_depth
);
1601 assert(rtex
->surface
.is_linear
);
1603 /* Reallocate the buffer in the same pipe_resource. */
1604 si_alloc_resource(rscreen
, &rtex
->resource
);
1606 /* Initialize the CMASK base address (needed even without CMASK). */
1607 rtex
->cmask
.base_address_reg
=
1608 (rtex
->resource
.gpu_address
+ rtex
->cmask
.offset
) >> 8;
1610 p_atomic_inc(&rscreen
->dirty_tex_counter
);
1612 rctx
->num_alloc_tex_transfer_bytes
+= rtex
->size
;
1615 static void *r600_texture_transfer_map(struct pipe_context
*ctx
,
1616 struct pipe_resource
*texture
,
1619 const struct pipe_box
*box
,
1620 struct pipe_transfer
**ptransfer
)
1622 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
1623 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
1624 struct r600_transfer
*trans
;
1625 struct r600_resource
*buf
;
1626 unsigned offset
= 0;
1628 bool use_staging_texture
= false;
1630 assert(!(texture
->flags
& R600_RESOURCE_FLAG_TRANSFER
));
1631 assert(box
->width
&& box
->height
&& box
->depth
);
1633 /* Depth textures use staging unconditionally. */
1634 if (!rtex
->is_depth
) {
1635 /* Degrade the tile mode if we get too many transfers on APUs.
1636 * On dGPUs, the staging texture is always faster.
1637 * Only count uploads that are at least 4x4 pixels large.
1639 if (!rctx
->screen
->info
.has_dedicated_vram
&&
1641 box
->width
>= 4 && box
->height
>= 4 &&
1642 p_atomic_inc_return(&rtex
->num_level0_transfers
) == 10) {
1643 bool can_invalidate
=
1644 r600_can_invalidate_texture(rctx
->screen
, rtex
,
1647 r600_reallocate_texture_inplace(rctx
, rtex
,
1652 /* Tiled textures need to be converted into a linear texture for CPU
1653 * access. The staging texture is always linear and is placed in GART.
1655 * Reading from VRAM or GTT WC is slow, always use the staging
1656 * texture in this case.
1658 * Use the staging texture for uploads if the underlying BO
1661 if (!rtex
->surface
.is_linear
)
1662 use_staging_texture
= true;
1663 else if (usage
& PIPE_TRANSFER_READ
)
1664 use_staging_texture
=
1665 rtex
->resource
.domains
& RADEON_DOMAIN_VRAM
||
1666 rtex
->resource
.flags
& RADEON_FLAG_GTT_WC
;
1667 /* Write & linear only: */
1668 else if (si_rings_is_buffer_referenced(rctx
, rtex
->resource
.buf
,
1669 RADEON_USAGE_READWRITE
) ||
1670 !rctx
->ws
->buffer_wait(rtex
->resource
.buf
, 0,
1671 RADEON_USAGE_READWRITE
)) {
1673 if (r600_can_invalidate_texture(rctx
->screen
, rtex
,
1675 r600_texture_invalidate_storage(rctx
, rtex
);
1677 use_staging_texture
= true;
1681 trans
= CALLOC_STRUCT(r600_transfer
);
1684 pipe_resource_reference(&trans
->b
.b
.resource
, texture
);
1685 trans
->b
.b
.level
= level
;
1686 trans
->b
.b
.usage
= usage
;
1687 trans
->b
.b
.box
= *box
;
1689 if (rtex
->is_depth
) {
1690 struct r600_texture
*staging_depth
;
1692 if (rtex
->resource
.b
.b
.nr_samples
> 1) {
1693 /* MSAA depth buffers need to be converted to single sample buffers.
1695 * Mapping MSAA depth buffers can occur if ReadPixels is called
1696 * with a multisample GLX visual.
1698 * First downsample the depth buffer to a temporary texture,
1699 * then decompress the temporary one to staging.
1701 * Only the region being mapped is transfered.
1703 struct pipe_resource resource
;
1705 r600_init_temp_resource_from_box(&resource
, texture
, box
, level
, 0);
1707 if (!si_init_flushed_depth_texture(ctx
, &resource
, &staging_depth
)) {
1708 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1713 if (usage
& PIPE_TRANSFER_READ
) {
1714 struct pipe_resource
*temp
= ctx
->screen
->resource_create(ctx
->screen
, &resource
);
1716 R600_ERR("failed to create a temporary depth texture\n");
1721 r600_copy_region_with_blit(ctx
, temp
, 0, 0, 0, 0, texture
, level
, box
);
1722 rctx
->blit_decompress_depth(ctx
, (struct r600_texture
*)temp
, staging_depth
,
1723 0, 0, 0, box
->depth
, 0, 0);
1724 pipe_resource_reference(&temp
, NULL
);
1727 /* Just get the strides. */
1728 r600_texture_get_offset(rctx
->screen
, staging_depth
, level
, NULL
,
1730 &trans
->b
.b
.layer_stride
);
1732 /* XXX: only readback the rectangle which is being mapped? */
1733 /* XXX: when discard is true, no need to read back from depth texture */
1734 if (!si_init_flushed_depth_texture(ctx
, texture
, &staging_depth
)) {
1735 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1740 rctx
->blit_decompress_depth(ctx
, rtex
, staging_depth
,
1742 box
->z
, box
->z
+ box
->depth
- 1,
1745 offset
= r600_texture_get_offset(rctx
->screen
, staging_depth
,
1748 &trans
->b
.b
.layer_stride
);
1751 trans
->staging
= (struct r600_resource
*)staging_depth
;
1752 buf
= trans
->staging
;
1753 } else if (use_staging_texture
) {
1754 struct pipe_resource resource
;
1755 struct r600_texture
*staging
;
1757 r600_init_temp_resource_from_box(&resource
, texture
, box
, level
,
1758 R600_RESOURCE_FLAG_TRANSFER
);
1759 resource
.usage
= (usage
& PIPE_TRANSFER_READ
) ?
1760 PIPE_USAGE_STAGING
: PIPE_USAGE_STREAM
;
1762 /* Create the temporary texture. */
1763 staging
= (struct r600_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
1765 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1769 trans
->staging
= &staging
->resource
;
1771 /* Just get the strides. */
1772 r600_texture_get_offset(rctx
->screen
, staging
, 0, NULL
,
1774 &trans
->b
.b
.layer_stride
);
1776 if (usage
& PIPE_TRANSFER_READ
)
1777 r600_copy_to_staging_texture(ctx
, trans
);
1779 usage
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
1781 buf
= trans
->staging
;
1783 /* the resource is mapped directly */
1784 offset
= r600_texture_get_offset(rctx
->screen
, rtex
, level
, box
,
1786 &trans
->b
.b
.layer_stride
);
1787 buf
= &rtex
->resource
;
1790 if (!(map
= si_buffer_map_sync_with_rings(rctx
, buf
, usage
))) {
1791 r600_resource_reference(&trans
->staging
, NULL
);
1796 *ptransfer
= &trans
->b
.b
;
1797 return map
+ offset
;
1800 static void r600_texture_transfer_unmap(struct pipe_context
*ctx
,
1801 struct pipe_transfer
* transfer
)
1803 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
1804 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
1805 struct pipe_resource
*texture
= transfer
->resource
;
1806 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
1808 if ((transfer
->usage
& PIPE_TRANSFER_WRITE
) && rtransfer
->staging
) {
1809 if (rtex
->is_depth
&& rtex
->resource
.b
.b
.nr_samples
<= 1) {
1810 ctx
->resource_copy_region(ctx
, texture
, transfer
->level
,
1811 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
1812 &rtransfer
->staging
->b
.b
, transfer
->level
,
1815 r600_copy_from_staging_texture(ctx
, rtransfer
);
1819 if (rtransfer
->staging
) {
1820 rctx
->num_alloc_tex_transfer_bytes
+= rtransfer
->staging
->buf
->size
;
1821 r600_resource_reference(&rtransfer
->staging
, NULL
);
1824 /* Heuristic for {upload, draw, upload, draw, ..}:
1826 * Flush the gfx IB if we've allocated too much texture storage.
1828 * The idea is that we don't want to build IBs that use too much
1829 * memory and put pressure on the kernel memory manager and we also
1830 * want to make temporary and invalidated buffers go idle ASAP to
1831 * decrease the total memory usage or make them reusable. The memory
1832 * usage will be slightly higher than given here because of the buffer
1833 * cache in the winsys.
1835 * The result is that the kernel memory manager is never a bottleneck.
1837 if (rctx
->num_alloc_tex_transfer_bytes
> rctx
->screen
->info
.gart_size
/ 4) {
1838 rctx
->gfx
.flush(rctx
, RADEON_FLUSH_ASYNC
, NULL
);
1839 rctx
->num_alloc_tex_transfer_bytes
= 0;
1842 pipe_resource_reference(&transfer
->resource
, NULL
);
1846 static const struct u_resource_vtbl r600_texture_vtbl
=
1848 NULL
, /* get_handle */
1849 r600_texture_destroy
, /* resource_destroy */
1850 r600_texture_transfer_map
, /* transfer_map */
1851 u_default_transfer_flush_region
, /* transfer_flush_region */
1852 r600_texture_transfer_unmap
, /* transfer_unmap */
1855 /* DCC channel type categories within which formats can be reinterpreted
1856 * while keeping the same DCC encoding. The swizzle must also match. */
1857 enum dcc_channel_type
{
1858 dcc_channel_float32
,
1861 dcc_channel_float16
,
1864 dcc_channel_uint_10_10_10_2
,
1867 dcc_channel_incompatible
,
1870 /* Return the type of DCC encoding. */
1871 static enum dcc_channel_type
1872 vi_get_dcc_channel_type(const struct util_format_description
*desc
)
1876 /* Find the first non-void channel. */
1877 for (i
= 0; i
< desc
->nr_channels
; i
++)
1878 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
)
1880 if (i
== desc
->nr_channels
)
1881 return dcc_channel_incompatible
;
1883 switch (desc
->channel
[i
].size
) {
1885 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_FLOAT
)
1886 return dcc_channel_float32
;
1887 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
)
1888 return dcc_channel_uint32
;
1889 return dcc_channel_sint32
;
1891 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_FLOAT
)
1892 return dcc_channel_float16
;
1893 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
)
1894 return dcc_channel_uint16
;
1895 return dcc_channel_sint16
;
1897 return dcc_channel_uint_10_10_10_2
;
1899 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
)
1900 return dcc_channel_uint8
;
1901 return dcc_channel_sint8
;
1903 return dcc_channel_incompatible
;
1907 /* Return if it's allowed to reinterpret one format as another with DCC enabled. */
1908 bool vi_dcc_formats_compatible(enum pipe_format format1
,
1909 enum pipe_format format2
)
1911 const struct util_format_description
*desc1
, *desc2
;
1912 enum dcc_channel_type type1
, type2
;
1915 if (format1
== format2
)
1918 desc1
= util_format_description(format1
);
1919 desc2
= util_format_description(format2
);
1921 if (desc1
->nr_channels
!= desc2
->nr_channels
)
1924 /* Swizzles must be the same. */
1925 for (i
= 0; i
< desc1
->nr_channels
; i
++)
1926 if (desc1
->swizzle
[i
] <= PIPE_SWIZZLE_W
&&
1927 desc2
->swizzle
[i
] <= PIPE_SWIZZLE_W
&&
1928 desc1
->swizzle
[i
] != desc2
->swizzle
[i
])
1931 type1
= vi_get_dcc_channel_type(desc1
);
1932 type2
= vi_get_dcc_channel_type(desc2
);
1934 return type1
!= dcc_channel_incompatible
&&
1935 type2
!= dcc_channel_incompatible
&&
1939 bool vi_dcc_formats_are_incompatible(struct pipe_resource
*tex
,
1941 enum pipe_format view_format
)
1943 struct r600_texture
*rtex
= (struct r600_texture
*)tex
;
1945 return vi_dcc_enabled(rtex
, level
) &&
1946 !vi_dcc_formats_compatible(tex
->format
, view_format
);
1949 /* This can't be merged with the above function, because
1950 * vi_dcc_formats_compatible should be called only when DCC is enabled. */
1951 void vi_disable_dcc_if_incompatible_format(struct r600_common_context
*rctx
,
1952 struct pipe_resource
*tex
,
1954 enum pipe_format view_format
)
1956 struct r600_texture
*rtex
= (struct r600_texture
*)tex
;
1958 if (vi_dcc_formats_are_incompatible(tex
, level
, view_format
))
1959 if (!si_texture_disable_dcc(rctx
, (struct r600_texture
*)tex
))
1960 rctx
->decompress_dcc(&rctx
->b
, rtex
);
1963 struct pipe_surface
*si_create_surface_custom(struct pipe_context
*pipe
,
1964 struct pipe_resource
*texture
,
1965 const struct pipe_surface
*templ
,
1966 unsigned width0
, unsigned height0
,
1967 unsigned width
, unsigned height
)
1969 struct r600_surface
*surface
= CALLOC_STRUCT(r600_surface
);
1974 assert(templ
->u
.tex
.first_layer
<= util_max_layer(texture
, templ
->u
.tex
.level
));
1975 assert(templ
->u
.tex
.last_layer
<= util_max_layer(texture
, templ
->u
.tex
.level
));
1977 pipe_reference_init(&surface
->base
.reference
, 1);
1978 pipe_resource_reference(&surface
->base
.texture
, texture
);
1979 surface
->base
.context
= pipe
;
1980 surface
->base
.format
= templ
->format
;
1981 surface
->base
.width
= width
;
1982 surface
->base
.height
= height
;
1983 surface
->base
.u
= templ
->u
;
1985 surface
->width0
= width0
;
1986 surface
->height0
= height0
;
1988 surface
->dcc_incompatible
=
1989 texture
->target
!= PIPE_BUFFER
&&
1990 vi_dcc_formats_are_incompatible(texture
, templ
->u
.tex
.level
,
1992 return &surface
->base
;
1995 static struct pipe_surface
*r600_create_surface(struct pipe_context
*pipe
,
1996 struct pipe_resource
*tex
,
1997 const struct pipe_surface
*templ
)
1999 unsigned level
= templ
->u
.tex
.level
;
2000 unsigned width
= u_minify(tex
->width0
, level
);
2001 unsigned height
= u_minify(tex
->height0
, level
);
2002 unsigned width0
= tex
->width0
;
2003 unsigned height0
= tex
->height0
;
2005 if (tex
->target
!= PIPE_BUFFER
&& templ
->format
!= tex
->format
) {
2006 const struct util_format_description
*tex_desc
2007 = util_format_description(tex
->format
);
2008 const struct util_format_description
*templ_desc
2009 = util_format_description(templ
->format
);
2011 assert(tex_desc
->block
.bits
== templ_desc
->block
.bits
);
2013 /* Adjust size of surface if and only if the block width or
2014 * height is changed. */
2015 if (tex_desc
->block
.width
!= templ_desc
->block
.width
||
2016 tex_desc
->block
.height
!= templ_desc
->block
.height
) {
2017 unsigned nblks_x
= util_format_get_nblocksx(tex
->format
, width
);
2018 unsigned nblks_y
= util_format_get_nblocksy(tex
->format
, height
);
2020 width
= nblks_x
* templ_desc
->block
.width
;
2021 height
= nblks_y
* templ_desc
->block
.height
;
2023 width0
= util_format_get_nblocksx(tex
->format
, width0
);
2024 height0
= util_format_get_nblocksy(tex
->format
, height0
);
2028 return si_create_surface_custom(pipe
, tex
, templ
,
2033 static void r600_surface_destroy(struct pipe_context
*pipe
,
2034 struct pipe_surface
*surface
)
2036 pipe_resource_reference(&surface
->texture
, NULL
);
2040 unsigned si_translate_colorswap(enum pipe_format format
, bool do_endian_swap
)
2042 const struct util_format_description
*desc
= util_format_description(format
);
2044 #define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == PIPE_SWIZZLE_##swz)
2046 if (format
== PIPE_FORMAT_R11G11B10_FLOAT
) /* isn't plain */
2047 return V_028C70_SWAP_STD
;
2049 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
2052 switch (desc
->nr_channels
) {
2054 if (HAS_SWIZZLE(0,X
))
2055 return V_028C70_SWAP_STD
; /* X___ */
2056 else if (HAS_SWIZZLE(3,X
))
2057 return V_028C70_SWAP_ALT_REV
; /* ___X */
2060 if ((HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(1,Y
)) ||
2061 (HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(1,NONE
)) ||
2062 (HAS_SWIZZLE(0,NONE
) && HAS_SWIZZLE(1,Y
)))
2063 return V_028C70_SWAP_STD
; /* XY__ */
2064 else if ((HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(1,X
)) ||
2065 (HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(1,NONE
)) ||
2066 (HAS_SWIZZLE(0,NONE
) && HAS_SWIZZLE(1,X
)))
2068 return (do_endian_swap
? V_028C70_SWAP_STD
: V_028C70_SWAP_STD_REV
);
2069 else if (HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(3,Y
))
2070 return V_028C70_SWAP_ALT
; /* X__Y */
2071 else if (HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(3,X
))
2072 return V_028C70_SWAP_ALT_REV
; /* Y__X */
2075 if (HAS_SWIZZLE(0,X
))
2076 return (do_endian_swap
? V_028C70_SWAP_STD_REV
: V_028C70_SWAP_STD
);
2077 else if (HAS_SWIZZLE(0,Z
))
2078 return V_028C70_SWAP_STD_REV
; /* ZYX */
2081 /* check the middle channels, the 1st and 4th channel can be NONE */
2082 if (HAS_SWIZZLE(1,Y
) && HAS_SWIZZLE(2,Z
)) {
2083 return V_028C70_SWAP_STD
; /* XYZW */
2084 } else if (HAS_SWIZZLE(1,Z
) && HAS_SWIZZLE(2,Y
)) {
2085 return V_028C70_SWAP_STD_REV
; /* WZYX */
2086 } else if (HAS_SWIZZLE(1,Y
) && HAS_SWIZZLE(2,X
)) {
2087 return V_028C70_SWAP_ALT
; /* ZYXW */
2088 } else if (HAS_SWIZZLE(1,Z
) && HAS_SWIZZLE(2,W
)) {
2091 return V_028C70_SWAP_ALT_REV
;
2093 return (do_endian_swap
? V_028C70_SWAP_ALT
: V_028C70_SWAP_ALT_REV
);
2100 /* PIPELINE_STAT-BASED DCC ENABLEMENT FOR DISPLAYABLE SURFACES */
2102 static void vi_dcc_clean_up_context_slot(struct r600_common_context
*rctx
,
2107 if (rctx
->dcc_stats
[slot
].query_active
)
2108 vi_separate_dcc_stop_query(&rctx
->b
,
2109 rctx
->dcc_stats
[slot
].tex
);
2111 for (i
= 0; i
< ARRAY_SIZE(rctx
->dcc_stats
[slot
].ps_stats
); i
++)
2112 if (rctx
->dcc_stats
[slot
].ps_stats
[i
]) {
2113 rctx
->b
.destroy_query(&rctx
->b
,
2114 rctx
->dcc_stats
[slot
].ps_stats
[i
]);
2115 rctx
->dcc_stats
[slot
].ps_stats
[i
] = NULL
;
2118 r600_texture_reference(&rctx
->dcc_stats
[slot
].tex
, NULL
);
2122 * Return the per-context slot where DCC statistics queries for the texture live.
2124 static unsigned vi_get_context_dcc_stats_index(struct r600_common_context
*rctx
,
2125 struct r600_texture
*tex
)
2127 int i
, empty_slot
= -1;
2129 /* Remove zombie textures (textures kept alive by this array only). */
2130 for (i
= 0; i
< ARRAY_SIZE(rctx
->dcc_stats
); i
++)
2131 if (rctx
->dcc_stats
[i
].tex
&&
2132 rctx
->dcc_stats
[i
].tex
->resource
.b
.b
.reference
.count
== 1)
2133 vi_dcc_clean_up_context_slot(rctx
, i
);
2135 /* Find the texture. */
2136 for (i
= 0; i
< ARRAY_SIZE(rctx
->dcc_stats
); i
++) {
2137 /* Return if found. */
2138 if (rctx
->dcc_stats
[i
].tex
== tex
) {
2139 rctx
->dcc_stats
[i
].last_use_timestamp
= os_time_get();
2143 /* Record the first seen empty slot. */
2144 if (empty_slot
== -1 && !rctx
->dcc_stats
[i
].tex
)
2148 /* Not found. Remove the oldest member to make space in the array. */
2149 if (empty_slot
== -1) {
2150 int oldest_slot
= 0;
2152 /* Find the oldest slot. */
2153 for (i
= 1; i
< ARRAY_SIZE(rctx
->dcc_stats
); i
++)
2154 if (rctx
->dcc_stats
[oldest_slot
].last_use_timestamp
>
2155 rctx
->dcc_stats
[i
].last_use_timestamp
)
2158 /* Clean up the oldest slot. */
2159 vi_dcc_clean_up_context_slot(rctx
, oldest_slot
);
2160 empty_slot
= oldest_slot
;
2163 /* Add the texture to the new slot. */
2164 r600_texture_reference(&rctx
->dcc_stats
[empty_slot
].tex
, tex
);
2165 rctx
->dcc_stats
[empty_slot
].last_use_timestamp
= os_time_get();
2169 static struct pipe_query
*
2170 vi_create_resuming_pipestats_query(struct pipe_context
*ctx
)
2172 struct r600_query_hw
*query
= (struct r600_query_hw
*)
2173 ctx
->create_query(ctx
, PIPE_QUERY_PIPELINE_STATISTICS
, 0);
2175 query
->flags
|= R600_QUERY_HW_FLAG_BEGIN_RESUMES
;
2176 return (struct pipe_query
*)query
;
2180 * Called when binding a color buffer.
2182 void vi_separate_dcc_start_query(struct pipe_context
*ctx
,
2183 struct r600_texture
*tex
)
2185 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
2186 unsigned i
= vi_get_context_dcc_stats_index(rctx
, tex
);
2188 assert(!rctx
->dcc_stats
[i
].query_active
);
2190 if (!rctx
->dcc_stats
[i
].ps_stats
[0])
2191 rctx
->dcc_stats
[i
].ps_stats
[0] = vi_create_resuming_pipestats_query(ctx
);
2193 /* begin or resume the query */
2194 ctx
->begin_query(ctx
, rctx
->dcc_stats
[i
].ps_stats
[0]);
2195 rctx
->dcc_stats
[i
].query_active
= true;
2199 * Called when unbinding a color buffer.
2201 void vi_separate_dcc_stop_query(struct pipe_context
*ctx
,
2202 struct r600_texture
*tex
)
2204 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
2205 unsigned i
= vi_get_context_dcc_stats_index(rctx
, tex
);
2207 assert(rctx
->dcc_stats
[i
].query_active
);
2208 assert(rctx
->dcc_stats
[i
].ps_stats
[0]);
2210 /* pause or end the query */
2211 ctx
->end_query(ctx
, rctx
->dcc_stats
[i
].ps_stats
[0]);
2212 rctx
->dcc_stats
[i
].query_active
= false;
2215 static bool vi_should_enable_separate_dcc(struct r600_texture
*tex
)
2217 /* The minimum number of fullscreen draws per frame that is required
2219 return tex
->ps_draw_ratio
+ tex
->num_slow_clears
>= 5;
2222 /* Called by fast clear. */
2223 void vi_separate_dcc_try_enable(struct r600_common_context
*rctx
,
2224 struct r600_texture
*tex
)
2226 /* The intent is to use this with shared displayable back buffers,
2227 * but it's not strictly limited only to them.
2229 if (!tex
->resource
.b
.is_shared
||
2230 !(tex
->resource
.external_usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
) ||
2231 tex
->resource
.b
.b
.target
!= PIPE_TEXTURE_2D
||
2232 tex
->resource
.b
.b
.last_level
> 0 ||
2233 !tex
->surface
.dcc_size
)
2236 if (tex
->dcc_offset
)
2237 return; /* already enabled */
2239 /* Enable the DCC stat gathering. */
2240 if (!tex
->dcc_gather_statistics
) {
2241 tex
->dcc_gather_statistics
= true;
2242 vi_separate_dcc_start_query(&rctx
->b
, tex
);
2245 if (!vi_should_enable_separate_dcc(tex
))
2246 return; /* stats show that DCC decompression is too expensive */
2248 assert(tex
->surface
.num_dcc_levels
);
2249 assert(!tex
->dcc_separate_buffer
);
2251 r600_texture_discard_cmask(rctx
->screen
, tex
);
2253 /* Get a DCC buffer. */
2254 if (tex
->last_dcc_separate_buffer
) {
2255 assert(tex
->dcc_gather_statistics
);
2256 assert(!tex
->dcc_separate_buffer
);
2257 tex
->dcc_separate_buffer
= tex
->last_dcc_separate_buffer
;
2258 tex
->last_dcc_separate_buffer
= NULL
;
2260 tex
->dcc_separate_buffer
= (struct r600_resource
*)
2261 si_aligned_buffer_create(rctx
->b
.screen
,
2262 R600_RESOURCE_FLAG_UNMAPPABLE
,
2264 tex
->surface
.dcc_size
,
2265 tex
->surface
.dcc_alignment
);
2266 if (!tex
->dcc_separate_buffer
)
2270 /* dcc_offset is the absolute GPUVM address. */
2271 tex
->dcc_offset
= tex
->dcc_separate_buffer
->gpu_address
;
2273 /* no need to flag anything since this is called by fast clear that
2274 * flags framebuffer state
2279 * Called by pipe_context::flush_resource, the place where DCC decompression
2282 void vi_separate_dcc_process_and_reset_stats(struct pipe_context
*ctx
,
2283 struct r600_texture
*tex
)
2285 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
2286 struct pipe_query
*tmp
;
2287 unsigned i
= vi_get_context_dcc_stats_index(rctx
, tex
);
2288 bool query_active
= rctx
->dcc_stats
[i
].query_active
;
2289 bool disable
= false;
2291 if (rctx
->dcc_stats
[i
].ps_stats
[2]) {
2292 union pipe_query_result result
;
2294 /* Read the results. */
2295 ctx
->get_query_result(ctx
, rctx
->dcc_stats
[i
].ps_stats
[2],
2297 si_query_hw_reset_buffers(rctx
,
2298 (struct r600_query_hw
*)
2299 rctx
->dcc_stats
[i
].ps_stats
[2]);
2301 /* Compute the approximate number of fullscreen draws. */
2302 tex
->ps_draw_ratio
=
2303 result
.pipeline_statistics
.ps_invocations
/
2304 (tex
->resource
.b
.b
.width0
* tex
->resource
.b
.b
.height0
);
2305 rctx
->last_tex_ps_draw_ratio
= tex
->ps_draw_ratio
;
2307 disable
= tex
->dcc_separate_buffer
&&
2308 !vi_should_enable_separate_dcc(tex
);
2311 tex
->num_slow_clears
= 0;
2313 /* stop the statistics query for ps_stats[0] */
2315 vi_separate_dcc_stop_query(ctx
, tex
);
2317 /* Move the queries in the queue by one. */
2318 tmp
= rctx
->dcc_stats
[i
].ps_stats
[2];
2319 rctx
->dcc_stats
[i
].ps_stats
[2] = rctx
->dcc_stats
[i
].ps_stats
[1];
2320 rctx
->dcc_stats
[i
].ps_stats
[1] = rctx
->dcc_stats
[i
].ps_stats
[0];
2321 rctx
->dcc_stats
[i
].ps_stats
[0] = tmp
;
2323 /* create and start a new query as ps_stats[0] */
2325 vi_separate_dcc_start_query(ctx
, tex
);
2328 assert(!tex
->last_dcc_separate_buffer
);
2329 tex
->last_dcc_separate_buffer
= tex
->dcc_separate_buffer
;
2330 tex
->dcc_separate_buffer
= NULL
;
2331 tex
->dcc_offset
= 0;
2332 /* no need to flag anything since this is called after
2333 * decompression that re-sets framebuffer state
2338 static struct pipe_memory_object
*
2339 r600_memobj_from_handle(struct pipe_screen
*screen
,
2340 struct winsys_handle
*whandle
,
2343 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)screen
;
2344 struct r600_memory_object
*memobj
= CALLOC_STRUCT(r600_memory_object
);
2345 struct pb_buffer
*buf
= NULL
;
2346 uint32_t stride
, offset
;
2351 buf
= rscreen
->ws
->buffer_from_handle(rscreen
->ws
, whandle
,
2358 memobj
->b
.dedicated
= dedicated
;
2360 memobj
->stride
= stride
;
2361 memobj
->offset
= offset
;
2363 return (struct pipe_memory_object
*)memobj
;
2368 r600_memobj_destroy(struct pipe_screen
*screen
,
2369 struct pipe_memory_object
*_memobj
)
2371 struct r600_memory_object
*memobj
= (struct r600_memory_object
*)_memobj
;
2373 pb_reference(&memobj
->buf
, NULL
);
2377 static struct pipe_resource
*
2378 r600_texture_from_memobj(struct pipe_screen
*screen
,
2379 const struct pipe_resource
*templ
,
2380 struct pipe_memory_object
*_memobj
,
2384 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)screen
;
2385 struct r600_memory_object
*memobj
= (struct r600_memory_object
*)_memobj
;
2386 struct r600_texture
*rtex
;
2387 struct radeon_surf surface
= {};
2388 struct radeon_bo_metadata metadata
= {};
2389 enum radeon_surf_mode array_mode
;
2391 struct pb_buffer
*buf
= NULL
;
2393 if (memobj
->b
.dedicated
) {
2394 rscreen
->ws
->buffer_get_metadata(memobj
->buf
, &metadata
);
2395 r600_surface_import_metadata(rscreen
, &surface
, &metadata
,
2396 &array_mode
, &is_scanout
);
2399 * The bo metadata is unset for un-dedicated images. So we fall
2400 * back to linear. See answer to question 5 of the
2401 * VK_KHX_external_memory spec for some details.
2403 * It is possible that this case isn't going to work if the
2404 * surface pitch isn't correctly aligned by default.
2406 * In order to support it correctly we require multi-image
2407 * metadata to be syncrhonized between radv and radeonsi. The
2408 * semantics of associating multiple image metadata to a memory
2409 * object on the vulkan export side are not concretely defined
2412 * All the use cases we are aware of at the moment for memory
2413 * objects use dedicated allocations. So lets keep the initial
2414 * implementation simple.
2416 * A possible alternative is to attempt to reconstruct the
2417 * tiling information when the TexParameter TEXTURE_TILING_EXT
2420 array_mode
= RADEON_SURF_MODE_LINEAR_ALIGNED
;
2425 r
= r600_init_surface(rscreen
, &surface
, templ
,
2426 array_mode
, memobj
->stride
,
2427 offset
, true, is_scanout
,
2432 rtex
= r600_texture_create_object(screen
, templ
, memobj
->buf
, &surface
);
2436 /* r600_texture_create_object doesn't increment refcount of
2437 * memobj->buf, so increment it here.
2439 pb_reference(&buf
, memobj
->buf
);
2441 rtex
->resource
.b
.is_shared
= true;
2442 rtex
->resource
.external_usage
= PIPE_HANDLE_USAGE_READ_WRITE
;
2444 si_apply_opaque_metadata(rscreen
, rtex
, &metadata
);
2446 return &rtex
->resource
.b
.b
;
2449 static bool si_check_resource_capability(struct pipe_screen
*screen
,
2450 struct pipe_resource
*resource
,
2453 struct r600_texture
*tex
= (struct r600_texture
*)resource
;
2455 /* Buffers only support the linear flag. */
2456 if (resource
->target
== PIPE_BUFFER
)
2457 return (bind
& ~PIPE_BIND_LINEAR
) == 0;
2459 if (bind
& PIPE_BIND_LINEAR
&& !tex
->surface
.is_linear
)
2462 if (bind
& PIPE_BIND_SCANOUT
&& !tex
->surface
.is_displayable
)
2465 /* TODO: PIPE_BIND_CURSOR - do we care? */
2469 void si_init_screen_texture_functions(struct r600_common_screen
*rscreen
)
2471 rscreen
->b
.resource_from_handle
= r600_texture_from_handle
;
2472 rscreen
->b
.resource_get_handle
= r600_texture_get_handle
;
2473 rscreen
->b
.resource_from_memobj
= r600_texture_from_memobj
;
2474 rscreen
->b
.memobj_create_from_handle
= r600_memobj_from_handle
;
2475 rscreen
->b
.memobj_destroy
= r600_memobj_destroy
;
2476 rscreen
->b
.check_resource_capability
= si_check_resource_capability
;
2479 void si_init_context_texture_functions(struct r600_common_context
*rctx
)
2481 rctx
->b
.create_surface
= r600_create_surface
;
2482 rctx
->b
.surface_destroy
= r600_surface_destroy
;