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.
27 #include "r600_pipe_common.h"
29 #include "r600_query.h"
30 #include "util/u_format.h"
31 #include "util/u_memory.h"
32 #include "util/u_pack_color.h"
33 #include "util/u_surface.h"
34 #include "os/os_time.h"
38 static void r600_texture_discard_cmask(struct r600_common_screen
*rscreen
,
39 struct r600_texture
*rtex
);
40 static enum radeon_surf_mode
41 r600_choose_tiling(struct r600_common_screen
*rscreen
,
42 const struct pipe_resource
*templ
);
45 bool r600_prepare_for_dma_blit(struct r600_common_context
*rctx
,
46 struct r600_texture
*rdst
,
47 unsigned dst_level
, unsigned dstx
,
48 unsigned dsty
, unsigned dstz
,
49 struct r600_texture
*rsrc
,
51 const struct pipe_box
*src_box
)
56 if (rdst
->surface
.bpe
!= rsrc
->surface
.bpe
)
59 /* MSAA: Blits don't exist in the real world. */
60 if (rsrc
->resource
.b
.b
.nr_samples
> 1 ||
61 rdst
->resource
.b
.b
.nr_samples
> 1)
64 /* Depth-stencil surfaces:
65 * When dst is linear, the DB->CB copy preserves HTILE.
66 * When dst is tiled, the 3D path must be used to update HTILE.
68 if (rsrc
->is_depth
|| rdst
->is_depth
)
72 * src: Use the 3D path. DCC decompression is expensive.
73 * dst: Use the 3D path to compress the pixels with DCC.
75 if (vi_dcc_enabled(rsrc
, src_level
) ||
76 vi_dcc_enabled(rdst
, dst_level
))
80 * src: Both texture and SDMA paths need decompression. Use SDMA.
81 * dst: If overwriting the whole texture, discard CMASK and use
82 * SDMA. Otherwise, use the 3D path.
84 if (rdst
->cmask
.size
&& rdst
->dirty_level_mask
& (1 << dst_level
)) {
85 /* The CMASK clear is only enabled for the first level. */
86 assert(dst_level
== 0);
87 if (!util_texrange_covers_whole_level(&rdst
->resource
.b
.b
, dst_level
,
88 dstx
, dsty
, dstz
, src_box
->width
,
89 src_box
->height
, src_box
->depth
))
92 r600_texture_discard_cmask(rctx
->screen
, rdst
);
95 /* All requirements are met. Prepare textures for SDMA. */
96 if (rsrc
->cmask
.size
&& rsrc
->dirty_level_mask
& (1 << src_level
))
97 rctx
->b
.flush_resource(&rctx
->b
, &rsrc
->resource
.b
.b
);
99 assert(!(rsrc
->dirty_level_mask
& (1 << src_level
)));
100 assert(!(rdst
->dirty_level_mask
& (1 << dst_level
)));
105 /* Same as resource_copy_region, except that both upsampling and downsampling are allowed. */
106 static void r600_copy_region_with_blit(struct pipe_context
*pipe
,
107 struct pipe_resource
*dst
,
109 unsigned dstx
, unsigned dsty
, unsigned dstz
,
110 struct pipe_resource
*src
,
112 const struct pipe_box
*src_box
)
114 struct pipe_blit_info blit
;
116 memset(&blit
, 0, sizeof(blit
));
117 blit
.src
.resource
= src
;
118 blit
.src
.format
= src
->format
;
119 blit
.src
.level
= src_level
;
120 blit
.src
.box
= *src_box
;
121 blit
.dst
.resource
= dst
;
122 blit
.dst
.format
= dst
->format
;
123 blit
.dst
.level
= dst_level
;
124 blit
.dst
.box
.x
= dstx
;
125 blit
.dst
.box
.y
= dsty
;
126 blit
.dst
.box
.z
= dstz
;
127 blit
.dst
.box
.width
= src_box
->width
;
128 blit
.dst
.box
.height
= src_box
->height
;
129 blit
.dst
.box
.depth
= src_box
->depth
;
130 blit
.mask
= util_format_get_mask(src
->format
) &
131 util_format_get_mask(dst
->format
);
132 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
135 pipe
->blit(pipe
, &blit
);
139 /* Copy from a full GPU texture to a transfer's staging one. */
140 static void r600_copy_to_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
142 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
143 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
144 struct pipe_resource
*dst
= &rtransfer
->staging
->b
.b
;
145 struct pipe_resource
*src
= transfer
->resource
;
147 if (src
->nr_samples
> 1) {
148 r600_copy_region_with_blit(ctx
, dst
, 0, 0, 0, 0,
149 src
, transfer
->level
, &transfer
->box
);
153 rctx
->dma_copy(ctx
, dst
, 0, 0, 0, 0, src
, transfer
->level
,
157 /* Copy from a transfer's staging texture to a full GPU one. */
158 static void r600_copy_from_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
160 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
161 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
162 struct pipe_resource
*dst
= transfer
->resource
;
163 struct pipe_resource
*src
= &rtransfer
->staging
->b
.b
;
164 struct pipe_box sbox
;
166 u_box_3d(0, 0, 0, transfer
->box
.width
, transfer
->box
.height
, transfer
->box
.depth
, &sbox
);
168 if (dst
->nr_samples
> 1) {
169 r600_copy_region_with_blit(ctx
, dst
, transfer
->level
,
170 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
175 rctx
->dma_copy(ctx
, dst
, transfer
->level
,
176 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
180 static unsigned r600_texture_get_offset(struct r600_common_screen
*rscreen
,
181 struct r600_texture
*rtex
, unsigned level
,
182 const struct pipe_box
*box
,
184 unsigned *layer_stride
)
186 if (rscreen
->chip_class
>= GFX9
) {
187 *stride
= rtex
->surface
.u
.gfx9
.surf_pitch
* rtex
->surface
.bpe
;
188 *layer_stride
= rtex
->surface
.u
.gfx9
.surf_slice_size
;
193 /* Each texture is an array of slices. Each slice is an array
194 * of mipmap levels. */
195 return box
->z
* rtex
->surface
.u
.gfx9
.surf_slice_size
+
196 rtex
->surface
.u
.gfx9
.offset
[level
] +
197 (box
->y
/ rtex
->surface
.blk_h
*
198 rtex
->surface
.u
.gfx9
.surf_pitch
+
199 box
->x
/ rtex
->surface
.blk_w
) * rtex
->surface
.bpe
;
201 *stride
= rtex
->surface
.u
.legacy
.level
[level
].nblk_x
*
203 *layer_stride
= rtex
->surface
.u
.legacy
.level
[level
].slice_size
;
206 return rtex
->surface
.u
.legacy
.level
[level
].offset
;
208 /* Each texture is an array of mipmap levels. Each level is
209 * an array of slices. */
210 return rtex
->surface
.u
.legacy
.level
[level
].offset
+
211 box
->z
* rtex
->surface
.u
.legacy
.level
[level
].slice_size
+
212 (box
->y
/ rtex
->surface
.blk_h
*
213 rtex
->surface
.u
.legacy
.level
[level
].nblk_x
+
214 box
->x
/ rtex
->surface
.blk_w
) * rtex
->surface
.bpe
;
218 static int r600_init_surface(struct r600_common_screen
*rscreen
,
219 struct radeon_surf
*surface
,
220 const struct pipe_resource
*ptex
,
221 enum radeon_surf_mode array_mode
,
222 unsigned pitch_in_bytes_override
,
226 bool is_flushed_depth
,
227 bool tc_compatible_htile
)
229 const struct util_format_description
*desc
=
230 util_format_description(ptex
->format
);
231 bool is_depth
, is_stencil
;
233 unsigned i
, bpe
, flags
= 0;
235 is_depth
= util_format_has_depth(desc
);
236 is_stencil
= util_format_has_stencil(desc
);
238 if (rscreen
->chip_class
>= EVERGREEN
&& !is_flushed_depth
&&
239 ptex
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
) {
240 bpe
= 4; /* stencil is allocated separately on evergreen */
242 bpe
= util_format_get_blocksize(ptex
->format
);
243 assert(util_is_power_of_two(bpe
));
246 if (!is_flushed_depth
&& is_depth
) {
247 flags
|= RADEON_SURF_ZBUFFER
;
249 if (tc_compatible_htile
&&
250 (rscreen
->chip_class
>= GFX9
||
251 array_mode
== RADEON_SURF_MODE_2D
)) {
252 /* TC-compatible HTILE only supports Z32_FLOAT.
253 * GFX9 also supports Z16_UNORM.
254 * On VI, promote Z16 to Z32. DB->CB copies will convert
255 * the format for transfers.
257 if (rscreen
->chip_class
== VI
)
260 flags
|= RADEON_SURF_TC_COMPATIBLE_HTILE
;
264 flags
|= RADEON_SURF_SBUFFER
;
267 if (rscreen
->chip_class
>= VI
&&
268 (ptex
->flags
& R600_RESOURCE_FLAG_DISABLE_DCC
||
269 ptex
->format
== PIPE_FORMAT_R9G9B9E5_FLOAT
))
270 flags
|= RADEON_SURF_DISABLE_DCC
;
272 if (ptex
->bind
& PIPE_BIND_SCANOUT
|| is_scanout
) {
273 /* This should catch bugs in gallium users setting incorrect flags. */
274 assert(ptex
->nr_samples
<= 1 &&
275 ptex
->array_size
== 1 &&
277 ptex
->last_level
== 0 &&
278 !(flags
& RADEON_SURF_Z_OR_SBUFFER
));
280 flags
|= RADEON_SURF_SCANOUT
;
284 flags
|= RADEON_SURF_IMPORTED
;
285 if (!(ptex
->flags
& R600_RESOURCE_FLAG_FORCE_TILING
))
286 flags
|= RADEON_SURF_OPTIMIZE_FOR_SPACE
;
288 r
= rscreen
->ws
->surface_init(rscreen
->ws
, ptex
, flags
, bpe
,
289 array_mode
, surface
);
294 if (rscreen
->chip_class
>= GFX9
) {
295 assert(!pitch_in_bytes_override
||
296 pitch_in_bytes_override
== surface
->u
.gfx9
.surf_pitch
* bpe
);
297 surface
->u
.gfx9
.surf_offset
= offset
;
299 if (pitch_in_bytes_override
&&
300 pitch_in_bytes_override
!= surface
->u
.legacy
.level
[0].nblk_x
* bpe
) {
301 /* old ddx on evergreen over estimate alignment for 1d, only 1 level
304 surface
->u
.legacy
.level
[0].nblk_x
= pitch_in_bytes_override
/ bpe
;
305 surface
->u
.legacy
.level
[0].slice_size
= pitch_in_bytes_override
*
306 surface
->u
.legacy
.level
[0].nblk_y
;
310 for (i
= 0; i
< ARRAY_SIZE(surface
->u
.legacy
.level
); ++i
)
311 surface
->u
.legacy
.level
[i
].offset
+= offset
;
317 static void r600_texture_init_metadata(struct r600_common_screen
*rscreen
,
318 struct r600_texture
*rtex
,
319 struct radeon_bo_metadata
*metadata
)
321 struct radeon_surf
*surface
= &rtex
->surface
;
323 memset(metadata
, 0, sizeof(*metadata
));
325 if (rscreen
->chip_class
>= GFX9
) {
326 metadata
->u
.gfx9
.swizzle_mode
= surface
->u
.gfx9
.surf
.swizzle_mode
;
328 metadata
->u
.legacy
.microtile
= surface
->u
.legacy
.level
[0].mode
>= RADEON_SURF_MODE_1D
?
329 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
;
330 metadata
->u
.legacy
.macrotile
= surface
->u
.legacy
.level
[0].mode
>= RADEON_SURF_MODE_2D
?
331 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
;
332 metadata
->u
.legacy
.pipe_config
= surface
->u
.legacy
.pipe_config
;
333 metadata
->u
.legacy
.bankw
= surface
->u
.legacy
.bankw
;
334 metadata
->u
.legacy
.bankh
= surface
->u
.legacy
.bankh
;
335 metadata
->u
.legacy
.tile_split
= surface
->u
.legacy
.tile_split
;
336 metadata
->u
.legacy
.mtilea
= surface
->u
.legacy
.mtilea
;
337 metadata
->u
.legacy
.num_banks
= surface
->u
.legacy
.num_banks
;
338 metadata
->u
.legacy
.stride
= surface
->u
.legacy
.level
[0].nblk_x
* surface
->bpe
;
339 metadata
->u
.legacy
.scanout
= (surface
->flags
& RADEON_SURF_SCANOUT
) != 0;
343 static void r600_eliminate_fast_color_clear(struct r600_common_context
*rctx
,
344 struct r600_texture
*rtex
)
346 struct r600_common_screen
*rscreen
= rctx
->screen
;
347 struct pipe_context
*ctx
= &rctx
->b
;
349 if (ctx
== rscreen
->aux_context
)
350 mtx_lock(&rscreen
->aux_context_lock
);
352 ctx
->flush_resource(ctx
, &rtex
->resource
.b
.b
);
353 ctx
->flush(ctx
, NULL
, 0);
355 if (ctx
== rscreen
->aux_context
)
356 mtx_unlock(&rscreen
->aux_context_lock
);
359 static void r600_texture_discard_cmask(struct r600_common_screen
*rscreen
,
360 struct r600_texture
*rtex
)
362 if (!rtex
->cmask
.size
)
365 assert(rtex
->resource
.b
.b
.nr_samples
<= 1);
368 memset(&rtex
->cmask
, 0, sizeof(rtex
->cmask
));
369 rtex
->cmask
.base_address_reg
= rtex
->resource
.gpu_address
>> 8;
370 rtex
->dirty_level_mask
= 0;
372 if (rscreen
->chip_class
>= SI
)
373 rtex
->cb_color_info
&= ~SI_S_028C70_FAST_CLEAR(1);
375 rtex
->cb_color_info
&= ~EG_S_028C70_FAST_CLEAR(1);
377 if (rtex
->cmask_buffer
!= &rtex
->resource
)
378 r600_resource_reference(&rtex
->cmask_buffer
, NULL
);
380 /* Notify all contexts about the change. */
381 p_atomic_inc(&rscreen
->dirty_tex_counter
);
382 p_atomic_inc(&rscreen
->compressed_colortex_counter
);
385 static bool r600_can_disable_dcc(struct r600_texture
*rtex
)
387 /* We can't disable DCC if it can be written by another process. */
388 return rtex
->dcc_offset
&&
389 (!rtex
->resource
.b
.is_shared
||
390 !(rtex
->resource
.external_usage
& PIPE_HANDLE_USAGE_WRITE
));
393 static bool r600_texture_discard_dcc(struct r600_common_screen
*rscreen
,
394 struct r600_texture
*rtex
)
396 if (!r600_can_disable_dcc(rtex
))
399 assert(rtex
->dcc_separate_buffer
== NULL
);
402 rtex
->dcc_offset
= 0;
404 /* Notify all contexts about the change. */
405 p_atomic_inc(&rscreen
->dirty_tex_counter
);
410 * Disable DCC for the texture. (first decompress, then discard metadata).
412 * There is unresolved multi-context synchronization issue between
413 * screen::aux_context and the current context. If applications do this with
414 * multiple contexts, it's already undefined behavior for them and we don't
415 * have to worry about that. The scenario is:
417 * If context 1 disables DCC and context 2 has queued commands that write
418 * to the texture via CB with DCC enabled, and the order of operations is
420 * context 2 queues draw calls rendering to the texture, but doesn't flush
421 * context 1 disables DCC and flushes
422 * context 1 & 2 reset descriptors and FB state
423 * context 2 flushes (new compressed tiles written by the draw calls)
424 * context 1 & 2 read garbage, because DCC is disabled, yet there are
427 * \param rctx the current context if you have one, or rscreen->aux_context
430 bool r600_texture_disable_dcc(struct r600_common_context
*rctx
,
431 struct r600_texture
*rtex
)
433 struct r600_common_screen
*rscreen
= rctx
->screen
;
435 if (!r600_can_disable_dcc(rtex
))
438 if (&rctx
->b
== rscreen
->aux_context
)
439 mtx_lock(&rscreen
->aux_context_lock
);
441 /* Decompress DCC. */
442 rctx
->decompress_dcc(&rctx
->b
, rtex
);
443 rctx
->b
.flush(&rctx
->b
, NULL
, 0);
445 if (&rctx
->b
== rscreen
->aux_context
)
446 mtx_unlock(&rscreen
->aux_context_lock
);
448 return r600_texture_discard_dcc(rscreen
, rtex
);
451 static void r600_reallocate_texture_inplace(struct r600_common_context
*rctx
,
452 struct r600_texture
*rtex
,
453 unsigned new_bind_flag
,
454 bool invalidate_storage
)
456 struct pipe_screen
*screen
= rctx
->b
.screen
;
457 struct r600_texture
*new_tex
;
458 struct pipe_resource templ
= rtex
->resource
.b
.b
;
461 templ
.bind
|= new_bind_flag
;
463 /* r600g doesn't react to dirty_tex_descriptor_counter */
464 if (rctx
->chip_class
< SI
)
467 if (rtex
->resource
.b
.is_shared
)
470 if (new_bind_flag
== PIPE_BIND_LINEAR
) {
471 if (rtex
->surface
.is_linear
)
474 /* This fails with MSAA, depth, and compressed textures. */
475 if (r600_choose_tiling(rctx
->screen
, &templ
) !=
476 RADEON_SURF_MODE_LINEAR_ALIGNED
)
480 new_tex
= (struct r600_texture
*)screen
->resource_create(screen
, &templ
);
484 /* Copy the pixels to the new texture. */
485 if (!invalidate_storage
) {
486 for (i
= 0; i
<= templ
.last_level
; i
++) {
490 u_minify(templ
.width0
, i
), u_minify(templ
.height0
, i
),
491 util_max_layer(&templ
, i
) + 1, &box
);
493 rctx
->dma_copy(&rctx
->b
, &new_tex
->resource
.b
.b
, i
, 0, 0, 0,
494 &rtex
->resource
.b
.b
, i
, &box
);
498 if (new_bind_flag
== PIPE_BIND_LINEAR
) {
499 r600_texture_discard_cmask(rctx
->screen
, rtex
);
500 r600_texture_discard_dcc(rctx
->screen
, rtex
);
503 /* Replace the structure fields of rtex. */
504 rtex
->resource
.b
.b
.bind
= templ
.bind
;
505 pb_reference(&rtex
->resource
.buf
, new_tex
->resource
.buf
);
506 rtex
->resource
.gpu_address
= new_tex
->resource
.gpu_address
;
507 rtex
->resource
.vram_usage
= new_tex
->resource
.vram_usage
;
508 rtex
->resource
.gart_usage
= new_tex
->resource
.gart_usage
;
509 rtex
->resource
.bo_size
= new_tex
->resource
.bo_size
;
510 rtex
->resource
.bo_alignment
= new_tex
->resource
.bo_alignment
;
511 rtex
->resource
.domains
= new_tex
->resource
.domains
;
512 rtex
->resource
.flags
= new_tex
->resource
.flags
;
513 rtex
->size
= new_tex
->size
;
514 rtex
->db_render_format
= new_tex
->db_render_format
;
515 rtex
->db_compatible
= new_tex
->db_compatible
;
516 rtex
->can_sample_z
= new_tex
->can_sample_z
;
517 rtex
->can_sample_s
= new_tex
->can_sample_s
;
518 rtex
->surface
= new_tex
->surface
;
519 rtex
->fmask
= new_tex
->fmask
;
520 rtex
->cmask
= new_tex
->cmask
;
521 rtex
->cb_color_info
= new_tex
->cb_color_info
;
522 rtex
->last_msaa_resolve_target_micro_mode
= new_tex
->last_msaa_resolve_target_micro_mode
;
523 rtex
->htile_offset
= new_tex
->htile_offset
;
524 rtex
->tc_compatible_htile
= new_tex
->tc_compatible_htile
;
525 rtex
->depth_cleared
= new_tex
->depth_cleared
;
526 rtex
->stencil_cleared
= new_tex
->stencil_cleared
;
527 rtex
->non_disp_tiling
= new_tex
->non_disp_tiling
;
528 rtex
->dcc_gather_statistics
= new_tex
->dcc_gather_statistics
;
529 rtex
->framebuffers_bound
= new_tex
->framebuffers_bound
;
531 if (new_bind_flag
== PIPE_BIND_LINEAR
) {
532 assert(!rtex
->htile_offset
);
533 assert(!rtex
->cmask
.size
);
534 assert(!rtex
->fmask
.size
);
535 assert(!rtex
->dcc_offset
);
536 assert(!rtex
->is_depth
);
539 r600_texture_reference(&new_tex
, NULL
);
541 p_atomic_inc(&rctx
->screen
->dirty_tex_counter
);
544 static boolean
r600_texture_get_handle(struct pipe_screen
* screen
,
545 struct pipe_context
*ctx
,
546 struct pipe_resource
*resource
,
547 struct winsys_handle
*whandle
,
550 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)screen
;
551 struct r600_common_context
*rctx
;
552 struct r600_resource
*res
= (struct r600_resource
*)resource
;
553 struct r600_texture
*rtex
= (struct r600_texture
*)resource
;
554 struct radeon_bo_metadata metadata
;
555 bool update_metadata
= false;
556 unsigned stride
, offset
, slice_size
;
558 ctx
= threaded_context_unwrap_sync(ctx
);
559 rctx
= (struct r600_common_context
*)(ctx
? ctx
: rscreen
->aux_context
);
561 if (resource
->target
!= PIPE_BUFFER
) {
562 /* This is not supported now, but it might be required for OpenCL
563 * interop in the future.
565 if (resource
->nr_samples
> 1 || rtex
->is_depth
)
568 /* Move a suballocated texture into a non-suballocated allocation. */
569 if (rscreen
->ws
->buffer_is_suballocated(res
->buf
)) {
570 assert(!res
->b
.is_shared
);
571 r600_reallocate_texture_inplace(rctx
, rtex
,
572 PIPE_BIND_SHARED
, false);
573 rctx
->b
.flush(&rctx
->b
, NULL
, 0);
574 assert(res
->b
.b
.bind
& PIPE_BIND_SHARED
);
575 assert(res
->flags
& RADEON_FLAG_NO_SUBALLOC
);
578 /* Since shader image stores don't support DCC on VI,
579 * disable it for external clients that want write
582 if (usage
& PIPE_HANDLE_USAGE_WRITE
&& rtex
->dcc_offset
) {
583 if (r600_texture_disable_dcc(rctx
, rtex
))
584 update_metadata
= true;
587 if (!(usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
) &&
588 (rtex
->cmask
.size
|| rtex
->dcc_offset
)) {
589 /* Eliminate fast clear (both CMASK and DCC) */
590 r600_eliminate_fast_color_clear(rctx
, rtex
);
592 /* Disable CMASK if flush_resource isn't going
595 if (rtex
->cmask
.size
)
596 r600_texture_discard_cmask(rscreen
, rtex
);
600 if (!res
->b
.is_shared
|| update_metadata
) {
601 r600_texture_init_metadata(rscreen
, rtex
, &metadata
);
602 if (rscreen
->query_opaque_metadata
)
603 rscreen
->query_opaque_metadata(rscreen
, rtex
,
606 rscreen
->ws
->buffer_set_metadata(res
->buf
, &metadata
);
609 if (rscreen
->chip_class
>= GFX9
) {
610 offset
= rtex
->surface
.u
.gfx9
.surf_offset
;
611 stride
= rtex
->surface
.u
.gfx9
.surf_pitch
*
613 slice_size
= rtex
->surface
.u
.gfx9
.surf_slice_size
;
615 offset
= rtex
->surface
.u
.legacy
.level
[0].offset
;
616 stride
= rtex
->surface
.u
.legacy
.level
[0].nblk_x
*
618 slice_size
= rtex
->surface
.u
.legacy
.level
[0].slice_size
;
621 /* Move a suballocated buffer into a non-suballocated allocation. */
622 if (rscreen
->ws
->buffer_is_suballocated(res
->buf
)) {
623 assert(!res
->b
.is_shared
);
625 /* Allocate a new buffer with PIPE_BIND_SHARED. */
626 struct pipe_resource templ
= res
->b
.b
;
627 templ
.bind
|= PIPE_BIND_SHARED
;
629 struct pipe_resource
*newb
=
630 screen
->resource_create(screen
, &templ
);
634 /* Copy the old buffer contents to the new one. */
636 u_box_1d(0, newb
->width0
, &box
);
637 rctx
->b
.resource_copy_region(&rctx
->b
, newb
, 0, 0, 0, 0,
639 /* Move the new buffer storage to the old pipe_resource. */
640 r600_replace_buffer_storage(&rctx
->b
, &res
->b
.b
, newb
);
641 pipe_resource_reference(&newb
, NULL
);
643 assert(res
->b
.b
.bind
& PIPE_BIND_SHARED
);
644 assert(res
->flags
& RADEON_FLAG_NO_SUBALLOC
);
653 if (res
->b
.is_shared
) {
654 /* USAGE_EXPLICIT_FLUSH must be cleared if at least one user
657 res
->external_usage
|= usage
& ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
;
658 if (!(usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
))
659 res
->external_usage
&= ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
;
661 res
->b
.is_shared
= true;
662 res
->external_usage
= usage
;
665 return rscreen
->ws
->buffer_get_handle(res
->buf
, stride
, offset
,
666 slice_size
, whandle
);
669 static void r600_texture_destroy(struct pipe_screen
*screen
,
670 struct pipe_resource
*ptex
)
672 struct r600_texture
*rtex
= (struct r600_texture
*)ptex
;
673 struct r600_resource
*resource
= &rtex
->resource
;
675 r600_texture_reference(&rtex
->flushed_depth_texture
, NULL
);
677 if (rtex
->cmask_buffer
!= &rtex
->resource
) {
678 r600_resource_reference(&rtex
->cmask_buffer
, NULL
);
680 pb_reference(&resource
->buf
, NULL
);
681 r600_resource_reference(&rtex
->dcc_separate_buffer
, NULL
);
682 r600_resource_reference(&rtex
->last_dcc_separate_buffer
, NULL
);
686 static const struct u_resource_vtbl r600_texture_vtbl
;
688 /* The number of samples can be specified independently of the texture. */
689 void r600_texture_get_fmask_info(struct r600_common_screen
*rscreen
,
690 struct r600_texture
*rtex
,
692 struct r600_fmask_info
*out
)
694 /* FMASK is allocated like an ordinary texture. */
695 struct pipe_resource templ
= rtex
->resource
.b
.b
;
696 struct radeon_surf fmask
= {};
699 memset(out
, 0, sizeof(*out
));
701 if (rscreen
->chip_class
>= GFX9
) {
702 out
->alignment
= rtex
->surface
.u
.gfx9
.fmask_alignment
;
703 out
->size
= rtex
->surface
.u
.gfx9
.fmask_size
;
707 templ
.nr_samples
= 1;
708 flags
= rtex
->surface
.flags
| RADEON_SURF_FMASK
;
710 if (rscreen
->chip_class
<= CAYMAN
) {
711 /* Use the same parameters and tile mode. */
712 fmask
.u
.legacy
.bankw
= rtex
->surface
.u
.legacy
.bankw
;
713 fmask
.u
.legacy
.bankh
= rtex
->surface
.u
.legacy
.bankh
;
714 fmask
.u
.legacy
.mtilea
= rtex
->surface
.u
.legacy
.mtilea
;
715 fmask
.u
.legacy
.tile_split
= rtex
->surface
.u
.legacy
.tile_split
;
718 fmask
.u
.legacy
.bankh
= 4;
721 switch (nr_samples
) {
730 R600_ERR("Invalid sample count for FMASK allocation.\n");
734 /* Overallocate FMASK on R600-R700 to fix colorbuffer corruption.
735 * This can be fixed by writing a separate FMASK allocator specifically
736 * for R600-R700 asics. */
737 if (rscreen
->chip_class
<= R700
) {
741 if (rscreen
->ws
->surface_init(rscreen
->ws
, &templ
, flags
, bpe
,
742 RADEON_SURF_MODE_2D
, &fmask
)) {
743 R600_ERR("Got error in surface_init while allocating FMASK.\n");
747 assert(fmask
.u
.legacy
.level
[0].mode
== RADEON_SURF_MODE_2D
);
749 out
->slice_tile_max
= (fmask
.u
.legacy
.level
[0].nblk_x
* fmask
.u
.legacy
.level
[0].nblk_y
) / 64;
750 if (out
->slice_tile_max
)
751 out
->slice_tile_max
-= 1;
753 out
->tile_mode_index
= fmask
.u
.legacy
.tiling_index
[0];
754 out
->pitch_in_pixels
= fmask
.u
.legacy
.level
[0].nblk_x
;
755 out
->bank_height
= fmask
.u
.legacy
.bankh
;
756 out
->alignment
= MAX2(256, fmask
.surf_alignment
);
757 out
->size
= fmask
.surf_size
;
760 static void r600_texture_allocate_fmask(struct r600_common_screen
*rscreen
,
761 struct r600_texture
*rtex
)
763 r600_texture_get_fmask_info(rscreen
, rtex
,
764 rtex
->resource
.b
.b
.nr_samples
, &rtex
->fmask
);
766 rtex
->fmask
.offset
= align64(rtex
->size
, rtex
->fmask
.alignment
);
767 rtex
->size
= rtex
->fmask
.offset
+ rtex
->fmask
.size
;
770 void r600_texture_get_cmask_info(struct r600_common_screen
*rscreen
,
771 struct r600_texture
*rtex
,
772 struct r600_cmask_info
*out
)
774 unsigned cmask_tile_width
= 8;
775 unsigned cmask_tile_height
= 8;
776 unsigned cmask_tile_elements
= cmask_tile_width
* cmask_tile_height
;
777 unsigned element_bits
= 4;
778 unsigned cmask_cache_bits
= 1024;
779 unsigned num_pipes
= rscreen
->info
.num_tile_pipes
;
780 unsigned pipe_interleave_bytes
= rscreen
->info
.pipe_interleave_bytes
;
782 unsigned elements_per_macro_tile
= (cmask_cache_bits
/ element_bits
) * num_pipes
;
783 unsigned pixels_per_macro_tile
= elements_per_macro_tile
* cmask_tile_elements
;
784 unsigned sqrt_pixels_per_macro_tile
= sqrt(pixels_per_macro_tile
);
785 unsigned macro_tile_width
= util_next_power_of_two(sqrt_pixels_per_macro_tile
);
786 unsigned macro_tile_height
= pixels_per_macro_tile
/ macro_tile_width
;
788 unsigned pitch_elements
= align(rtex
->resource
.b
.b
.width0
, macro_tile_width
);
789 unsigned height
= align(rtex
->resource
.b
.b
.height0
, macro_tile_height
);
791 unsigned base_align
= num_pipes
* pipe_interleave_bytes
;
792 unsigned slice_bytes
=
793 ((pitch_elements
* height
* element_bits
+ 7) / 8) / cmask_tile_elements
;
795 assert(macro_tile_width
% 128 == 0);
796 assert(macro_tile_height
% 128 == 0);
798 out
->slice_tile_max
= ((pitch_elements
* height
) / (128*128)) - 1;
799 out
->alignment
= MAX2(256, base_align
);
800 out
->size
= (util_max_layer(&rtex
->resource
.b
.b
, 0) + 1) *
801 align(slice_bytes
, base_align
);
804 static void si_texture_get_cmask_info(struct r600_common_screen
*rscreen
,
805 struct r600_texture
*rtex
,
806 struct r600_cmask_info
*out
)
808 unsigned pipe_interleave_bytes
= rscreen
->info
.pipe_interleave_bytes
;
809 unsigned num_pipes
= rscreen
->info
.num_tile_pipes
;
810 unsigned cl_width
, cl_height
;
812 if (rscreen
->chip_class
>= GFX9
) {
813 out
->alignment
= rtex
->surface
.u
.gfx9
.cmask_alignment
;
814 out
->size
= rtex
->surface
.u
.gfx9
.cmask_size
;
831 case 16: /* Hawaii */
840 unsigned base_align
= num_pipes
* pipe_interleave_bytes
;
842 unsigned width
= align(rtex
->resource
.b
.b
.width0
, cl_width
*8);
843 unsigned height
= align(rtex
->resource
.b
.b
.height0
, cl_height
*8);
844 unsigned slice_elements
= (width
* height
) / (8*8);
846 /* Each element of CMASK is a nibble. */
847 unsigned slice_bytes
= slice_elements
/ 2;
849 out
->slice_tile_max
= (width
* height
) / (128*128);
850 if (out
->slice_tile_max
)
851 out
->slice_tile_max
-= 1;
853 out
->alignment
= MAX2(256, base_align
);
854 out
->size
= (util_max_layer(&rtex
->resource
.b
.b
, 0) + 1) *
855 align(slice_bytes
, base_align
);
858 static void r600_texture_allocate_cmask(struct r600_common_screen
*rscreen
,
859 struct r600_texture
*rtex
)
861 if (rscreen
->chip_class
>= SI
) {
862 si_texture_get_cmask_info(rscreen
, rtex
, &rtex
->cmask
);
864 r600_texture_get_cmask_info(rscreen
, rtex
, &rtex
->cmask
);
867 rtex
->cmask
.offset
= align64(rtex
->size
, rtex
->cmask
.alignment
);
868 rtex
->size
= rtex
->cmask
.offset
+ rtex
->cmask
.size
;
870 if (rscreen
->chip_class
>= SI
)
871 rtex
->cb_color_info
|= SI_S_028C70_FAST_CLEAR(1);
873 rtex
->cb_color_info
|= EG_S_028C70_FAST_CLEAR(1);
876 static void r600_texture_alloc_cmask_separate(struct r600_common_screen
*rscreen
,
877 struct r600_texture
*rtex
)
879 if (rtex
->cmask_buffer
)
882 assert(rtex
->cmask
.size
== 0);
884 if (rscreen
->chip_class
>= SI
) {
885 si_texture_get_cmask_info(rscreen
, rtex
, &rtex
->cmask
);
887 r600_texture_get_cmask_info(rscreen
, rtex
, &rtex
->cmask
);
890 rtex
->cmask_buffer
= (struct r600_resource
*)
891 r600_aligned_buffer_create(&rscreen
->b
,
892 R600_RESOURCE_FLAG_UNMAPPABLE
,
895 rtex
->cmask
.alignment
);
896 if (rtex
->cmask_buffer
== NULL
) {
897 rtex
->cmask
.size
= 0;
901 /* update colorbuffer state bits */
902 rtex
->cmask
.base_address_reg
= rtex
->cmask_buffer
->gpu_address
>> 8;
904 if (rscreen
->chip_class
>= SI
)
905 rtex
->cb_color_info
|= SI_S_028C70_FAST_CLEAR(1);
907 rtex
->cb_color_info
|= EG_S_028C70_FAST_CLEAR(1);
909 p_atomic_inc(&rscreen
->compressed_colortex_counter
);
912 static void r600_texture_get_htile_size(struct r600_common_screen
*rscreen
,
913 struct r600_texture
*rtex
)
915 unsigned cl_width
, cl_height
, width
, height
;
916 unsigned slice_elements
, slice_bytes
, pipe_interleave_bytes
, base_align
;
917 unsigned num_pipes
= rscreen
->info
.num_tile_pipes
;
919 assert(rscreen
->chip_class
<= VI
);
921 rtex
->surface
.htile_size
= 0;
923 if (rscreen
->chip_class
<= EVERGREEN
&&
924 rscreen
->info
.drm_major
== 2 && rscreen
->info
.drm_minor
< 26)
927 /* HW bug on R6xx. */
928 if (rscreen
->chip_class
== R600
&&
929 (rtex
->resource
.b
.b
.width0
> 7680 ||
930 rtex
->resource
.b
.b
.height0
> 7680))
933 /* HTILE is broken with 1D tiling on old kernels and CIK. */
934 if (rscreen
->chip_class
>= CIK
&&
935 rtex
->surface
.u
.legacy
.level
[0].mode
== RADEON_SURF_MODE_1D
&&
936 rscreen
->info
.drm_major
== 2 && rscreen
->info
.drm_minor
< 38)
939 /* Overalign HTILE on P2 configs to work around GPU hangs in
940 * piglit/depthstencil-render-miplevels 585.
942 * This has been confirmed to help Kabini & Stoney, where the hangs
943 * are always reproducible. I think I have seen the test hang
944 * on Carrizo too, though it was very rare there.
946 if (rscreen
->chip_class
>= CIK
&& num_pipes
< 4)
975 width
= align(rtex
->resource
.b
.b
.width0
, cl_width
* 8);
976 height
= align(rtex
->resource
.b
.b
.height0
, cl_height
* 8);
978 slice_elements
= (width
* height
) / (8 * 8);
979 slice_bytes
= slice_elements
* 4;
981 pipe_interleave_bytes
= rscreen
->info
.pipe_interleave_bytes
;
982 base_align
= num_pipes
* pipe_interleave_bytes
;
984 rtex
->surface
.htile_alignment
= base_align
;
985 rtex
->surface
.htile_size
=
986 (util_max_layer(&rtex
->resource
.b
.b
, 0) + 1) *
987 align(slice_bytes
, base_align
);
990 static void r600_texture_allocate_htile(struct r600_common_screen
*rscreen
,
991 struct r600_texture
*rtex
)
993 if (rscreen
->chip_class
<= VI
&& !rtex
->tc_compatible_htile
)
994 r600_texture_get_htile_size(rscreen
, rtex
);
996 if (!rtex
->surface
.htile_size
)
999 rtex
->htile_offset
= align(rtex
->size
, rtex
->surface
.htile_alignment
);
1000 rtex
->size
= rtex
->htile_offset
+ rtex
->surface
.htile_size
;
1003 void r600_print_texture_info(struct r600_common_screen
*rscreen
,
1004 struct r600_texture
*rtex
, FILE *f
)
1008 /* Common parameters. */
1009 fprintf(f
, " Info: npix_x=%u, npix_y=%u, npix_z=%u, blk_w=%u, "
1010 "blk_h=%u, array_size=%u, last_level=%u, "
1011 "bpe=%u, nsamples=%u, flags=0x%x, %s\n",
1012 rtex
->resource
.b
.b
.width0
, rtex
->resource
.b
.b
.height0
,
1013 rtex
->resource
.b
.b
.depth0
, rtex
->surface
.blk_w
,
1014 rtex
->surface
.blk_h
,
1015 rtex
->resource
.b
.b
.array_size
, rtex
->resource
.b
.b
.last_level
,
1016 rtex
->surface
.bpe
, rtex
->resource
.b
.b
.nr_samples
,
1017 rtex
->surface
.flags
, util_format_short_name(rtex
->resource
.b
.b
.format
));
1019 if (rscreen
->chip_class
>= GFX9
) {
1020 fprintf(f
, " Surf: size=%"PRIu64
", slice_size=%"PRIu64
", "
1021 "alignment=%u, swmode=%u, epitch=%u, pitch=%u\n",
1022 rtex
->surface
.surf_size
,
1023 rtex
->surface
.u
.gfx9
.surf_slice_size
,
1024 rtex
->surface
.surf_alignment
,
1025 rtex
->surface
.u
.gfx9
.surf
.swizzle_mode
,
1026 rtex
->surface
.u
.gfx9
.surf
.epitch
,
1027 rtex
->surface
.u
.gfx9
.surf_pitch
);
1029 if (rtex
->fmask
.size
) {
1030 fprintf(f
, " FMASK: offset=%"PRIu64
", size=%"PRIu64
", "
1031 "alignment=%u, swmode=%u, epitch=%u\n",
1033 rtex
->surface
.u
.gfx9
.fmask_size
,
1034 rtex
->surface
.u
.gfx9
.fmask_alignment
,
1035 rtex
->surface
.u
.gfx9
.fmask
.swizzle_mode
,
1036 rtex
->surface
.u
.gfx9
.fmask
.epitch
);
1039 if (rtex
->cmask
.size
) {
1040 fprintf(f
, " CMask: offset=%"PRIu64
", size=%"PRIu64
", "
1041 "alignment=%u, rb_aligned=%u, pipe_aligned=%u\n",
1043 rtex
->surface
.u
.gfx9
.cmask_size
,
1044 rtex
->surface
.u
.gfx9
.cmask_alignment
,
1045 rtex
->surface
.u
.gfx9
.cmask
.rb_aligned
,
1046 rtex
->surface
.u
.gfx9
.cmask
.pipe_aligned
);
1049 if (rtex
->htile_offset
) {
1050 fprintf(f
, " HTile: offset=%"PRIu64
", size=%"PRIu64
", alignment=%u, "
1051 "rb_aligned=%u, pipe_aligned=%u\n",
1053 rtex
->surface
.htile_size
,
1054 rtex
->surface
.htile_alignment
,
1055 rtex
->surface
.u
.gfx9
.htile
.rb_aligned
,
1056 rtex
->surface
.u
.gfx9
.htile
.pipe_aligned
);
1059 if (rtex
->dcc_offset
) {
1060 fprintf(f
, " DCC: offset=%"PRIu64
", size=%"PRIu64
", "
1061 "alignment=%u, pitch_max=%u, num_dcc_levels=%u\n",
1062 rtex
->dcc_offset
, rtex
->surface
.dcc_size
,
1063 rtex
->surface
.dcc_alignment
,
1064 rtex
->surface
.u
.gfx9
.dcc_pitch_max
,
1065 rtex
->surface
.num_dcc_levels
);
1068 if (rtex
->surface
.u
.gfx9
.stencil_offset
) {
1069 fprintf(f
, " Stencil: offset=%"PRIu64
", swmode=%u, epitch=%u\n",
1070 rtex
->surface
.u
.gfx9
.stencil_offset
,
1071 rtex
->surface
.u
.gfx9
.stencil
.swizzle_mode
,
1072 rtex
->surface
.u
.gfx9
.stencil
.epitch
);
1077 fprintf(f
, " Layout: size=%"PRIu64
", alignment=%u, bankw=%u, "
1078 "bankh=%u, nbanks=%u, mtilea=%u, tilesplit=%u, pipeconfig=%u, scanout=%u\n",
1079 rtex
->surface
.surf_size
, rtex
->surface
.surf_alignment
, rtex
->surface
.u
.legacy
.bankw
,
1080 rtex
->surface
.u
.legacy
.bankh
, rtex
->surface
.u
.legacy
.num_banks
, rtex
->surface
.u
.legacy
.mtilea
,
1081 rtex
->surface
.u
.legacy
.tile_split
, rtex
->surface
.u
.legacy
.pipe_config
,
1082 (rtex
->surface
.flags
& RADEON_SURF_SCANOUT
) != 0);
1084 if (rtex
->fmask
.size
)
1085 fprintf(f
, " FMask: offset=%"PRIu64
", size=%"PRIu64
", alignment=%u, pitch_in_pixels=%u, "
1086 "bankh=%u, slice_tile_max=%u, tile_mode_index=%u\n",
1087 rtex
->fmask
.offset
, rtex
->fmask
.size
, rtex
->fmask
.alignment
,
1088 rtex
->fmask
.pitch_in_pixels
, rtex
->fmask
.bank_height
,
1089 rtex
->fmask
.slice_tile_max
, rtex
->fmask
.tile_mode_index
);
1091 if (rtex
->cmask
.size
)
1092 fprintf(f
, " CMask: offset=%"PRIu64
", size=%"PRIu64
", alignment=%u, "
1093 "slice_tile_max=%u\n",
1094 rtex
->cmask
.offset
, rtex
->cmask
.size
, rtex
->cmask
.alignment
,
1095 rtex
->cmask
.slice_tile_max
);
1097 if (rtex
->htile_offset
)
1098 fprintf(f
, " HTile: offset=%"PRIu64
", size=%"PRIu64
", "
1099 "alignment=%u, TC_compatible = %u\n",
1100 rtex
->htile_offset
, rtex
->surface
.htile_size
,
1101 rtex
->surface
.htile_alignment
,
1102 rtex
->tc_compatible_htile
);
1104 if (rtex
->dcc_offset
) {
1105 fprintf(f
, " DCC: offset=%"PRIu64
", size=%"PRIu64
", alignment=%u\n",
1106 rtex
->dcc_offset
, rtex
->surface
.dcc_size
,
1107 rtex
->surface
.dcc_alignment
);
1108 for (i
= 0; i
<= rtex
->resource
.b
.b
.last_level
; i
++)
1109 fprintf(f
, " DCCLevel[%i]: enabled=%u, offset=%"PRIu64
", "
1110 "fast_clear_size=%"PRIu64
"\n",
1111 i
, i
< rtex
->surface
.num_dcc_levels
,
1112 rtex
->surface
.u
.legacy
.level
[i
].dcc_offset
,
1113 rtex
->surface
.u
.legacy
.level
[i
].dcc_fast_clear_size
);
1116 for (i
= 0; i
<= rtex
->resource
.b
.b
.last_level
; i
++)
1117 fprintf(f
, " Level[%i]: offset=%"PRIu64
", slice_size=%"PRIu64
", "
1118 "npix_x=%u, npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
1119 "mode=%u, tiling_index = %u\n",
1120 i
, rtex
->surface
.u
.legacy
.level
[i
].offset
,
1121 rtex
->surface
.u
.legacy
.level
[i
].slice_size
,
1122 u_minify(rtex
->resource
.b
.b
.width0
, i
),
1123 u_minify(rtex
->resource
.b
.b
.height0
, i
),
1124 u_minify(rtex
->resource
.b
.b
.depth0
, i
),
1125 rtex
->surface
.u
.legacy
.level
[i
].nblk_x
,
1126 rtex
->surface
.u
.legacy
.level
[i
].nblk_y
,
1127 rtex
->surface
.u
.legacy
.level
[i
].mode
,
1128 rtex
->surface
.u
.legacy
.tiling_index
[i
]);
1130 if (rtex
->surface
.flags
& RADEON_SURF_SBUFFER
) {
1131 fprintf(f
, " StencilLayout: tilesplit=%u\n",
1132 rtex
->surface
.u
.legacy
.stencil_tile_split
);
1133 for (i
= 0; i
<= rtex
->resource
.b
.b
.last_level
; i
++) {
1134 fprintf(f
, " StencilLevel[%i]: offset=%"PRIu64
", "
1135 "slice_size=%"PRIu64
", npix_x=%u, "
1136 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
1137 "mode=%u, tiling_index = %u\n",
1138 i
, rtex
->surface
.u
.legacy
.stencil_level
[i
].offset
,
1139 rtex
->surface
.u
.legacy
.stencil_level
[i
].slice_size
,
1140 u_minify(rtex
->resource
.b
.b
.width0
, i
),
1141 u_minify(rtex
->resource
.b
.b
.height0
, i
),
1142 u_minify(rtex
->resource
.b
.b
.depth0
, i
),
1143 rtex
->surface
.u
.legacy
.stencil_level
[i
].nblk_x
,
1144 rtex
->surface
.u
.legacy
.stencil_level
[i
].nblk_y
,
1145 rtex
->surface
.u
.legacy
.stencil_level
[i
].mode
,
1146 rtex
->surface
.u
.legacy
.stencil_tiling_index
[i
]);
1151 /* Common processing for r600_texture_create and r600_texture_from_handle */
1152 static struct r600_texture
*
1153 r600_texture_create_object(struct pipe_screen
*screen
,
1154 const struct pipe_resource
*base
,
1155 struct pb_buffer
*buf
,
1156 struct radeon_surf
*surface
)
1158 struct r600_texture
*rtex
;
1159 struct r600_resource
*resource
;
1160 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)screen
;
1162 rtex
= CALLOC_STRUCT(r600_texture
);
1166 resource
= &rtex
->resource
;
1167 resource
->b
.b
= *base
;
1168 resource
->b
.b
.next
= NULL
;
1169 resource
->b
.vtbl
= &r600_texture_vtbl
;
1170 pipe_reference_init(&resource
->b
.b
.reference
, 1);
1171 resource
->b
.b
.screen
= screen
;
1173 /* don't include stencil-only formats which we don't support for rendering */
1174 rtex
->is_depth
= util_format_has_depth(util_format_description(rtex
->resource
.b
.b
.format
));
1176 rtex
->surface
= *surface
;
1177 rtex
->size
= rtex
->surface
.surf_size
;
1179 rtex
->tc_compatible_htile
= rtex
->surface
.htile_size
!= 0 &&
1180 (rtex
->surface
.flags
&
1181 RADEON_SURF_TC_COMPATIBLE_HTILE
);
1183 /* TC-compatible HTILE:
1184 * - VI only supports Z32_FLOAT.
1185 * - GFX9 only supports Z32_FLOAT and Z16_UNORM. */
1186 if (rtex
->tc_compatible_htile
) {
1187 if (rscreen
->chip_class
>= GFX9
&&
1188 base
->format
== PIPE_FORMAT_Z16_UNORM
)
1189 rtex
->db_render_format
= base
->format
;
1191 rtex
->db_render_format
= PIPE_FORMAT_Z32_FLOAT
;
1193 rtex
->db_render_format
= base
->format
;
1196 /* Tiled depth textures utilize the non-displayable tile order.
1197 * This must be done after r600_setup_surface.
1198 * Applies to R600-Cayman. */
1199 rtex
->non_disp_tiling
= rtex
->is_depth
&& rtex
->surface
.u
.legacy
.level
[0].mode
>= RADEON_SURF_MODE_1D
;
1200 /* Applies to GCN. */
1201 rtex
->last_msaa_resolve_target_micro_mode
= rtex
->surface
.micro_tile_mode
;
1203 /* Disable separate DCC at the beginning. DRI2 doesn't reuse buffers
1204 * between frames, so the only thing that can enable separate DCC
1205 * with DRI2 is multiple slow clears within a frame.
1207 rtex
->ps_draw_ratio
= 0;
1209 if (rtex
->is_depth
) {
1210 if (base
->flags
& (R600_RESOURCE_FLAG_TRANSFER
|
1211 R600_RESOURCE_FLAG_FLUSHED_DEPTH
) ||
1212 rscreen
->chip_class
>= EVERGREEN
) {
1213 if (rscreen
->chip_class
>= GFX9
) {
1214 rtex
->can_sample_z
= true;
1215 rtex
->can_sample_s
= true;
1217 rtex
->can_sample_z
= !rtex
->surface
.u
.legacy
.depth_adjusted
;
1218 rtex
->can_sample_s
= !rtex
->surface
.u
.legacy
.stencil_adjusted
;
1221 if (rtex
->resource
.b
.b
.nr_samples
<= 1 &&
1222 (rtex
->resource
.b
.b
.format
== PIPE_FORMAT_Z16_UNORM
||
1223 rtex
->resource
.b
.b
.format
== PIPE_FORMAT_Z32_FLOAT
))
1224 rtex
->can_sample_z
= true;
1227 if (!(base
->flags
& (R600_RESOURCE_FLAG_TRANSFER
|
1228 R600_RESOURCE_FLAG_FLUSHED_DEPTH
))) {
1229 rtex
->db_compatible
= true;
1231 if (!(rscreen
->debug_flags
& DBG_NO_HYPERZ
))
1232 r600_texture_allocate_htile(rscreen
, rtex
);
1235 if (base
->nr_samples
> 1) {
1237 r600_texture_allocate_fmask(rscreen
, rtex
);
1238 r600_texture_allocate_cmask(rscreen
, rtex
);
1239 rtex
->cmask_buffer
= &rtex
->resource
;
1241 if (!rtex
->fmask
.size
|| !rtex
->cmask
.size
) {
1247 /* Shared textures must always set up DCC here.
1248 * If it's not present, it will be disabled by
1249 * apply_opaque_metadata later.
1251 if (rtex
->surface
.dcc_size
&&
1252 (buf
|| !(rscreen
->debug_flags
& DBG_NO_DCC
)) &&
1253 !(rtex
->surface
.flags
& RADEON_SURF_SCANOUT
)) {
1254 /* Reserve space for the DCC buffer. */
1255 rtex
->dcc_offset
= align64(rtex
->size
, rtex
->surface
.dcc_alignment
);
1256 rtex
->size
= rtex
->dcc_offset
+ rtex
->surface
.dcc_size
;
1260 /* Now create the backing buffer. */
1262 r600_init_resource_fields(rscreen
, resource
, rtex
->size
,
1263 rtex
->surface
.surf_alignment
);
1265 /* Displayable surfaces are not suballocated. */
1266 if (resource
->b
.b
.bind
& PIPE_BIND_SCANOUT
)
1267 resource
->flags
|= RADEON_FLAG_NO_SUBALLOC
;
1269 if (!r600_alloc_resource(rscreen
, resource
)) {
1274 resource
->buf
= buf
;
1275 resource
->gpu_address
= rscreen
->ws
->buffer_get_virtual_address(resource
->buf
);
1276 resource
->bo_size
= buf
->size
;
1277 resource
->bo_alignment
= buf
->alignment
;
1278 resource
->domains
= rscreen
->ws
->buffer_get_initial_domain(resource
->buf
);
1279 if (resource
->domains
& RADEON_DOMAIN_VRAM
)
1280 resource
->vram_usage
= buf
->size
;
1281 else if (resource
->domains
& RADEON_DOMAIN_GTT
)
1282 resource
->gart_usage
= buf
->size
;
1285 if (rtex
->cmask
.size
) {
1286 /* Initialize the cmask to 0xCC (= compressed state). */
1287 r600_screen_clear_buffer(rscreen
, &rtex
->cmask_buffer
->b
.b
,
1288 rtex
->cmask
.offset
, rtex
->cmask
.size
,
1291 if (rtex
->htile_offset
) {
1292 uint32_t clear_value
= 0;
1294 if (rscreen
->chip_class
>= GFX9
|| rtex
->tc_compatible_htile
)
1295 clear_value
= 0x0000030F;
1297 r600_screen_clear_buffer(rscreen
, &rtex
->resource
.b
.b
,
1299 rtex
->surface
.htile_size
,
1303 /* Initialize DCC only if the texture is not being imported. */
1304 if (!buf
&& rtex
->dcc_offset
) {
1305 r600_screen_clear_buffer(rscreen
, &rtex
->resource
.b
.b
,
1307 rtex
->surface
.dcc_size
,
1311 /* Initialize the CMASK base register value. */
1312 rtex
->cmask
.base_address_reg
=
1313 (rtex
->resource
.gpu_address
+ rtex
->cmask
.offset
) >> 8;
1315 if (rscreen
->debug_flags
& DBG_VM
) {
1316 fprintf(stderr
, "VM start=0x%"PRIX64
" end=0x%"PRIX64
" | Texture %ix%ix%i, %i levels, %i samples, %s\n",
1317 rtex
->resource
.gpu_address
,
1318 rtex
->resource
.gpu_address
+ rtex
->resource
.buf
->size
,
1319 base
->width0
, base
->height0
, util_max_layer(base
, 0)+1, base
->last_level
+1,
1320 base
->nr_samples
? base
->nr_samples
: 1, util_format_short_name(base
->format
));
1323 if (rscreen
->debug_flags
& DBG_TEX
) {
1325 r600_print_texture_info(rscreen
, rtex
, stdout
);
1332 static enum radeon_surf_mode
1333 r600_choose_tiling(struct r600_common_screen
*rscreen
,
1334 const struct pipe_resource
*templ
)
1336 const struct util_format_description
*desc
= util_format_description(templ
->format
);
1337 bool force_tiling
= templ
->flags
& R600_RESOURCE_FLAG_FORCE_TILING
;
1338 bool is_depth_stencil
= util_format_is_depth_or_stencil(templ
->format
) &&
1339 !(templ
->flags
& R600_RESOURCE_FLAG_FLUSHED_DEPTH
);
1341 /* MSAA resources must be 2D tiled. */
1342 if (templ
->nr_samples
> 1)
1343 return RADEON_SURF_MODE_2D
;
1345 /* Transfer resources should be linear. */
1346 if (templ
->flags
& R600_RESOURCE_FLAG_TRANSFER
)
1347 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1349 /* Avoid Z/S decompress blits by forcing TC-compatible HTILE on VI,
1350 * which requires 2D tiling.
1352 if (rscreen
->chip_class
== VI
&&
1354 (templ
->flags
& PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY
))
1355 return RADEON_SURF_MODE_2D
;
1357 /* r600g: force tiling on TEXTURE_2D and TEXTURE_3D compute resources. */
1358 if (rscreen
->chip_class
>= R600
&& rscreen
->chip_class
<= CAYMAN
&&
1359 (templ
->bind
& PIPE_BIND_COMPUTE_RESOURCE
) &&
1360 (templ
->target
== PIPE_TEXTURE_2D
||
1361 templ
->target
== PIPE_TEXTURE_3D
))
1362 force_tiling
= true;
1364 /* Handle common candidates for the linear mode.
1365 * Compressed textures and DB surfaces must always be tiled.
1367 if (!force_tiling
&&
1368 !is_depth_stencil
&&
1369 !util_format_is_compressed(templ
->format
)) {
1370 if (rscreen
->debug_flags
& DBG_NO_TILING
)
1371 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1373 /* Tiling doesn't work with the 422 (SUBSAMPLED) formats on R600+. */
1374 if (desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
)
1375 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1377 /* Cursors are linear on SI.
1378 * (XXX double-check, maybe also use RADEON_SURF_SCANOUT) */
1379 if (rscreen
->chip_class
>= SI
&&
1380 (templ
->bind
& PIPE_BIND_CURSOR
))
1381 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1383 if (templ
->bind
& PIPE_BIND_LINEAR
)
1384 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1386 /* Textures with a very small height are recommended to be linear. */
1387 if (templ
->target
== PIPE_TEXTURE_1D
||
1388 templ
->target
== PIPE_TEXTURE_1D_ARRAY
||
1389 /* Only very thin and long 2D textures should benefit from
1390 * linear_aligned. */
1391 (templ
->width0
> 8 && templ
->height0
<= 2))
1392 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1394 /* Textures likely to be mapped often. */
1395 if (templ
->usage
== PIPE_USAGE_STAGING
||
1396 templ
->usage
== PIPE_USAGE_STREAM
)
1397 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1400 /* Make small textures 1D tiled. */
1401 if (templ
->width0
<= 16 || templ
->height0
<= 16 ||
1402 (rscreen
->debug_flags
& DBG_NO_2D_TILING
))
1403 return RADEON_SURF_MODE_1D
;
1405 /* The allocator will switch to 1D if needed. */
1406 return RADEON_SURF_MODE_2D
;
1409 struct pipe_resource
*r600_texture_create(struct pipe_screen
*screen
,
1410 const struct pipe_resource
*templ
)
1412 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)screen
;
1413 struct radeon_surf surface
= {0};
1414 bool is_flushed_depth
= templ
->flags
& R600_RESOURCE_FLAG_FLUSHED_DEPTH
;
1415 bool tc_compatible_htile
=
1416 rscreen
->chip_class
>= VI
&&
1417 (templ
->flags
& PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY
) &&
1418 !(rscreen
->debug_flags
& DBG_NO_HYPERZ
) &&
1419 !is_flushed_depth
&&
1420 templ
->nr_samples
<= 1 && /* TC-compat HTILE is less efficient with MSAA */
1421 util_format_is_depth_or_stencil(templ
->format
);
1425 r
= r600_init_surface(rscreen
, &surface
, templ
,
1426 r600_choose_tiling(rscreen
, templ
), 0, 0,
1427 false, false, is_flushed_depth
,
1428 tc_compatible_htile
);
1433 return (struct pipe_resource
*)
1434 r600_texture_create_object(screen
, templ
, NULL
, &surface
);
1437 static struct pipe_resource
*r600_texture_from_handle(struct pipe_screen
*screen
,
1438 const struct pipe_resource
*templ
,
1439 struct winsys_handle
*whandle
,
1442 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)screen
;
1443 struct pb_buffer
*buf
= NULL
;
1444 unsigned stride
= 0, offset
= 0;
1445 unsigned array_mode
;
1446 struct radeon_surf surface
;
1448 struct radeon_bo_metadata metadata
= {};
1449 struct r600_texture
*rtex
;
1452 /* Support only 2D textures without mipmaps */
1453 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
) ||
1454 templ
->depth0
!= 1 || templ
->last_level
!= 0)
1457 buf
= rscreen
->ws
->buffer_from_handle(rscreen
->ws
, whandle
, &stride
, &offset
);
1461 rscreen
->ws
->buffer_get_metadata(buf
, &metadata
);
1463 if (rscreen
->chip_class
>= GFX9
) {
1464 if (metadata
.u
.gfx9
.swizzle_mode
> 0)
1465 array_mode
= RADEON_SURF_MODE_2D
;
1467 array_mode
= RADEON_SURF_MODE_LINEAR_ALIGNED
;
1469 is_scanout
= metadata
.u
.gfx9
.swizzle_mode
== 0 ||
1470 metadata
.u
.gfx9
.swizzle_mode
% 4 == 2;
1472 surface
.u
.legacy
.pipe_config
= metadata
.u
.legacy
.pipe_config
;
1473 surface
.u
.legacy
.bankw
= metadata
.u
.legacy
.bankw
;
1474 surface
.u
.legacy
.bankh
= metadata
.u
.legacy
.bankh
;
1475 surface
.u
.legacy
.tile_split
= metadata
.u
.legacy
.tile_split
;
1476 surface
.u
.legacy
.mtilea
= metadata
.u
.legacy
.mtilea
;
1477 surface
.u
.legacy
.num_banks
= metadata
.u
.legacy
.num_banks
;
1479 if (metadata
.u
.legacy
.macrotile
== RADEON_LAYOUT_TILED
)
1480 array_mode
= RADEON_SURF_MODE_2D
;
1481 else if (metadata
.u
.legacy
.microtile
== RADEON_LAYOUT_TILED
)
1482 array_mode
= RADEON_SURF_MODE_1D
;
1484 array_mode
= RADEON_SURF_MODE_LINEAR_ALIGNED
;
1486 is_scanout
= metadata
.u
.legacy
.scanout
;
1489 r
= r600_init_surface(rscreen
, &surface
, templ
, array_mode
, stride
,
1490 offset
, true, is_scanout
, false, false);
1495 rtex
= r600_texture_create_object(screen
, templ
, buf
, &surface
);
1499 rtex
->resource
.b
.is_shared
= true;
1500 rtex
->resource
.external_usage
= usage
;
1502 if (rscreen
->apply_opaque_metadata
)
1503 rscreen
->apply_opaque_metadata(rscreen
, rtex
, &metadata
);
1505 /* Validate that addrlib arrived at the same surface parameters. */
1506 if (rscreen
->chip_class
>= GFX9
) {
1507 assert(metadata
.u
.gfx9
.swizzle_mode
== surface
.u
.gfx9
.surf
.swizzle_mode
);
1510 return &rtex
->resource
.b
.b
;
1513 bool r600_init_flushed_depth_texture(struct pipe_context
*ctx
,
1514 struct pipe_resource
*texture
,
1515 struct r600_texture
**staging
)
1517 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
1518 struct pipe_resource resource
;
1519 struct r600_texture
**flushed_depth_texture
= staging
?
1520 staging
: &rtex
->flushed_depth_texture
;
1521 enum pipe_format pipe_format
= texture
->format
;
1524 if (rtex
->flushed_depth_texture
)
1525 return true; /* it's ready */
1527 if (!rtex
->can_sample_z
&& rtex
->can_sample_s
) {
1528 switch (pipe_format
) {
1529 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1530 /* Save memory by not allocating the S plane. */
1531 pipe_format
= PIPE_FORMAT_Z32_FLOAT
;
1533 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1534 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1535 /* Save memory bandwidth by not copying the
1536 * stencil part during flush.
1538 * This potentially increases memory bandwidth
1539 * if an application uses both Z and S texturing
1540 * simultaneously (a flushed Z24S8 texture
1541 * would be stored compactly), but how often
1542 * does that really happen?
1544 pipe_format
= PIPE_FORMAT_Z24X8_UNORM
;
1548 } else if (!rtex
->can_sample_s
&& rtex
->can_sample_z
) {
1549 assert(util_format_has_stencil(util_format_description(pipe_format
)));
1551 /* DB->CB copies to an 8bpp surface don't work. */
1552 pipe_format
= PIPE_FORMAT_X24S8_UINT
;
1556 memset(&resource
, 0, sizeof(resource
));
1557 resource
.target
= texture
->target
;
1558 resource
.format
= pipe_format
;
1559 resource
.width0
= texture
->width0
;
1560 resource
.height0
= texture
->height0
;
1561 resource
.depth0
= texture
->depth0
;
1562 resource
.array_size
= texture
->array_size
;
1563 resource
.last_level
= texture
->last_level
;
1564 resource
.nr_samples
= texture
->nr_samples
;
1565 resource
.usage
= staging
? PIPE_USAGE_STAGING
: PIPE_USAGE_DEFAULT
;
1566 resource
.bind
= texture
->bind
& ~PIPE_BIND_DEPTH_STENCIL
;
1567 resource
.flags
= texture
->flags
| R600_RESOURCE_FLAG_FLUSHED_DEPTH
;
1570 resource
.flags
|= R600_RESOURCE_FLAG_TRANSFER
;
1572 *flushed_depth_texture
= (struct r600_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
1573 if (*flushed_depth_texture
== NULL
) {
1574 R600_ERR("failed to create temporary texture to hold flushed depth\n");
1578 (*flushed_depth_texture
)->non_disp_tiling
= false;
1583 * Initialize the pipe_resource descriptor to be of the same size as the box,
1584 * which is supposed to hold a subregion of the texture "orig" at the given
1587 static void r600_init_temp_resource_from_box(struct pipe_resource
*res
,
1588 struct pipe_resource
*orig
,
1589 const struct pipe_box
*box
,
1590 unsigned level
, unsigned flags
)
1592 memset(res
, 0, sizeof(*res
));
1593 res
->format
= orig
->format
;
1594 res
->width0
= box
->width
;
1595 res
->height0
= box
->height
;
1597 res
->array_size
= 1;
1598 res
->usage
= flags
& R600_RESOURCE_FLAG_TRANSFER
? PIPE_USAGE_STAGING
: PIPE_USAGE_DEFAULT
;
1601 /* We must set the correct texture target and dimensions for a 3D box. */
1602 if (box
->depth
> 1 && util_max_layer(orig
, level
) > 0) {
1603 res
->target
= PIPE_TEXTURE_2D_ARRAY
;
1604 res
->array_size
= box
->depth
;
1606 res
->target
= PIPE_TEXTURE_2D
;
1610 static bool r600_can_invalidate_texture(struct r600_common_screen
*rscreen
,
1611 struct r600_texture
*rtex
,
1612 unsigned transfer_usage
,
1613 const struct pipe_box
*box
)
1615 /* r600g doesn't react to dirty_tex_descriptor_counter */
1616 return rscreen
->chip_class
>= SI
&&
1617 !rtex
->resource
.b
.is_shared
&&
1618 !(transfer_usage
& PIPE_TRANSFER_READ
) &&
1619 rtex
->resource
.b
.b
.last_level
== 0 &&
1620 util_texrange_covers_whole_level(&rtex
->resource
.b
.b
, 0,
1621 box
->x
, box
->y
, box
->z
,
1622 box
->width
, box
->height
,
1626 static void r600_texture_invalidate_storage(struct r600_common_context
*rctx
,
1627 struct r600_texture
*rtex
)
1629 struct r600_common_screen
*rscreen
= rctx
->screen
;
1631 /* There is no point in discarding depth and tiled buffers. */
1632 assert(!rtex
->is_depth
);
1633 assert(rtex
->surface
.is_linear
);
1635 /* Reallocate the buffer in the same pipe_resource. */
1636 r600_alloc_resource(rscreen
, &rtex
->resource
);
1638 /* Initialize the CMASK base address (needed even without CMASK). */
1639 rtex
->cmask
.base_address_reg
=
1640 (rtex
->resource
.gpu_address
+ rtex
->cmask
.offset
) >> 8;
1642 p_atomic_inc(&rscreen
->dirty_tex_counter
);
1644 rctx
->num_alloc_tex_transfer_bytes
+= rtex
->size
;
1647 static void *r600_texture_transfer_map(struct pipe_context
*ctx
,
1648 struct pipe_resource
*texture
,
1651 const struct pipe_box
*box
,
1652 struct pipe_transfer
**ptransfer
)
1654 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
1655 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
1656 struct r600_transfer
*trans
;
1657 struct r600_resource
*buf
;
1658 unsigned offset
= 0;
1660 bool use_staging_texture
= false;
1662 assert(!(texture
->flags
& R600_RESOURCE_FLAG_TRANSFER
));
1663 assert(box
->width
&& box
->height
&& box
->depth
);
1665 /* Depth textures use staging unconditionally. */
1666 if (!rtex
->is_depth
) {
1667 /* Degrade the tile mode if we get too many transfers on APUs.
1668 * On dGPUs, the staging texture is always faster.
1669 * Only count uploads that are at least 4x4 pixels large.
1671 if (!rctx
->screen
->info
.has_dedicated_vram
&&
1673 box
->width
>= 4 && box
->height
>= 4 &&
1674 p_atomic_inc_return(&rtex
->num_level0_transfers
) == 10) {
1675 bool can_invalidate
=
1676 r600_can_invalidate_texture(rctx
->screen
, rtex
,
1679 r600_reallocate_texture_inplace(rctx
, rtex
,
1684 /* Tiled textures need to be converted into a linear texture for CPU
1685 * access. The staging texture is always linear and is placed in GART.
1687 * Reading from VRAM or GTT WC is slow, always use the staging
1688 * texture in this case.
1690 * Use the staging texture for uploads if the underlying BO
1693 if (!rtex
->surface
.is_linear
)
1694 use_staging_texture
= true;
1695 else if (usage
& PIPE_TRANSFER_READ
)
1696 use_staging_texture
=
1697 rtex
->resource
.domains
& RADEON_DOMAIN_VRAM
||
1698 rtex
->resource
.flags
& RADEON_FLAG_GTT_WC
;
1699 /* Write & linear only: */
1700 else if (r600_rings_is_buffer_referenced(rctx
, rtex
->resource
.buf
,
1701 RADEON_USAGE_READWRITE
) ||
1702 !rctx
->ws
->buffer_wait(rtex
->resource
.buf
, 0,
1703 RADEON_USAGE_READWRITE
)) {
1705 if (r600_can_invalidate_texture(rctx
->screen
, rtex
,
1707 r600_texture_invalidate_storage(rctx
, rtex
);
1709 use_staging_texture
= true;
1713 trans
= CALLOC_STRUCT(r600_transfer
);
1716 pipe_resource_reference(&trans
->b
.b
.resource
, texture
);
1717 trans
->b
.b
.level
= level
;
1718 trans
->b
.b
.usage
= usage
;
1719 trans
->b
.b
.box
= *box
;
1721 if (rtex
->is_depth
) {
1722 struct r600_texture
*staging_depth
;
1724 if (rtex
->resource
.b
.b
.nr_samples
> 1) {
1725 /* MSAA depth buffers need to be converted to single sample buffers.
1727 * Mapping MSAA depth buffers can occur if ReadPixels is called
1728 * with a multisample GLX visual.
1730 * First downsample the depth buffer to a temporary texture,
1731 * then decompress the temporary one to staging.
1733 * Only the region being mapped is transfered.
1735 struct pipe_resource resource
;
1737 r600_init_temp_resource_from_box(&resource
, texture
, box
, level
, 0);
1739 if (!r600_init_flushed_depth_texture(ctx
, &resource
, &staging_depth
)) {
1740 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1745 if (usage
& PIPE_TRANSFER_READ
) {
1746 struct pipe_resource
*temp
= ctx
->screen
->resource_create(ctx
->screen
, &resource
);
1748 R600_ERR("failed to create a temporary depth texture\n");
1753 r600_copy_region_with_blit(ctx
, temp
, 0, 0, 0, 0, texture
, level
, box
);
1754 rctx
->blit_decompress_depth(ctx
, (struct r600_texture
*)temp
, staging_depth
,
1755 0, 0, 0, box
->depth
, 0, 0);
1756 pipe_resource_reference(&temp
, NULL
);
1759 /* Just get the strides. */
1760 r600_texture_get_offset(rctx
->screen
, staging_depth
, level
, NULL
,
1762 &trans
->b
.b
.layer_stride
);
1764 /* XXX: only readback the rectangle which is being mapped? */
1765 /* XXX: when discard is true, no need to read back from depth texture */
1766 if (!r600_init_flushed_depth_texture(ctx
, texture
, &staging_depth
)) {
1767 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1772 rctx
->blit_decompress_depth(ctx
, rtex
, staging_depth
,
1774 box
->z
, box
->z
+ box
->depth
- 1,
1777 offset
= r600_texture_get_offset(rctx
->screen
, staging_depth
,
1780 &trans
->b
.b
.layer_stride
);
1783 trans
->staging
= (struct r600_resource
*)staging_depth
;
1784 buf
= trans
->staging
;
1785 } else if (use_staging_texture
) {
1786 struct pipe_resource resource
;
1787 struct r600_texture
*staging
;
1789 r600_init_temp_resource_from_box(&resource
, texture
, box
, level
,
1790 R600_RESOURCE_FLAG_TRANSFER
);
1791 resource
.usage
= (usage
& PIPE_TRANSFER_READ
) ?
1792 PIPE_USAGE_STAGING
: PIPE_USAGE_STREAM
;
1794 /* Create the temporary texture. */
1795 staging
= (struct r600_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
1797 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1801 trans
->staging
= &staging
->resource
;
1803 /* Just get the strides. */
1804 r600_texture_get_offset(rctx
->screen
, staging
, 0, NULL
,
1806 &trans
->b
.b
.layer_stride
);
1808 if (usage
& PIPE_TRANSFER_READ
)
1809 r600_copy_to_staging_texture(ctx
, trans
);
1811 usage
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
1813 buf
= trans
->staging
;
1815 /* the resource is mapped directly */
1816 offset
= r600_texture_get_offset(rctx
->screen
, rtex
, level
, box
,
1818 &trans
->b
.b
.layer_stride
);
1819 buf
= &rtex
->resource
;
1822 if (!(map
= r600_buffer_map_sync_with_rings(rctx
, buf
, usage
))) {
1823 r600_resource_reference(&trans
->staging
, NULL
);
1828 *ptransfer
= &trans
->b
.b
;
1829 return map
+ offset
;
1832 static void r600_texture_transfer_unmap(struct pipe_context
*ctx
,
1833 struct pipe_transfer
* transfer
)
1835 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
1836 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
1837 struct pipe_resource
*texture
= transfer
->resource
;
1838 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
1840 if ((transfer
->usage
& PIPE_TRANSFER_WRITE
) && rtransfer
->staging
) {
1841 if (rtex
->is_depth
&& rtex
->resource
.b
.b
.nr_samples
<= 1) {
1842 ctx
->resource_copy_region(ctx
, texture
, transfer
->level
,
1843 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
1844 &rtransfer
->staging
->b
.b
, transfer
->level
,
1847 r600_copy_from_staging_texture(ctx
, rtransfer
);
1851 if (rtransfer
->staging
) {
1852 rctx
->num_alloc_tex_transfer_bytes
+= rtransfer
->staging
->buf
->size
;
1853 r600_resource_reference(&rtransfer
->staging
, NULL
);
1856 /* Heuristic for {upload, draw, upload, draw, ..}:
1858 * Flush the gfx IB if we've allocated too much texture storage.
1860 * The idea is that we don't want to build IBs that use too much
1861 * memory and put pressure on the kernel memory manager and we also
1862 * want to make temporary and invalidated buffers go idle ASAP to
1863 * decrease the total memory usage or make them reusable. The memory
1864 * usage will be slightly higher than given here because of the buffer
1865 * cache in the winsys.
1867 * The result is that the kernel memory manager is never a bottleneck.
1869 if (rctx
->num_alloc_tex_transfer_bytes
> rctx
->screen
->info
.gart_size
/ 4) {
1870 rctx
->gfx
.flush(rctx
, RADEON_FLUSH_ASYNC
, NULL
);
1871 rctx
->num_alloc_tex_transfer_bytes
= 0;
1874 pipe_resource_reference(&transfer
->resource
, NULL
);
1878 static const struct u_resource_vtbl r600_texture_vtbl
=
1880 NULL
, /* get_handle */
1881 r600_texture_destroy
, /* resource_destroy */
1882 r600_texture_transfer_map
, /* transfer_map */
1883 u_default_transfer_flush_region
, /* transfer_flush_region */
1884 r600_texture_transfer_unmap
, /* transfer_unmap */
1887 /* DCC channel type categories within which formats can be reinterpreted
1888 * while keeping the same DCC encoding. The swizzle must also match. */
1889 enum dcc_channel_type
{
1890 dcc_channel_float32
,
1893 dcc_channel_float16
,
1896 dcc_channel_uint_10_10_10_2
,
1899 dcc_channel_incompatible
,
1902 /* Return the type of DCC encoding. */
1903 static enum dcc_channel_type
1904 vi_get_dcc_channel_type(const struct util_format_description
*desc
)
1908 /* Find the first non-void channel. */
1909 for (i
= 0; i
< desc
->nr_channels
; i
++)
1910 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
)
1912 if (i
== desc
->nr_channels
)
1913 return dcc_channel_incompatible
;
1915 switch (desc
->channel
[i
].size
) {
1917 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_FLOAT
)
1918 return dcc_channel_float32
;
1919 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
)
1920 return dcc_channel_uint32
;
1921 return dcc_channel_sint32
;
1923 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_FLOAT
)
1924 return dcc_channel_float16
;
1925 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
)
1926 return dcc_channel_uint16
;
1927 return dcc_channel_sint16
;
1929 return dcc_channel_uint_10_10_10_2
;
1931 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
)
1932 return dcc_channel_uint8
;
1933 return dcc_channel_sint8
;
1935 return dcc_channel_incompatible
;
1939 /* Return if it's allowed to reinterpret one format as another with DCC enabled. */
1940 bool vi_dcc_formats_compatible(enum pipe_format format1
,
1941 enum pipe_format format2
)
1943 const struct util_format_description
*desc1
, *desc2
;
1944 enum dcc_channel_type type1
, type2
;
1947 if (format1
== format2
)
1950 desc1
= util_format_description(format1
);
1951 desc2
= util_format_description(format2
);
1953 if (desc1
->nr_channels
!= desc2
->nr_channels
)
1956 /* Swizzles must be the same. */
1957 for (i
= 0; i
< desc1
->nr_channels
; i
++)
1958 if (desc1
->swizzle
[i
] <= PIPE_SWIZZLE_W
&&
1959 desc2
->swizzle
[i
] <= PIPE_SWIZZLE_W
&&
1960 desc1
->swizzle
[i
] != desc2
->swizzle
[i
])
1963 type1
= vi_get_dcc_channel_type(desc1
);
1964 type2
= vi_get_dcc_channel_type(desc2
);
1966 return type1
!= dcc_channel_incompatible
&&
1967 type2
!= dcc_channel_incompatible
&&
1971 bool vi_dcc_formats_are_incompatible(struct pipe_resource
*tex
,
1973 enum pipe_format view_format
)
1975 struct r600_texture
*rtex
= (struct r600_texture
*)tex
;
1977 return vi_dcc_enabled(rtex
, level
) &&
1978 !vi_dcc_formats_compatible(tex
->format
, view_format
);
1981 /* This can't be merged with the above function, because
1982 * vi_dcc_formats_compatible should be called only when DCC is enabled. */
1983 void vi_disable_dcc_if_incompatible_format(struct r600_common_context
*rctx
,
1984 struct pipe_resource
*tex
,
1986 enum pipe_format view_format
)
1988 struct r600_texture
*rtex
= (struct r600_texture
*)tex
;
1990 if (vi_dcc_enabled(rtex
, level
) &&
1991 !vi_dcc_formats_compatible(tex
->format
, view_format
))
1992 if (!r600_texture_disable_dcc(rctx
, (struct r600_texture
*)tex
))
1993 rctx
->decompress_dcc(&rctx
->b
, rtex
);
1996 struct pipe_surface
*r600_create_surface_custom(struct pipe_context
*pipe
,
1997 struct pipe_resource
*texture
,
1998 const struct pipe_surface
*templ
,
1999 unsigned width0
, unsigned height0
,
2000 unsigned width
, unsigned height
)
2002 struct r600_surface
*surface
= CALLOC_STRUCT(r600_surface
);
2007 assert(templ
->u
.tex
.first_layer
<= util_max_layer(texture
, templ
->u
.tex
.level
));
2008 assert(templ
->u
.tex
.last_layer
<= util_max_layer(texture
, templ
->u
.tex
.level
));
2010 pipe_reference_init(&surface
->base
.reference
, 1);
2011 pipe_resource_reference(&surface
->base
.texture
, texture
);
2012 surface
->base
.context
= pipe
;
2013 surface
->base
.format
= templ
->format
;
2014 surface
->base
.width
= width
;
2015 surface
->base
.height
= height
;
2016 surface
->base
.u
= templ
->u
;
2018 surface
->width0
= width0
;
2019 surface
->height0
= height0
;
2021 surface
->dcc_incompatible
=
2022 texture
->target
!= PIPE_BUFFER
&&
2023 vi_dcc_formats_are_incompatible(texture
, templ
->u
.tex
.level
,
2025 return &surface
->base
;
2028 static struct pipe_surface
*r600_create_surface(struct pipe_context
*pipe
,
2029 struct pipe_resource
*tex
,
2030 const struct pipe_surface
*templ
)
2032 unsigned level
= templ
->u
.tex
.level
;
2033 unsigned width
= u_minify(tex
->width0
, level
);
2034 unsigned height
= u_minify(tex
->height0
, level
);
2035 unsigned width0
= tex
->width0
;
2036 unsigned height0
= tex
->height0
;
2038 if (tex
->target
!= PIPE_BUFFER
&& templ
->format
!= tex
->format
) {
2039 const struct util_format_description
*tex_desc
2040 = util_format_description(tex
->format
);
2041 const struct util_format_description
*templ_desc
2042 = util_format_description(templ
->format
);
2044 assert(tex_desc
->block
.bits
== templ_desc
->block
.bits
);
2046 /* Adjust size of surface if and only if the block width or
2047 * height is changed. */
2048 if (tex_desc
->block
.width
!= templ_desc
->block
.width
||
2049 tex_desc
->block
.height
!= templ_desc
->block
.height
) {
2050 unsigned nblks_x
= util_format_get_nblocksx(tex
->format
, width
);
2051 unsigned nblks_y
= util_format_get_nblocksy(tex
->format
, height
);
2053 width
= nblks_x
* templ_desc
->block
.width
;
2054 height
= nblks_y
* templ_desc
->block
.height
;
2056 width0
= util_format_get_nblocksx(tex
->format
, width0
);
2057 height0
= util_format_get_nblocksy(tex
->format
, height0
);
2061 return r600_create_surface_custom(pipe
, tex
, templ
,
2066 static void r600_surface_destroy(struct pipe_context
*pipe
,
2067 struct pipe_surface
*surface
)
2069 struct r600_surface
*surf
= (struct r600_surface
*)surface
;
2070 r600_resource_reference(&surf
->cb_buffer_fmask
, NULL
);
2071 r600_resource_reference(&surf
->cb_buffer_cmask
, NULL
);
2072 pipe_resource_reference(&surface
->texture
, NULL
);
2076 static void r600_clear_texture(struct pipe_context
*pipe
,
2077 struct pipe_resource
*tex
,
2079 const struct pipe_box
*box
,
2082 struct pipe_screen
*screen
= pipe
->screen
;
2083 struct r600_texture
*rtex
= (struct r600_texture
*)tex
;
2084 struct pipe_surface tmpl
= {{0}};
2085 struct pipe_surface
*sf
;
2086 const struct util_format_description
*desc
=
2087 util_format_description(tex
->format
);
2089 tmpl
.format
= tex
->format
;
2090 tmpl
.u
.tex
.first_layer
= box
->z
;
2091 tmpl
.u
.tex
.last_layer
= box
->z
+ box
->depth
- 1;
2092 tmpl
.u
.tex
.level
= level
;
2093 sf
= pipe
->create_surface(pipe
, tex
, &tmpl
);
2097 if (rtex
->is_depth
) {
2100 uint8_t stencil
= 0;
2102 /* Depth is always present. */
2103 clear
= PIPE_CLEAR_DEPTH
;
2104 desc
->unpack_z_float(&depth
, 0, data
, 0, 1, 1);
2106 if (rtex
->surface
.flags
& RADEON_SURF_SBUFFER
) {
2107 clear
|= PIPE_CLEAR_STENCIL
;
2108 desc
->unpack_s_8uint(&stencil
, 0, data
, 0, 1, 1);
2111 pipe
->clear_depth_stencil(pipe
, sf
, clear
, depth
, stencil
,
2113 box
->width
, box
->height
, false);
2115 union pipe_color_union color
;
2117 /* pipe_color_union requires the full vec4 representation. */
2118 if (util_format_is_pure_uint(tex
->format
))
2119 desc
->unpack_rgba_uint(color
.ui
, 0, data
, 0, 1, 1);
2120 else if (util_format_is_pure_sint(tex
->format
))
2121 desc
->unpack_rgba_sint(color
.i
, 0, data
, 0, 1, 1);
2123 desc
->unpack_rgba_float(color
.f
, 0, data
, 0, 1, 1);
2125 if (screen
->is_format_supported(screen
, tex
->format
,
2127 PIPE_BIND_RENDER_TARGET
)) {
2128 pipe
->clear_render_target(pipe
, sf
, &color
,
2130 box
->width
, box
->height
, false);
2132 /* Software fallback - just for R9G9B9E5_FLOAT */
2133 util_clear_render_target(pipe
, sf
, &color
,
2135 box
->width
, box
->height
);
2138 pipe_surface_reference(&sf
, NULL
);
2141 unsigned r600_translate_colorswap(enum pipe_format format
, bool do_endian_swap
)
2143 const struct util_format_description
*desc
= util_format_description(format
);
2145 #define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == PIPE_SWIZZLE_##swz)
2147 if (format
== PIPE_FORMAT_R11G11B10_FLOAT
) /* isn't plain */
2148 return V_0280A0_SWAP_STD
;
2150 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
2153 switch (desc
->nr_channels
) {
2155 if (HAS_SWIZZLE(0,X
))
2156 return V_0280A0_SWAP_STD
; /* X___ */
2157 else if (HAS_SWIZZLE(3,X
))
2158 return V_0280A0_SWAP_ALT_REV
; /* ___X */
2161 if ((HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(1,Y
)) ||
2162 (HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(1,NONE
)) ||
2163 (HAS_SWIZZLE(0,NONE
) && HAS_SWIZZLE(1,Y
)))
2164 return V_0280A0_SWAP_STD
; /* XY__ */
2165 else if ((HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(1,X
)) ||
2166 (HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(1,NONE
)) ||
2167 (HAS_SWIZZLE(0,NONE
) && HAS_SWIZZLE(1,X
)))
2169 return (do_endian_swap
? V_0280A0_SWAP_STD
: V_0280A0_SWAP_STD_REV
);
2170 else if (HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(3,Y
))
2171 return V_0280A0_SWAP_ALT
; /* X__Y */
2172 else if (HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(3,X
))
2173 return V_0280A0_SWAP_ALT_REV
; /* Y__X */
2176 if (HAS_SWIZZLE(0,X
))
2177 return (do_endian_swap
? V_0280A0_SWAP_STD_REV
: V_0280A0_SWAP_STD
);
2178 else if (HAS_SWIZZLE(0,Z
))
2179 return V_0280A0_SWAP_STD_REV
; /* ZYX */
2182 /* check the middle channels, the 1st and 4th channel can be NONE */
2183 if (HAS_SWIZZLE(1,Y
) && HAS_SWIZZLE(2,Z
)) {
2184 return V_0280A0_SWAP_STD
; /* XYZW */
2185 } else if (HAS_SWIZZLE(1,Z
) && HAS_SWIZZLE(2,Y
)) {
2186 return V_0280A0_SWAP_STD_REV
; /* WZYX */
2187 } else if (HAS_SWIZZLE(1,Y
) && HAS_SWIZZLE(2,X
)) {
2188 return V_0280A0_SWAP_ALT
; /* ZYXW */
2189 } else if (HAS_SWIZZLE(1,Z
) && HAS_SWIZZLE(2,W
)) {
2192 return V_0280A0_SWAP_ALT_REV
;
2194 return (do_endian_swap
? V_0280A0_SWAP_ALT
: V_0280A0_SWAP_ALT_REV
);
2201 /* PIPELINE_STAT-BASED DCC ENABLEMENT FOR DISPLAYABLE SURFACES */
2203 static void vi_dcc_clean_up_context_slot(struct r600_common_context
*rctx
,
2208 if (rctx
->dcc_stats
[slot
].query_active
)
2209 vi_separate_dcc_stop_query(&rctx
->b
,
2210 rctx
->dcc_stats
[slot
].tex
);
2212 for (i
= 0; i
< ARRAY_SIZE(rctx
->dcc_stats
[slot
].ps_stats
); i
++)
2213 if (rctx
->dcc_stats
[slot
].ps_stats
[i
]) {
2214 rctx
->b
.destroy_query(&rctx
->b
,
2215 rctx
->dcc_stats
[slot
].ps_stats
[i
]);
2216 rctx
->dcc_stats
[slot
].ps_stats
[i
] = NULL
;
2219 r600_texture_reference(&rctx
->dcc_stats
[slot
].tex
, NULL
);
2223 * Return the per-context slot where DCC statistics queries for the texture live.
2225 static unsigned vi_get_context_dcc_stats_index(struct r600_common_context
*rctx
,
2226 struct r600_texture
*tex
)
2228 int i
, empty_slot
= -1;
2230 /* Remove zombie textures (textures kept alive by this array only). */
2231 for (i
= 0; i
< ARRAY_SIZE(rctx
->dcc_stats
); i
++)
2232 if (rctx
->dcc_stats
[i
].tex
&&
2233 rctx
->dcc_stats
[i
].tex
->resource
.b
.b
.reference
.count
== 1)
2234 vi_dcc_clean_up_context_slot(rctx
, i
);
2236 /* Find the texture. */
2237 for (i
= 0; i
< ARRAY_SIZE(rctx
->dcc_stats
); i
++) {
2238 /* Return if found. */
2239 if (rctx
->dcc_stats
[i
].tex
== tex
) {
2240 rctx
->dcc_stats
[i
].last_use_timestamp
= os_time_get();
2244 /* Record the first seen empty slot. */
2245 if (empty_slot
== -1 && !rctx
->dcc_stats
[i
].tex
)
2249 /* Not found. Remove the oldest member to make space in the array. */
2250 if (empty_slot
== -1) {
2251 int oldest_slot
= 0;
2253 /* Find the oldest slot. */
2254 for (i
= 1; i
< ARRAY_SIZE(rctx
->dcc_stats
); i
++)
2255 if (rctx
->dcc_stats
[oldest_slot
].last_use_timestamp
>
2256 rctx
->dcc_stats
[i
].last_use_timestamp
)
2259 /* Clean up the oldest slot. */
2260 vi_dcc_clean_up_context_slot(rctx
, oldest_slot
);
2261 empty_slot
= oldest_slot
;
2264 /* Add the texture to the new slot. */
2265 r600_texture_reference(&rctx
->dcc_stats
[empty_slot
].tex
, tex
);
2266 rctx
->dcc_stats
[empty_slot
].last_use_timestamp
= os_time_get();
2270 static struct pipe_query
*
2271 vi_create_resuming_pipestats_query(struct pipe_context
*ctx
)
2273 struct r600_query_hw
*query
= (struct r600_query_hw
*)
2274 ctx
->create_query(ctx
, PIPE_QUERY_PIPELINE_STATISTICS
, 0);
2276 query
->flags
|= R600_QUERY_HW_FLAG_BEGIN_RESUMES
;
2277 return (struct pipe_query
*)query
;
2281 * Called when binding a color buffer.
2283 void vi_separate_dcc_start_query(struct pipe_context
*ctx
,
2284 struct r600_texture
*tex
)
2286 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
2287 unsigned i
= vi_get_context_dcc_stats_index(rctx
, tex
);
2289 assert(!rctx
->dcc_stats
[i
].query_active
);
2291 if (!rctx
->dcc_stats
[i
].ps_stats
[0])
2292 rctx
->dcc_stats
[i
].ps_stats
[0] = vi_create_resuming_pipestats_query(ctx
);
2294 /* begin or resume the query */
2295 ctx
->begin_query(ctx
, rctx
->dcc_stats
[i
].ps_stats
[0]);
2296 rctx
->dcc_stats
[i
].query_active
= true;
2300 * Called when unbinding a color buffer.
2302 void vi_separate_dcc_stop_query(struct pipe_context
*ctx
,
2303 struct r600_texture
*tex
)
2305 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
2306 unsigned i
= vi_get_context_dcc_stats_index(rctx
, tex
);
2308 assert(rctx
->dcc_stats
[i
].query_active
);
2309 assert(rctx
->dcc_stats
[i
].ps_stats
[0]);
2311 /* pause or end the query */
2312 ctx
->end_query(ctx
, rctx
->dcc_stats
[i
].ps_stats
[0]);
2313 rctx
->dcc_stats
[i
].query_active
= false;
2316 static bool vi_should_enable_separate_dcc(struct r600_texture
*tex
)
2318 /* The minimum number of fullscreen draws per frame that is required
2320 return tex
->ps_draw_ratio
+ tex
->num_slow_clears
>= 5;
2323 /* Called by fast clear. */
2324 static void vi_separate_dcc_try_enable(struct r600_common_context
*rctx
,
2325 struct r600_texture
*tex
)
2327 /* The intent is to use this with shared displayable back buffers,
2328 * but it's not strictly limited only to them.
2330 if (!tex
->resource
.b
.is_shared
||
2331 !(tex
->resource
.external_usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
) ||
2332 tex
->resource
.b
.b
.target
!= PIPE_TEXTURE_2D
||
2333 tex
->resource
.b
.b
.last_level
> 0 ||
2334 !tex
->surface
.dcc_size
)
2337 if (tex
->dcc_offset
)
2338 return; /* already enabled */
2340 /* Enable the DCC stat gathering. */
2341 if (!tex
->dcc_gather_statistics
) {
2342 tex
->dcc_gather_statistics
= true;
2343 vi_separate_dcc_start_query(&rctx
->b
, tex
);
2346 if (!vi_should_enable_separate_dcc(tex
))
2347 return; /* stats show that DCC decompression is too expensive */
2349 assert(tex
->surface
.num_dcc_levels
);
2350 assert(!tex
->dcc_separate_buffer
);
2352 r600_texture_discard_cmask(rctx
->screen
, tex
);
2354 /* Get a DCC buffer. */
2355 if (tex
->last_dcc_separate_buffer
) {
2356 assert(tex
->dcc_gather_statistics
);
2357 assert(!tex
->dcc_separate_buffer
);
2358 tex
->dcc_separate_buffer
= tex
->last_dcc_separate_buffer
;
2359 tex
->last_dcc_separate_buffer
= NULL
;
2361 tex
->dcc_separate_buffer
= (struct r600_resource
*)
2362 r600_aligned_buffer_create(rctx
->b
.screen
,
2363 R600_RESOURCE_FLAG_UNMAPPABLE
,
2365 tex
->surface
.dcc_size
,
2366 tex
->surface
.dcc_alignment
);
2367 if (!tex
->dcc_separate_buffer
)
2371 /* dcc_offset is the absolute GPUVM address. */
2372 tex
->dcc_offset
= tex
->dcc_separate_buffer
->gpu_address
;
2374 /* no need to flag anything since this is called by fast clear that
2375 * flags framebuffer state
2380 * Called by pipe_context::flush_resource, the place where DCC decompression
2383 void vi_separate_dcc_process_and_reset_stats(struct pipe_context
*ctx
,
2384 struct r600_texture
*tex
)
2386 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
2387 struct pipe_query
*tmp
;
2388 unsigned i
= vi_get_context_dcc_stats_index(rctx
, tex
);
2389 bool query_active
= rctx
->dcc_stats
[i
].query_active
;
2390 bool disable
= false;
2392 if (rctx
->dcc_stats
[i
].ps_stats
[2]) {
2393 union pipe_query_result result
;
2395 /* Read the results. */
2396 ctx
->get_query_result(ctx
, rctx
->dcc_stats
[i
].ps_stats
[2],
2398 r600_query_hw_reset_buffers(rctx
,
2399 (struct r600_query_hw
*)
2400 rctx
->dcc_stats
[i
].ps_stats
[2]);
2402 /* Compute the approximate number of fullscreen draws. */
2403 tex
->ps_draw_ratio
=
2404 result
.pipeline_statistics
.ps_invocations
/
2405 (tex
->resource
.b
.b
.width0
* tex
->resource
.b
.b
.height0
);
2406 rctx
->last_tex_ps_draw_ratio
= tex
->ps_draw_ratio
;
2408 disable
= tex
->dcc_separate_buffer
&&
2409 !vi_should_enable_separate_dcc(tex
);
2412 tex
->num_slow_clears
= 0;
2414 /* stop the statistics query for ps_stats[0] */
2416 vi_separate_dcc_stop_query(ctx
, tex
);
2418 /* Move the queries in the queue by one. */
2419 tmp
= rctx
->dcc_stats
[i
].ps_stats
[2];
2420 rctx
->dcc_stats
[i
].ps_stats
[2] = rctx
->dcc_stats
[i
].ps_stats
[1];
2421 rctx
->dcc_stats
[i
].ps_stats
[1] = rctx
->dcc_stats
[i
].ps_stats
[0];
2422 rctx
->dcc_stats
[i
].ps_stats
[0] = tmp
;
2424 /* create and start a new query as ps_stats[0] */
2426 vi_separate_dcc_start_query(ctx
, tex
);
2429 assert(!tex
->last_dcc_separate_buffer
);
2430 tex
->last_dcc_separate_buffer
= tex
->dcc_separate_buffer
;
2431 tex
->dcc_separate_buffer
= NULL
;
2432 tex
->dcc_offset
= 0;
2433 /* no need to flag anything since this is called after
2434 * decompression that re-sets framebuffer state
2439 /* FAST COLOR CLEAR */
2441 static void evergreen_set_clear_color(struct r600_texture
*rtex
,
2442 enum pipe_format surface_format
,
2443 const union pipe_color_union
*color
)
2445 union util_color uc
;
2447 memset(&uc
, 0, sizeof(uc
));
2449 if (rtex
->surface
.bpe
== 16) {
2450 /* DCC fast clear only:
2451 * CLEAR_WORD0 = R = G = B
2454 assert(color
->ui
[0] == color
->ui
[1] &&
2455 color
->ui
[0] == color
->ui
[2]);
2456 uc
.ui
[0] = color
->ui
[0];
2457 uc
.ui
[1] = color
->ui
[3];
2458 } else if (util_format_is_pure_uint(surface_format
)) {
2459 util_format_write_4ui(surface_format
, color
->ui
, 0, &uc
, 0, 0, 0, 1, 1);
2460 } else if (util_format_is_pure_sint(surface_format
)) {
2461 util_format_write_4i(surface_format
, color
->i
, 0, &uc
, 0, 0, 0, 1, 1);
2463 util_pack_color(color
->f
, surface_format
, &uc
);
2466 memcpy(rtex
->color_clear_value
, &uc
, 2 * sizeof(uint32_t));
2469 static bool vi_get_fast_clear_parameters(enum pipe_format surface_format
,
2470 const union pipe_color_union
*color
,
2471 uint32_t* reset_value
,
2472 bool* clear_words_needed
)
2474 bool values
[4] = {};
2476 bool main_value
= false;
2477 bool extra_value
= false;
2480 /* This is needed to get the correct DCC clear value for luminance formats.
2481 * 1) Get the linear format (because the next step can't handle L8_SRGB).
2482 * 2) Convert luminance to red. (the real hw format for luminance)
2484 surface_format
= util_format_linear(surface_format
);
2485 surface_format
= util_format_luminance_to_red(surface_format
);
2487 const struct util_format_description
*desc
= util_format_description(surface_format
);
2489 if (desc
->block
.bits
== 128 &&
2490 (color
->ui
[0] != color
->ui
[1] ||
2491 color
->ui
[0] != color
->ui
[2]))
2494 *clear_words_needed
= true;
2495 *reset_value
= 0x20202020U
;
2497 /* If we want to clear without needing a fast clear eliminate step, we
2498 * can set each channel to 0 or 1 (or 0/max for integer formats). We
2499 * have two sets of flags, one for the last or first channel(extra) and
2500 * one for the other channels(main).
2503 if (surface_format
== PIPE_FORMAT_R11G11B10_FLOAT
||
2504 surface_format
== PIPE_FORMAT_B5G6R5_UNORM
||
2505 surface_format
== PIPE_FORMAT_B5G6R5_SRGB
||
2506 util_format_is_alpha(surface_format
)) {
2508 } else if (desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
) {
2509 if(r600_translate_colorswap(surface_format
, false) <= 1)
2510 extra_channel
= desc
->nr_channels
- 1;
2516 for (i
= 0; i
< 4; ++i
) {
2517 int index
= desc
->swizzle
[i
] - PIPE_SWIZZLE_X
;
2519 if (desc
->swizzle
[i
] < PIPE_SWIZZLE_X
||
2520 desc
->swizzle
[i
] > PIPE_SWIZZLE_W
)
2523 if (desc
->channel
[i
].pure_integer
&&
2524 desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
2525 /* Use the maximum value for clamping the clear color. */
2526 int max
= u_bit_consecutive(0, desc
->channel
[i
].size
- 1);
2528 values
[i
] = color
->i
[i
] != 0;
2529 if (color
->i
[i
] != 0 && MIN2(color
->i
[i
], max
) != max
)
2531 } else if (desc
->channel
[i
].pure_integer
&&
2532 desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
2533 /* Use the maximum value for clamping the clear color. */
2534 unsigned max
= u_bit_consecutive(0, desc
->channel
[i
].size
);
2536 values
[i
] = color
->ui
[i
] != 0U;
2537 if (color
->ui
[i
] != 0U && MIN2(color
->ui
[i
], max
) != max
)
2540 values
[i
] = color
->f
[i
] != 0.0F
;
2541 if (color
->f
[i
] != 0.0F
&& color
->f
[i
] != 1.0F
)
2545 if (index
== extra_channel
)
2546 extra_value
= values
[i
];
2548 main_value
= values
[i
];
2551 for (int i
= 0; i
< 4; ++i
)
2552 if (values
[i
] != main_value
&&
2553 desc
->swizzle
[i
] - PIPE_SWIZZLE_X
!= extra_channel
&&
2554 desc
->swizzle
[i
] >= PIPE_SWIZZLE_X
&&
2555 desc
->swizzle
[i
] <= PIPE_SWIZZLE_W
)
2558 *clear_words_needed
= false;
2560 *reset_value
|= 0x80808080U
;
2563 *reset_value
|= 0x40404040U
;
2567 void vi_dcc_clear_level(struct r600_common_context
*rctx
,
2568 struct r600_texture
*rtex
,
2569 unsigned level
, unsigned clear_value
)
2571 struct pipe_resource
*dcc_buffer
;
2572 uint64_t dcc_offset
, clear_size
;
2574 assert(vi_dcc_enabled(rtex
, level
));
2576 if (rtex
->dcc_separate_buffer
) {
2577 dcc_buffer
= &rtex
->dcc_separate_buffer
->b
.b
;
2580 dcc_buffer
= &rtex
->resource
.b
.b
;
2581 dcc_offset
= rtex
->dcc_offset
;
2584 if (rctx
->chip_class
>= GFX9
) {
2585 /* Mipmap level clears aren't implemented. */
2586 assert(rtex
->resource
.b
.b
.last_level
== 0);
2587 /* MSAA needs a different clear size. */
2588 assert(rtex
->resource
.b
.b
.nr_samples
<= 1);
2589 clear_size
= rtex
->surface
.dcc_size
;
2591 dcc_offset
+= rtex
->surface
.u
.legacy
.level
[level
].dcc_offset
;
2592 clear_size
= rtex
->surface
.u
.legacy
.level
[level
].dcc_fast_clear_size
;
2595 rctx
->clear_buffer(&rctx
->b
, dcc_buffer
, dcc_offset
, clear_size
,
2596 clear_value
, R600_COHERENCY_CB_META
);
2599 /* Set the same micro tile mode as the destination of the last MSAA resolve.
2600 * This allows hitting the MSAA resolve fast path, which requires that both
2601 * src and dst micro tile modes match.
2603 static void si_set_optimal_micro_tile_mode(struct r600_common_screen
*rscreen
,
2604 struct r600_texture
*rtex
)
2606 if (rtex
->resource
.b
.is_shared
||
2607 rtex
->resource
.b
.b
.nr_samples
<= 1 ||
2608 rtex
->surface
.micro_tile_mode
== rtex
->last_msaa_resolve_target_micro_mode
)
2611 assert(rscreen
->chip_class
>= GFX9
||
2612 rtex
->surface
.u
.legacy
.level
[0].mode
== RADEON_SURF_MODE_2D
);
2613 assert(rtex
->resource
.b
.b
.last_level
== 0);
2615 if (rscreen
->chip_class
>= GFX9
) {
2616 /* 4K or larger tiles only. 0 is linear. 1-3 are 256B tiles. */
2617 assert(rtex
->surface
.u
.gfx9
.surf
.swizzle_mode
>= 4);
2619 /* If you do swizzle_mode % 4, you'll get:
2625 * Depth-sample order isn't allowed:
2627 assert(rtex
->surface
.u
.gfx9
.surf
.swizzle_mode
% 4 != 0);
2629 switch (rtex
->last_msaa_resolve_target_micro_mode
) {
2630 case RADEON_MICRO_MODE_DISPLAY
:
2631 rtex
->surface
.u
.gfx9
.surf
.swizzle_mode
&= ~0x3;
2632 rtex
->surface
.u
.gfx9
.surf
.swizzle_mode
+= 2; /* D */
2634 case RADEON_MICRO_MODE_THIN
:
2635 rtex
->surface
.u
.gfx9
.surf
.swizzle_mode
&= ~0x3;
2636 rtex
->surface
.u
.gfx9
.surf
.swizzle_mode
+= 1; /* S */
2638 case RADEON_MICRO_MODE_ROTATED
:
2639 rtex
->surface
.u
.gfx9
.surf
.swizzle_mode
&= ~0x3;
2640 rtex
->surface
.u
.gfx9
.surf
.swizzle_mode
+= 3; /* R */
2642 default: /* depth */
2643 assert(!"unexpected micro mode");
2646 } else if (rscreen
->chip_class
>= CIK
) {
2647 /* These magic numbers were copied from addrlib. It doesn't use
2648 * any definitions for them either. They are all 2D_TILED_THIN1
2649 * modes with different bpp and micro tile mode.
2651 switch (rtex
->last_msaa_resolve_target_micro_mode
) {
2652 case RADEON_MICRO_MODE_DISPLAY
:
2653 rtex
->surface
.u
.legacy
.tiling_index
[0] = 10;
2655 case RADEON_MICRO_MODE_THIN
:
2656 rtex
->surface
.u
.legacy
.tiling_index
[0] = 14;
2658 case RADEON_MICRO_MODE_ROTATED
:
2659 rtex
->surface
.u
.legacy
.tiling_index
[0] = 28;
2661 default: /* depth, thick */
2662 assert(!"unexpected micro mode");
2666 switch (rtex
->last_msaa_resolve_target_micro_mode
) {
2667 case RADEON_MICRO_MODE_DISPLAY
:
2668 switch (rtex
->surface
.bpe
) {
2670 rtex
->surface
.u
.legacy
.tiling_index
[0] = 10;
2673 rtex
->surface
.u
.legacy
.tiling_index
[0] = 11;
2676 rtex
->surface
.u
.legacy
.tiling_index
[0] = 12;
2680 case RADEON_MICRO_MODE_THIN
:
2681 switch (rtex
->surface
.bpe
) {
2683 rtex
->surface
.u
.legacy
.tiling_index
[0] = 14;
2686 rtex
->surface
.u
.legacy
.tiling_index
[0] = 15;
2689 rtex
->surface
.u
.legacy
.tiling_index
[0] = 16;
2691 default: /* 8, 16 */
2692 rtex
->surface
.u
.legacy
.tiling_index
[0] = 17;
2696 default: /* depth, thick */
2697 assert(!"unexpected micro mode");
2702 rtex
->surface
.micro_tile_mode
= rtex
->last_msaa_resolve_target_micro_mode
;
2704 p_atomic_inc(&rscreen
->dirty_tex_counter
);
2707 void evergreen_do_fast_color_clear(struct r600_common_context
*rctx
,
2708 struct pipe_framebuffer_state
*fb
,
2709 struct r600_atom
*fb_state
,
2710 unsigned *buffers
, ubyte
*dirty_cbufs
,
2711 const union pipe_color_union
*color
)
2715 /* This function is broken in BE, so just disable this path for now */
2716 #ifdef PIPE_ARCH_BIG_ENDIAN
2720 if (rctx
->render_cond
)
2723 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
2724 struct r600_texture
*tex
;
2725 unsigned clear_bit
= PIPE_CLEAR_COLOR0
<< i
;
2730 /* if this colorbuffer is not being cleared */
2731 if (!(*buffers
& clear_bit
))
2734 tex
= (struct r600_texture
*)fb
->cbufs
[i
]->texture
;
2736 /* the clear is allowed if all layers are bound */
2737 if (fb
->cbufs
[i
]->u
.tex
.first_layer
!= 0 ||
2738 fb
->cbufs
[i
]->u
.tex
.last_layer
!= util_max_layer(&tex
->resource
.b
.b
, 0)) {
2742 /* cannot clear mipmapped textures */
2743 if (fb
->cbufs
[i
]->texture
->last_level
!= 0) {
2747 /* only supported on tiled surfaces */
2748 if (tex
->surface
.is_linear
) {
2752 /* shared textures can't use fast clear without an explicit flush,
2753 * because there is no way to communicate the clear color among
2756 if (tex
->resource
.b
.is_shared
&&
2757 !(tex
->resource
.external_usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
))
2760 /* fast color clear with 1D tiling doesn't work on old kernels and CIK */
2761 if (rctx
->chip_class
== CIK
&&
2762 tex
->surface
.u
.legacy
.level
[0].mode
== RADEON_SURF_MODE_1D
&&
2763 rctx
->screen
->info
.drm_major
== 2 &&
2764 rctx
->screen
->info
.drm_minor
< 38) {
2768 /* Fast clear is the most appropriate place to enable DCC for
2769 * displayable surfaces.
2771 if (rctx
->chip_class
>= VI
&&
2772 !(rctx
->screen
->debug_flags
& DBG_NO_DCC_FB
)) {
2773 vi_separate_dcc_try_enable(rctx
, tex
);
2775 /* RB+ isn't supported with a CMASK clear only on Stoney,
2776 * so all clears are considered to be hypothetically slow
2777 * clears, which is weighed when determining whether to
2778 * enable separate DCC.
2780 if (tex
->dcc_gather_statistics
&&
2781 rctx
->family
== CHIP_STONEY
)
2782 tex
->num_slow_clears
++;
2785 /* Try to clear DCC first, otherwise try CMASK. */
2786 if (vi_dcc_enabled(tex
, 0)) {
2787 uint32_t reset_value
;
2788 bool clear_words_needed
;
2790 if (rctx
->screen
->debug_flags
& DBG_NO_DCC_CLEAR
)
2793 if (!vi_get_fast_clear_parameters(fb
->cbufs
[i
]->format
,
2794 color
, &reset_value
,
2795 &clear_words_needed
))
2798 vi_dcc_clear_level(rctx
, tex
, 0, reset_value
);
2800 unsigned level_bit
= 1 << fb
->cbufs
[i
]->u
.tex
.level
;
2801 if (clear_words_needed
) {
2802 bool need_compressed_update
= !tex
->dirty_level_mask
;
2804 tex
->dirty_level_mask
|= level_bit
;
2806 if (need_compressed_update
)
2807 p_atomic_inc(&rctx
->screen
->compressed_colortex_counter
);
2809 tex
->separate_dcc_dirty
= true;
2811 /* 128-bit formats are unusupported */
2812 if (tex
->surface
.bpe
> 8) {
2816 /* RB+ doesn't work with CMASK fast clear on Stoney. */
2817 if (rctx
->family
== CHIP_STONEY
)
2820 /* ensure CMASK is enabled */
2821 r600_texture_alloc_cmask_separate(rctx
->screen
, tex
);
2822 if (tex
->cmask
.size
== 0) {
2826 /* Do the fast clear. */
2827 rctx
->clear_buffer(&rctx
->b
, &tex
->cmask_buffer
->b
.b
,
2828 tex
->cmask
.offset
, tex
->cmask
.size
, 0,
2829 R600_COHERENCY_CB_META
);
2831 bool need_compressed_update
= !tex
->dirty_level_mask
;
2833 tex
->dirty_level_mask
|= 1 << fb
->cbufs
[i
]->u
.tex
.level
;
2835 if (need_compressed_update
)
2836 p_atomic_inc(&rctx
->screen
->compressed_colortex_counter
);
2839 /* We can change the micro tile mode before a full clear. */
2840 if (rctx
->screen
->chip_class
>= SI
)
2841 si_set_optimal_micro_tile_mode(rctx
->screen
, tex
);
2843 evergreen_set_clear_color(tex
, fb
->cbufs
[i
]->format
, color
);
2846 *dirty_cbufs
|= 1 << i
;
2847 rctx
->set_atom_dirty(rctx
, fb_state
, true);
2848 *buffers
&= ~clear_bit
;
2852 void r600_init_screen_texture_functions(struct r600_common_screen
*rscreen
)
2854 rscreen
->b
.resource_from_handle
= r600_texture_from_handle
;
2855 rscreen
->b
.resource_get_handle
= r600_texture_get_handle
;
2858 void r600_init_context_texture_functions(struct r600_common_context
*rctx
)
2860 rctx
->b
.create_surface
= r600_create_surface
;
2861 rctx
->b
.surface_destroy
= r600_surface_destroy
;
2862 rctx
->b
.clear_texture
= r600_clear_texture
;