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_math.h>
31 #include <util/u_inlines.h>
32 #include <util/u_memory.h>
33 #include "state_tracker/drm_driver.h"
34 #include "pipebuffer/pb_buffer.h"
35 #include "r600_pipe.h"
36 #include "r600_resource.h"
37 #include "r600_state_inlines.h"
39 #include "r600_formats.h"
41 extern struct u_resource_vtbl r600_texture_vtbl
;
43 /* Copy from a full GPU texture to a transfer's staging one. */
44 static void r600_copy_to_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
46 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
47 struct pipe_resource
*texture
= transfer
->resource
;
49 ctx
->resource_copy_region(ctx
, rtransfer
->staging_texture
,
50 0, 0, 0, 0, texture
, transfer
->level
,
55 /* Copy from a transfer's staging texture to a full GPU one. */
56 static void r600_copy_from_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
58 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
59 struct pipe_resource
*texture
= transfer
->resource
;
62 sbox
.x
= sbox
.y
= sbox
.z
= 0;
63 sbox
.width
= transfer
->box
.width
;
64 sbox
.height
= transfer
->box
.height
;
65 /* XXX that might be wrong */
67 ctx
->resource_copy_region(ctx
, texture
, transfer
->level
,
68 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
69 rtransfer
->staging_texture
,
72 ctx
->flush(ctx
, 0, NULL
);
75 unsigned r600_texture_get_offset(struct r600_resource_texture
*rtex
,
76 unsigned level
, unsigned layer
)
78 unsigned offset
= rtex
->offset
[level
];
80 switch (rtex
->resource
.base
.b
.target
) {
82 case PIPE_TEXTURE_CUBE
:
83 return offset
+ layer
* rtex
->layer_size
[level
];
90 static unsigned r600_get_pixel_alignment(struct pipe_screen
*screen
,
91 enum pipe_format format
,
94 struct r600_screen
* rscreen
= (struct r600_screen
*)screen
;
95 unsigned pixsize
= util_format_get_blocksize(format
);
99 case V_038000_ARRAY_1D_TILED_THIN1
:
101 ((rscreen
->tiling_info
->group_bytes
/ 8 / pixsize
)));
103 case V_038000_ARRAY_2D_TILED_THIN1
:
104 p_align
= MAX2(rscreen
->tiling_info
->num_banks
,
105 (((rscreen
->tiling_info
->group_bytes
/ 8 / pixsize
)) *
106 rscreen
->tiling_info
->num_banks
)) * 8;
108 case V_038000_ARRAY_LINEAR_GENERAL
:
110 p_align
= rscreen
->tiling_info
->group_bytes
/ pixsize
;
116 static unsigned r600_get_height_alignment(struct pipe_screen
*screen
,
119 struct r600_screen
* rscreen
= (struct r600_screen
*)screen
;
122 switch (array_mode
) {
123 case V_038000_ARRAY_2D_TILED_THIN1
:
124 h_align
= rscreen
->tiling_info
->num_channels
* 8;
126 case V_038000_ARRAY_1D_TILED_THIN1
:
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_pixel_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
:
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_stride(struct pipe_screen
*screen
,
169 struct r600_resource_texture
*rtex
,
172 struct pipe_resource
*ptex
= &rtex
->resource
.base
.b
;
173 unsigned width
, stride
, tile_width
;
175 if (rtex
->pitch_override
)
176 return rtex
->pitch_override
;
178 width
= mip_minify(ptex
->width0
, level
);
179 if (util_format_is_plain(ptex
->format
)) {
180 tile_width
= r600_get_pixel_alignment(screen
, ptex
->format
,
181 rtex
->array_mode
[level
]);
182 width
= align(width
, tile_width
);
184 stride
= util_format_get_stride(ptex
->format
, width
);
189 static unsigned r600_texture_get_nblocksy(struct pipe_screen
*screen
,
190 struct r600_resource_texture
*rtex
,
193 struct pipe_resource
*ptex
= &rtex
->resource
.base
.b
;
194 unsigned height
, tile_height
;
196 height
= mip_minify(ptex
->height0
, level
);
197 if (util_format_is_plain(ptex
->format
)) {
198 tile_height
= r600_get_height_alignment(screen
,
199 rtex
->array_mode
[level
]);
200 height
= align(height
, tile_height
);
202 return util_format_get_nblocksy(ptex
->format
, height
);
205 /* Get a width in pixels from a stride in bytes. */
206 static unsigned pitch_to_width(enum pipe_format format
, unsigned pitch_in_bytes
)
208 return (pitch_in_bytes
/ util_format_get_blocksize(format
)) *
209 util_format_get_blockwidth(format
);
212 static void r600_texture_set_array_mode(struct pipe_screen
*screen
,
213 struct r600_resource_texture
*rtex
,
214 unsigned level
, unsigned array_mode
)
216 struct pipe_resource
*ptex
= &rtex
->resource
.base
.b
;
218 switch (array_mode
) {
219 case V_0280A0_ARRAY_LINEAR_GENERAL
:
220 case V_0280A0_ARRAY_LINEAR_ALIGNED
:
221 case V_0280A0_ARRAY_1D_TILED_THIN1
:
223 rtex
->array_mode
[level
] = array_mode
;
225 case V_0280A0_ARRAY_2D_TILED_THIN1
:
227 unsigned w
, h
, tile_height
, tile_width
;
229 tile_height
= r600_get_height_alignment(screen
, array_mode
);
230 tile_width
= r600_get_pixel_alignment(screen
, ptex
->format
, array_mode
);
232 w
= mip_minify(ptex
->width0
, level
);
233 h
= mip_minify(ptex
->height0
, level
);
234 if (w
< tile_width
|| h
< tile_height
)
235 rtex
->array_mode
[level
] = V_0280A0_ARRAY_1D_TILED_THIN1
;
237 rtex
->array_mode
[level
] = array_mode
;
243 static void r600_setup_miptree(struct pipe_screen
*screen
,
244 struct r600_resource_texture
*rtex
,
247 struct pipe_resource
*ptex
= &rtex
->resource
.base
.b
;
248 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
249 enum chip_class chipc
= r600_get_family_class(radeon
);
250 unsigned pitch
, size
, layer_size
, i
, offset
;
253 for (i
= 0, offset
= 0; i
<= ptex
->last_level
; i
++) {
254 r600_texture_set_array_mode(screen
, rtex
, i
, array_mode
);
256 pitch
= r600_texture_get_stride(screen
, rtex
, i
);
257 nblocksy
= r600_texture_get_nblocksy(screen
, rtex
, i
);
259 layer_size
= pitch
* nblocksy
;
261 if (ptex
->target
== PIPE_TEXTURE_CUBE
) {
263 size
= layer_size
* 8;
265 size
= layer_size
* 6;
268 size
= layer_size
* u_minify(ptex
->depth0
, i
);
269 /* align base image and start of miptree */
270 if ((i
== 0) || (i
== 1))
271 offset
= align(offset
, r600_get_base_alignment(screen
, ptex
->format
, array_mode
));
272 rtex
->offset
[i
] = offset
;
273 rtex
->layer_size
[i
] = layer_size
;
274 rtex
->pitch_in_bytes
[i
] = pitch
;
275 rtex
->pitch_in_pixels
[i
] = pitch_to_width(ptex
->format
, pitch
);
281 static struct r600_resource_texture
*
282 r600_texture_create_object(struct pipe_screen
*screen
,
283 const struct pipe_resource
*base
,
285 unsigned pitch_in_bytes_override
,
286 unsigned max_buffer_size
,
289 struct r600_resource_texture
*rtex
;
290 struct r600_resource
*resource
;
291 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
293 rtex
= CALLOC_STRUCT(r600_resource_texture
);
297 resource
= &rtex
->resource
;
298 resource
->base
.b
= *base
;
299 resource
->base
.vtbl
= &r600_texture_vtbl
;
300 pipe_reference_init(&resource
->base
.b
.reference
, 1);
301 resource
->base
.b
.screen
= screen
;
303 rtex
->pitch_override
= pitch_in_bytes_override
;
307 r600_setup_miptree(screen
, rtex
, array_mode
);
309 resource
->size
= rtex
->size
;
312 struct pipe_resource
*ptex
= &rtex
->resource
.base
.b
;
313 int base_align
= r600_get_base_alignment(screen
, ptex
->format
, array_mode
);
315 resource
->bo
= r600_bo(radeon
, rtex
->size
, base_align
, base
->bind
, base
->usage
);
324 struct pipe_resource
*r600_texture_create(struct pipe_screen
*screen
,
325 const struct pipe_resource
*templ
)
327 unsigned array_mode
= 0;
328 static int force_tiling
= -1;
330 /* Would like some magic "get_bool_option_once" routine.
332 if (force_tiling
== -1)
333 force_tiling
= debug_get_bool_option("R600_FORCE_TILING", FALSE
);
336 if (!(templ
->flags
& R600_RESOURCE_FLAG_TRANSFER
) &&
337 !(templ
->bind
& PIPE_BIND_SCANOUT
)) {
338 array_mode
= V_038000_ARRAY_2D_TILED_THIN1
;
342 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
, array_mode
,
347 static void r600_texture_destroy(struct pipe_screen
*screen
,
348 struct pipe_resource
*ptex
)
350 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)ptex
;
351 struct r600_resource
*resource
= &rtex
->resource
;
352 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
354 if (rtex
->flushed_depth_texture
)
355 pipe_resource_reference((struct pipe_resource
**)&rtex
->flushed_depth_texture
, NULL
);
358 r600_bo_reference(radeon
, &resource
->bo
, NULL
);
363 static boolean
r600_texture_get_handle(struct pipe_screen
* screen
,
364 struct pipe_resource
*ptex
,
365 struct winsys_handle
*whandle
)
367 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)ptex
;
368 struct r600_resource
*resource
= &rtex
->resource
;
369 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
371 return r600_bo_get_winsys_handle(radeon
, resource
->bo
,
372 rtex
->pitch_in_bytes
[0], whandle
);
375 static struct pipe_surface
*r600_create_surface(struct pipe_context
*pipe
,
376 struct pipe_resource
*texture
,
377 const struct pipe_surface
*surf_tmpl
)
379 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
380 struct r600_surface
*surface
= CALLOC_STRUCT(r600_surface
);
381 unsigned tile_height
;
382 unsigned level
= surf_tmpl
->u
.tex
.level
;
384 assert(surf_tmpl
->u
.tex
.first_layer
== surf_tmpl
->u
.tex
.last_layer
);
388 /* offset = r600_texture_get_offset(rtex, level, surf_tmpl->u.tex.first_layer);*/
389 pipe_reference_init(&surface
->base
.reference
, 1);
390 pipe_resource_reference(&surface
->base
.texture
, texture
);
391 surface
->base
.context
= pipe
;
392 surface
->base
.format
= surf_tmpl
->format
;
393 surface
->base
.width
= mip_minify(texture
->width0
, level
);
394 surface
->base
.height
= mip_minify(texture
->height0
, level
);
395 surface
->base
.usage
= surf_tmpl
->usage
;
396 surface
->base
.texture
= texture
;
397 surface
->base
.u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
398 surface
->base
.u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
399 surface
->base
.u
.tex
.level
= level
;
401 tile_height
= r600_get_height_alignment(pipe
->screen
, rtex
->array_mode
[level
]);
402 surface
->aligned_height
= align(surface
->base
.height
, tile_height
);
403 return &surface
->base
;
406 static void r600_surface_destroy(struct pipe_context
*pipe
,
407 struct pipe_surface
*surface
)
409 pipe_resource_reference(&surface
->texture
, NULL
);
414 struct pipe_resource
*r600_texture_from_handle(struct pipe_screen
*screen
,
415 const struct pipe_resource
*templ
,
416 struct winsys_handle
*whandle
)
418 struct radeon
*rw
= (struct radeon
*)screen
->winsys
;
419 struct r600_bo
*bo
= NULL
;
420 unsigned array_mode
= 0;
422 /* Support only 2D textures without mipmaps */
423 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
) ||
424 templ
->depth0
!= 1 || templ
->last_level
!= 0)
427 bo
= r600_bo_handle(rw
, whandle
->handle
, &array_mode
);
432 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
, array_mode
,
438 static unsigned int r600_texture_is_referenced(struct pipe_context
*context
,
439 struct pipe_resource
*texture
,
440 unsigned level
, int layer
)
443 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
446 int r600_texture_depth_flush(struct pipe_context
*ctx
,
447 struct pipe_resource
*texture
)
449 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
450 struct pipe_resource resource
;
452 if (rtex
->flushed_depth_texture
)
455 resource
.target
= PIPE_TEXTURE_2D
;
456 resource
.format
= texture
->format
;
457 resource
.width0
= texture
->width0
;
458 resource
.height0
= texture
->height0
;
460 resource
.last_level
= 0;
461 resource
.nr_samples
= 0;
462 resource
.usage
= PIPE_USAGE_DYNAMIC
;
464 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
;
466 resource
.bind
|= PIPE_BIND_DEPTH_STENCIL
;
468 rtex
->flushed_depth_texture
= (struct r600_resource_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
469 if (rtex
->flushed_depth_texture
== NULL
) {
470 R600_ERR("failed to create temporary texture to hold untiled copy\n");
475 /* XXX: only do this if the depth texture has actually changed:
477 r600_blit_uncompress_depth(ctx
, rtex
);
481 /* Needs adjustment for pixelformat:
483 static INLINE
unsigned u_box_volume( const struct pipe_box
*box
)
485 return box
->width
* box
->depth
* box
->height
;
489 /* Figure out whether u_blitter will fallback to a transfer operation.
490 * If so, don't use a staging resource.
492 static boolean
permit_hardware_blit(struct pipe_screen
*screen
,
493 struct pipe_resource
*res
)
497 if (util_format_is_depth_or_stencil(res
->format
))
498 bind
= PIPE_BIND_DEPTH_STENCIL
;
500 bind
= PIPE_BIND_RENDER_TARGET
;
502 /* See r600_resource_copy_region: there is something wrong
503 * with depth resource copies at the moment so avoid them for
506 if (util_format_get_component_bits(res
->format
,
507 UTIL_FORMAT_COLORSPACE_ZS
,
511 if (!screen
->is_format_supported(screen
,
518 if (!screen
->is_format_supported(screen
,
522 PIPE_BIND_SAMPLER_VIEW
, 0))
528 struct pipe_transfer
* r600_texture_get_transfer(struct pipe_context
*ctx
,
529 struct pipe_resource
*texture
,
532 const struct pipe_box
*box
)
534 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
535 struct pipe_resource resource
;
536 struct r600_transfer
*trans
;
538 boolean use_staging_texture
= FALSE
;
540 /* We cannot map a tiled texture directly because the data is
541 * in a different order, therefore we do detiling using a blit.
543 * Also, use a temporary in GTT memory for read transfers, as
544 * the CPU is much happier reading out of cached system memory
545 * than uncached VRAM.
548 use_staging_texture
= TRUE
;
550 if ((usage
& PIPE_TRANSFER_READ
) && u_box_volume(box
) > 1024)
551 use_staging_texture
= TRUE
;
553 /* XXX: Use a staging texture for uploads if the underlying BO
554 * is busy. No interface for checking that currently? so do
555 * it eagerly whenever the transfer doesn't require a readback
558 if ((usage
& PIPE_TRANSFER_WRITE
) &&
559 !(usage
& (PIPE_TRANSFER_READ
|
560 PIPE_TRANSFER_DONTBLOCK
|
561 PIPE_TRANSFER_UNSYNCHRONIZED
)))
562 use_staging_texture
= TRUE
;
564 if (!permit_hardware_blit(ctx
->screen
, texture
) ||
565 (texture
->flags
& R600_RESOURCE_FLAG_TRANSFER
))
566 use_staging_texture
= FALSE
;
568 trans
= CALLOC_STRUCT(r600_transfer
);
571 pipe_resource_reference(&trans
->transfer
.resource
, texture
);
572 trans
->transfer
.level
= level
;
573 trans
->transfer
.usage
= usage
;
574 trans
->transfer
.box
= *box
;
576 /* XXX: only readback the rectangle which is being mapped?
578 /* XXX: when discard is true, no need to read back from depth texture
580 r
= r600_texture_depth_flush(ctx
, texture
);
582 R600_ERR("failed to create temporary texture to hold untiled copy\n");
583 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
587 } else if (use_staging_texture
) {
588 resource
.target
= PIPE_TEXTURE_2D
;
589 resource
.format
= texture
->format
;
590 resource
.width0
= box
->width
;
591 resource
.height0
= box
->height
;
593 resource
.array_size
= 1;
594 resource
.last_level
= 0;
595 resource
.nr_samples
= 0;
596 resource
.usage
= PIPE_USAGE_STAGING
;
598 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
;
599 /* For texture reading, the temporary (detiled) texture is used as
600 * a render target when blitting from a tiled texture. */
601 if (usage
& PIPE_TRANSFER_READ
) {
602 resource
.bind
|= PIPE_BIND_RENDER_TARGET
;
604 /* For texture writing, the temporary texture is used as a sampler
605 * when blitting into a tiled texture. */
606 if (usage
& PIPE_TRANSFER_WRITE
) {
607 resource
.bind
|= PIPE_BIND_SAMPLER_VIEW
;
609 /* Create the temporary texture. */
610 trans
->staging_texture
= ctx
->screen
->resource_create(ctx
->screen
, &resource
);
611 if (trans
->staging_texture
== NULL
) {
612 R600_ERR("failed to create temporary texture to hold untiled copy\n");
613 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
618 trans
->transfer
.stride
=
619 ((struct r600_resource_texture
*)trans
->staging_texture
)->pitch_in_bytes
[0];
620 if (usage
& PIPE_TRANSFER_READ
) {
621 r600_copy_to_staging_texture(ctx
, trans
);
622 /* Always referenced in the blit. */
623 ctx
->flush(ctx
, 0, NULL
);
625 return &trans
->transfer
;
627 trans
->transfer
.stride
= rtex
->pitch_in_bytes
[level
];
628 trans
->offset
= r600_texture_get_offset(rtex
, level
, box
->z
);
629 return &trans
->transfer
;
632 void r600_texture_transfer_destroy(struct pipe_context
*ctx
,
633 struct pipe_transfer
*transfer
)
635 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
636 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
638 if (rtransfer
->staging_texture
) {
639 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
640 r600_copy_from_staging_texture(ctx
, rtransfer
);
642 pipe_resource_reference(&rtransfer
->staging_texture
, NULL
);
644 if (rtex
->flushed_depth_texture
) {
645 pipe_resource_reference((struct pipe_resource
**)&rtex
->flushed_depth_texture
, NULL
);
647 pipe_resource_reference(&transfer
->resource
, NULL
);
651 void* r600_texture_transfer_map(struct pipe_context
*ctx
,
652 struct pipe_transfer
* transfer
)
654 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
656 enum pipe_format format
= transfer
->resource
->format
;
657 struct radeon
*radeon
= (struct radeon
*)ctx
->screen
->winsys
;
662 if (rtransfer
->staging_texture
) {
663 bo
= ((struct r600_resource
*)rtransfer
->staging_texture
)->bo
;
665 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
667 if (rtex
->flushed_depth_texture
)
668 bo
= ((struct r600_resource
*)rtex
->flushed_depth_texture
)->bo
;
670 bo
= ((struct r600_resource
*)transfer
->resource
)->bo
;
672 offset
= rtransfer
->offset
+
673 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
674 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
677 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
678 usage
|= PB_USAGE_CPU_WRITE
;
680 if (transfer
->usage
& PIPE_TRANSFER_DISCARD
) {
683 if (transfer
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
) {
687 if (transfer
->usage
& PIPE_TRANSFER_READ
) {
688 usage
|= PB_USAGE_CPU_READ
;
691 if (transfer
->usage
& PIPE_TRANSFER_DONTBLOCK
) {
692 usage
|= PB_USAGE_DONTBLOCK
;
695 if (transfer
->usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) {
696 usage
|= PB_USAGE_UNSYNCHRONIZED
;
699 map
= r600_bo_map(radeon
, bo
, usage
, ctx
);
707 void r600_texture_transfer_unmap(struct pipe_context
*ctx
,
708 struct pipe_transfer
* transfer
)
710 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
711 struct radeon
*radeon
= (struct radeon
*)ctx
->screen
->winsys
;
714 if (rtransfer
->staging_texture
) {
715 bo
= ((struct r600_resource
*)rtransfer
->staging_texture
)->bo
;
717 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
719 if (rtex
->flushed_depth_texture
) {
720 bo
= ((struct r600_resource
*)rtex
->flushed_depth_texture
)->bo
;
722 bo
= ((struct r600_resource
*)transfer
->resource
)->bo
;
725 r600_bo_unmap(radeon
, bo
);
728 struct u_resource_vtbl r600_texture_vtbl
=
730 r600_texture_get_handle
, /* get_handle */
731 r600_texture_destroy
, /* resource_destroy */
732 r600_texture_is_referenced
, /* is_resource_referenced */
733 r600_texture_get_transfer
, /* get_transfer */
734 r600_texture_transfer_destroy
, /* transfer_destroy */
735 r600_texture_transfer_map
, /* transfer_map */
736 u_default_transfer_flush_region
,/* transfer_flush_region */
737 r600_texture_transfer_unmap
, /* transfer_unmap */
738 u_default_transfer_inline_write
/* transfer_inline_write */
741 void r600_init_surface_functions(struct r600_pipe_context
*r600
)
743 r600
->context
.create_surface
= r600_create_surface
;
744 r600
->context
.surface_destroy
= r600_surface_destroy
;
747 static unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format
,
748 const unsigned char *swizzle_view
)
751 unsigned char swizzle
[4];
753 const uint32_t swizzle_shift
[4] = {
756 const uint32_t swizzle_bit
[4] = {
761 /* Combine two sets of swizzles. */
762 for (i
= 0; i
< 4; i
++) {
763 swizzle
[i
] = swizzle_view
[i
] <= UTIL_FORMAT_SWIZZLE_W
?
764 swizzle_format
[swizzle_view
[i
]] : swizzle_view
[i
];
767 memcpy(swizzle
, swizzle_format
, 4);
771 for (i
= 0; i
< 4; i
++) {
772 switch (swizzle
[i
]) {
773 case UTIL_FORMAT_SWIZZLE_Y
:
774 result
|= swizzle_bit
[1] << swizzle_shift
[i
];
776 case UTIL_FORMAT_SWIZZLE_Z
:
777 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
779 case UTIL_FORMAT_SWIZZLE_W
:
780 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
782 case UTIL_FORMAT_SWIZZLE_0
:
783 result
|= V_038010_SQ_SEL_0
<< swizzle_shift
[i
];
785 case UTIL_FORMAT_SWIZZLE_1
:
786 result
|= V_038010_SQ_SEL_1
<< swizzle_shift
[i
];
788 default: /* UTIL_FORMAT_SWIZZLE_X */
789 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
795 /* texture format translate */
796 uint32_t r600_translate_texformat(enum pipe_format format
,
797 const unsigned char *swizzle_view
,
798 uint32_t *word4_p
, uint32_t *yuv_format_p
)
800 uint32_t result
= 0, word4
= 0, yuv_format
= 0;
801 const struct util_format_description
*desc
;
802 boolean uniform
= TRUE
;
804 const uint32_t sign_bit
[4] = {
805 S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED
),
806 S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED
),
807 S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED
),
808 S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED
)
810 desc
= util_format_description(format
);
812 word4
|= r600_get_swizzle_combined(desc
->swizzle
, swizzle_view
);
814 /* Colorspace (return non-RGB formats directly). */
815 switch (desc
->colorspace
) {
816 /* Depth stencil formats */
817 case UTIL_FORMAT_COLORSPACE_ZS
:
819 case PIPE_FORMAT_Z16_UNORM
:
822 case PIPE_FORMAT_X24S8_USCALED
:
823 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
824 case PIPE_FORMAT_Z24X8_UNORM
:
825 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
828 case PIPE_FORMAT_S8X24_USCALED
:
829 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
830 case PIPE_FORMAT_X8Z24_UNORM
:
831 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
834 case PIPE_FORMAT_S8_USCALED
:
836 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
842 case UTIL_FORMAT_COLORSPACE_YUV
:
843 yuv_format
|= (1 << 30);
845 case PIPE_FORMAT_UYVY
:
846 case PIPE_FORMAT_YUYV
:
850 goto out_unknown
; /* TODO */
852 case UTIL_FORMAT_COLORSPACE_SRGB
:
853 word4
|= S_038010_FORCE_DEGAMMA(1);
854 if (format
== PIPE_FORMAT_L8A8_SRGB
|| format
== PIPE_FORMAT_L8_SRGB
)
855 goto out_unknown
; /* fails for some reason - TODO */
862 /* S3TC formats. TODO */
863 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
864 static int r600_enable_s3tc
= -1;
866 if (r600_enable_s3tc
== -1)
868 debug_get_bool_option("R600_ENABLE_S3TC", FALSE
);
870 if (!r600_enable_s3tc
)
874 case PIPE_FORMAT_DXT1_RGB
:
875 case PIPE_FORMAT_DXT1_RGBA
:
878 case PIPE_FORMAT_DXT3_RGBA
:
881 case PIPE_FORMAT_DXT5_RGBA
:
890 for (i
= 0; i
< desc
->nr_channels
; i
++) {
891 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
892 word4
|= sign_bit
[i
];
896 /* R8G8Bx_SNORM - TODO CxV8U8 */
900 /* See whether the components are of the same size. */
901 for (i
= 1; i
< desc
->nr_channels
; i
++) {
902 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
905 /* Non-uniform formats. */
907 switch(desc
->nr_channels
) {
909 if (desc
->channel
[0].size
== 5 &&
910 desc
->channel
[1].size
== 6 &&
911 desc
->channel
[2].size
== 5) {
917 if (desc
->channel
[0].size
== 5 &&
918 desc
->channel
[1].size
== 5 &&
919 desc
->channel
[2].size
== 5 &&
920 desc
->channel
[3].size
== 1) {
921 result
= FMT_1_5_5_5
;
924 if (desc
->channel
[0].size
== 10 &&
925 desc
->channel
[1].size
== 10 &&
926 desc
->channel
[2].size
== 10 &&
927 desc
->channel
[3].size
== 2) {
928 result
= FMT_10_10_10_2
;
936 /* Find the first non-VOID channel. */
937 for (i
= 0; i
< 4; i
++) {
938 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
946 /* uniform formats */
947 switch (desc
->channel
[i
].type
) {
948 case UTIL_FORMAT_TYPE_UNSIGNED
:
949 case UTIL_FORMAT_TYPE_SIGNED
:
950 if (!desc
->channel
[i
].normalized
&&
951 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
955 switch (desc
->channel
[i
].size
) {
957 switch (desc
->nr_channels
) {
962 result
= FMT_4_4_4_4
;
967 switch (desc
->nr_channels
) {
975 result
= FMT_8_8_8_8
;
980 switch (desc
->nr_channels
) {
988 result
= FMT_16_16_16_16
;
994 case UTIL_FORMAT_TYPE_FLOAT
:
995 switch (desc
->channel
[i
].size
) {
997 switch (desc
->nr_channels
) {
999 result
= FMT_16_FLOAT
;
1002 result
= FMT_16_16_FLOAT
;
1005 result
= FMT_16_16_16_16_FLOAT
;
1010 switch (desc
->nr_channels
) {
1012 result
= FMT_32_FLOAT
;
1015 result
= FMT_32_32_FLOAT
;
1018 result
= FMT_32_32_32_32_FLOAT
;
1028 *yuv_format_p
= yuv_format
;
1031 // R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format));