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
,
70 unsigned r600_texture_get_offset(struct r600_resource_texture
*rtex
,
71 unsigned level
, unsigned layer
)
73 unsigned offset
= rtex
->offset
[level
];
75 switch (rtex
->resource
.b
.b
.b
.target
) {
77 case PIPE_TEXTURE_CUBE
:
79 return offset
+ layer
* rtex
->layer_size
[level
];
83 static unsigned r600_get_block_alignment(struct pipe_screen
*screen
,
84 enum pipe_format format
,
87 struct r600_screen
* rscreen
= (struct r600_screen
*)screen
;
88 unsigned pixsize
= util_format_get_blocksize(format
);
92 case V_038000_ARRAY_1D_TILED_THIN1
:
94 ((rscreen
->tiling_info
.group_bytes
/ 8 / pixsize
)));
96 case V_038000_ARRAY_2D_TILED_THIN1
:
97 p_align
= MAX2(rscreen
->tiling_info
.num_banks
,
98 (((rscreen
->tiling_info
.group_bytes
/ 8 / pixsize
)) *
99 rscreen
->tiling_info
.num_banks
)) * 8;
101 case V_038000_ARRAY_LINEAR_ALIGNED
:
102 p_align
= MAX2(64, rscreen
->tiling_info
.group_bytes
/ pixsize
);
104 case V_038000_ARRAY_LINEAR_GENERAL
:
106 p_align
= rscreen
->tiling_info
.group_bytes
/ pixsize
;
112 static unsigned r600_get_height_alignment(struct pipe_screen
*screen
,
115 struct r600_screen
* rscreen
= (struct r600_screen
*)screen
;
118 switch (array_mode
) {
119 case V_038000_ARRAY_2D_TILED_THIN1
:
120 h_align
= rscreen
->tiling_info
.num_channels
* 8;
122 case V_038000_ARRAY_1D_TILED_THIN1
:
123 case V_038000_ARRAY_LINEAR_ALIGNED
:
126 case V_038000_ARRAY_LINEAR_GENERAL
:
134 static unsigned r600_get_base_alignment(struct pipe_screen
*screen
,
135 enum pipe_format format
,
138 struct r600_screen
* rscreen
= (struct r600_screen
*)screen
;
139 unsigned pixsize
= util_format_get_blocksize(format
);
140 int p_align
= r600_get_block_alignment(screen
, format
, array_mode
);
141 int h_align
= r600_get_height_alignment(screen
, array_mode
);
144 switch (array_mode
) {
145 case V_038000_ARRAY_2D_TILED_THIN1
:
146 b_align
= MAX2(rscreen
->tiling_info
.num_banks
* rscreen
->tiling_info
.num_channels
* 8 * 8 * pixsize
,
147 p_align
* pixsize
* h_align
);
149 case V_038000_ARRAY_1D_TILED_THIN1
:
150 case V_038000_ARRAY_LINEAR_ALIGNED
:
151 case V_038000_ARRAY_LINEAR_GENERAL
:
153 b_align
= rscreen
->tiling_info
.group_bytes
;
159 static unsigned mip_minify(unsigned size
, unsigned level
)
162 val
= u_minify(size
, level
);
164 val
= util_next_power_of_two(val
);
168 static unsigned r600_texture_get_nblocksx(struct pipe_screen
*screen
,
169 struct r600_resource_texture
*rtex
,
172 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
173 unsigned nblocksx
, block_align
, width
;
174 unsigned blocksize
= util_format_get_blocksize(rtex
->real_format
);
176 if (rtex
->pitch_override
)
177 return rtex
->pitch_override
/ blocksize
;
179 width
= mip_minify(ptex
->width0
, level
);
180 nblocksx
= util_format_get_nblocksx(rtex
->real_format
, width
);
182 block_align
= r600_get_block_alignment(screen
, rtex
->real_format
,
183 rtex
->array_mode
[level
]);
184 nblocksx
= align(nblocksx
, block_align
);
188 static unsigned r600_texture_get_nblocksy(struct pipe_screen
*screen
,
189 struct r600_resource_texture
*rtex
,
192 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
193 unsigned height
, tile_height
;
195 height
= mip_minify(ptex
->height0
, level
);
196 height
= util_format_get_nblocksy(rtex
->real_format
, height
);
197 tile_height
= r600_get_height_alignment(screen
,
198 rtex
->array_mode
[level
]);
200 /* XXX Hack around an alignment issue. Less tests fail with this.
202 * The thing is depth-stencil buffers should be tiled, i.e.
203 * the alignment should be >=8. If I make them tiled, stencil starts
204 * working because it no longer overlaps with the depth buffer
205 * in memory, but texturing like drawpix-stencil breaks. */
206 if (util_format_is_depth_or_stencil(rtex
->real_format
) && tile_height
< 8)
209 height
= align(height
, tile_height
);
213 static void r600_texture_set_array_mode(struct pipe_screen
*screen
,
214 struct r600_resource_texture
*rtex
,
215 unsigned level
, unsigned array_mode
)
217 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
219 switch (array_mode
) {
220 case V_0280A0_ARRAY_LINEAR_GENERAL
:
221 case V_0280A0_ARRAY_LINEAR_ALIGNED
:
222 case V_0280A0_ARRAY_1D_TILED_THIN1
:
224 rtex
->array_mode
[level
] = array_mode
;
226 case V_0280A0_ARRAY_2D_TILED_THIN1
:
228 unsigned w
, h
, tile_height
, tile_width
;
230 tile_height
= r600_get_height_alignment(screen
, array_mode
);
231 tile_width
= r600_get_block_alignment(screen
, rtex
->real_format
, array_mode
);
233 w
= mip_minify(ptex
->width0
, level
);
234 h
= mip_minify(ptex
->height0
, level
);
235 if (w
<= tile_width
|| h
<= tile_height
)
236 rtex
->array_mode
[level
] = V_0280A0_ARRAY_1D_TILED_THIN1
;
238 rtex
->array_mode
[level
] = array_mode
;
244 static void r600_setup_miptree(struct pipe_screen
*screen
,
245 struct r600_resource_texture
*rtex
,
248 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
249 enum chip_class chipc
= ((struct r600_screen
*)screen
)->chip_class
;
250 unsigned size
, layer_size
, i
, offset
;
251 unsigned nblocksx
, nblocksy
;
253 for (i
= 0, offset
= 0; i
<= ptex
->last_level
; i
++) {
254 unsigned blocksize
= util_format_get_blocksize(rtex
->real_format
);
255 unsigned base_align
= r600_get_base_alignment(screen
, rtex
->real_format
, array_mode
);
257 r600_texture_set_array_mode(screen
, rtex
, i
, array_mode
);
259 nblocksx
= r600_texture_get_nblocksx(screen
, rtex
, i
);
260 nblocksy
= r600_texture_get_nblocksy(screen
, rtex
, i
);
262 if (chipc
>= EVERGREEN
&& array_mode
== V_038000_ARRAY_LINEAR_GENERAL
)
263 layer_size
= align(nblocksx
, 64) * nblocksy
* blocksize
;
265 layer_size
= nblocksx
* nblocksy
* blocksize
;
267 if (ptex
->target
== PIPE_TEXTURE_CUBE
) {
269 size
= layer_size
* 8;
271 size
= layer_size
* 6;
273 else if (ptex
->target
== PIPE_TEXTURE_3D
)
274 size
= layer_size
* u_minify(ptex
->depth0
, i
);
276 size
= layer_size
* ptex
->array_size
;
278 /* align base image and start of miptree */
279 if ((i
== 0) || (i
== 1))
280 offset
= align(offset
, base_align
);
281 rtex
->offset
[i
] = offset
;
282 rtex
->layer_size
[i
] = layer_size
;
283 rtex
->pitch_in_blocks
[i
] = nblocksx
; /* CB talks in elements */
284 rtex
->pitch_in_bytes
[i
] = nblocksx
* blocksize
;
291 /* Figure out whether u_blitter will fallback to a transfer operation.
292 * If so, don't use a staging resource.
294 static boolean
permit_hardware_blit(struct pipe_screen
*screen
,
295 const struct pipe_resource
*res
)
299 if (util_format_is_depth_or_stencil(res
->format
))
300 bind
= PIPE_BIND_DEPTH_STENCIL
;
302 bind
= PIPE_BIND_RENDER_TARGET
;
304 /* hackaround for S3TC */
305 if (util_format_is_compressed(res
->format
))
308 if (!screen
->is_format_supported(screen
,
315 if (!screen
->is_format_supported(screen
,
319 PIPE_BIND_SAMPLER_VIEW
))
322 switch (res
->usage
) {
323 case PIPE_USAGE_STREAM
:
324 case PIPE_USAGE_STAGING
:
332 static boolean
r600_texture_get_handle(struct pipe_screen
* screen
,
333 struct pipe_resource
*ptex
,
334 struct winsys_handle
*whandle
)
336 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)ptex
;
337 struct r600_resource
*resource
= &rtex
->resource
;
338 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
340 return rscreen
->ws
->buffer_get_handle(resource
->buf
,
341 rtex
->pitch_in_bytes
[0], whandle
);
344 static void r600_texture_destroy(struct pipe_screen
*screen
,
345 struct pipe_resource
*ptex
)
347 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)ptex
;
348 struct r600_resource
*resource
= &rtex
->resource
;
350 if (rtex
->flushed_depth_texture
)
351 pipe_resource_reference((struct pipe_resource
**)&rtex
->flushed_depth_texture
, NULL
);
354 pipe_resource_reference((struct pipe_resource
**)&rtex
->stencil
, NULL
);
356 pb_reference(&resource
->buf
, NULL
);
360 static const struct u_resource_vtbl r600_texture_vtbl
=
362 r600_texture_get_handle
, /* get_handle */
363 r600_texture_destroy
, /* resource_destroy */
364 r600_texture_get_transfer
, /* get_transfer */
365 r600_texture_transfer_destroy
, /* transfer_destroy */
366 r600_texture_transfer_map
, /* transfer_map */
367 u_default_transfer_flush_region
,/* transfer_flush_region */
368 r600_texture_transfer_unmap
, /* transfer_unmap */
369 u_default_transfer_inline_write
/* transfer_inline_write */
372 static struct r600_resource_texture
*
373 r600_texture_create_object(struct pipe_screen
*screen
,
374 const struct pipe_resource
*base
,
376 unsigned pitch_in_bytes_override
,
377 unsigned max_buffer_size
,
378 struct pb_buffer
*buf
,
381 struct r600_resource_texture
*rtex
;
382 struct r600_resource
*resource
;
383 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
385 rtex
= CALLOC_STRUCT(r600_resource_texture
);
389 resource
= &rtex
->resource
;
390 resource
->b
.b
.b
= *base
;
391 resource
->b
.b
.vtbl
= &r600_texture_vtbl
;
392 pipe_reference_init(&resource
->b
.b
.b
.reference
, 1);
393 resource
->b
.b
.b
.screen
= screen
;
394 rtex
->pitch_override
= pitch_in_bytes_override
;
395 rtex
->real_format
= base
->format
;
397 /* We must split depth and stencil into two separate buffers on Evergreen. */
398 if (!(base
->flags
& R600_RESOURCE_FLAG_TRANSFER
) &&
399 ((struct r600_screen
*)screen
)->chip_class
>= EVERGREEN
&&
400 util_format_is_depth_and_stencil(base
->format
)) {
401 struct pipe_resource stencil
;
402 unsigned stencil_pitch_override
= 0;
404 switch (base
->format
) {
405 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
406 rtex
->real_format
= PIPE_FORMAT_Z24X8_UNORM
;
408 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
409 rtex
->real_format
= PIPE_FORMAT_X8Z24_UNORM
;
411 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
412 rtex
->real_format
= PIPE_FORMAT_Z32_FLOAT
;
420 /* Divide the pitch in bytes by 4 for stencil, because it has a smaller pixel size. */
421 if (pitch_in_bytes_override
) {
422 assert(base
->format
== PIPE_FORMAT_Z24_UNORM_S8_UINT
||
423 base
->format
== PIPE_FORMAT_S8_UINT_Z24_UNORM
);
424 stencil_pitch_override
= pitch_in_bytes_override
/ 4;
427 /* Allocate the stencil buffer. */
429 stencil
.format
= PIPE_FORMAT_S8_UINT
;
430 rtex
->stencil
= r600_texture_create_object(screen
, &stencil
, array_mode
,
431 stencil_pitch_override
,
432 max_buffer_size
, NULL
, FALSE
);
433 if (!rtex
->stencil
) {
437 /* Proceed in creating the depth buffer. */
440 /* only mark depth textures the HW can hit as depth textures */
441 if (util_format_is_depth_or_stencil(rtex
->real_format
) && permit_hardware_blit(screen
, base
))
444 r600_setup_miptree(screen
, rtex
, array_mode
);
446 /* If we initialized separate stencil for Evergreen. place it after depth. */
448 unsigned stencil_align
, stencil_offset
;
450 stencil_align
= r600_get_base_alignment(screen
, rtex
->stencil
->real_format
, array_mode
);
451 stencil_offset
= align(rtex
->size
, stencil_align
);
453 for (unsigned i
= 0; i
<= rtex
->stencil
->resource
.b
.b
.b
.last_level
; i
++)
454 rtex
->stencil
->offset
[i
] += stencil_offset
;
456 rtex
->size
= stencil_offset
+ rtex
->stencil
->size
;
459 /* Now create the backing buffer. */
460 if (!buf
&& alloc_bo
) {
461 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
462 unsigned base_align
= r600_get_base_alignment(screen
, ptex
->format
, array_mode
);
464 if (!r600_init_resource(rscreen
, resource
, rtex
->size
, base_align
, base
->bind
, base
->usage
)) {
465 pipe_resource_reference((struct pipe_resource
**)&rtex
->stencil
, NULL
);
471 resource
->cs_buf
= rscreen
->ws
->buffer_get_cs_handle(buf
);
472 resource
->domains
= RADEON_DOMAIN_GTT
| RADEON_DOMAIN_VRAM
;
476 pb_reference(&rtex
->stencil
->resource
.buf
, rtex
->resource
.buf
);
477 rtex
->stencil
->resource
.cs_buf
= rtex
->resource
.cs_buf
;
478 rtex
->stencil
->resource
.domains
= rtex
->resource
.domains
;
483 DEBUG_GET_ONCE_BOOL_OPTION(tiling_enabled
, "R600_TILING", FALSE
);
485 struct pipe_resource
*r600_texture_create(struct pipe_screen
*screen
,
486 const struct pipe_resource
*templ
)
488 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
489 unsigned array_mode
= 0;
491 if (!(templ
->flags
& R600_RESOURCE_FLAG_TRANSFER
) &&
492 !(templ
->bind
& PIPE_BIND_SCANOUT
)) {
493 if (util_format_is_compressed(templ
->format
)) {
494 array_mode
= V_038000_ARRAY_1D_TILED_THIN1
;
496 else if (debug_get_option_tiling_enabled() &&
497 rscreen
->info
.drm_minor
>= 9 &&
498 permit_hardware_blit(screen
, templ
)) {
499 array_mode
= V_038000_ARRAY_2D_TILED_THIN1
;
503 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
, array_mode
,
507 static struct pipe_surface
*r600_create_surface(struct pipe_context
*pipe
,
508 struct pipe_resource
*texture
,
509 const struct pipe_surface
*surf_tmpl
)
511 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
512 struct r600_surface
*surface
= CALLOC_STRUCT(r600_surface
);
513 unsigned level
= surf_tmpl
->u
.tex
.level
;
515 assert(surf_tmpl
->u
.tex
.first_layer
== surf_tmpl
->u
.tex
.last_layer
);
519 /* offset = r600_texture_get_offset(rtex, level, surf_tmpl->u.tex.first_layer);*/
520 pipe_reference_init(&surface
->base
.reference
, 1);
521 pipe_resource_reference(&surface
->base
.texture
, texture
);
522 surface
->base
.context
= pipe
;
523 surface
->base
.format
= surf_tmpl
->format
;
524 surface
->base
.width
= mip_minify(texture
->width0
, level
);
525 surface
->base
.height
= mip_minify(texture
->height0
, level
);
526 surface
->base
.usage
= surf_tmpl
->usage
;
527 surface
->base
.texture
= texture
;
528 surface
->base
.u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
529 surface
->base
.u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
530 surface
->base
.u
.tex
.level
= level
;
532 surface
->aligned_height
= r600_texture_get_nblocksy(pipe
->screen
,
534 return &surface
->base
;
537 static void r600_surface_destroy(struct pipe_context
*pipe
,
538 struct pipe_surface
*surface
)
540 pipe_resource_reference(&surface
->texture
, NULL
);
544 struct pipe_resource
*r600_texture_from_handle(struct pipe_screen
*screen
,
545 const struct pipe_resource
*templ
,
546 struct winsys_handle
*whandle
)
548 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
549 struct pb_buffer
*buf
= NULL
;
551 unsigned array_mode
= 0;
552 enum radeon_bo_layout micro
, macro
;
554 /* Support only 2D textures without mipmaps */
555 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
) ||
556 templ
->depth0
!= 1 || templ
->last_level
!= 0)
559 buf
= rscreen
->ws
->buffer_from_handle(rscreen
->ws
, whandle
, &stride
);
563 rscreen
->ws
->buffer_get_tiling(buf
, µ
, ¯o
);
565 if (macro
== RADEON_LAYOUT_TILED
)
566 array_mode
= V_0280A0_ARRAY_2D_TILED_THIN1
;
567 else if (micro
== RADEON_LAYOUT_TILED
)
568 array_mode
= V_0280A0_ARRAY_1D_TILED_THIN1
;
572 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
, array_mode
,
573 stride
, 0, buf
, FALSE
);
576 int r600_texture_depth_flush(struct pipe_context
*ctx
,
577 struct pipe_resource
*texture
, boolean just_create
)
579 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
580 struct pipe_resource resource
;
582 if (rtex
->flushed_depth_texture
)
585 resource
.target
= texture
->target
;
586 resource
.format
= texture
->format
;
587 resource
.width0
= texture
->width0
;
588 resource
.height0
= texture
->height0
;
589 resource
.depth0
= texture
->depth0
;
590 resource
.array_size
= texture
->array_size
;
591 resource
.last_level
= texture
->last_level
;
592 resource
.nr_samples
= texture
->nr_samples
;
593 resource
.usage
= PIPE_USAGE_DYNAMIC
;
594 resource
.bind
= texture
->bind
| PIPE_BIND_DEPTH_STENCIL
;
595 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
| texture
->flags
;
597 rtex
->flushed_depth_texture
= (struct r600_resource_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
598 if (rtex
->flushed_depth_texture
== NULL
) {
599 R600_ERR("failed to create temporary texture to hold untiled copy\n");
603 ((struct r600_resource_texture
*)rtex
->flushed_depth_texture
)->is_flushing_texture
= TRUE
;
608 /* XXX: only do this if the depth texture has actually changed:
610 r600_blit_uncompress_depth(ctx
, rtex
);
614 /* Needs adjustment for pixelformat:
616 static INLINE
unsigned u_box_volume( const struct pipe_box
*box
)
618 return box
->width
* box
->depth
* box
->height
;
621 struct pipe_transfer
* r600_texture_get_transfer(struct pipe_context
*ctx
,
622 struct pipe_resource
*texture
,
625 const struct pipe_box
*box
)
627 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
628 struct pipe_resource resource
;
629 struct r600_transfer
*trans
;
631 boolean use_staging_texture
= FALSE
;
633 if (usage
& PIPE_TRANSFER_MAP_PERMANENTLY
) {
637 /* We cannot map a tiled texture directly because the data is
638 * in a different order, therefore we do detiling using a blit.
640 * Also, use a temporary in GTT memory for read transfers, as
641 * the CPU is much happier reading out of cached system memory
642 * than uncached VRAM.
644 if (R600_TEX_IS_TILED(rtex
, level
))
645 use_staging_texture
= TRUE
;
647 if ((usage
& PIPE_TRANSFER_READ
) && u_box_volume(box
) > 1024)
648 use_staging_texture
= TRUE
;
650 /* XXX: Use a staging texture for uploads if the underlying BO
651 * is busy. No interface for checking that currently? so do
652 * it eagerly whenever the transfer doesn't require a readback
655 if ((usage
& PIPE_TRANSFER_WRITE
) &&
656 !(usage
& (PIPE_TRANSFER_READ
|
657 PIPE_TRANSFER_DONTBLOCK
|
658 PIPE_TRANSFER_UNSYNCHRONIZED
)))
659 use_staging_texture
= TRUE
;
661 if (!permit_hardware_blit(ctx
->screen
, texture
) ||
662 (texture
->flags
& R600_RESOURCE_FLAG_TRANSFER
))
663 use_staging_texture
= FALSE
;
665 if (use_staging_texture
&& (usage
& PIPE_TRANSFER_MAP_DIRECTLY
))
668 trans
= CALLOC_STRUCT(r600_transfer
);
671 pipe_resource_reference(&trans
->transfer
.resource
, texture
);
672 trans
->transfer
.level
= level
;
673 trans
->transfer
.usage
= usage
;
674 trans
->transfer
.box
= *box
;
676 /* XXX: only readback the rectangle which is being mapped?
678 /* XXX: when discard is true, no need to read back from depth texture
680 r
= r600_texture_depth_flush(ctx
, texture
, FALSE
);
682 R600_ERR("failed to create temporary texture to hold untiled copy\n");
683 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
687 trans
->transfer
.stride
= rtex
->flushed_depth_texture
->pitch_in_bytes
[level
];
688 trans
->offset
= r600_texture_get_offset(rtex
->flushed_depth_texture
, level
, box
->z
);
689 return &trans
->transfer
;
690 } else if (use_staging_texture
) {
691 resource
.target
= PIPE_TEXTURE_2D
;
692 resource
.format
= texture
->format
;
693 resource
.width0
= box
->width
;
694 resource
.height0
= box
->height
;
696 resource
.array_size
= 1;
697 resource
.last_level
= 0;
698 resource
.nr_samples
= 0;
699 resource
.usage
= PIPE_USAGE_STAGING
;
701 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
;
702 /* For texture reading, the temporary (detiled) texture is used as
703 * a render target when blitting from a tiled texture. */
704 if (usage
& PIPE_TRANSFER_READ
) {
705 resource
.bind
|= PIPE_BIND_RENDER_TARGET
;
707 /* For texture writing, the temporary texture is used as a sampler
708 * when blitting into a tiled texture. */
709 if (usage
& PIPE_TRANSFER_WRITE
) {
710 resource
.bind
|= PIPE_BIND_SAMPLER_VIEW
;
712 /* Create the temporary texture. */
713 trans
->staging_texture
= ctx
->screen
->resource_create(ctx
->screen
, &resource
);
714 if (trans
->staging_texture
== NULL
) {
715 R600_ERR("failed to create temporary texture to hold untiled copy\n");
716 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
721 trans
->transfer
.stride
=
722 ((struct r600_resource_texture
*)trans
->staging_texture
)->pitch_in_bytes
[0];
723 if (usage
& PIPE_TRANSFER_READ
) {
724 r600_copy_to_staging_texture(ctx
, trans
);
725 /* Always referenced in the blit. */
726 r600_flush(ctx
, NULL
, 0);
728 return &trans
->transfer
;
730 trans
->transfer
.stride
= rtex
->pitch_in_bytes
[level
];
731 trans
->transfer
.layer_stride
= rtex
->layer_size
[level
];
732 trans
->offset
= r600_texture_get_offset(rtex
, level
, box
->z
);
733 return &trans
->transfer
;
736 void r600_texture_transfer_destroy(struct pipe_context
*ctx
,
737 struct pipe_transfer
*transfer
)
739 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
740 struct pipe_resource
*texture
= transfer
->resource
;
741 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
743 if (rtransfer
->staging_texture
) {
744 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
745 r600_copy_from_staging_texture(ctx
, rtransfer
);
747 pipe_resource_reference(&rtransfer
->staging_texture
, NULL
);
750 if (rtex
->depth
&& !rtex
->is_flushing_texture
) {
751 if ((transfer
->usage
& PIPE_TRANSFER_WRITE
) && rtex
->flushed_depth_texture
)
752 r600_blit_push_depth(ctx
, rtex
);
755 pipe_resource_reference(&transfer
->resource
, NULL
);
759 void* r600_texture_transfer_map(struct pipe_context
*ctx
,
760 struct pipe_transfer
* transfer
)
762 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
763 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
764 struct pb_buffer
*buf
;
765 enum pipe_format format
= transfer
->resource
->format
;
769 if (rtransfer
->staging_texture
) {
770 buf
= ((struct r600_resource
*)rtransfer
->staging_texture
)->buf
;
772 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
774 if (rtex
->flushed_depth_texture
)
775 buf
= ((struct r600_resource
*)rtex
->flushed_depth_texture
)->buf
;
777 buf
= ((struct r600_resource
*)transfer
->resource
)->buf
;
779 offset
= rtransfer
->offset
+
780 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
781 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
784 if (!(map
= rctx
->ws
->buffer_map(buf
, rctx
->cs
, transfer
->usage
))) {
791 void r600_texture_transfer_unmap(struct pipe_context
*ctx
,
792 struct pipe_transfer
* transfer
)
794 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
795 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
796 struct pb_buffer
*buf
;
798 if (rtransfer
->staging_texture
) {
799 buf
= ((struct r600_resource
*)rtransfer
->staging_texture
)->buf
;
801 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
803 if (rtex
->flushed_depth_texture
) {
804 buf
= ((struct r600_resource
*)rtex
->flushed_depth_texture
)->buf
;
806 buf
= ((struct r600_resource
*)transfer
->resource
)->buf
;
809 rctx
->ws
->buffer_unmap(buf
);
812 void r600_init_surface_functions(struct r600_context
*r600
)
814 r600
->context
.create_surface
= r600_create_surface
;
815 r600
->context
.surface_destroy
= r600_surface_destroy
;
818 static unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format
,
819 const unsigned char *swizzle_view
)
822 unsigned char swizzle
[4];
824 const uint32_t swizzle_shift
[4] = {
827 const uint32_t swizzle_bit
[4] = {
832 util_format_compose_swizzles(swizzle_format
, swizzle_view
, swizzle
);
834 memcpy(swizzle
, swizzle_format
, 4);
838 for (i
= 0; i
< 4; i
++) {
839 switch (swizzle
[i
]) {
840 case UTIL_FORMAT_SWIZZLE_Y
:
841 result
|= swizzle_bit
[1] << swizzle_shift
[i
];
843 case UTIL_FORMAT_SWIZZLE_Z
:
844 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
846 case UTIL_FORMAT_SWIZZLE_W
:
847 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
849 case UTIL_FORMAT_SWIZZLE_0
:
850 result
|= V_038010_SQ_SEL_0
<< swizzle_shift
[i
];
852 case UTIL_FORMAT_SWIZZLE_1
:
853 result
|= V_038010_SQ_SEL_1
<< swizzle_shift
[i
];
855 default: /* UTIL_FORMAT_SWIZZLE_X */
856 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
862 /* texture format translate */
863 uint32_t r600_translate_texformat(struct pipe_screen
*screen
,
864 enum pipe_format format
,
865 const unsigned char *swizzle_view
,
866 uint32_t *word4_p
, uint32_t *yuv_format_p
)
868 uint32_t result
= 0, word4
= 0, yuv_format
= 0;
869 const struct util_format_description
*desc
;
870 boolean uniform
= TRUE
;
871 static int r600_enable_s3tc
= -1;
872 bool is_srgb_valid
= FALSE
;
875 const uint32_t sign_bit
[4] = {
876 S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED
),
877 S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED
),
878 S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED
),
879 S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED
)
881 desc
= util_format_description(format
);
883 word4
|= r600_get_swizzle_combined(desc
->swizzle
, swizzle_view
);
885 /* Colorspace (return non-RGB formats directly). */
886 switch (desc
->colorspace
) {
887 /* Depth stencil formats */
888 case UTIL_FORMAT_COLORSPACE_ZS
:
890 case PIPE_FORMAT_Z16_UNORM
:
893 case PIPE_FORMAT_X24S8_UINT
:
894 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
895 case PIPE_FORMAT_Z24X8_UNORM
:
896 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
899 case PIPE_FORMAT_S8X24_UINT
:
900 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
901 case PIPE_FORMAT_X8Z24_UNORM
:
902 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
905 case PIPE_FORMAT_S8_UINT
:
907 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
909 case PIPE_FORMAT_Z32_FLOAT
:
910 result
= FMT_32_FLOAT
;
912 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
913 result
= FMT_X24_8_32_FLOAT
;
919 case UTIL_FORMAT_COLORSPACE_YUV
:
920 yuv_format
|= (1 << 30);
922 case PIPE_FORMAT_UYVY
:
923 case PIPE_FORMAT_YUYV
:
927 goto out_unknown
; /* TODO */
929 case UTIL_FORMAT_COLORSPACE_SRGB
:
930 word4
|= S_038010_FORCE_DEGAMMA(1);
937 if (r600_enable_s3tc
== -1) {
938 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
939 if (rscreen
->info
.drm_minor
>= 9)
940 r600_enable_s3tc
= 1;
942 r600_enable_s3tc
= debug_get_bool_option("R600_ENABLE_S3TC", FALSE
);
945 if (desc
->layout
== UTIL_FORMAT_LAYOUT_RGTC
) {
946 if (!r600_enable_s3tc
)
950 case PIPE_FORMAT_RGTC1_SNORM
:
951 case PIPE_FORMAT_LATC1_SNORM
:
952 word4
|= sign_bit
[0];
953 case PIPE_FORMAT_RGTC1_UNORM
:
954 case PIPE_FORMAT_LATC1_UNORM
:
957 case PIPE_FORMAT_RGTC2_SNORM
:
958 case PIPE_FORMAT_LATC2_SNORM
:
959 word4
|= sign_bit
[0] | sign_bit
[1];
960 case PIPE_FORMAT_RGTC2_UNORM
:
961 case PIPE_FORMAT_LATC2_UNORM
:
969 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
971 if (!r600_enable_s3tc
)
974 if (!util_format_s3tc_enabled
) {
979 case PIPE_FORMAT_DXT1_RGB
:
980 case PIPE_FORMAT_DXT1_RGBA
:
981 case PIPE_FORMAT_DXT1_SRGB
:
982 case PIPE_FORMAT_DXT1_SRGBA
:
984 is_srgb_valid
= TRUE
;
986 case PIPE_FORMAT_DXT3_RGBA
:
987 case PIPE_FORMAT_DXT3_SRGBA
:
989 is_srgb_valid
= TRUE
;
991 case PIPE_FORMAT_DXT5_RGBA
:
992 case PIPE_FORMAT_DXT5_SRGBA
:
994 is_srgb_valid
= TRUE
;
1001 if (format
== PIPE_FORMAT_R9G9B9E5_FLOAT
) {
1002 result
= FMT_5_9_9_9_SHAREDEXP
;
1004 } else if (format
== PIPE_FORMAT_R11G11B10_FLOAT
) {
1005 result
= FMT_10_11_11_FLOAT
;
1010 for (i
= 0; i
< desc
->nr_channels
; i
++) {
1011 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
1012 word4
|= sign_bit
[i
];
1016 /* R8G8Bx_SNORM - TODO CxV8U8 */
1018 /* See whether the components are of the same size. */
1019 for (i
= 1; i
< desc
->nr_channels
; i
++) {
1020 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
1023 /* Non-uniform formats. */
1025 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
&&
1026 desc
->channel
[0].pure_integer
)
1027 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
1028 switch(desc
->nr_channels
) {
1030 if (desc
->channel
[0].size
== 5 &&
1031 desc
->channel
[1].size
== 6 &&
1032 desc
->channel
[2].size
== 5) {
1038 if (desc
->channel
[0].size
== 5 &&
1039 desc
->channel
[1].size
== 5 &&
1040 desc
->channel
[2].size
== 5 &&
1041 desc
->channel
[3].size
== 1) {
1042 result
= FMT_1_5_5_5
;
1045 if (desc
->channel
[0].size
== 10 &&
1046 desc
->channel
[1].size
== 10 &&
1047 desc
->channel
[2].size
== 10 &&
1048 desc
->channel
[3].size
== 2) {
1049 result
= FMT_2_10_10_10
;
1057 /* Find the first non-VOID channel. */
1058 for (i
= 0; i
< 4; i
++) {
1059 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
1067 /* uniform formats */
1068 switch (desc
->channel
[i
].type
) {
1069 case UTIL_FORMAT_TYPE_UNSIGNED
:
1070 case UTIL_FORMAT_TYPE_SIGNED
:
1072 if (!desc
->channel
[i
].normalized
&&
1073 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
1077 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
&&
1078 desc
->channel
[i
].pure_integer
)
1079 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
1081 switch (desc
->channel
[i
].size
) {
1083 switch (desc
->nr_channels
) {
1088 result
= FMT_4_4_4_4
;
1093 switch (desc
->nr_channels
) {
1101 result
= FMT_8_8_8_8
;
1102 is_srgb_valid
= TRUE
;
1107 switch (desc
->nr_channels
) {
1115 result
= FMT_16_16_16_16
;
1120 switch (desc
->nr_channels
) {
1128 result
= FMT_32_32_32_32
;
1134 case UTIL_FORMAT_TYPE_FLOAT
:
1135 switch (desc
->channel
[i
].size
) {
1137 switch (desc
->nr_channels
) {
1139 result
= FMT_16_FLOAT
;
1142 result
= FMT_16_16_FLOAT
;
1145 result
= FMT_16_16_16_16_FLOAT
;
1150 switch (desc
->nr_channels
) {
1152 result
= FMT_32_FLOAT
;
1155 result
= FMT_32_32_FLOAT
;
1158 result
= FMT_32_32_32_32_FLOAT
;
1167 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
&& !is_srgb_valid
)
1172 *yuv_format_p
= yuv_format
;
1175 /* R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format)); */