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.
28 #include "pipe/p_screen.h"
29 #include "util/u_format.h"
30 #include "util/u_format_s3tc.h"
31 #include "util/u_math.h"
32 #include "util/u_inlines.h"
33 #include "util/u_memory.h"
34 #include "pipebuffer/pb_buffer.h"
35 #include "r600_pipe.h"
36 #include "r600_resource.h"
38 #include "r600_formats.h"
40 /* Copy from a full GPU texture to a transfer's staging one. */
41 static void r600_copy_to_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
43 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
44 struct pipe_resource
*texture
= transfer
->resource
;
46 ctx
->resource_copy_region(ctx
, rtransfer
->staging_texture
,
47 0, 0, 0, 0, texture
, transfer
->level
,
52 /* Copy from a transfer's staging texture to a full GPU one. */
53 static void r600_copy_from_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
55 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
56 struct pipe_resource
*texture
= transfer
->resource
;
59 sbox
.x
= sbox
.y
= sbox
.z
= 0;
60 sbox
.width
= transfer
->box
.width
;
61 sbox
.height
= transfer
->box
.height
;
62 /* XXX that might be wrong */
64 ctx
->resource_copy_region(ctx
, texture
, transfer
->level
,
65 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
66 rtransfer
->staging_texture
,
69 r600_flush(ctx
, NULL
, RADEON_FLUSH_ASYNC
);
72 unsigned r600_texture_get_offset(struct r600_resource_texture
*rtex
,
73 unsigned level
, unsigned layer
)
75 unsigned offset
= rtex
->offset
[level
];
77 switch (rtex
->resource
.b
.b
.b
.target
) {
79 case PIPE_TEXTURE_CUBE
:
81 return offset
+ layer
* rtex
->layer_size
[level
];
85 static unsigned r600_get_block_alignment(struct pipe_screen
*screen
,
86 enum pipe_format format
,
89 struct r600_screen
* rscreen
= (struct r600_screen
*)screen
;
90 unsigned pixsize
= util_format_get_blocksize(format
);
94 case V_038000_ARRAY_1D_TILED_THIN1
:
96 ((rscreen
->tiling_info
.group_bytes
/ 8 / pixsize
)));
98 case V_038000_ARRAY_2D_TILED_THIN1
:
99 p_align
= MAX2(rscreen
->tiling_info
.num_banks
,
100 (((rscreen
->tiling_info
.group_bytes
/ 8 / pixsize
)) *
101 rscreen
->tiling_info
.num_banks
)) * 8;
103 case V_038000_ARRAY_LINEAR_ALIGNED
:
104 p_align
= MAX2(64, rscreen
->tiling_info
.group_bytes
/ pixsize
);
106 case V_038000_ARRAY_LINEAR_GENERAL
:
108 p_align
= rscreen
->tiling_info
.group_bytes
/ pixsize
;
114 static unsigned r600_get_height_alignment(struct pipe_screen
*screen
,
117 struct r600_screen
* rscreen
= (struct r600_screen
*)screen
;
120 switch (array_mode
) {
121 case V_038000_ARRAY_2D_TILED_THIN1
:
122 h_align
= rscreen
->tiling_info
.num_channels
* 8;
124 case V_038000_ARRAY_1D_TILED_THIN1
:
125 case V_038000_ARRAY_LINEAR_ALIGNED
:
128 case V_038000_ARRAY_LINEAR_GENERAL
:
136 static unsigned r600_get_base_alignment(struct pipe_screen
*screen
,
137 enum pipe_format format
,
140 struct r600_screen
* rscreen
= (struct r600_screen
*)screen
;
141 unsigned pixsize
= util_format_get_blocksize(format
);
142 int p_align
= r600_get_block_alignment(screen
, format
, array_mode
);
143 int h_align
= r600_get_height_alignment(screen
, array_mode
);
146 switch (array_mode
) {
147 case V_038000_ARRAY_2D_TILED_THIN1
:
148 b_align
= MAX2(rscreen
->tiling_info
.num_banks
* rscreen
->tiling_info
.num_channels
* 8 * 8 * pixsize
,
149 p_align
* pixsize
* h_align
);
151 case V_038000_ARRAY_1D_TILED_THIN1
:
152 case V_038000_ARRAY_LINEAR_ALIGNED
:
153 case V_038000_ARRAY_LINEAR_GENERAL
:
155 b_align
= rscreen
->tiling_info
.group_bytes
;
161 static unsigned mip_minify(unsigned size
, unsigned level
)
164 val
= u_minify(size
, level
);
166 val
= util_next_power_of_two(val
);
170 static unsigned r600_texture_get_nblocksx(struct pipe_screen
*screen
,
171 struct r600_resource_texture
*rtex
,
174 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
175 unsigned nblocksx
, block_align
, width
;
176 unsigned blocksize
= util_format_get_blocksize(rtex
->real_format
);
178 if (rtex
->pitch_override
)
179 return rtex
->pitch_override
/ blocksize
;
181 width
= mip_minify(ptex
->width0
, level
);
182 nblocksx
= util_format_get_nblocksx(rtex
->real_format
, width
);
184 block_align
= r600_get_block_alignment(screen
, rtex
->real_format
,
185 rtex
->array_mode
[level
]);
186 nblocksx
= align(nblocksx
, block_align
);
190 static unsigned r600_texture_get_nblocksy(struct pipe_screen
*screen
,
191 struct r600_resource_texture
*rtex
,
194 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
195 unsigned height
, tile_height
;
197 height
= mip_minify(ptex
->height0
, level
);
198 height
= util_format_get_nblocksy(rtex
->real_format
, height
);
199 tile_height
= r600_get_height_alignment(screen
,
200 rtex
->array_mode
[level
]);
202 /* XXX Hack around an alignment issue. Less tests fail with this.
204 * The thing is depth-stencil buffers should be tiled, i.e.
205 * the alignment should be >=8. If I make them tiled, stencil starts
206 * working because it no longer overlaps with the depth buffer
207 * in memory, but texturing like drawpix-stencil breaks. */
208 if (util_format_is_depth_or_stencil(rtex
->real_format
) && tile_height
< 8)
211 height
= align(height
, tile_height
);
215 static void r600_texture_set_array_mode(struct pipe_screen
*screen
,
216 struct r600_resource_texture
*rtex
,
217 unsigned level
, unsigned array_mode
)
219 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
221 switch (array_mode
) {
222 case V_0280A0_ARRAY_LINEAR_GENERAL
:
223 case V_0280A0_ARRAY_LINEAR_ALIGNED
:
224 case V_0280A0_ARRAY_1D_TILED_THIN1
:
226 rtex
->array_mode
[level
] = array_mode
;
228 case V_0280A0_ARRAY_2D_TILED_THIN1
:
230 unsigned w
, h
, tile_height
, tile_width
;
232 tile_height
= r600_get_height_alignment(screen
, array_mode
);
233 tile_width
= r600_get_block_alignment(screen
, rtex
->real_format
, array_mode
);
235 w
= mip_minify(ptex
->width0
, level
);
236 h
= mip_minify(ptex
->height0
, level
);
237 if (w
<= tile_width
|| h
<= tile_height
)
238 rtex
->array_mode
[level
] = V_0280A0_ARRAY_1D_TILED_THIN1
;
240 rtex
->array_mode
[level
] = array_mode
;
246 static void r600_setup_miptree(struct pipe_screen
*screen
,
247 struct r600_resource_texture
*rtex
,
250 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
251 enum chip_class chipc
= ((struct r600_screen
*)screen
)->chip_class
;
252 unsigned size
, layer_size
, i
, offset
;
253 unsigned nblocksx
, nblocksy
;
255 for (i
= 0, offset
= 0; i
<= ptex
->last_level
; i
++) {
256 unsigned blocksize
= util_format_get_blocksize(rtex
->real_format
);
257 unsigned base_align
= r600_get_base_alignment(screen
, rtex
->real_format
, array_mode
);
259 r600_texture_set_array_mode(screen
, rtex
, i
, array_mode
);
261 nblocksx
= r600_texture_get_nblocksx(screen
, rtex
, i
);
262 nblocksy
= r600_texture_get_nblocksy(screen
, rtex
, i
);
264 if (chipc
>= EVERGREEN
&& array_mode
== V_038000_ARRAY_LINEAR_GENERAL
)
265 layer_size
= align(nblocksx
, 64) * nblocksy
* blocksize
;
267 layer_size
= nblocksx
* nblocksy
* blocksize
;
269 if (ptex
->target
== PIPE_TEXTURE_CUBE
) {
271 size
= layer_size
* 8;
273 size
= layer_size
* 6;
275 else if (ptex
->target
== PIPE_TEXTURE_3D
)
276 size
= layer_size
* u_minify(ptex
->depth0
, i
);
278 size
= layer_size
* ptex
->array_size
;
280 /* align base image and start of miptree */
281 if ((i
== 0) || (i
== 1))
282 offset
= align(offset
, base_align
);
283 rtex
->offset
[i
] = offset
;
284 rtex
->layer_size
[i
] = layer_size
;
285 rtex
->pitch_in_blocks
[i
] = nblocksx
; /* CB talks in elements */
286 rtex
->pitch_in_bytes
[i
] = nblocksx
* blocksize
;
293 /* Figure out whether u_blitter will fallback to a transfer operation.
294 * If so, don't use a staging resource.
296 static boolean
permit_hardware_blit(struct pipe_screen
*screen
,
297 const struct pipe_resource
*res
)
301 if (util_format_is_depth_or_stencil(res
->format
))
302 bind
= PIPE_BIND_DEPTH_STENCIL
;
304 bind
= PIPE_BIND_RENDER_TARGET
;
306 /* hackaround for S3TC */
307 if (util_format_is_compressed(res
->format
))
310 if (!screen
->is_format_supported(screen
,
317 if (!screen
->is_format_supported(screen
,
321 PIPE_BIND_SAMPLER_VIEW
))
324 switch (res
->usage
) {
325 case PIPE_USAGE_STREAM
:
326 case PIPE_USAGE_STAGING
:
334 static boolean
r600_texture_get_handle(struct pipe_screen
* screen
,
335 struct pipe_resource
*ptex
,
336 struct winsys_handle
*whandle
)
338 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)ptex
;
339 struct r600_resource
*resource
= &rtex
->resource
;
340 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
342 return rscreen
->ws
->buffer_get_handle(resource
->buf
,
343 rtex
->pitch_in_bytes
[0], whandle
);
346 static void r600_texture_destroy(struct pipe_screen
*screen
,
347 struct pipe_resource
*ptex
)
349 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)ptex
;
350 struct r600_resource
*resource
= &rtex
->resource
;
352 if (rtex
->flushed_depth_texture
)
353 pipe_resource_reference((struct pipe_resource
**)&rtex
->flushed_depth_texture
, NULL
);
355 pb_reference(&resource
->buf
, NULL
);
359 static const struct u_resource_vtbl r600_texture_vtbl
=
361 r600_texture_get_handle
, /* get_handle */
362 r600_texture_destroy
, /* resource_destroy */
363 r600_texture_get_transfer
, /* get_transfer */
364 r600_texture_transfer_destroy
, /* transfer_destroy */
365 r600_texture_transfer_map
, /* transfer_map */
366 u_default_transfer_flush_region
,/* transfer_flush_region */
367 r600_texture_transfer_unmap
, /* transfer_unmap */
368 u_default_transfer_inline_write
/* transfer_inline_write */
371 static struct r600_resource_texture
*
372 r600_texture_create_object(struct pipe_screen
*screen
,
373 const struct pipe_resource
*base
,
375 unsigned pitch_in_bytes_override
,
376 unsigned max_buffer_size
,
377 struct pb_buffer
*buf
,
380 struct r600_resource_texture
*rtex
;
381 struct r600_resource
*resource
;
382 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
384 rtex
= CALLOC_STRUCT(r600_resource_texture
);
388 resource
= &rtex
->resource
;
389 resource
->b
.b
.b
= *base
;
390 resource
->b
.b
.vtbl
= &r600_texture_vtbl
;
391 pipe_reference_init(&resource
->b
.b
.b
.reference
, 1);
392 resource
->b
.b
.b
.screen
= screen
;
393 rtex
->pitch_override
= pitch_in_bytes_override
;
394 rtex
->real_format
= base
->format
;
396 /* We must split depth and stencil into two separate buffers on Evergreen. */
397 if (!(base
->flags
& R600_RESOURCE_FLAG_TRANSFER
) &&
398 ((struct r600_screen
*)screen
)->chip_class
>= EVERGREEN
&&
399 util_format_is_depth_and_stencil(base
->format
)) {
400 struct pipe_resource stencil
;
401 unsigned stencil_pitch_override
= 0;
403 switch (base
->format
) {
404 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
405 rtex
->real_format
= PIPE_FORMAT_Z24X8_UNORM
;
407 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
408 rtex
->real_format
= PIPE_FORMAT_X8Z24_UNORM
;
410 case PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED
:
411 rtex
->real_format
= PIPE_FORMAT_Z32_FLOAT
;
419 /* Divide the pitch in bytes by 4 for stencil, because it has a smaller pixel size. */
420 if (pitch_in_bytes_override
) {
421 assert(base
->format
== PIPE_FORMAT_Z24_UNORM_S8_USCALED
||
422 base
->format
== PIPE_FORMAT_S8_USCALED_Z24_UNORM
);
423 stencil_pitch_override
= pitch_in_bytes_override
/ 4;
426 /* Allocate the stencil buffer. */
428 stencil
.format
= PIPE_FORMAT_S8_USCALED
;
429 rtex
->stencil
= r600_texture_create_object(screen
, &stencil
, array_mode
,
430 stencil_pitch_override
,
431 max_buffer_size
, NULL
, FALSE
);
432 if (!rtex
->stencil
) {
436 /* Proceed in creating the depth buffer. */
439 /* only mark depth textures the HW can hit as depth textures */
440 if (util_format_is_depth_or_stencil(rtex
->real_format
) && permit_hardware_blit(screen
, base
))
443 r600_setup_miptree(screen
, rtex
, array_mode
);
445 /* If we initialized separate stencil for Evergreen. place it after depth. */
447 unsigned stencil_align
, stencil_offset
;
449 stencil_align
= r600_get_base_alignment(screen
, rtex
->stencil
->real_format
, array_mode
);
450 stencil_offset
= align(rtex
->size
, stencil_align
);
452 for (unsigned i
= 0; i
<= rtex
->stencil
->resource
.b
.b
.b
.last_level
; i
++)
453 rtex
->stencil
->offset
[i
] += stencil_offset
;
455 rtex
->size
= stencil_offset
+ rtex
->stencil
->size
;
458 /* Now create the backing buffer. */
459 if (!buf
&& alloc_bo
) {
460 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
461 unsigned base_align
= r600_get_base_alignment(screen
, ptex
->format
, array_mode
);
463 if (!r600_init_resource(rscreen
, resource
, rtex
->size
, base_align
, base
->bind
, base
->usage
)) {
464 pipe_resource_reference((struct pipe_resource
**)&rtex
->stencil
, NULL
);
470 resource
->cs_buf
= rscreen
->ws
->buffer_get_cs_handle(buf
);
471 resource
->domains
= RADEON_DOMAIN_GTT
| RADEON_DOMAIN_VRAM
;
475 rtex
->stencil
->resource
.buf
= rtex
->resource
.buf
;
476 rtex
->stencil
->resource
.cs_buf
= rtex
->resource
.cs_buf
;
477 rtex
->stencil
->resource
.domains
= rtex
->resource
.domains
;
482 DEBUG_GET_ONCE_BOOL_OPTION(tiling_enabled
, "R600_TILING", FALSE
);
484 struct pipe_resource
*r600_texture_create(struct pipe_screen
*screen
,
485 const struct pipe_resource
*templ
)
487 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
488 unsigned array_mode
= 0;
490 if (!(templ
->flags
& R600_RESOURCE_FLAG_TRANSFER
) &&
491 !(templ
->bind
& PIPE_BIND_SCANOUT
)) {
492 if (util_format_is_compressed(templ
->format
)) {
493 array_mode
= V_038000_ARRAY_1D_TILED_THIN1
;
495 else if (debug_get_option_tiling_enabled() &&
496 rscreen
->info
.drm_minor
>= 9 &&
497 permit_hardware_blit(screen
, templ
)) {
498 array_mode
= V_038000_ARRAY_2D_TILED_THIN1
;
502 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
, array_mode
,
506 static struct pipe_surface
*r600_create_surface(struct pipe_context
*pipe
,
507 struct pipe_resource
*texture
,
508 const struct pipe_surface
*surf_tmpl
)
510 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
511 struct r600_surface
*surface
= CALLOC_STRUCT(r600_surface
);
512 unsigned level
= surf_tmpl
->u
.tex
.level
;
514 assert(surf_tmpl
->u
.tex
.first_layer
== surf_tmpl
->u
.tex
.last_layer
);
518 /* offset = r600_texture_get_offset(rtex, level, surf_tmpl->u.tex.first_layer);*/
519 pipe_reference_init(&surface
->base
.reference
, 1);
520 pipe_resource_reference(&surface
->base
.texture
, texture
);
521 surface
->base
.context
= pipe
;
522 surface
->base
.format
= surf_tmpl
->format
;
523 surface
->base
.width
= mip_minify(texture
->width0
, level
);
524 surface
->base
.height
= mip_minify(texture
->height0
, level
);
525 surface
->base
.usage
= surf_tmpl
->usage
;
526 surface
->base
.texture
= texture
;
527 surface
->base
.u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
528 surface
->base
.u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
529 surface
->base
.u
.tex
.level
= level
;
531 surface
->aligned_height
= r600_texture_get_nblocksy(pipe
->screen
,
533 return &surface
->base
;
536 static void r600_surface_destroy(struct pipe_context
*pipe
,
537 struct pipe_surface
*surface
)
539 pipe_resource_reference(&surface
->texture
, NULL
);
543 struct pipe_resource
*r600_texture_from_handle(struct pipe_screen
*screen
,
544 const struct pipe_resource
*templ
,
545 struct winsys_handle
*whandle
)
547 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
548 struct pb_buffer
*buf
= NULL
;
550 unsigned array_mode
= 0;
551 enum radeon_bo_layout micro
, macro
;
553 /* Support only 2D textures without mipmaps */
554 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
) ||
555 templ
->depth0
!= 1 || templ
->last_level
!= 0)
558 buf
= rscreen
->ws
->buffer_from_handle(rscreen
->ws
, whandle
, &stride
, NULL
);
562 rscreen
->ws
->buffer_get_tiling(buf
, µ
, ¯o
);
564 if (macro
== RADEON_LAYOUT_TILED
)
565 array_mode
= V_0280A0_ARRAY_2D_TILED_THIN1
;
566 else if (micro
== RADEON_LAYOUT_TILED
)
567 array_mode
= V_0280A0_ARRAY_1D_TILED_THIN1
;
571 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
, array_mode
,
572 stride
, 0, buf
, FALSE
);
575 int r600_texture_depth_flush(struct pipe_context
*ctx
,
576 struct pipe_resource
*texture
, boolean just_create
)
578 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
579 struct pipe_resource resource
;
581 if (rtex
->flushed_depth_texture
)
584 resource
.target
= texture
->target
;
585 resource
.format
= texture
->format
;
586 resource
.width0
= texture
->width0
;
587 resource
.height0
= texture
->height0
;
588 resource
.depth0
= texture
->depth0
;
589 resource
.array_size
= texture
->array_size
;
590 resource
.last_level
= texture
->last_level
;
591 resource
.nr_samples
= texture
->nr_samples
;
592 resource
.usage
= PIPE_USAGE_DYNAMIC
;
593 resource
.bind
= texture
->bind
| PIPE_BIND_DEPTH_STENCIL
;
594 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
| texture
->flags
;
596 rtex
->flushed_depth_texture
= (struct r600_resource_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
597 if (rtex
->flushed_depth_texture
== NULL
) {
598 R600_ERR("failed to create temporary texture to hold untiled copy\n");
602 ((struct r600_resource_texture
*)rtex
->flushed_depth_texture
)->is_flushing_texture
= TRUE
;
607 /* XXX: only do this if the depth texture has actually changed:
609 r600_blit_uncompress_depth(ctx
, rtex
);
613 /* Needs adjustment for pixelformat:
615 static INLINE
unsigned u_box_volume( const struct pipe_box
*box
)
617 return box
->width
* box
->depth
* box
->height
;
620 struct pipe_transfer
* r600_texture_get_transfer(struct pipe_context
*ctx
,
621 struct pipe_resource
*texture
,
624 const struct pipe_box
*box
)
626 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
627 struct pipe_resource resource
;
628 struct r600_transfer
*trans
;
630 boolean use_staging_texture
= FALSE
;
632 /* We cannot map a tiled texture directly because the data is
633 * in a different order, therefore we do detiling using a blit.
635 * Also, use a temporary in GTT memory for read transfers, as
636 * the CPU is much happier reading out of cached system memory
637 * than uncached VRAM.
639 if (R600_TEX_IS_TILED(rtex
, level
))
640 use_staging_texture
= TRUE
;
642 if ((usage
& PIPE_TRANSFER_READ
) && u_box_volume(box
) > 1024)
643 use_staging_texture
= TRUE
;
645 /* XXX: Use a staging texture for uploads if the underlying BO
646 * is busy. No interface for checking that currently? so do
647 * it eagerly whenever the transfer doesn't require a readback
650 if ((usage
& PIPE_TRANSFER_WRITE
) &&
651 !(usage
& (PIPE_TRANSFER_READ
|
652 PIPE_TRANSFER_DONTBLOCK
|
653 PIPE_TRANSFER_UNSYNCHRONIZED
)))
654 use_staging_texture
= TRUE
;
656 if (!permit_hardware_blit(ctx
->screen
, texture
) ||
657 (texture
->flags
& R600_RESOURCE_FLAG_TRANSFER
))
658 use_staging_texture
= FALSE
;
660 if (use_staging_texture
&& (usage
& PIPE_TRANSFER_MAP_DIRECTLY
))
663 trans
= CALLOC_STRUCT(r600_transfer
);
666 pipe_resource_reference(&trans
->transfer
.resource
, texture
);
667 trans
->transfer
.level
= level
;
668 trans
->transfer
.usage
= usage
;
669 trans
->transfer
.box
= *box
;
671 /* XXX: only readback the rectangle which is being mapped?
673 /* XXX: when discard is true, no need to read back from depth texture
675 r
= r600_texture_depth_flush(ctx
, texture
, FALSE
);
677 R600_ERR("failed to create temporary texture to hold untiled copy\n");
678 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
682 trans
->transfer
.stride
= rtex
->flushed_depth_texture
->pitch_in_bytes
[level
];
683 trans
->offset
= r600_texture_get_offset(rtex
->flushed_depth_texture
, level
, box
->z
);
684 return &trans
->transfer
;
685 } else if (use_staging_texture
) {
686 resource
.target
= PIPE_TEXTURE_2D
;
687 resource
.format
= texture
->format
;
688 resource
.width0
= box
->width
;
689 resource
.height0
= box
->height
;
691 resource
.array_size
= 1;
692 resource
.last_level
= 0;
693 resource
.nr_samples
= 0;
694 resource
.usage
= PIPE_USAGE_STAGING
;
696 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
;
697 /* For texture reading, the temporary (detiled) texture is used as
698 * a render target when blitting from a tiled texture. */
699 if (usage
& PIPE_TRANSFER_READ
) {
700 resource
.bind
|= PIPE_BIND_RENDER_TARGET
;
702 /* For texture writing, the temporary texture is used as a sampler
703 * when blitting into a tiled texture. */
704 if (usage
& PIPE_TRANSFER_WRITE
) {
705 resource
.bind
|= PIPE_BIND_SAMPLER_VIEW
;
707 /* Create the temporary texture. */
708 trans
->staging_texture
= ctx
->screen
->resource_create(ctx
->screen
, &resource
);
709 if (trans
->staging_texture
== NULL
) {
710 R600_ERR("failed to create temporary texture to hold untiled copy\n");
711 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
716 trans
->transfer
.stride
=
717 ((struct r600_resource_texture
*)trans
->staging_texture
)->pitch_in_bytes
[0];
718 if (usage
& PIPE_TRANSFER_READ
) {
719 r600_copy_to_staging_texture(ctx
, trans
);
720 /* Always referenced in the blit. */
721 r600_flush(ctx
, NULL
, 0);
723 return &trans
->transfer
;
725 trans
->transfer
.stride
= rtex
->pitch_in_bytes
[level
];
726 trans
->transfer
.layer_stride
= rtex
->layer_size
[level
];
727 trans
->offset
= r600_texture_get_offset(rtex
, level
, box
->z
);
728 return &trans
->transfer
;
731 void r600_texture_transfer_destroy(struct pipe_context
*ctx
,
732 struct pipe_transfer
*transfer
)
734 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
735 struct pipe_resource
*texture
= transfer
->resource
;
736 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
738 if (rtransfer
->staging_texture
) {
739 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
740 r600_copy_from_staging_texture(ctx
, rtransfer
);
742 pipe_resource_reference(&rtransfer
->staging_texture
, NULL
);
745 if (rtex
->depth
&& !rtex
->is_flushing_texture
) {
746 if ((transfer
->usage
& PIPE_TRANSFER_WRITE
) && rtex
->flushed_depth_texture
)
747 r600_blit_push_depth(ctx
, rtex
);
750 pipe_resource_reference(&transfer
->resource
, NULL
);
754 void* r600_texture_transfer_map(struct pipe_context
*ctx
,
755 struct pipe_transfer
* transfer
)
757 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
758 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
759 struct pb_buffer
*buf
;
760 enum pipe_format format
= transfer
->resource
->format
;
764 if (rtransfer
->staging_texture
) {
765 buf
= ((struct r600_resource
*)rtransfer
->staging_texture
)->buf
;
767 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
769 if (rtex
->flushed_depth_texture
)
770 buf
= ((struct r600_resource
*)rtex
->flushed_depth_texture
)->buf
;
772 buf
= ((struct r600_resource
*)transfer
->resource
)->buf
;
774 offset
= rtransfer
->offset
+
775 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
776 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
779 if (!(map
= rctx
->ws
->buffer_map(buf
, rctx
->ctx
.cs
, transfer
->usage
))) {
786 void r600_texture_transfer_unmap(struct pipe_context
*ctx
,
787 struct pipe_transfer
* transfer
)
789 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
790 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
791 struct pb_buffer
*buf
;
793 if (rtransfer
->staging_texture
) {
794 buf
= ((struct r600_resource
*)rtransfer
->staging_texture
)->buf
;
796 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
798 if (rtex
->flushed_depth_texture
) {
799 buf
= ((struct r600_resource
*)rtex
->flushed_depth_texture
)->buf
;
801 buf
= ((struct r600_resource
*)transfer
->resource
)->buf
;
804 rctx
->ws
->buffer_unmap(buf
);
807 void r600_init_surface_functions(struct r600_pipe_context
*r600
)
809 r600
->context
.create_surface
= r600_create_surface
;
810 r600
->context
.surface_destroy
= r600_surface_destroy
;
813 static unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format
,
814 const unsigned char *swizzle_view
)
817 unsigned char swizzle
[4];
819 const uint32_t swizzle_shift
[4] = {
822 const uint32_t swizzle_bit
[4] = {
827 util_format_compose_swizzles(swizzle_format
, swizzle_view
, swizzle
);
829 memcpy(swizzle
, swizzle_format
, 4);
833 for (i
= 0; i
< 4; i
++) {
834 switch (swizzle
[i
]) {
835 case UTIL_FORMAT_SWIZZLE_Y
:
836 result
|= swizzle_bit
[1] << swizzle_shift
[i
];
838 case UTIL_FORMAT_SWIZZLE_Z
:
839 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
841 case UTIL_FORMAT_SWIZZLE_W
:
842 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
844 case UTIL_FORMAT_SWIZZLE_0
:
845 result
|= V_038010_SQ_SEL_0
<< swizzle_shift
[i
];
847 case UTIL_FORMAT_SWIZZLE_1
:
848 result
|= V_038010_SQ_SEL_1
<< swizzle_shift
[i
];
850 default: /* UTIL_FORMAT_SWIZZLE_X */
851 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
857 /* texture format translate */
858 uint32_t r600_translate_texformat(struct pipe_screen
*screen
,
859 enum pipe_format format
,
860 const unsigned char *swizzle_view
,
861 uint32_t *word4_p
, uint32_t *yuv_format_p
)
863 uint32_t result
= 0, word4
= 0, yuv_format
= 0;
864 const struct util_format_description
*desc
;
865 boolean uniform
= TRUE
;
866 static int r600_enable_s3tc
= -1;
869 const uint32_t sign_bit
[4] = {
870 S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED
),
871 S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED
),
872 S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED
),
873 S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED
)
875 desc
= util_format_description(format
);
877 word4
|= r600_get_swizzle_combined(desc
->swizzle
, swizzle_view
);
879 /* Colorspace (return non-RGB formats directly). */
880 switch (desc
->colorspace
) {
881 /* Depth stencil formats */
882 case UTIL_FORMAT_COLORSPACE_ZS
:
884 case PIPE_FORMAT_Z16_UNORM
:
887 case PIPE_FORMAT_X24S8_USCALED
:
888 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
889 case PIPE_FORMAT_Z24X8_UNORM
:
890 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
893 case PIPE_FORMAT_S8X24_USCALED
:
894 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
895 case PIPE_FORMAT_X8Z24_UNORM
:
896 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
899 case PIPE_FORMAT_S8_USCALED
:
901 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
903 case PIPE_FORMAT_Z32_FLOAT
:
904 result
= FMT_32_FLOAT
;
906 case PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED
:
907 result
= FMT_X24_8_32_FLOAT
;
913 case UTIL_FORMAT_COLORSPACE_YUV
:
914 yuv_format
|= (1 << 30);
916 case PIPE_FORMAT_UYVY
:
917 case PIPE_FORMAT_YUYV
:
921 goto out_unknown
; /* TODO */
923 case UTIL_FORMAT_COLORSPACE_SRGB
:
924 word4
|= S_038010_FORCE_DEGAMMA(1);
931 if (r600_enable_s3tc
== -1) {
932 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
933 if (rscreen
->info
.drm_minor
>= 9)
934 r600_enable_s3tc
= 1;
936 r600_enable_s3tc
= debug_get_bool_option("R600_ENABLE_S3TC", FALSE
);
939 if (desc
->layout
== UTIL_FORMAT_LAYOUT_RGTC
) {
940 if (!r600_enable_s3tc
)
944 case PIPE_FORMAT_RGTC1_SNORM
:
945 case PIPE_FORMAT_LATC1_SNORM
:
946 word4
|= sign_bit
[0];
947 case PIPE_FORMAT_RGTC1_UNORM
:
948 case PIPE_FORMAT_LATC1_UNORM
:
951 case PIPE_FORMAT_RGTC2_SNORM
:
952 case PIPE_FORMAT_LATC2_SNORM
:
953 word4
|= sign_bit
[0] | sign_bit
[1];
954 case PIPE_FORMAT_RGTC2_UNORM
:
955 case PIPE_FORMAT_LATC2_UNORM
:
963 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
965 if (!r600_enable_s3tc
)
968 if (!util_format_s3tc_enabled
) {
973 case PIPE_FORMAT_DXT1_RGB
:
974 case PIPE_FORMAT_DXT1_RGBA
:
975 case PIPE_FORMAT_DXT1_SRGB
:
976 case PIPE_FORMAT_DXT1_SRGBA
:
979 case PIPE_FORMAT_DXT3_RGBA
:
980 case PIPE_FORMAT_DXT3_SRGBA
:
983 case PIPE_FORMAT_DXT5_RGBA
:
984 case PIPE_FORMAT_DXT5_SRGBA
:
992 if (format
== PIPE_FORMAT_R9G9B9E5_FLOAT
) {
993 result
= FMT_5_9_9_9_SHAREDEXP
;
995 } else if (format
== PIPE_FORMAT_R11G11B10_FLOAT
) {
996 result
= FMT_10_11_11_FLOAT
;
1001 for (i
= 0; i
< desc
->nr_channels
; i
++) {
1002 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
1003 word4
|= sign_bit
[i
];
1007 /* R8G8Bx_SNORM - TODO CxV8U8 */
1009 /* See whether the components are of the same size. */
1010 for (i
= 1; i
< desc
->nr_channels
; i
++) {
1011 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
1014 /* Non-uniform formats. */
1016 switch(desc
->nr_channels
) {
1018 if (desc
->channel
[0].size
== 5 &&
1019 desc
->channel
[1].size
== 6 &&
1020 desc
->channel
[2].size
== 5) {
1026 if (desc
->channel
[0].size
== 5 &&
1027 desc
->channel
[1].size
== 5 &&
1028 desc
->channel
[2].size
== 5 &&
1029 desc
->channel
[3].size
== 1) {
1030 result
= FMT_1_5_5_5
;
1033 if (desc
->channel
[0].size
== 10 &&
1034 desc
->channel
[1].size
== 10 &&
1035 desc
->channel
[2].size
== 10 &&
1036 desc
->channel
[3].size
== 2) {
1037 result
= FMT_2_10_10_10
;
1045 /* Find the first non-VOID channel. */
1046 for (i
= 0; i
< 4; i
++) {
1047 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
1055 /* uniform formats */
1056 switch (desc
->channel
[i
].type
) {
1057 case UTIL_FORMAT_TYPE_UNSIGNED
:
1058 case UTIL_FORMAT_TYPE_SIGNED
:
1060 if (!desc
->channel
[i
].normalized
&&
1061 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
1065 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
&&
1066 !desc
->channel
[i
].normalized
)
1067 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
1069 switch (desc
->channel
[i
].size
) {
1071 switch (desc
->nr_channels
) {
1076 result
= FMT_4_4_4_4
;
1081 switch (desc
->nr_channels
) {
1089 result
= FMT_8_8_8_8
;
1094 switch (desc
->nr_channels
) {
1102 result
= FMT_16_16_16_16
;
1107 switch (desc
->nr_channels
) {
1115 result
= FMT_32_32_32_32
;
1121 case UTIL_FORMAT_TYPE_FLOAT
:
1122 switch (desc
->channel
[i
].size
) {
1124 switch (desc
->nr_channels
) {
1126 result
= FMT_16_FLOAT
;
1129 result
= FMT_16_16_FLOAT
;
1132 result
= FMT_16_16_16_16_FLOAT
;
1137 switch (desc
->nr_channels
) {
1139 result
= FMT_32_FLOAT
;
1142 result
= FMT_32_32_FLOAT
;
1145 result
= FMT_32_32_32_32_FLOAT
;
1156 *yuv_format_p
= yuv_format
;
1159 /* R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format)); */