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_formats.h"
31 #include "util/u_format_s3tc.h"
32 #include "util/u_memory.h"
35 /* Copy from a full GPU texture to a transfer's staging one. */
36 static void r600_copy_to_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
38 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
39 struct pipe_resource
*dst
= &rtransfer
->staging
->b
.b
;
40 struct pipe_resource
*src
= transfer
->resource
;
42 if (src
->nr_samples
<= 1) {
43 ctx
->resource_copy_region(ctx
, dst
, 0, 0, 0, 0,
44 src
, transfer
->level
, &transfer
->box
);
46 /* Resolve the resource. */
47 struct pipe_blit_info blit
;
49 memset(&blit
, 0, sizeof(blit
));
50 blit
.src
.resource
= src
;
51 blit
.src
.format
= src
->format
;
52 blit
.src
.level
= transfer
->level
;
53 blit
.src
.box
= transfer
->box
;
54 blit
.dst
.resource
= dst
;
55 blit
.dst
.format
= dst
->format
;
56 blit
.dst
.box
.width
= transfer
->box
.width
;
57 blit
.dst
.box
.height
= transfer
->box
.height
;
58 blit
.dst
.box
.depth
= transfer
->box
.depth
;
59 blit
.mask
= PIPE_MASK_RGBA
;
60 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
62 ctx
->blit(ctx
, &blit
);
66 /* Copy from a transfer's staging texture to a full GPU one. */
67 static void r600_copy_from_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
69 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
70 struct pipe_resource
*texture
= transfer
->resource
;
73 u_box_origin_2d(transfer
->box
.width
, transfer
->box
.height
, &sbox
);
75 ctx
->resource_copy_region(ctx
, texture
, transfer
->level
,
76 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
77 &rtransfer
->staging
->b
.b
,
81 unsigned r600_texture_get_offset(struct r600_texture
*rtex
,
82 unsigned level
, unsigned layer
)
84 return rtex
->surface
.level
[level
].offset
+
85 layer
* rtex
->surface
.level
[level
].slice_size
;
88 static int r600_init_surface(struct r600_screen
*rscreen
,
89 struct radeon_surface
*surface
,
90 const struct pipe_resource
*ptex
,
92 bool is_flushed_depth
,
95 const struct util_format_description
*desc
=
96 util_format_description(ptex
->format
);
97 bool is_depth
, is_stencil
;
99 is_depth
= util_format_has_depth(desc
);
100 is_stencil
= util_format_has_stencil(desc
);
102 surface
->npix_x
= ptex
->width0
;
103 surface
->npix_y
= ptex
->height0
;
104 surface
->npix_z
= ptex
->depth0
;
105 surface
->blk_w
= util_format_get_blockwidth(ptex
->format
);
106 surface
->blk_h
= util_format_get_blockheight(ptex
->format
);
108 surface
->array_size
= 1;
109 surface
->last_level
= ptex
->last_level
;
112 surface
->npix_y
= align(surface
->npix_y
, 8);
115 if (rscreen
->chip_class
>= EVERGREEN
&& !is_flushed_depth
&&
116 ptex
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
) {
117 surface
->bpe
= 4; /* stencil is allocated separately on evergreen */
119 surface
->bpe
= util_format_get_blocksize(ptex
->format
);
120 /* align byte per element on dword */
121 if (surface
->bpe
== 3) {
126 surface
->nsamples
= ptex
->nr_samples
? ptex
->nr_samples
: 1;
129 switch (array_mode
) {
130 case V_038000_ARRAY_1D_TILED_THIN1
:
131 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_1D
, MODE
);
133 case V_038000_ARRAY_2D_TILED_THIN1
:
134 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_2D
, MODE
);
136 case V_038000_ARRAY_LINEAR_ALIGNED
:
137 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_LINEAR_ALIGNED
, MODE
);
139 case V_038000_ARRAY_LINEAR_GENERAL
:
141 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_LINEAR
, MODE
);
144 switch (ptex
->target
) {
145 case PIPE_TEXTURE_1D
:
146 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_1D
, TYPE
);
148 case PIPE_TEXTURE_RECT
:
149 case PIPE_TEXTURE_2D
:
150 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_2D
, TYPE
);
152 case PIPE_TEXTURE_3D
:
153 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_3D
, TYPE
);
155 case PIPE_TEXTURE_1D_ARRAY
:
156 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_1D_ARRAY
, TYPE
);
157 surface
->array_size
= ptex
->array_size
;
159 case PIPE_TEXTURE_2D_ARRAY
:
160 case PIPE_TEXTURE_CUBE_ARRAY
: /* cube array layout like 2d layout for now */
161 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_2D_ARRAY
, TYPE
);
162 surface
->array_size
= ptex
->array_size
;
164 case PIPE_TEXTURE_CUBE
:
165 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_CUBEMAP
, TYPE
);
171 if (ptex
->bind
& PIPE_BIND_SCANOUT
) {
172 surface
->flags
|= RADEON_SURF_SCANOUT
;
175 if (!is_flushed_depth
&& is_depth
) {
176 surface
->flags
|= RADEON_SURF_ZBUFFER
;
179 surface
->flags
|= RADEON_SURF_SBUFFER
|
180 RADEON_SURF_HAS_SBUFFER_MIPTREE
;
186 static int r600_setup_surface(struct pipe_screen
*screen
,
187 struct r600_texture
*rtex
,
188 unsigned pitch_in_bytes_override
)
190 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
;
191 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
195 r
= rscreen
->ws
->surface_init(rscreen
->ws
, &rtex
->surface
);
199 rtex
->size
= rtex
->surface
.bo_size
;
200 if (pitch_in_bytes_override
&& pitch_in_bytes_override
!= rtex
->surface
.level
[0].pitch_bytes
) {
201 /* old ddx on evergreen over estimate alignment for 1d, only 1 level
204 rtex
->surface
.level
[0].nblk_x
= pitch_in_bytes_override
/ rtex
->surface
.bpe
;
205 rtex
->surface
.level
[0].pitch_bytes
= pitch_in_bytes_override
;
206 rtex
->surface
.level
[0].slice_size
= pitch_in_bytes_override
* rtex
->surface
.level
[0].nblk_y
;
207 if (rtex
->surface
.flags
& RADEON_SURF_SBUFFER
) {
208 rtex
->surface
.stencil_offset
=
209 rtex
->surface
.stencil_level
[0].offset
= rtex
->surface
.level
[0].slice_size
;
212 for (i
= 0; i
<= ptex
->last_level
; i
++) {
213 switch (rtex
->surface
.level
[i
].mode
) {
214 case RADEON_SURF_MODE_LINEAR_ALIGNED
:
215 rtex
->array_mode
[i
] = V_038000_ARRAY_LINEAR_ALIGNED
;
217 case RADEON_SURF_MODE_1D
:
218 rtex
->array_mode
[i
] = V_038000_ARRAY_1D_TILED_THIN1
;
220 case RADEON_SURF_MODE_2D
:
221 rtex
->array_mode
[i
] = V_038000_ARRAY_2D_TILED_THIN1
;
224 case RADEON_SURF_MODE_LINEAR
:
225 rtex
->array_mode
[i
] = 0;
232 static boolean
r600_texture_get_handle(struct pipe_screen
* screen
,
233 struct pipe_resource
*ptex
,
234 struct winsys_handle
*whandle
)
236 struct r600_texture
*rtex
= (struct r600_texture
*)ptex
;
237 struct r600_resource
*resource
= &rtex
->resource
;
238 struct radeon_surface
*surface
= &rtex
->surface
;
239 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
241 rscreen
->ws
->buffer_set_tiling(resource
->buf
,
243 surface
->level
[0].mode
>= RADEON_SURF_MODE_1D
?
244 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
,
245 surface
->level
[0].mode
>= RADEON_SURF_MODE_2D
?
246 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
,
247 surface
->bankw
, surface
->bankh
,
249 surface
->stencil_tile_split
,
251 rtex
->surface
.level
[0].pitch_bytes
);
253 return rscreen
->ws
->buffer_get_handle(resource
->buf
,
254 rtex
->surface
.level
[0].pitch_bytes
, whandle
);
257 static void r600_texture_destroy(struct pipe_screen
*screen
,
258 struct pipe_resource
*ptex
)
260 struct r600_texture
*rtex
= (struct r600_texture
*)ptex
;
261 struct r600_resource
*resource
= &rtex
->resource
;
263 if (rtex
->flushed_depth_texture
)
264 pipe_resource_reference((struct pipe_resource
**)&rtex
->flushed_depth_texture
, NULL
);
266 pb_reference(&resource
->buf
, NULL
);
270 static const struct u_resource_vtbl r600_texture_vtbl
;
272 /* The number of samples can be specified independently of the texture. */
273 void r600_texture_get_fmask_info(struct r600_screen
*rscreen
,
274 struct r600_texture
*rtex
,
276 struct r600_fmask_info
*out
)
278 /* FMASK is allocated pretty much like an ordinary texture.
279 * Here we use bpe in the units of bits, not bytes. */
280 struct radeon_surface fmask
= rtex
->surface
;
282 switch (nr_samples
) {
284 /* This should be 8,1, but we should set nsamples > 1
285 * for the allocator to treat it as a multisample surface.
286 * Let's set 4,2 then. */
300 R600_ERR("Invalid sample count for FMASK allocation.\n");
304 /* R600-R700 errata? Anyway, this fixes colorbuffer corruption. */
305 if (rscreen
->chip_class
<= R700
) {
309 if (rscreen
->chip_class
>= EVERGREEN
) {
310 fmask
.bankh
= nr_samples
<= 4 ? 4 : 1;
313 if (rscreen
->ws
->surface_init(rscreen
->ws
, &fmask
)) {
314 R600_ERR("Got error in surface_init while allocating FMASK.\n");
317 assert(fmask
.level
[0].mode
== RADEON_SURF_MODE_2D
);
319 out
->bank_height
= fmask
.bankh
;
320 out
->alignment
= MAX2(256, fmask
.bo_alignment
);
321 out
->size
= (fmask
.bo_size
+ 7) / 8;
324 static void r600_texture_allocate_fmask(struct r600_screen
*rscreen
,
325 struct r600_texture
*rtex
)
327 struct r600_fmask_info fmask
;
329 r600_texture_get_fmask_info(rscreen
, rtex
,
330 rtex
->resource
.b
.b
.nr_samples
, &fmask
);
332 /* Reserve space for FMASK while converting bits back to bytes. */
333 rtex
->fmask_bank_height
= fmask
.bank_height
;
334 rtex
->fmask_offset
= align(rtex
->size
, fmask
.alignment
);
335 rtex
->fmask_size
= fmask
.size
;
336 rtex
->size
= rtex
->fmask_offset
+ rtex
->fmask_size
;
338 printf("FMASK width=%u, height=%i, bits=%u, size=%u\n",
339 fmask
.npix_x
, fmask
.npix_y
, fmask
.bpe
* fmask
.nsamples
, rtex
->fmask_size
);
343 void r600_texture_get_cmask_info(struct r600_screen
*rscreen
,
344 struct r600_texture
*rtex
,
345 struct r600_cmask_info
*out
)
347 unsigned cmask_tile_width
= 8;
348 unsigned cmask_tile_height
= 8;
349 unsigned cmask_tile_elements
= cmask_tile_width
* cmask_tile_height
;
350 unsigned element_bits
= 4;
351 unsigned cmask_cache_bits
= 1024;
352 unsigned num_pipes
= rscreen
->tiling_info
.num_channels
;
353 unsigned pipe_interleave_bytes
= rscreen
->tiling_info
.group_bytes
;
355 unsigned elements_per_macro_tile
= (cmask_cache_bits
/ element_bits
) * num_pipes
;
356 unsigned pixels_per_macro_tile
= elements_per_macro_tile
* cmask_tile_elements
;
357 unsigned sqrt_pixels_per_macro_tile
= sqrt(pixels_per_macro_tile
);
358 unsigned macro_tile_width
= util_next_power_of_two(sqrt_pixels_per_macro_tile
);
359 unsigned macro_tile_height
= pixels_per_macro_tile
/ macro_tile_width
;
361 unsigned pitch_elements
= align(rtex
->surface
.npix_x
, macro_tile_width
);
362 unsigned height
= align(rtex
->surface
.npix_y
, macro_tile_height
);
364 unsigned base_align
= num_pipes
* pipe_interleave_bytes
;
365 unsigned slice_bytes
=
366 ((pitch_elements
* height
* element_bits
+ 7) / 8) / cmask_tile_elements
;
368 assert(macro_tile_width
% 128 == 0);
369 assert(macro_tile_height
% 128 == 0);
371 out
->slice_tile_max
= ((pitch_elements
* height
) / (128*128)) - 1;
372 out
->alignment
= MAX2(256, base_align
);
373 out
->size
= rtex
->surface
.array_size
* align(slice_bytes
, base_align
);
376 static void r600_texture_allocate_cmask(struct r600_screen
*rscreen
,
377 struct r600_texture
*rtex
)
379 struct r600_cmask_info cmask
;
381 r600_texture_get_cmask_info(rscreen
, rtex
, &cmask
);
383 rtex
->cmask_slice_tile_max
= cmask
.slice_tile_max
;
384 rtex
->cmask_offset
= align(rtex
->size
, cmask
.alignment
);
385 rtex
->cmask_size
= cmask
.size
;
386 rtex
->size
= rtex
->cmask_offset
+ rtex
->cmask_size
;
388 printf("CMASK: macro tile width = %u, macro tile height = %u, "
389 "pitch elements = %u, height = %u, slice tile max = %u\n",
390 macro_tile_width
, macro_tile_height
, pitch_elements
, height
,
391 rtex
->cmask_slice_tile_max
);
395 DEBUG_GET_ONCE_BOOL_OPTION(print_texdepth
, "R600_PRINT_TEXDEPTH", FALSE
);
397 static struct r600_texture
*
398 r600_texture_create_object(struct pipe_screen
*screen
,
399 const struct pipe_resource
*base
,
400 unsigned pitch_in_bytes_override
,
401 struct pb_buffer
*buf
,
403 struct radeon_surface
*surface
)
405 struct r600_texture
*rtex
;
406 struct r600_resource
*resource
;
407 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
410 rtex
= CALLOC_STRUCT(r600_texture
);
414 resource
= &rtex
->resource
;
415 resource
->b
.b
= *base
;
416 resource
->b
.vtbl
= &r600_texture_vtbl
;
417 pipe_reference_init(&resource
->b
.b
.reference
, 1);
418 resource
->b
.b
.screen
= screen
;
419 rtex
->pitch_override
= pitch_in_bytes_override
;
421 /* don't include stencil-only formats which we don't support for rendering */
422 rtex
->is_depth
= util_format_has_depth(util_format_description(rtex
->resource
.b
.b
.format
));
424 rtex
->surface
= *surface
;
425 r
= r600_setup_surface(screen
, rtex
,
426 pitch_in_bytes_override
);
432 if (base
->nr_samples
> 1 && !rtex
->is_depth
&& alloc_bo
) {
433 r600_texture_allocate_cmask(rscreen
, rtex
);
434 r600_texture_allocate_fmask(rscreen
, rtex
);
437 if (!rtex
->is_depth
&& base
->nr_samples
> 1 &&
438 (!rtex
->fmask_size
|| !rtex
->cmask_size
)) {
443 /* Tiled depth textures utilize the non-displayable tile order. */
444 rtex
->non_disp_tiling
= rtex
->is_depth
&& rtex
->surface
.level
[0].mode
>= RADEON_SURF_MODE_1D
;
446 /* Now create the backing buffer. */
447 if (!buf
&& alloc_bo
) {
448 unsigned base_align
= rtex
->surface
.bo_alignment
;
449 unsigned usage
= R600_TEX_IS_TILED(rtex
, 0) ? PIPE_USAGE_STATIC
: base
->usage
;
451 if (!r600_init_resource(rscreen
, resource
, rtex
->size
, base_align
, FALSE
, usage
)) {
456 /* This is usually the window framebuffer. We want it in VRAM, always. */
458 resource
->cs_buf
= rscreen
->ws
->buffer_get_cs_handle(buf
);
459 resource
->domains
= RADEON_DOMAIN_VRAM
;
462 if (rtex
->cmask_size
) {
463 /* Initialize the cmask to 0xCC (= compressed state). */
464 char *ptr
= rscreen
->ws
->buffer_map(resource
->cs_buf
, NULL
, PIPE_TRANSFER_WRITE
);
465 memset(ptr
+ rtex
->cmask_offset
, 0xCC, rtex
->cmask_size
);
466 rscreen
->ws
->buffer_unmap(resource
->cs_buf
);
469 if (debug_get_option_print_texdepth() && rtex
->is_depth
&& rtex
->non_disp_tiling
) {
470 printf("Texture: npix_x=%u, npix_y=%u, npix_z=%u, blk_w=%u, "
471 "blk_h=%u, blk_d=%u, array_size=%u, last_level=%u, "
472 "bpe=%u, nsamples=%u, flags=%u\n",
473 rtex
->surface
.npix_x
, rtex
->surface
.npix_y
,
474 rtex
->surface
.npix_z
, rtex
->surface
.blk_w
,
475 rtex
->surface
.blk_h
, rtex
->surface
.blk_d
,
476 rtex
->surface
.array_size
, rtex
->surface
.last_level
,
477 rtex
->surface
.bpe
, rtex
->surface
.nsamples
,
478 rtex
->surface
.flags
);
479 if (rtex
->surface
.flags
& RADEON_SURF_ZBUFFER
) {
480 for (int i
= 0; i
<= rtex
->surface
.last_level
; i
++) {
481 printf(" Z %i: offset=%llu, slice_size=%llu, npix_x=%u, "
482 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
483 "nblk_z=%u, pitch_bytes=%u, mode=%u\n",
484 i
, (unsigned long long)rtex
->surface
.level
[i
].offset
,
485 (unsigned long long)rtex
->surface
.level
[i
].slice_size
,
486 rtex
->surface
.level
[i
].npix_x
,
487 rtex
->surface
.level
[i
].npix_y
,
488 rtex
->surface
.level
[i
].npix_z
,
489 rtex
->surface
.level
[i
].nblk_x
,
490 rtex
->surface
.level
[i
].nblk_y
,
491 rtex
->surface
.level
[i
].nblk_z
,
492 rtex
->surface
.level
[i
].pitch_bytes
,
493 rtex
->surface
.level
[i
].mode
);
496 if (rtex
->surface
.flags
& RADEON_SURF_SBUFFER
) {
497 for (int i
= 0; i
<= rtex
->surface
.last_level
; i
++) {
498 printf(" S %i: offset=%llu, slice_size=%llu, npix_x=%u, "
499 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
500 "nblk_z=%u, pitch_bytes=%u, mode=%u\n",
501 i
, (unsigned long long)rtex
->surface
.stencil_level
[i
].offset
,
502 (unsigned long long)rtex
->surface
.stencil_level
[i
].slice_size
,
503 rtex
->surface
.stencil_level
[i
].npix_x
,
504 rtex
->surface
.stencil_level
[i
].npix_y
,
505 rtex
->surface
.stencil_level
[i
].npix_z
,
506 rtex
->surface
.stencil_level
[i
].nblk_x
,
507 rtex
->surface
.stencil_level
[i
].nblk_y
,
508 rtex
->surface
.stencil_level
[i
].nblk_z
,
509 rtex
->surface
.stencil_level
[i
].pitch_bytes
,
510 rtex
->surface
.stencil_level
[i
].mode
);
517 struct pipe_resource
*r600_texture_create(struct pipe_screen
*screen
,
518 const struct pipe_resource
*templ
)
520 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
521 struct radeon_surface surface
;
522 const struct util_format_description
*desc
= util_format_description(templ
->format
);
526 /* Default tiling mode for staging textures. */
527 array_mode
= V_038000_ARRAY_LINEAR_ALIGNED
;
529 /* Tiling doesn't work with the 422 (SUBSAMPLED) formats. That's not an issue,
530 * because 422 formats are used for videos, which prefer linear buffers
531 * for fast uploads anyway. */
532 if (!(templ
->flags
& R600_RESOURCE_FLAG_TRANSFER
) &&
533 desc
->layout
!= UTIL_FORMAT_LAYOUT_SUBSAMPLED
) {
534 if (!(templ
->bind
& PIPE_BIND_SCANOUT
) &&
535 templ
->usage
!= PIPE_USAGE_STAGING
&&
536 templ
->usage
!= PIPE_USAGE_STREAM
&&
537 templ
->target
!= PIPE_TEXTURE_1D
&&
538 templ
->target
!= PIPE_TEXTURE_1D_ARRAY
&&
539 templ
->height0
> 3) {
540 array_mode
= V_038000_ARRAY_2D_TILED_THIN1
;
541 } else if (util_format_is_compressed(templ
->format
)) {
542 array_mode
= V_038000_ARRAY_1D_TILED_THIN1
;
546 r
= r600_init_surface(rscreen
, &surface
, templ
, array_mode
,
547 templ
->flags
& R600_RESOURCE_FLAG_FLUSHED_DEPTH
,
552 r
= rscreen
->ws
->surface_best(rscreen
->ws
, &surface
);
556 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
,
557 0, NULL
, TRUE
, &surface
);
560 struct pipe_surface
*r600_create_surface_custom(struct pipe_context
*pipe
,
561 struct pipe_resource
*texture
,
562 const struct pipe_surface
*templ
,
563 unsigned width
, unsigned height
)
565 struct r600_surface
*surface
= CALLOC_STRUCT(r600_surface
);
567 assert(templ
->u
.tex
.first_layer
== templ
->u
.tex
.last_layer
);
570 pipe_reference_init(&surface
->base
.reference
, 1);
571 pipe_resource_reference(&surface
->base
.texture
, texture
);
572 surface
->base
.context
= pipe
;
573 surface
->base
.format
= templ
->format
;
574 surface
->base
.width
= width
;
575 surface
->base
.height
= height
;
576 surface
->base
.u
= templ
->u
;
577 return &surface
->base
;
580 static struct pipe_surface
*r600_create_surface(struct pipe_context
*pipe
,
581 struct pipe_resource
*texture
,
582 const struct pipe_surface
*templ
)
584 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
585 unsigned level
= templ
->u
.tex
.level
;
587 return r600_create_surface_custom(pipe
, texture
, templ
,
588 rtex
->surface
.level
[level
].npix_x
,
589 rtex
->surface
.level
[level
].npix_y
);
592 static void r600_surface_destroy(struct pipe_context
*pipe
,
593 struct pipe_surface
*surface
)
595 struct r600_surface
*surf
= (struct r600_surface
*)surface
;
596 pipe_resource_reference((struct pipe_resource
**)&surf
->cb_buffer_fmask
, NULL
);
597 pipe_resource_reference((struct pipe_resource
**)&surf
->cb_buffer_cmask
, NULL
);
598 pipe_resource_reference(&surface
->texture
, NULL
);
602 struct pipe_resource
*r600_texture_from_handle(struct pipe_screen
*screen
,
603 const struct pipe_resource
*templ
,
604 struct winsys_handle
*whandle
)
606 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
607 struct pb_buffer
*buf
= NULL
;
609 unsigned array_mode
= 0;
610 enum radeon_bo_layout micro
, macro
;
611 struct radeon_surface surface
;
614 /* Support only 2D textures without mipmaps */
615 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
) ||
616 templ
->depth0
!= 1 || templ
->last_level
!= 0)
619 buf
= rscreen
->ws
->buffer_from_handle(rscreen
->ws
, whandle
, &stride
);
623 rscreen
->ws
->buffer_get_tiling(buf
, µ
, ¯o
,
624 &surface
.bankw
, &surface
.bankh
,
626 &surface
.stencil_tile_split
,
629 if (macro
== RADEON_LAYOUT_TILED
)
630 array_mode
= V_0280A0_ARRAY_2D_TILED_THIN1
;
631 else if (micro
== RADEON_LAYOUT_TILED
)
632 array_mode
= V_0280A0_ARRAY_1D_TILED_THIN1
;
634 array_mode
= V_038000_ARRAY_LINEAR_ALIGNED
;
636 r
= r600_init_surface(rscreen
, &surface
, templ
, array_mode
, false, true);
640 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
,
641 stride
, buf
, FALSE
, &surface
);
644 bool r600_init_flushed_depth_texture(struct pipe_context
*ctx
,
645 struct pipe_resource
*texture
,
646 struct r600_texture
**staging
)
648 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
649 struct pipe_resource resource
;
650 struct r600_texture
**flushed_depth_texture
= staging
?
651 staging
: &rtex
->flushed_depth_texture
;
653 if (!staging
&& rtex
->flushed_depth_texture
)
654 return true; /* it's ready */
656 resource
.target
= texture
->target
;
657 resource
.format
= texture
->format
;
658 resource
.width0
= texture
->width0
;
659 resource
.height0
= texture
->height0
;
660 resource
.depth0
= texture
->depth0
;
661 resource
.array_size
= texture
->array_size
;
662 resource
.last_level
= texture
->last_level
;
663 resource
.nr_samples
= texture
->nr_samples
;
664 resource
.usage
= staging
? PIPE_USAGE_STAGING
: PIPE_USAGE_STATIC
;
665 resource
.bind
= texture
->bind
& ~PIPE_BIND_DEPTH_STENCIL
;
666 resource
.flags
= texture
->flags
| R600_RESOURCE_FLAG_FLUSHED_DEPTH
;
669 resource
.flags
|= R600_RESOURCE_FLAG_TRANSFER
;
671 *flushed_depth_texture
= (struct r600_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
672 if (*flushed_depth_texture
== NULL
) {
673 R600_ERR("failed to create temporary texture to hold flushed depth\n");
677 (*flushed_depth_texture
)->is_flushing_texture
= TRUE
;
678 (*flushed_depth_texture
)->non_disp_tiling
= false;
682 static void *r600_texture_transfer_map(struct pipe_context
*ctx
,
683 struct pipe_resource
*texture
,
686 const struct pipe_box
*box
,
687 struct pipe_transfer
**ptransfer
)
689 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
690 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
691 struct pipe_resource resource
;
692 struct r600_transfer
*trans
;
693 boolean use_staging_texture
= FALSE
;
694 enum pipe_format format
= texture
->format
;
695 struct radeon_winsys_cs_handle
*buf
;
699 if ((texture
->bind
& PIPE_BIND_GLOBAL
) && texture
->target
== PIPE_BUFFER
) {
700 return r600_compute_global_transfer_map(ctx
, texture
, level
, usage
, box
, ptransfer
);
703 /* We cannot map a tiled texture directly because the data is
704 * in a different order, therefore we do detiling using a blit.
706 * Also, use a temporary in GTT memory for read transfers, as
707 * the CPU is much happier reading out of cached system memory
708 * than uncached VRAM.
710 if (R600_TEX_IS_TILED(rtex
, level
)) {
711 use_staging_texture
= TRUE
;
714 /* Use a staging texture for uploads if the underlying BO is busy. */
715 if (!(usage
& PIPE_TRANSFER_READ
) &&
716 (rctx
->ws
->cs_is_buffer_referenced(rctx
->cs
, rtex
->resource
.cs_buf
, RADEON_USAGE_READWRITE
) ||
717 rctx
->ws
->buffer_is_busy(rtex
->resource
.buf
, RADEON_USAGE_READWRITE
))) {
718 use_staging_texture
= TRUE
;
721 if (texture
->flags
& R600_RESOURCE_FLAG_TRANSFER
) {
722 use_staging_texture
= FALSE
;
725 if (use_staging_texture
&& (usage
& PIPE_TRANSFER_MAP_DIRECTLY
)) {
729 trans
= CALLOC_STRUCT(r600_transfer
);
732 trans
->transfer
.resource
= texture
;
733 trans
->transfer
.level
= level
;
734 trans
->transfer
.usage
= usage
;
735 trans
->transfer
.box
= *box
;
736 if (rtex
->is_depth
) {
737 /* XXX: only readback the rectangle which is being mapped?
739 /* XXX: when discard is true, no need to read back from depth texture
741 struct r600_texture
*staging_depth
;
743 assert(rtex
->resource
.b
.b
.nr_samples
<= 1);
744 if (rtex
->resource
.b
.b
.nr_samples
> 1) {
745 R600_ERR("mapping MSAA zbuffer unimplemented\n");
750 if (!r600_init_flushed_depth_texture(ctx
, texture
, &staging_depth
)) {
751 R600_ERR("failed to create temporary texture to hold untiled copy\n");
756 r600_blit_decompress_depth(ctx
, rtex
, staging_depth
,
758 box
->z
, box
->z
+ box
->depth
- 1,
761 trans
->transfer
.stride
= staging_depth
->surface
.level
[level
].pitch_bytes
;
762 trans
->offset
= r600_texture_get_offset(staging_depth
, level
, box
->z
);
763 trans
->staging
= (struct r600_resource
*)staging_depth
;
764 } else if (use_staging_texture
) {
765 resource
.target
= PIPE_TEXTURE_2D
;
766 resource
.format
= texture
->format
;
767 resource
.width0
= box
->width
;
768 resource
.height0
= box
->height
;
770 resource
.array_size
= 1;
771 resource
.last_level
= 0;
772 resource
.nr_samples
= 0;
773 resource
.usage
= PIPE_USAGE_STAGING
;
775 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
;
776 /* For texture reading, the temporary (detiled) texture is used as
777 * a render target when blitting from a tiled texture. */
778 if (usage
& PIPE_TRANSFER_READ
) {
779 resource
.bind
|= PIPE_BIND_RENDER_TARGET
;
781 /* For texture writing, the temporary texture is used as a sampler
782 * when blitting into a tiled texture. */
783 if (usage
& PIPE_TRANSFER_WRITE
) {
784 resource
.bind
|= PIPE_BIND_SAMPLER_VIEW
;
786 /* Create the temporary texture. */
787 trans
->staging
= (struct r600_resource
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
788 if (trans
->staging
== NULL
) {
789 R600_ERR("failed to create temporary texture to hold untiled copy\n");
794 trans
->transfer
.stride
=
795 ((struct r600_texture
*)trans
->staging
)->surface
.level
[0].pitch_bytes
;
796 if (usage
& PIPE_TRANSFER_READ
) {
797 r600_copy_to_staging_texture(ctx
, trans
);
798 /* Always referenced in the blit. */
799 r600_flush(ctx
, NULL
, 0);
802 trans
->transfer
.stride
= rtex
->surface
.level
[level
].pitch_bytes
;
803 trans
->transfer
.layer_stride
= rtex
->surface
.level
[level
].slice_size
;
804 trans
->offset
= r600_texture_get_offset(rtex
, level
, box
->z
);
807 if (trans
->staging
) {
808 buf
= ((struct r600_resource
*)trans
->staging
)->cs_buf
;
810 buf
= ((struct r600_resource
*)texture
)->cs_buf
;
813 if (rtex
->is_depth
|| !trans
->staging
)
814 offset
= trans
->offset
+
815 box
->y
/ util_format_get_blockheight(format
) * trans
->transfer
.stride
+
816 box
->x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
818 if (!(map
= rctx
->ws
->buffer_map(buf
, rctx
->cs
, usage
))) {
819 pipe_resource_reference((struct pipe_resource
**)&trans
->staging
, NULL
);
824 *ptransfer
= &trans
->transfer
;
828 static void r600_texture_transfer_unmap(struct pipe_context
*ctx
,
829 struct pipe_transfer
* transfer
)
831 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
832 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
833 struct radeon_winsys_cs_handle
*buf
;
834 struct pipe_resource
*texture
= transfer
->resource
;
835 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
837 if ((transfer
->resource
->bind
& PIPE_BIND_GLOBAL
) && transfer
->resource
->target
== PIPE_BUFFER
) {
838 return r600_compute_global_transfer_unmap(ctx
, transfer
);
841 if (rtransfer
->staging
) {
842 buf
= ((struct r600_resource
*)rtransfer
->staging
)->cs_buf
;
844 buf
= ((struct r600_resource
*)transfer
->resource
)->cs_buf
;
846 rctx
->ws
->buffer_unmap(buf
);
848 if ((transfer
->usage
& PIPE_TRANSFER_WRITE
) && rtransfer
->staging
) {
849 if (rtex
->is_depth
) {
850 ctx
->resource_copy_region(ctx
, texture
, transfer
->level
,
851 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
852 &rtransfer
->staging
->b
.b
, transfer
->level
,
855 r600_copy_from_staging_texture(ctx
, rtransfer
);
859 if (rtransfer
->staging
)
860 pipe_resource_reference((struct pipe_resource
**)&rtransfer
->staging
, NULL
);
865 void r600_init_surface_functions(struct r600_context
*r600
)
867 r600
->context
.create_surface
= r600_create_surface
;
868 r600
->context
.surface_destroy
= r600_surface_destroy
;
871 static unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format
,
872 const unsigned char *swizzle_view
)
875 unsigned char swizzle
[4];
877 const uint32_t swizzle_shift
[4] = {
880 const uint32_t swizzle_bit
[4] = {
885 util_format_compose_swizzles(swizzle_format
, swizzle_view
, swizzle
);
887 memcpy(swizzle
, swizzle_format
, 4);
891 for (i
= 0; i
< 4; i
++) {
892 switch (swizzle
[i
]) {
893 case UTIL_FORMAT_SWIZZLE_Y
:
894 result
|= swizzle_bit
[1] << swizzle_shift
[i
];
896 case UTIL_FORMAT_SWIZZLE_Z
:
897 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
899 case UTIL_FORMAT_SWIZZLE_W
:
900 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
902 case UTIL_FORMAT_SWIZZLE_0
:
903 result
|= V_038010_SQ_SEL_0
<< swizzle_shift
[i
];
905 case UTIL_FORMAT_SWIZZLE_1
:
906 result
|= V_038010_SQ_SEL_1
<< swizzle_shift
[i
];
908 default: /* UTIL_FORMAT_SWIZZLE_X */
909 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
915 /* texture format translate */
916 uint32_t r600_translate_texformat(struct pipe_screen
*screen
,
917 enum pipe_format format
,
918 const unsigned char *swizzle_view
,
919 uint32_t *word4_p
, uint32_t *yuv_format_p
)
921 uint32_t result
= 0, word4
= 0, yuv_format
= 0;
922 const struct util_format_description
*desc
;
923 boolean uniform
= TRUE
;
924 static int r600_enable_s3tc
= -1;
925 bool is_srgb_valid
= FALSE
;
928 const uint32_t sign_bit
[4] = {
929 S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED
),
930 S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED
),
931 S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED
),
932 S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED
)
934 desc
= util_format_description(format
);
936 word4
|= r600_get_swizzle_combined(desc
->swizzle
, swizzle_view
);
938 /* Colorspace (return non-RGB formats directly). */
939 switch (desc
->colorspace
) {
940 /* Depth stencil formats */
941 case UTIL_FORMAT_COLORSPACE_ZS
:
943 case PIPE_FORMAT_Z16_UNORM
:
946 case PIPE_FORMAT_X24S8_UINT
:
947 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
948 case PIPE_FORMAT_Z24X8_UNORM
:
949 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
952 case PIPE_FORMAT_S8X24_UINT
:
953 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
954 case PIPE_FORMAT_X8Z24_UNORM
:
955 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
958 case PIPE_FORMAT_S8_UINT
:
960 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
962 case PIPE_FORMAT_Z32_FLOAT
:
963 result
= FMT_32_FLOAT
;
965 case PIPE_FORMAT_X32_S8X24_UINT
:
966 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
967 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
968 result
= FMT_X24_8_32_FLOAT
;
974 case UTIL_FORMAT_COLORSPACE_YUV
:
975 yuv_format
|= (1 << 30);
977 case PIPE_FORMAT_UYVY
:
978 case PIPE_FORMAT_YUYV
:
982 goto out_unknown
; /* XXX */
984 case UTIL_FORMAT_COLORSPACE_SRGB
:
985 word4
|= S_038010_FORCE_DEGAMMA(1);
992 if (r600_enable_s3tc
== -1) {
993 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
994 if (rscreen
->info
.drm_minor
>= 9)
995 r600_enable_s3tc
= 1;
997 r600_enable_s3tc
= debug_get_bool_option("R600_ENABLE_S3TC", FALSE
);
1000 if (desc
->layout
== UTIL_FORMAT_LAYOUT_RGTC
) {
1001 if (!r600_enable_s3tc
)
1005 case PIPE_FORMAT_RGTC1_SNORM
:
1006 case PIPE_FORMAT_LATC1_SNORM
:
1007 word4
|= sign_bit
[0];
1008 case PIPE_FORMAT_RGTC1_UNORM
:
1009 case PIPE_FORMAT_LATC1_UNORM
:
1012 case PIPE_FORMAT_RGTC2_SNORM
:
1013 case PIPE_FORMAT_LATC2_SNORM
:
1014 word4
|= sign_bit
[0] | sign_bit
[1];
1015 case PIPE_FORMAT_RGTC2_UNORM
:
1016 case PIPE_FORMAT_LATC2_UNORM
:
1024 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
1026 if (!r600_enable_s3tc
)
1029 if (!util_format_s3tc_enabled
) {
1034 case PIPE_FORMAT_DXT1_RGB
:
1035 case PIPE_FORMAT_DXT1_RGBA
:
1036 case PIPE_FORMAT_DXT1_SRGB
:
1037 case PIPE_FORMAT_DXT1_SRGBA
:
1039 is_srgb_valid
= TRUE
;
1041 case PIPE_FORMAT_DXT3_RGBA
:
1042 case PIPE_FORMAT_DXT3_SRGBA
:
1044 is_srgb_valid
= TRUE
;
1046 case PIPE_FORMAT_DXT5_RGBA
:
1047 case PIPE_FORMAT_DXT5_SRGBA
:
1049 is_srgb_valid
= TRUE
;
1056 if (desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
) {
1058 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
1059 case PIPE_FORMAT_G8R8_B8R8_UNORM
:
1062 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
1063 case PIPE_FORMAT_R8G8_R8B8_UNORM
:
1071 if (format
== PIPE_FORMAT_R9G9B9E5_FLOAT
) {
1072 result
= FMT_5_9_9_9_SHAREDEXP
;
1074 } else if (format
== PIPE_FORMAT_R11G11B10_FLOAT
) {
1075 result
= FMT_10_11_11_FLOAT
;
1080 for (i
= 0; i
< desc
->nr_channels
; i
++) {
1081 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
1082 word4
|= sign_bit
[i
];
1086 /* R8G8Bx_SNORM - XXX CxV8U8 */
1088 /* See whether the components are of the same size. */
1089 for (i
= 1; i
< desc
->nr_channels
; i
++) {
1090 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
1093 /* Non-uniform formats. */
1095 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
&&
1096 desc
->channel
[0].pure_integer
)
1097 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
1098 switch(desc
->nr_channels
) {
1100 if (desc
->channel
[0].size
== 5 &&
1101 desc
->channel
[1].size
== 6 &&
1102 desc
->channel
[2].size
== 5) {
1108 if (desc
->channel
[0].size
== 5 &&
1109 desc
->channel
[1].size
== 5 &&
1110 desc
->channel
[2].size
== 5 &&
1111 desc
->channel
[3].size
== 1) {
1112 result
= FMT_1_5_5_5
;
1115 if (desc
->channel
[0].size
== 10 &&
1116 desc
->channel
[1].size
== 10 &&
1117 desc
->channel
[2].size
== 10 &&
1118 desc
->channel
[3].size
== 2) {
1119 result
= FMT_2_10_10_10
;
1127 /* Find the first non-VOID channel. */
1128 for (i
= 0; i
< 4; i
++) {
1129 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
1137 /* uniform formats */
1138 switch (desc
->channel
[i
].type
) {
1139 case UTIL_FORMAT_TYPE_UNSIGNED
:
1140 case UTIL_FORMAT_TYPE_SIGNED
:
1142 if (!desc
->channel
[i
].normalized
&&
1143 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
1147 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
&&
1148 desc
->channel
[i
].pure_integer
)
1149 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
1151 switch (desc
->channel
[i
].size
) {
1153 switch (desc
->nr_channels
) {
1158 result
= FMT_4_4_4_4
;
1163 switch (desc
->nr_channels
) {
1171 result
= FMT_8_8_8_8
;
1172 is_srgb_valid
= TRUE
;
1177 switch (desc
->nr_channels
) {
1185 result
= FMT_16_16_16_16
;
1190 switch (desc
->nr_channels
) {
1198 result
= FMT_32_32_32_32
;
1204 case UTIL_FORMAT_TYPE_FLOAT
:
1205 switch (desc
->channel
[i
].size
) {
1207 switch (desc
->nr_channels
) {
1209 result
= FMT_16_FLOAT
;
1212 result
= FMT_16_16_FLOAT
;
1215 result
= FMT_16_16_16_16_FLOAT
;
1220 switch (desc
->nr_channels
) {
1222 result
= FMT_32_FLOAT
;
1225 result
= FMT_32_32_FLOAT
;
1228 result
= FMT_32_32_32_32_FLOAT
;
1237 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
&& !is_srgb_valid
)
1242 *yuv_format_p
= yuv_format
;
1245 /* R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format)); */
1249 static const struct u_resource_vtbl r600_texture_vtbl
=
1251 r600_texture_get_handle
, /* get_handle */
1252 r600_texture_destroy
, /* resource_destroy */
1253 r600_texture_transfer_map
, /* transfer_map */
1254 NULL
, /* transfer_flush_region */
1255 r600_texture_transfer_unmap
, /* transfer_unmap */
1256 NULL
/* transfer_inline_write */