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 ((rsrc
->dcc_offset
&& src_level
< rsrc
->surface
.num_dcc_levels
) ||
76 (rdst
->dcc_offset
&& dst_level
< rdst
->surface
.num_dcc_levels
))
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_texture
*rtex
, unsigned level
,
181 const struct pipe_box
*box
)
183 return rtex
->surface
.level
[level
].offset
+
184 box
->z
* rtex
->surface
.level
[level
].slice_size
+
185 (box
->y
/ rtex
->surface
.blk_h
*
186 rtex
->surface
.level
[level
].nblk_x
+
187 box
->x
/ rtex
->surface
.blk_w
) * rtex
->surface
.bpe
;
190 static int r600_init_surface(struct r600_common_screen
*rscreen
,
191 struct radeon_surf
*surface
,
192 const struct pipe_resource
*ptex
,
193 enum radeon_surf_mode array_mode
,
194 unsigned pitch_in_bytes_override
,
198 bool is_flushed_depth
,
199 bool tc_compatible_htile
)
201 const struct util_format_description
*desc
=
202 util_format_description(ptex
->format
);
203 bool is_depth
, is_stencil
;
205 unsigned i
, bpe
, flags
= 0;
207 is_depth
= util_format_has_depth(desc
);
208 is_stencil
= util_format_has_stencil(desc
);
210 if (rscreen
->chip_class
>= EVERGREEN
&& !is_flushed_depth
&&
211 ptex
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
) {
212 bpe
= 4; /* stencil is allocated separately on evergreen */
214 bpe
= util_format_get_blocksize(ptex
->format
);
215 /* align byte per element on dword */
221 if (!is_flushed_depth
&& is_depth
) {
222 flags
|= RADEON_SURF_ZBUFFER
;
224 if (tc_compatible_htile
&&
225 array_mode
== RADEON_SURF_MODE_2D
) {
226 /* TC-compatible HTILE only supports Z32_FLOAT.
227 * Promote Z16 to Z32. DB->CB copies will convert
228 * the format for transfers.
231 flags
|= RADEON_SURF_TC_COMPATIBLE_HTILE
;
235 flags
|= RADEON_SURF_SBUFFER
;
238 if (rscreen
->chip_class
>= VI
&&
239 (ptex
->flags
& R600_RESOURCE_FLAG_DISABLE_DCC
||
240 ptex
->format
== PIPE_FORMAT_R9G9B9E5_FLOAT
))
241 flags
|= RADEON_SURF_DISABLE_DCC
;
243 if (ptex
->bind
& PIPE_BIND_SCANOUT
|| is_scanout
) {
244 /* This should catch bugs in gallium users setting incorrect flags. */
245 assert(ptex
->nr_samples
<= 1 &&
246 ptex
->array_size
== 1 &&
248 ptex
->last_level
== 0 &&
249 !(flags
& RADEON_SURF_Z_OR_SBUFFER
));
251 flags
|= RADEON_SURF_SCANOUT
;
255 flags
|= RADEON_SURF_IMPORTED
;
257 r
= rscreen
->ws
->surface_init(rscreen
->ws
, ptex
, flags
, bpe
,
258 array_mode
, surface
);
263 if (pitch_in_bytes_override
&&
264 pitch_in_bytes_override
!= surface
->level
[0].nblk_x
* bpe
) {
265 /* old ddx on evergreen over estimate alignment for 1d, only 1 level
268 surface
->level
[0].nblk_x
= pitch_in_bytes_override
/ bpe
;
269 surface
->level
[0].slice_size
= pitch_in_bytes_override
* surface
->level
[0].nblk_y
;
273 for (i
= 0; i
< ARRAY_SIZE(surface
->level
); ++i
)
274 surface
->level
[i
].offset
+= offset
;
279 static void r600_texture_init_metadata(struct r600_texture
*rtex
,
280 struct radeon_bo_metadata
*metadata
)
282 struct radeon_surf
*surface
= &rtex
->surface
;
284 memset(metadata
, 0, sizeof(*metadata
));
285 metadata
->microtile
= surface
->level
[0].mode
>= RADEON_SURF_MODE_1D
?
286 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
;
287 metadata
->macrotile
= surface
->level
[0].mode
>= RADEON_SURF_MODE_2D
?
288 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
;
289 metadata
->pipe_config
= surface
->pipe_config
;
290 metadata
->bankw
= surface
->bankw
;
291 metadata
->bankh
= surface
->bankh
;
292 metadata
->tile_split
= surface
->tile_split
;
293 metadata
->mtilea
= surface
->mtilea
;
294 metadata
->num_banks
= surface
->num_banks
;
295 metadata
->stride
= surface
->level
[0].nblk_x
* surface
->bpe
;
296 metadata
->scanout
= (surface
->flags
& RADEON_SURF_SCANOUT
) != 0;
299 static void r600_dirty_all_framebuffer_states(struct r600_common_screen
*rscreen
)
301 p_atomic_inc(&rscreen
->dirty_fb_counter
);
304 static void r600_eliminate_fast_color_clear(struct r600_common_context
*rctx
,
305 struct r600_texture
*rtex
)
307 struct r600_common_screen
*rscreen
= rctx
->screen
;
308 struct pipe_context
*ctx
= &rctx
->b
;
310 if (ctx
== rscreen
->aux_context
)
311 pipe_mutex_lock(rscreen
->aux_context_lock
);
313 ctx
->flush_resource(ctx
, &rtex
->resource
.b
.b
);
314 ctx
->flush(ctx
, NULL
, 0);
316 if (ctx
== rscreen
->aux_context
)
317 pipe_mutex_unlock(rscreen
->aux_context_lock
);
320 static void r600_texture_discard_cmask(struct r600_common_screen
*rscreen
,
321 struct r600_texture
*rtex
)
323 if (!rtex
->cmask
.size
)
326 assert(rtex
->resource
.b
.b
.nr_samples
<= 1);
329 memset(&rtex
->cmask
, 0, sizeof(rtex
->cmask
));
330 rtex
->cmask
.base_address_reg
= rtex
->resource
.gpu_address
>> 8;
331 rtex
->dirty_level_mask
= 0;
333 if (rscreen
->chip_class
>= SI
)
334 rtex
->cb_color_info
&= ~SI_S_028C70_FAST_CLEAR(1);
336 rtex
->cb_color_info
&= ~EG_S_028C70_FAST_CLEAR(1);
338 if (rtex
->cmask_buffer
!= &rtex
->resource
)
339 r600_resource_reference(&rtex
->cmask_buffer
, NULL
);
341 /* Notify all contexts about the change. */
342 r600_dirty_all_framebuffer_states(rscreen
);
343 p_atomic_inc(&rscreen
->compressed_colortex_counter
);
346 static bool r600_can_disable_dcc(struct r600_texture
*rtex
)
348 /* We can't disable DCC if it can be written by another process. */
349 return rtex
->dcc_offset
&&
350 (!rtex
->resource
.is_shared
||
351 !(rtex
->resource
.external_usage
& PIPE_HANDLE_USAGE_WRITE
));
354 static bool r600_texture_discard_dcc(struct r600_common_screen
*rscreen
,
355 struct r600_texture
*rtex
)
357 if (!r600_can_disable_dcc(rtex
))
360 assert(rtex
->dcc_separate_buffer
== NULL
);
363 rtex
->dcc_offset
= 0;
365 /* Notify all contexts about the change. */
366 r600_dirty_all_framebuffer_states(rscreen
);
371 * Disable DCC for the texture. (first decompress, then discard metadata).
373 * There is unresolved multi-context synchronization issue between
374 * screen::aux_context and the current context. If applications do this with
375 * multiple contexts, it's already undefined behavior for them and we don't
376 * have to worry about that. The scenario is:
378 * If context 1 disables DCC and context 2 has queued commands that write
379 * to the texture via CB with DCC enabled, and the order of operations is
381 * context 2 queues draw calls rendering to the texture, but doesn't flush
382 * context 1 disables DCC and flushes
383 * context 1 & 2 reset descriptors and FB state
384 * context 2 flushes (new compressed tiles written by the draw calls)
385 * context 1 & 2 read garbage, because DCC is disabled, yet there are
388 * \param rctx the current context if you have one, or rscreen->aux_context
391 bool r600_texture_disable_dcc(struct r600_common_context
*rctx
,
392 struct r600_texture
*rtex
)
394 struct r600_common_screen
*rscreen
= rctx
->screen
;
396 if (!r600_can_disable_dcc(rtex
))
399 if (&rctx
->b
== rscreen
->aux_context
)
400 pipe_mutex_lock(rscreen
->aux_context_lock
);
402 /* Decompress DCC. */
403 rctx
->decompress_dcc(&rctx
->b
, rtex
);
404 rctx
->b
.flush(&rctx
->b
, NULL
, 0);
406 if (&rctx
->b
== rscreen
->aux_context
)
407 pipe_mutex_unlock(rscreen
->aux_context_lock
);
409 return r600_texture_discard_dcc(rscreen
, rtex
);
412 static void r600_degrade_tile_mode_to_linear(struct r600_common_context
*rctx
,
413 struct r600_texture
*rtex
,
414 bool invalidate_storage
)
416 struct pipe_screen
*screen
= rctx
->b
.screen
;
417 struct r600_texture
*new_tex
;
418 struct pipe_resource templ
= rtex
->resource
.b
.b
;
421 templ
.bind
|= PIPE_BIND_LINEAR
;
423 /* r600g doesn't react to dirty_tex_descriptor_counter */
424 if (rctx
->chip_class
< SI
)
427 if (rtex
->resource
.is_shared
||
428 rtex
->surface
.is_linear
)
431 /* This fails with MSAA, depth, and compressed textures. */
432 if (r600_choose_tiling(rctx
->screen
, &templ
) !=
433 RADEON_SURF_MODE_LINEAR_ALIGNED
)
436 new_tex
= (struct r600_texture
*)screen
->resource_create(screen
, &templ
);
440 /* Copy the pixels to the new texture. */
441 if (!invalidate_storage
) {
442 for (i
= 0; i
<= templ
.last_level
; i
++) {
446 u_minify(templ
.width0
, i
), u_minify(templ
.height0
, i
),
447 util_max_layer(&templ
, i
) + 1, &box
);
449 rctx
->dma_copy(&rctx
->b
, &new_tex
->resource
.b
.b
, i
, 0, 0, 0,
450 &rtex
->resource
.b
.b
, i
, &box
);
454 r600_texture_discard_cmask(rctx
->screen
, rtex
);
455 r600_texture_discard_dcc(rctx
->screen
, rtex
);
457 /* Replace the structure fields of rtex. */
458 rtex
->resource
.b
.b
.bind
= templ
.bind
;
459 pb_reference(&rtex
->resource
.buf
, new_tex
->resource
.buf
);
460 rtex
->resource
.gpu_address
= new_tex
->resource
.gpu_address
;
461 rtex
->resource
.vram_usage
= new_tex
->resource
.vram_usage
;
462 rtex
->resource
.gart_usage
= new_tex
->resource
.gart_usage
;
463 rtex
->resource
.bo_size
= new_tex
->resource
.bo_size
;
464 rtex
->resource
.bo_alignment
= new_tex
->resource
.bo_alignment
;
465 rtex
->resource
.domains
= new_tex
->resource
.domains
;
466 rtex
->resource
.flags
= new_tex
->resource
.flags
;
467 rtex
->size
= new_tex
->size
;
468 rtex
->surface
= new_tex
->surface
;
469 rtex
->non_disp_tiling
= new_tex
->non_disp_tiling
;
470 rtex
->cb_color_info
= new_tex
->cb_color_info
;
471 rtex
->cmask
= new_tex
->cmask
; /* needed even without CMASK */
473 assert(!rtex
->htile_buffer
);
474 assert(!rtex
->cmask
.size
);
475 assert(!rtex
->fmask
.size
);
476 assert(!rtex
->dcc_offset
);
477 assert(!rtex
->is_depth
);
479 r600_texture_reference(&new_tex
, NULL
);
481 r600_dirty_all_framebuffer_states(rctx
->screen
);
482 p_atomic_inc(&rctx
->screen
->dirty_tex_descriptor_counter
);
485 static boolean
r600_texture_get_handle(struct pipe_screen
* screen
,
486 struct pipe_context
*ctx
,
487 struct pipe_resource
*resource
,
488 struct winsys_handle
*whandle
,
491 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)screen
;
492 struct r600_common_context
*rctx
= (struct r600_common_context
*)
493 (ctx
? ctx
: rscreen
->aux_context
);
494 struct r600_resource
*res
= (struct r600_resource
*)resource
;
495 struct r600_texture
*rtex
= (struct r600_texture
*)resource
;
496 struct radeon_bo_metadata metadata
;
497 bool update_metadata
= false;
499 /* This is not supported now, but it might be required for OpenCL
500 * interop in the future.
502 if (resource
->target
!= PIPE_BUFFER
&&
503 (resource
->nr_samples
> 1 || rtex
->is_depth
))
506 if (resource
->target
!= PIPE_BUFFER
) {
507 /* Since shader image stores don't support DCC on VI,
508 * disable it for external clients that want write
511 if (usage
& PIPE_HANDLE_USAGE_WRITE
&& rtex
->dcc_offset
) {
512 if (r600_texture_disable_dcc(rctx
, rtex
))
513 update_metadata
= true;
516 if (!(usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
) &&
517 (rtex
->cmask
.size
|| rtex
->dcc_offset
)) {
518 /* Eliminate fast clear (both CMASK and DCC) */
519 r600_eliminate_fast_color_clear(rctx
, rtex
);
521 /* Disable CMASK if flush_resource isn't going
524 if (rtex
->cmask
.size
)
525 r600_texture_discard_cmask(rscreen
, rtex
);
529 if (!res
->is_shared
|| update_metadata
) {
530 r600_texture_init_metadata(rtex
, &metadata
);
531 if (rscreen
->query_opaque_metadata
)
532 rscreen
->query_opaque_metadata(rscreen
, rtex
,
535 rscreen
->ws
->buffer_set_metadata(res
->buf
, &metadata
);
539 if (res
->is_shared
) {
540 /* USAGE_EXPLICIT_FLUSH must be cleared if at least one user
543 res
->external_usage
|= usage
& ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
;
544 if (!(usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
))
545 res
->external_usage
&= ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
;
547 res
->is_shared
= true;
548 res
->external_usage
= usage
;
551 return rscreen
->ws
->buffer_get_handle(res
->buf
,
552 rtex
->surface
.level
[0].nblk_x
*
554 rtex
->surface
.level
[0].offset
,
555 rtex
->surface
.level
[0].slice_size
,
559 static void r600_texture_destroy(struct pipe_screen
*screen
,
560 struct pipe_resource
*ptex
)
562 struct r600_texture
*rtex
= (struct r600_texture
*)ptex
;
563 struct r600_resource
*resource
= &rtex
->resource
;
565 r600_texture_reference(&rtex
->flushed_depth_texture
, NULL
);
567 r600_resource_reference(&rtex
->htile_buffer
, NULL
);
568 if (rtex
->cmask_buffer
!= &rtex
->resource
) {
569 r600_resource_reference(&rtex
->cmask_buffer
, NULL
);
571 pb_reference(&resource
->buf
, NULL
);
572 r600_resource_reference(&rtex
->dcc_separate_buffer
, NULL
);
573 r600_resource_reference(&rtex
->last_dcc_separate_buffer
, NULL
);
577 static const struct u_resource_vtbl r600_texture_vtbl
;
579 /* The number of samples can be specified independently of the texture. */
580 void r600_texture_get_fmask_info(struct r600_common_screen
*rscreen
,
581 struct r600_texture
*rtex
,
583 struct r600_fmask_info
*out
)
585 /* FMASK is allocated like an ordinary texture. */
586 struct pipe_resource templ
= rtex
->resource
.b
.b
;
587 struct radeon_surf fmask
= {};
590 memset(out
, 0, sizeof(*out
));
592 templ
.nr_samples
= 1;
593 flags
= rtex
->surface
.flags
| RADEON_SURF_FMASK
;
595 if (rscreen
->chip_class
<= CAYMAN
) {
596 /* Use the same parameters and tile mode. */
597 fmask
.bankw
= rtex
->surface
.bankw
;
598 fmask
.bankh
= rtex
->surface
.bankh
;
599 fmask
.mtilea
= rtex
->surface
.mtilea
;
600 fmask
.tile_split
= rtex
->surface
.tile_split
;
606 switch (nr_samples
) {
615 R600_ERR("Invalid sample count for FMASK allocation.\n");
619 /* Overallocate FMASK on R600-R700 to fix colorbuffer corruption.
620 * This can be fixed by writing a separate FMASK allocator specifically
621 * for R600-R700 asics. */
622 if (rscreen
->chip_class
<= R700
) {
626 if (rscreen
->ws
->surface_init(rscreen
->ws
, &templ
, flags
, bpe
,
627 RADEON_SURF_MODE_2D
, &fmask
)) {
628 R600_ERR("Got error in surface_init while allocating FMASK.\n");
632 assert(fmask
.level
[0].mode
== RADEON_SURF_MODE_2D
);
634 out
->slice_tile_max
= (fmask
.level
[0].nblk_x
* fmask
.level
[0].nblk_y
) / 64;
635 if (out
->slice_tile_max
)
636 out
->slice_tile_max
-= 1;
638 out
->tile_mode_index
= fmask
.tiling_index
[0];
639 out
->pitch_in_pixels
= fmask
.level
[0].nblk_x
;
640 out
->bank_height
= fmask
.bankh
;
641 out
->alignment
= MAX2(256, fmask
.surf_alignment
);
642 out
->size
= fmask
.surf_size
;
645 static void r600_texture_allocate_fmask(struct r600_common_screen
*rscreen
,
646 struct r600_texture
*rtex
)
648 r600_texture_get_fmask_info(rscreen
, rtex
,
649 rtex
->resource
.b
.b
.nr_samples
, &rtex
->fmask
);
651 rtex
->fmask
.offset
= align64(rtex
->size
, rtex
->fmask
.alignment
);
652 rtex
->size
= rtex
->fmask
.offset
+ rtex
->fmask
.size
;
655 void r600_texture_get_cmask_info(struct r600_common_screen
*rscreen
,
656 struct r600_texture
*rtex
,
657 struct r600_cmask_info
*out
)
659 unsigned cmask_tile_width
= 8;
660 unsigned cmask_tile_height
= 8;
661 unsigned cmask_tile_elements
= cmask_tile_width
* cmask_tile_height
;
662 unsigned element_bits
= 4;
663 unsigned cmask_cache_bits
= 1024;
664 unsigned num_pipes
= rscreen
->info
.num_tile_pipes
;
665 unsigned pipe_interleave_bytes
= rscreen
->info
.pipe_interleave_bytes
;
667 unsigned elements_per_macro_tile
= (cmask_cache_bits
/ element_bits
) * num_pipes
;
668 unsigned pixels_per_macro_tile
= elements_per_macro_tile
* cmask_tile_elements
;
669 unsigned sqrt_pixels_per_macro_tile
= sqrt(pixels_per_macro_tile
);
670 unsigned macro_tile_width
= util_next_power_of_two(sqrt_pixels_per_macro_tile
);
671 unsigned macro_tile_height
= pixels_per_macro_tile
/ macro_tile_width
;
673 unsigned pitch_elements
= align(rtex
->resource
.b
.b
.width0
, macro_tile_width
);
674 unsigned height
= align(rtex
->resource
.b
.b
.height0
, macro_tile_height
);
676 unsigned base_align
= num_pipes
* pipe_interleave_bytes
;
677 unsigned slice_bytes
=
678 ((pitch_elements
* height
* element_bits
+ 7) / 8) / cmask_tile_elements
;
680 assert(macro_tile_width
% 128 == 0);
681 assert(macro_tile_height
% 128 == 0);
683 out
->slice_tile_max
= ((pitch_elements
* height
) / (128*128)) - 1;
684 out
->alignment
= MAX2(256, base_align
);
685 out
->size
= (util_max_layer(&rtex
->resource
.b
.b
, 0) + 1) *
686 align(slice_bytes
, base_align
);
689 static void si_texture_get_cmask_info(struct r600_common_screen
*rscreen
,
690 struct r600_texture
*rtex
,
691 struct r600_cmask_info
*out
)
693 unsigned pipe_interleave_bytes
= rscreen
->info
.pipe_interleave_bytes
;
694 unsigned num_pipes
= rscreen
->info
.num_tile_pipes
;
695 unsigned cl_width
, cl_height
;
710 case 16: /* Hawaii */
719 unsigned base_align
= num_pipes
* pipe_interleave_bytes
;
721 unsigned width
= align(rtex
->resource
.b
.b
.width0
, cl_width
*8);
722 unsigned height
= align(rtex
->resource
.b
.b
.height0
, cl_height
*8);
723 unsigned slice_elements
= (width
* height
) / (8*8);
725 /* Each element of CMASK is a nibble. */
726 unsigned slice_bytes
= slice_elements
/ 2;
728 out
->slice_tile_max
= (width
* height
) / (128*128);
729 if (out
->slice_tile_max
)
730 out
->slice_tile_max
-= 1;
732 out
->alignment
= MAX2(256, base_align
);
733 out
->size
= (util_max_layer(&rtex
->resource
.b
.b
, 0) + 1) *
734 align(slice_bytes
, base_align
);
737 static void r600_texture_allocate_cmask(struct r600_common_screen
*rscreen
,
738 struct r600_texture
*rtex
)
740 if (rscreen
->chip_class
>= SI
) {
741 si_texture_get_cmask_info(rscreen
, rtex
, &rtex
->cmask
);
743 r600_texture_get_cmask_info(rscreen
, rtex
, &rtex
->cmask
);
746 rtex
->cmask
.offset
= align64(rtex
->size
, rtex
->cmask
.alignment
);
747 rtex
->size
= rtex
->cmask
.offset
+ rtex
->cmask
.size
;
749 if (rscreen
->chip_class
>= SI
)
750 rtex
->cb_color_info
|= SI_S_028C70_FAST_CLEAR(1);
752 rtex
->cb_color_info
|= EG_S_028C70_FAST_CLEAR(1);
755 static void r600_texture_alloc_cmask_separate(struct r600_common_screen
*rscreen
,
756 struct r600_texture
*rtex
)
758 if (rtex
->cmask_buffer
)
761 assert(rtex
->cmask
.size
== 0);
763 if (rscreen
->chip_class
>= SI
) {
764 si_texture_get_cmask_info(rscreen
, rtex
, &rtex
->cmask
);
766 r600_texture_get_cmask_info(rscreen
, rtex
, &rtex
->cmask
);
769 rtex
->cmask_buffer
= (struct r600_resource
*)
770 r600_aligned_buffer_create(&rscreen
->b
, 0, PIPE_USAGE_DEFAULT
,
772 rtex
->cmask
.alignment
);
773 if (rtex
->cmask_buffer
== NULL
) {
774 rtex
->cmask
.size
= 0;
778 /* update colorbuffer state bits */
779 rtex
->cmask
.base_address_reg
= rtex
->cmask_buffer
->gpu_address
>> 8;
781 if (rscreen
->chip_class
>= SI
)
782 rtex
->cb_color_info
|= SI_S_028C70_FAST_CLEAR(1);
784 rtex
->cb_color_info
|= EG_S_028C70_FAST_CLEAR(1);
786 p_atomic_inc(&rscreen
->compressed_colortex_counter
);
789 static void r600_texture_get_htile_size(struct r600_common_screen
*rscreen
,
790 struct r600_texture
*rtex
)
792 unsigned cl_width
, cl_height
, width
, height
;
793 unsigned slice_elements
, slice_bytes
, pipe_interleave_bytes
, base_align
;
794 unsigned num_pipes
= rscreen
->info
.num_tile_pipes
;
796 rtex
->surface
.htile_size
= 0;
798 if (rscreen
->chip_class
<= EVERGREEN
&&
799 rscreen
->info
.drm_major
== 2 && rscreen
->info
.drm_minor
< 26)
802 /* HW bug on R6xx. */
803 if (rscreen
->chip_class
== R600
&&
804 (rtex
->resource
.b
.b
.width0
> 7680 ||
805 rtex
->resource
.b
.b
.height0
> 7680))
808 /* HTILE is broken with 1D tiling on old kernels and CIK. */
809 if (rscreen
->chip_class
>= CIK
&&
810 rtex
->surface
.level
[0].mode
== RADEON_SURF_MODE_1D
&&
811 rscreen
->info
.drm_major
== 2 && rscreen
->info
.drm_minor
< 38)
814 /* Overalign HTILE on P2 configs to work around GPU hangs in
815 * piglit/depthstencil-render-miplevels 585.
817 * This has been confirmed to help Kabini & Stoney, where the hangs
818 * are always reproducible. I think I have seen the test hang
819 * on Carrizo too, though it was very rare there.
821 if (rscreen
->chip_class
>= CIK
&& num_pipes
< 4)
850 width
= align(rtex
->resource
.b
.b
.width0
, cl_width
* 8);
851 height
= align(rtex
->resource
.b
.b
.height0
, cl_height
* 8);
853 slice_elements
= (width
* height
) / (8 * 8);
854 slice_bytes
= slice_elements
* 4;
856 pipe_interleave_bytes
= rscreen
->info
.pipe_interleave_bytes
;
857 base_align
= num_pipes
* pipe_interleave_bytes
;
859 rtex
->surface
.htile_alignment
= base_align
;
860 rtex
->surface
.htile_size
=
861 (util_max_layer(&rtex
->resource
.b
.b
, 0) + 1) *
862 align(slice_bytes
, base_align
);
865 static void r600_texture_allocate_htile(struct r600_common_screen
*rscreen
,
866 struct r600_texture
*rtex
)
868 uint32_t clear_value
;
870 if (rtex
->tc_compatible_htile
) {
871 clear_value
= 0x0000030F;
873 r600_texture_get_htile_size(rscreen
, rtex
);
877 if (!rtex
->surface
.htile_size
)
880 rtex
->htile_buffer
= (struct r600_resource
*)
881 r600_aligned_buffer_create(&rscreen
->b
, 0, PIPE_USAGE_DEFAULT
,
882 rtex
->surface
.htile_size
,
883 rtex
->surface
.htile_alignment
);
884 if (rtex
->htile_buffer
== NULL
) {
885 /* this is not a fatal error as we can still keep rendering
886 * without htile buffer */
887 R600_ERR("Failed to create buffer object for htile buffer.\n");
889 r600_screen_clear_buffer(rscreen
, &rtex
->htile_buffer
->b
.b
,
890 0, rtex
->surface
.htile_size
,
891 clear_value
, R600_COHERENCY_NONE
);
895 void r600_print_texture_info(struct r600_texture
*rtex
, FILE *f
)
899 fprintf(f
, " Info: npix_x=%u, npix_y=%u, npix_z=%u, blk_w=%u, "
900 "blk_h=%u, array_size=%u, last_level=%u, "
901 "bpe=%u, nsamples=%u, flags=0x%x, %s\n",
902 rtex
->resource
.b
.b
.width0
, rtex
->resource
.b
.b
.height0
,
903 rtex
->resource
.b
.b
.depth0
, rtex
->surface
.blk_w
,
905 rtex
->resource
.b
.b
.array_size
, rtex
->resource
.b
.b
.last_level
,
906 rtex
->surface
.bpe
, rtex
->resource
.b
.b
.nr_samples
,
907 rtex
->surface
.flags
, util_format_short_name(rtex
->resource
.b
.b
.format
));
909 fprintf(f
, " Layout: size=%"PRIu64
", alignment=%u, bankw=%u, "
910 "bankh=%u, nbanks=%u, mtilea=%u, tilesplit=%u, pipeconfig=%u, scanout=%u\n",
911 rtex
->surface
.surf_size
, rtex
->surface
.surf_alignment
, rtex
->surface
.bankw
,
912 rtex
->surface
.bankh
, rtex
->surface
.num_banks
, rtex
->surface
.mtilea
,
913 rtex
->surface
.tile_split
, rtex
->surface
.pipe_config
,
914 (rtex
->surface
.flags
& RADEON_SURF_SCANOUT
) != 0);
916 if (rtex
->fmask
.size
)
917 fprintf(f
, " FMask: offset=%"PRIu64
", size=%"PRIu64
", alignment=%u, pitch_in_pixels=%u, "
918 "bankh=%u, slice_tile_max=%u, tile_mode_index=%u\n",
919 rtex
->fmask
.offset
, rtex
->fmask
.size
, rtex
->fmask
.alignment
,
920 rtex
->fmask
.pitch_in_pixels
, rtex
->fmask
.bank_height
,
921 rtex
->fmask
.slice_tile_max
, rtex
->fmask
.tile_mode_index
);
923 if (rtex
->cmask
.size
)
924 fprintf(f
, " CMask: offset=%"PRIu64
", size=%"PRIu64
", alignment=%u, "
925 "slice_tile_max=%u\n",
926 rtex
->cmask
.offset
, rtex
->cmask
.size
, rtex
->cmask
.alignment
,
927 rtex
->cmask
.slice_tile_max
);
929 if (rtex
->htile_buffer
)
930 fprintf(f
, " HTile: size=%u, alignment=%u, TC_compatible = %u\n",
931 rtex
->htile_buffer
->b
.b
.width0
,
932 rtex
->htile_buffer
->buf
->alignment
,
933 rtex
->tc_compatible_htile
);
935 if (rtex
->dcc_offset
) {
936 fprintf(f
, " DCC: offset=%"PRIu64
", size=%"PRIu64
", alignment=%u\n",
937 rtex
->dcc_offset
, rtex
->surface
.dcc_size
,
938 rtex
->surface
.dcc_alignment
);
939 for (i
= 0; i
<= rtex
->resource
.b
.b
.last_level
; i
++)
940 fprintf(f
, " DCCLevel[%i]: enabled=%u, offset=%"PRIu64
", "
941 "fast_clear_size=%"PRIu64
"\n",
942 i
, i
< rtex
->surface
.num_dcc_levels
,
943 rtex
->surface
.level
[i
].dcc_offset
,
944 rtex
->surface
.level
[i
].dcc_fast_clear_size
);
947 for (i
= 0; i
<= rtex
->resource
.b
.b
.last_level
; i
++)
948 fprintf(f
, " Level[%i]: offset=%"PRIu64
", slice_size=%"PRIu64
", "
949 "npix_x=%u, npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
950 "mode=%u, tiling_index = %u\n",
951 i
, rtex
->surface
.level
[i
].offset
,
952 rtex
->surface
.level
[i
].slice_size
,
953 u_minify(rtex
->resource
.b
.b
.width0
, i
),
954 u_minify(rtex
->resource
.b
.b
.height0
, i
),
955 u_minify(rtex
->resource
.b
.b
.depth0
, i
),
956 rtex
->surface
.level
[i
].nblk_x
,
957 rtex
->surface
.level
[i
].nblk_y
,
958 rtex
->surface
.level
[i
].mode
,
959 rtex
->surface
.tiling_index
[i
]);
961 if (rtex
->surface
.flags
& RADEON_SURF_SBUFFER
) {
962 fprintf(f
, " StencilLayout: tilesplit=%u\n",
963 rtex
->surface
.stencil_tile_split
);
964 for (i
= 0; i
<= rtex
->resource
.b
.b
.last_level
; i
++) {
965 fprintf(f
, " StencilLevel[%i]: offset=%"PRIu64
", "
966 "slice_size=%"PRIu64
", npix_x=%u, "
967 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
968 "mode=%u, tiling_index = %u\n",
969 i
, rtex
->surface
.stencil_level
[i
].offset
,
970 rtex
->surface
.stencil_level
[i
].slice_size
,
971 u_minify(rtex
->resource
.b
.b
.width0
, i
),
972 u_minify(rtex
->resource
.b
.b
.height0
, i
),
973 u_minify(rtex
->resource
.b
.b
.depth0
, i
),
974 rtex
->surface
.stencil_level
[i
].nblk_x
,
975 rtex
->surface
.stencil_level
[i
].nblk_y
,
976 rtex
->surface
.stencil_level
[i
].mode
,
977 rtex
->surface
.stencil_tiling_index
[i
]);
982 /* Common processing for r600_texture_create and r600_texture_from_handle */
983 static struct r600_texture
*
984 r600_texture_create_object(struct pipe_screen
*screen
,
985 const struct pipe_resource
*base
,
986 struct pb_buffer
*buf
,
987 struct radeon_surf
*surface
)
989 struct r600_texture
*rtex
;
990 struct r600_resource
*resource
;
991 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)screen
;
993 rtex
= CALLOC_STRUCT(r600_texture
);
997 resource
= &rtex
->resource
;
998 resource
->b
.b
= *base
;
999 resource
->b
.b
.next
= NULL
;
1000 resource
->b
.vtbl
= &r600_texture_vtbl
;
1001 pipe_reference_init(&resource
->b
.b
.reference
, 1);
1002 resource
->b
.b
.screen
= screen
;
1004 /* don't include stencil-only formats which we don't support for rendering */
1005 rtex
->is_depth
= util_format_has_depth(util_format_description(rtex
->resource
.b
.b
.format
));
1007 rtex
->surface
= *surface
;
1008 rtex
->size
= rtex
->surface
.surf_size
;
1010 rtex
->tc_compatible_htile
= rtex
->surface
.htile_size
!= 0;
1011 assert(!!(rtex
->surface
.flags
& RADEON_SURF_TC_COMPATIBLE_HTILE
) ==
1012 rtex
->tc_compatible_htile
);
1014 /* TC-compatible HTILE only supports Z32_FLOAT. */
1015 if (rtex
->tc_compatible_htile
)
1016 rtex
->db_render_format
= PIPE_FORMAT_Z32_FLOAT
;
1018 rtex
->db_render_format
= base
->format
;
1020 /* Tiled depth textures utilize the non-displayable tile order.
1021 * This must be done after r600_setup_surface.
1022 * Applies to R600-Cayman. */
1023 rtex
->non_disp_tiling
= rtex
->is_depth
&& rtex
->surface
.level
[0].mode
>= RADEON_SURF_MODE_1D
;
1024 /* Applies to GCN. */
1025 rtex
->last_msaa_resolve_target_micro_mode
= rtex
->surface
.micro_tile_mode
;
1027 /* Disable separate DCC at the beginning. DRI2 doesn't reuse buffers
1028 * between frames, so the only thing that can enable separate DCC
1029 * with DRI2 is multiple slow clears within a frame.
1031 rtex
->ps_draw_ratio
= 0;
1033 if (rtex
->is_depth
) {
1034 if (base
->flags
& (R600_RESOURCE_FLAG_TRANSFER
|
1035 R600_RESOURCE_FLAG_FLUSHED_DEPTH
) ||
1036 rscreen
->chip_class
>= EVERGREEN
) {
1037 rtex
->can_sample_z
= !rtex
->surface
.depth_adjusted
;
1038 rtex
->can_sample_s
= !rtex
->surface
.stencil_adjusted
;
1040 if (rtex
->resource
.b
.b
.nr_samples
<= 1 &&
1041 (rtex
->resource
.b
.b
.format
== PIPE_FORMAT_Z16_UNORM
||
1042 rtex
->resource
.b
.b
.format
== PIPE_FORMAT_Z32_FLOAT
))
1043 rtex
->can_sample_z
= true;
1046 if (!(base
->flags
& (R600_RESOURCE_FLAG_TRANSFER
|
1047 R600_RESOURCE_FLAG_FLUSHED_DEPTH
))) {
1048 rtex
->db_compatible
= true;
1050 if (!(rscreen
->debug_flags
& DBG_NO_HYPERZ
))
1051 r600_texture_allocate_htile(rscreen
, rtex
);
1054 if (base
->nr_samples
> 1) {
1056 r600_texture_allocate_fmask(rscreen
, rtex
);
1057 r600_texture_allocate_cmask(rscreen
, rtex
);
1058 rtex
->cmask_buffer
= &rtex
->resource
;
1060 if (!rtex
->fmask
.size
|| !rtex
->cmask
.size
) {
1066 /* Shared textures must always set up DCC here.
1067 * If it's not present, it will be disabled by
1068 * apply_opaque_metadata later.
1070 if (rtex
->surface
.dcc_size
&&
1071 (buf
|| !(rscreen
->debug_flags
& DBG_NO_DCC
)) &&
1072 !(rtex
->surface
.flags
& RADEON_SURF_SCANOUT
)) {
1073 /* Reserve space for the DCC buffer. */
1074 rtex
->dcc_offset
= align64(rtex
->size
, rtex
->surface
.dcc_alignment
);
1075 rtex
->size
= rtex
->dcc_offset
+ rtex
->surface
.dcc_size
;
1079 /* Now create the backing buffer. */
1081 r600_init_resource_fields(rscreen
, resource
, rtex
->size
,
1082 rtex
->surface
.surf_alignment
);
1084 resource
->flags
|= RADEON_FLAG_HANDLE
;
1086 if (!r600_alloc_resource(rscreen
, resource
)) {
1091 resource
->buf
= buf
;
1092 resource
->gpu_address
= rscreen
->ws
->buffer_get_virtual_address(resource
->buf
);
1093 resource
->bo_size
= buf
->size
;
1094 resource
->bo_alignment
= buf
->alignment
;
1095 resource
->domains
= rscreen
->ws
->buffer_get_initial_domain(resource
->buf
);
1096 if (resource
->domains
& RADEON_DOMAIN_VRAM
)
1097 resource
->vram_usage
= buf
->size
;
1098 else if (resource
->domains
& RADEON_DOMAIN_GTT
)
1099 resource
->gart_usage
= buf
->size
;
1102 if (rtex
->cmask
.size
) {
1103 /* Initialize the cmask to 0xCC (= compressed state). */
1104 r600_screen_clear_buffer(rscreen
, &rtex
->cmask_buffer
->b
.b
,
1105 rtex
->cmask
.offset
, rtex
->cmask
.size
,
1106 0xCCCCCCCC, R600_COHERENCY_NONE
);
1109 /* Initialize DCC only if the texture is not being imported. */
1110 if (!buf
&& rtex
->dcc_offset
) {
1111 r600_screen_clear_buffer(rscreen
, &rtex
->resource
.b
.b
,
1113 rtex
->surface
.dcc_size
,
1114 0xFFFFFFFF, R600_COHERENCY_NONE
);
1117 /* Initialize the CMASK base register value. */
1118 rtex
->cmask
.base_address_reg
=
1119 (rtex
->resource
.gpu_address
+ rtex
->cmask
.offset
) >> 8;
1121 if (rscreen
->debug_flags
& DBG_VM
) {
1122 fprintf(stderr
, "VM start=0x%"PRIX64
" end=0x%"PRIX64
" | Texture %ix%ix%i, %i levels, %i samples, %s\n",
1123 rtex
->resource
.gpu_address
,
1124 rtex
->resource
.gpu_address
+ rtex
->resource
.buf
->size
,
1125 base
->width0
, base
->height0
, util_max_layer(base
, 0)+1, base
->last_level
+1,
1126 base
->nr_samples
? base
->nr_samples
: 1, util_format_short_name(base
->format
));
1129 if (rscreen
->debug_flags
& DBG_TEX
) {
1131 r600_print_texture_info(rtex
, stdout
);
1138 static enum radeon_surf_mode
1139 r600_choose_tiling(struct r600_common_screen
*rscreen
,
1140 const struct pipe_resource
*templ
)
1142 const struct util_format_description
*desc
= util_format_description(templ
->format
);
1143 bool force_tiling
= templ
->flags
& R600_RESOURCE_FLAG_FORCE_TILING
;
1145 /* MSAA resources must be 2D tiled. */
1146 if (templ
->nr_samples
> 1)
1147 return RADEON_SURF_MODE_2D
;
1149 /* Transfer resources should be linear. */
1150 if (templ
->flags
& R600_RESOURCE_FLAG_TRANSFER
)
1151 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1153 /* r600g: force tiling on TEXTURE_2D and TEXTURE_3D compute resources. */
1154 if (rscreen
->chip_class
>= R600
&& rscreen
->chip_class
<= CAYMAN
&&
1155 (templ
->bind
& PIPE_BIND_COMPUTE_RESOURCE
) &&
1156 (templ
->target
== PIPE_TEXTURE_2D
||
1157 templ
->target
== PIPE_TEXTURE_3D
))
1158 force_tiling
= true;
1160 /* Handle common candidates for the linear mode.
1161 * Compressed textures and DB surfaces must always be tiled.
1163 if (!force_tiling
&& !util_format_is_compressed(templ
->format
) &&
1164 (!util_format_is_depth_or_stencil(templ
->format
) ||
1165 templ
->flags
& R600_RESOURCE_FLAG_FLUSHED_DEPTH
)) {
1166 if (rscreen
->debug_flags
& DBG_NO_TILING
)
1167 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1169 /* Tiling doesn't work with the 422 (SUBSAMPLED) formats on R600+. */
1170 if (desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
)
1171 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1173 /* Cursors are linear on SI.
1174 * (XXX double-check, maybe also use RADEON_SURF_SCANOUT) */
1175 if (rscreen
->chip_class
>= SI
&&
1176 (templ
->bind
& PIPE_BIND_CURSOR
))
1177 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1179 if (templ
->bind
& PIPE_BIND_LINEAR
)
1180 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1182 /* Textures with a very small height are recommended to be linear. */
1183 if (templ
->target
== PIPE_TEXTURE_1D
||
1184 templ
->target
== PIPE_TEXTURE_1D_ARRAY
||
1185 templ
->height0
<= 4)
1186 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1188 /* Textures likely to be mapped often. */
1189 if (templ
->usage
== PIPE_USAGE_STAGING
||
1190 templ
->usage
== PIPE_USAGE_STREAM
)
1191 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1194 /* Make small textures 1D tiled. */
1195 if (templ
->width0
<= 16 || templ
->height0
<= 16 ||
1196 (rscreen
->debug_flags
& DBG_NO_2D_TILING
))
1197 return RADEON_SURF_MODE_1D
;
1199 /* The allocator will switch to 1D if needed. */
1200 return RADEON_SURF_MODE_2D
;
1203 struct pipe_resource
*r600_texture_create(struct pipe_screen
*screen
,
1204 const struct pipe_resource
*templ
)
1206 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)screen
;
1207 struct radeon_surf surface
= {0};
1208 bool is_flushed_depth
= templ
->flags
& R600_RESOURCE_FLAG_FLUSHED_DEPTH
;
1209 bool tc_compatible_htile
=
1210 rscreen
->chip_class
>= VI
&&
1211 (templ
->flags
& PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY
) &&
1212 !(rscreen
->debug_flags
& DBG_NO_HYPERZ
) &&
1213 !is_flushed_depth
&&
1214 templ
->nr_samples
<= 1 && /* TC-compat HTILE is less efficient with MSAA */
1215 util_format_is_depth_or_stencil(templ
->format
);
1219 r
= r600_init_surface(rscreen
, &surface
, templ
,
1220 r600_choose_tiling(rscreen
, templ
), 0, 0,
1221 false, false, is_flushed_depth
,
1222 tc_compatible_htile
);
1227 return (struct pipe_resource
*)
1228 r600_texture_create_object(screen
, templ
, NULL
, &surface
);
1231 static struct pipe_resource
*r600_texture_from_handle(struct pipe_screen
*screen
,
1232 const struct pipe_resource
*templ
,
1233 struct winsys_handle
*whandle
,
1236 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)screen
;
1237 struct pb_buffer
*buf
= NULL
;
1238 unsigned stride
= 0, offset
= 0;
1239 unsigned array_mode
;
1240 struct radeon_surf surface
;
1242 struct radeon_bo_metadata metadata
= {};
1243 struct r600_texture
*rtex
;
1245 /* Support only 2D textures without mipmaps */
1246 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
) ||
1247 templ
->depth0
!= 1 || templ
->last_level
!= 0)
1250 buf
= rscreen
->ws
->buffer_from_handle(rscreen
->ws
, whandle
, &stride
, &offset
);
1254 rscreen
->ws
->buffer_get_metadata(buf
, &metadata
);
1256 surface
.pipe_config
= metadata
.pipe_config
;
1257 surface
.bankw
= metadata
.bankw
;
1258 surface
.bankh
= metadata
.bankh
;
1259 surface
.tile_split
= metadata
.tile_split
;
1260 surface
.mtilea
= metadata
.mtilea
;
1261 surface
.num_banks
= metadata
.num_banks
;
1263 if (metadata
.macrotile
== RADEON_LAYOUT_TILED
)
1264 array_mode
= RADEON_SURF_MODE_2D
;
1265 else if (metadata
.microtile
== RADEON_LAYOUT_TILED
)
1266 array_mode
= RADEON_SURF_MODE_1D
;
1268 array_mode
= RADEON_SURF_MODE_LINEAR_ALIGNED
;
1270 r
= r600_init_surface(rscreen
, &surface
, templ
, array_mode
, stride
,
1271 offset
, true, metadata
.scanout
, false, false);
1276 rtex
= r600_texture_create_object(screen
, templ
, buf
, &surface
);
1280 rtex
->resource
.is_shared
= true;
1281 rtex
->resource
.external_usage
= usage
;
1283 if (rscreen
->apply_opaque_metadata
)
1284 rscreen
->apply_opaque_metadata(rscreen
, rtex
, &metadata
);
1286 return &rtex
->resource
.b
.b
;
1289 bool r600_init_flushed_depth_texture(struct pipe_context
*ctx
,
1290 struct pipe_resource
*texture
,
1291 struct r600_texture
**staging
)
1293 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
1294 struct pipe_resource resource
;
1295 struct r600_texture
**flushed_depth_texture
= staging
?
1296 staging
: &rtex
->flushed_depth_texture
;
1297 enum pipe_format pipe_format
= texture
->format
;
1300 if (rtex
->flushed_depth_texture
)
1301 return true; /* it's ready */
1303 if (!rtex
->can_sample_z
&& rtex
->can_sample_s
) {
1304 switch (pipe_format
) {
1305 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1306 /* Save memory by not allocating the S plane. */
1307 pipe_format
= PIPE_FORMAT_Z32_FLOAT
;
1309 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1310 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1311 /* Save memory bandwidth by not copying the
1312 * stencil part during flush.
1314 * This potentially increases memory bandwidth
1315 * if an application uses both Z and S texturing
1316 * simultaneously (a flushed Z24S8 texture
1317 * would be stored compactly), but how often
1318 * does that really happen?
1320 pipe_format
= PIPE_FORMAT_Z24X8_UNORM
;
1324 } else if (!rtex
->can_sample_s
&& rtex
->can_sample_z
) {
1325 assert(util_format_has_stencil(util_format_description(pipe_format
)));
1327 /* DB->CB copies to an 8bpp surface don't work. */
1328 pipe_format
= PIPE_FORMAT_X24S8_UINT
;
1332 memset(&resource
, 0, sizeof(resource
));
1333 resource
.target
= texture
->target
;
1334 resource
.format
= pipe_format
;
1335 resource
.width0
= texture
->width0
;
1336 resource
.height0
= texture
->height0
;
1337 resource
.depth0
= texture
->depth0
;
1338 resource
.array_size
= texture
->array_size
;
1339 resource
.last_level
= texture
->last_level
;
1340 resource
.nr_samples
= texture
->nr_samples
;
1341 resource
.usage
= staging
? PIPE_USAGE_STAGING
: PIPE_USAGE_DEFAULT
;
1342 resource
.bind
= texture
->bind
& ~PIPE_BIND_DEPTH_STENCIL
;
1343 resource
.flags
= texture
->flags
| R600_RESOURCE_FLAG_FLUSHED_DEPTH
;
1346 resource
.flags
|= R600_RESOURCE_FLAG_TRANSFER
;
1348 *flushed_depth_texture
= (struct r600_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
1349 if (*flushed_depth_texture
== NULL
) {
1350 R600_ERR("failed to create temporary texture to hold flushed depth\n");
1354 (*flushed_depth_texture
)->non_disp_tiling
= false;
1359 * Initialize the pipe_resource descriptor to be of the same size as the box,
1360 * which is supposed to hold a subregion of the texture "orig" at the given
1363 static void r600_init_temp_resource_from_box(struct pipe_resource
*res
,
1364 struct pipe_resource
*orig
,
1365 const struct pipe_box
*box
,
1366 unsigned level
, unsigned flags
)
1368 memset(res
, 0, sizeof(*res
));
1369 res
->format
= orig
->format
;
1370 res
->width0
= box
->width
;
1371 res
->height0
= box
->height
;
1373 res
->array_size
= 1;
1374 res
->usage
= flags
& R600_RESOURCE_FLAG_TRANSFER
? PIPE_USAGE_STAGING
: PIPE_USAGE_DEFAULT
;
1377 /* We must set the correct texture target and dimensions for a 3D box. */
1378 if (box
->depth
> 1 && util_max_layer(orig
, level
) > 0) {
1379 res
->target
= PIPE_TEXTURE_2D_ARRAY
;
1380 res
->array_size
= box
->depth
;
1382 res
->target
= PIPE_TEXTURE_2D
;
1386 static bool r600_can_invalidate_texture(struct r600_common_screen
*rscreen
,
1387 struct r600_texture
*rtex
,
1388 unsigned transfer_usage
,
1389 const struct pipe_box
*box
)
1391 /* r600g doesn't react to dirty_tex_descriptor_counter */
1392 return rscreen
->chip_class
>= SI
&&
1393 !rtex
->resource
.is_shared
&&
1394 !(transfer_usage
& PIPE_TRANSFER_READ
) &&
1395 rtex
->resource
.b
.b
.last_level
== 0 &&
1396 util_texrange_covers_whole_level(&rtex
->resource
.b
.b
, 0,
1397 box
->x
, box
->y
, box
->z
,
1398 box
->width
, box
->height
,
1402 static void r600_texture_invalidate_storage(struct r600_common_context
*rctx
,
1403 struct r600_texture
*rtex
)
1405 struct r600_common_screen
*rscreen
= rctx
->screen
;
1407 /* There is no point in discarding depth and tiled buffers. */
1408 assert(!rtex
->is_depth
);
1409 assert(rtex
->surface
.is_linear
);
1411 /* Reallocate the buffer in the same pipe_resource. */
1412 r600_alloc_resource(rscreen
, &rtex
->resource
);
1414 /* Initialize the CMASK base address (needed even without CMASK). */
1415 rtex
->cmask
.base_address_reg
=
1416 (rtex
->resource
.gpu_address
+ rtex
->cmask
.offset
) >> 8;
1418 r600_dirty_all_framebuffer_states(rscreen
);
1419 p_atomic_inc(&rscreen
->dirty_tex_descriptor_counter
);
1421 rctx
->num_alloc_tex_transfer_bytes
+= rtex
->size
;
1424 static void *r600_texture_transfer_map(struct pipe_context
*ctx
,
1425 struct pipe_resource
*texture
,
1428 const struct pipe_box
*box
,
1429 struct pipe_transfer
**ptransfer
)
1431 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
1432 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
1433 struct r600_transfer
*trans
;
1434 struct r600_resource
*buf
;
1435 unsigned offset
= 0;
1437 bool use_staging_texture
= false;
1439 assert(!(texture
->flags
& R600_RESOURCE_FLAG_TRANSFER
));
1441 /* Depth textures use staging unconditionally. */
1442 if (!rtex
->is_depth
) {
1443 /* Degrade the tile mode if we get too many transfers on APUs.
1444 * On dGPUs, the staging texture is always faster.
1445 * Only count uploads that are at least 4x4 pixels large.
1447 if (!rctx
->screen
->info
.has_dedicated_vram
&&
1449 box
->width
>= 4 && box
->height
>= 4 &&
1450 p_atomic_inc_return(&rtex
->num_level0_transfers
) == 10) {
1451 bool can_invalidate
=
1452 r600_can_invalidate_texture(rctx
->screen
, rtex
,
1455 r600_degrade_tile_mode_to_linear(rctx
, rtex
,
1459 /* Tiled textures need to be converted into a linear texture for CPU
1460 * access. The staging texture is always linear and is placed in GART.
1462 * Reading from VRAM is slow, always use the staging texture in
1465 * Use the staging texture for uploads if the underlying BO
1468 if (!rtex
->surface
.is_linear
)
1469 use_staging_texture
= true;
1470 else if (usage
& PIPE_TRANSFER_READ
)
1471 use_staging_texture
= (rtex
->resource
.domains
&
1472 RADEON_DOMAIN_VRAM
) != 0;
1473 /* Write & linear only: */
1474 else if (r600_rings_is_buffer_referenced(rctx
, rtex
->resource
.buf
,
1475 RADEON_USAGE_READWRITE
) ||
1476 !rctx
->ws
->buffer_wait(rtex
->resource
.buf
, 0,
1477 RADEON_USAGE_READWRITE
)) {
1479 if (r600_can_invalidate_texture(rctx
->screen
, rtex
,
1481 r600_texture_invalidate_storage(rctx
, rtex
);
1483 use_staging_texture
= true;
1487 trans
= CALLOC_STRUCT(r600_transfer
);
1490 trans
->transfer
.resource
= texture
;
1491 trans
->transfer
.level
= level
;
1492 trans
->transfer
.usage
= usage
;
1493 trans
->transfer
.box
= *box
;
1495 if (rtex
->is_depth
) {
1496 struct r600_texture
*staging_depth
;
1498 if (rtex
->resource
.b
.b
.nr_samples
> 1) {
1499 /* MSAA depth buffers need to be converted to single sample buffers.
1501 * Mapping MSAA depth buffers can occur if ReadPixels is called
1502 * with a multisample GLX visual.
1504 * First downsample the depth buffer to a temporary texture,
1505 * then decompress the temporary one to staging.
1507 * Only the region being mapped is transfered.
1509 struct pipe_resource resource
;
1511 r600_init_temp_resource_from_box(&resource
, texture
, box
, level
, 0);
1513 if (!r600_init_flushed_depth_texture(ctx
, &resource
, &staging_depth
)) {
1514 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1519 if (usage
& PIPE_TRANSFER_READ
) {
1520 struct pipe_resource
*temp
= ctx
->screen
->resource_create(ctx
->screen
, &resource
);
1522 R600_ERR("failed to create a temporary depth texture\n");
1527 r600_copy_region_with_blit(ctx
, temp
, 0, 0, 0, 0, texture
, level
, box
);
1528 rctx
->blit_decompress_depth(ctx
, (struct r600_texture
*)temp
, staging_depth
,
1529 0, 0, 0, box
->depth
, 0, 0);
1530 pipe_resource_reference(&temp
, NULL
);
1534 /* XXX: only readback the rectangle which is being mapped? */
1535 /* XXX: when discard is true, no need to read back from depth texture */
1536 if (!r600_init_flushed_depth_texture(ctx
, texture
, &staging_depth
)) {
1537 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1542 rctx
->blit_decompress_depth(ctx
, rtex
, staging_depth
,
1544 box
->z
, box
->z
+ box
->depth
- 1,
1547 offset
= r600_texture_get_offset(staging_depth
, level
, box
);
1550 trans
->transfer
.stride
= staging_depth
->surface
.level
[level
].nblk_x
*
1551 staging_depth
->surface
.bpe
;
1552 trans
->transfer
.layer_stride
= staging_depth
->surface
.level
[level
].slice_size
;
1553 trans
->staging
= (struct r600_resource
*)staging_depth
;
1554 buf
= trans
->staging
;
1555 } else if (use_staging_texture
) {
1556 struct pipe_resource resource
;
1557 struct r600_texture
*staging
;
1559 r600_init_temp_resource_from_box(&resource
, texture
, box
, level
,
1560 R600_RESOURCE_FLAG_TRANSFER
);
1561 resource
.usage
= (usage
& PIPE_TRANSFER_READ
) ?
1562 PIPE_USAGE_STAGING
: PIPE_USAGE_STREAM
;
1564 /* Create the temporary texture. */
1565 staging
= (struct r600_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
1567 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1571 trans
->staging
= &staging
->resource
;
1572 trans
->transfer
.stride
= staging
->surface
.level
[0].nblk_x
*
1573 staging
->surface
.bpe
;
1574 trans
->transfer
.layer_stride
= staging
->surface
.level
[0].slice_size
;
1576 if (usage
& PIPE_TRANSFER_READ
)
1577 r600_copy_to_staging_texture(ctx
, trans
);
1579 usage
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
1581 buf
= trans
->staging
;
1583 /* the resource is mapped directly */
1584 trans
->transfer
.stride
= rtex
->surface
.level
[level
].nblk_x
*
1586 trans
->transfer
.layer_stride
= rtex
->surface
.level
[level
].slice_size
;
1587 offset
= r600_texture_get_offset(rtex
, level
, box
);
1588 buf
= &rtex
->resource
;
1591 if (!(map
= r600_buffer_map_sync_with_rings(rctx
, buf
, usage
))) {
1592 r600_resource_reference(&trans
->staging
, NULL
);
1597 *ptransfer
= &trans
->transfer
;
1598 return map
+ offset
;
1601 static void r600_texture_transfer_unmap(struct pipe_context
*ctx
,
1602 struct pipe_transfer
* transfer
)
1604 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
1605 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
1606 struct pipe_resource
*texture
= transfer
->resource
;
1607 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
1609 if ((transfer
->usage
& PIPE_TRANSFER_WRITE
) && rtransfer
->staging
) {
1610 if (rtex
->is_depth
&& rtex
->resource
.b
.b
.nr_samples
<= 1) {
1611 ctx
->resource_copy_region(ctx
, texture
, transfer
->level
,
1612 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
1613 &rtransfer
->staging
->b
.b
, transfer
->level
,
1616 r600_copy_from_staging_texture(ctx
, rtransfer
);
1620 if (rtransfer
->staging
) {
1621 rctx
->num_alloc_tex_transfer_bytes
+= rtransfer
->staging
->buf
->size
;
1622 r600_resource_reference(&rtransfer
->staging
, NULL
);
1625 /* Heuristic for {upload, draw, upload, draw, ..}:
1627 * Flush the gfx IB if we've allocated too much texture storage.
1629 * The idea is that we don't want to build IBs that use too much
1630 * memory and put pressure on the kernel memory manager and we also
1631 * want to make temporary and invalidated buffers go idle ASAP to
1632 * decrease the total memory usage or make them reusable. The memory
1633 * usage will be slightly higher than given here because of the buffer
1634 * cache in the winsys.
1636 * The result is that the kernel memory manager is never a bottleneck.
1638 if (rctx
->num_alloc_tex_transfer_bytes
> rctx
->screen
->info
.gart_size
/ 4) {
1639 rctx
->gfx
.flush(rctx
, RADEON_FLUSH_ASYNC
, NULL
);
1640 rctx
->num_alloc_tex_transfer_bytes
= 0;
1646 static const struct u_resource_vtbl r600_texture_vtbl
=
1648 NULL
, /* get_handle */
1649 r600_texture_destroy
, /* resource_destroy */
1650 r600_texture_transfer_map
, /* transfer_map */
1651 u_default_transfer_flush_region
, /* transfer_flush_region */
1652 r600_texture_transfer_unmap
, /* transfer_unmap */
1655 /* DCC channel type categories within which formats can be reinterpreted
1656 * while keeping the same DCC encoding. The swizzle must also match. */
1657 enum dcc_channel_type
{
1658 dcc_channel_float32
,
1661 dcc_channel_float16
,
1664 dcc_channel_uint_10_10_10_2
,
1667 dcc_channel_incompatible
,
1670 /* Return the type of DCC encoding. */
1671 static enum dcc_channel_type
1672 vi_get_dcc_channel_type(const struct util_format_description
*desc
)
1676 /* Find the first non-void channel. */
1677 for (i
= 0; i
< desc
->nr_channels
; i
++)
1678 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
)
1680 if (i
== desc
->nr_channels
)
1681 return dcc_channel_incompatible
;
1683 switch (desc
->channel
[i
].size
) {
1685 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_FLOAT
)
1686 return dcc_channel_float32
;
1687 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
)
1688 return dcc_channel_uint32
;
1689 return dcc_channel_sint32
;
1691 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_FLOAT
)
1692 return dcc_channel_float16
;
1693 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
)
1694 return dcc_channel_uint16
;
1695 return dcc_channel_sint16
;
1697 return dcc_channel_uint_10_10_10_2
;
1699 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
)
1700 return dcc_channel_uint8
;
1701 return dcc_channel_sint8
;
1703 return dcc_channel_incompatible
;
1707 /* Return if it's allowed to reinterpret one format as another with DCC enabled. */
1708 bool vi_dcc_formats_compatible(enum pipe_format format1
,
1709 enum pipe_format format2
)
1711 const struct util_format_description
*desc1
, *desc2
;
1712 enum dcc_channel_type type1
, type2
;
1715 if (format1
== format2
)
1718 desc1
= util_format_description(format1
);
1719 desc2
= util_format_description(format2
);
1721 if (desc1
->nr_channels
!= desc2
->nr_channels
)
1724 /* Swizzles must be the same. */
1725 for (i
= 0; i
< desc1
->nr_channels
; i
++)
1726 if (desc1
->swizzle
[i
] <= PIPE_SWIZZLE_W
&&
1727 desc2
->swizzle
[i
] <= PIPE_SWIZZLE_W
&&
1728 desc1
->swizzle
[i
] != desc2
->swizzle
[i
])
1731 type1
= vi_get_dcc_channel_type(desc1
);
1732 type2
= vi_get_dcc_channel_type(desc2
);
1734 return type1
!= dcc_channel_incompatible
&&
1735 type2
!= dcc_channel_incompatible
&&
1739 void vi_dcc_disable_if_incompatible_format(struct r600_common_context
*rctx
,
1740 struct pipe_resource
*tex
,
1742 enum pipe_format view_format
)
1744 struct r600_texture
*rtex
= (struct r600_texture
*)tex
;
1746 if (rtex
->dcc_offset
&&
1747 level
< rtex
->surface
.num_dcc_levels
&&
1748 !vi_dcc_formats_compatible(tex
->format
, view_format
))
1749 if (!r600_texture_disable_dcc(rctx
, (struct r600_texture
*)tex
))
1750 rctx
->decompress_dcc(&rctx
->b
, rtex
);
1753 struct pipe_surface
*r600_create_surface_custom(struct pipe_context
*pipe
,
1754 struct pipe_resource
*texture
,
1755 const struct pipe_surface
*templ
,
1756 unsigned width
, unsigned height
)
1758 struct r600_common_context
*rctx
= (struct r600_common_context
*)pipe
;
1759 struct r600_surface
*surface
= CALLOC_STRUCT(r600_surface
);
1764 assert(templ
->u
.tex
.first_layer
<= util_max_layer(texture
, templ
->u
.tex
.level
));
1765 assert(templ
->u
.tex
.last_layer
<= util_max_layer(texture
, templ
->u
.tex
.level
));
1767 pipe_reference_init(&surface
->base
.reference
, 1);
1768 pipe_resource_reference(&surface
->base
.texture
, texture
);
1769 surface
->base
.context
= pipe
;
1770 surface
->base
.format
= templ
->format
;
1771 surface
->base
.width
= width
;
1772 surface
->base
.height
= height
;
1773 surface
->base
.u
= templ
->u
;
1775 if (texture
->target
!= PIPE_BUFFER
)
1776 vi_dcc_disable_if_incompatible_format(rctx
, texture
,
1780 return &surface
->base
;
1783 static struct pipe_surface
*r600_create_surface(struct pipe_context
*pipe
,
1784 struct pipe_resource
*tex
,
1785 const struct pipe_surface
*templ
)
1787 unsigned level
= templ
->u
.tex
.level
;
1788 unsigned width
= u_minify(tex
->width0
, level
);
1789 unsigned height
= u_minify(tex
->height0
, level
);
1791 if (tex
->target
!= PIPE_BUFFER
&& templ
->format
!= tex
->format
) {
1792 const struct util_format_description
*tex_desc
1793 = util_format_description(tex
->format
);
1794 const struct util_format_description
*templ_desc
1795 = util_format_description(templ
->format
);
1797 assert(tex_desc
->block
.bits
== templ_desc
->block
.bits
);
1799 /* Adjust size of surface if and only if the block width or
1800 * height is changed. */
1801 if (tex_desc
->block
.width
!= templ_desc
->block
.width
||
1802 tex_desc
->block
.height
!= templ_desc
->block
.height
) {
1803 unsigned nblks_x
= util_format_get_nblocksx(tex
->format
, width
);
1804 unsigned nblks_y
= util_format_get_nblocksy(tex
->format
, height
);
1806 width
= nblks_x
* templ_desc
->block
.width
;
1807 height
= nblks_y
* templ_desc
->block
.height
;
1811 return r600_create_surface_custom(pipe
, tex
, templ
, width
, height
);
1814 static void r600_surface_destroy(struct pipe_context
*pipe
,
1815 struct pipe_surface
*surface
)
1817 struct r600_surface
*surf
= (struct r600_surface
*)surface
;
1818 r600_resource_reference(&surf
->cb_buffer_fmask
, NULL
);
1819 r600_resource_reference(&surf
->cb_buffer_cmask
, NULL
);
1820 pipe_resource_reference(&surface
->texture
, NULL
);
1824 static void r600_clear_texture(struct pipe_context
*pipe
,
1825 struct pipe_resource
*tex
,
1827 const struct pipe_box
*box
,
1830 struct pipe_screen
*screen
= pipe
->screen
;
1831 struct r600_texture
*rtex
= (struct r600_texture
*)tex
;
1832 struct pipe_surface tmpl
= {{0}};
1833 struct pipe_surface
*sf
;
1834 const struct util_format_description
*desc
=
1835 util_format_description(tex
->format
);
1837 tmpl
.format
= tex
->format
;
1838 tmpl
.u
.tex
.first_layer
= box
->z
;
1839 tmpl
.u
.tex
.last_layer
= box
->z
+ box
->depth
- 1;
1840 tmpl
.u
.tex
.level
= level
;
1841 sf
= pipe
->create_surface(pipe
, tex
, &tmpl
);
1845 if (rtex
->is_depth
) {
1848 uint8_t stencil
= 0;
1850 /* Depth is always present. */
1851 clear
= PIPE_CLEAR_DEPTH
;
1852 desc
->unpack_z_float(&depth
, 0, data
, 0, 1, 1);
1854 if (rtex
->surface
.flags
& RADEON_SURF_SBUFFER
) {
1855 clear
|= PIPE_CLEAR_STENCIL
;
1856 desc
->unpack_s_8uint(&stencil
, 0, data
, 0, 1, 1);
1859 pipe
->clear_depth_stencil(pipe
, sf
, clear
, depth
, stencil
,
1861 box
->width
, box
->height
, false);
1863 union pipe_color_union color
;
1865 /* pipe_color_union requires the full vec4 representation. */
1866 if (util_format_is_pure_uint(tex
->format
))
1867 desc
->unpack_rgba_uint(color
.ui
, 0, data
, 0, 1, 1);
1868 else if (util_format_is_pure_sint(tex
->format
))
1869 desc
->unpack_rgba_sint(color
.i
, 0, data
, 0, 1, 1);
1871 desc
->unpack_rgba_float(color
.f
, 0, data
, 0, 1, 1);
1873 if (screen
->is_format_supported(screen
, tex
->format
,
1875 PIPE_BIND_RENDER_TARGET
)) {
1876 pipe
->clear_render_target(pipe
, sf
, &color
,
1878 box
->width
, box
->height
, false);
1880 /* Software fallback - just for R9G9B9E5_FLOAT */
1881 util_clear_render_target(pipe
, sf
, &color
,
1883 box
->width
, box
->height
);
1886 pipe_surface_reference(&sf
, NULL
);
1889 unsigned r600_translate_colorswap(enum pipe_format format
, bool do_endian_swap
)
1891 const struct util_format_description
*desc
= util_format_description(format
);
1893 #define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == PIPE_SWIZZLE_##swz)
1895 if (format
== PIPE_FORMAT_R11G11B10_FLOAT
) /* isn't plain */
1896 return V_0280A0_SWAP_STD
;
1898 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1901 switch (desc
->nr_channels
) {
1903 if (HAS_SWIZZLE(0,X
))
1904 return V_0280A0_SWAP_STD
; /* X___ */
1905 else if (HAS_SWIZZLE(3,X
))
1906 return V_0280A0_SWAP_ALT_REV
; /* ___X */
1909 if ((HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(1,Y
)) ||
1910 (HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(1,NONE
)) ||
1911 (HAS_SWIZZLE(0,NONE
) && HAS_SWIZZLE(1,Y
)))
1912 return V_0280A0_SWAP_STD
; /* XY__ */
1913 else if ((HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(1,X
)) ||
1914 (HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(1,NONE
)) ||
1915 (HAS_SWIZZLE(0,NONE
) && HAS_SWIZZLE(1,X
)))
1917 return (do_endian_swap
? V_0280A0_SWAP_STD
: V_0280A0_SWAP_STD_REV
);
1918 else if (HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(3,Y
))
1919 return V_0280A0_SWAP_ALT
; /* X__Y */
1920 else if (HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(3,X
))
1921 return V_0280A0_SWAP_ALT_REV
; /* Y__X */
1924 if (HAS_SWIZZLE(0,X
))
1925 return (do_endian_swap
? V_0280A0_SWAP_STD_REV
: V_0280A0_SWAP_STD
);
1926 else if (HAS_SWIZZLE(0,Z
))
1927 return V_0280A0_SWAP_STD_REV
; /* ZYX */
1930 /* check the middle channels, the 1st and 4th channel can be NONE */
1931 if (HAS_SWIZZLE(1,Y
) && HAS_SWIZZLE(2,Z
)) {
1932 return V_0280A0_SWAP_STD
; /* XYZW */
1933 } else if (HAS_SWIZZLE(1,Z
) && HAS_SWIZZLE(2,Y
)) {
1934 return V_0280A0_SWAP_STD_REV
; /* WZYX */
1935 } else if (HAS_SWIZZLE(1,Y
) && HAS_SWIZZLE(2,X
)) {
1936 return V_0280A0_SWAP_ALT
; /* ZYXW */
1937 } else if (HAS_SWIZZLE(1,Z
) && HAS_SWIZZLE(2,W
)) {
1940 return V_0280A0_SWAP_ALT_REV
;
1942 return (do_endian_swap
? V_0280A0_SWAP_ALT
: V_0280A0_SWAP_ALT_REV
);
1949 /* PIPELINE_STAT-BASED DCC ENABLEMENT FOR DISPLAYABLE SURFACES */
1951 static void vi_dcc_clean_up_context_slot(struct r600_common_context
*rctx
,
1956 if (rctx
->dcc_stats
[slot
].query_active
)
1957 vi_separate_dcc_stop_query(&rctx
->b
,
1958 rctx
->dcc_stats
[slot
].tex
);
1960 for (i
= 0; i
< ARRAY_SIZE(rctx
->dcc_stats
[slot
].ps_stats
); i
++)
1961 if (rctx
->dcc_stats
[slot
].ps_stats
[i
]) {
1962 rctx
->b
.destroy_query(&rctx
->b
,
1963 rctx
->dcc_stats
[slot
].ps_stats
[i
]);
1964 rctx
->dcc_stats
[slot
].ps_stats
[i
] = NULL
;
1967 r600_texture_reference(&rctx
->dcc_stats
[slot
].tex
, NULL
);
1971 * Return the per-context slot where DCC statistics queries for the texture live.
1973 static unsigned vi_get_context_dcc_stats_index(struct r600_common_context
*rctx
,
1974 struct r600_texture
*tex
)
1976 int i
, empty_slot
= -1;
1978 /* Remove zombie textures (textures kept alive by this array only). */
1979 for (i
= 0; i
< ARRAY_SIZE(rctx
->dcc_stats
); i
++)
1980 if (rctx
->dcc_stats
[i
].tex
&&
1981 rctx
->dcc_stats
[i
].tex
->resource
.b
.b
.reference
.count
== 1)
1982 vi_dcc_clean_up_context_slot(rctx
, i
);
1984 /* Find the texture. */
1985 for (i
= 0; i
< ARRAY_SIZE(rctx
->dcc_stats
); i
++) {
1986 /* Return if found. */
1987 if (rctx
->dcc_stats
[i
].tex
== tex
) {
1988 rctx
->dcc_stats
[i
].last_use_timestamp
= os_time_get();
1992 /* Record the first seen empty slot. */
1993 if (empty_slot
== -1 && !rctx
->dcc_stats
[i
].tex
)
1997 /* Not found. Remove the oldest member to make space in the array. */
1998 if (empty_slot
== -1) {
1999 int oldest_slot
= 0;
2001 /* Find the oldest slot. */
2002 for (i
= 1; i
< ARRAY_SIZE(rctx
->dcc_stats
); i
++)
2003 if (rctx
->dcc_stats
[oldest_slot
].last_use_timestamp
>
2004 rctx
->dcc_stats
[i
].last_use_timestamp
)
2007 /* Clean up the oldest slot. */
2008 vi_dcc_clean_up_context_slot(rctx
, oldest_slot
);
2009 empty_slot
= oldest_slot
;
2012 /* Add the texture to the new slot. */
2013 r600_texture_reference(&rctx
->dcc_stats
[empty_slot
].tex
, tex
);
2014 rctx
->dcc_stats
[empty_slot
].last_use_timestamp
= os_time_get();
2018 static struct pipe_query
*
2019 vi_create_resuming_pipestats_query(struct pipe_context
*ctx
)
2021 struct r600_query_hw
*query
= (struct r600_query_hw
*)
2022 ctx
->create_query(ctx
, PIPE_QUERY_PIPELINE_STATISTICS
, 0);
2024 query
->flags
|= R600_QUERY_HW_FLAG_BEGIN_RESUMES
;
2025 return (struct pipe_query
*)query
;
2029 * Called when binding a color buffer.
2031 void vi_separate_dcc_start_query(struct pipe_context
*ctx
,
2032 struct r600_texture
*tex
)
2034 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
2035 unsigned i
= vi_get_context_dcc_stats_index(rctx
, tex
);
2037 assert(!rctx
->dcc_stats
[i
].query_active
);
2039 if (!rctx
->dcc_stats
[i
].ps_stats
[0])
2040 rctx
->dcc_stats
[i
].ps_stats
[0] = vi_create_resuming_pipestats_query(ctx
);
2042 /* begin or resume the query */
2043 ctx
->begin_query(ctx
, rctx
->dcc_stats
[i
].ps_stats
[0]);
2044 rctx
->dcc_stats
[i
].query_active
= true;
2048 * Called when unbinding a color buffer.
2050 void vi_separate_dcc_stop_query(struct pipe_context
*ctx
,
2051 struct r600_texture
*tex
)
2053 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
2054 unsigned i
= vi_get_context_dcc_stats_index(rctx
, tex
);
2056 assert(rctx
->dcc_stats
[i
].query_active
);
2057 assert(rctx
->dcc_stats
[i
].ps_stats
[0]);
2059 /* pause or end the query */
2060 ctx
->end_query(ctx
, rctx
->dcc_stats
[i
].ps_stats
[0]);
2061 rctx
->dcc_stats
[i
].query_active
= false;
2064 static bool vi_should_enable_separate_dcc(struct r600_texture
*tex
)
2066 /* The minimum number of fullscreen draws per frame that is required
2068 return tex
->ps_draw_ratio
+ tex
->num_slow_clears
>= 5;
2071 /* Called by fast clear. */
2072 static void vi_separate_dcc_try_enable(struct r600_common_context
*rctx
,
2073 struct r600_texture
*tex
)
2075 /* The intent is to use this with shared displayable back buffers,
2076 * but it's not strictly limited only to them.
2078 if (!tex
->resource
.is_shared
||
2079 !(tex
->resource
.external_usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
) ||
2080 tex
->resource
.b
.b
.target
!= PIPE_TEXTURE_2D
||
2081 tex
->resource
.b
.b
.last_level
> 0 ||
2082 !tex
->surface
.dcc_size
)
2085 if (tex
->dcc_offset
)
2086 return; /* already enabled */
2088 /* Enable the DCC stat gathering. */
2089 if (!tex
->dcc_gather_statistics
) {
2090 tex
->dcc_gather_statistics
= true;
2091 vi_separate_dcc_start_query(&rctx
->b
, tex
);
2094 if (!vi_should_enable_separate_dcc(tex
))
2095 return; /* stats show that DCC decompression is too expensive */
2097 assert(tex
->surface
.num_dcc_levels
);
2098 assert(!tex
->dcc_separate_buffer
);
2100 r600_texture_discard_cmask(rctx
->screen
, tex
);
2102 /* Get a DCC buffer. */
2103 if (tex
->last_dcc_separate_buffer
) {
2104 assert(tex
->dcc_gather_statistics
);
2105 assert(!tex
->dcc_separate_buffer
);
2106 tex
->dcc_separate_buffer
= tex
->last_dcc_separate_buffer
;
2107 tex
->last_dcc_separate_buffer
= NULL
;
2109 tex
->dcc_separate_buffer
= (struct r600_resource
*)
2110 r600_aligned_buffer_create(rctx
->b
.screen
, 0,
2112 tex
->surface
.dcc_size
,
2113 tex
->surface
.dcc_alignment
);
2114 if (!tex
->dcc_separate_buffer
)
2118 /* dcc_offset is the absolute GPUVM address. */
2119 tex
->dcc_offset
= tex
->dcc_separate_buffer
->gpu_address
;
2121 /* no need to flag anything since this is called by fast clear that
2122 * flags framebuffer state
2127 * Called by pipe_context::flush_resource, the place where DCC decompression
2130 void vi_separate_dcc_process_and_reset_stats(struct pipe_context
*ctx
,
2131 struct r600_texture
*tex
)
2133 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
2134 struct pipe_query
*tmp
;
2135 unsigned i
= vi_get_context_dcc_stats_index(rctx
, tex
);
2136 bool query_active
= rctx
->dcc_stats
[i
].query_active
;
2137 bool disable
= false;
2139 if (rctx
->dcc_stats
[i
].ps_stats
[2]) {
2140 union pipe_query_result result
;
2142 /* Read the results. */
2143 ctx
->get_query_result(ctx
, rctx
->dcc_stats
[i
].ps_stats
[2],
2145 r600_query_hw_reset_buffers(rctx
,
2146 (struct r600_query_hw
*)
2147 rctx
->dcc_stats
[i
].ps_stats
[2]);
2149 /* Compute the approximate number of fullscreen draws. */
2150 tex
->ps_draw_ratio
=
2151 result
.pipeline_statistics
.ps_invocations
/
2152 (tex
->resource
.b
.b
.width0
* tex
->resource
.b
.b
.height0
);
2153 rctx
->last_tex_ps_draw_ratio
= tex
->ps_draw_ratio
;
2155 disable
= tex
->dcc_separate_buffer
&&
2156 !vi_should_enable_separate_dcc(tex
);
2159 tex
->num_slow_clears
= 0;
2161 /* stop the statistics query for ps_stats[0] */
2163 vi_separate_dcc_stop_query(ctx
, tex
);
2165 /* Move the queries in the queue by one. */
2166 tmp
= rctx
->dcc_stats
[i
].ps_stats
[2];
2167 rctx
->dcc_stats
[i
].ps_stats
[2] = rctx
->dcc_stats
[i
].ps_stats
[1];
2168 rctx
->dcc_stats
[i
].ps_stats
[1] = rctx
->dcc_stats
[i
].ps_stats
[0];
2169 rctx
->dcc_stats
[i
].ps_stats
[0] = tmp
;
2171 /* create and start a new query as ps_stats[0] */
2173 vi_separate_dcc_start_query(ctx
, tex
);
2176 assert(!tex
->last_dcc_separate_buffer
);
2177 tex
->last_dcc_separate_buffer
= tex
->dcc_separate_buffer
;
2178 tex
->dcc_separate_buffer
= NULL
;
2179 tex
->dcc_offset
= 0;
2180 /* no need to flag anything since this is called after
2181 * decompression that re-sets framebuffer state
2186 /* FAST COLOR CLEAR */
2188 static void evergreen_set_clear_color(struct r600_texture
*rtex
,
2189 enum pipe_format surface_format
,
2190 const union pipe_color_union
*color
)
2192 union util_color uc
;
2194 memset(&uc
, 0, sizeof(uc
));
2196 if (rtex
->surface
.bpe
== 16) {
2197 /* DCC fast clear only:
2198 * CLEAR_WORD0 = R = G = B
2201 assert(color
->ui
[0] == color
->ui
[1] &&
2202 color
->ui
[0] == color
->ui
[2]);
2203 uc
.ui
[0] = color
->ui
[0];
2204 uc
.ui
[1] = color
->ui
[3];
2205 } else if (util_format_is_pure_uint(surface_format
)) {
2206 util_format_write_4ui(surface_format
, color
->ui
, 0, &uc
, 0, 0, 0, 1, 1);
2207 } else if (util_format_is_pure_sint(surface_format
)) {
2208 util_format_write_4i(surface_format
, color
->i
, 0, &uc
, 0, 0, 0, 1, 1);
2210 util_pack_color(color
->f
, surface_format
, &uc
);
2213 memcpy(rtex
->color_clear_value
, &uc
, 2 * sizeof(uint32_t));
2216 static bool vi_get_fast_clear_parameters(enum pipe_format surface_format
,
2217 const union pipe_color_union
*color
,
2218 uint32_t* reset_value
,
2219 bool* clear_words_needed
)
2221 bool values
[4] = {};
2223 bool main_value
= false;
2224 bool extra_value
= false;
2226 const struct util_format_description
*desc
= util_format_description(surface_format
);
2228 if (desc
->block
.bits
== 128 &&
2229 (color
->ui
[0] != color
->ui
[1] ||
2230 color
->ui
[0] != color
->ui
[2]))
2233 *clear_words_needed
= true;
2234 *reset_value
= 0x20202020U
;
2236 /* If we want to clear without needing a fast clear eliminate step, we
2237 * can set each channel to 0 or 1 (or 0/max for integer formats). We
2238 * have two sets of flags, one for the last or first channel(extra) and
2239 * one for the other channels(main).
2242 if (surface_format
== PIPE_FORMAT_R11G11B10_FLOAT
||
2243 surface_format
== PIPE_FORMAT_B5G6R5_UNORM
||
2244 surface_format
== PIPE_FORMAT_B5G6R5_SRGB
) {
2246 } else if (desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
) {
2247 if(r600_translate_colorswap(surface_format
, false) <= 1)
2248 extra_channel
= desc
->nr_channels
- 1;
2254 for (i
= 0; i
< 4; ++i
) {
2255 int index
= desc
->swizzle
[i
] - PIPE_SWIZZLE_X
;
2257 if (desc
->swizzle
[i
] < PIPE_SWIZZLE_X
||
2258 desc
->swizzle
[i
] > PIPE_SWIZZLE_W
)
2261 if (desc
->channel
[i
].pure_integer
&&
2262 desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
2263 /* Use the maximum value for clamping the clear color. */
2264 int max
= u_bit_consecutive(0, desc
->channel
[i
].size
- 1);
2266 values
[i
] = color
->i
[i
] != 0;
2267 if (color
->i
[i
] != 0 && MIN2(color
->i
[i
], max
) != max
)
2269 } else if (desc
->channel
[i
].pure_integer
&&
2270 desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
2271 /* Use the maximum value for clamping the clear color. */
2272 unsigned max
= u_bit_consecutive(0, desc
->channel
[i
].size
);
2274 values
[i
] = color
->ui
[i
] != 0U;
2275 if (color
->ui
[i
] != 0U && MIN2(color
->ui
[i
], max
) != max
)
2278 values
[i
] = color
->f
[i
] != 0.0F
;
2279 if (color
->f
[i
] != 0.0F
&& color
->f
[i
] != 1.0F
)
2283 if (index
== extra_channel
)
2284 extra_value
= values
[i
];
2286 main_value
= values
[i
];
2289 for (int i
= 0; i
< 4; ++i
)
2290 if (values
[i
] != main_value
&&
2291 desc
->swizzle
[i
] - PIPE_SWIZZLE_X
!= extra_channel
&&
2292 desc
->swizzle
[i
] >= PIPE_SWIZZLE_X
&&
2293 desc
->swizzle
[i
] <= PIPE_SWIZZLE_W
)
2296 *clear_words_needed
= false;
2298 *reset_value
|= 0x80808080U
;
2301 *reset_value
|= 0x40404040U
;
2305 void vi_dcc_clear_level(struct r600_common_context
*rctx
,
2306 struct r600_texture
*rtex
,
2307 unsigned level
, unsigned clear_value
)
2309 struct pipe_resource
*dcc_buffer
;
2310 uint64_t dcc_offset
;
2312 assert(rtex
->dcc_offset
&& level
< rtex
->surface
.num_dcc_levels
);
2314 if (rtex
->dcc_separate_buffer
) {
2315 dcc_buffer
= &rtex
->dcc_separate_buffer
->b
.b
;
2318 dcc_buffer
= &rtex
->resource
.b
.b
;
2319 dcc_offset
= rtex
->dcc_offset
;
2322 dcc_offset
+= rtex
->surface
.level
[level
].dcc_offset
;
2324 rctx
->clear_buffer(&rctx
->b
, dcc_buffer
, dcc_offset
,
2325 rtex
->surface
.level
[level
].dcc_fast_clear_size
,
2326 clear_value
, R600_COHERENCY_CB_META
);
2329 /* Set the same micro tile mode as the destination of the last MSAA resolve.
2330 * This allows hitting the MSAA resolve fast path, which requires that both
2331 * src and dst micro tile modes match.
2333 static void si_set_optimal_micro_tile_mode(struct r600_common_screen
*rscreen
,
2334 struct r600_texture
*rtex
)
2336 if (rtex
->resource
.is_shared
||
2337 rtex
->resource
.b
.b
.nr_samples
<= 1 ||
2338 rtex
->surface
.micro_tile_mode
== rtex
->last_msaa_resolve_target_micro_mode
)
2341 assert(rtex
->surface
.level
[0].mode
== RADEON_SURF_MODE_2D
);
2342 assert(rtex
->resource
.b
.b
.last_level
== 0);
2344 /* These magic numbers were copied from addrlib. It doesn't use any
2345 * definitions for them either. They are all 2D_TILED_THIN1 modes with
2346 * different bpp and micro tile mode.
2348 if (rscreen
->chip_class
>= CIK
) {
2349 switch (rtex
->last_msaa_resolve_target_micro_mode
) {
2350 case RADEON_MICRO_MODE_DISPLAY
:
2351 rtex
->surface
.tiling_index
[0] = 10;
2353 case RADEON_MICRO_MODE_THIN
:
2354 rtex
->surface
.tiling_index
[0] = 14;
2356 case RADEON_MICRO_MODE_ROTATED
:
2357 rtex
->surface
.tiling_index
[0] = 28;
2359 default: /* depth, thick */
2360 assert(!"unexpected micro mode");
2364 switch (rtex
->last_msaa_resolve_target_micro_mode
) {
2365 case RADEON_MICRO_MODE_DISPLAY
:
2366 switch (rtex
->surface
.bpe
) {
2368 rtex
->surface
.tiling_index
[0] = 10;
2371 rtex
->surface
.tiling_index
[0] = 11;
2374 rtex
->surface
.tiling_index
[0] = 12;
2378 case RADEON_MICRO_MODE_THIN
:
2379 switch (rtex
->surface
.bpe
) {
2381 rtex
->surface
.tiling_index
[0] = 14;
2384 rtex
->surface
.tiling_index
[0] = 15;
2387 rtex
->surface
.tiling_index
[0] = 16;
2389 default: /* 8, 16 */
2390 rtex
->surface
.tiling_index
[0] = 17;
2394 default: /* depth, thick */
2395 assert(!"unexpected micro mode");
2400 rtex
->surface
.micro_tile_mode
= rtex
->last_msaa_resolve_target_micro_mode
;
2402 p_atomic_inc(&rscreen
->dirty_fb_counter
);
2403 p_atomic_inc(&rscreen
->dirty_tex_descriptor_counter
);
2406 void evergreen_do_fast_color_clear(struct r600_common_context
*rctx
,
2407 struct pipe_framebuffer_state
*fb
,
2408 struct r600_atom
*fb_state
,
2409 unsigned *buffers
, unsigned *dirty_cbufs
,
2410 const union pipe_color_union
*color
)
2414 /* This function is broken in BE, so just disable this path for now */
2415 #ifdef PIPE_ARCH_BIG_ENDIAN
2419 if (rctx
->render_cond
)
2422 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
2423 struct r600_texture
*tex
;
2424 unsigned clear_bit
= PIPE_CLEAR_COLOR0
<< i
;
2429 /* if this colorbuffer is not being cleared */
2430 if (!(*buffers
& clear_bit
))
2433 tex
= (struct r600_texture
*)fb
->cbufs
[i
]->texture
;
2435 /* the clear is allowed if all layers are bound */
2436 if (fb
->cbufs
[i
]->u
.tex
.first_layer
!= 0 ||
2437 fb
->cbufs
[i
]->u
.tex
.last_layer
!= util_max_layer(&tex
->resource
.b
.b
, 0)) {
2441 /* cannot clear mipmapped textures */
2442 if (fb
->cbufs
[i
]->texture
->last_level
!= 0) {
2446 /* only supported on tiled surfaces */
2447 if (tex
->surface
.is_linear
) {
2451 /* shared textures can't use fast clear without an explicit flush,
2452 * because there is no way to communicate the clear color among
2455 if (tex
->resource
.is_shared
&&
2456 !(tex
->resource
.external_usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
))
2459 /* fast color clear with 1D tiling doesn't work on old kernels and CIK */
2460 if (rctx
->chip_class
== CIK
&&
2461 tex
->surface
.level
[0].mode
== RADEON_SURF_MODE_1D
&&
2462 rctx
->screen
->info
.drm_major
== 2 &&
2463 rctx
->screen
->info
.drm_minor
< 38) {
2467 /* Fast clear is the most appropriate place to enable DCC for
2468 * displayable surfaces.
2470 if (rctx
->chip_class
>= VI
&&
2471 !(rctx
->screen
->debug_flags
& DBG_NO_DCC_FB
)) {
2472 vi_separate_dcc_try_enable(rctx
, tex
);
2474 /* Stoney can't do a CMASK-based clear, so all clears are
2475 * considered to be hypothetically slow clears, which
2476 * is weighed when determining to enable separate DCC.
2478 if (tex
->dcc_gather_statistics
&&
2479 rctx
->family
== CHIP_STONEY
)
2480 tex
->num_slow_clears
++;
2483 /* Try to clear DCC first, otherwise try CMASK. */
2484 if (tex
->dcc_offset
&& tex
->surface
.num_dcc_levels
) {
2485 uint32_t reset_value
;
2486 bool clear_words_needed
;
2488 if (rctx
->screen
->debug_flags
& DBG_NO_DCC_CLEAR
)
2491 if (!vi_get_fast_clear_parameters(fb
->cbufs
[i
]->format
,
2492 color
, &reset_value
,
2493 &clear_words_needed
))
2496 vi_dcc_clear_level(rctx
, tex
, 0, reset_value
);
2498 if (clear_words_needed
)
2499 tex
->dirty_level_mask
|= 1 << fb
->cbufs
[i
]->u
.tex
.level
;
2500 tex
->separate_dcc_dirty
= true;
2502 /* 128-bit formats are unusupported */
2503 if (tex
->surface
.bpe
> 8) {
2507 /* Stoney/RB+ doesn't work with CMASK fast clear. */
2508 if (rctx
->family
== CHIP_STONEY
)
2511 /* ensure CMASK is enabled */
2512 r600_texture_alloc_cmask_separate(rctx
->screen
, tex
);
2513 if (tex
->cmask
.size
== 0) {
2517 /* Do the fast clear. */
2518 rctx
->clear_buffer(&rctx
->b
, &tex
->cmask_buffer
->b
.b
,
2519 tex
->cmask
.offset
, tex
->cmask
.size
, 0,
2520 R600_COHERENCY_CB_META
);
2522 tex
->dirty_level_mask
|= 1 << fb
->cbufs
[i
]->u
.tex
.level
;
2525 /* We can change the micro tile mode before a full clear. */
2526 if (rctx
->screen
->chip_class
>= SI
)
2527 si_set_optimal_micro_tile_mode(rctx
->screen
, tex
);
2529 evergreen_set_clear_color(tex
, fb
->cbufs
[i
]->format
, color
);
2532 *dirty_cbufs
|= 1 << i
;
2533 rctx
->set_atom_dirty(rctx
, fb_state
, true);
2534 *buffers
&= ~clear_bit
;
2538 void r600_init_screen_texture_functions(struct r600_common_screen
*rscreen
)
2540 rscreen
->b
.resource_from_handle
= r600_texture_from_handle
;
2541 rscreen
->b
.resource_get_handle
= r600_texture_get_handle
;
2544 void r600_init_context_texture_functions(struct r600_common_context
*rctx
)
2546 rctx
->b
.create_surface
= r600_create_surface
;
2547 rctx
->b
.surface_destroy
= r600_surface_destroy
;
2548 rctx
->b
.clear_texture
= r600_clear_texture
;