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 sbox
.x
= sbox
.y
= sbox
.z
= 0;
54 sbox
.width
= transfer
->box
.width
;
55 sbox
.height
= transfer
->box
.height
;
56 /* XXX that might be wrong */
58 ctx
->resource_copy_region(ctx
, texture
, transfer
->level
,
59 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
60 &rtransfer
->staging
->b
.b
,
64 unsigned r600_texture_get_offset(struct r600_resource_texture
*rtex
,
65 unsigned level
, unsigned layer
)
67 unsigned offset
= rtex
->offset
[level
];
69 switch (rtex
->resource
.b
.b
.target
) {
71 case PIPE_TEXTURE_CUBE
:
73 return offset
+ layer
* rtex
->layer_size
[level
];
77 static unsigned r600_get_block_alignment(struct pipe_screen
*screen
,
78 enum pipe_format format
,
81 struct r600_screen
* rscreen
= (struct r600_screen
*)screen
;
82 unsigned pixsize
= util_format_get_blocksize(format
);
86 case V_038000_ARRAY_1D_TILED_THIN1
:
88 ((rscreen
->tiling_info
.group_bytes
/ 8 / pixsize
)));
90 case V_038000_ARRAY_2D_TILED_THIN1
:
91 p_align
= MAX2(rscreen
->tiling_info
.num_banks
,
92 (((rscreen
->tiling_info
.group_bytes
/ 8 / pixsize
)) *
93 rscreen
->tiling_info
.num_banks
)) * 8;
95 case V_038000_ARRAY_LINEAR_ALIGNED
:
96 p_align
= MAX2(64, rscreen
->tiling_info
.group_bytes
/ pixsize
);
98 case V_038000_ARRAY_LINEAR_GENERAL
:
100 p_align
= rscreen
->tiling_info
.group_bytes
/ pixsize
;
106 static unsigned r600_get_height_alignment(struct pipe_screen
*screen
,
109 struct r600_screen
* rscreen
= (struct r600_screen
*)screen
;
112 switch (array_mode
) {
113 case V_038000_ARRAY_2D_TILED_THIN1
:
114 h_align
= rscreen
->tiling_info
.num_channels
* 8;
116 case V_038000_ARRAY_1D_TILED_THIN1
:
117 case V_038000_ARRAY_LINEAR_ALIGNED
:
120 case V_038000_ARRAY_LINEAR_GENERAL
:
128 static unsigned r600_get_base_alignment(struct pipe_screen
*screen
,
129 enum pipe_format format
,
132 struct r600_screen
* rscreen
= (struct r600_screen
*)screen
;
133 unsigned pixsize
= util_format_get_blocksize(format
);
134 int p_align
= r600_get_block_alignment(screen
, format
, array_mode
);
135 int h_align
= r600_get_height_alignment(screen
, array_mode
);
138 switch (array_mode
) {
139 case V_038000_ARRAY_2D_TILED_THIN1
:
140 b_align
= MAX2(rscreen
->tiling_info
.num_banks
* rscreen
->tiling_info
.num_channels
* 8 * 8 * pixsize
,
141 p_align
* pixsize
* h_align
);
143 case V_038000_ARRAY_1D_TILED_THIN1
:
144 case V_038000_ARRAY_LINEAR_ALIGNED
:
145 case V_038000_ARRAY_LINEAR_GENERAL
:
147 b_align
= rscreen
->tiling_info
.group_bytes
;
153 static unsigned mip_minify(unsigned size
, unsigned level
)
156 val
= u_minify(size
, level
);
158 val
= util_next_power_of_two(val
);
162 static unsigned r600_texture_get_nblocksx(struct pipe_screen
*screen
,
163 struct r600_resource_texture
*rtex
,
166 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
;
167 unsigned nblocksx
, block_align
, width
;
168 unsigned blocksize
= util_format_get_blocksize(rtex
->real_format
);
170 if (rtex
->pitch_override
)
171 return rtex
->pitch_override
/ blocksize
;
173 width
= mip_minify(ptex
->width0
, level
);
174 nblocksx
= util_format_get_nblocksx(rtex
->real_format
, width
);
176 block_align
= r600_get_block_alignment(screen
, rtex
->real_format
,
177 rtex
->array_mode
[level
]);
178 nblocksx
= align(nblocksx
, block_align
);
182 static unsigned r600_texture_get_nblocksy(struct pipe_screen
*screen
,
183 struct r600_resource_texture
*rtex
,
186 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
;
187 unsigned height
, tile_height
;
189 height
= mip_minify(ptex
->height0
, level
);
190 height
= util_format_get_nblocksy(rtex
->real_format
, height
);
191 tile_height
= r600_get_height_alignment(screen
,
192 rtex
->array_mode
[level
]);
194 /* XXX Hack around an alignment issue. Less tests fail with this.
196 * The thing is depth-stencil buffers should be tiled, i.e.
197 * the alignment should be >=8. If I make them tiled, stencil starts
198 * working because it no longer overlaps with the depth buffer
199 * in memory, but texturing like drawpix-stencil breaks. */
200 if (util_format_is_depth_or_stencil(rtex
->real_format
) && tile_height
< 8)
203 height
= align(height
, tile_height
);
207 static void r600_texture_set_array_mode(struct pipe_screen
*screen
,
208 struct r600_resource_texture
*rtex
,
209 unsigned level
, unsigned array_mode
)
211 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
;
213 switch (array_mode
) {
214 case V_0280A0_ARRAY_LINEAR_GENERAL
:
215 case V_0280A0_ARRAY_LINEAR_ALIGNED
:
216 case V_0280A0_ARRAY_1D_TILED_THIN1
:
218 rtex
->array_mode
[level
] = array_mode
;
220 case V_0280A0_ARRAY_2D_TILED_THIN1
:
222 unsigned w
, h
, tile_height
, tile_width
;
224 tile_height
= r600_get_height_alignment(screen
, array_mode
);
225 tile_width
= r600_get_block_alignment(screen
, rtex
->real_format
, array_mode
);
227 w
= mip_minify(ptex
->width0
, level
);
228 h
= mip_minify(ptex
->height0
, level
);
229 if (w
<= tile_width
|| h
<= tile_height
)
230 rtex
->array_mode
[level
] = V_0280A0_ARRAY_1D_TILED_THIN1
;
232 rtex
->array_mode
[level
] = array_mode
;
238 static int r600_init_surface(struct radeon_surface
*surface
,
239 const struct pipe_resource
*ptex
,
242 surface
->npix_x
= ptex
->width0
;
243 surface
->npix_y
= ptex
->height0
;
244 surface
->npix_z
= ptex
->depth0
;
245 surface
->blk_w
= util_format_get_blockwidth(ptex
->format
);
246 surface
->blk_h
= util_format_get_blockheight(ptex
->format
);
248 surface
->array_size
= 1;
249 surface
->last_level
= ptex
->last_level
;
250 surface
->bpe
= util_format_get_blocksize(ptex
->format
);
251 /* align byte per element on dword */
252 if (surface
->bpe
== 3) {
255 surface
->nsamples
= 1;
257 switch (array_mode
) {
258 case V_038000_ARRAY_1D_TILED_THIN1
:
259 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_1D
, MODE
);
261 case V_038000_ARRAY_2D_TILED_THIN1
:
262 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_2D
, MODE
);
264 case V_038000_ARRAY_LINEAR_ALIGNED
:
265 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_LINEAR_ALIGNED
, MODE
);
267 case V_038000_ARRAY_LINEAR_GENERAL
:
269 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_LINEAR
, MODE
);
272 switch (ptex
->target
) {
273 case PIPE_TEXTURE_1D
:
274 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_1D
, TYPE
);
276 case PIPE_TEXTURE_RECT
:
277 case PIPE_TEXTURE_2D
:
278 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_2D
, TYPE
);
280 case PIPE_TEXTURE_3D
:
281 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_3D
, TYPE
);
283 case PIPE_TEXTURE_1D_ARRAY
:
284 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_1D_ARRAY
, TYPE
);
285 surface
->array_size
= ptex
->array_size
;
287 case PIPE_TEXTURE_2D_ARRAY
:
288 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_2D_ARRAY
, TYPE
);
289 surface
->array_size
= ptex
->array_size
;
291 case PIPE_TEXTURE_CUBE
:
292 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_CUBEMAP
, TYPE
);
298 if (ptex
->bind
& PIPE_BIND_SCANOUT
) {
299 surface
->flags
|= RADEON_SURF_SCANOUT
;
301 if (util_format_is_depth_and_stencil(ptex
->format
)) {
302 surface
->flags
|= RADEON_SURF_ZBUFFER
;
303 surface
->flags
|= RADEON_SURF_SBUFFER
;
309 static int r600_setup_surface(struct pipe_screen
*screen
,
310 struct r600_resource_texture
*rtex
,
312 unsigned pitch_in_bytes_override
)
314 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
;
315 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
319 if (util_format_is_depth_or_stencil(rtex
->real_format
)) {
320 rtex
->surface
.flags
|= RADEON_SURF_ZBUFFER
;
321 rtex
->surface
.flags
|= RADEON_SURF_SBUFFER
;
324 r
= rscreen
->ws
->surface_init(rscreen
->ws
, &rtex
->surface
);
328 rtex
->size
= rtex
->surface
.bo_size
;
329 if (pitch_in_bytes_override
&& pitch_in_bytes_override
!= rtex
->surface
.level
[0].pitch_bytes
) {
330 /* old ddx on evergreen over estimate alignment for 1d, only 1 level
333 rtex
->surface
.level
[0].nblk_x
= pitch_in_bytes_override
/ rtex
->surface
.bpe
;
334 rtex
->surface
.level
[0].pitch_bytes
= pitch_in_bytes_override
;
335 rtex
->surface
.level
[0].slice_size
= pitch_in_bytes_override
* rtex
->surface
.level
[0].nblk_y
;
336 if (rtex
->surface
.flags
& RADEON_SURF_SBUFFER
) {
337 rtex
->surface
.stencil_offset
= rtex
->surface
.level
[0].slice_size
;
340 for (i
= 0; i
<= ptex
->last_level
; i
++) {
341 rtex
->offset
[i
] = rtex
->surface
.level
[i
].offset
;
342 rtex
->layer_size
[i
] = rtex
->surface
.level
[i
].slice_size
;
343 rtex
->pitch_in_bytes
[i
] = rtex
->surface
.level
[i
].pitch_bytes
;
344 switch (rtex
->surface
.level
[i
].mode
) {
345 case RADEON_SURF_MODE_LINEAR_ALIGNED
:
346 rtex
->array_mode
[i
] = V_038000_ARRAY_LINEAR_ALIGNED
;
348 case RADEON_SURF_MODE_1D
:
349 rtex
->array_mode
[i
] = V_038000_ARRAY_1D_TILED_THIN1
;
351 case RADEON_SURF_MODE_2D
:
352 rtex
->array_mode
[i
] = V_038000_ARRAY_2D_TILED_THIN1
;
355 case RADEON_SURF_MODE_LINEAR
:
356 rtex
->array_mode
[i
] = 0;
363 static void r600_setup_miptree(struct pipe_screen
*screen
,
364 struct r600_resource_texture
*rtex
,
367 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
;
368 enum chip_class chipc
= ((struct r600_screen
*)screen
)->chip_class
;
369 unsigned size
, layer_size
, i
, offset
;
370 unsigned nblocksx
, nblocksy
;
372 for (i
= 0, offset
= 0; i
<= ptex
->last_level
; i
++) {
373 unsigned blocksize
= util_format_get_blocksize(rtex
->real_format
);
374 unsigned base_align
= r600_get_base_alignment(screen
, rtex
->real_format
, array_mode
);
376 r600_texture_set_array_mode(screen
, rtex
, i
, array_mode
);
378 nblocksx
= r600_texture_get_nblocksx(screen
, rtex
, i
);
379 nblocksy
= r600_texture_get_nblocksy(screen
, rtex
, i
);
381 if (chipc
>= EVERGREEN
&& array_mode
== V_038000_ARRAY_LINEAR_GENERAL
)
382 layer_size
= align(nblocksx
, 64) * nblocksy
* blocksize
;
384 layer_size
= nblocksx
* nblocksy
* blocksize
;
386 if (ptex
->target
== PIPE_TEXTURE_CUBE
) {
388 size
= layer_size
* 8;
390 size
= layer_size
* 6;
392 else if (ptex
->target
== PIPE_TEXTURE_3D
)
393 size
= layer_size
* u_minify(ptex
->depth0
, i
);
395 size
= layer_size
* ptex
->array_size
;
397 /* align base image and start of miptree */
398 if ((i
== 0) || (i
== 1))
399 offset
= align(offset
, base_align
);
400 rtex
->offset
[i
] = offset
;
401 rtex
->layer_size
[i
] = layer_size
;
402 rtex
->pitch_in_blocks
[i
] = nblocksx
; /* CB talks in elements */
403 rtex
->pitch_in_bytes
[i
] = nblocksx
* blocksize
;
410 /* Figure out whether u_blitter will fallback to a transfer operation.
411 * If so, don't use a staging resource.
413 static boolean
permit_hardware_blit(struct pipe_screen
*screen
,
414 const struct pipe_resource
*res
)
418 if (util_format_is_depth_or_stencil(res
->format
))
419 bind
= PIPE_BIND_DEPTH_STENCIL
;
421 bind
= PIPE_BIND_RENDER_TARGET
;
423 /* hackaround for S3TC */
424 if (util_format_is_compressed(res
->format
))
427 if (!screen
->is_format_supported(screen
,
434 if (!screen
->is_format_supported(screen
,
438 PIPE_BIND_SAMPLER_VIEW
))
444 static boolean
r600_texture_get_handle(struct pipe_screen
* screen
,
445 struct pipe_resource
*ptex
,
446 struct winsys_handle
*whandle
)
448 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)ptex
;
449 struct r600_resource
*resource
= &rtex
->resource
;
450 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
452 return rscreen
->ws
->buffer_get_handle(resource
->buf
,
453 rtex
->pitch_in_bytes
[0], whandle
);
456 static void r600_texture_destroy(struct pipe_screen
*screen
,
457 struct pipe_resource
*ptex
)
459 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)ptex
;
460 struct r600_resource
*resource
= &rtex
->resource
;
462 if (rtex
->flushed_depth_texture
)
463 pipe_resource_reference((struct pipe_resource
**)&rtex
->flushed_depth_texture
, NULL
);
466 pipe_resource_reference((struct pipe_resource
**)&rtex
->stencil
, NULL
);
468 pb_reference(&resource
->buf
, NULL
);
472 static const struct u_resource_vtbl r600_texture_vtbl
=
474 r600_texture_get_handle
, /* get_handle */
475 r600_texture_destroy
, /* resource_destroy */
476 r600_texture_get_transfer
, /* get_transfer */
477 r600_texture_transfer_destroy
, /* transfer_destroy */
478 r600_texture_transfer_map
, /* transfer_map */
479 NULL
, /* transfer_flush_region */
480 r600_texture_transfer_unmap
, /* transfer_unmap */
481 NULL
/* transfer_inline_write */
484 static struct r600_resource_texture
*
485 r600_texture_create_object(struct pipe_screen
*screen
,
486 const struct pipe_resource
*base
,
488 unsigned pitch_in_bytes_override
,
489 unsigned max_buffer_size
,
490 struct pb_buffer
*buf
,
492 struct radeon_surface
*surface
)
494 struct r600_resource_texture
*rtex
;
495 struct r600_resource
*resource
;
496 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
499 rtex
= CALLOC_STRUCT(r600_resource_texture
);
503 resource
= &rtex
->resource
;
504 resource
->b
.b
= *base
;
505 resource
->b
.vtbl
= &r600_texture_vtbl
;
506 pipe_reference_init(&resource
->b
.b
.reference
, 1);
507 resource
->b
.b
.screen
= screen
;
508 rtex
->pitch_override
= pitch_in_bytes_override
;
509 rtex
->real_format
= base
->format
;
511 /* We must split depth and stencil into two separate buffers on Evergreen. */
512 if (!(base
->flags
& R600_RESOURCE_FLAG_TRANSFER
) &&
513 ((struct r600_screen
*)screen
)->chip_class
>= EVERGREEN
&&
514 util_format_is_depth_and_stencil(base
->format
) &&
515 !rscreen
->use_surface_alloc
) {
516 struct pipe_resource stencil
;
517 unsigned stencil_pitch_override
= 0;
519 switch (base
->format
) {
520 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
521 rtex
->real_format
= PIPE_FORMAT_Z24X8_UNORM
;
523 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
524 rtex
->real_format
= PIPE_FORMAT_X8Z24_UNORM
;
526 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
527 rtex
->real_format
= PIPE_FORMAT_Z32_FLOAT
;
535 /* Divide the pitch in bytes by 4 for stencil, because it has a smaller pixel size. */
536 if (pitch_in_bytes_override
) {
537 assert(base
->format
== PIPE_FORMAT_Z24_UNORM_S8_UINT
||
538 base
->format
== PIPE_FORMAT_S8_UINT_Z24_UNORM
);
539 stencil_pitch_override
= pitch_in_bytes_override
/ 4;
542 /* Allocate the stencil buffer. */
544 stencil
.format
= PIPE_FORMAT_S8_UINT
;
545 rtex
->stencil
= r600_texture_create_object(screen
, &stencil
, array_mode
,
546 stencil_pitch_override
,
547 max_buffer_size
, NULL
, FALSE
, surface
);
548 if (!rtex
->stencil
) {
552 /* Proceed in creating the depth buffer. */
555 /* only mark depth textures the HW can hit as depth textures */
556 if (util_format_is_depth_or_stencil(rtex
->real_format
) && permit_hardware_blit(screen
, base
))
557 rtex
->is_depth
= true;
559 r600_setup_miptree(screen
, rtex
, array_mode
);
560 if (rscreen
->use_surface_alloc
) {
561 rtex
->surface
= *surface
;
562 r
= r600_setup_surface(screen
, rtex
, array_mode
, pitch_in_bytes_override
);
569 /* If we initialized separate stencil for Evergreen. place it after depth. */
571 unsigned stencil_align
, stencil_offset
;
573 stencil_align
= r600_get_base_alignment(screen
, rtex
->stencil
->real_format
, array_mode
);
574 stencil_offset
= align(rtex
->size
, stencil_align
);
576 for (unsigned i
= 0; i
<= rtex
->stencil
->resource
.b
.b
.last_level
; i
++)
577 rtex
->stencil
->offset
[i
] += stencil_offset
;
579 rtex
->size
= stencil_offset
+ rtex
->stencil
->size
;
582 /* Now create the backing buffer. */
583 if (!buf
&& alloc_bo
) {
584 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
;
585 unsigned base_align
= r600_get_base_alignment(screen
, ptex
->format
, array_mode
);
587 if (rscreen
->use_surface_alloc
) {
588 base_align
= rtex
->surface
.bo_alignment
;
589 } else if (util_format_is_depth_or_stencil(rtex
->real_format
)) {
590 /* ugly work around depth buffer need stencil room at end of bo */
591 rtex
->size
+= ptex
->width0
* ptex
->height0
;
593 if (!r600_init_resource(rscreen
, resource
, rtex
->size
, base_align
, base
->bind
, base
->usage
)) {
594 pipe_resource_reference((struct pipe_resource
**)&rtex
->stencil
, NULL
);
600 resource
->cs_buf
= rscreen
->ws
->buffer_get_cs_handle(buf
);
601 resource
->domains
= RADEON_DOMAIN_GTT
| RADEON_DOMAIN_VRAM
;
605 pb_reference(&rtex
->stencil
->resource
.buf
, rtex
->resource
.buf
);
606 rtex
->stencil
->resource
.cs_buf
= rtex
->resource
.cs_buf
;
607 rtex
->stencil
->resource
.domains
= rtex
->resource
.domains
;
612 struct pipe_resource
*r600_texture_create(struct pipe_screen
*screen
,
613 const struct pipe_resource
*templ
)
615 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
616 struct radeon_surface surface
;
617 unsigned array_mode
= 0;
620 if (!(templ
->flags
& R600_RESOURCE_FLAG_TRANSFER
)) {
621 if (rscreen
->use_surface_alloc
&&
622 !(templ
->bind
& PIPE_BIND_SCANOUT
) &&
623 templ
->usage
!= PIPE_USAGE_STAGING
&&
624 templ
->usage
!= PIPE_USAGE_STREAM
&&
625 permit_hardware_blit(screen
, templ
)) {
626 array_mode
= V_038000_ARRAY_2D_TILED_THIN1
;
627 } else if (util_format_is_compressed(templ
->format
)) {
628 array_mode
= V_038000_ARRAY_1D_TILED_THIN1
;
632 r
= r600_init_surface(&surface
, templ
, array_mode
);
636 r
= rscreen
->ws
->surface_best(rscreen
->ws
, &surface
);
640 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
, array_mode
,
641 0, 0, NULL
, TRUE
, &surface
);
644 static struct pipe_surface
*r600_create_surface(struct pipe_context
*pipe
,
645 struct pipe_resource
*texture
,
646 const struct pipe_surface
*surf_tmpl
)
648 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
649 struct r600_surface
*surface
= CALLOC_STRUCT(r600_surface
);
650 unsigned level
= surf_tmpl
->u
.tex
.level
;
652 assert(surf_tmpl
->u
.tex
.first_layer
== surf_tmpl
->u
.tex
.last_layer
);
655 pipe_reference_init(&surface
->base
.reference
, 1);
656 pipe_resource_reference(&surface
->base
.texture
, texture
);
657 surface
->base
.context
= pipe
;
658 surface
->base
.format
= surf_tmpl
->format
;
659 surface
->base
.width
= mip_minify(texture
->width0
, level
);
660 surface
->base
.height
= mip_minify(texture
->height0
, level
);
661 surface
->base
.usage
= surf_tmpl
->usage
;
662 surface
->base
.texture
= texture
;
663 surface
->base
.u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
664 surface
->base
.u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
665 surface
->base
.u
.tex
.level
= level
;
667 surface
->aligned_height
= r600_texture_get_nblocksy(pipe
->screen
,
669 return &surface
->base
;
672 static void r600_surface_destroy(struct pipe_context
*pipe
,
673 struct pipe_surface
*surface
)
675 pipe_resource_reference(&surface
->texture
, NULL
);
679 struct pipe_resource
*r600_texture_from_handle(struct pipe_screen
*screen
,
680 const struct pipe_resource
*templ
,
681 struct winsys_handle
*whandle
)
683 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
684 struct pb_buffer
*buf
= NULL
;
686 unsigned array_mode
= 0;
687 enum radeon_bo_layout micro
, macro
;
688 struct radeon_surface surface
;
691 /* Support only 2D textures without mipmaps */
692 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
) ||
693 templ
->depth0
!= 1 || templ
->last_level
!= 0)
696 buf
= rscreen
->ws
->buffer_from_handle(rscreen
->ws
, whandle
, &stride
);
700 rscreen
->ws
->buffer_get_tiling(buf
, µ
, ¯o
,
701 &surface
.bankw
, &surface
.bankh
,
703 &surface
.stencil_tile_split
,
706 if (macro
== RADEON_LAYOUT_TILED
)
707 array_mode
= V_0280A0_ARRAY_2D_TILED_THIN1
;
708 else if (micro
== RADEON_LAYOUT_TILED
)
709 array_mode
= V_0280A0_ARRAY_1D_TILED_THIN1
;
713 r
= r600_init_surface(&surface
, templ
, array_mode
);
717 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
, array_mode
,
718 stride
, 0, buf
, FALSE
, &surface
);
721 int r600_texture_depth_flush(struct pipe_context
*ctx
,
722 struct pipe_resource
*texture
, boolean just_create
)
724 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
725 struct pipe_resource resource
;
727 if (rtex
->flushed_depth_texture
)
730 resource
.target
= texture
->target
;
731 resource
.format
= texture
->format
;
732 resource
.width0
= texture
->width0
;
733 resource
.height0
= texture
->height0
;
734 resource
.depth0
= texture
->depth0
;
735 resource
.array_size
= texture
->array_size
;
736 resource
.last_level
= texture
->last_level
;
737 resource
.nr_samples
= texture
->nr_samples
;
738 resource
.usage
= PIPE_USAGE_DYNAMIC
;
739 resource
.bind
= texture
->bind
| PIPE_BIND_DEPTH_STENCIL
;
740 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
| texture
->flags
;
742 rtex
->flushed_depth_texture
= (struct r600_resource_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
743 if (rtex
->flushed_depth_texture
== NULL
) {
744 R600_ERR("failed to create temporary texture to hold untiled copy\n");
748 ((struct r600_resource_texture
*)rtex
->flushed_depth_texture
)->is_flushing_texture
= TRUE
;
753 /* XXX: only do this if the depth texture has actually changed:
755 r600_blit_uncompress_depth(ctx
, rtex
);
759 /* Needs adjustment for pixelformat:
761 static INLINE
unsigned u_box_volume( const struct pipe_box
*box
)
763 return box
->width
* box
->depth
* box
->height
;
766 struct pipe_transfer
* r600_texture_get_transfer(struct pipe_context
*ctx
,
767 struct pipe_resource
*texture
,
770 const struct pipe_box
*box
)
772 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
773 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
774 struct pipe_resource resource
;
775 struct r600_transfer
*trans
;
777 boolean use_staging_texture
= FALSE
;
779 /* We cannot map a tiled texture directly because the data is
780 * in a different order, therefore we do detiling using a blit.
782 * Also, use a temporary in GTT memory for read transfers, as
783 * the CPU is much happier reading out of cached system memory
784 * than uncached VRAM.
786 if (R600_TEX_IS_TILED(rtex
, level
))
787 use_staging_texture
= TRUE
;
789 if ((usage
& PIPE_TRANSFER_READ
) && u_box_volume(box
) > 1024)
790 use_staging_texture
= TRUE
;
792 /* Use a staging texture for uploads if the underlying BO is busy. */
793 if (!(usage
& PIPE_TRANSFER_READ
) &&
794 (rctx
->ws
->cs_is_buffer_referenced(rctx
->cs
, rtex
->resource
.cs_buf
, RADEON_USAGE_READWRITE
) ||
795 rctx
->ws
->buffer_is_busy(rtex
->resource
.buf
, RADEON_USAGE_READWRITE
)))
796 use_staging_texture
= TRUE
;
798 if (!permit_hardware_blit(ctx
->screen
, texture
) ||
799 (texture
->flags
& R600_RESOURCE_FLAG_TRANSFER
))
800 use_staging_texture
= FALSE
;
802 if (use_staging_texture
&& (usage
& PIPE_TRANSFER_MAP_DIRECTLY
))
805 trans
= CALLOC_STRUCT(r600_transfer
);
808 pipe_resource_reference(&trans
->transfer
.resource
, texture
);
809 trans
->transfer
.level
= level
;
810 trans
->transfer
.usage
= usage
;
811 trans
->transfer
.box
= *box
;
812 if (rtex
->is_depth
) {
813 /* XXX: only readback the rectangle which is being mapped?
815 /* XXX: when discard is true, no need to read back from depth texture
817 r
= r600_texture_depth_flush(ctx
, texture
, FALSE
);
819 R600_ERR("failed to create temporary texture to hold untiled copy\n");
820 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
824 trans
->transfer
.stride
= rtex
->flushed_depth_texture
->pitch_in_bytes
[level
];
825 trans
->offset
= r600_texture_get_offset(rtex
->flushed_depth_texture
, level
, box
->z
);
826 return &trans
->transfer
;
827 } else if (use_staging_texture
) {
828 resource
.target
= PIPE_TEXTURE_2D
;
829 resource
.format
= texture
->format
;
830 resource
.width0
= box
->width
;
831 resource
.height0
= box
->height
;
833 resource
.array_size
= 1;
834 resource
.last_level
= 0;
835 resource
.nr_samples
= 0;
836 resource
.usage
= PIPE_USAGE_STAGING
;
838 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
;
839 /* For texture reading, the temporary (detiled) texture is used as
840 * a render target when blitting from a tiled texture. */
841 if (usage
& PIPE_TRANSFER_READ
) {
842 resource
.bind
|= PIPE_BIND_RENDER_TARGET
;
844 /* For texture writing, the temporary texture is used as a sampler
845 * when blitting into a tiled texture. */
846 if (usage
& PIPE_TRANSFER_WRITE
) {
847 resource
.bind
|= PIPE_BIND_SAMPLER_VIEW
;
849 /* Create the temporary texture. */
850 trans
->staging
= (struct r600_resource
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
851 if (trans
->staging
== NULL
) {
852 R600_ERR("failed to create temporary texture to hold untiled copy\n");
853 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
858 trans
->transfer
.stride
=
859 ((struct r600_resource_texture
*)trans
->staging
)->pitch_in_bytes
[0];
860 if (usage
& PIPE_TRANSFER_READ
) {
861 r600_copy_to_staging_texture(ctx
, trans
);
862 /* Always referenced in the blit. */
863 r600_flush(ctx
, NULL
, 0);
865 return &trans
->transfer
;
867 trans
->transfer
.stride
= rtex
->pitch_in_bytes
[level
];
868 trans
->transfer
.layer_stride
= rtex
->layer_size
[level
];
869 trans
->offset
= r600_texture_get_offset(rtex
, level
, box
->z
);
870 return &trans
->transfer
;
873 void r600_texture_transfer_destroy(struct pipe_context
*ctx
,
874 struct pipe_transfer
*transfer
)
876 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
877 struct pipe_resource
*texture
= transfer
->resource
;
878 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
880 if (rtransfer
->staging
) {
881 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
882 r600_copy_from_staging_texture(ctx
, rtransfer
);
884 pipe_resource_reference((struct pipe_resource
**)&rtransfer
->staging
, NULL
);
887 if (rtex
->is_depth
&& !rtex
->is_flushing_texture
) {
888 if ((transfer
->usage
& PIPE_TRANSFER_WRITE
) && rtex
->flushed_depth_texture
)
889 r600_blit_push_depth(ctx
, rtex
);
892 pipe_resource_reference(&transfer
->resource
, NULL
);
896 void* r600_texture_transfer_map(struct pipe_context
*ctx
,
897 struct pipe_transfer
* transfer
)
899 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
900 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
901 struct pb_buffer
*buf
;
902 enum pipe_format format
= transfer
->resource
->format
;
906 if (rtransfer
->staging
) {
907 buf
= ((struct r600_resource
*)rtransfer
->staging
)->buf
;
909 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
911 if (rtex
->flushed_depth_texture
)
912 buf
= ((struct r600_resource
*)rtex
->flushed_depth_texture
)->buf
;
914 buf
= ((struct r600_resource
*)transfer
->resource
)->buf
;
916 offset
= rtransfer
->offset
+
917 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
918 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
921 if (!(map
= rctx
->ws
->buffer_map(buf
, rctx
->cs
, transfer
->usage
))) {
928 void r600_texture_transfer_unmap(struct pipe_context
*ctx
,
929 struct pipe_transfer
* transfer
)
931 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
932 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
933 struct pb_buffer
*buf
;
935 if (rtransfer
->staging
) {
936 buf
= ((struct r600_resource
*)rtransfer
->staging
)->buf
;
938 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
940 if (rtex
->flushed_depth_texture
) {
941 buf
= ((struct r600_resource
*)rtex
->flushed_depth_texture
)->buf
;
943 buf
= ((struct r600_resource
*)transfer
->resource
)->buf
;
946 rctx
->ws
->buffer_unmap(buf
);
949 void r600_init_surface_functions(struct r600_context
*r600
)
951 r600
->context
.create_surface
= r600_create_surface
;
952 r600
->context
.surface_destroy
= r600_surface_destroy
;
955 static unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format
,
956 const unsigned char *swizzle_view
)
959 unsigned char swizzle
[4];
961 const uint32_t swizzle_shift
[4] = {
964 const uint32_t swizzle_bit
[4] = {
969 util_format_compose_swizzles(swizzle_format
, swizzle_view
, swizzle
);
971 memcpy(swizzle
, swizzle_format
, 4);
975 for (i
= 0; i
< 4; i
++) {
976 switch (swizzle
[i
]) {
977 case UTIL_FORMAT_SWIZZLE_Y
:
978 result
|= swizzle_bit
[1] << swizzle_shift
[i
];
980 case UTIL_FORMAT_SWIZZLE_Z
:
981 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
983 case UTIL_FORMAT_SWIZZLE_W
:
984 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
986 case UTIL_FORMAT_SWIZZLE_0
:
987 result
|= V_038010_SQ_SEL_0
<< swizzle_shift
[i
];
989 case UTIL_FORMAT_SWIZZLE_1
:
990 result
|= V_038010_SQ_SEL_1
<< swizzle_shift
[i
];
992 default: /* UTIL_FORMAT_SWIZZLE_X */
993 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
999 /* texture format translate */
1000 uint32_t r600_translate_texformat(struct pipe_screen
*screen
,
1001 enum pipe_format format
,
1002 const unsigned char *swizzle_view
,
1003 uint32_t *word4_p
, uint32_t *yuv_format_p
)
1005 uint32_t result
= 0, word4
= 0, yuv_format
= 0;
1006 const struct util_format_description
*desc
;
1007 boolean uniform
= TRUE
;
1008 static int r600_enable_s3tc
= -1;
1009 bool is_srgb_valid
= FALSE
;
1012 const uint32_t sign_bit
[4] = {
1013 S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED
),
1014 S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED
),
1015 S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED
),
1016 S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED
)
1018 desc
= util_format_description(format
);
1020 word4
|= r600_get_swizzle_combined(desc
->swizzle
, swizzle_view
);
1022 /* Colorspace (return non-RGB formats directly). */
1023 switch (desc
->colorspace
) {
1024 /* Depth stencil formats */
1025 case UTIL_FORMAT_COLORSPACE_ZS
:
1027 case PIPE_FORMAT_Z16_UNORM
:
1030 case PIPE_FORMAT_X24S8_UINT
:
1031 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
1032 case PIPE_FORMAT_Z24X8_UNORM
:
1033 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
1036 case PIPE_FORMAT_S8X24_UINT
:
1037 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
1038 case PIPE_FORMAT_X8Z24_UNORM
:
1039 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
1042 case PIPE_FORMAT_S8_UINT
:
1044 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
1046 case PIPE_FORMAT_Z32_FLOAT
:
1047 result
= FMT_32_FLOAT
;
1049 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
1050 result
= FMT_X24_8_32_FLOAT
;
1056 case UTIL_FORMAT_COLORSPACE_YUV
:
1057 yuv_format
|= (1 << 30);
1059 case PIPE_FORMAT_UYVY
:
1060 case PIPE_FORMAT_YUYV
:
1064 goto out_unknown
; /* XXX */
1066 case UTIL_FORMAT_COLORSPACE_SRGB
:
1067 word4
|= S_038010_FORCE_DEGAMMA(1);
1074 if (r600_enable_s3tc
== -1) {
1075 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
1076 if (rscreen
->info
.drm_minor
>= 9)
1077 r600_enable_s3tc
= 1;
1079 r600_enable_s3tc
= debug_get_bool_option("R600_ENABLE_S3TC", FALSE
);
1082 if (desc
->layout
== UTIL_FORMAT_LAYOUT_RGTC
) {
1083 if (!r600_enable_s3tc
)
1087 case PIPE_FORMAT_RGTC1_SNORM
:
1088 case PIPE_FORMAT_LATC1_SNORM
:
1089 word4
|= sign_bit
[0];
1090 case PIPE_FORMAT_RGTC1_UNORM
:
1091 case PIPE_FORMAT_LATC1_UNORM
:
1094 case PIPE_FORMAT_RGTC2_SNORM
:
1095 case PIPE_FORMAT_LATC2_SNORM
:
1096 word4
|= sign_bit
[0] | sign_bit
[1];
1097 case PIPE_FORMAT_RGTC2_UNORM
:
1098 case PIPE_FORMAT_LATC2_UNORM
:
1106 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
1108 if (!r600_enable_s3tc
)
1111 if (!util_format_s3tc_enabled
) {
1116 case PIPE_FORMAT_DXT1_RGB
:
1117 case PIPE_FORMAT_DXT1_RGBA
:
1118 case PIPE_FORMAT_DXT1_SRGB
:
1119 case PIPE_FORMAT_DXT1_SRGBA
:
1121 is_srgb_valid
= TRUE
;
1123 case PIPE_FORMAT_DXT3_RGBA
:
1124 case PIPE_FORMAT_DXT3_SRGBA
:
1126 is_srgb_valid
= TRUE
;
1128 case PIPE_FORMAT_DXT5_RGBA
:
1129 case PIPE_FORMAT_DXT5_SRGBA
:
1131 is_srgb_valid
= TRUE
;
1138 if (desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
) {
1140 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
1141 case PIPE_FORMAT_G8R8_B8R8_UNORM
:
1144 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
1145 case PIPE_FORMAT_R8G8_R8B8_UNORM
:
1153 if (format
== PIPE_FORMAT_R9G9B9E5_FLOAT
) {
1154 result
= FMT_5_9_9_9_SHAREDEXP
;
1156 } else if (format
== PIPE_FORMAT_R11G11B10_FLOAT
) {
1157 result
= FMT_10_11_11_FLOAT
;
1162 for (i
= 0; i
< desc
->nr_channels
; i
++) {
1163 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
1164 word4
|= sign_bit
[i
];
1168 /* R8G8Bx_SNORM - XXX CxV8U8 */
1170 /* See whether the components are of the same size. */
1171 for (i
= 1; i
< desc
->nr_channels
; i
++) {
1172 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
1175 /* Non-uniform formats. */
1177 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
&&
1178 desc
->channel
[0].pure_integer
)
1179 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
1180 switch(desc
->nr_channels
) {
1182 if (desc
->channel
[0].size
== 5 &&
1183 desc
->channel
[1].size
== 6 &&
1184 desc
->channel
[2].size
== 5) {
1190 if (desc
->channel
[0].size
== 5 &&
1191 desc
->channel
[1].size
== 5 &&
1192 desc
->channel
[2].size
== 5 &&
1193 desc
->channel
[3].size
== 1) {
1194 result
= FMT_1_5_5_5
;
1197 if (desc
->channel
[0].size
== 10 &&
1198 desc
->channel
[1].size
== 10 &&
1199 desc
->channel
[2].size
== 10 &&
1200 desc
->channel
[3].size
== 2) {
1201 result
= FMT_2_10_10_10
;
1209 /* Find the first non-VOID channel. */
1210 for (i
= 0; i
< 4; i
++) {
1211 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
1219 /* uniform formats */
1220 switch (desc
->channel
[i
].type
) {
1221 case UTIL_FORMAT_TYPE_UNSIGNED
:
1222 case UTIL_FORMAT_TYPE_SIGNED
:
1224 if (!desc
->channel
[i
].normalized
&&
1225 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
1229 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
&&
1230 desc
->channel
[i
].pure_integer
)
1231 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
1233 switch (desc
->channel
[i
].size
) {
1235 switch (desc
->nr_channels
) {
1240 result
= FMT_4_4_4_4
;
1245 switch (desc
->nr_channels
) {
1253 result
= FMT_8_8_8_8
;
1254 is_srgb_valid
= TRUE
;
1259 switch (desc
->nr_channels
) {
1267 result
= FMT_16_16_16_16
;
1272 switch (desc
->nr_channels
) {
1280 result
= FMT_32_32_32_32
;
1286 case UTIL_FORMAT_TYPE_FLOAT
:
1287 switch (desc
->channel
[i
].size
) {
1289 switch (desc
->nr_channels
) {
1291 result
= FMT_16_FLOAT
;
1294 result
= FMT_16_16_FLOAT
;
1297 result
= FMT_16_16_16_16_FLOAT
;
1302 switch (desc
->nr_channels
) {
1304 result
= FMT_32_FLOAT
;
1307 result
= FMT_32_32_FLOAT
;
1310 result
= FMT_32_32_32_32_FLOAT
;
1319 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
&& !is_srgb_valid
)
1324 *yuv_format_p
= yuv_format
;
1327 /* R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format)); */