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 (util_format_get_blocksizebits(rdst
->resource
.b
.b
.format
) !=
57 util_format_get_blocksizebits(rsrc
->resource
.b
.b
.format
))
60 /* MSAA: Blits don't exist in the real world. */
61 if (rsrc
->resource
.b
.b
.nr_samples
> 1 ||
62 rdst
->resource
.b
.b
.nr_samples
> 1)
65 /* Depth-stencil surfaces:
66 * When dst is linear, the DB->CB copy preserves HTILE.
67 * When dst is tiled, the 3D path must be used to update HTILE.
69 if (rsrc
->is_depth
|| rdst
->is_depth
)
73 * src: Use the 3D path. DCC decompression is expensive.
74 * dst: Use the 3D path to compress the pixels with DCC.
76 if ((rsrc
->dcc_offset
&& rsrc
->surface
.level
[src_level
].dcc_enabled
) ||
77 (rdst
->dcc_offset
&& rdst
->surface
.level
[dst_level
].dcc_enabled
))
81 * src: Both texture and SDMA paths need decompression. Use SDMA.
82 * dst: If overwriting the whole texture, discard CMASK and use
83 * SDMA. Otherwise, use the 3D path.
85 if (rdst
->cmask
.size
&& rdst
->dirty_level_mask
& (1 << dst_level
)) {
86 /* The CMASK clear is only enabled for the first level. */
87 assert(dst_level
== 0);
88 if (!util_texrange_covers_whole_level(&rdst
->resource
.b
.b
, dst_level
,
89 dstx
, dsty
, dstz
, src_box
->width
,
90 src_box
->height
, src_box
->depth
))
93 r600_texture_discard_cmask(rctx
->screen
, rdst
);
96 /* All requirements are met. Prepare textures for SDMA. */
97 if (rsrc
->cmask
.size
&& rsrc
->dirty_level_mask
& (1 << src_level
))
98 rctx
->b
.flush_resource(&rctx
->b
, &rsrc
->resource
.b
.b
);
100 assert(!(rsrc
->dirty_level_mask
& (1 << src_level
)));
101 assert(!(rdst
->dirty_level_mask
& (1 << dst_level
)));
106 /* Same as resource_copy_region, except that both upsampling and downsampling are allowed. */
107 static void r600_copy_region_with_blit(struct pipe_context
*pipe
,
108 struct pipe_resource
*dst
,
110 unsigned dstx
, unsigned dsty
, unsigned dstz
,
111 struct pipe_resource
*src
,
113 const struct pipe_box
*src_box
)
115 struct pipe_blit_info blit
;
117 memset(&blit
, 0, sizeof(blit
));
118 blit
.src
.resource
= src
;
119 blit
.src
.format
= src
->format
;
120 blit
.src
.level
= src_level
;
121 blit
.src
.box
= *src_box
;
122 blit
.dst
.resource
= dst
;
123 blit
.dst
.format
= dst
->format
;
124 blit
.dst
.level
= dst_level
;
125 blit
.dst
.box
.x
= dstx
;
126 blit
.dst
.box
.y
= dsty
;
127 blit
.dst
.box
.z
= dstz
;
128 blit
.dst
.box
.width
= src_box
->width
;
129 blit
.dst
.box
.height
= src_box
->height
;
130 blit
.dst
.box
.depth
= src_box
->depth
;
131 blit
.mask
= util_format_get_mask(src
->format
) &
132 util_format_get_mask(dst
->format
);
133 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
136 pipe
->blit(pipe
, &blit
);
140 /* Copy from a full GPU texture to a transfer's staging one. */
141 static void r600_copy_to_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
143 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
144 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
145 struct pipe_resource
*dst
= &rtransfer
->staging
->b
.b
;
146 struct pipe_resource
*src
= transfer
->resource
;
148 if (src
->nr_samples
> 1) {
149 r600_copy_region_with_blit(ctx
, dst
, 0, 0, 0, 0,
150 src
, transfer
->level
, &transfer
->box
);
154 rctx
->dma_copy(ctx
, dst
, 0, 0, 0, 0, src
, transfer
->level
,
158 /* Copy from a transfer's staging texture to a full GPU one. */
159 static void r600_copy_from_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
161 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
162 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
163 struct pipe_resource
*dst
= transfer
->resource
;
164 struct pipe_resource
*src
= &rtransfer
->staging
->b
.b
;
165 struct pipe_box sbox
;
167 u_box_3d(0, 0, 0, transfer
->box
.width
, transfer
->box
.height
, transfer
->box
.depth
, &sbox
);
169 if (dst
->nr_samples
> 1) {
170 r600_copy_region_with_blit(ctx
, dst
, transfer
->level
,
171 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
176 rctx
->dma_copy(ctx
, dst
, transfer
->level
,
177 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
181 static unsigned r600_texture_get_offset(struct r600_texture
*rtex
, unsigned level
,
182 const struct pipe_box
*box
)
184 enum pipe_format format
= rtex
->resource
.b
.b
.format
;
186 return rtex
->surface
.level
[level
].offset
+
187 box
->z
* rtex
->surface
.level
[level
].slice_size
+
188 box
->y
/ util_format_get_blockheight(format
) * rtex
->surface
.level
[level
].pitch_bytes
+
189 box
->x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
192 static int r600_init_surface(struct r600_common_screen
*rscreen
,
193 struct radeon_surf
*surface
,
194 const struct pipe_resource
*ptex
,
195 enum radeon_surf_mode array_mode
,
196 unsigned pitch_in_bytes_override
,
200 bool is_flushed_depth
,
201 bool tc_compatible_htile
)
203 const struct util_format_description
*desc
=
204 util_format_description(ptex
->format
);
205 bool is_depth
, is_stencil
;
207 unsigned i
, bpe
, flags
= 0;
209 is_depth
= util_format_has_depth(desc
);
210 is_stencil
= util_format_has_stencil(desc
);
212 if (rscreen
->chip_class
>= EVERGREEN
&& !is_flushed_depth
&&
213 ptex
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
) {
214 bpe
= 4; /* stencil is allocated separately on evergreen */
216 bpe
= util_format_get_blocksize(ptex
->format
);
217 /* align byte per element on dword */
223 if (!is_flushed_depth
&& is_depth
) {
224 flags
|= RADEON_SURF_ZBUFFER
;
226 if (tc_compatible_htile
&&
227 array_mode
== RADEON_SURF_MODE_2D
) {
228 /* TC-compatible HTILE only supports Z32_FLOAT.
229 * Promote Z16 to Z32. DB->CB copies will convert
230 * the format for transfers.
233 flags
|= RADEON_SURF_TC_COMPATIBLE_HTILE
;
237 flags
|= RADEON_SURF_SBUFFER
;
240 if (rscreen
->chip_class
>= VI
&&
241 (ptex
->flags
& R600_RESOURCE_FLAG_DISABLE_DCC
||
242 ptex
->format
== PIPE_FORMAT_R9G9B9E5_FLOAT
))
243 flags
|= RADEON_SURF_DISABLE_DCC
;
245 if (ptex
->bind
& PIPE_BIND_SCANOUT
|| is_scanout
) {
246 /* This should catch bugs in gallium users setting incorrect flags. */
247 assert(ptex
->nr_samples
<= 1 &&
248 ptex
->array_size
== 1 &&
250 ptex
->last_level
== 0 &&
251 !(flags
& RADEON_SURF_Z_OR_SBUFFER
));
253 flags
|= RADEON_SURF_SCANOUT
;
257 flags
|= RADEON_SURF_IMPORTED
;
259 r
= rscreen
->ws
->surface_init(rscreen
->ws
, ptex
, flags
, bpe
,
260 array_mode
, surface
);
265 if (pitch_in_bytes_override
&& pitch_in_bytes_override
!= surface
->level
[0].pitch_bytes
) {
266 /* old ddx on evergreen over estimate alignment for 1d, only 1 level
269 surface
->level
[0].nblk_x
= pitch_in_bytes_override
/ bpe
;
270 surface
->level
[0].pitch_bytes
= pitch_in_bytes_override
;
271 surface
->level
[0].slice_size
= pitch_in_bytes_override
* surface
->level
[0].nblk_y
;
275 for (i
= 0; i
< ARRAY_SIZE(surface
->level
); ++i
)
276 surface
->level
[i
].offset
+= offset
;
281 static void r600_texture_init_metadata(struct r600_texture
*rtex
,
282 struct radeon_bo_metadata
*metadata
)
284 struct radeon_surf
*surface
= &rtex
->surface
;
286 memset(metadata
, 0, sizeof(*metadata
));
287 metadata
->microtile
= surface
->level
[0].mode
>= RADEON_SURF_MODE_1D
?
288 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
;
289 metadata
->macrotile
= surface
->level
[0].mode
>= RADEON_SURF_MODE_2D
?
290 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
;
291 metadata
->pipe_config
= surface
->pipe_config
;
292 metadata
->bankw
= surface
->bankw
;
293 metadata
->bankh
= surface
->bankh
;
294 metadata
->tile_split
= surface
->tile_split
;
295 metadata
->mtilea
= surface
->mtilea
;
296 metadata
->num_banks
= surface
->num_banks
;
297 metadata
->stride
= surface
->level
[0].pitch_bytes
;
298 metadata
->scanout
= (surface
->flags
& RADEON_SURF_SCANOUT
) != 0;
301 static void r600_dirty_all_framebuffer_states(struct r600_common_screen
*rscreen
)
303 p_atomic_inc(&rscreen
->dirty_fb_counter
);
306 static void r600_eliminate_fast_color_clear(struct r600_common_context
*rctx
,
307 struct r600_texture
*rtex
)
309 struct r600_common_screen
*rscreen
= rctx
->screen
;
310 struct pipe_context
*ctx
= &rctx
->b
;
312 if (ctx
== rscreen
->aux_context
)
313 pipe_mutex_lock(rscreen
->aux_context_lock
);
315 ctx
->flush_resource(ctx
, &rtex
->resource
.b
.b
);
316 ctx
->flush(ctx
, NULL
, 0);
318 if (ctx
== rscreen
->aux_context
)
319 pipe_mutex_unlock(rscreen
->aux_context_lock
);
322 static void r600_texture_discard_cmask(struct r600_common_screen
*rscreen
,
323 struct r600_texture
*rtex
)
325 if (!rtex
->cmask
.size
)
328 assert(rtex
->resource
.b
.b
.nr_samples
<= 1);
331 memset(&rtex
->cmask
, 0, sizeof(rtex
->cmask
));
332 rtex
->cmask
.base_address_reg
= rtex
->resource
.gpu_address
>> 8;
333 rtex
->dirty_level_mask
= 0;
335 if (rscreen
->chip_class
>= SI
)
336 rtex
->cb_color_info
&= ~SI_S_028C70_FAST_CLEAR(1);
338 rtex
->cb_color_info
&= ~EG_S_028C70_FAST_CLEAR(1);
340 if (rtex
->cmask_buffer
!= &rtex
->resource
)
341 r600_resource_reference(&rtex
->cmask_buffer
, NULL
);
343 /* Notify all contexts about the change. */
344 r600_dirty_all_framebuffer_states(rscreen
);
345 p_atomic_inc(&rscreen
->compressed_colortex_counter
);
348 static bool r600_can_disable_dcc(struct r600_texture
*rtex
)
350 /* We can't disable DCC if it can be written by another process. */
351 return rtex
->dcc_offset
&&
352 (!rtex
->resource
.is_shared
||
353 !(rtex
->resource
.external_usage
& PIPE_HANDLE_USAGE_WRITE
));
356 static bool r600_texture_discard_dcc(struct r600_common_screen
*rscreen
,
357 struct r600_texture
*rtex
)
359 if (!r600_can_disable_dcc(rtex
))
362 assert(rtex
->dcc_separate_buffer
== NULL
);
365 rtex
->dcc_offset
= 0;
367 /* Notify all contexts about the change. */
368 r600_dirty_all_framebuffer_states(rscreen
);
373 * Disable DCC for the texture. (first decompress, then discard metadata).
375 * There is unresolved multi-context synchronization issue between
376 * screen::aux_context and the current context. If applications do this with
377 * multiple contexts, it's already undefined behavior for them and we don't
378 * have to worry about that. The scenario is:
380 * If context 1 disables DCC and context 2 has queued commands that write
381 * to the texture via CB with DCC enabled, and the order of operations is
383 * context 2 queues draw calls rendering to the texture, but doesn't flush
384 * context 1 disables DCC and flushes
385 * context 1 & 2 reset descriptors and FB state
386 * context 2 flushes (new compressed tiles written by the draw calls)
387 * context 1 & 2 read garbage, because DCC is disabled, yet there are
390 * \param rctx the current context if you have one, or rscreen->aux_context
393 bool r600_texture_disable_dcc(struct r600_common_context
*rctx
,
394 struct r600_texture
*rtex
)
396 struct r600_common_screen
*rscreen
= rctx
->screen
;
398 if (!r600_can_disable_dcc(rtex
))
401 if (&rctx
->b
== rscreen
->aux_context
)
402 pipe_mutex_lock(rscreen
->aux_context_lock
);
404 /* Decompress DCC. */
405 rctx
->decompress_dcc(&rctx
->b
, rtex
);
406 rctx
->b
.flush(&rctx
->b
, NULL
, 0);
408 if (&rctx
->b
== rscreen
->aux_context
)
409 pipe_mutex_unlock(rscreen
->aux_context_lock
);
411 return r600_texture_discard_dcc(rscreen
, rtex
);
414 static void r600_degrade_tile_mode_to_linear(struct r600_common_context
*rctx
,
415 struct r600_texture
*rtex
,
416 bool invalidate_storage
)
418 struct pipe_screen
*screen
= rctx
->b
.screen
;
419 struct r600_texture
*new_tex
;
420 struct pipe_resource templ
= rtex
->resource
.b
.b
;
423 templ
.bind
|= PIPE_BIND_LINEAR
;
425 /* r600g doesn't react to dirty_tex_descriptor_counter */
426 if (rctx
->chip_class
< SI
)
429 if (rtex
->resource
.is_shared
||
430 rtex
->surface
.level
[0].mode
== RADEON_SURF_MODE_LINEAR_ALIGNED
)
433 /* This fails with MSAA, depth, and compressed textures. */
434 if (r600_choose_tiling(rctx
->screen
, &templ
) !=
435 RADEON_SURF_MODE_LINEAR_ALIGNED
)
438 new_tex
= (struct r600_texture
*)screen
->resource_create(screen
, &templ
);
442 /* Copy the pixels to the new texture. */
443 if (!invalidate_storage
) {
444 for (i
= 0; i
<= templ
.last_level
; i
++) {
448 u_minify(templ
.width0
, i
), u_minify(templ
.height0
, i
),
449 util_max_layer(&templ
, i
) + 1, &box
);
451 rctx
->dma_copy(&rctx
->b
, &new_tex
->resource
.b
.b
, i
, 0, 0, 0,
452 &rtex
->resource
.b
.b
, i
, &box
);
456 r600_texture_discard_cmask(rctx
->screen
, rtex
);
457 r600_texture_discard_dcc(rctx
->screen
, rtex
);
459 /* Replace the structure fields of rtex. */
460 rtex
->resource
.b
.b
.bind
= templ
.bind
;
461 pb_reference(&rtex
->resource
.buf
, new_tex
->resource
.buf
);
462 rtex
->resource
.gpu_address
= new_tex
->resource
.gpu_address
;
463 rtex
->resource
.vram_usage
= new_tex
->resource
.vram_usage
;
464 rtex
->resource
.gart_usage
= new_tex
->resource
.gart_usage
;
465 rtex
->resource
.bo_size
= new_tex
->resource
.bo_size
;
466 rtex
->resource
.bo_alignment
= new_tex
->resource
.bo_alignment
;
467 rtex
->resource
.domains
= new_tex
->resource
.domains
;
468 rtex
->resource
.flags
= new_tex
->resource
.flags
;
469 rtex
->size
= new_tex
->size
;
470 rtex
->surface
= new_tex
->surface
;
471 rtex
->non_disp_tiling
= new_tex
->non_disp_tiling
;
472 rtex
->cb_color_info
= new_tex
->cb_color_info
;
473 rtex
->cmask
= new_tex
->cmask
; /* needed even without CMASK */
475 assert(!rtex
->htile_buffer
);
476 assert(!rtex
->cmask
.size
);
477 assert(!rtex
->fmask
.size
);
478 assert(!rtex
->dcc_offset
);
479 assert(!rtex
->is_depth
);
481 r600_texture_reference(&new_tex
, NULL
);
483 r600_dirty_all_framebuffer_states(rctx
->screen
);
484 p_atomic_inc(&rctx
->screen
->dirty_tex_descriptor_counter
);
487 static boolean
r600_texture_get_handle(struct pipe_screen
* screen
,
488 struct pipe_context
*ctx
,
489 struct pipe_resource
*resource
,
490 struct winsys_handle
*whandle
,
493 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)screen
;
494 struct r600_common_context
*rctx
= (struct r600_common_context
*)
495 (ctx
? ctx
: rscreen
->aux_context
);
496 struct r600_resource
*res
= (struct r600_resource
*)resource
;
497 struct r600_texture
*rtex
= (struct r600_texture
*)resource
;
498 struct radeon_bo_metadata metadata
;
499 bool update_metadata
= false;
501 /* This is not supported now, but it might be required for OpenCL
502 * interop in the future.
504 if (resource
->target
!= PIPE_BUFFER
&&
505 (resource
->nr_samples
> 1 || rtex
->is_depth
))
508 if (resource
->target
!= PIPE_BUFFER
) {
509 /* Since shader image stores don't support DCC on VI,
510 * disable it for external clients that want write
513 if (usage
& PIPE_HANDLE_USAGE_WRITE
&& rtex
->dcc_offset
) {
514 if (r600_texture_disable_dcc(rctx
, rtex
))
515 update_metadata
= true;
518 if (!(usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
) &&
519 (rtex
->cmask
.size
|| rtex
->dcc_offset
)) {
520 /* Eliminate fast clear (both CMASK and DCC) */
521 r600_eliminate_fast_color_clear(rctx
, rtex
);
523 /* Disable CMASK if flush_resource isn't going
526 if (rtex
->cmask
.size
)
527 r600_texture_discard_cmask(rscreen
, rtex
);
531 if (!res
->is_shared
|| update_metadata
) {
532 r600_texture_init_metadata(rtex
, &metadata
);
533 if (rscreen
->query_opaque_metadata
)
534 rscreen
->query_opaque_metadata(rscreen
, rtex
,
537 rscreen
->ws
->buffer_set_metadata(res
->buf
, &metadata
);
541 if (res
->is_shared
) {
542 /* USAGE_EXPLICIT_FLUSH must be cleared if at least one user
545 res
->external_usage
|= usage
& ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
;
546 if (!(usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
))
547 res
->external_usage
&= ~PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
;
549 res
->is_shared
= true;
550 res
->external_usage
= usage
;
553 return rscreen
->ws
->buffer_get_handle(res
->buf
,
554 rtex
->surface
.level
[0].pitch_bytes
,
555 rtex
->surface
.level
[0].offset
,
556 rtex
->surface
.level
[0].slice_size
,
560 static void r600_texture_destroy(struct pipe_screen
*screen
,
561 struct pipe_resource
*ptex
)
563 struct r600_texture
*rtex
= (struct r600_texture
*)ptex
;
564 struct r600_resource
*resource
= &rtex
->resource
;
566 r600_texture_reference(&rtex
->flushed_depth_texture
, NULL
);
568 r600_resource_reference(&rtex
->htile_buffer
, NULL
);
569 if (rtex
->cmask_buffer
!= &rtex
->resource
) {
570 r600_resource_reference(&rtex
->cmask_buffer
, NULL
);
572 pb_reference(&resource
->buf
, NULL
);
573 r600_resource_reference(&rtex
->dcc_separate_buffer
, NULL
);
574 r600_resource_reference(&rtex
->last_dcc_separate_buffer
, NULL
);
578 static const struct u_resource_vtbl r600_texture_vtbl
;
580 /* The number of samples can be specified independently of the texture. */
581 void r600_texture_get_fmask_info(struct r600_common_screen
*rscreen
,
582 struct r600_texture
*rtex
,
584 struct r600_fmask_info
*out
)
586 /* FMASK is allocated like an ordinary texture. */
587 struct pipe_resource templ
= rtex
->resource
.b
.b
;
588 struct radeon_surf fmask
= {};
591 memset(out
, 0, sizeof(*out
));
593 templ
.nr_samples
= 1;
594 flags
= rtex
->surface
.flags
| RADEON_SURF_FMASK
;
596 if (rscreen
->chip_class
<= CAYMAN
) {
597 /* Use the same parameters and tile mode. */
598 fmask
.bankw
= rtex
->surface
.bankw
;
599 fmask
.bankh
= rtex
->surface
.bankh
;
600 fmask
.mtilea
= rtex
->surface
.mtilea
;
601 fmask
.tile_split
= rtex
->surface
.tile_split
;
607 switch (nr_samples
) {
616 R600_ERR("Invalid sample count for FMASK allocation.\n");
620 /* Overallocate FMASK on R600-R700 to fix colorbuffer corruption.
621 * This can be fixed by writing a separate FMASK allocator specifically
622 * for R600-R700 asics. */
623 if (rscreen
->chip_class
<= R700
) {
627 if (rscreen
->ws
->surface_init(rscreen
->ws
, &templ
, flags
, bpe
,
628 RADEON_SURF_MODE_2D
, &fmask
)) {
629 R600_ERR("Got error in surface_init while allocating FMASK.\n");
633 assert(fmask
.level
[0].mode
== RADEON_SURF_MODE_2D
);
635 out
->slice_tile_max
= (fmask
.level
[0].nblk_x
* fmask
.level
[0].nblk_y
) / 64;
636 if (out
->slice_tile_max
)
637 out
->slice_tile_max
-= 1;
639 out
->tile_mode_index
= fmask
.tiling_index
[0];
640 out
->pitch_in_pixels
= fmask
.level
[0].nblk_x
;
641 out
->bank_height
= fmask
.bankh
;
642 out
->alignment
= MAX2(256, fmask
.surf_alignment
);
643 out
->size
= fmask
.surf_size
;
646 static void r600_texture_allocate_fmask(struct r600_common_screen
*rscreen
,
647 struct r600_texture
*rtex
)
649 r600_texture_get_fmask_info(rscreen
, rtex
,
650 rtex
->resource
.b
.b
.nr_samples
, &rtex
->fmask
);
652 rtex
->fmask
.offset
= align64(rtex
->size
, rtex
->fmask
.alignment
);
653 rtex
->size
= rtex
->fmask
.offset
+ rtex
->fmask
.size
;
656 void r600_texture_get_cmask_info(struct r600_common_screen
*rscreen
,
657 struct r600_texture
*rtex
,
658 struct r600_cmask_info
*out
)
660 unsigned cmask_tile_width
= 8;
661 unsigned cmask_tile_height
= 8;
662 unsigned cmask_tile_elements
= cmask_tile_width
* cmask_tile_height
;
663 unsigned element_bits
= 4;
664 unsigned cmask_cache_bits
= 1024;
665 unsigned num_pipes
= rscreen
->info
.num_tile_pipes
;
666 unsigned pipe_interleave_bytes
= rscreen
->info
.pipe_interleave_bytes
;
668 unsigned elements_per_macro_tile
= (cmask_cache_bits
/ element_bits
) * num_pipes
;
669 unsigned pixels_per_macro_tile
= elements_per_macro_tile
* cmask_tile_elements
;
670 unsigned sqrt_pixels_per_macro_tile
= sqrt(pixels_per_macro_tile
);
671 unsigned macro_tile_width
= util_next_power_of_two(sqrt_pixels_per_macro_tile
);
672 unsigned macro_tile_height
= pixels_per_macro_tile
/ macro_tile_width
;
674 unsigned pitch_elements
= align(rtex
->resource
.b
.b
.width0
, macro_tile_width
);
675 unsigned height
= align(rtex
->resource
.b
.b
.height0
, macro_tile_height
);
677 unsigned base_align
= num_pipes
* pipe_interleave_bytes
;
678 unsigned slice_bytes
=
679 ((pitch_elements
* height
* element_bits
+ 7) / 8) / cmask_tile_elements
;
681 assert(macro_tile_width
% 128 == 0);
682 assert(macro_tile_height
% 128 == 0);
684 out
->slice_tile_max
= ((pitch_elements
* height
) / (128*128)) - 1;
685 out
->alignment
= MAX2(256, base_align
);
686 out
->size
= (util_max_layer(&rtex
->resource
.b
.b
, 0) + 1) *
687 align(slice_bytes
, base_align
);
690 static void si_texture_get_cmask_info(struct r600_common_screen
*rscreen
,
691 struct r600_texture
*rtex
,
692 struct r600_cmask_info
*out
)
694 unsigned pipe_interleave_bytes
= rscreen
->info
.pipe_interleave_bytes
;
695 unsigned num_pipes
= rscreen
->info
.num_tile_pipes
;
696 unsigned cl_width
, cl_height
;
711 case 16: /* Hawaii */
720 unsigned base_align
= num_pipes
* pipe_interleave_bytes
;
722 unsigned width
= align(rtex
->resource
.b
.b
.width0
, cl_width
*8);
723 unsigned height
= align(rtex
->resource
.b
.b
.height0
, cl_height
*8);
724 unsigned slice_elements
= (width
* height
) / (8*8);
726 /* Each element of CMASK is a nibble. */
727 unsigned slice_bytes
= slice_elements
/ 2;
729 out
->slice_tile_max
= (width
* height
) / (128*128);
730 if (out
->slice_tile_max
)
731 out
->slice_tile_max
-= 1;
733 out
->alignment
= MAX2(256, base_align
);
734 out
->size
= (util_max_layer(&rtex
->resource
.b
.b
, 0) + 1) *
735 align(slice_bytes
, base_align
);
738 static void r600_texture_allocate_cmask(struct r600_common_screen
*rscreen
,
739 struct r600_texture
*rtex
)
741 if (rscreen
->chip_class
>= SI
) {
742 si_texture_get_cmask_info(rscreen
, rtex
, &rtex
->cmask
);
744 r600_texture_get_cmask_info(rscreen
, rtex
, &rtex
->cmask
);
747 rtex
->cmask
.offset
= align64(rtex
->size
, rtex
->cmask
.alignment
);
748 rtex
->size
= rtex
->cmask
.offset
+ rtex
->cmask
.size
;
750 if (rscreen
->chip_class
>= SI
)
751 rtex
->cb_color_info
|= SI_S_028C70_FAST_CLEAR(1);
753 rtex
->cb_color_info
|= EG_S_028C70_FAST_CLEAR(1);
756 static void r600_texture_alloc_cmask_separate(struct r600_common_screen
*rscreen
,
757 struct r600_texture
*rtex
)
759 if (rtex
->cmask_buffer
)
762 assert(rtex
->cmask
.size
== 0);
764 if (rscreen
->chip_class
>= SI
) {
765 si_texture_get_cmask_info(rscreen
, rtex
, &rtex
->cmask
);
767 r600_texture_get_cmask_info(rscreen
, rtex
, &rtex
->cmask
);
770 rtex
->cmask_buffer
= (struct r600_resource
*)
771 r600_aligned_buffer_create(&rscreen
->b
, 0, PIPE_USAGE_DEFAULT
,
773 rtex
->cmask
.alignment
);
774 if (rtex
->cmask_buffer
== NULL
) {
775 rtex
->cmask
.size
= 0;
779 /* update colorbuffer state bits */
780 rtex
->cmask
.base_address_reg
= rtex
->cmask_buffer
->gpu_address
>> 8;
782 if (rscreen
->chip_class
>= SI
)
783 rtex
->cb_color_info
|= SI_S_028C70_FAST_CLEAR(1);
785 rtex
->cb_color_info
|= EG_S_028C70_FAST_CLEAR(1);
787 p_atomic_inc(&rscreen
->compressed_colortex_counter
);
790 static void r600_texture_get_htile_size(struct r600_common_screen
*rscreen
,
791 struct r600_texture
*rtex
)
793 unsigned cl_width
, cl_height
, width
, height
;
794 unsigned slice_elements
, slice_bytes
, pipe_interleave_bytes
, base_align
;
795 unsigned num_pipes
= rscreen
->info
.num_tile_pipes
;
797 rtex
->surface
.htile_size
= 0;
799 if (rscreen
->chip_class
<= EVERGREEN
&&
800 rscreen
->info
.drm_major
== 2 && rscreen
->info
.drm_minor
< 26)
803 /* HW bug on R6xx. */
804 if (rscreen
->chip_class
== R600
&&
805 (rtex
->resource
.b
.b
.width0
> 7680 ||
806 rtex
->resource
.b
.b
.height0
> 7680))
809 /* HTILE is broken with 1D tiling on old kernels and CIK. */
810 if (rscreen
->chip_class
>= CIK
&&
811 rtex
->surface
.level
[0].mode
== RADEON_SURF_MODE_1D
&&
812 rscreen
->info
.drm_major
== 2 && rscreen
->info
.drm_minor
< 38)
815 /* Overalign HTILE on P2 configs to work around GPU hangs in
816 * piglit/depthstencil-render-miplevels 585.
818 * This has been confirmed to help Kabini & Stoney, where the hangs
819 * are always reproducible. I think I have seen the test hang
820 * on Carrizo too, though it was very rare there.
822 if (rscreen
->chip_class
>= CIK
&& num_pipes
< 4)
851 width
= align(rtex
->resource
.b
.b
.width0
, cl_width
* 8);
852 height
= align(rtex
->resource
.b
.b
.height0
, cl_height
* 8);
854 slice_elements
= (width
* height
) / (8 * 8);
855 slice_bytes
= slice_elements
* 4;
857 pipe_interleave_bytes
= rscreen
->info
.pipe_interleave_bytes
;
858 base_align
= num_pipes
* pipe_interleave_bytes
;
860 rtex
->surface
.htile_alignment
= base_align
;
861 rtex
->surface
.htile_size
=
862 (util_max_layer(&rtex
->resource
.b
.b
, 0) + 1) *
863 align(slice_bytes
, base_align
);
866 static void r600_texture_allocate_htile(struct r600_common_screen
*rscreen
,
867 struct r600_texture
*rtex
)
869 uint32_t clear_value
;
871 if (rtex
->tc_compatible_htile
) {
872 clear_value
= 0x0000030F;
874 r600_texture_get_htile_size(rscreen
, rtex
);
878 if (!rtex
->surface
.htile_size
)
881 rtex
->htile_buffer
= (struct r600_resource
*)
882 r600_aligned_buffer_create(&rscreen
->b
, 0, PIPE_USAGE_DEFAULT
,
883 rtex
->surface
.htile_size
,
884 rtex
->surface
.htile_alignment
);
885 if (rtex
->htile_buffer
== NULL
) {
886 /* this is not a fatal error as we can still keep rendering
887 * without htile buffer */
888 R600_ERR("Failed to create buffer object for htile buffer.\n");
890 r600_screen_clear_buffer(rscreen
, &rtex
->htile_buffer
->b
.b
,
891 0, rtex
->surface
.htile_size
,
892 clear_value
, R600_COHERENCY_NONE
);
896 void r600_print_texture_info(struct r600_texture
*rtex
, FILE *f
)
900 fprintf(f
, " Info: npix_x=%u, npix_y=%u, npix_z=%u, blk_w=%u, "
901 "blk_h=%u, array_size=%u, last_level=%u, "
902 "bpe=%u, nsamples=%u, flags=0x%x, %s\n",
903 rtex
->resource
.b
.b
.width0
, rtex
->resource
.b
.b
.height0
,
904 rtex
->resource
.b
.b
.depth0
, rtex
->surface
.blk_w
,
906 rtex
->resource
.b
.b
.array_size
, rtex
->resource
.b
.b
.last_level
,
907 rtex
->surface
.bpe
, rtex
->resource
.b
.b
.nr_samples
,
908 rtex
->surface
.flags
, util_format_short_name(rtex
->resource
.b
.b
.format
));
910 fprintf(f
, " Layout: size=%"PRIu64
", alignment=%u, bankw=%u, "
911 "bankh=%u, nbanks=%u, mtilea=%u, tilesplit=%u, pipeconfig=%u, scanout=%u\n",
912 rtex
->surface
.surf_size
, rtex
->surface
.surf_alignment
, rtex
->surface
.bankw
,
913 rtex
->surface
.bankh
, rtex
->surface
.num_banks
, rtex
->surface
.mtilea
,
914 rtex
->surface
.tile_split
, rtex
->surface
.pipe_config
,
915 (rtex
->surface
.flags
& RADEON_SURF_SCANOUT
) != 0);
917 if (rtex
->fmask
.size
)
918 fprintf(f
, " FMask: offset=%"PRIu64
", size=%"PRIu64
", alignment=%u, pitch_in_pixels=%u, "
919 "bankh=%u, slice_tile_max=%u, tile_mode_index=%u\n",
920 rtex
->fmask
.offset
, rtex
->fmask
.size
, rtex
->fmask
.alignment
,
921 rtex
->fmask
.pitch_in_pixels
, rtex
->fmask
.bank_height
,
922 rtex
->fmask
.slice_tile_max
, rtex
->fmask
.tile_mode_index
);
924 if (rtex
->cmask
.size
)
925 fprintf(f
, " CMask: offset=%"PRIu64
", size=%"PRIu64
", alignment=%u, "
926 "slice_tile_max=%u\n",
927 rtex
->cmask
.offset
, rtex
->cmask
.size
, rtex
->cmask
.alignment
,
928 rtex
->cmask
.slice_tile_max
);
930 if (rtex
->htile_buffer
)
931 fprintf(f
, " HTile: size=%u, alignment=%u, TC_compatible = %u\n",
932 rtex
->htile_buffer
->b
.b
.width0
,
933 rtex
->htile_buffer
->buf
->alignment
,
934 rtex
->tc_compatible_htile
);
936 if (rtex
->dcc_offset
) {
937 fprintf(f
, " DCC: offset=%"PRIu64
", size=%"PRIu64
", alignment=%u\n",
938 rtex
->dcc_offset
, rtex
->surface
.dcc_size
,
939 rtex
->surface
.dcc_alignment
);
940 for (i
= 0; i
<= rtex
->resource
.b
.b
.last_level
; i
++)
941 fprintf(f
, " DCCLevel[%i]: enabled=%u, offset=%"PRIu64
", "
942 "fast_clear_size=%"PRIu64
"\n",
943 i
, rtex
->surface
.level
[i
].dcc_enabled
,
944 rtex
->surface
.level
[i
].dcc_offset
,
945 rtex
->surface
.level
[i
].dcc_fast_clear_size
);
948 for (i
= 0; i
<= rtex
->resource
.b
.b
.last_level
; i
++)
949 fprintf(f
, " Level[%i]: offset=%"PRIu64
", slice_size=%"PRIu64
", "
950 "npix_x=%u, npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
951 "pitch_bytes=%u, mode=%u, tiling_index = %u\n",
952 i
, rtex
->surface
.level
[i
].offset
,
953 rtex
->surface
.level
[i
].slice_size
,
954 u_minify(rtex
->resource
.b
.b
.width0
, i
),
955 u_minify(rtex
->resource
.b
.b
.height0
, i
),
956 u_minify(rtex
->resource
.b
.b
.depth0
, i
),
957 rtex
->surface
.level
[i
].nblk_x
,
958 rtex
->surface
.level
[i
].nblk_y
,
959 rtex
->surface
.level
[i
].pitch_bytes
,
960 rtex
->surface
.level
[i
].mode
,
961 rtex
->surface
.tiling_index
[i
]);
963 if (rtex
->surface
.flags
& RADEON_SURF_SBUFFER
) {
964 fprintf(f
, " StencilLayout: tilesplit=%u\n",
965 rtex
->surface
.stencil_tile_split
);
966 for (i
= 0; i
<= rtex
->resource
.b
.b
.last_level
; i
++) {
967 fprintf(f
, " StencilLevel[%i]: offset=%"PRIu64
", "
968 "slice_size=%"PRIu64
", npix_x=%u, "
969 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
970 "pitch_bytes=%u, mode=%u, tiling_index = %u\n",
971 i
, rtex
->surface
.stencil_level
[i
].offset
,
972 rtex
->surface
.stencil_level
[i
].slice_size
,
973 u_minify(rtex
->resource
.b
.b
.width0
, i
),
974 u_minify(rtex
->resource
.b
.b
.height0
, i
),
975 u_minify(rtex
->resource
.b
.b
.depth0
, i
),
976 rtex
->surface
.stencil_level
[i
].nblk_x
,
977 rtex
->surface
.stencil_level
[i
].nblk_y
,
978 rtex
->surface
.stencil_level
[i
].pitch_bytes
,
979 rtex
->surface
.stencil_level
[i
].mode
,
980 rtex
->surface
.stencil_tiling_index
[i
]);
985 /* Common processing for r600_texture_create and r600_texture_from_handle */
986 static struct r600_texture
*
987 r600_texture_create_object(struct pipe_screen
*screen
,
988 const struct pipe_resource
*base
,
989 struct pb_buffer
*buf
,
990 struct radeon_surf
*surface
)
992 struct r600_texture
*rtex
;
993 struct r600_resource
*resource
;
994 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)screen
;
996 rtex
= CALLOC_STRUCT(r600_texture
);
1000 resource
= &rtex
->resource
;
1001 resource
->b
.b
= *base
;
1002 resource
->b
.b
.next
= NULL
;
1003 resource
->b
.vtbl
= &r600_texture_vtbl
;
1004 pipe_reference_init(&resource
->b
.b
.reference
, 1);
1005 resource
->b
.b
.screen
= screen
;
1007 /* don't include stencil-only formats which we don't support for rendering */
1008 rtex
->is_depth
= util_format_has_depth(util_format_description(rtex
->resource
.b
.b
.format
));
1010 rtex
->surface
= *surface
;
1011 rtex
->size
= rtex
->surface
.surf_size
;
1013 rtex
->tc_compatible_htile
= rtex
->surface
.htile_size
!= 0;
1014 assert(!!(rtex
->surface
.flags
& RADEON_SURF_TC_COMPATIBLE_HTILE
) ==
1015 rtex
->tc_compatible_htile
);
1017 /* TC-compatible HTILE only supports Z32_FLOAT. */
1018 if (rtex
->tc_compatible_htile
)
1019 rtex
->db_render_format
= PIPE_FORMAT_Z32_FLOAT
;
1021 rtex
->db_render_format
= base
->format
;
1023 /* Tiled depth textures utilize the non-displayable tile order.
1024 * This must be done after r600_setup_surface.
1025 * Applies to R600-Cayman. */
1026 rtex
->non_disp_tiling
= rtex
->is_depth
&& rtex
->surface
.level
[0].mode
>= RADEON_SURF_MODE_1D
;
1027 /* Applies to GCN. */
1028 rtex
->last_msaa_resolve_target_micro_mode
= rtex
->surface
.micro_tile_mode
;
1030 /* Disable separate DCC at the beginning. DRI2 doesn't reuse buffers
1031 * between frames, so the only thing that can enable separate DCC
1032 * with DRI2 is multiple slow clears within a frame.
1034 rtex
->ps_draw_ratio
= 0;
1036 if (rtex
->is_depth
) {
1037 if (base
->flags
& (R600_RESOURCE_FLAG_TRANSFER
|
1038 R600_RESOURCE_FLAG_FLUSHED_DEPTH
) ||
1039 rscreen
->chip_class
>= EVERGREEN
) {
1040 rtex
->can_sample_z
= !rtex
->surface
.depth_adjusted
;
1041 rtex
->can_sample_s
= !rtex
->surface
.stencil_adjusted
;
1043 if (rtex
->resource
.b
.b
.nr_samples
<= 1 &&
1044 (rtex
->resource
.b
.b
.format
== PIPE_FORMAT_Z16_UNORM
||
1045 rtex
->resource
.b
.b
.format
== PIPE_FORMAT_Z32_FLOAT
))
1046 rtex
->can_sample_z
= true;
1049 if (!(base
->flags
& (R600_RESOURCE_FLAG_TRANSFER
|
1050 R600_RESOURCE_FLAG_FLUSHED_DEPTH
))) {
1051 rtex
->db_compatible
= true;
1053 if (!(rscreen
->debug_flags
& DBG_NO_HYPERZ
))
1054 r600_texture_allocate_htile(rscreen
, rtex
);
1057 if (base
->nr_samples
> 1) {
1059 r600_texture_allocate_fmask(rscreen
, rtex
);
1060 r600_texture_allocate_cmask(rscreen
, rtex
);
1061 rtex
->cmask_buffer
= &rtex
->resource
;
1063 if (!rtex
->fmask
.size
|| !rtex
->cmask
.size
) {
1069 /* Shared textures must always set up DCC here.
1070 * If it's not present, it will be disabled by
1071 * apply_opaque_metadata later.
1073 if (rtex
->surface
.dcc_size
&&
1074 (buf
|| !(rscreen
->debug_flags
& DBG_NO_DCC
)) &&
1075 !(rtex
->surface
.flags
& RADEON_SURF_SCANOUT
)) {
1076 /* Reserve space for the DCC buffer. */
1077 rtex
->dcc_offset
= align64(rtex
->size
, rtex
->surface
.dcc_alignment
);
1078 rtex
->size
= rtex
->dcc_offset
+ rtex
->surface
.dcc_size
;
1082 /* Now create the backing buffer. */
1084 r600_init_resource_fields(rscreen
, resource
, rtex
->size
,
1085 rtex
->surface
.surf_alignment
);
1087 resource
->flags
|= RADEON_FLAG_HANDLE
;
1089 if (!r600_alloc_resource(rscreen
, resource
)) {
1094 resource
->buf
= buf
;
1095 resource
->gpu_address
= rscreen
->ws
->buffer_get_virtual_address(resource
->buf
);
1096 resource
->bo_size
= buf
->size
;
1097 resource
->bo_alignment
= buf
->alignment
;
1098 resource
->domains
= rscreen
->ws
->buffer_get_initial_domain(resource
->buf
);
1099 if (resource
->domains
& RADEON_DOMAIN_VRAM
)
1100 resource
->vram_usage
= buf
->size
;
1101 else if (resource
->domains
& RADEON_DOMAIN_GTT
)
1102 resource
->gart_usage
= buf
->size
;
1105 if (rtex
->cmask
.size
) {
1106 /* Initialize the cmask to 0xCC (= compressed state). */
1107 r600_screen_clear_buffer(rscreen
, &rtex
->cmask_buffer
->b
.b
,
1108 rtex
->cmask
.offset
, rtex
->cmask
.size
,
1109 0xCCCCCCCC, R600_COHERENCY_NONE
);
1112 /* Initialize DCC only if the texture is not being imported. */
1113 if (!buf
&& rtex
->dcc_offset
) {
1114 r600_screen_clear_buffer(rscreen
, &rtex
->resource
.b
.b
,
1116 rtex
->surface
.dcc_size
,
1117 0xFFFFFFFF, R600_COHERENCY_NONE
);
1120 /* Initialize the CMASK base register value. */
1121 rtex
->cmask
.base_address_reg
=
1122 (rtex
->resource
.gpu_address
+ rtex
->cmask
.offset
) >> 8;
1124 if (rscreen
->debug_flags
& DBG_VM
) {
1125 fprintf(stderr
, "VM start=0x%"PRIX64
" end=0x%"PRIX64
" | Texture %ix%ix%i, %i levels, %i samples, %s\n",
1126 rtex
->resource
.gpu_address
,
1127 rtex
->resource
.gpu_address
+ rtex
->resource
.buf
->size
,
1128 base
->width0
, base
->height0
, util_max_layer(base
, 0)+1, base
->last_level
+1,
1129 base
->nr_samples
? base
->nr_samples
: 1, util_format_short_name(base
->format
));
1132 if (rscreen
->debug_flags
& DBG_TEX
) {
1134 r600_print_texture_info(rtex
, stdout
);
1141 static enum radeon_surf_mode
1142 r600_choose_tiling(struct r600_common_screen
*rscreen
,
1143 const struct pipe_resource
*templ
)
1145 const struct util_format_description
*desc
= util_format_description(templ
->format
);
1146 bool force_tiling
= templ
->flags
& R600_RESOURCE_FLAG_FORCE_TILING
;
1148 /* MSAA resources must be 2D tiled. */
1149 if (templ
->nr_samples
> 1)
1150 return RADEON_SURF_MODE_2D
;
1152 /* Transfer resources should be linear. */
1153 if (templ
->flags
& R600_RESOURCE_FLAG_TRANSFER
)
1154 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1156 /* r600g: force tiling on TEXTURE_2D and TEXTURE_3D compute resources. */
1157 if (rscreen
->chip_class
>= R600
&& rscreen
->chip_class
<= CAYMAN
&&
1158 (templ
->bind
& PIPE_BIND_COMPUTE_RESOURCE
) &&
1159 (templ
->target
== PIPE_TEXTURE_2D
||
1160 templ
->target
== PIPE_TEXTURE_3D
))
1161 force_tiling
= true;
1163 /* Handle common candidates for the linear mode.
1164 * Compressed textures and DB surfaces must always be tiled.
1166 if (!force_tiling
&& !util_format_is_compressed(templ
->format
) &&
1167 (!util_format_is_depth_or_stencil(templ
->format
) ||
1168 templ
->flags
& R600_RESOURCE_FLAG_FLUSHED_DEPTH
)) {
1169 if (rscreen
->debug_flags
& DBG_NO_TILING
)
1170 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1172 /* Tiling doesn't work with the 422 (SUBSAMPLED) formats on R600+. */
1173 if (desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
)
1174 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1176 /* Cursors are linear on SI.
1177 * (XXX double-check, maybe also use RADEON_SURF_SCANOUT) */
1178 if (rscreen
->chip_class
>= SI
&&
1179 (templ
->bind
& PIPE_BIND_CURSOR
))
1180 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1182 if (templ
->bind
& PIPE_BIND_LINEAR
)
1183 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1185 /* Textures with a very small height are recommended to be linear. */
1186 if (templ
->target
== PIPE_TEXTURE_1D
||
1187 templ
->target
== PIPE_TEXTURE_1D_ARRAY
||
1188 templ
->height0
<= 4)
1189 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1191 /* Textures likely to be mapped often. */
1192 if (templ
->usage
== PIPE_USAGE_STAGING
||
1193 templ
->usage
== PIPE_USAGE_STREAM
)
1194 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
1197 /* Make small textures 1D tiled. */
1198 if (templ
->width0
<= 16 || templ
->height0
<= 16 ||
1199 (rscreen
->debug_flags
& DBG_NO_2D_TILING
))
1200 return RADEON_SURF_MODE_1D
;
1202 /* The allocator will switch to 1D if needed. */
1203 return RADEON_SURF_MODE_2D
;
1206 struct pipe_resource
*r600_texture_create(struct pipe_screen
*screen
,
1207 const struct pipe_resource
*templ
)
1209 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)screen
;
1210 struct radeon_surf surface
= {0};
1211 bool is_flushed_depth
= templ
->flags
& R600_RESOURCE_FLAG_FLUSHED_DEPTH
;
1212 bool tc_compatible_htile
=
1213 rscreen
->chip_class
>= VI
&&
1214 (templ
->flags
& PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY
) &&
1215 !(rscreen
->debug_flags
& DBG_NO_HYPERZ
) &&
1216 !is_flushed_depth
&&
1217 templ
->nr_samples
<= 1 && /* TC-compat HTILE is less efficient with MSAA */
1218 util_format_is_depth_or_stencil(templ
->format
);
1222 r
= r600_init_surface(rscreen
, &surface
, templ
,
1223 r600_choose_tiling(rscreen
, templ
), 0, 0,
1224 false, false, is_flushed_depth
,
1225 tc_compatible_htile
);
1230 return (struct pipe_resource
*)
1231 r600_texture_create_object(screen
, templ
, NULL
, &surface
);
1234 static struct pipe_resource
*r600_texture_from_handle(struct pipe_screen
*screen
,
1235 const struct pipe_resource
*templ
,
1236 struct winsys_handle
*whandle
,
1239 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)screen
;
1240 struct pb_buffer
*buf
= NULL
;
1241 unsigned stride
= 0, offset
= 0;
1242 unsigned array_mode
;
1243 struct radeon_surf surface
;
1245 struct radeon_bo_metadata metadata
= {};
1246 struct r600_texture
*rtex
;
1248 /* Support only 2D textures without mipmaps */
1249 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
) ||
1250 templ
->depth0
!= 1 || templ
->last_level
!= 0)
1253 buf
= rscreen
->ws
->buffer_from_handle(rscreen
->ws
, whandle
, &stride
, &offset
);
1257 rscreen
->ws
->buffer_get_metadata(buf
, &metadata
);
1259 surface
.pipe_config
= metadata
.pipe_config
;
1260 surface
.bankw
= metadata
.bankw
;
1261 surface
.bankh
= metadata
.bankh
;
1262 surface
.tile_split
= metadata
.tile_split
;
1263 surface
.mtilea
= metadata
.mtilea
;
1264 surface
.num_banks
= metadata
.num_banks
;
1266 if (metadata
.macrotile
== RADEON_LAYOUT_TILED
)
1267 array_mode
= RADEON_SURF_MODE_2D
;
1268 else if (metadata
.microtile
== RADEON_LAYOUT_TILED
)
1269 array_mode
= RADEON_SURF_MODE_1D
;
1271 array_mode
= RADEON_SURF_MODE_LINEAR_ALIGNED
;
1273 r
= r600_init_surface(rscreen
, &surface
, templ
, array_mode
, stride
,
1274 offset
, true, metadata
.scanout
, false, false);
1279 rtex
= r600_texture_create_object(screen
, templ
, buf
, &surface
);
1283 rtex
->resource
.is_shared
= true;
1284 rtex
->resource
.external_usage
= usage
;
1286 if (rscreen
->apply_opaque_metadata
)
1287 rscreen
->apply_opaque_metadata(rscreen
, rtex
, &metadata
);
1289 return &rtex
->resource
.b
.b
;
1292 bool r600_init_flushed_depth_texture(struct pipe_context
*ctx
,
1293 struct pipe_resource
*texture
,
1294 struct r600_texture
**staging
)
1296 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
1297 struct pipe_resource resource
;
1298 struct r600_texture
**flushed_depth_texture
= staging
?
1299 staging
: &rtex
->flushed_depth_texture
;
1300 enum pipe_format pipe_format
= texture
->format
;
1303 if (rtex
->flushed_depth_texture
)
1304 return true; /* it's ready */
1306 if (!rtex
->can_sample_z
&& rtex
->can_sample_s
) {
1307 switch (pipe_format
) {
1308 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1309 /* Save memory by not allocating the S plane. */
1310 pipe_format
= PIPE_FORMAT_Z32_FLOAT
;
1312 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1313 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1314 /* Save memory bandwidth by not copying the
1315 * stencil part during flush.
1317 * This potentially increases memory bandwidth
1318 * if an application uses both Z and S texturing
1319 * simultaneously (a flushed Z24S8 texture
1320 * would be stored compactly), but how often
1321 * does that really happen?
1323 pipe_format
= PIPE_FORMAT_Z24X8_UNORM
;
1327 } else if (!rtex
->can_sample_s
&& rtex
->can_sample_z
) {
1328 assert(util_format_has_stencil(util_format_description(pipe_format
)));
1330 /* DB->CB copies to an 8bpp surface don't work. */
1331 pipe_format
= PIPE_FORMAT_X24S8_UINT
;
1335 memset(&resource
, 0, sizeof(resource
));
1336 resource
.target
= texture
->target
;
1337 resource
.format
= pipe_format
;
1338 resource
.width0
= texture
->width0
;
1339 resource
.height0
= texture
->height0
;
1340 resource
.depth0
= texture
->depth0
;
1341 resource
.array_size
= texture
->array_size
;
1342 resource
.last_level
= texture
->last_level
;
1343 resource
.nr_samples
= texture
->nr_samples
;
1344 resource
.usage
= staging
? PIPE_USAGE_STAGING
: PIPE_USAGE_DEFAULT
;
1345 resource
.bind
= texture
->bind
& ~PIPE_BIND_DEPTH_STENCIL
;
1346 resource
.flags
= texture
->flags
| R600_RESOURCE_FLAG_FLUSHED_DEPTH
;
1349 resource
.flags
|= R600_RESOURCE_FLAG_TRANSFER
;
1351 *flushed_depth_texture
= (struct r600_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
1352 if (*flushed_depth_texture
== NULL
) {
1353 R600_ERR("failed to create temporary texture to hold flushed depth\n");
1357 (*flushed_depth_texture
)->non_disp_tiling
= false;
1362 * Initialize the pipe_resource descriptor to be of the same size as the box,
1363 * which is supposed to hold a subregion of the texture "orig" at the given
1366 static void r600_init_temp_resource_from_box(struct pipe_resource
*res
,
1367 struct pipe_resource
*orig
,
1368 const struct pipe_box
*box
,
1369 unsigned level
, unsigned flags
)
1371 memset(res
, 0, sizeof(*res
));
1372 res
->format
= orig
->format
;
1373 res
->width0
= box
->width
;
1374 res
->height0
= box
->height
;
1376 res
->array_size
= 1;
1377 res
->usage
= flags
& R600_RESOURCE_FLAG_TRANSFER
? PIPE_USAGE_STAGING
: PIPE_USAGE_DEFAULT
;
1380 /* We must set the correct texture target and dimensions for a 3D box. */
1381 if (box
->depth
> 1 && util_max_layer(orig
, level
) > 0) {
1382 res
->target
= PIPE_TEXTURE_2D_ARRAY
;
1383 res
->array_size
= box
->depth
;
1385 res
->target
= PIPE_TEXTURE_2D
;
1389 static bool r600_can_invalidate_texture(struct r600_common_screen
*rscreen
,
1390 struct r600_texture
*rtex
,
1391 unsigned transfer_usage
,
1392 const struct pipe_box
*box
)
1394 /* r600g doesn't react to dirty_tex_descriptor_counter */
1395 return rscreen
->chip_class
>= SI
&&
1396 !rtex
->resource
.is_shared
&&
1397 !(transfer_usage
& PIPE_TRANSFER_READ
) &&
1398 rtex
->resource
.b
.b
.last_level
== 0 &&
1399 util_texrange_covers_whole_level(&rtex
->resource
.b
.b
, 0,
1400 box
->x
, box
->y
, box
->z
,
1401 box
->width
, box
->height
,
1405 static void r600_texture_invalidate_storage(struct r600_common_context
*rctx
,
1406 struct r600_texture
*rtex
)
1408 struct r600_common_screen
*rscreen
= rctx
->screen
;
1410 /* There is no point in discarding depth and tiled buffers. */
1411 assert(!rtex
->is_depth
);
1412 assert(rtex
->surface
.level
[0].mode
== RADEON_SURF_MODE_LINEAR_ALIGNED
);
1414 /* Reallocate the buffer in the same pipe_resource. */
1415 r600_alloc_resource(rscreen
, &rtex
->resource
);
1417 /* Initialize the CMASK base address (needed even without CMASK). */
1418 rtex
->cmask
.base_address_reg
=
1419 (rtex
->resource
.gpu_address
+ rtex
->cmask
.offset
) >> 8;
1421 r600_dirty_all_framebuffer_states(rscreen
);
1422 p_atomic_inc(&rscreen
->dirty_tex_descriptor_counter
);
1424 rctx
->num_alloc_tex_transfer_bytes
+= rtex
->size
;
1427 static void *r600_texture_transfer_map(struct pipe_context
*ctx
,
1428 struct pipe_resource
*texture
,
1431 const struct pipe_box
*box
,
1432 struct pipe_transfer
**ptransfer
)
1434 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
1435 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
1436 struct r600_transfer
*trans
;
1437 struct r600_resource
*buf
;
1438 unsigned offset
= 0;
1440 bool use_staging_texture
= false;
1442 assert(!(texture
->flags
& R600_RESOURCE_FLAG_TRANSFER
));
1444 /* Depth textures use staging unconditionally. */
1445 if (!rtex
->is_depth
) {
1446 /* Degrade the tile mode if we get too many transfers on APUs.
1447 * On dGPUs, the staging texture is always faster.
1448 * Only count uploads that are at least 4x4 pixels large.
1450 if (!rctx
->screen
->info
.has_dedicated_vram
&&
1452 box
->width
>= 4 && box
->height
>= 4 &&
1453 p_atomic_inc_return(&rtex
->num_level0_transfers
) == 10) {
1454 bool can_invalidate
=
1455 r600_can_invalidate_texture(rctx
->screen
, rtex
,
1458 r600_degrade_tile_mode_to_linear(rctx
, rtex
,
1462 /* Tiled textures need to be converted into a linear texture for CPU
1463 * access. The staging texture is always linear and is placed in GART.
1465 * Reading from VRAM is slow, always use the staging texture in
1468 * Use the staging texture for uploads if the underlying BO
1471 if (rtex
->surface
.level
[0].mode
>= RADEON_SURF_MODE_1D
)
1472 use_staging_texture
= true;
1473 else if (usage
& PIPE_TRANSFER_READ
)
1474 use_staging_texture
= (rtex
->resource
.domains
&
1475 RADEON_DOMAIN_VRAM
) != 0;
1476 /* Write & linear only: */
1477 else if (r600_rings_is_buffer_referenced(rctx
, rtex
->resource
.buf
,
1478 RADEON_USAGE_READWRITE
) ||
1479 !rctx
->ws
->buffer_wait(rtex
->resource
.buf
, 0,
1480 RADEON_USAGE_READWRITE
)) {
1482 if (r600_can_invalidate_texture(rctx
->screen
, rtex
,
1484 r600_texture_invalidate_storage(rctx
, rtex
);
1486 use_staging_texture
= true;
1490 trans
= CALLOC_STRUCT(r600_transfer
);
1493 trans
->transfer
.resource
= texture
;
1494 trans
->transfer
.level
= level
;
1495 trans
->transfer
.usage
= usage
;
1496 trans
->transfer
.box
= *box
;
1498 if (rtex
->is_depth
) {
1499 struct r600_texture
*staging_depth
;
1501 if (rtex
->resource
.b
.b
.nr_samples
> 1) {
1502 /* MSAA depth buffers need to be converted to single sample buffers.
1504 * Mapping MSAA depth buffers can occur if ReadPixels is called
1505 * with a multisample GLX visual.
1507 * First downsample the depth buffer to a temporary texture,
1508 * then decompress the temporary one to staging.
1510 * Only the region being mapped is transfered.
1512 struct pipe_resource resource
;
1514 r600_init_temp_resource_from_box(&resource
, texture
, box
, level
, 0);
1516 if (!r600_init_flushed_depth_texture(ctx
, &resource
, &staging_depth
)) {
1517 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1522 if (usage
& PIPE_TRANSFER_READ
) {
1523 struct pipe_resource
*temp
= ctx
->screen
->resource_create(ctx
->screen
, &resource
);
1525 R600_ERR("failed to create a temporary depth texture\n");
1530 r600_copy_region_with_blit(ctx
, temp
, 0, 0, 0, 0, texture
, level
, box
);
1531 rctx
->blit_decompress_depth(ctx
, (struct r600_texture
*)temp
, staging_depth
,
1532 0, 0, 0, box
->depth
, 0, 0);
1533 pipe_resource_reference(&temp
, NULL
);
1537 /* XXX: only readback the rectangle which is being mapped? */
1538 /* XXX: when discard is true, no need to read back from depth texture */
1539 if (!r600_init_flushed_depth_texture(ctx
, texture
, &staging_depth
)) {
1540 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1545 rctx
->blit_decompress_depth(ctx
, rtex
, staging_depth
,
1547 box
->z
, box
->z
+ box
->depth
- 1,
1550 offset
= r600_texture_get_offset(staging_depth
, level
, box
);
1553 trans
->transfer
.stride
= staging_depth
->surface
.level
[level
].pitch_bytes
;
1554 trans
->transfer
.layer_stride
= staging_depth
->surface
.level
[level
].slice_size
;
1555 trans
->staging
= (struct r600_resource
*)staging_depth
;
1556 buf
= trans
->staging
;
1557 } else if (use_staging_texture
) {
1558 struct pipe_resource resource
;
1559 struct r600_texture
*staging
;
1561 r600_init_temp_resource_from_box(&resource
, texture
, box
, level
,
1562 R600_RESOURCE_FLAG_TRANSFER
);
1563 resource
.usage
= (usage
& PIPE_TRANSFER_READ
) ?
1564 PIPE_USAGE_STAGING
: PIPE_USAGE_STREAM
;
1566 /* Create the temporary texture. */
1567 staging
= (struct r600_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
1569 R600_ERR("failed to create temporary texture to hold untiled copy\n");
1573 trans
->staging
= &staging
->resource
;
1574 trans
->transfer
.stride
= staging
->surface
.level
[0].pitch_bytes
;
1575 trans
->transfer
.layer_stride
= staging
->surface
.level
[0].slice_size
;
1577 if (usage
& PIPE_TRANSFER_READ
)
1578 r600_copy_to_staging_texture(ctx
, trans
);
1580 usage
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
1582 buf
= trans
->staging
;
1584 /* the resource is mapped directly */
1585 trans
->transfer
.stride
= rtex
->surface
.level
[level
].pitch_bytes
;
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 rtex
->surface
.level
[level
].dcc_enabled
&&
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_texture
*rtex
= (struct r600_texture
*)texture
;
1760 struct r600_surface
*surface
= CALLOC_STRUCT(r600_surface
);
1765 assert(templ
->u
.tex
.first_layer
<= util_max_layer(texture
, templ
->u
.tex
.level
));
1766 assert(templ
->u
.tex
.last_layer
<= util_max_layer(texture
, templ
->u
.tex
.level
));
1768 pipe_reference_init(&surface
->base
.reference
, 1);
1769 pipe_resource_reference(&surface
->base
.texture
, texture
);
1770 surface
->base
.context
= pipe
;
1771 surface
->base
.format
= templ
->format
;
1772 surface
->base
.width
= width
;
1773 surface
->base
.height
= height
;
1774 surface
->base
.u
= templ
->u
;
1775 surface
->level_info
= &rtex
->surface
.level
[templ
->u
.tex
.level
];
1777 if (texture
->target
!= PIPE_BUFFER
)
1778 vi_dcc_disable_if_incompatible_format(rctx
, texture
,
1782 return &surface
->base
;
1785 static struct pipe_surface
*r600_create_surface(struct pipe_context
*pipe
,
1786 struct pipe_resource
*tex
,
1787 const struct pipe_surface
*templ
)
1789 unsigned level
= templ
->u
.tex
.level
;
1790 unsigned width
= u_minify(tex
->width0
, level
);
1791 unsigned height
= u_minify(tex
->height0
, level
);
1793 if (tex
->target
!= PIPE_BUFFER
&& templ
->format
!= tex
->format
) {
1794 const struct util_format_description
*tex_desc
1795 = util_format_description(tex
->format
);
1796 const struct util_format_description
*templ_desc
1797 = util_format_description(templ
->format
);
1799 assert(tex_desc
->block
.bits
== templ_desc
->block
.bits
);
1801 /* Adjust size of surface if and only if the block width or
1802 * height is changed. */
1803 if (tex_desc
->block
.width
!= templ_desc
->block
.width
||
1804 tex_desc
->block
.height
!= templ_desc
->block
.height
) {
1805 unsigned nblks_x
= util_format_get_nblocksx(tex
->format
, width
);
1806 unsigned nblks_y
= util_format_get_nblocksy(tex
->format
, height
);
1808 width
= nblks_x
* templ_desc
->block
.width
;
1809 height
= nblks_y
* templ_desc
->block
.height
;
1813 return r600_create_surface_custom(pipe
, tex
, templ
, width
, height
);
1816 static void r600_surface_destroy(struct pipe_context
*pipe
,
1817 struct pipe_surface
*surface
)
1819 struct r600_surface
*surf
= (struct r600_surface
*)surface
;
1820 r600_resource_reference(&surf
->cb_buffer_fmask
, NULL
);
1821 r600_resource_reference(&surf
->cb_buffer_cmask
, NULL
);
1822 pipe_resource_reference(&surface
->texture
, NULL
);
1826 static void r600_clear_texture(struct pipe_context
*pipe
,
1827 struct pipe_resource
*tex
,
1829 const struct pipe_box
*box
,
1832 struct pipe_screen
*screen
= pipe
->screen
;
1833 struct r600_texture
*rtex
= (struct r600_texture
*)tex
;
1834 struct pipe_surface tmpl
= {{0}};
1835 struct pipe_surface
*sf
;
1836 const struct util_format_description
*desc
=
1837 util_format_description(tex
->format
);
1839 tmpl
.format
= tex
->format
;
1840 tmpl
.u
.tex
.first_layer
= box
->z
;
1841 tmpl
.u
.tex
.last_layer
= box
->z
+ box
->depth
- 1;
1842 tmpl
.u
.tex
.level
= level
;
1843 sf
= pipe
->create_surface(pipe
, tex
, &tmpl
);
1847 if (rtex
->is_depth
) {
1850 uint8_t stencil
= 0;
1852 /* Depth is always present. */
1853 clear
= PIPE_CLEAR_DEPTH
;
1854 desc
->unpack_z_float(&depth
, 0, data
, 0, 1, 1);
1856 if (rtex
->surface
.flags
& RADEON_SURF_SBUFFER
) {
1857 clear
|= PIPE_CLEAR_STENCIL
;
1858 desc
->unpack_s_8uint(&stencil
, 0, data
, 0, 1, 1);
1861 pipe
->clear_depth_stencil(pipe
, sf
, clear
, depth
, stencil
,
1863 box
->width
, box
->height
, false);
1865 union pipe_color_union color
;
1867 /* pipe_color_union requires the full vec4 representation. */
1868 if (util_format_is_pure_uint(tex
->format
))
1869 desc
->unpack_rgba_uint(color
.ui
, 0, data
, 0, 1, 1);
1870 else if (util_format_is_pure_sint(tex
->format
))
1871 desc
->unpack_rgba_sint(color
.i
, 0, data
, 0, 1, 1);
1873 desc
->unpack_rgba_float(color
.f
, 0, data
, 0, 1, 1);
1875 if (screen
->is_format_supported(screen
, tex
->format
,
1877 PIPE_BIND_RENDER_TARGET
)) {
1878 pipe
->clear_render_target(pipe
, sf
, &color
,
1880 box
->width
, box
->height
, false);
1882 /* Software fallback - just for R9G9B9E5_FLOAT */
1883 util_clear_render_target(pipe
, sf
, &color
,
1885 box
->width
, box
->height
);
1888 pipe_surface_reference(&sf
, NULL
);
1891 unsigned r600_translate_colorswap(enum pipe_format format
, bool do_endian_swap
)
1893 const struct util_format_description
*desc
= util_format_description(format
);
1895 #define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == PIPE_SWIZZLE_##swz)
1897 if (format
== PIPE_FORMAT_R11G11B10_FLOAT
) /* isn't plain */
1898 return V_0280A0_SWAP_STD
;
1900 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1903 switch (desc
->nr_channels
) {
1905 if (HAS_SWIZZLE(0,X
))
1906 return V_0280A0_SWAP_STD
; /* X___ */
1907 else if (HAS_SWIZZLE(3,X
))
1908 return V_0280A0_SWAP_ALT_REV
; /* ___X */
1911 if ((HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(1,Y
)) ||
1912 (HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(1,NONE
)) ||
1913 (HAS_SWIZZLE(0,NONE
) && HAS_SWIZZLE(1,Y
)))
1914 return V_0280A0_SWAP_STD
; /* XY__ */
1915 else if ((HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(1,X
)) ||
1916 (HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(1,NONE
)) ||
1917 (HAS_SWIZZLE(0,NONE
) && HAS_SWIZZLE(1,X
)))
1919 return (do_endian_swap
? V_0280A0_SWAP_STD
: V_0280A0_SWAP_STD_REV
);
1920 else if (HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(3,Y
))
1921 return V_0280A0_SWAP_ALT
; /* X__Y */
1922 else if (HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(3,X
))
1923 return V_0280A0_SWAP_ALT_REV
; /* Y__X */
1926 if (HAS_SWIZZLE(0,X
))
1927 return (do_endian_swap
? V_0280A0_SWAP_STD_REV
: V_0280A0_SWAP_STD
);
1928 else if (HAS_SWIZZLE(0,Z
))
1929 return V_0280A0_SWAP_STD_REV
; /* ZYX */
1932 /* check the middle channels, the 1st and 4th channel can be NONE */
1933 if (HAS_SWIZZLE(1,Y
) && HAS_SWIZZLE(2,Z
)) {
1934 return V_0280A0_SWAP_STD
; /* XYZW */
1935 } else if (HAS_SWIZZLE(1,Z
) && HAS_SWIZZLE(2,Y
)) {
1936 return V_0280A0_SWAP_STD_REV
; /* WZYX */
1937 } else if (HAS_SWIZZLE(1,Y
) && HAS_SWIZZLE(2,X
)) {
1938 return V_0280A0_SWAP_ALT
; /* ZYXW */
1939 } else if (HAS_SWIZZLE(1,Z
) && HAS_SWIZZLE(2,W
)) {
1942 return V_0280A0_SWAP_ALT_REV
;
1944 return (do_endian_swap
? V_0280A0_SWAP_ALT
: V_0280A0_SWAP_ALT_REV
);
1951 /* PIPELINE_STAT-BASED DCC ENABLEMENT FOR DISPLAYABLE SURFACES */
1953 static void vi_dcc_clean_up_context_slot(struct r600_common_context
*rctx
,
1958 if (rctx
->dcc_stats
[slot
].query_active
)
1959 vi_separate_dcc_stop_query(&rctx
->b
,
1960 rctx
->dcc_stats
[slot
].tex
);
1962 for (i
= 0; i
< ARRAY_SIZE(rctx
->dcc_stats
[slot
].ps_stats
); i
++)
1963 if (rctx
->dcc_stats
[slot
].ps_stats
[i
]) {
1964 rctx
->b
.destroy_query(&rctx
->b
,
1965 rctx
->dcc_stats
[slot
].ps_stats
[i
]);
1966 rctx
->dcc_stats
[slot
].ps_stats
[i
] = NULL
;
1969 r600_texture_reference(&rctx
->dcc_stats
[slot
].tex
, NULL
);
1973 * Return the per-context slot where DCC statistics queries for the texture live.
1975 static unsigned vi_get_context_dcc_stats_index(struct r600_common_context
*rctx
,
1976 struct r600_texture
*tex
)
1978 int i
, empty_slot
= -1;
1980 /* Remove zombie textures (textures kept alive by this array only). */
1981 for (i
= 0; i
< ARRAY_SIZE(rctx
->dcc_stats
); i
++)
1982 if (rctx
->dcc_stats
[i
].tex
&&
1983 rctx
->dcc_stats
[i
].tex
->resource
.b
.b
.reference
.count
== 1)
1984 vi_dcc_clean_up_context_slot(rctx
, i
);
1986 /* Find the texture. */
1987 for (i
= 0; i
< ARRAY_SIZE(rctx
->dcc_stats
); i
++) {
1988 /* Return if found. */
1989 if (rctx
->dcc_stats
[i
].tex
== tex
) {
1990 rctx
->dcc_stats
[i
].last_use_timestamp
= os_time_get();
1994 /* Record the first seen empty slot. */
1995 if (empty_slot
== -1 && !rctx
->dcc_stats
[i
].tex
)
1999 /* Not found. Remove the oldest member to make space in the array. */
2000 if (empty_slot
== -1) {
2001 int oldest_slot
= 0;
2003 /* Find the oldest slot. */
2004 for (i
= 1; i
< ARRAY_SIZE(rctx
->dcc_stats
); i
++)
2005 if (rctx
->dcc_stats
[oldest_slot
].last_use_timestamp
>
2006 rctx
->dcc_stats
[i
].last_use_timestamp
)
2009 /* Clean up the oldest slot. */
2010 vi_dcc_clean_up_context_slot(rctx
, oldest_slot
);
2011 empty_slot
= oldest_slot
;
2014 /* Add the texture to the new slot. */
2015 r600_texture_reference(&rctx
->dcc_stats
[empty_slot
].tex
, tex
);
2016 rctx
->dcc_stats
[empty_slot
].last_use_timestamp
= os_time_get();
2020 static struct pipe_query
*
2021 vi_create_resuming_pipestats_query(struct pipe_context
*ctx
)
2023 struct r600_query_hw
*query
= (struct r600_query_hw
*)
2024 ctx
->create_query(ctx
, PIPE_QUERY_PIPELINE_STATISTICS
, 0);
2026 query
->flags
|= R600_QUERY_HW_FLAG_BEGIN_RESUMES
;
2027 return (struct pipe_query
*)query
;
2031 * Called when binding a color buffer.
2033 void vi_separate_dcc_start_query(struct pipe_context
*ctx
,
2034 struct r600_texture
*tex
)
2036 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
2037 unsigned i
= vi_get_context_dcc_stats_index(rctx
, tex
);
2039 assert(!rctx
->dcc_stats
[i
].query_active
);
2041 if (!rctx
->dcc_stats
[i
].ps_stats
[0])
2042 rctx
->dcc_stats
[i
].ps_stats
[0] = vi_create_resuming_pipestats_query(ctx
);
2044 /* begin or resume the query */
2045 ctx
->begin_query(ctx
, rctx
->dcc_stats
[i
].ps_stats
[0]);
2046 rctx
->dcc_stats
[i
].query_active
= true;
2050 * Called when unbinding a color buffer.
2052 void vi_separate_dcc_stop_query(struct pipe_context
*ctx
,
2053 struct r600_texture
*tex
)
2055 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
2056 unsigned i
= vi_get_context_dcc_stats_index(rctx
, tex
);
2058 assert(rctx
->dcc_stats
[i
].query_active
);
2059 assert(rctx
->dcc_stats
[i
].ps_stats
[0]);
2061 /* pause or end the query */
2062 ctx
->end_query(ctx
, rctx
->dcc_stats
[i
].ps_stats
[0]);
2063 rctx
->dcc_stats
[i
].query_active
= false;
2066 static bool vi_should_enable_separate_dcc(struct r600_texture
*tex
)
2068 /* The minimum number of fullscreen draws per frame that is required
2070 return tex
->ps_draw_ratio
+ tex
->num_slow_clears
>= 5;
2073 /* Called by fast clear. */
2074 static void vi_separate_dcc_try_enable(struct r600_common_context
*rctx
,
2075 struct r600_texture
*tex
)
2077 /* The intent is to use this with shared displayable back buffers,
2078 * but it's not strictly limited only to them.
2080 if (!tex
->resource
.is_shared
||
2081 !(tex
->resource
.external_usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
) ||
2082 tex
->resource
.b
.b
.target
!= PIPE_TEXTURE_2D
||
2083 tex
->resource
.b
.b
.last_level
> 0 ||
2084 !tex
->surface
.dcc_size
)
2087 if (tex
->dcc_offset
)
2088 return; /* already enabled */
2090 /* Enable the DCC stat gathering. */
2091 if (!tex
->dcc_gather_statistics
) {
2092 tex
->dcc_gather_statistics
= true;
2093 vi_separate_dcc_start_query(&rctx
->b
, tex
);
2096 if (!vi_should_enable_separate_dcc(tex
))
2097 return; /* stats show that DCC decompression is too expensive */
2099 assert(tex
->surface
.level
[0].dcc_enabled
);
2100 assert(!tex
->dcc_separate_buffer
);
2102 r600_texture_discard_cmask(rctx
->screen
, tex
);
2104 /* Get a DCC buffer. */
2105 if (tex
->last_dcc_separate_buffer
) {
2106 assert(tex
->dcc_gather_statistics
);
2107 assert(!tex
->dcc_separate_buffer
);
2108 tex
->dcc_separate_buffer
= tex
->last_dcc_separate_buffer
;
2109 tex
->last_dcc_separate_buffer
= NULL
;
2111 tex
->dcc_separate_buffer
= (struct r600_resource
*)
2112 r600_aligned_buffer_create(rctx
->b
.screen
, 0,
2114 tex
->surface
.dcc_size
,
2115 tex
->surface
.dcc_alignment
);
2116 if (!tex
->dcc_separate_buffer
)
2120 /* dcc_offset is the absolute GPUVM address. */
2121 tex
->dcc_offset
= tex
->dcc_separate_buffer
->gpu_address
;
2123 /* no need to flag anything since this is called by fast clear that
2124 * flags framebuffer state
2129 * Called by pipe_context::flush_resource, the place where DCC decompression
2132 void vi_separate_dcc_process_and_reset_stats(struct pipe_context
*ctx
,
2133 struct r600_texture
*tex
)
2135 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
2136 struct pipe_query
*tmp
;
2137 unsigned i
= vi_get_context_dcc_stats_index(rctx
, tex
);
2138 bool query_active
= rctx
->dcc_stats
[i
].query_active
;
2139 bool disable
= false;
2141 if (rctx
->dcc_stats
[i
].ps_stats
[2]) {
2142 union pipe_query_result result
;
2144 /* Read the results. */
2145 ctx
->get_query_result(ctx
, rctx
->dcc_stats
[i
].ps_stats
[2],
2147 r600_query_hw_reset_buffers(rctx
,
2148 (struct r600_query_hw
*)
2149 rctx
->dcc_stats
[i
].ps_stats
[2]);
2151 /* Compute the approximate number of fullscreen draws. */
2152 tex
->ps_draw_ratio
=
2153 result
.pipeline_statistics
.ps_invocations
/
2154 (tex
->resource
.b
.b
.width0
* tex
->resource
.b
.b
.height0
);
2155 rctx
->last_tex_ps_draw_ratio
= tex
->ps_draw_ratio
;
2157 disable
= tex
->dcc_separate_buffer
&&
2158 !vi_should_enable_separate_dcc(tex
);
2161 tex
->num_slow_clears
= 0;
2163 /* stop the statistics query for ps_stats[0] */
2165 vi_separate_dcc_stop_query(ctx
, tex
);
2167 /* Move the queries in the queue by one. */
2168 tmp
= rctx
->dcc_stats
[i
].ps_stats
[2];
2169 rctx
->dcc_stats
[i
].ps_stats
[2] = rctx
->dcc_stats
[i
].ps_stats
[1];
2170 rctx
->dcc_stats
[i
].ps_stats
[1] = rctx
->dcc_stats
[i
].ps_stats
[0];
2171 rctx
->dcc_stats
[i
].ps_stats
[0] = tmp
;
2173 /* create and start a new query as ps_stats[0] */
2175 vi_separate_dcc_start_query(ctx
, tex
);
2178 assert(!tex
->last_dcc_separate_buffer
);
2179 tex
->last_dcc_separate_buffer
= tex
->dcc_separate_buffer
;
2180 tex
->dcc_separate_buffer
= NULL
;
2181 tex
->dcc_offset
= 0;
2182 /* no need to flag anything since this is called after
2183 * decompression that re-sets framebuffer state
2188 /* FAST COLOR CLEAR */
2190 static void evergreen_set_clear_color(struct r600_texture
*rtex
,
2191 enum pipe_format surface_format
,
2192 const union pipe_color_union
*color
)
2194 union util_color uc
;
2196 memset(&uc
, 0, sizeof(uc
));
2198 if (util_format_get_blocksizebits(surface_format
) == 128) {
2199 /* DCC fast clear only:
2200 * CLEAR_WORD0 = R = G = B
2203 assert(color
->ui
[0] == color
->ui
[1] &&
2204 color
->ui
[0] == color
->ui
[2]);
2205 uc
.ui
[0] = color
->ui
[0];
2206 uc
.ui
[1] = color
->ui
[3];
2207 } else if (util_format_is_pure_uint(surface_format
)) {
2208 util_format_write_4ui(surface_format
, color
->ui
, 0, &uc
, 0, 0, 0, 1, 1);
2209 } else if (util_format_is_pure_sint(surface_format
)) {
2210 util_format_write_4i(surface_format
, color
->i
, 0, &uc
, 0, 0, 0, 1, 1);
2212 util_pack_color(color
->f
, surface_format
, &uc
);
2215 memcpy(rtex
->color_clear_value
, &uc
, 2 * sizeof(uint32_t));
2218 static bool vi_get_fast_clear_parameters(enum pipe_format surface_format
,
2219 const union pipe_color_union
*color
,
2220 uint32_t* reset_value
,
2221 bool* clear_words_needed
)
2223 bool values
[4] = {};
2225 bool main_value
= false;
2226 bool extra_value
= false;
2228 const struct util_format_description
*desc
= util_format_description(surface_format
);
2230 if (desc
->block
.bits
== 128 &&
2231 (color
->ui
[0] != color
->ui
[1] ||
2232 color
->ui
[0] != color
->ui
[2]))
2235 *clear_words_needed
= true;
2236 *reset_value
= 0x20202020U
;
2238 /* If we want to clear without needing a fast clear eliminate step, we
2239 * can set each channel to 0 or 1 (or 0/max for integer formats). We
2240 * have two sets of flags, one for the last or first channel(extra) and
2241 * one for the other channels(main).
2244 if (surface_format
== PIPE_FORMAT_R11G11B10_FLOAT
||
2245 surface_format
== PIPE_FORMAT_B5G6R5_UNORM
||
2246 surface_format
== PIPE_FORMAT_B5G6R5_SRGB
) {
2248 } else if (desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
) {
2249 if(r600_translate_colorswap(surface_format
, false) <= 1)
2250 extra_channel
= desc
->nr_channels
- 1;
2256 for (i
= 0; i
< 4; ++i
) {
2257 int index
= desc
->swizzle
[i
] - PIPE_SWIZZLE_X
;
2259 if (desc
->swizzle
[i
] < PIPE_SWIZZLE_X
||
2260 desc
->swizzle
[i
] > PIPE_SWIZZLE_W
)
2263 if (desc
->channel
[i
].pure_integer
&&
2264 desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
2265 /* Use the maximum value for clamping the clear color. */
2266 int max
= u_bit_consecutive(0, desc
->channel
[i
].size
- 1);
2268 values
[i
] = color
->i
[i
] != 0;
2269 if (color
->i
[i
] != 0 && MIN2(color
->i
[i
], max
) != max
)
2271 } else if (desc
->channel
[i
].pure_integer
&&
2272 desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
2273 /* Use the maximum value for clamping the clear color. */
2274 unsigned max
= u_bit_consecutive(0, desc
->channel
[i
].size
);
2276 values
[i
] = color
->ui
[i
] != 0U;
2277 if (color
->ui
[i
] != 0U && MIN2(color
->ui
[i
], max
) != max
)
2280 values
[i
] = color
->f
[i
] != 0.0F
;
2281 if (color
->f
[i
] != 0.0F
&& color
->f
[i
] != 1.0F
)
2285 if (index
== extra_channel
)
2286 extra_value
= values
[i
];
2288 main_value
= values
[i
];
2291 for (int i
= 0; i
< 4; ++i
)
2292 if (values
[i
] != main_value
&&
2293 desc
->swizzle
[i
] - PIPE_SWIZZLE_X
!= extra_channel
&&
2294 desc
->swizzle
[i
] >= PIPE_SWIZZLE_X
&&
2295 desc
->swizzle
[i
] <= PIPE_SWIZZLE_W
)
2298 *clear_words_needed
= false;
2300 *reset_value
|= 0x80808080U
;
2303 *reset_value
|= 0x40404040U
;
2307 void vi_dcc_clear_level(struct r600_common_context
*rctx
,
2308 struct r600_texture
*rtex
,
2309 unsigned level
, unsigned clear_value
)
2311 struct pipe_resource
*dcc_buffer
;
2312 uint64_t dcc_offset
;
2314 assert(rtex
->dcc_offset
&& rtex
->surface
.level
[level
].dcc_enabled
);
2316 if (rtex
->dcc_separate_buffer
) {
2317 dcc_buffer
= &rtex
->dcc_separate_buffer
->b
.b
;
2320 dcc_buffer
= &rtex
->resource
.b
.b
;
2321 dcc_offset
= rtex
->dcc_offset
;
2324 dcc_offset
+= rtex
->surface
.level
[level
].dcc_offset
;
2326 rctx
->clear_buffer(&rctx
->b
, dcc_buffer
, dcc_offset
,
2327 rtex
->surface
.level
[level
].dcc_fast_clear_size
,
2328 clear_value
, R600_COHERENCY_CB_META
);
2331 /* Set the same micro tile mode as the destination of the last MSAA resolve.
2332 * This allows hitting the MSAA resolve fast path, which requires that both
2333 * src and dst micro tile modes match.
2335 static void si_set_optimal_micro_tile_mode(struct r600_common_screen
*rscreen
,
2336 struct r600_texture
*rtex
)
2338 if (rtex
->resource
.is_shared
||
2339 rtex
->resource
.b
.b
.nr_samples
<= 1 ||
2340 rtex
->surface
.micro_tile_mode
== rtex
->last_msaa_resolve_target_micro_mode
)
2343 assert(rtex
->surface
.level
[0].mode
== RADEON_SURF_MODE_2D
);
2344 assert(rtex
->resource
.b
.b
.last_level
== 0);
2346 /* These magic numbers were copied from addrlib. It doesn't use any
2347 * definitions for them either. They are all 2D_TILED_THIN1 modes with
2348 * different bpp and micro tile mode.
2350 if (rscreen
->chip_class
>= CIK
) {
2351 switch (rtex
->last_msaa_resolve_target_micro_mode
) {
2352 case 0: /* displayable */
2353 rtex
->surface
.tiling_index
[0] = 10;
2356 rtex
->surface
.tiling_index
[0] = 14;
2358 case 3: /* rotated */
2359 rtex
->surface
.tiling_index
[0] = 28;
2361 default: /* depth, thick */
2362 assert(!"unexpected micro mode");
2366 switch (rtex
->last_msaa_resolve_target_micro_mode
) {
2367 case 0: /* displayable */
2368 switch (rtex
->surface
.bpe
) {
2370 rtex
->surface
.tiling_index
[0] = 10;
2373 rtex
->surface
.tiling_index
[0] = 11;
2376 rtex
->surface
.tiling_index
[0] = 12;
2381 switch (rtex
->surface
.bpe
) {
2383 rtex
->surface
.tiling_index
[0] = 14;
2386 rtex
->surface
.tiling_index
[0] = 15;
2389 rtex
->surface
.tiling_index
[0] = 16;
2391 default: /* 8, 16 */
2392 rtex
->surface
.tiling_index
[0] = 17;
2396 default: /* depth, thick */
2397 assert(!"unexpected micro mode");
2402 rtex
->surface
.micro_tile_mode
= rtex
->last_msaa_resolve_target_micro_mode
;
2404 p_atomic_inc(&rscreen
->dirty_fb_counter
);
2405 p_atomic_inc(&rscreen
->dirty_tex_descriptor_counter
);
2408 void evergreen_do_fast_color_clear(struct r600_common_context
*rctx
,
2409 struct pipe_framebuffer_state
*fb
,
2410 struct r600_atom
*fb_state
,
2411 unsigned *buffers
, unsigned *dirty_cbufs
,
2412 const union pipe_color_union
*color
)
2416 /* This function is broken in BE, so just disable this path for now */
2417 #ifdef PIPE_ARCH_BIG_ENDIAN
2421 if (rctx
->render_cond
)
2424 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
2425 struct r600_texture
*tex
;
2426 unsigned clear_bit
= PIPE_CLEAR_COLOR0
<< i
;
2431 /* if this colorbuffer is not being cleared */
2432 if (!(*buffers
& clear_bit
))
2435 tex
= (struct r600_texture
*)fb
->cbufs
[i
]->texture
;
2437 /* the clear is allowed if all layers are bound */
2438 if (fb
->cbufs
[i
]->u
.tex
.first_layer
!= 0 ||
2439 fb
->cbufs
[i
]->u
.tex
.last_layer
!= util_max_layer(&tex
->resource
.b
.b
, 0)) {
2443 /* cannot clear mipmapped textures */
2444 if (fb
->cbufs
[i
]->texture
->last_level
!= 0) {
2448 /* only supported on tiled surfaces */
2449 if (tex
->surface
.level
[0].mode
< RADEON_SURF_MODE_1D
) {
2453 /* shared textures can't use fast clear without an explicit flush,
2454 * because there is no way to communicate the clear color among
2457 if (tex
->resource
.is_shared
&&
2458 !(tex
->resource
.external_usage
& PIPE_HANDLE_USAGE_EXPLICIT_FLUSH
))
2461 /* fast color clear with 1D tiling doesn't work on old kernels and CIK */
2462 if (tex
->surface
.level
[0].mode
== RADEON_SURF_MODE_1D
&&
2463 rctx
->chip_class
>= CIK
&&
2464 rctx
->screen
->info
.drm_major
== 2 &&
2465 rctx
->screen
->info
.drm_minor
< 38) {
2469 /* Fast clear is the most appropriate place to enable DCC for
2470 * displayable surfaces.
2472 if (rctx
->chip_class
>= VI
&&
2473 !(rctx
->screen
->debug_flags
& DBG_NO_DCC_FB
)) {
2474 vi_separate_dcc_try_enable(rctx
, tex
);
2476 /* Stoney can't do a CMASK-based clear, so all clears are
2477 * considered to be hypothetically slow clears, which
2478 * is weighed when determining to enable separate DCC.
2480 if (tex
->dcc_gather_statistics
&&
2481 rctx
->family
== CHIP_STONEY
)
2482 tex
->num_slow_clears
++;
2485 /* Try to clear DCC first, otherwise try CMASK. */
2486 if (tex
->dcc_offset
&& tex
->surface
.level
[0].dcc_enabled
) {
2487 uint32_t reset_value
;
2488 bool clear_words_needed
;
2490 if (rctx
->screen
->debug_flags
& DBG_NO_DCC_CLEAR
)
2493 if (!vi_get_fast_clear_parameters(fb
->cbufs
[i
]->format
,
2494 color
, &reset_value
,
2495 &clear_words_needed
))
2498 vi_dcc_clear_level(rctx
, tex
, 0, reset_value
);
2500 if (clear_words_needed
)
2501 tex
->dirty_level_mask
|= 1 << fb
->cbufs
[i
]->u
.tex
.level
;
2502 tex
->separate_dcc_dirty
= true;
2504 /* 128-bit formats are unusupported */
2505 if (util_format_get_blocksizebits(fb
->cbufs
[i
]->format
) > 64) {
2509 /* Stoney/RB+ doesn't work with CMASK fast clear. */
2510 if (rctx
->family
== CHIP_STONEY
)
2513 /* ensure CMASK is enabled */
2514 r600_texture_alloc_cmask_separate(rctx
->screen
, tex
);
2515 if (tex
->cmask
.size
== 0) {
2519 /* Do the fast clear. */
2520 rctx
->clear_buffer(&rctx
->b
, &tex
->cmask_buffer
->b
.b
,
2521 tex
->cmask
.offset
, tex
->cmask
.size
, 0,
2522 R600_COHERENCY_CB_META
);
2524 tex
->dirty_level_mask
|= 1 << fb
->cbufs
[i
]->u
.tex
.level
;
2527 /* We can change the micro tile mode before a full clear. */
2528 if (rctx
->screen
->chip_class
>= SI
)
2529 si_set_optimal_micro_tile_mode(rctx
->screen
, tex
);
2531 evergreen_set_clear_color(tex
, fb
->cbufs
[i
]->format
, color
);
2534 *dirty_cbufs
|= 1 << i
;
2535 rctx
->set_atom_dirty(rctx
, fb_state
, true);
2536 *buffers
&= ~clear_bit
;
2540 void r600_init_screen_texture_functions(struct r600_common_screen
*rscreen
)
2542 rscreen
->b
.resource_from_handle
= r600_texture_from_handle
;
2543 rscreen
->b
.resource_get_handle
= r600_texture_get_handle
;
2546 void r600_init_context_texture_functions(struct r600_common_context
*rctx
)
2548 rctx
->b
.create_surface
= r600_create_surface
;
2549 rctx
->b
.surface_destroy
= r600_surface_destroy
;
2550 rctx
->b
.clear_texture
= r600_clear_texture
;