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"
28 #include "evergreen_compute.h"
32 #include "util/u_format_s3tc.h"
33 #include "util/u_memory.h"
36 /* Same as resource_copy_region, except that both upsampling and downsampling are allowed. */
37 static void r600_copy_region_with_blit(struct pipe_context
*pipe
,
38 struct pipe_resource
*dst
,
40 unsigned dstx
, unsigned dsty
, unsigned dstz
,
41 struct pipe_resource
*src
,
43 const struct pipe_box
*src_box
)
45 struct pipe_blit_info blit
;
47 memset(&blit
, 0, sizeof(blit
));
48 blit
.src
.resource
= src
;
49 blit
.src
.format
= src
->format
;
50 blit
.src
.level
= src_level
;
51 blit
.src
.box
= *src_box
;
52 blit
.dst
.resource
= dst
;
53 blit
.dst
.format
= dst
->format
;
54 blit
.dst
.level
= dst_level
;
55 blit
.dst
.box
.x
= dstx
;
56 blit
.dst
.box
.y
= dsty
;
57 blit
.dst
.box
.z
= dstz
;
58 blit
.dst
.box
.width
= src_box
->width
;
59 blit
.dst
.box
.height
= src_box
->height
;
60 blit
.dst
.box
.depth
= src_box
->depth
;
61 blit
.mask
= util_format_get_mask(src
->format
) &
62 util_format_get_mask(dst
->format
);
63 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
66 pipe
->blit(pipe
, &blit
);
70 /* Copy from a full GPU texture to a transfer's staging one. */
71 static void r600_copy_to_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
73 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
74 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
75 struct pipe_resource
*dst
= &rtransfer
->staging
->b
.b
;
76 struct pipe_resource
*src
= transfer
->resource
;
78 if (src
->nr_samples
> 1) {
79 r600_copy_region_with_blit(ctx
, dst
, 0, 0, 0, 0,
80 src
, transfer
->level
, &transfer
->box
);
84 if (!rctx
->screen
->dma_blit(ctx
, dst
, 0, 0, 0, 0,
87 ctx
->resource_copy_region(ctx
, dst
, 0, 0, 0, 0,
88 src
, transfer
->level
, &transfer
->box
);
92 /* Copy from a transfer's staging texture to a full GPU one. */
93 static void r600_copy_from_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
95 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
96 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
97 struct pipe_resource
*dst
= transfer
->resource
;
98 struct pipe_resource
*src
= &rtransfer
->staging
->b
.b
;
101 u_box_3d(0, 0, 0, transfer
->box
.width
, transfer
->box
.height
, transfer
->box
.depth
, &sbox
);
103 if (dst
->nr_samples
> 1) {
104 r600_copy_region_with_blit(ctx
, dst
, transfer
->level
,
105 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
110 if (!rctx
->screen
->dma_blit(ctx
, dst
, transfer
->level
,
111 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
113 ctx
->resource_copy_region(ctx
, dst
, transfer
->level
,
114 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
119 static unsigned r600_texture_get_offset(struct r600_texture
*rtex
, unsigned level
,
120 const struct pipe_box
*box
)
122 enum pipe_format format
= rtex
->resource
.b
.b
.format
;
124 return rtex
->surface
.level
[level
].offset
+
125 box
->z
* rtex
->surface
.level
[level
].slice_size
+
126 box
->y
/ util_format_get_blockheight(format
) * rtex
->surface
.level
[level
].pitch_bytes
+
127 box
->x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
130 static int r600_init_surface(struct r600_screen
*rscreen
,
131 struct radeon_surface
*surface
,
132 const struct pipe_resource
*ptex
,
134 bool is_flushed_depth
)
136 const struct util_format_description
*desc
=
137 util_format_description(ptex
->format
);
138 bool is_depth
, is_stencil
;
140 is_depth
= util_format_has_depth(desc
);
141 is_stencil
= util_format_has_stencil(desc
);
143 surface
->npix_x
= ptex
->width0
;
144 surface
->npix_y
= ptex
->height0
;
145 surface
->npix_z
= ptex
->depth0
;
146 surface
->blk_w
= util_format_get_blockwidth(ptex
->format
);
147 surface
->blk_h
= util_format_get_blockheight(ptex
->format
);
149 surface
->array_size
= 1;
150 surface
->last_level
= ptex
->last_level
;
152 if (rscreen
->b
.chip_class
>= EVERGREEN
&& !is_flushed_depth
&&
153 ptex
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
) {
154 surface
->bpe
= 4; /* stencil is allocated separately on evergreen */
156 surface
->bpe
= util_format_get_blocksize(ptex
->format
);
157 /* align byte per element on dword */
158 if (surface
->bpe
== 3) {
163 surface
->nsamples
= ptex
->nr_samples
? ptex
->nr_samples
: 1;
166 switch (array_mode
) {
167 case V_038000_ARRAY_1D_TILED_THIN1
:
168 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_1D
, MODE
);
170 case V_038000_ARRAY_2D_TILED_THIN1
:
171 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_2D
, MODE
);
173 case V_038000_ARRAY_LINEAR_ALIGNED
:
174 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_LINEAR_ALIGNED
, MODE
);
176 case V_038000_ARRAY_LINEAR_GENERAL
:
178 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_LINEAR
, MODE
);
181 switch (ptex
->target
) {
182 case PIPE_TEXTURE_1D
:
183 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_1D
, TYPE
);
185 case PIPE_TEXTURE_RECT
:
186 case PIPE_TEXTURE_2D
:
187 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_2D
, TYPE
);
189 case PIPE_TEXTURE_3D
:
190 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_3D
, TYPE
);
192 case PIPE_TEXTURE_1D_ARRAY
:
193 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_1D_ARRAY
, TYPE
);
194 surface
->array_size
= ptex
->array_size
;
196 case PIPE_TEXTURE_2D_ARRAY
:
197 case PIPE_TEXTURE_CUBE_ARRAY
: /* cube array layout like 2d layout for now */
198 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_2D_ARRAY
, TYPE
);
199 surface
->array_size
= ptex
->array_size
;
201 case PIPE_TEXTURE_CUBE
:
202 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_CUBEMAP
, TYPE
);
208 if (ptex
->bind
& PIPE_BIND_SCANOUT
) {
209 surface
->flags
|= RADEON_SURF_SCANOUT
;
212 if (!is_flushed_depth
&& is_depth
) {
213 surface
->flags
|= RADEON_SURF_ZBUFFER
;
216 surface
->flags
|= RADEON_SURF_SBUFFER
|
217 RADEON_SURF_HAS_SBUFFER_MIPTREE
;
223 static int r600_setup_surface(struct pipe_screen
*screen
,
224 struct r600_texture
*rtex
,
225 unsigned pitch_in_bytes_override
)
227 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
;
228 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
232 r
= rscreen
->b
.ws
->surface_init(rscreen
->b
.ws
, &rtex
->surface
);
236 rtex
->size
= rtex
->surface
.bo_size
;
237 if (pitch_in_bytes_override
&& pitch_in_bytes_override
!= rtex
->surface
.level
[0].pitch_bytes
) {
238 /* old ddx on evergreen over estimate alignment for 1d, only 1 level
241 rtex
->surface
.level
[0].nblk_x
= pitch_in_bytes_override
/ rtex
->surface
.bpe
;
242 rtex
->surface
.level
[0].pitch_bytes
= pitch_in_bytes_override
;
243 rtex
->surface
.level
[0].slice_size
= pitch_in_bytes_override
* rtex
->surface
.level
[0].nblk_y
;
244 if (rtex
->surface
.flags
& RADEON_SURF_SBUFFER
) {
245 rtex
->surface
.stencil_offset
=
246 rtex
->surface
.stencil_level
[0].offset
= rtex
->surface
.level
[0].slice_size
;
249 for (i
= 0; i
<= ptex
->last_level
; i
++) {
250 switch (rtex
->surface
.level
[i
].mode
) {
251 case RADEON_SURF_MODE_LINEAR_ALIGNED
:
252 rtex
->array_mode
[i
] = V_038000_ARRAY_LINEAR_ALIGNED
;
254 case RADEON_SURF_MODE_1D
:
255 rtex
->array_mode
[i
] = V_038000_ARRAY_1D_TILED_THIN1
;
257 case RADEON_SURF_MODE_2D
:
258 rtex
->array_mode
[i
] = V_038000_ARRAY_2D_TILED_THIN1
;
261 case RADEON_SURF_MODE_LINEAR
:
262 rtex
->array_mode
[i
] = 0;
269 static boolean
r600_texture_get_handle(struct pipe_screen
* screen
,
270 struct pipe_resource
*ptex
,
271 struct winsys_handle
*whandle
)
273 struct r600_texture
*rtex
= (struct r600_texture
*)ptex
;
274 struct r600_resource
*resource
= &rtex
->resource
;
275 struct radeon_surface
*surface
= &rtex
->surface
;
276 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
278 rscreen
->b
.ws
->buffer_set_tiling(resource
->buf
,
280 surface
->level
[0].mode
>= RADEON_SURF_MODE_1D
?
281 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
,
282 surface
->level
[0].mode
>= RADEON_SURF_MODE_2D
?
283 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
,
284 surface
->bankw
, surface
->bankh
,
286 surface
->stencil_tile_split
,
288 rtex
->surface
.level
[0].pitch_bytes
);
290 return rscreen
->b
.ws
->buffer_get_handle(resource
->buf
,
291 rtex
->surface
.level
[0].pitch_bytes
, whandle
);
294 static void r600_texture_destroy(struct pipe_screen
*screen
,
295 struct pipe_resource
*ptex
)
297 struct r600_texture
*rtex
= (struct r600_texture
*)ptex
;
298 struct r600_resource
*resource
= &rtex
->resource
;
300 if (rtex
->flushed_depth_texture
)
301 pipe_resource_reference((struct pipe_resource
**)&rtex
->flushed_depth_texture
, NULL
);
303 pipe_resource_reference((struct pipe_resource
**)&rtex
->htile
, NULL
);
304 pb_reference(&resource
->buf
, NULL
);
308 static const struct u_resource_vtbl r600_texture_vtbl
;
310 /* The number of samples can be specified independently of the texture. */
311 void r600_texture_get_fmask_info(struct r600_screen
*rscreen
,
312 struct r600_texture
*rtex
,
314 struct r600_fmask_info
*out
)
316 /* FMASK is allocated like an ordinary texture. */
317 struct radeon_surface fmask
= rtex
->surface
;
319 memset(out
, 0, sizeof(*out
));
321 fmask
.bo_alignment
= 0;
324 fmask
.flags
|= RADEON_SURF_FMASK
;
326 switch (nr_samples
) {
336 R600_ERR("Invalid sample count for FMASK allocation.\n");
340 /* Overallocate FMASK on R600-R700 to fix colorbuffer corruption.
341 * This can be fixed by writing a separate FMASK allocator specifically
342 * for R600-R700 asics. */
343 if (rscreen
->b
.chip_class
<= R700
) {
347 if (rscreen
->b
.ws
->surface_init(rscreen
->b
.ws
, &fmask
)) {
348 R600_ERR("Got error in surface_init while allocating FMASK.\n");
352 assert(fmask
.level
[0].mode
== RADEON_SURF_MODE_2D
);
354 out
->slice_tile_max
= (fmask
.level
[0].nblk_x
* fmask
.level
[0].nblk_y
) / 64;
355 if (out
->slice_tile_max
)
356 out
->slice_tile_max
-= 1;
358 out
->bank_height
= fmask
.bankh
;
359 out
->alignment
= MAX2(256, fmask
.bo_alignment
);
360 out
->size
= fmask
.bo_size
;
363 static void r600_texture_allocate_fmask(struct r600_screen
*rscreen
,
364 struct r600_texture
*rtex
)
366 struct r600_fmask_info fmask
;
368 r600_texture_get_fmask_info(rscreen
, rtex
,
369 rtex
->resource
.b
.b
.nr_samples
, &fmask
);
371 rtex
->fmask_bank_height
= fmask
.bank_height
;
372 rtex
->fmask_slice_tile_max
= fmask
.slice_tile_max
;
373 rtex
->fmask_offset
= align(rtex
->size
, fmask
.alignment
);
374 rtex
->fmask_size
= fmask
.size
;
375 rtex
->size
= rtex
->fmask_offset
+ rtex
->fmask_size
;
377 printf("FMASK width=%u, height=%i, bits=%u, size=%u\n",
378 fmask
.npix_x
, fmask
.npix_y
, fmask
.bpe
* fmask
.nsamples
, rtex
->fmask_size
);
382 void r600_texture_get_cmask_info(struct r600_screen
*rscreen
,
383 struct r600_texture
*rtex
,
384 struct r600_cmask_info
*out
)
386 unsigned cmask_tile_width
= 8;
387 unsigned cmask_tile_height
= 8;
388 unsigned cmask_tile_elements
= cmask_tile_width
* cmask_tile_height
;
389 unsigned element_bits
= 4;
390 unsigned cmask_cache_bits
= 1024;
391 unsigned num_pipes
= rscreen
->tiling_info
.num_channels
;
392 unsigned pipe_interleave_bytes
= rscreen
->tiling_info
.group_bytes
;
394 unsigned elements_per_macro_tile
= (cmask_cache_bits
/ element_bits
) * num_pipes
;
395 unsigned pixels_per_macro_tile
= elements_per_macro_tile
* cmask_tile_elements
;
396 unsigned sqrt_pixels_per_macro_tile
= sqrt(pixels_per_macro_tile
);
397 unsigned macro_tile_width
= util_next_power_of_two(sqrt_pixels_per_macro_tile
);
398 unsigned macro_tile_height
= pixels_per_macro_tile
/ macro_tile_width
;
400 unsigned pitch_elements
= align(rtex
->surface
.npix_x
, macro_tile_width
);
401 unsigned height
= align(rtex
->surface
.npix_y
, macro_tile_height
);
403 unsigned base_align
= num_pipes
* pipe_interleave_bytes
;
404 unsigned slice_bytes
=
405 ((pitch_elements
* height
* element_bits
+ 7) / 8) / cmask_tile_elements
;
407 assert(macro_tile_width
% 128 == 0);
408 assert(macro_tile_height
% 128 == 0);
410 out
->slice_tile_max
= ((pitch_elements
* height
) / (128*128)) - 1;
411 out
->alignment
= MAX2(256, base_align
);
412 out
->size
= rtex
->surface
.array_size
* align(slice_bytes
, base_align
);
415 static void r600_texture_allocate_cmask(struct r600_screen
*rscreen
,
416 struct r600_texture
*rtex
)
418 struct r600_cmask_info cmask
;
420 r600_texture_get_cmask_info(rscreen
, rtex
, &cmask
);
422 rtex
->cmask_slice_tile_max
= cmask
.slice_tile_max
;
423 rtex
->cmask_offset
= align(rtex
->size
, cmask
.alignment
);
424 rtex
->cmask_size
= cmask
.size
;
425 rtex
->size
= rtex
->cmask_offset
+ rtex
->cmask_size
;
427 printf("CMASK: macro tile width = %u, macro tile height = %u, "
428 "pitch elements = %u, height = %u, slice tile max = %u\n",
429 macro_tile_width
, macro_tile_height
, pitch_elements
, height
,
430 rtex
->cmask_slice_tile_max
);
434 static struct r600_texture
*
435 r600_texture_create_object(struct pipe_screen
*screen
,
436 const struct pipe_resource
*base
,
437 unsigned pitch_in_bytes_override
,
438 struct pb_buffer
*buf
,
439 struct radeon_surface
*surface
)
441 struct r600_texture
*rtex
;
442 struct r600_resource
*resource
;
443 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
446 rtex
= CALLOC_STRUCT(r600_texture
);
450 resource
= &rtex
->resource
;
451 resource
->b
.b
= *base
;
452 resource
->b
.vtbl
= &r600_texture_vtbl
;
453 pipe_reference_init(&resource
->b
.b
.reference
, 1);
454 resource
->b
.b
.screen
= screen
;
455 rtex
->pitch_override
= pitch_in_bytes_override
;
457 /* don't include stencil-only formats which we don't support for rendering */
458 rtex
->is_depth
= util_format_has_depth(util_format_description(rtex
->resource
.b
.b
.format
));
460 rtex
->surface
= *surface
;
461 r
= r600_setup_surface(screen
, rtex
, pitch_in_bytes_override
);
467 if (base
->nr_samples
> 1 && !rtex
->is_depth
&& !buf
) {
468 r600_texture_allocate_fmask(rscreen
, rtex
);
469 r600_texture_allocate_cmask(rscreen
, rtex
);
472 if (!rtex
->is_depth
&& base
->nr_samples
> 1 &&
473 (!rtex
->fmask_size
|| !rtex
->cmask_size
)) {
478 /* Tiled depth textures utilize the non-displayable tile order. */
479 rtex
->non_disp_tiling
= rtex
->is_depth
&& rtex
->surface
.level
[0].mode
>= RADEON_SURF_MODE_1D
;
481 /* only enable hyperz for PIPE_TEXTURE_2D not for PIPE_TEXTURE_2D_ARRAY
482 * Thought it might still be interessting to use hyperz for texture
483 * array without using fast clear features
486 if (!(base
->flags
& (R600_RESOURCE_FLAG_TRANSFER
| R600_RESOURCE_FLAG_FLUSHED_DEPTH
)) &&
487 util_format_is_depth_or_stencil(base
->format
) &&
488 rscreen
->b
.info
.drm_minor
>= 26 &&
489 !(rscreen
->debug_flags
& DBG_NO_HYPERZ
) &&
490 base
->target
== PIPE_TEXTURE_2D
&&
491 rtex
->surface
.level
[0].nblk_x
>= 32 &&
492 rtex
->surface
.level
[0].nblk_y
>= 32) {
493 unsigned sw
= rtex
->surface
.level
[0].nblk_x
* rtex
->surface
.blk_w
;
494 unsigned sh
= rtex
->surface
.level
[0].nblk_y
* rtex
->surface
.blk_h
;
496 unsigned npipes
= rscreen
->b
.info
.r600_num_tile_pipes
;
498 /* this alignment and htile size only apply to linear htile buffer */
499 sw
= align(sw
, 16 << 3);
500 sh
= align(sh
, npipes
<< 3);
501 htile_size
= (sw
>> 3) * (sh
>> 3) * 4;
502 /* must be aligned with 2K * npipes */
503 htile_size
= align(htile_size
, (2 << 10) * npipes
);
505 rtex
->htile
= (struct r600_resource
*)pipe_buffer_create(&rscreen
->b
.b
, PIPE_BIND_CUSTOM
,
506 PIPE_USAGE_STATIC
, htile_size
);
507 if (rtex
->htile
== NULL
) {
508 /* this is not a fatal error as we can still keep rendering
509 * without htile buffer
511 R600_ERR("r600: failed to create bo for htile buffers\n");
513 r600_screen_clear_buffer(rscreen
, &rtex
->htile
->b
.b
, 0, htile_size
, 0);
517 /* Now create the backing buffer. */
519 unsigned base_align
= rtex
->surface
.bo_alignment
;
520 unsigned usage
= R600_TEX_IS_TILED(rtex
, 0) ? PIPE_USAGE_STATIC
: base
->usage
;
522 if (!r600_init_resource(rscreen
, resource
, rtex
->size
, base_align
, FALSE
, usage
)) {
527 /* This is usually the window framebuffer. We want it in VRAM, always. */
529 resource
->cs_buf
= rscreen
->b
.ws
->buffer_get_cs_handle(buf
);
530 resource
->domains
= RADEON_DOMAIN_VRAM
;
533 if (rtex
->cmask_size
) {
534 /* Initialize the cmask to 0xCC (= compressed state). */
535 r600_screen_clear_buffer(rscreen
, &rtex
->resource
.b
.b
,
536 rtex
->cmask_offset
, rtex
->cmask_size
, 0xCC);
539 if (rscreen
->debug_flags
& DBG_VM
) {
540 fprintf(stderr
, "VM start=0x%llX end=0x%llX | Texture %ix%ix%i, %i levels, %i samples, %s\n",
541 r600_resource_va(screen
, &rtex
->resource
.b
.b
),
542 r600_resource_va(screen
, &rtex
->resource
.b
.b
) + rtex
->resource
.buf
->size
,
543 base
->width0
, base
->height0
, util_max_layer(base
, 0)+1, base
->last_level
+1,
544 base
->nr_samples
? base
->nr_samples
: 1, util_format_short_name(base
->format
));
547 if (rscreen
->debug_flags
& DBG_TEX_DEPTH
&& rtex
->is_depth
&& rtex
->non_disp_tiling
) {
548 printf("Texture: npix_x=%u, npix_y=%u, npix_z=%u, blk_w=%u, "
549 "blk_h=%u, blk_d=%u, array_size=%u, last_level=%u, "
550 "bpe=%u, nsamples=%u, flags=%u\n",
551 rtex
->surface
.npix_x
, rtex
->surface
.npix_y
,
552 rtex
->surface
.npix_z
, rtex
->surface
.blk_w
,
553 rtex
->surface
.blk_h
, rtex
->surface
.blk_d
,
554 rtex
->surface
.array_size
, rtex
->surface
.last_level
,
555 rtex
->surface
.bpe
, rtex
->surface
.nsamples
,
556 rtex
->surface
.flags
);
557 if (rtex
->surface
.flags
& RADEON_SURF_ZBUFFER
) {
558 for (int i
= 0; i
<= rtex
->surface
.last_level
; i
++) {
559 printf(" Z %i: offset=%llu, slice_size=%llu, npix_x=%u, "
560 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
561 "nblk_z=%u, pitch_bytes=%u, mode=%u\n",
562 i
, (unsigned long long)rtex
->surface
.level
[i
].offset
,
563 (unsigned long long)rtex
->surface
.level
[i
].slice_size
,
564 u_minify(rtex
->resource
.b
.b
.width0
, i
),
565 u_minify(rtex
->resource
.b
.b
.height0
, i
),
566 u_minify(rtex
->resource
.b
.b
.depth0
, i
),
567 rtex
->surface
.level
[i
].nblk_x
,
568 rtex
->surface
.level
[i
].nblk_y
,
569 rtex
->surface
.level
[i
].nblk_z
,
570 rtex
->surface
.level
[i
].pitch_bytes
,
571 rtex
->surface
.level
[i
].mode
);
574 if (rtex
->surface
.flags
& RADEON_SURF_SBUFFER
) {
575 for (int i
= 0; i
<= rtex
->surface
.last_level
; i
++) {
576 printf(" S %i: offset=%llu, slice_size=%llu, npix_x=%u, "
577 "npix_y=%u, npix_z=%u, nblk_x=%u, nblk_y=%u, "
578 "nblk_z=%u, pitch_bytes=%u, mode=%u\n",
579 i
, (unsigned long long)rtex
->surface
.stencil_level
[i
].offset
,
580 (unsigned long long)rtex
->surface
.stencil_level
[i
].slice_size
,
581 u_minify(rtex
->resource
.b
.b
.width0
, i
),
582 u_minify(rtex
->resource
.b
.b
.height0
, i
),
583 u_minify(rtex
->resource
.b
.b
.depth0
, i
),
584 rtex
->surface
.stencil_level
[i
].nblk_x
,
585 rtex
->surface
.stencil_level
[i
].nblk_y
,
586 rtex
->surface
.stencil_level
[i
].nblk_z
,
587 rtex
->surface
.stencil_level
[i
].pitch_bytes
,
588 rtex
->surface
.stencil_level
[i
].mode
);
595 struct pipe_resource
*r600_texture_create(struct pipe_screen
*screen
,
596 const struct pipe_resource
*templ
)
598 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
599 struct radeon_surface surface
;
600 const struct util_format_description
*desc
= util_format_description(templ
->format
);
604 /* Default tiling mode for staging textures. */
605 array_mode
= V_038000_ARRAY_LINEAR_ALIGNED
;
607 /* Tiling doesn't work with the 422 (SUBSAMPLED) formats. That's not an issue,
608 * because 422 formats are used for videos, which prefer linear buffers
609 * for fast uploads anyway. */
610 if (!(templ
->flags
& R600_RESOURCE_FLAG_TRANSFER
) &&
611 desc
->layout
!= UTIL_FORMAT_LAYOUT_SUBSAMPLED
) {
612 if (templ
->flags
& R600_RESOURCE_FLAG_FORCE_TILING
) {
613 array_mode
= V_038000_ARRAY_2D_TILED_THIN1
;
614 } else if (!(templ
->bind
& PIPE_BIND_SCANOUT
) &&
615 templ
->usage
!= PIPE_USAGE_STAGING
&&
616 templ
->usage
!= PIPE_USAGE_STREAM
&&
617 templ
->target
!= PIPE_TEXTURE_1D
&&
618 templ
->target
!= PIPE_TEXTURE_1D_ARRAY
&&
619 templ
->height0
> 3) {
620 array_mode
= V_038000_ARRAY_2D_TILED_THIN1
;
621 } else if (util_format_is_compressed(templ
->format
)) {
622 array_mode
= V_038000_ARRAY_1D_TILED_THIN1
;
626 r
= r600_init_surface(rscreen
, &surface
, templ
, array_mode
,
627 templ
->flags
& R600_RESOURCE_FLAG_FLUSHED_DEPTH
);
631 r
= rscreen
->b
.ws
->surface_best(rscreen
->b
.ws
, &surface
);
635 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
,
639 struct pipe_surface
*r600_create_surface_custom(struct pipe_context
*pipe
,
640 struct pipe_resource
*texture
,
641 const struct pipe_surface
*templ
,
642 unsigned width
, unsigned height
)
644 struct r600_surface
*surface
= CALLOC_STRUCT(r600_surface
);
646 assert(templ
->u
.tex
.first_layer
<= util_max_layer(texture
, templ
->u
.tex
.level
));
647 assert(templ
->u
.tex
.last_layer
<= util_max_layer(texture
, templ
->u
.tex
.level
));
648 assert(templ
->u
.tex
.first_layer
== templ
->u
.tex
.last_layer
);
651 pipe_reference_init(&surface
->base
.reference
, 1);
652 pipe_resource_reference(&surface
->base
.texture
, texture
);
653 surface
->base
.context
= pipe
;
654 surface
->base
.format
= templ
->format
;
655 surface
->base
.width
= width
;
656 surface
->base
.height
= height
;
657 surface
->base
.u
= templ
->u
;
658 return &surface
->base
;
661 static struct pipe_surface
*r600_create_surface(struct pipe_context
*pipe
,
662 struct pipe_resource
*tex
,
663 const struct pipe_surface
*templ
)
665 unsigned level
= templ
->u
.tex
.level
;
667 return r600_create_surface_custom(pipe
, tex
, templ
,
668 u_minify(tex
->width0
, level
),
669 u_minify(tex
->height0
, level
));
672 static void r600_surface_destroy(struct pipe_context
*pipe
,
673 struct pipe_surface
*surface
)
675 struct r600_surface
*surf
= (struct r600_surface
*)surface
;
676 pipe_resource_reference((struct pipe_resource
**)&surf
->cb_buffer_fmask
, NULL
);
677 pipe_resource_reference((struct pipe_resource
**)&surf
->cb_buffer_cmask
, NULL
);
678 pipe_resource_reference(&surface
->texture
, NULL
);
682 struct pipe_resource
*r600_texture_from_handle(struct pipe_screen
*screen
,
683 const struct pipe_resource
*templ
,
684 struct winsys_handle
*whandle
)
686 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
687 struct pb_buffer
*buf
= NULL
;
689 unsigned array_mode
= 0;
690 enum radeon_bo_layout micro
, macro
;
691 struct radeon_surface surface
;
694 /* Support only 2D textures without mipmaps */
695 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
) ||
696 templ
->depth0
!= 1 || templ
->last_level
!= 0)
699 buf
= rscreen
->b
.ws
->buffer_from_handle(rscreen
->b
.ws
, whandle
, &stride
);
703 rscreen
->b
.ws
->buffer_get_tiling(buf
, µ
, ¯o
,
704 &surface
.bankw
, &surface
.bankh
,
706 &surface
.stencil_tile_split
,
709 if (macro
== RADEON_LAYOUT_TILED
)
710 array_mode
= V_0280A0_ARRAY_2D_TILED_THIN1
;
711 else if (micro
== RADEON_LAYOUT_TILED
)
712 array_mode
= V_0280A0_ARRAY_1D_TILED_THIN1
;
714 array_mode
= V_038000_ARRAY_LINEAR_ALIGNED
;
716 r
= r600_init_surface(rscreen
, &surface
, templ
, array_mode
, false);
720 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
,
721 stride
, buf
, &surface
);
724 bool r600_init_flushed_depth_texture(struct pipe_context
*ctx
,
725 struct pipe_resource
*texture
,
726 struct r600_texture
**staging
)
728 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
729 struct pipe_resource resource
;
730 struct r600_texture
**flushed_depth_texture
= staging
?
731 staging
: &rtex
->flushed_depth_texture
;
733 if (!staging
&& rtex
->flushed_depth_texture
)
734 return true; /* it's ready */
736 resource
.target
= texture
->target
;
737 resource
.format
= texture
->format
;
738 resource
.width0
= texture
->width0
;
739 resource
.height0
= texture
->height0
;
740 resource
.depth0
= texture
->depth0
;
741 resource
.array_size
= texture
->array_size
;
742 resource
.last_level
= texture
->last_level
;
743 resource
.nr_samples
= texture
->nr_samples
;
744 resource
.usage
= staging
? PIPE_USAGE_STAGING
: PIPE_USAGE_STATIC
;
745 resource
.bind
= texture
->bind
& ~PIPE_BIND_DEPTH_STENCIL
;
746 resource
.flags
= texture
->flags
| R600_RESOURCE_FLAG_FLUSHED_DEPTH
;
749 resource
.flags
|= R600_RESOURCE_FLAG_TRANSFER
;
751 *flushed_depth_texture
= (struct r600_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
752 if (*flushed_depth_texture
== NULL
) {
753 R600_ERR("failed to create temporary texture to hold flushed depth\n");
757 (*flushed_depth_texture
)->is_flushing_texture
= TRUE
;
758 (*flushed_depth_texture
)->non_disp_tiling
= false;
763 * Initialize the pipe_resource descriptor to be of the same size as the box,
764 * which is supposed to hold a subregion of the texture "orig" at the given
767 static void r600_init_temp_resource_from_box(struct pipe_resource
*res
,
768 struct pipe_resource
*orig
,
769 const struct pipe_box
*box
,
770 unsigned level
, unsigned flags
)
772 memset(res
, 0, sizeof(*res
));
773 res
->format
= orig
->format
;
774 res
->width0
= box
->width
;
775 res
->height0
= box
->height
;
778 res
->usage
= flags
& R600_RESOURCE_FLAG_TRANSFER
? PIPE_USAGE_STAGING
: PIPE_USAGE_STATIC
;
781 /* We must set the correct texture target and dimensions for a 3D box. */
782 if (box
->depth
> 1 && util_max_layer(orig
, level
) > 0)
783 res
->target
= orig
->target
;
785 res
->target
= PIPE_TEXTURE_2D
;
787 switch (res
->target
) {
788 case PIPE_TEXTURE_1D_ARRAY
:
789 case PIPE_TEXTURE_2D_ARRAY
:
790 case PIPE_TEXTURE_CUBE_ARRAY
:
791 res
->array_size
= box
->depth
;
793 case PIPE_TEXTURE_3D
:
794 res
->depth0
= box
->depth
;
800 static void *r600_texture_transfer_map(struct pipe_context
*ctx
,
801 struct pipe_resource
*texture
,
804 const struct pipe_box
*box
,
805 struct pipe_transfer
**ptransfer
)
807 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
808 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
809 struct r600_transfer
*trans
;
810 boolean use_staging_texture
= FALSE
;
811 struct r600_resource
*buf
;
815 if ((texture
->bind
& PIPE_BIND_GLOBAL
) && texture
->target
== PIPE_BUFFER
) {
816 return r600_compute_global_transfer_map(ctx
, texture
, level
, usage
, box
, ptransfer
);
819 /* We cannot map a tiled texture directly because the data is
820 * in a different order, therefore we do detiling using a blit.
822 * Also, use a temporary in GTT memory for read transfers, as
823 * the CPU is much happier reading out of cached system memory
824 * than uncached VRAM.
826 if (R600_TEX_IS_TILED(rtex
, level
)) {
827 use_staging_texture
= TRUE
;
830 /* Use a staging texture for uploads if the underlying BO is busy. */
831 if (!(usage
& PIPE_TRANSFER_READ
) &&
832 (r600_rings_is_buffer_referenced(rctx
, rtex
->resource
.cs_buf
, RADEON_USAGE_READWRITE
) ||
833 rctx
->b
.ws
->buffer_is_busy(rtex
->resource
.buf
, RADEON_USAGE_READWRITE
))) {
834 use_staging_texture
= TRUE
;
837 if (texture
->flags
& R600_RESOURCE_FLAG_TRANSFER
) {
838 use_staging_texture
= FALSE
;
841 if (use_staging_texture
&& (usage
& PIPE_TRANSFER_MAP_DIRECTLY
)) {
845 trans
= CALLOC_STRUCT(r600_transfer
);
848 trans
->transfer
.resource
= texture
;
849 trans
->transfer
.level
= level
;
850 trans
->transfer
.usage
= usage
;
851 trans
->transfer
.box
= *box
;
853 if (rtex
->is_depth
) {
854 struct r600_texture
*staging_depth
;
856 if (rtex
->resource
.b
.b
.nr_samples
> 1) {
857 /* MSAA depth buffers need to be converted to single sample buffers.
859 * Mapping MSAA depth buffers can occur if ReadPixels is called
860 * with a multisample GLX visual.
862 * First downsample the depth buffer to a temporary texture,
863 * then decompress the temporary one to staging.
865 * Only the region being mapped is transfered.
867 struct pipe_resource resource
;
869 r600_init_temp_resource_from_box(&resource
, texture
, box
, level
, 0);
871 if (!r600_init_flushed_depth_texture(ctx
, &resource
, &staging_depth
)) {
872 R600_ERR("failed to create temporary texture to hold untiled copy\n");
877 if (usage
& PIPE_TRANSFER_READ
) {
878 struct pipe_resource
*temp
= ctx
->screen
->resource_create(ctx
->screen
, &resource
);
880 r600_copy_region_with_blit(ctx
, temp
, 0, 0, 0, 0, texture
, level
, box
);
881 r600_blit_decompress_depth(ctx
, (struct r600_texture
*)temp
, staging_depth
,
882 0, 0, 0, box
->depth
, 0, 0);
883 pipe_resource_reference((struct pipe_resource
**)&temp
, NULL
);
887 /* XXX: only readback the rectangle which is being mapped? */
888 /* XXX: when discard is true, no need to read back from depth texture */
889 if (!r600_init_flushed_depth_texture(ctx
, texture
, &staging_depth
)) {
890 R600_ERR("failed to create temporary texture to hold untiled copy\n");
895 r600_blit_decompress_depth(ctx
, rtex
, staging_depth
,
897 box
->z
, box
->z
+ box
->depth
- 1,
900 offset
= r600_texture_get_offset(staging_depth
, level
, box
);
903 trans
->transfer
.stride
= staging_depth
->surface
.level
[level
].pitch_bytes
;
904 trans
->transfer
.layer_stride
= staging_depth
->surface
.level
[level
].slice_size
;
905 trans
->staging
= (struct r600_resource
*)staging_depth
;
906 } else if (use_staging_texture
) {
907 struct pipe_resource resource
;
908 struct r600_texture
*staging
;
910 r600_init_temp_resource_from_box(&resource
, texture
, box
, level
,
911 R600_RESOURCE_FLAG_TRANSFER
);
913 /* Create the temporary texture. */
914 staging
= (struct r600_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
915 if (staging
== NULL
) {
916 R600_ERR("failed to create temporary texture to hold untiled copy\n");
920 trans
->staging
= &staging
->resource
;
921 trans
->transfer
.stride
= staging
->surface
.level
[0].pitch_bytes
;
922 trans
->transfer
.layer_stride
= staging
->surface
.level
[0].slice_size
;
923 if (usage
& PIPE_TRANSFER_READ
) {
924 r600_copy_to_staging_texture(ctx
, trans
);
927 /* the resource is mapped directly */
928 trans
->transfer
.stride
= rtex
->surface
.level
[level
].pitch_bytes
;
929 trans
->transfer
.layer_stride
= rtex
->surface
.level
[level
].slice_size
;
930 offset
= r600_texture_get_offset(rtex
, level
, box
);
933 if (trans
->staging
) {
934 buf
= trans
->staging
;
936 buf
= &rtex
->resource
;
939 if (!(map
= r600_buffer_mmap_sync_with_rings(rctx
, buf
, usage
))) {
940 pipe_resource_reference((struct pipe_resource
**)&trans
->staging
, NULL
);
945 *ptransfer
= &trans
->transfer
;
949 static void r600_texture_transfer_unmap(struct pipe_context
*ctx
,
950 struct pipe_transfer
* transfer
)
952 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
953 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
954 struct radeon_winsys_cs_handle
*buf
;
955 struct pipe_resource
*texture
= transfer
->resource
;
956 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
958 if ((transfer
->resource
->bind
& PIPE_BIND_GLOBAL
) && transfer
->resource
->target
== PIPE_BUFFER
) {
959 return r600_compute_global_transfer_unmap(ctx
, transfer
);
962 if (rtransfer
->staging
) {
963 buf
= ((struct r600_resource
*)rtransfer
->staging
)->cs_buf
;
965 buf
= ((struct r600_resource
*)transfer
->resource
)->cs_buf
;
967 rctx
->b
.ws
->buffer_unmap(buf
);
969 if ((transfer
->usage
& PIPE_TRANSFER_WRITE
) && rtransfer
->staging
) {
970 if (rtex
->is_depth
&& rtex
->resource
.b
.b
.nr_samples
<= 1) {
971 ctx
->resource_copy_region(ctx
, texture
, transfer
->level
,
972 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
973 &rtransfer
->staging
->b
.b
, transfer
->level
,
976 r600_copy_from_staging_texture(ctx
, rtransfer
);
980 if (rtransfer
->staging
)
981 pipe_resource_reference((struct pipe_resource
**)&rtransfer
->staging
, NULL
);
986 void r600_init_surface_functions(struct r600_context
*r600
)
988 r600
->b
.b
.create_surface
= r600_create_surface
;
989 r600
->b
.b
.surface_destroy
= r600_surface_destroy
;
992 unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format
,
993 const unsigned char *swizzle_view
,
997 unsigned char swizzle
[4];
999 const uint32_t tex_swizzle_shift
[4] = {
1002 const uint32_t vtx_swizzle_shift
[4] = {
1005 const uint32_t swizzle_bit
[4] = {
1008 const uint32_t *swizzle_shift
= tex_swizzle_shift
;
1011 swizzle_shift
= vtx_swizzle_shift
;
1014 util_format_compose_swizzles(swizzle_format
, swizzle_view
, swizzle
);
1016 memcpy(swizzle
, swizzle_format
, 4);
1020 for (i
= 0; i
< 4; i
++) {
1021 switch (swizzle
[i
]) {
1022 case UTIL_FORMAT_SWIZZLE_Y
:
1023 result
|= swizzle_bit
[1] << swizzle_shift
[i
];
1025 case UTIL_FORMAT_SWIZZLE_Z
:
1026 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
1028 case UTIL_FORMAT_SWIZZLE_W
:
1029 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
1031 case UTIL_FORMAT_SWIZZLE_0
:
1032 result
|= V_038010_SQ_SEL_0
<< swizzle_shift
[i
];
1034 case UTIL_FORMAT_SWIZZLE_1
:
1035 result
|= V_038010_SQ_SEL_1
<< swizzle_shift
[i
];
1037 default: /* UTIL_FORMAT_SWIZZLE_X */
1038 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
1044 /* texture format translate */
1045 uint32_t r600_translate_texformat(struct pipe_screen
*screen
,
1046 enum pipe_format format
,
1047 const unsigned char *swizzle_view
,
1048 uint32_t *word4_p
, uint32_t *yuv_format_p
)
1050 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
1051 uint32_t result
= 0, word4
= 0, yuv_format
= 0;
1052 const struct util_format_description
*desc
;
1053 boolean uniform
= TRUE
;
1054 bool enable_s3tc
= rscreen
->b
.info
.drm_minor
>= 9;
1055 bool is_srgb_valid
= FALSE
;
1056 const unsigned char swizzle_xxxx
[4] = {0, 0, 0, 0};
1057 const unsigned char swizzle_yyyy
[4] = {1, 1, 1, 1};
1060 const uint32_t sign_bit
[4] = {
1061 S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED
),
1062 S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED
),
1063 S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED
),
1064 S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED
)
1066 desc
= util_format_description(format
);
1068 /* Depth and stencil swizzling is handled separately. */
1069 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
) {
1070 word4
|= r600_get_swizzle_combined(desc
->swizzle
, swizzle_view
, FALSE
);
1073 /* Colorspace (return non-RGB formats directly). */
1074 switch (desc
->colorspace
) {
1075 /* Depth stencil formats */
1076 case UTIL_FORMAT_COLORSPACE_ZS
:
1078 /* Depth sampler formats. */
1079 case PIPE_FORMAT_Z16_UNORM
:
1080 word4
|= r600_get_swizzle_combined(swizzle_xxxx
, swizzle_view
, FALSE
);
1083 case PIPE_FORMAT_Z24X8_UNORM
:
1084 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1085 word4
|= r600_get_swizzle_combined(swizzle_xxxx
, swizzle_view
, FALSE
);
1088 case PIPE_FORMAT_X8Z24_UNORM
:
1089 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1090 if (rscreen
->b
.chip_class
< EVERGREEN
)
1092 word4
|= r600_get_swizzle_combined(swizzle_yyyy
, swizzle_view
, FALSE
);
1095 case PIPE_FORMAT_Z32_FLOAT
:
1096 word4
|= r600_get_swizzle_combined(swizzle_xxxx
, swizzle_view
, FALSE
);
1097 result
= FMT_32_FLOAT
;
1099 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1100 word4
|= r600_get_swizzle_combined(swizzle_xxxx
, swizzle_view
, FALSE
);
1101 result
= FMT_X24_8_32_FLOAT
;
1103 /* Stencil sampler formats. */
1104 case PIPE_FORMAT_S8_UINT
:
1105 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
1106 word4
|= r600_get_swizzle_combined(swizzle_xxxx
, swizzle_view
, FALSE
);
1109 case PIPE_FORMAT_X24S8_UINT
:
1110 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
1111 word4
|= r600_get_swizzle_combined(swizzle_yyyy
, swizzle_view
, FALSE
);
1114 case PIPE_FORMAT_S8X24_UINT
:
1115 if (rscreen
->b
.chip_class
< EVERGREEN
)
1117 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
1118 word4
|= r600_get_swizzle_combined(swizzle_xxxx
, swizzle_view
, FALSE
);
1121 case PIPE_FORMAT_X32_S8X24_UINT
:
1122 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
1123 word4
|= r600_get_swizzle_combined(swizzle_yyyy
, swizzle_view
, FALSE
);
1124 result
= FMT_X24_8_32_FLOAT
;
1130 case UTIL_FORMAT_COLORSPACE_YUV
:
1131 yuv_format
|= (1 << 30);
1133 case PIPE_FORMAT_UYVY
:
1134 case PIPE_FORMAT_YUYV
:
1138 goto out_unknown
; /* XXX */
1140 case UTIL_FORMAT_COLORSPACE_SRGB
:
1141 word4
|= S_038010_FORCE_DEGAMMA(1);
1148 if (desc
->layout
== UTIL_FORMAT_LAYOUT_RGTC
) {
1153 case PIPE_FORMAT_RGTC1_SNORM
:
1154 case PIPE_FORMAT_LATC1_SNORM
:
1155 word4
|= sign_bit
[0];
1156 case PIPE_FORMAT_RGTC1_UNORM
:
1157 case PIPE_FORMAT_LATC1_UNORM
:
1160 case PIPE_FORMAT_RGTC2_SNORM
:
1161 case PIPE_FORMAT_LATC2_SNORM
:
1162 word4
|= sign_bit
[0] | sign_bit
[1];
1163 case PIPE_FORMAT_RGTC2_UNORM
:
1164 case PIPE_FORMAT_LATC2_UNORM
:
1172 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
1177 if (!util_format_s3tc_enabled
) {
1182 case PIPE_FORMAT_DXT1_RGB
:
1183 case PIPE_FORMAT_DXT1_RGBA
:
1184 case PIPE_FORMAT_DXT1_SRGB
:
1185 case PIPE_FORMAT_DXT1_SRGBA
:
1187 is_srgb_valid
= TRUE
;
1189 case PIPE_FORMAT_DXT3_RGBA
:
1190 case PIPE_FORMAT_DXT3_SRGBA
:
1192 is_srgb_valid
= TRUE
;
1194 case PIPE_FORMAT_DXT5_RGBA
:
1195 case PIPE_FORMAT_DXT5_SRGBA
:
1197 is_srgb_valid
= TRUE
;
1204 if (desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
) {
1206 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
1207 case PIPE_FORMAT_G8R8_B8R8_UNORM
:
1210 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
1211 case PIPE_FORMAT_R8G8_R8B8_UNORM
:
1219 if (format
== PIPE_FORMAT_R9G9B9E5_FLOAT
) {
1220 result
= FMT_5_9_9_9_SHAREDEXP
;
1222 } else if (format
== PIPE_FORMAT_R11G11B10_FLOAT
) {
1223 result
= FMT_10_11_11_FLOAT
;
1228 for (i
= 0; i
< desc
->nr_channels
; i
++) {
1229 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
1230 word4
|= sign_bit
[i
];
1234 /* R8G8Bx_SNORM - XXX CxV8U8 */
1236 /* See whether the components are of the same size. */
1237 for (i
= 1; i
< desc
->nr_channels
; i
++) {
1238 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
1241 /* Non-uniform formats. */
1243 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
&&
1244 desc
->channel
[0].pure_integer
)
1245 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
1246 switch(desc
->nr_channels
) {
1248 if (desc
->channel
[0].size
== 5 &&
1249 desc
->channel
[1].size
== 6 &&
1250 desc
->channel
[2].size
== 5) {
1256 if (desc
->channel
[0].size
== 5 &&
1257 desc
->channel
[1].size
== 5 &&
1258 desc
->channel
[2].size
== 5 &&
1259 desc
->channel
[3].size
== 1) {
1260 result
= FMT_1_5_5_5
;
1263 if (desc
->channel
[0].size
== 10 &&
1264 desc
->channel
[1].size
== 10 &&
1265 desc
->channel
[2].size
== 10 &&
1266 desc
->channel
[3].size
== 2) {
1267 result
= FMT_2_10_10_10
;
1275 /* Find the first non-VOID channel. */
1276 for (i
= 0; i
< 4; i
++) {
1277 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
1285 /* uniform formats */
1286 switch (desc
->channel
[i
].type
) {
1287 case UTIL_FORMAT_TYPE_UNSIGNED
:
1288 case UTIL_FORMAT_TYPE_SIGNED
:
1290 if (!desc
->channel
[i
].normalized
&&
1291 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
1295 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
&&
1296 desc
->channel
[i
].pure_integer
)
1297 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
1299 switch (desc
->channel
[i
].size
) {
1301 switch (desc
->nr_channels
) {
1306 result
= FMT_4_4_4_4
;
1311 switch (desc
->nr_channels
) {
1319 result
= FMT_8_8_8_8
;
1320 is_srgb_valid
= TRUE
;
1325 switch (desc
->nr_channels
) {
1333 result
= FMT_16_16_16_16
;
1338 switch (desc
->nr_channels
) {
1346 result
= FMT_32_32_32_32
;
1352 case UTIL_FORMAT_TYPE_FLOAT
:
1353 switch (desc
->channel
[i
].size
) {
1355 switch (desc
->nr_channels
) {
1357 result
= FMT_16_FLOAT
;
1360 result
= FMT_16_16_FLOAT
;
1363 result
= FMT_16_16_16_16_FLOAT
;
1368 switch (desc
->nr_channels
) {
1370 result
= FMT_32_FLOAT
;
1373 result
= FMT_32_32_FLOAT
;
1376 result
= FMT_32_32_32_32_FLOAT
;
1385 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
&& !is_srgb_valid
)
1390 *yuv_format_p
= yuv_format
;
1393 /* R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format)); */
1397 static const struct u_resource_vtbl r600_texture_vtbl
=
1399 r600_texture_get_handle
, /* get_handle */
1400 r600_texture_destroy
, /* resource_destroy */
1401 r600_texture_transfer_map
, /* transfer_map */
1402 NULL
, /* transfer_flush_region */
1403 r600_texture_transfer_unmap
, /* transfer_unmap */
1404 NULL
/* transfer_inline_write */