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
;
48 struct pipe_subresource subdst
;
52 ctx
->resource_copy_region(ctx
, rtransfer
->staging_texture
,
53 subdst
, 0, 0, 0, texture
, transfer
->sr
,
54 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
55 transfer
->box
.width
, transfer
->box
.height
);
59 /* Copy from a transfer's staging texture to a full GPU one. */
60 static void r600_copy_from_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
62 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
63 struct pipe_resource
*texture
= transfer
->resource
;
64 struct pipe_subresource subsrc
;
68 ctx
->resource_copy_region(ctx
, texture
, transfer
->sr
,
69 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
70 rtransfer
->staging_texture
, subsrc
,
72 transfer
->box
.width
, transfer
->box
.height
);
74 ctx
->flush(ctx
, 0, NULL
);
77 static unsigned r600_texture_get_offset(struct r600_resource_texture
*rtex
,
78 unsigned level
, unsigned zslice
,
81 unsigned offset
= rtex
->offset
[level
];
83 switch (rtex
->resource
.base
.b
.target
) {
86 return offset
+ zslice
* rtex
->layer_size
[level
];
87 case PIPE_TEXTURE_CUBE
:
89 return offset
+ face
* rtex
->layer_size
[level
];
91 assert(zslice
== 0 && face
== 0);
96 static unsigned r600_get_pixel_alignment(struct pipe_screen
*screen
,
97 enum pipe_format format
,
100 struct r600_screen
* rscreen
= (struct r600_screen
*)screen
;
101 unsigned pixsize
= util_format_get_blocksize(format
);
105 case V_038000_ARRAY_1D_TILED_THIN1
:
107 ((rscreen
->tiling_info
->group_bytes
/ 8 / pixsize
)));
109 case V_038000_ARRAY_2D_TILED_THIN1
:
110 p_align
= MAX2(rscreen
->tiling_info
->num_banks
,
111 (((rscreen
->tiling_info
->group_bytes
/ 8 / pixsize
)) *
112 rscreen
->tiling_info
->num_banks
)) * 8;
114 case V_038000_ARRAY_LINEAR_GENERAL
:
116 p_align
= rscreen
->tiling_info
->group_bytes
/ pixsize
;
122 static unsigned r600_get_height_alignment(struct pipe_screen
*screen
,
125 struct r600_screen
* rscreen
= (struct r600_screen
*)screen
;
128 switch (array_mode
) {
129 case V_038000_ARRAY_2D_TILED_THIN1
:
130 h_align
= rscreen
->tiling_info
->num_channels
* 8;
132 case V_038000_ARRAY_1D_TILED_THIN1
:
142 static unsigned r600_get_base_alignment(struct pipe_screen
*screen
,
143 enum pipe_format format
,
146 struct r600_screen
* rscreen
= (struct r600_screen
*)screen
;
147 unsigned pixsize
= util_format_get_blocksize(format
);
148 int p_align
= r600_get_pixel_alignment(screen
, format
, array_mode
);
149 int h_align
= r600_get_height_alignment(screen
, array_mode
);
152 switch (array_mode
) {
153 case V_038000_ARRAY_2D_TILED_THIN1
:
154 b_align
= MAX2(rscreen
->tiling_info
->num_banks
* rscreen
->tiling_info
->num_channels
* 8 * 8 * pixsize
,
155 p_align
* pixsize
* h_align
);
157 case V_038000_ARRAY_1D_TILED_THIN1
:
159 b_align
= rscreen
->tiling_info
->group_bytes
;
165 static unsigned mip_minify(unsigned size
, unsigned level
)
168 val
= u_minify(size
, level
);
170 val
= util_next_power_of_two(val
);
174 static unsigned r600_texture_get_stride(struct pipe_screen
*screen
,
175 struct r600_resource_texture
*rtex
,
178 struct r600_screen
* rscreen
= (struct r600_screen
*)screen
;
179 struct pipe_resource
*ptex
= &rtex
->resource
.base
.b
;
180 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
181 enum chip_class chipc
= r600_get_family_class(radeon
);
182 unsigned width
, stride
, tile_width
;
184 if (rtex
->pitch_override
)
185 return rtex
->pitch_override
;
187 width
= mip_minify(ptex
->width0
, level
);
188 if (util_format_is_plain(ptex
->format
)) {
189 tile_width
= r600_get_pixel_alignment(screen
, ptex
->format
,
190 rtex
->array_mode
[level
]);
191 width
= align(width
, tile_width
);
193 stride
= util_format_get_stride(ptex
->format
, width
);
198 static unsigned r600_texture_get_nblocksy(struct pipe_screen
*screen
,
199 struct r600_resource_texture
*rtex
,
202 struct pipe_resource
*ptex
= &rtex
->resource
.base
.b
;
203 unsigned height
, tile_height
;
205 height
= mip_minify(ptex
->height0
, level
);
206 if (util_format_is_plain(ptex
->format
)) {
207 tile_height
= r600_get_height_alignment(screen
,
208 rtex
->array_mode
[level
]);
209 height
= align(height
, tile_height
);
211 return util_format_get_nblocksy(ptex
->format
, height
);
214 /* Get a width in pixels from a stride in bytes. */
215 static unsigned pitch_to_width(enum pipe_format format
,
216 unsigned pitch_in_bytes
)
218 return (pitch_in_bytes
/ util_format_get_blocksize(format
)) *
219 util_format_get_blockwidth(format
);
222 static void r600_texture_set_array_mode(struct pipe_screen
*screen
,
223 struct r600_resource_texture
*rtex
,
224 unsigned level
, unsigned array_mode
)
226 struct pipe_resource
*ptex
= &rtex
->resource
.base
.b
;
228 switch (array_mode
) {
229 case V_0280A0_ARRAY_LINEAR_GENERAL
:
230 case V_0280A0_ARRAY_LINEAR_ALIGNED
:
231 case V_0280A0_ARRAY_1D_TILED_THIN1
:
233 rtex
->array_mode
[level
] = array_mode
;
235 case V_0280A0_ARRAY_2D_TILED_THIN1
:
237 unsigned w
, h
, tile_height
, tile_width
;
239 tile_height
= r600_get_height_alignment(screen
, array_mode
);
240 tile_width
= r600_get_pixel_alignment(screen
, ptex
->format
, array_mode
);
242 w
= mip_minify(ptex
->width0
, level
);
243 h
= mip_minify(ptex
->height0
, level
);
244 if (w
< tile_width
|| h
< tile_height
)
245 rtex
->array_mode
[level
] = V_0280A0_ARRAY_1D_TILED_THIN1
;
247 rtex
->array_mode
[level
] = array_mode
;
253 static void r600_setup_miptree(struct pipe_screen
*screen
,
254 struct r600_resource_texture
*rtex
,
257 struct pipe_resource
*ptex
= &rtex
->resource
.base
.b
;
258 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
259 enum chip_class chipc
= r600_get_family_class(radeon
);
260 unsigned pitch
, size
, layer_size
, i
, offset
;
263 for (i
= 0, offset
= 0; i
<= ptex
->last_level
; i
++) {
264 r600_texture_set_array_mode(screen
, rtex
, i
, array_mode
);
266 pitch
= r600_texture_get_stride(screen
, rtex
, i
);
267 nblocksy
= r600_texture_get_nblocksy(screen
, rtex
, i
);
269 layer_size
= pitch
* nblocksy
;
271 if (ptex
->target
== PIPE_TEXTURE_CUBE
) {
273 size
= layer_size
* 8;
275 size
= layer_size
* 6;
278 size
= layer_size
* u_minify(ptex
->depth0
, i
);
279 /* align base image and start of miptree */
280 if ((i
== 0) || (i
== 1))
281 offset
= align(offset
, r600_get_base_alignment(screen
, ptex
->format
, array_mode
));
282 rtex
->offset
[i
] = offset
;
283 rtex
->layer_size
[i
] = layer_size
;
284 rtex
->pitch_in_bytes
[i
] = pitch
;
285 rtex
->pitch_in_pixels
[i
] = pitch_to_width(ptex
->format
, pitch
);
291 static struct r600_resource_texture
*
292 r600_texture_create_object(struct pipe_screen
*screen
,
293 const struct pipe_resource
*base
,
295 unsigned pitch_in_bytes_override
,
296 unsigned max_buffer_size
,
299 struct r600_resource_texture
*rtex
;
300 struct r600_resource
*resource
;
301 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
303 rtex
= CALLOC_STRUCT(r600_resource_texture
);
307 resource
= &rtex
->resource
;
308 resource
->base
.b
= *base
;
309 resource
->base
.vtbl
= &r600_texture_vtbl
;
310 pipe_reference_init(&resource
->base
.b
.reference
, 1);
311 resource
->base
.b
.screen
= screen
;
313 rtex
->pitch_override
= pitch_in_bytes_override
;
317 r600_setup_miptree(screen
, rtex
, array_mode
);
319 resource
->size
= rtex
->size
;
322 struct pipe_resource
*ptex
= &rtex
->resource
.base
.b
;
323 int base_align
= r600_get_base_alignment(screen
, ptex
->format
, array_mode
);
325 resource
->bo
= r600_bo(radeon
, rtex
->size
, base_align
, base
->bind
, base
->usage
);
334 struct pipe_resource
*r600_texture_create(struct pipe_screen
*screen
,
335 const struct pipe_resource
*templ
)
337 unsigned array_mode
= 0;
338 static int force_tiling
= -1;
340 /* Would like some magic "get_bool_option_once" routine.
342 if (force_tiling
== -1)
343 force_tiling
= debug_get_bool_option("R600_FORCE_TILING", FALSE
);
346 if (!(templ
->flags
& R600_RESOURCE_FLAG_TRANSFER
) &&
347 !(templ
->bind
& PIPE_BIND_SCANOUT
)) {
348 array_mode
= V_038000_ARRAY_2D_TILED_THIN1
;
352 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
, array_mode
,
357 static void r600_texture_destroy(struct pipe_screen
*screen
,
358 struct pipe_resource
*ptex
)
360 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)ptex
;
361 struct r600_resource
*resource
= &rtex
->resource
;
362 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
364 if (rtex
->flushed_depth_texture
)
365 pipe_resource_reference((struct pipe_resource
**)&rtex
->flushed_depth_texture
, NULL
);
368 r600_bo_reference(radeon
, &resource
->bo
, NULL
);
373 static boolean
r600_texture_get_handle(struct pipe_screen
* screen
,
374 struct pipe_resource
*ptex
,
375 struct winsys_handle
*whandle
)
377 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)ptex
;
378 struct r600_resource
*resource
= &rtex
->resource
;
379 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
381 return r600_bo_get_winsys_handle(radeon
, resource
->bo
,
382 rtex
->pitch_in_bytes
[0], whandle
);
385 static struct pipe_surface
*r600_get_tex_surface(struct pipe_screen
*screen
,
386 struct pipe_resource
*texture
,
387 unsigned face
, unsigned level
,
388 unsigned zslice
, unsigned flags
)
390 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
391 struct r600_surface
*surface
= CALLOC_STRUCT(r600_surface
);
392 unsigned offset
, tile_height
;
396 offset
= r600_texture_get_offset(rtex
, level
, zslice
, face
);
397 pipe_reference_init(&surface
->base
.reference
, 1);
398 pipe_resource_reference(&surface
->base
.texture
, texture
);
399 surface
->base
.format
= texture
->format
;
400 surface
->base
.width
= mip_minify(texture
->width0
, level
);
401 surface
->base
.height
= mip_minify(texture
->height0
, level
);
402 surface
->base
.offset
= offset
;
403 surface
->base
.usage
= flags
;
404 surface
->base
.zslice
= zslice
;
405 surface
->base
.texture
= texture
;
406 surface
->base
.face
= face
;
407 surface
->base
.level
= level
;
409 tile_height
= r600_get_height_alignment(screen
, rtex
->array_mode
[level
]);
410 surface
->aligned_height
= align(surface
->base
.height
, tile_height
);
411 return &surface
->base
;
414 static void r600_tex_surface_destroy(struct pipe_surface
*surface
)
416 pipe_resource_reference(&surface
->texture
, NULL
);
421 struct pipe_resource
*r600_texture_from_handle(struct pipe_screen
*screen
,
422 const struct pipe_resource
*templ
,
423 struct winsys_handle
*whandle
)
425 struct radeon
*rw
= (struct radeon
*)screen
->winsys
;
426 struct r600_bo
*bo
= NULL
;
427 unsigned array_mode
= 0;
429 /* Support only 2D textures without mipmaps */
430 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
) ||
431 templ
->depth0
!= 1 || templ
->last_level
!= 0)
434 bo
= r600_bo_handle(rw
, whandle
->handle
, &array_mode
);
439 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
, array_mode
,
445 static unsigned int r600_texture_is_referenced(struct pipe_context
*context
,
446 struct pipe_resource
*texture
,
447 unsigned face
, unsigned level
)
450 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
453 int (*r600_blit_uncompress_depth_ptr
)(struct pipe_context
*ctx
, struct r600_resource_texture
*texture
);
455 int r600_texture_depth_flush(struct pipe_context
*ctx
,
456 struct pipe_resource
*texture
)
458 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
459 struct pipe_resource resource
;
461 if (rtex
->flushed_depth_texture
)
464 resource
.target
= PIPE_TEXTURE_2D
;
465 resource
.format
= texture
->format
;
466 resource
.width0
= texture
->width0
;
467 resource
.height0
= texture
->height0
;
469 resource
.last_level
= 0;
470 resource
.nr_samples
= 0;
471 resource
.usage
= PIPE_USAGE_DYNAMIC
;
473 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
;
475 resource
.bind
|= PIPE_BIND_DEPTH_STENCIL
;
477 rtex
->flushed_depth_texture
= (struct r600_resource_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
478 if (rtex
->flushed_depth_texture
== NULL
) {
479 R600_ERR("failed to create temporary texture to hold untiled copy\n");
484 /* XXX: only do this if the depth texture has actually changed:
486 r600_blit_uncompress_depth_ptr(ctx
, rtex
);
490 /* Needs adjustment for pixelformat:
492 static INLINE
unsigned u_box_volume( const struct pipe_box
*box
)
494 return box
->width
* box
->depth
* box
->height
;
498 /* Figure out whether u_blitter will fallback to a transfer operation.
499 * If so, don't use a staging resource.
501 static boolean
permit_hardware_blit(struct pipe_screen
*screen
,
502 struct pipe_resource
*res
)
506 if (util_format_is_depth_or_stencil(res
->format
))
507 bind
= PIPE_BIND_DEPTH_STENCIL
;
509 bind
= PIPE_BIND_RENDER_TARGET
;
511 /* See r600_resource_copy_region: there is something wrong
512 * with depth resource copies at the moment so avoid them for
515 if (util_format_get_component_bits(res
->format
,
516 UTIL_FORMAT_COLORSPACE_ZS
,
520 if (!screen
->is_format_supported(screen
,
527 if (!screen
->is_format_supported(screen
,
531 PIPE_BIND_SAMPLER_VIEW
, 0))
537 struct pipe_transfer
* r600_texture_get_transfer(struct pipe_context
*ctx
,
538 struct pipe_resource
*texture
,
539 struct pipe_subresource sr
,
541 const struct pipe_box
*box
)
543 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
544 struct pipe_resource resource
;
545 struct r600_transfer
*trans
;
547 boolean use_staging_texture
= FALSE
;
549 /* We cannot map a tiled texture directly because the data is
550 * in a different order, therefore we do detiling using a blit.
552 * Also, use a temporary in GTT memory for read transfers, as
553 * the CPU is much happier reading out of cached system memory
554 * than uncached VRAM.
557 use_staging_texture
= TRUE
;
559 if ((usage
& PIPE_TRANSFER_READ
) &&
560 u_box_volume(box
) > 1024)
561 use_staging_texture
= TRUE
;
563 /* XXX: Use a staging texture for uploads if the underlying BO
564 * is busy. No interface for checking that currently? so do
565 * it eagerly whenever the transfer doesn't require a readback
568 if ((usage
& PIPE_TRANSFER_WRITE
) &&
569 !(usage
& (PIPE_TRANSFER_READ
|
570 PIPE_TRANSFER_DONTBLOCK
|
571 PIPE_TRANSFER_UNSYNCHRONIZED
)))
572 use_staging_texture
= TRUE
;
574 if (!permit_hardware_blit(ctx
->screen
, texture
) ||
575 (texture
->flags
& R600_RESOURCE_FLAG_TRANSFER
))
576 use_staging_texture
= FALSE
;
578 trans
= CALLOC_STRUCT(r600_transfer
);
581 pipe_resource_reference(&trans
->transfer
.resource
, texture
);
582 trans
->transfer
.sr
= sr
;
583 trans
->transfer
.usage
= usage
;
584 trans
->transfer
.box
= *box
;
586 /* XXX: only readback the rectangle which is being mapped?
588 /* XXX: when discard is true, no need to read back from depth texture
590 r
= r600_texture_depth_flush(ctx
, texture
);
592 R600_ERR("failed to create temporary texture to hold untiled copy\n");
593 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
597 } else if (use_staging_texture
) {
598 resource
.target
= PIPE_TEXTURE_2D
;
599 resource
.format
= texture
->format
;
600 resource
.width0
= box
->width
;
601 resource
.height0
= box
->height
;
603 resource
.last_level
= 0;
604 resource
.nr_samples
= 0;
605 resource
.usage
= PIPE_USAGE_STAGING
;
607 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
;
608 /* For texture reading, the temporary (detiled) texture is used as
609 * a render target when blitting from a tiled texture. */
610 if (usage
& PIPE_TRANSFER_READ
) {
611 resource
.bind
|= PIPE_BIND_RENDER_TARGET
;
613 /* For texture writing, the temporary texture is used as a sampler
614 * when blitting into a tiled texture. */
615 if (usage
& PIPE_TRANSFER_WRITE
) {
616 resource
.bind
|= PIPE_BIND_SAMPLER_VIEW
;
618 /* Create the temporary texture. */
619 trans
->staging_texture
= ctx
->screen
->resource_create(ctx
->screen
, &resource
);
620 if (trans
->staging_texture
== NULL
) {
621 R600_ERR("failed to create temporary texture to hold untiled copy\n");
622 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
627 trans
->transfer
.stride
=
628 ((struct r600_resource_texture
*)trans
->staging_texture
)->pitch_in_bytes
[0];
629 if (usage
& PIPE_TRANSFER_READ
) {
630 r600_copy_to_staging_texture(ctx
, trans
);
631 /* Always referenced in the blit. */
632 ctx
->flush(ctx
, 0, NULL
);
634 return &trans
->transfer
;
636 trans
->transfer
.stride
= rtex
->pitch_in_bytes
[sr
.level
];
637 trans
->offset
= r600_texture_get_offset(rtex
, sr
.level
, box
->z
, sr
.face
);
638 return &trans
->transfer
;
641 void r600_texture_transfer_destroy(struct pipe_context
*ctx
,
642 struct pipe_transfer
*transfer
)
644 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
645 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
647 if (rtransfer
->staging_texture
) {
648 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
649 r600_copy_from_staging_texture(ctx
, rtransfer
);
651 pipe_resource_reference(&rtransfer
->staging_texture
, NULL
);
653 if (rtex
->flushed_depth_texture
) {
654 pipe_resource_reference((struct pipe_resource
**)&rtex
->flushed_depth_texture
, NULL
);
656 pipe_resource_reference(&transfer
->resource
, NULL
);
660 void* r600_texture_transfer_map(struct pipe_context
*ctx
,
661 struct pipe_transfer
* transfer
)
663 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
665 enum pipe_format format
= transfer
->resource
->format
;
666 struct radeon
*radeon
= (struct radeon
*)ctx
->screen
->winsys
;
671 if (rtransfer
->staging_texture
) {
672 bo
= ((struct r600_resource
*)rtransfer
->staging_texture
)->bo
;
674 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
676 if (rtex
->flushed_depth_texture
)
677 bo
= ((struct r600_resource
*)rtex
->flushed_depth_texture
)->bo
;
679 bo
= ((struct r600_resource
*)transfer
->resource
)->bo
;
681 offset
= rtransfer
->offset
+
682 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
683 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
686 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
687 usage
|= PB_USAGE_CPU_WRITE
;
689 if (transfer
->usage
& PIPE_TRANSFER_DISCARD
) {
692 if (transfer
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
) {
696 if (transfer
->usage
& PIPE_TRANSFER_READ
) {
697 usage
|= PB_USAGE_CPU_READ
;
700 if (transfer
->usage
& PIPE_TRANSFER_DONTBLOCK
) {
701 usage
|= PB_USAGE_DONTBLOCK
;
704 if (transfer
->usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) {
705 usage
|= PB_USAGE_UNSYNCHRONIZED
;
708 map
= r600_bo_map(radeon
, bo
, usage
, ctx
);
716 void r600_texture_transfer_unmap(struct pipe_context
*ctx
,
717 struct pipe_transfer
* transfer
)
719 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
720 struct radeon
*radeon
= (struct radeon
*)ctx
->screen
->winsys
;
723 if (rtransfer
->staging_texture
) {
724 bo
= ((struct r600_resource
*)rtransfer
->staging_texture
)->bo
;
726 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
728 if (rtex
->flushed_depth_texture
) {
729 bo
= ((struct r600_resource
*)rtex
->flushed_depth_texture
)->bo
;
731 bo
= ((struct r600_resource
*)transfer
->resource
)->bo
;
734 r600_bo_unmap(radeon
, bo
);
737 struct u_resource_vtbl r600_texture_vtbl
=
739 r600_texture_get_handle
, /* get_handle */
740 r600_texture_destroy
, /* resource_destroy */
741 r600_texture_is_referenced
, /* is_resource_referenced */
742 r600_texture_get_transfer
, /* get_transfer */
743 r600_texture_transfer_destroy
, /* transfer_destroy */
744 r600_texture_transfer_map
, /* transfer_map */
745 u_default_transfer_flush_region
,/* transfer_flush_region */
746 r600_texture_transfer_unmap
, /* transfer_unmap */
747 u_default_transfer_inline_write
/* transfer_inline_write */
750 void r600_init_screen_texture_functions(struct pipe_screen
*screen
)
752 screen
->get_tex_surface
= r600_get_tex_surface
;
753 screen
->tex_surface_destroy
= r600_tex_surface_destroy
;
756 static unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format
,
757 const unsigned char *swizzle_view
)
760 unsigned char swizzle
[4];
762 const uint32_t swizzle_shift
[4] = {
765 const uint32_t swizzle_bit
[4] = {
770 /* Combine two sets of swizzles. */
771 for (i
= 0; i
< 4; i
++) {
772 swizzle
[i
] = swizzle_view
[i
] <= UTIL_FORMAT_SWIZZLE_W
?
773 swizzle_format
[swizzle_view
[i
]] : swizzle_view
[i
];
776 memcpy(swizzle
, swizzle_format
, 4);
780 for (i
= 0; i
< 4; i
++) {
781 switch (swizzle
[i
]) {
782 case UTIL_FORMAT_SWIZZLE_Y
:
783 result
|= swizzle_bit
[1] << swizzle_shift
[i
];
785 case UTIL_FORMAT_SWIZZLE_Z
:
786 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
788 case UTIL_FORMAT_SWIZZLE_W
:
789 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
791 case UTIL_FORMAT_SWIZZLE_0
:
792 result
|= V_038010_SQ_SEL_0
<< swizzle_shift
[i
];
794 case UTIL_FORMAT_SWIZZLE_1
:
795 result
|= V_038010_SQ_SEL_1
<< swizzle_shift
[i
];
797 default: /* UTIL_FORMAT_SWIZZLE_X */
798 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
804 /* texture format translate */
805 uint32_t r600_translate_texformat(enum pipe_format format
,
806 const unsigned char *swizzle_view
,
807 uint32_t *word4_p
, uint32_t *yuv_format_p
)
809 uint32_t result
= 0, word4
= 0, yuv_format
= 0;
810 const struct util_format_description
*desc
;
811 boolean uniform
= TRUE
;
813 const uint32_t sign_bit
[4] = {
814 S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED
),
815 S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED
),
816 S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED
),
817 S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED
)
819 desc
= util_format_description(format
);
821 word4
|= r600_get_swizzle_combined(desc
->swizzle
, swizzle_view
);
823 /* Colorspace (return non-RGB formats directly). */
824 switch (desc
->colorspace
) {
825 /* Depth stencil formats */
826 case UTIL_FORMAT_COLORSPACE_ZS
:
828 case PIPE_FORMAT_Z16_UNORM
:
831 case PIPE_FORMAT_X24S8_USCALED
:
832 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
833 case PIPE_FORMAT_Z24X8_UNORM
:
834 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
837 case PIPE_FORMAT_S8X24_USCALED
:
838 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
839 case PIPE_FORMAT_X8Z24_UNORM
:
840 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
843 case PIPE_FORMAT_S8_USCALED
:
845 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
851 case UTIL_FORMAT_COLORSPACE_YUV
:
852 yuv_format
|= (1 << 30);
854 case PIPE_FORMAT_UYVY
:
855 case PIPE_FORMAT_YUYV
:
859 goto out_unknown
; /* TODO */
861 case UTIL_FORMAT_COLORSPACE_SRGB
:
862 word4
|= S_038010_FORCE_DEGAMMA(1);
863 if (format
== PIPE_FORMAT_L8A8_SRGB
|| format
== PIPE_FORMAT_L8_SRGB
)
864 goto out_unknown
; /* fails for some reason - TODO */
871 /* S3TC formats. TODO */
872 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
873 static int r600_enable_s3tc
= -1;
875 if (r600_enable_s3tc
== -1)
877 debug_get_bool_option("R600_ENABLE_S3TC", FALSE
);
879 if (!r600_enable_s3tc
)
883 case PIPE_FORMAT_DXT1_RGB
:
884 case PIPE_FORMAT_DXT1_RGBA
:
887 case PIPE_FORMAT_DXT3_RGBA
:
890 case PIPE_FORMAT_DXT5_RGBA
:
899 for (i
= 0; i
< desc
->nr_channels
; i
++) {
900 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
901 word4
|= sign_bit
[i
];
905 /* R8G8Bx_SNORM - TODO CxV8U8 */
909 /* See whether the components are of the same size. */
910 for (i
= 1; i
< desc
->nr_channels
; i
++) {
911 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
914 /* Non-uniform formats. */
916 switch(desc
->nr_channels
) {
918 if (desc
->channel
[0].size
== 5 &&
919 desc
->channel
[1].size
== 6 &&
920 desc
->channel
[2].size
== 5) {
926 if (desc
->channel
[0].size
== 5 &&
927 desc
->channel
[1].size
== 5 &&
928 desc
->channel
[2].size
== 5 &&
929 desc
->channel
[3].size
== 1) {
930 result
= FMT_1_5_5_5
;
933 if (desc
->channel
[0].size
== 10 &&
934 desc
->channel
[1].size
== 10 &&
935 desc
->channel
[2].size
== 10 &&
936 desc
->channel
[3].size
== 2) {
937 result
= FMT_10_10_10_2
;
945 /* Find the first non-VOID channel. */
946 for (i
= 0; i
< 4; i
++) {
947 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
955 /* uniform formats */
956 switch (desc
->channel
[i
].type
) {
957 case UTIL_FORMAT_TYPE_UNSIGNED
:
958 case UTIL_FORMAT_TYPE_SIGNED
:
959 if (!desc
->channel
[i
].normalized
&&
960 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
964 switch (desc
->channel
[i
].size
) {
966 switch (desc
->nr_channels
) {
971 result
= FMT_4_4_4_4
;
976 switch (desc
->nr_channels
) {
984 result
= FMT_8_8_8_8
;
989 switch (desc
->nr_channels
) {
997 result
= FMT_16_16_16_16
;
1003 case UTIL_FORMAT_TYPE_FLOAT
:
1004 switch (desc
->channel
[i
].size
) {
1006 switch (desc
->nr_channels
) {
1008 result
= FMT_16_FLOAT
;
1011 result
= FMT_16_16_FLOAT
;
1014 result
= FMT_16_16_16_16_FLOAT
;
1019 switch (desc
->nr_channels
) {
1021 result
= FMT_32_FLOAT
;
1024 result
= FMT_32_32_FLOAT
;
1027 result
= FMT_32_32_32_32_FLOAT
;
1037 *yuv_format_p
= yuv_format
;
1040 // R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format));