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_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
&& !is_flushed_depth
&&
91 ptex
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
) {
92 surface
->bpe
= 4; /* stencil is allocated separately on evergreen */
94 surface
->bpe
= util_format_get_blocksize(ptex
->format
);
95 /* align byte per element on dword */
96 if (surface
->bpe
== 3) {
101 surface
->nsamples
= ptex
->nr_samples
? ptex
->nr_samples
: 1;
104 switch (array_mode
) {
105 case V_038000_ARRAY_1D_TILED_THIN1
:
106 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_1D
, MODE
);
108 case V_038000_ARRAY_2D_TILED_THIN1
:
109 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_2D
, MODE
);
111 case V_038000_ARRAY_LINEAR_ALIGNED
:
112 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_LINEAR_ALIGNED
, MODE
);
114 case V_038000_ARRAY_LINEAR_GENERAL
:
116 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_LINEAR
, MODE
);
119 switch (ptex
->target
) {
120 case PIPE_TEXTURE_1D
:
121 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_1D
, TYPE
);
123 case PIPE_TEXTURE_RECT
:
124 case PIPE_TEXTURE_2D
:
125 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_2D
, TYPE
);
127 case PIPE_TEXTURE_3D
:
128 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_3D
, TYPE
);
130 case PIPE_TEXTURE_1D_ARRAY
:
131 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_1D_ARRAY
, TYPE
);
132 surface
->array_size
= ptex
->array_size
;
134 case PIPE_TEXTURE_2D_ARRAY
:
135 case PIPE_TEXTURE_CUBE_ARRAY
: /* cube array layout like 2d layout for now */
136 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_2D_ARRAY
, TYPE
);
137 surface
->array_size
= ptex
->array_size
;
139 case PIPE_TEXTURE_CUBE
:
140 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_CUBEMAP
, TYPE
);
146 if (ptex
->bind
& PIPE_BIND_SCANOUT
) {
147 surface
->flags
|= RADEON_SURF_SCANOUT
;
150 if (!is_flushed_depth
&& is_depth
) {
151 surface
->flags
|= RADEON_SURF_ZBUFFER
;
154 surface
->flags
|= RADEON_SURF_SBUFFER
|
155 RADEON_SURF_HAS_SBUFFER_MIPTREE
;
161 static int r600_setup_surface(struct pipe_screen
*screen
,
162 struct r600_texture
*rtex
,
163 unsigned pitch_in_bytes_override
)
165 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
;
166 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
170 r
= rscreen
->ws
->surface_init(rscreen
->ws
, &rtex
->surface
);
174 rtex
->size
= rtex
->surface
.bo_size
;
175 if (pitch_in_bytes_override
&& pitch_in_bytes_override
!= rtex
->surface
.level
[0].pitch_bytes
) {
176 /* old ddx on evergreen over estimate alignment for 1d, only 1 level
179 rtex
->surface
.level
[0].nblk_x
= pitch_in_bytes_override
/ rtex
->surface
.bpe
;
180 rtex
->surface
.level
[0].pitch_bytes
= pitch_in_bytes_override
;
181 rtex
->surface
.level
[0].slice_size
= pitch_in_bytes_override
* rtex
->surface
.level
[0].nblk_y
;
182 if (rtex
->surface
.flags
& RADEON_SURF_SBUFFER
) {
183 rtex
->surface
.stencil_offset
=
184 rtex
->surface
.stencil_level
[0].offset
= rtex
->surface
.level
[0].slice_size
;
187 for (i
= 0; i
<= ptex
->last_level
; i
++) {
188 switch (rtex
->surface
.level
[i
].mode
) {
189 case RADEON_SURF_MODE_LINEAR_ALIGNED
:
190 rtex
->array_mode
[i
] = V_038000_ARRAY_LINEAR_ALIGNED
;
192 case RADEON_SURF_MODE_1D
:
193 rtex
->array_mode
[i
] = V_038000_ARRAY_1D_TILED_THIN1
;
195 case RADEON_SURF_MODE_2D
:
196 rtex
->array_mode
[i
] = V_038000_ARRAY_2D_TILED_THIN1
;
199 case RADEON_SURF_MODE_LINEAR
:
200 rtex
->array_mode
[i
] = 0;
207 static boolean
r600_texture_get_handle(struct pipe_screen
* screen
,
208 struct pipe_resource
*ptex
,
209 struct winsys_handle
*whandle
)
211 struct r600_texture
*rtex
= (struct r600_texture
*)ptex
;
212 struct r600_resource
*resource
= &rtex
->resource
;
213 struct radeon_surface
*surface
= &rtex
->surface
;
214 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
216 rscreen
->ws
->buffer_set_tiling(resource
->buf
,
218 surface
->level
[0].mode
>= RADEON_SURF_MODE_1D
?
219 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
,
220 surface
->level
[0].mode
>= RADEON_SURF_MODE_2D
?
221 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
,
222 surface
->bankw
, surface
->bankh
,
224 surface
->stencil_tile_split
,
226 rtex
->surface
.level
[0].pitch_bytes
);
228 return rscreen
->ws
->buffer_get_handle(resource
->buf
,
229 rtex
->surface
.level
[0].pitch_bytes
, whandle
);
232 static void r600_texture_destroy(struct pipe_screen
*screen
,
233 struct pipe_resource
*ptex
)
235 struct r600_texture
*rtex
= (struct r600_texture
*)ptex
;
236 struct r600_resource
*resource
= &rtex
->resource
;
238 if (rtex
->flushed_depth_texture
)
239 pipe_resource_reference((struct pipe_resource
**)&rtex
->flushed_depth_texture
, NULL
);
241 pb_reference(&resource
->buf
, NULL
);
245 static const struct u_resource_vtbl r600_texture_vtbl
;
247 /* The number of samples can be specified independently of the texture. */
248 void r600_texture_get_fmask_info(struct r600_screen
*rscreen
,
249 struct r600_texture
*rtex
,
251 struct r600_fmask_info
*out
)
253 /* FMASK is allocated pretty much like an ordinary texture.
254 * Here we use bpe in the units of bits, not bytes. */
255 struct radeon_surface fmask
= rtex
->surface
;
257 switch (nr_samples
) {
259 /* This should be 8,1, but we should set nsamples > 1
260 * for the allocator to treat it as a multisample surface.
261 * Let's set 4,2 then. */
275 R600_ERR("Invalid sample count for FMASK allocation.\n");
279 /* R600-R700 errata? Anyway, this fixes colorbuffer corruption. */
280 if (rscreen
->chip_class
<= R700
) {
284 if (rscreen
->chip_class
>= EVERGREEN
) {
285 fmask
.bankh
= nr_samples
<= 4 ? 4 : 1;
288 if (rscreen
->ws
->surface_init(rscreen
->ws
, &fmask
)) {
289 R600_ERR("Got error in surface_init while allocating FMASK.\n");
292 assert(fmask
.level
[0].mode
== RADEON_SURF_MODE_2D
);
294 out
->bank_height
= fmask
.bankh
;
295 out
->alignment
= MAX2(256, fmask
.bo_alignment
);
296 out
->size
= (fmask
.bo_size
+ 7) / 8;
299 static void r600_texture_allocate_fmask(struct r600_screen
*rscreen
,
300 struct r600_texture
*rtex
)
302 struct r600_fmask_info fmask
;
304 r600_texture_get_fmask_info(rscreen
, rtex
,
305 rtex
->resource
.b
.b
.nr_samples
, &fmask
);
307 /* Reserve space for FMASK while converting bits back to bytes. */
308 rtex
->fmask_bank_height
= fmask
.bank_height
;
309 rtex
->fmask_offset
= align(rtex
->size
, fmask
.alignment
);
310 rtex
->fmask_size
= fmask
.size
;
311 rtex
->size
= rtex
->fmask_offset
+ rtex
->fmask_size
;
313 printf("FMASK width=%u, height=%i, bits=%u, size=%u\n",
314 fmask
.npix_x
, fmask
.npix_y
, fmask
.bpe
* fmask
.nsamples
, rtex
->fmask_size
);
318 void r600_texture_get_cmask_info(struct r600_screen
*rscreen
,
319 struct r600_texture
*rtex
,
320 struct r600_cmask_info
*out
)
322 unsigned cmask_tile_width
= 8;
323 unsigned cmask_tile_height
= 8;
324 unsigned cmask_tile_elements
= cmask_tile_width
* cmask_tile_height
;
325 unsigned element_bits
= 4;
326 unsigned cmask_cache_bits
= 1024;
327 unsigned num_pipes
= rscreen
->tiling_info
.num_channels
;
328 unsigned pipe_interleave_bytes
= rscreen
->tiling_info
.group_bytes
;
330 unsigned elements_per_macro_tile
= (cmask_cache_bits
/ element_bits
) * num_pipes
;
331 unsigned pixels_per_macro_tile
= elements_per_macro_tile
* cmask_tile_elements
;
332 unsigned sqrt_pixels_per_macro_tile
= sqrt(pixels_per_macro_tile
);
333 unsigned macro_tile_width
= util_next_power_of_two(sqrt_pixels_per_macro_tile
);
334 unsigned macro_tile_height
= pixels_per_macro_tile
/ macro_tile_width
;
336 unsigned pitch_elements
= align(rtex
->surface
.npix_x
, macro_tile_width
);
337 unsigned height
= align(rtex
->surface
.npix_y
, macro_tile_height
);
339 unsigned base_align
= num_pipes
* pipe_interleave_bytes
;
340 unsigned slice_bytes
=
341 ((pitch_elements
* height
* element_bits
+ 7) / 8) / cmask_tile_elements
;
343 assert(macro_tile_width
% 128 == 0);
344 assert(macro_tile_height
% 128 == 0);
346 out
->slice_tile_max
= ((pitch_elements
* height
) / (128*128)) - 1;
347 out
->alignment
= MAX2(256, base_align
);
348 out
->size
= rtex
->surface
.array_size
* align(slice_bytes
, base_align
);
351 static void r600_texture_allocate_cmask(struct r600_screen
*rscreen
,
352 struct r600_texture
*rtex
)
354 struct r600_cmask_info cmask
;
356 r600_texture_get_cmask_info(rscreen
, rtex
, &cmask
);
358 rtex
->cmask_slice_tile_max
= cmask
.slice_tile_max
;
359 rtex
->cmask_offset
= align(rtex
->size
, cmask
.alignment
);
360 rtex
->cmask_size
= cmask
.size
;
361 rtex
->size
= rtex
->cmask_offset
+ rtex
->cmask_size
;
363 printf("CMASK: macro tile width = %u, macro tile height = %u, "
364 "pitch elements = %u, height = %u, slice tile max = %u\n",
365 macro_tile_width
, macro_tile_height
, pitch_elements
, height
,
366 rtex
->cmask_slice_tile_max
);
370 DEBUG_GET_ONCE_BOOL_OPTION(print_texdepth
, "R600_PRINT_TEXDEPTH", FALSE
);
372 static struct r600_texture
*
373 r600_texture_create_object(struct pipe_screen
*screen
,
374 const struct pipe_resource
*base
,
375 unsigned pitch_in_bytes_override
,
376 struct pb_buffer
*buf
,
378 struct radeon_surface
*surface
)
380 struct r600_texture
*rtex
;
381 struct r600_resource
*resource
;
382 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
385 rtex
= CALLOC_STRUCT(r600_texture
);
389 resource
= &rtex
->resource
;
390 resource
->b
.b
= *base
;
391 resource
->b
.vtbl
= &r600_texture_vtbl
;
392 pipe_reference_init(&resource
->b
.b
.reference
, 1);
393 resource
->b
.b
.screen
= screen
;
394 rtex
->pitch_override
= pitch_in_bytes_override
;
396 /* don't include stencil-only formats which we don't support for rendering */
397 rtex
->is_depth
= util_format_has_depth(util_format_description(rtex
->resource
.b
.b
.format
));
399 rtex
->surface
= *surface
;
400 r
= r600_setup_surface(screen
, rtex
,
401 pitch_in_bytes_override
);
407 if (base
->nr_samples
> 1 && !rtex
->is_depth
&& alloc_bo
) {
408 r600_texture_allocate_cmask(rscreen
, rtex
);
409 r600_texture_allocate_fmask(rscreen
, rtex
);
412 if (!rtex
->is_depth
&& base
->nr_samples
> 1 &&
413 (!rtex
->fmask_size
|| !rtex
->cmask_size
)) {
418 /* Tiled depth textures utilize the non-displayable tile order. */
419 rtex
->non_disp_tiling
= rtex
->is_depth
&& rtex
->surface
.level
[0].mode
>= RADEON_SURF_MODE_1D
;
421 /* Now create the backing buffer. */
422 if (!buf
&& alloc_bo
) {
423 unsigned base_align
= rtex
->surface
.bo_alignment
;
424 unsigned usage
= R600_TEX_IS_TILED(rtex
, 0) ? PIPE_USAGE_STATIC
: base
->usage
;
426 if (!r600_init_resource(rscreen
, resource
, rtex
->size
, base_align
, base
->bind
, usage
)) {
431 /* This is usually the window framebuffer. We want it in VRAM, always. */
433 resource
->cs_buf
= rscreen
->ws
->buffer_get_cs_handle(buf
);
434 resource
->domains
= RADEON_DOMAIN_VRAM
;
437 if (rtex
->cmask_size
) {
438 /* Initialize the cmask to 0xCC (= compressed state). */
439 char *ptr
= rscreen
->ws
->buffer_map(resource
->cs_buf
, NULL
, PIPE_TRANSFER_WRITE
);
440 memset(ptr
+ rtex
->cmask_offset
, 0xCC, rtex
->cmask_size
);
441 rscreen
->ws
->buffer_unmap(resource
->cs_buf
);
444 if (debug_get_option_print_texdepth() && rtex
->is_depth
&& rtex
->non_disp_tiling
) {
445 printf("Texture: npix_x=%u, npix_y=%u, npix_z=%u, blk_w=%u, "
446 "blk_h=%u, blk_d=%u, array_size=%u, last_level=%u, "
447 "bpe=%u, nsamples=%u, flags=%u\n",
448 rtex
->surface
.npix_x
, rtex
->surface
.npix_y
,
449 rtex
->surface
.npix_z
, rtex
->surface
.blk_w
,
450 rtex
->surface
.blk_h
, rtex
->surface
.blk_d
,
451 rtex
->surface
.array_size
, rtex
->surface
.last_level
,
452 rtex
->surface
.bpe
, rtex
->surface
.nsamples
,
453 rtex
->surface
.flags
);
454 if (rtex
->surface
.flags
& RADEON_SURF_ZBUFFER
) {
455 for (int i
= 0; i
<= rtex
->surface
.last_level
; i
++) {
456 printf(" Z %i: offset=%llu, slice_size=%llu, npix_x=%u, "
457 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
458 "nblk_z=%u, pitch_bytes=%u, mode=%u\n",
459 i
, (unsigned long long)rtex
->surface
.level
[i
].offset
,
460 (unsigned long long)rtex
->surface
.level
[i
].slice_size
,
461 rtex
->surface
.level
[i
].npix_x
,
462 rtex
->surface
.level
[i
].npix_y
,
463 rtex
->surface
.level
[i
].npix_z
,
464 rtex
->surface
.level
[i
].nblk_x
,
465 rtex
->surface
.level
[i
].nblk_y
,
466 rtex
->surface
.level
[i
].nblk_z
,
467 rtex
->surface
.level
[i
].pitch_bytes
,
468 rtex
->surface
.level
[i
].mode
);
471 if (rtex
->surface
.flags
& RADEON_SURF_SBUFFER
) {
472 for (int i
= 0; i
<= rtex
->surface
.last_level
; i
++) {
473 printf(" S %i: offset=%llu, slice_size=%llu, npix_x=%u, "
474 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
475 "nblk_z=%u, pitch_bytes=%u, mode=%u\n",
476 i
, (unsigned long long)rtex
->surface
.stencil_level
[i
].offset
,
477 (unsigned long long)rtex
->surface
.stencil_level
[i
].slice_size
,
478 rtex
->surface
.stencil_level
[i
].npix_x
,
479 rtex
->surface
.stencil_level
[i
].npix_y
,
480 rtex
->surface
.stencil_level
[i
].npix_z
,
481 rtex
->surface
.stencil_level
[i
].nblk_x
,
482 rtex
->surface
.stencil_level
[i
].nblk_y
,
483 rtex
->surface
.stencil_level
[i
].nblk_z
,
484 rtex
->surface
.stencil_level
[i
].pitch_bytes
,
485 rtex
->surface
.stencil_level
[i
].mode
);
492 struct pipe_resource
*r600_texture_create(struct pipe_screen
*screen
,
493 const struct pipe_resource
*templ
)
495 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
496 struct radeon_surface surface
;
497 const struct util_format_description
*desc
= util_format_description(templ
->format
);
501 /* Default tiling mode for staging textures. */
502 array_mode
= V_038000_ARRAY_LINEAR_ALIGNED
;
504 /* Tiling doesn't work with the 422 (SUBSAMPLED) formats. That's not an issue,
505 * because 422 formats are used for videos, which prefer linear buffers
506 * for fast uploads anyway. */
507 if (!(templ
->flags
& R600_RESOURCE_FLAG_TRANSFER
) &&
508 desc
->layout
!= UTIL_FORMAT_LAYOUT_SUBSAMPLED
) {
509 if (!(templ
->bind
& PIPE_BIND_SCANOUT
) &&
510 templ
->usage
!= PIPE_USAGE_STAGING
&&
511 templ
->usage
!= PIPE_USAGE_STREAM
&&
512 templ
->target
!= PIPE_TEXTURE_1D
&&
513 templ
->target
!= PIPE_TEXTURE_1D_ARRAY
&&
514 templ
->height0
> 3) {
515 array_mode
= V_038000_ARRAY_2D_TILED_THIN1
;
516 } else if (util_format_is_compressed(templ
->format
)) {
517 array_mode
= V_038000_ARRAY_1D_TILED_THIN1
;
521 r
= r600_init_surface(rscreen
, &surface
, templ
, array_mode
,
522 templ
->flags
& R600_RESOURCE_FLAG_FLUSHED_DEPTH
);
526 r
= rscreen
->ws
->surface_best(rscreen
->ws
, &surface
);
530 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
,
531 0, NULL
, TRUE
, &surface
);
534 struct pipe_surface
*r600_create_surface_custom(struct pipe_context
*pipe
,
535 struct pipe_resource
*texture
,
536 const struct pipe_surface
*templ
,
537 unsigned width
, unsigned height
)
539 struct r600_surface
*surface
= CALLOC_STRUCT(r600_surface
);
541 assert(templ
->u
.tex
.first_layer
== templ
->u
.tex
.last_layer
);
544 pipe_reference_init(&surface
->base
.reference
, 1);
545 pipe_resource_reference(&surface
->base
.texture
, texture
);
546 surface
->base
.context
= pipe
;
547 surface
->base
.format
= templ
->format
;
548 surface
->base
.width
= width
;
549 surface
->base
.height
= height
;
550 surface
->base
.usage
= templ
->usage
;
551 surface
->base
.u
= templ
->u
;
552 return &surface
->base
;
555 static struct pipe_surface
*r600_create_surface(struct pipe_context
*pipe
,
556 struct pipe_resource
*texture
,
557 const struct pipe_surface
*templ
)
559 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
560 unsigned level
= templ
->u
.tex
.level
;
562 return r600_create_surface_custom(pipe
, texture
, templ
,
563 rtex
->surface
.level
[level
].npix_x
,
564 rtex
->surface
.level
[level
].npix_y
);
567 static void r600_surface_destroy(struct pipe_context
*pipe
,
568 struct pipe_surface
*surface
)
570 struct r600_surface
*surf
= (struct r600_surface
*)surface
;
571 pipe_resource_reference((struct pipe_resource
**)&surf
->cb_buffer_fmask
, NULL
);
572 pipe_resource_reference((struct pipe_resource
**)&surf
->cb_buffer_cmask
, NULL
);
573 pipe_resource_reference(&surface
->texture
, NULL
);
577 struct pipe_resource
*r600_texture_from_handle(struct pipe_screen
*screen
,
578 const struct pipe_resource
*templ
,
579 struct winsys_handle
*whandle
)
581 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
582 struct pb_buffer
*buf
= NULL
;
584 unsigned array_mode
= 0;
585 enum radeon_bo_layout micro
, macro
;
586 struct radeon_surface surface
;
589 /* Support only 2D textures without mipmaps */
590 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
) ||
591 templ
->depth0
!= 1 || templ
->last_level
!= 0)
594 buf
= rscreen
->ws
->buffer_from_handle(rscreen
->ws
, whandle
, &stride
);
598 rscreen
->ws
->buffer_get_tiling(buf
, µ
, ¯o
,
599 &surface
.bankw
, &surface
.bankh
,
601 &surface
.stencil_tile_split
,
604 if (macro
== RADEON_LAYOUT_TILED
)
605 array_mode
= V_0280A0_ARRAY_2D_TILED_THIN1
;
606 else if (micro
== RADEON_LAYOUT_TILED
)
607 array_mode
= V_0280A0_ARRAY_1D_TILED_THIN1
;
609 array_mode
= V_038000_ARRAY_LINEAR_ALIGNED
;
611 r
= r600_init_surface(rscreen
, &surface
, templ
, array_mode
, false);
615 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
,
616 stride
, buf
, FALSE
, &surface
);
619 bool r600_init_flushed_depth_texture(struct pipe_context
*ctx
,
620 struct pipe_resource
*texture
,
621 struct r600_texture
**staging
)
623 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
624 struct pipe_resource resource
;
625 struct r600_texture
**flushed_depth_texture
= staging
?
626 staging
: &rtex
->flushed_depth_texture
;
628 if (!staging
&& rtex
->flushed_depth_texture
)
629 return true; /* it's ready */
631 resource
.target
= texture
->target
;
632 resource
.format
= texture
->format
;
633 resource
.width0
= texture
->width0
;
634 resource
.height0
= texture
->height0
;
635 resource
.depth0
= texture
->depth0
;
636 resource
.array_size
= texture
->array_size
;
637 resource
.last_level
= texture
->last_level
;
638 resource
.nr_samples
= texture
->nr_samples
;
639 resource
.usage
= staging
? PIPE_USAGE_STAGING
: PIPE_USAGE_STATIC
;
640 resource
.bind
= texture
->bind
& ~PIPE_BIND_DEPTH_STENCIL
;
641 resource
.flags
= texture
->flags
| R600_RESOURCE_FLAG_FLUSHED_DEPTH
;
644 resource
.flags
|= R600_RESOURCE_FLAG_TRANSFER
;
646 *flushed_depth_texture
= (struct r600_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
647 if (*flushed_depth_texture
== NULL
) {
648 R600_ERR("failed to create temporary texture to hold flushed depth\n");
652 (*flushed_depth_texture
)->is_flushing_texture
= TRUE
;
653 (*flushed_depth_texture
)->non_disp_tiling
= false;
657 static void *r600_texture_transfer_map(struct pipe_context
*ctx
,
658 struct pipe_resource
*texture
,
661 const struct pipe_box
*box
,
662 struct pipe_transfer
**ptransfer
)
664 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
665 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
666 struct pipe_resource resource
;
667 struct r600_transfer
*trans
;
668 boolean use_staging_texture
= FALSE
;
669 enum pipe_format format
= texture
->format
;
670 struct radeon_winsys_cs_handle
*buf
;
674 if ((texture
->bind
& PIPE_BIND_GLOBAL
) && texture
->target
== PIPE_BUFFER
) {
675 return r600_compute_global_transfer_map(ctx
, texture
, level
, usage
, box
, ptransfer
);
678 /* We cannot map a tiled texture directly because the data is
679 * in a different order, therefore we do detiling using a blit.
681 * Also, use a temporary in GTT memory for read transfers, as
682 * the CPU is much happier reading out of cached system memory
683 * than uncached VRAM.
685 if (R600_TEX_IS_TILED(rtex
, level
)) {
686 use_staging_texture
= TRUE
;
689 /* Use a staging texture for uploads if the underlying BO is busy. */
690 if (!(usage
& PIPE_TRANSFER_READ
) &&
691 (rctx
->ws
->cs_is_buffer_referenced(rctx
->cs
, rtex
->resource
.cs_buf
, RADEON_USAGE_READWRITE
) ||
692 rctx
->ws
->buffer_is_busy(rtex
->resource
.buf
, RADEON_USAGE_READWRITE
))) {
693 use_staging_texture
= TRUE
;
696 if (texture
->flags
& R600_RESOURCE_FLAG_TRANSFER
) {
697 use_staging_texture
= FALSE
;
700 if (use_staging_texture
&& (usage
& PIPE_TRANSFER_MAP_DIRECTLY
)) {
704 trans
= CALLOC_STRUCT(r600_transfer
);
707 trans
->transfer
.resource
= texture
;
708 trans
->transfer
.level
= level
;
709 trans
->transfer
.usage
= usage
;
710 trans
->transfer
.box
= *box
;
711 if (rtex
->is_depth
) {
712 /* XXX: only readback the rectangle which is being mapped?
714 /* XXX: when discard is true, no need to read back from depth texture
716 struct r600_texture
*staging_depth
;
718 if (!r600_init_flushed_depth_texture(ctx
, texture
, &staging_depth
)) {
719 R600_ERR("failed to create temporary texture to hold untiled copy\n");
724 r600_blit_decompress_depth(ctx
, rtex
, staging_depth
,
726 box
->z
, box
->z
+ box
->depth
- 1,
729 trans
->transfer
.stride
= staging_depth
->surface
.level
[level
].pitch_bytes
;
730 trans
->offset
= r600_texture_get_offset(staging_depth
, level
, box
->z
);
731 trans
->staging
= (struct r600_resource
*)staging_depth
;
732 } else if (use_staging_texture
) {
733 resource
.target
= PIPE_TEXTURE_2D
;
734 resource
.format
= texture
->format
;
735 resource
.width0
= box
->width
;
736 resource
.height0
= box
->height
;
738 resource
.array_size
= 1;
739 resource
.last_level
= 0;
740 resource
.nr_samples
= 0;
741 resource
.usage
= PIPE_USAGE_STAGING
;
743 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
;
744 /* For texture reading, the temporary (detiled) texture is used as
745 * a render target when blitting from a tiled texture. */
746 if (usage
& PIPE_TRANSFER_READ
) {
747 resource
.bind
|= PIPE_BIND_RENDER_TARGET
;
749 /* For texture writing, the temporary texture is used as a sampler
750 * when blitting into a tiled texture. */
751 if (usage
& PIPE_TRANSFER_WRITE
) {
752 resource
.bind
|= PIPE_BIND_SAMPLER_VIEW
;
754 /* Create the temporary texture. */
755 trans
->staging
= (struct r600_resource
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
756 if (trans
->staging
== NULL
) {
757 R600_ERR("failed to create temporary texture to hold untiled copy\n");
762 trans
->transfer
.stride
=
763 ((struct r600_texture
*)trans
->staging
)->surface
.level
[0].pitch_bytes
;
764 if (usage
& PIPE_TRANSFER_READ
) {
765 r600_copy_to_staging_texture(ctx
, trans
);
766 /* Always referenced in the blit. */
767 r600_flush(ctx
, NULL
, 0);
770 trans
->transfer
.stride
= rtex
->surface
.level
[level
].pitch_bytes
;
771 trans
->transfer
.layer_stride
= rtex
->surface
.level
[level
].slice_size
;
772 trans
->offset
= r600_texture_get_offset(rtex
, level
, box
->z
);
775 if (trans
->staging
) {
776 buf
= ((struct r600_resource
*)trans
->staging
)->cs_buf
;
778 buf
= ((struct r600_resource
*)texture
)->cs_buf
;
781 if (rtex
->is_depth
|| !trans
->staging
)
782 offset
= trans
->offset
+
783 box
->y
/ util_format_get_blockheight(format
) * trans
->transfer
.stride
+
784 box
->x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
786 if (!(map
= rctx
->ws
->buffer_map(buf
, rctx
->cs
, usage
))) {
787 pipe_resource_reference((struct pipe_resource
**)&trans
->staging
, NULL
);
792 *ptransfer
= &trans
->transfer
;
796 static void r600_texture_transfer_unmap(struct pipe_context
*ctx
,
797 struct pipe_transfer
* transfer
)
799 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
800 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
801 struct radeon_winsys_cs_handle
*buf
;
802 struct pipe_resource
*texture
= transfer
->resource
;
803 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
805 if ((transfer
->resource
->bind
& PIPE_BIND_GLOBAL
) && transfer
->resource
->target
== PIPE_BUFFER
) {
806 return r600_compute_global_transfer_unmap(ctx
, transfer
);
809 if (rtransfer
->staging
) {
810 buf
= ((struct r600_resource
*)rtransfer
->staging
)->cs_buf
;
812 buf
= ((struct r600_resource
*)transfer
->resource
)->cs_buf
;
814 rctx
->ws
->buffer_unmap(buf
);
816 if ((transfer
->usage
& PIPE_TRANSFER_WRITE
) && rtransfer
->staging
) {
817 if (rtex
->is_depth
) {
818 ctx
->resource_copy_region(ctx
, texture
, transfer
->level
,
819 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
820 &rtransfer
->staging
->b
.b
, transfer
->level
,
823 r600_copy_from_staging_texture(ctx
, rtransfer
);
827 if (rtransfer
->staging
)
828 pipe_resource_reference((struct pipe_resource
**)&rtransfer
->staging
, NULL
);
833 void r600_init_surface_functions(struct r600_context
*r600
)
835 r600
->context
.create_surface
= r600_create_surface
;
836 r600
->context
.surface_destroy
= r600_surface_destroy
;
839 static unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format
,
840 const unsigned char *swizzle_view
)
843 unsigned char swizzle
[4];
845 const uint32_t swizzle_shift
[4] = {
848 const uint32_t swizzle_bit
[4] = {
853 util_format_compose_swizzles(swizzle_format
, swizzle_view
, swizzle
);
855 memcpy(swizzle
, swizzle_format
, 4);
859 for (i
= 0; i
< 4; i
++) {
860 switch (swizzle
[i
]) {
861 case UTIL_FORMAT_SWIZZLE_Y
:
862 result
|= swizzle_bit
[1] << swizzle_shift
[i
];
864 case UTIL_FORMAT_SWIZZLE_Z
:
865 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
867 case UTIL_FORMAT_SWIZZLE_W
:
868 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
870 case UTIL_FORMAT_SWIZZLE_0
:
871 result
|= V_038010_SQ_SEL_0
<< swizzle_shift
[i
];
873 case UTIL_FORMAT_SWIZZLE_1
:
874 result
|= V_038010_SQ_SEL_1
<< swizzle_shift
[i
];
876 default: /* UTIL_FORMAT_SWIZZLE_X */
877 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
883 /* texture format translate */
884 uint32_t r600_translate_texformat(struct pipe_screen
*screen
,
885 enum pipe_format format
,
886 const unsigned char *swizzle_view
,
887 uint32_t *word4_p
, uint32_t *yuv_format_p
)
889 uint32_t result
= 0, word4
= 0, yuv_format
= 0;
890 const struct util_format_description
*desc
;
891 boolean uniform
= TRUE
;
892 static int r600_enable_s3tc
= -1;
893 bool is_srgb_valid
= FALSE
;
896 const uint32_t sign_bit
[4] = {
897 S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED
),
898 S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED
),
899 S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED
),
900 S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED
)
902 desc
= util_format_description(format
);
904 word4
|= r600_get_swizzle_combined(desc
->swizzle
, swizzle_view
);
906 /* Colorspace (return non-RGB formats directly). */
907 switch (desc
->colorspace
) {
908 /* Depth stencil formats */
909 case UTIL_FORMAT_COLORSPACE_ZS
:
911 case PIPE_FORMAT_Z16_UNORM
:
914 case PIPE_FORMAT_X24S8_UINT
:
915 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
916 case PIPE_FORMAT_Z24X8_UNORM
:
917 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
920 case PIPE_FORMAT_S8X24_UINT
:
921 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
922 case PIPE_FORMAT_X8Z24_UNORM
:
923 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
926 case PIPE_FORMAT_S8_UINT
:
928 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
930 case PIPE_FORMAT_Z32_FLOAT
:
931 result
= FMT_32_FLOAT
;
933 case PIPE_FORMAT_X32_S8X24_UINT
:
934 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
935 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
936 result
= FMT_X24_8_32_FLOAT
;
942 case UTIL_FORMAT_COLORSPACE_YUV
:
943 yuv_format
|= (1 << 30);
945 case PIPE_FORMAT_UYVY
:
946 case PIPE_FORMAT_YUYV
:
950 goto out_unknown
; /* XXX */
952 case UTIL_FORMAT_COLORSPACE_SRGB
:
953 word4
|= S_038010_FORCE_DEGAMMA(1);
960 if (r600_enable_s3tc
== -1) {
961 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
962 if (rscreen
->info
.drm_minor
>= 9)
963 r600_enable_s3tc
= 1;
965 r600_enable_s3tc
= debug_get_bool_option("R600_ENABLE_S3TC", FALSE
);
968 if (desc
->layout
== UTIL_FORMAT_LAYOUT_RGTC
) {
969 if (!r600_enable_s3tc
)
973 case PIPE_FORMAT_RGTC1_SNORM
:
974 case PIPE_FORMAT_LATC1_SNORM
:
975 word4
|= sign_bit
[0];
976 case PIPE_FORMAT_RGTC1_UNORM
:
977 case PIPE_FORMAT_LATC1_UNORM
:
980 case PIPE_FORMAT_RGTC2_SNORM
:
981 case PIPE_FORMAT_LATC2_SNORM
:
982 word4
|= sign_bit
[0] | sign_bit
[1];
983 case PIPE_FORMAT_RGTC2_UNORM
:
984 case PIPE_FORMAT_LATC2_UNORM
:
992 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
994 if (!r600_enable_s3tc
)
997 if (!util_format_s3tc_enabled
) {
1002 case PIPE_FORMAT_DXT1_RGB
:
1003 case PIPE_FORMAT_DXT1_RGBA
:
1004 case PIPE_FORMAT_DXT1_SRGB
:
1005 case PIPE_FORMAT_DXT1_SRGBA
:
1007 is_srgb_valid
= TRUE
;
1009 case PIPE_FORMAT_DXT3_RGBA
:
1010 case PIPE_FORMAT_DXT3_SRGBA
:
1012 is_srgb_valid
= TRUE
;
1014 case PIPE_FORMAT_DXT5_RGBA
:
1015 case PIPE_FORMAT_DXT5_SRGBA
:
1017 is_srgb_valid
= TRUE
;
1024 if (desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
) {
1026 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
1027 case PIPE_FORMAT_G8R8_B8R8_UNORM
:
1030 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
1031 case PIPE_FORMAT_R8G8_R8B8_UNORM
:
1039 if (format
== PIPE_FORMAT_R9G9B9E5_FLOAT
) {
1040 result
= FMT_5_9_9_9_SHAREDEXP
;
1042 } else if (format
== PIPE_FORMAT_R11G11B10_FLOAT
) {
1043 result
= FMT_10_11_11_FLOAT
;
1048 for (i
= 0; i
< desc
->nr_channels
; i
++) {
1049 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
1050 word4
|= sign_bit
[i
];
1054 /* R8G8Bx_SNORM - XXX CxV8U8 */
1056 /* See whether the components are of the same size. */
1057 for (i
= 1; i
< desc
->nr_channels
; i
++) {
1058 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
1061 /* Non-uniform formats. */
1063 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
&&
1064 desc
->channel
[0].pure_integer
)
1065 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
1066 switch(desc
->nr_channels
) {
1068 if (desc
->channel
[0].size
== 5 &&
1069 desc
->channel
[1].size
== 6 &&
1070 desc
->channel
[2].size
== 5) {
1076 if (desc
->channel
[0].size
== 5 &&
1077 desc
->channel
[1].size
== 5 &&
1078 desc
->channel
[2].size
== 5 &&
1079 desc
->channel
[3].size
== 1) {
1080 result
= FMT_1_5_5_5
;
1083 if (desc
->channel
[0].size
== 10 &&
1084 desc
->channel
[1].size
== 10 &&
1085 desc
->channel
[2].size
== 10 &&
1086 desc
->channel
[3].size
== 2) {
1087 result
= FMT_2_10_10_10
;
1095 /* Find the first non-VOID channel. */
1096 for (i
= 0; i
< 4; i
++) {
1097 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
1105 /* uniform formats */
1106 switch (desc
->channel
[i
].type
) {
1107 case UTIL_FORMAT_TYPE_UNSIGNED
:
1108 case UTIL_FORMAT_TYPE_SIGNED
:
1110 if (!desc
->channel
[i
].normalized
&&
1111 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
1115 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
&&
1116 desc
->channel
[i
].pure_integer
)
1117 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
1119 switch (desc
->channel
[i
].size
) {
1121 switch (desc
->nr_channels
) {
1126 result
= FMT_4_4_4_4
;
1131 switch (desc
->nr_channels
) {
1139 result
= FMT_8_8_8_8
;
1140 is_srgb_valid
= TRUE
;
1145 switch (desc
->nr_channels
) {
1153 result
= FMT_16_16_16_16
;
1158 switch (desc
->nr_channels
) {
1166 result
= FMT_32_32_32_32
;
1172 case UTIL_FORMAT_TYPE_FLOAT
:
1173 switch (desc
->channel
[i
].size
) {
1175 switch (desc
->nr_channels
) {
1177 result
= FMT_16_FLOAT
;
1180 result
= FMT_16_16_FLOAT
;
1183 result
= FMT_16_16_16_16_FLOAT
;
1188 switch (desc
->nr_channels
) {
1190 result
= FMT_32_FLOAT
;
1193 result
= FMT_32_32_FLOAT
;
1196 result
= FMT_32_32_32_32_FLOAT
;
1205 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
&& !is_srgb_valid
)
1210 *yuv_format_p
= yuv_format
;
1213 /* R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format)); */
1217 static const struct u_resource_vtbl r600_texture_vtbl
=
1219 r600_texture_get_handle
, /* get_handle */
1220 r600_texture_destroy
, /* resource_destroy */
1221 r600_texture_transfer_map
, /* transfer_map */
1222 NULL
, /* transfer_flush_region */
1223 r600_texture_transfer_unmap
, /* transfer_unmap */
1224 NULL
/* transfer_inline_write */