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"
34 /* Copy from a full GPU texture to a transfer's staging one. */
35 static void r600_copy_to_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
37 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
38 struct pipe_resource
*texture
= transfer
->resource
;
40 ctx
->resource_copy_region(ctx
, &rtransfer
->staging
->b
.b
,
41 0, 0, 0, 0, texture
, transfer
->level
,
46 /* Copy from a transfer's staging texture to a full GPU one. */
47 static void r600_copy_from_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
49 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
50 struct pipe_resource
*texture
= transfer
->resource
;
53 u_box_origin_2d(transfer
->box
.width
, transfer
->box
.height
, &sbox
);
55 ctx
->resource_copy_region(ctx
, texture
, transfer
->level
,
56 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
57 &rtransfer
->staging
->b
.b
,
61 unsigned r600_texture_get_offset(struct r600_texture
*rtex
,
62 unsigned level
, unsigned layer
)
64 return rtex
->surface
.level
[level
].offset
+
65 layer
* rtex
->surface
.level
[level
].slice_size
;
68 static int r600_init_surface(struct r600_screen
*rscreen
,
69 struct radeon_surface
*surface
,
70 const struct pipe_resource
*ptex
,
72 bool is_transfer
, bool is_flushed_depth
)
74 const struct util_format_description
*desc
=
75 util_format_description(ptex
->format
);
76 bool is_depth
, is_stencil
;
78 is_depth
= util_format_has_depth(desc
);
79 is_stencil
= util_format_has_stencil(desc
);
81 surface
->npix_x
= ptex
->width0
;
82 surface
->npix_y
= ptex
->height0
;
83 surface
->npix_z
= ptex
->depth0
;
84 surface
->blk_w
= util_format_get_blockwidth(ptex
->format
);
85 surface
->blk_h
= util_format_get_blockheight(ptex
->format
);
87 surface
->array_size
= 1;
88 surface
->last_level
= ptex
->last_level
;
90 if (rscreen
->chip_class
>= EVERGREEN
&&
91 !is_transfer
&& !is_flushed_depth
&&
92 ptex
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
) {
93 surface
->bpe
= 4; /* stencil is allocated separately on evergreen */
95 surface
->bpe
= util_format_get_blocksize(ptex
->format
);
96 /* align byte per element on dword */
97 if (surface
->bpe
== 3) {
102 surface
->nsamples
= ptex
->nr_samples
? ptex
->nr_samples
: 1;
105 switch (array_mode
) {
106 case V_038000_ARRAY_1D_TILED_THIN1
:
107 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_1D
, MODE
);
109 case V_038000_ARRAY_2D_TILED_THIN1
:
110 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_2D
, MODE
);
112 case V_038000_ARRAY_LINEAR_ALIGNED
:
113 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_LINEAR_ALIGNED
, MODE
);
115 case V_038000_ARRAY_LINEAR_GENERAL
:
117 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_LINEAR
, MODE
);
120 switch (ptex
->target
) {
121 case PIPE_TEXTURE_1D
:
122 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_1D
, TYPE
);
124 case PIPE_TEXTURE_RECT
:
125 case PIPE_TEXTURE_2D
:
126 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_2D
, TYPE
);
128 case PIPE_TEXTURE_3D
:
129 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_3D
, TYPE
);
131 case PIPE_TEXTURE_1D_ARRAY
:
132 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_1D_ARRAY
, TYPE
);
133 surface
->array_size
= ptex
->array_size
;
135 case PIPE_TEXTURE_2D_ARRAY
:
136 case PIPE_TEXTURE_CUBE_ARRAY
: /* cube array layout like 2d layout for now */
137 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_2D_ARRAY
, TYPE
);
138 surface
->array_size
= ptex
->array_size
;
140 case PIPE_TEXTURE_CUBE
:
141 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_CUBEMAP
, TYPE
);
147 if (ptex
->bind
& PIPE_BIND_SCANOUT
) {
148 surface
->flags
|= RADEON_SURF_SCANOUT
;
151 if (!is_transfer
&& !is_flushed_depth
&& is_depth
) {
152 surface
->flags
|= RADEON_SURF_ZBUFFER
;
155 surface
->flags
|= RADEON_SURF_SBUFFER
|
156 RADEON_SURF_HAS_SBUFFER_MIPTREE
;
162 static int r600_setup_surface(struct pipe_screen
*screen
,
163 struct r600_texture
*rtex
,
164 unsigned pitch_in_bytes_override
)
166 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
;
167 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
171 r
= rscreen
->ws
->surface_init(rscreen
->ws
, &rtex
->surface
);
175 rtex
->size
= rtex
->surface
.bo_size
;
176 if (pitch_in_bytes_override
&& pitch_in_bytes_override
!= rtex
->surface
.level
[0].pitch_bytes
) {
177 /* old ddx on evergreen over estimate alignment for 1d, only 1 level
180 rtex
->surface
.level
[0].nblk_x
= pitch_in_bytes_override
/ rtex
->surface
.bpe
;
181 rtex
->surface
.level
[0].pitch_bytes
= pitch_in_bytes_override
;
182 rtex
->surface
.level
[0].slice_size
= pitch_in_bytes_override
* rtex
->surface
.level
[0].nblk_y
;
183 if (rtex
->surface
.flags
& RADEON_SURF_SBUFFER
) {
184 rtex
->surface
.stencil_offset
=
185 rtex
->surface
.stencil_level
[0].offset
= rtex
->surface
.level
[0].slice_size
;
188 for (i
= 0; i
<= ptex
->last_level
; i
++) {
189 switch (rtex
->surface
.level
[i
].mode
) {
190 case RADEON_SURF_MODE_LINEAR_ALIGNED
:
191 rtex
->array_mode
[i
] = V_038000_ARRAY_LINEAR_ALIGNED
;
193 case RADEON_SURF_MODE_1D
:
194 rtex
->array_mode
[i
] = V_038000_ARRAY_1D_TILED_THIN1
;
196 case RADEON_SURF_MODE_2D
:
197 rtex
->array_mode
[i
] = V_038000_ARRAY_2D_TILED_THIN1
;
200 case RADEON_SURF_MODE_LINEAR
:
201 rtex
->array_mode
[i
] = 0;
208 static boolean
r600_texture_get_handle(struct pipe_screen
* screen
,
209 struct pipe_resource
*ptex
,
210 struct winsys_handle
*whandle
)
212 struct r600_texture
*rtex
= (struct r600_texture
*)ptex
;
213 struct r600_resource
*resource
= &rtex
->resource
;
214 struct radeon_surface
*surface
= &rtex
->surface
;
215 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
217 rscreen
->ws
->buffer_set_tiling(resource
->buf
,
219 surface
->level
[0].mode
>= RADEON_SURF_MODE_1D
?
220 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
,
221 surface
->level
[0].mode
>= RADEON_SURF_MODE_2D
?
222 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
,
223 surface
->bankw
, surface
->bankh
,
225 surface
->stencil_tile_split
,
227 rtex
->surface
.level
[0].pitch_bytes
);
229 return rscreen
->ws
->buffer_get_handle(resource
->buf
,
230 rtex
->surface
.level
[0].pitch_bytes
, whandle
);
233 static void r600_texture_destroy(struct pipe_screen
*screen
,
234 struct pipe_resource
*ptex
)
236 struct r600_texture
*rtex
= (struct r600_texture
*)ptex
;
237 struct r600_resource
*resource
= &rtex
->resource
;
239 if (rtex
->flushed_depth_texture
)
240 pipe_resource_reference((struct pipe_resource
**)&rtex
->flushed_depth_texture
, NULL
);
242 pb_reference(&resource
->buf
, NULL
);
246 static const struct u_resource_vtbl r600_texture_vtbl
;
248 /* The number of samples can be specified independently of the texture. */
249 void r600_texture_get_fmask_info(struct r600_screen
*rscreen
,
250 struct r600_texture
*rtex
,
252 struct r600_fmask_info
*out
)
254 /* FMASK is allocated pretty much like an ordinary texture.
255 * Here we use bpe in the units of bits, not bytes. */
256 struct radeon_surface fmask
= rtex
->surface
;
258 switch (nr_samples
) {
260 /* This should be 8,1, but we should set nsamples > 1
261 * for the allocator to treat it as a multisample surface.
262 * Let's set 4,2 then. */
276 R600_ERR("Invalid sample count for FMASK allocation.\n");
280 /* R600-R700 errata? Anyway, this fixes colorbuffer corruption. */
281 if (rscreen
->chip_class
<= R700
) {
285 if (rscreen
->chip_class
>= EVERGREEN
) {
286 fmask
.bankh
= nr_samples
<= 4 ? 4 : 1;
289 if (rscreen
->ws
->surface_init(rscreen
->ws
, &fmask
)) {
290 R600_ERR("Got error in surface_init while allocating FMASK.\n");
293 assert(fmask
.level
[0].mode
== RADEON_SURF_MODE_2D
);
295 out
->bank_height
= fmask
.bankh
;
296 out
->alignment
= MAX2(256, fmask
.bo_alignment
);
297 out
->size
= (fmask
.bo_size
+ 7) / 8;
300 static void r600_texture_allocate_fmask(struct r600_screen
*rscreen
,
301 struct r600_texture
*rtex
)
303 struct r600_fmask_info fmask
;
305 r600_texture_get_fmask_info(rscreen
, rtex
,
306 rtex
->resource
.b
.b
.nr_samples
, &fmask
);
308 /* Reserve space for FMASK while converting bits back to bytes. */
309 rtex
->fmask_bank_height
= fmask
.bank_height
;
310 rtex
->fmask_offset
= align(rtex
->size
, fmask
.alignment
);
311 rtex
->fmask_size
= fmask
.size
;
312 rtex
->size
= rtex
->fmask_offset
+ rtex
->fmask_size
;
314 printf("FMASK width=%u, height=%i, bits=%u, size=%u\n",
315 fmask
.npix_x
, fmask
.npix_y
, fmask
.bpe
* fmask
.nsamples
, rtex
->fmask_size
);
319 void r600_texture_get_cmask_info(struct r600_screen
*rscreen
,
320 struct r600_texture
*rtex
,
321 struct r600_cmask_info
*out
)
323 unsigned cmask_tile_width
= 8;
324 unsigned cmask_tile_height
= 8;
325 unsigned cmask_tile_elements
= cmask_tile_width
* cmask_tile_height
;
326 unsigned element_bits
= 4;
327 unsigned cmask_cache_bits
= 1024;
328 unsigned num_pipes
= rscreen
->tiling_info
.num_channels
;
329 unsigned pipe_interleave_bytes
= rscreen
->tiling_info
.group_bytes
;
331 unsigned elements_per_macro_tile
= (cmask_cache_bits
/ element_bits
) * num_pipes
;
332 unsigned pixels_per_macro_tile
= elements_per_macro_tile
* cmask_tile_elements
;
333 unsigned sqrt_pixels_per_macro_tile
= sqrt(pixels_per_macro_tile
);
334 unsigned macro_tile_width
= util_next_power_of_two(sqrt_pixels_per_macro_tile
);
335 unsigned macro_tile_height
= pixels_per_macro_tile
/ macro_tile_width
;
337 unsigned pitch_elements
= align(rtex
->surface
.npix_x
, macro_tile_width
);
338 unsigned height
= align(rtex
->surface
.npix_y
, macro_tile_height
);
340 unsigned base_align
= num_pipes
* pipe_interleave_bytes
;
341 unsigned slice_bytes
=
342 ((pitch_elements
* height
* element_bits
+ 7) / 8) / cmask_tile_elements
;
344 assert(macro_tile_width
% 128 == 0);
345 assert(macro_tile_height
% 128 == 0);
347 out
->slice_tile_max
= ((pitch_elements
* height
) / (128*128)) - 1;
348 out
->alignment
= MAX2(256, base_align
);
349 out
->size
= rtex
->surface
.array_size
* align(slice_bytes
, base_align
);
352 static void r600_texture_allocate_cmask(struct r600_screen
*rscreen
,
353 struct r600_texture
*rtex
)
355 struct r600_cmask_info cmask
;
357 r600_texture_get_cmask_info(rscreen
, rtex
, &cmask
);
359 rtex
->cmask_slice_tile_max
= cmask
.slice_tile_max
;
360 rtex
->cmask_offset
= align(rtex
->size
, cmask
.alignment
);
361 rtex
->cmask_size
= cmask
.size
;
362 rtex
->size
= rtex
->cmask_offset
+ rtex
->cmask_size
;
364 printf("CMASK: macro tile width = %u, macro tile height = %u, "
365 "pitch elements = %u, height = %u, slice tile max = %u\n",
366 macro_tile_width
, macro_tile_height
, pitch_elements
, height
,
367 rtex
->cmask_slice_tile_max
);
371 DEBUG_GET_ONCE_BOOL_OPTION(print_texdepth
, "R600_PRINT_TEXDEPTH", FALSE
);
373 static struct r600_texture
*
374 r600_texture_create_object(struct pipe_screen
*screen
,
375 const struct pipe_resource
*base
,
376 unsigned pitch_in_bytes_override
,
377 struct pb_buffer
*buf
,
379 struct radeon_surface
*surface
)
381 struct r600_texture
*rtex
;
382 struct r600_resource
*resource
;
383 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
386 rtex
= CALLOC_STRUCT(r600_texture
);
390 resource
= &rtex
->resource
;
391 resource
->b
.b
= *base
;
392 resource
->b
.vtbl
= &r600_texture_vtbl
;
393 pipe_reference_init(&resource
->b
.b
.reference
, 1);
394 resource
->b
.b
.screen
= screen
;
395 rtex
->pitch_override
= pitch_in_bytes_override
;
397 /* don't include stencil-only formats which we don't support for rendering */
398 rtex
->is_depth
= util_format_has_depth(util_format_description(rtex
->resource
.b
.b
.format
));
400 rtex
->surface
= *surface
;
401 r
= r600_setup_surface(screen
, rtex
,
402 pitch_in_bytes_override
);
408 if (base
->nr_samples
> 1 && !rtex
->is_depth
&& alloc_bo
) {
409 r600_texture_allocate_cmask(rscreen
, rtex
);
410 r600_texture_allocate_fmask(rscreen
, rtex
);
413 if (!rtex
->is_depth
&& base
->nr_samples
> 1 &&
414 (!rtex
->fmask_size
|| !rtex
->cmask_size
)) {
419 /* Tiled depth textures utilize the non-displayable tile order. */
420 rtex
->non_disp_tiling
= rtex
->is_depth
&& rtex
->surface
.level
[0].mode
>= RADEON_SURF_MODE_1D
;
422 /* Now create the backing buffer. */
423 if (!buf
&& alloc_bo
) {
424 unsigned base_align
= rtex
->surface
.bo_alignment
;
425 unsigned usage
= R600_TEX_IS_TILED(rtex
, 0) ? PIPE_USAGE_STATIC
: base
->usage
;
427 if (!r600_init_resource(rscreen
, resource
, rtex
->size
, base_align
, base
->bind
, usage
)) {
432 /* This is usually the window framebuffer. We want it in VRAM, always. */
434 resource
->cs_buf
= rscreen
->ws
->buffer_get_cs_handle(buf
);
435 resource
->domains
= RADEON_DOMAIN_VRAM
;
438 if (rtex
->cmask_size
) {
439 /* Initialize the cmask to 0xCC (= compressed state). */
440 char *ptr
= rscreen
->ws
->buffer_map(resource
->cs_buf
, NULL
, PIPE_TRANSFER_WRITE
);
441 memset(ptr
+ rtex
->cmask_offset
, 0xCC, rtex
->cmask_size
);
442 rscreen
->ws
->buffer_unmap(resource
->cs_buf
);
445 if (debug_get_option_print_texdepth() && rtex
->is_depth
&& rtex
->non_disp_tiling
) {
446 printf("Texture: npix_x=%u, npix_y=%u, npix_z=%u, blk_w=%u, "
447 "blk_h=%u, blk_d=%u, array_size=%u, last_level=%u, "
448 "bpe=%u, nsamples=%u, flags=%u\n",
449 rtex
->surface
.npix_x
, rtex
->surface
.npix_y
,
450 rtex
->surface
.npix_z
, rtex
->surface
.blk_w
,
451 rtex
->surface
.blk_h
, rtex
->surface
.blk_d
,
452 rtex
->surface
.array_size
, rtex
->surface
.last_level
,
453 rtex
->surface
.bpe
, rtex
->surface
.nsamples
,
454 rtex
->surface
.flags
);
455 if (rtex
->surface
.flags
& RADEON_SURF_ZBUFFER
) {
456 for (int i
= 0; i
<= rtex
->surface
.last_level
; i
++) {
457 printf(" Z %i: offset=%llu, slice_size=%llu, npix_x=%u, "
458 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
459 "nblk_z=%u, pitch_bytes=%u, mode=%u\n",
460 i
, (unsigned long long)rtex
->surface
.level
[i
].offset
,
461 (unsigned long long)rtex
->surface
.level
[i
].slice_size
,
462 rtex
->surface
.level
[i
].npix_x
,
463 rtex
->surface
.level
[i
].npix_y
,
464 rtex
->surface
.level
[i
].npix_z
,
465 rtex
->surface
.level
[i
].nblk_x
,
466 rtex
->surface
.level
[i
].nblk_y
,
467 rtex
->surface
.level
[i
].nblk_z
,
468 rtex
->surface
.level
[i
].pitch_bytes
,
469 rtex
->surface
.level
[i
].mode
);
472 if (rtex
->surface
.flags
& RADEON_SURF_SBUFFER
) {
473 for (int i
= 0; i
<= rtex
->surface
.last_level
; i
++) {
474 printf(" S %i: offset=%llu, slice_size=%llu, npix_x=%u, "
475 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
476 "nblk_z=%u, pitch_bytes=%u, mode=%u\n",
477 i
, (unsigned long long)rtex
->surface
.stencil_level
[i
].offset
,
478 (unsigned long long)rtex
->surface
.stencil_level
[i
].slice_size
,
479 rtex
->surface
.stencil_level
[i
].npix_x
,
480 rtex
->surface
.stencil_level
[i
].npix_y
,
481 rtex
->surface
.stencil_level
[i
].npix_z
,
482 rtex
->surface
.stencil_level
[i
].nblk_x
,
483 rtex
->surface
.stencil_level
[i
].nblk_y
,
484 rtex
->surface
.stencil_level
[i
].nblk_z
,
485 rtex
->surface
.stencil_level
[i
].pitch_bytes
,
486 rtex
->surface
.stencil_level
[i
].mode
);
493 struct pipe_resource
*r600_texture_create(struct pipe_screen
*screen
,
494 const struct pipe_resource
*templ
)
496 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
497 struct radeon_surface surface
;
498 unsigned array_mode
= 0;
501 if (!(templ
->flags
& R600_RESOURCE_FLAG_TRANSFER
)) {
502 if (!(templ
->bind
& PIPE_BIND_SCANOUT
) &&
503 templ
->usage
!= PIPE_USAGE_STAGING
&&
504 templ
->usage
!= PIPE_USAGE_STREAM
) {
505 array_mode
= V_038000_ARRAY_2D_TILED_THIN1
;
506 } else if (util_format_is_compressed(templ
->format
)) {
507 array_mode
= V_038000_ARRAY_1D_TILED_THIN1
;
511 /* XXX tiling is broken for the 422 formats */
512 if (util_format_description(templ
->format
)->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
)
513 array_mode
= V_038000_ARRAY_LINEAR_ALIGNED
;
515 r
= r600_init_surface(rscreen
, &surface
, templ
, array_mode
,
516 templ
->flags
& R600_RESOURCE_FLAG_TRANSFER
,
517 templ
->flags
& R600_RESOURCE_FLAG_FLUSHED_DEPTH
);
521 r
= rscreen
->ws
->surface_best(rscreen
->ws
, &surface
);
525 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
,
526 0, NULL
, TRUE
, &surface
);
529 struct pipe_surface
*r600_create_surface_custom(struct pipe_context
*pipe
,
530 struct pipe_resource
*texture
,
531 const struct pipe_surface
*templ
,
532 unsigned width
, unsigned height
)
534 struct r600_surface
*surface
= CALLOC_STRUCT(r600_surface
);
536 assert(templ
->u
.tex
.first_layer
== templ
->u
.tex
.last_layer
);
539 pipe_reference_init(&surface
->base
.reference
, 1);
540 pipe_resource_reference(&surface
->base
.texture
, texture
);
541 surface
->base
.context
= pipe
;
542 surface
->base
.format
= templ
->format
;
543 surface
->base
.width
= width
;
544 surface
->base
.height
= height
;
545 surface
->base
.usage
= templ
->usage
;
546 surface
->base
.u
= templ
->u
;
547 return &surface
->base
;
550 static struct pipe_surface
*r600_create_surface(struct pipe_context
*pipe
,
551 struct pipe_resource
*texture
,
552 const struct pipe_surface
*templ
)
554 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
555 unsigned level
= templ
->u
.tex
.level
;
557 return r600_create_surface_custom(pipe
, texture
, templ
,
558 rtex
->surface
.level
[level
].npix_x
,
559 rtex
->surface
.level
[level
].npix_y
);
562 static void r600_surface_destroy(struct pipe_context
*pipe
,
563 struct pipe_surface
*surface
)
565 struct r600_surface
*surf
= (struct r600_surface
*)surface
;
566 pipe_resource_reference((struct pipe_resource
**)&surf
->cb_buffer_fmask
, NULL
);
567 pipe_resource_reference((struct pipe_resource
**)&surf
->cb_buffer_cmask
, NULL
);
568 pipe_resource_reference(&surface
->texture
, NULL
);
572 struct pipe_resource
*r600_texture_from_handle(struct pipe_screen
*screen
,
573 const struct pipe_resource
*templ
,
574 struct winsys_handle
*whandle
)
576 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
577 struct pb_buffer
*buf
= NULL
;
579 unsigned array_mode
= 0;
580 enum radeon_bo_layout micro
, macro
;
581 struct radeon_surface surface
;
584 /* Support only 2D textures without mipmaps */
585 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
) ||
586 templ
->depth0
!= 1 || templ
->last_level
!= 0)
589 buf
= rscreen
->ws
->buffer_from_handle(rscreen
->ws
, whandle
, &stride
);
593 rscreen
->ws
->buffer_get_tiling(buf
, µ
, ¯o
,
594 &surface
.bankw
, &surface
.bankh
,
596 &surface
.stencil_tile_split
,
599 if (macro
== RADEON_LAYOUT_TILED
)
600 array_mode
= V_0280A0_ARRAY_2D_TILED_THIN1
;
601 else if (micro
== RADEON_LAYOUT_TILED
)
602 array_mode
= V_0280A0_ARRAY_1D_TILED_THIN1
;
606 r
= r600_init_surface(rscreen
, &surface
, templ
, array_mode
, false, false);
610 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
,
611 stride
, buf
, FALSE
, &surface
);
614 bool r600_init_flushed_depth_texture(struct pipe_context
*ctx
,
615 struct pipe_resource
*texture
,
616 struct r600_texture
**staging
)
618 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
619 struct pipe_resource resource
;
620 struct r600_texture
**flushed_depth_texture
= staging
?
621 staging
: &rtex
->flushed_depth_texture
;
623 if (!staging
&& rtex
->flushed_depth_texture
)
624 return true; /* it's ready */
626 resource
.target
= texture
->target
;
627 resource
.format
= texture
->format
;
628 resource
.width0
= texture
->width0
;
629 resource
.height0
= texture
->height0
;
630 resource
.depth0
= texture
->depth0
;
631 resource
.array_size
= texture
->array_size
;
632 resource
.last_level
= texture
->last_level
;
633 resource
.nr_samples
= texture
->nr_samples
;
634 resource
.usage
= staging
? PIPE_USAGE_STAGING
: PIPE_USAGE_STATIC
;
635 resource
.bind
= texture
->bind
& ~PIPE_BIND_DEPTH_STENCIL
;
636 resource
.flags
= texture
->flags
| R600_RESOURCE_FLAG_FLUSHED_DEPTH
;
639 resource
.flags
|= R600_RESOURCE_FLAG_TRANSFER
;
641 *flushed_depth_texture
= (struct r600_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
642 if (*flushed_depth_texture
== NULL
) {
643 R600_ERR("failed to create temporary texture to hold flushed depth\n");
647 (*flushed_depth_texture
)->is_flushing_texture
= TRUE
;
648 (*flushed_depth_texture
)->non_disp_tiling
= false;
652 static void *r600_texture_transfer_map(struct pipe_context
*ctx
,
653 struct pipe_resource
*texture
,
656 const struct pipe_box
*box
,
657 struct pipe_transfer
**ptransfer
)
659 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
660 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
661 struct pipe_resource resource
;
662 struct r600_transfer
*trans
;
663 boolean use_staging_texture
= FALSE
;
664 enum pipe_format format
= texture
->format
;
665 struct radeon_winsys_cs_handle
*buf
;
669 if ((texture
->bind
& PIPE_BIND_GLOBAL
) && texture
->target
== PIPE_BUFFER
) {
670 return r600_compute_global_transfer_map(ctx
, texture
, level
, usage
, box
, ptransfer
);
673 /* We cannot map a tiled texture directly because the data is
674 * in a different order, therefore we do detiling using a blit.
676 * Also, use a temporary in GTT memory for read transfers, as
677 * the CPU is much happier reading out of cached system memory
678 * than uncached VRAM.
680 if (R600_TEX_IS_TILED(rtex
, level
)) {
681 use_staging_texture
= TRUE
;
684 /* Use a staging texture for uploads if the underlying BO is busy. */
685 if (!(usage
& PIPE_TRANSFER_READ
) &&
686 (rctx
->ws
->cs_is_buffer_referenced(rctx
->cs
, rtex
->resource
.cs_buf
, RADEON_USAGE_READWRITE
) ||
687 rctx
->ws
->buffer_is_busy(rtex
->resource
.buf
, RADEON_USAGE_READWRITE
))) {
688 use_staging_texture
= TRUE
;
691 if (texture
->flags
& R600_RESOURCE_FLAG_TRANSFER
) {
692 use_staging_texture
= FALSE
;
695 if (use_staging_texture
&& (usage
& PIPE_TRANSFER_MAP_DIRECTLY
)) {
699 trans
= CALLOC_STRUCT(r600_transfer
);
702 trans
->transfer
.resource
= texture
;
703 trans
->transfer
.level
= level
;
704 trans
->transfer
.usage
= usage
;
705 trans
->transfer
.box
= *box
;
706 if (rtex
->is_depth
) {
707 /* XXX: only readback the rectangle which is being mapped?
709 /* XXX: when discard is true, no need to read back from depth texture
711 struct r600_texture
*staging_depth
;
713 if (!r600_init_flushed_depth_texture(ctx
, texture
, &staging_depth
)) {
714 R600_ERR("failed to create temporary texture to hold untiled copy\n");
719 r600_blit_decompress_depth(ctx
, rtex
, staging_depth
,
721 box
->z
, box
->z
+ box
->depth
- 1,
724 trans
->transfer
.stride
= staging_depth
->surface
.level
[level
].pitch_bytes
;
725 trans
->offset
= r600_texture_get_offset(staging_depth
, level
, box
->z
);
726 trans
->staging
= (struct r600_resource
*)staging_depth
;
727 } else if (use_staging_texture
) {
728 resource
.target
= PIPE_TEXTURE_2D
;
729 resource
.format
= texture
->format
;
730 resource
.width0
= box
->width
;
731 resource
.height0
= box
->height
;
733 resource
.array_size
= 1;
734 resource
.last_level
= 0;
735 resource
.nr_samples
= 0;
736 resource
.usage
= PIPE_USAGE_STAGING
;
738 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
;
739 /* For texture reading, the temporary (detiled) texture is used as
740 * a render target when blitting from a tiled texture. */
741 if (usage
& PIPE_TRANSFER_READ
) {
742 resource
.bind
|= PIPE_BIND_RENDER_TARGET
;
744 /* For texture writing, the temporary texture is used as a sampler
745 * when blitting into a tiled texture. */
746 if (usage
& PIPE_TRANSFER_WRITE
) {
747 resource
.bind
|= PIPE_BIND_SAMPLER_VIEW
;
749 /* Create the temporary texture. */
750 trans
->staging
= (struct r600_resource
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
751 if (trans
->staging
== NULL
) {
752 R600_ERR("failed to create temporary texture to hold untiled copy\n");
757 trans
->transfer
.stride
=
758 ((struct r600_texture
*)trans
->staging
)->surface
.level
[0].pitch_bytes
;
759 if (usage
& PIPE_TRANSFER_READ
) {
760 r600_copy_to_staging_texture(ctx
, trans
);
761 /* Always referenced in the blit. */
762 r600_flush(ctx
, NULL
, 0);
765 trans
->transfer
.stride
= rtex
->surface
.level
[level
].pitch_bytes
;
766 trans
->transfer
.layer_stride
= rtex
->surface
.level
[level
].slice_size
;
767 trans
->offset
= r600_texture_get_offset(rtex
, level
, box
->z
);
770 if (trans
->staging
) {
771 buf
= ((struct r600_resource
*)trans
->staging
)->cs_buf
;
773 buf
= ((struct r600_resource
*)texture
)->cs_buf
;
776 if (rtex
->is_depth
|| !trans
->staging
)
777 offset
= trans
->offset
+
778 box
->y
/ util_format_get_blockheight(format
) * trans
->transfer
.stride
+
779 box
->x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
781 if (!(map
= rctx
->ws
->buffer_map(buf
, rctx
->cs
, usage
))) {
782 pipe_resource_reference((struct pipe_resource
**)&trans
->staging
, NULL
);
787 *ptransfer
= &trans
->transfer
;
791 static void r600_texture_transfer_unmap(struct pipe_context
*ctx
,
792 struct pipe_transfer
* transfer
)
794 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
795 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
796 struct radeon_winsys_cs_handle
*buf
;
797 struct pipe_resource
*texture
= transfer
->resource
;
798 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
800 if ((transfer
->resource
->bind
& PIPE_BIND_GLOBAL
) && transfer
->resource
->target
== PIPE_BUFFER
) {
801 return r600_compute_global_transfer_unmap(ctx
, transfer
);
804 if (rtransfer
->staging
) {
805 buf
= ((struct r600_resource
*)rtransfer
->staging
)->cs_buf
;
807 buf
= ((struct r600_resource
*)transfer
->resource
)->cs_buf
;
809 rctx
->ws
->buffer_unmap(buf
);
811 if ((transfer
->usage
& PIPE_TRANSFER_WRITE
) && rtransfer
->staging
) {
812 if (rtex
->is_depth
) {
813 ctx
->resource_copy_region(ctx
, texture
, transfer
->level
,
814 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
815 &rtransfer
->staging
->b
.b
, transfer
->level
,
818 r600_copy_from_staging_texture(ctx
, rtransfer
);
822 if (rtransfer
->staging
)
823 pipe_resource_reference((struct pipe_resource
**)&rtransfer
->staging
, NULL
);
828 void r600_init_surface_functions(struct r600_context
*r600
)
830 r600
->context
.create_surface
= r600_create_surface
;
831 r600
->context
.surface_destroy
= r600_surface_destroy
;
834 static unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format
,
835 const unsigned char *swizzle_view
)
838 unsigned char swizzle
[4];
840 const uint32_t swizzle_shift
[4] = {
843 const uint32_t swizzle_bit
[4] = {
848 util_format_compose_swizzles(swizzle_format
, swizzle_view
, swizzle
);
850 memcpy(swizzle
, swizzle_format
, 4);
854 for (i
= 0; i
< 4; i
++) {
855 switch (swizzle
[i
]) {
856 case UTIL_FORMAT_SWIZZLE_Y
:
857 result
|= swizzle_bit
[1] << swizzle_shift
[i
];
859 case UTIL_FORMAT_SWIZZLE_Z
:
860 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
862 case UTIL_FORMAT_SWIZZLE_W
:
863 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
865 case UTIL_FORMAT_SWIZZLE_0
:
866 result
|= V_038010_SQ_SEL_0
<< swizzle_shift
[i
];
868 case UTIL_FORMAT_SWIZZLE_1
:
869 result
|= V_038010_SQ_SEL_1
<< swizzle_shift
[i
];
871 default: /* UTIL_FORMAT_SWIZZLE_X */
872 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
878 /* texture format translate */
879 uint32_t r600_translate_texformat(struct pipe_screen
*screen
,
880 enum pipe_format format
,
881 const unsigned char *swizzle_view
,
882 uint32_t *word4_p
, uint32_t *yuv_format_p
)
884 uint32_t result
= 0, word4
= 0, yuv_format
= 0;
885 const struct util_format_description
*desc
;
886 boolean uniform
= TRUE
;
887 static int r600_enable_s3tc
= -1;
888 bool is_srgb_valid
= FALSE
;
891 const uint32_t sign_bit
[4] = {
892 S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED
),
893 S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED
),
894 S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED
),
895 S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED
)
897 desc
= util_format_description(format
);
899 word4
|= r600_get_swizzle_combined(desc
->swizzle
, swizzle_view
);
901 /* Colorspace (return non-RGB formats directly). */
902 switch (desc
->colorspace
) {
903 /* Depth stencil formats */
904 case UTIL_FORMAT_COLORSPACE_ZS
:
906 case PIPE_FORMAT_Z16_UNORM
:
909 case PIPE_FORMAT_X24S8_UINT
:
910 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
911 case PIPE_FORMAT_Z24X8_UNORM
:
912 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
915 case PIPE_FORMAT_S8X24_UINT
:
916 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
917 case PIPE_FORMAT_X8Z24_UNORM
:
918 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
921 case PIPE_FORMAT_S8_UINT
:
923 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
925 case PIPE_FORMAT_Z32_FLOAT
:
926 result
= FMT_32_FLOAT
;
928 case PIPE_FORMAT_X32_S8X24_UINT
:
929 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
930 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
931 result
= FMT_X24_8_32_FLOAT
;
937 case UTIL_FORMAT_COLORSPACE_YUV
:
938 yuv_format
|= (1 << 30);
940 case PIPE_FORMAT_UYVY
:
941 case PIPE_FORMAT_YUYV
:
945 goto out_unknown
; /* XXX */
947 case UTIL_FORMAT_COLORSPACE_SRGB
:
948 word4
|= S_038010_FORCE_DEGAMMA(1);
955 if (r600_enable_s3tc
== -1) {
956 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
957 if (rscreen
->info
.drm_minor
>= 9)
958 r600_enable_s3tc
= 1;
960 r600_enable_s3tc
= debug_get_bool_option("R600_ENABLE_S3TC", FALSE
);
963 if (desc
->layout
== UTIL_FORMAT_LAYOUT_RGTC
) {
964 if (!r600_enable_s3tc
)
968 case PIPE_FORMAT_RGTC1_SNORM
:
969 case PIPE_FORMAT_LATC1_SNORM
:
970 word4
|= sign_bit
[0];
971 case PIPE_FORMAT_RGTC1_UNORM
:
972 case PIPE_FORMAT_LATC1_UNORM
:
975 case PIPE_FORMAT_RGTC2_SNORM
:
976 case PIPE_FORMAT_LATC2_SNORM
:
977 word4
|= sign_bit
[0] | sign_bit
[1];
978 case PIPE_FORMAT_RGTC2_UNORM
:
979 case PIPE_FORMAT_LATC2_UNORM
:
987 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
989 if (!r600_enable_s3tc
)
992 if (!util_format_s3tc_enabled
) {
997 case PIPE_FORMAT_DXT1_RGB
:
998 case PIPE_FORMAT_DXT1_RGBA
:
999 case PIPE_FORMAT_DXT1_SRGB
:
1000 case PIPE_FORMAT_DXT1_SRGBA
:
1002 is_srgb_valid
= TRUE
;
1004 case PIPE_FORMAT_DXT3_RGBA
:
1005 case PIPE_FORMAT_DXT3_SRGBA
:
1007 is_srgb_valid
= TRUE
;
1009 case PIPE_FORMAT_DXT5_RGBA
:
1010 case PIPE_FORMAT_DXT5_SRGBA
:
1012 is_srgb_valid
= TRUE
;
1019 if (desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
) {
1021 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
1022 case PIPE_FORMAT_G8R8_B8R8_UNORM
:
1025 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
1026 case PIPE_FORMAT_R8G8_R8B8_UNORM
:
1034 if (format
== PIPE_FORMAT_R9G9B9E5_FLOAT
) {
1035 result
= FMT_5_9_9_9_SHAREDEXP
;
1037 } else if (format
== PIPE_FORMAT_R11G11B10_FLOAT
) {
1038 result
= FMT_10_11_11_FLOAT
;
1043 for (i
= 0; i
< desc
->nr_channels
; i
++) {
1044 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
1045 word4
|= sign_bit
[i
];
1049 /* R8G8Bx_SNORM - XXX CxV8U8 */
1051 /* See whether the components are of the same size. */
1052 for (i
= 1; i
< desc
->nr_channels
; i
++) {
1053 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
1056 /* Non-uniform formats. */
1058 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
&&
1059 desc
->channel
[0].pure_integer
)
1060 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
1061 switch(desc
->nr_channels
) {
1063 if (desc
->channel
[0].size
== 5 &&
1064 desc
->channel
[1].size
== 6 &&
1065 desc
->channel
[2].size
== 5) {
1071 if (desc
->channel
[0].size
== 5 &&
1072 desc
->channel
[1].size
== 5 &&
1073 desc
->channel
[2].size
== 5 &&
1074 desc
->channel
[3].size
== 1) {
1075 result
= FMT_1_5_5_5
;
1078 if (desc
->channel
[0].size
== 10 &&
1079 desc
->channel
[1].size
== 10 &&
1080 desc
->channel
[2].size
== 10 &&
1081 desc
->channel
[3].size
== 2) {
1082 result
= FMT_2_10_10_10
;
1090 /* Find the first non-VOID channel. */
1091 for (i
= 0; i
< 4; i
++) {
1092 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
1100 /* uniform formats */
1101 switch (desc
->channel
[i
].type
) {
1102 case UTIL_FORMAT_TYPE_UNSIGNED
:
1103 case UTIL_FORMAT_TYPE_SIGNED
:
1105 if (!desc
->channel
[i
].normalized
&&
1106 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
1110 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
&&
1111 desc
->channel
[i
].pure_integer
)
1112 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
1114 switch (desc
->channel
[i
].size
) {
1116 switch (desc
->nr_channels
) {
1121 result
= FMT_4_4_4_4
;
1126 switch (desc
->nr_channels
) {
1134 result
= FMT_8_8_8_8
;
1135 is_srgb_valid
= TRUE
;
1140 switch (desc
->nr_channels
) {
1148 result
= FMT_16_16_16_16
;
1153 switch (desc
->nr_channels
) {
1161 result
= FMT_32_32_32_32
;
1167 case UTIL_FORMAT_TYPE_FLOAT
:
1168 switch (desc
->channel
[i
].size
) {
1170 switch (desc
->nr_channels
) {
1172 result
= FMT_16_FLOAT
;
1175 result
= FMT_16_16_FLOAT
;
1178 result
= FMT_16_16_16_16_FLOAT
;
1183 switch (desc
->nr_channels
) {
1185 result
= FMT_32_FLOAT
;
1188 result
= FMT_32_32_FLOAT
;
1191 result
= FMT_32_32_32_32_FLOAT
;
1200 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
&& !is_srgb_valid
)
1205 *yuv_format_p
= yuv_format
;
1208 /* R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format)); */
1212 static const struct u_resource_vtbl r600_texture_vtbl
=
1214 r600_texture_get_handle
, /* get_handle */
1215 r600_texture_destroy
, /* resource_destroy */
1216 r600_texture_transfer_map
, /* transfer_map */
1217 NULL
, /* transfer_flush_region */
1218 r600_texture_transfer_unmap
, /* transfer_unmap */
1219 NULL
/* transfer_inline_write */