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
));
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 mip_minify(unsigned size
, unsigned level
)
145 val
= u_minify(size
, level
);
147 val
= util_next_power_of_two(val
);
151 static unsigned r600_texture_get_stride(struct pipe_screen
*screen
,
152 struct r600_resource_texture
*rtex
,
155 struct pipe_resource
*ptex
= &rtex
->resource
.base
.b
;
156 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
157 enum chip_class chipc
= r600_get_family_class(radeon
);
158 unsigned width
, stride
, tile_width
;
160 if (rtex
->pitch_override
)
161 return rtex
->pitch_override
;
163 width
= mip_minify(ptex
->width0
, level
);
164 if (util_format_is_plain(ptex
->format
)) {
165 tile_width
= r600_get_pixel_alignment(screen
, ptex
->format
,
166 rtex
->array_mode
[level
]);
167 width
= align(width
, tile_width
);
169 stride
= util_format_get_stride(ptex
->format
, width
);
170 if (chipc
== EVERGREEN
)
171 stride
= align(stride
, 512);
173 if (ptex
->bind
& PIPE_BIND_RENDER_TARGET
)
174 stride
= align(stride
, 512);
179 static unsigned r600_texture_get_nblocksy(struct pipe_screen
*screen
,
180 struct r600_resource_texture
*rtex
,
183 struct pipe_resource
*ptex
= &rtex
->resource
.base
.b
;
184 unsigned height
, tile_height
;
186 height
= mip_minify(ptex
->height0
, level
);
187 if (util_format_is_plain(ptex
->format
)) {
188 tile_height
= r600_get_height_alignment(screen
,
189 rtex
->array_mode
[level
]);
190 height
= align(height
, tile_height
);
192 return util_format_get_nblocksy(ptex
->format
, height
);
195 /* Get a width in pixels from a stride in bytes. */
196 static unsigned pitch_to_width(enum pipe_format format
,
197 unsigned pitch_in_bytes
)
199 return (pitch_in_bytes
/ util_format_get_blocksize(format
)) *
200 util_format_get_blockwidth(format
);
203 static void r600_texture_set_array_mode(struct pipe_screen
*screen
,
204 struct r600_resource_texture
*rtex
,
205 unsigned level
, unsigned array_mode
)
207 struct pipe_resource
*ptex
= &rtex
->resource
.base
.b
;
209 switch (array_mode
) {
210 case V_0280A0_ARRAY_LINEAR_GENERAL
:
211 case V_0280A0_ARRAY_LINEAR_ALIGNED
:
212 case V_0280A0_ARRAY_1D_TILED_THIN1
:
214 rtex
->array_mode
[level
] = array_mode
;
216 case V_0280A0_ARRAY_2D_TILED_THIN1
:
218 unsigned w
, h
, tile_height
, tile_width
;
220 tile_height
= r600_get_height_alignment(screen
, array_mode
);
221 tile_width
= r600_get_pixel_alignment(screen
, ptex
->format
, array_mode
);
223 w
= mip_minify(ptex
->width0
, level
);
224 h
= mip_minify(ptex
->height0
, level
);
225 if (w
< tile_width
|| h
< tile_height
)
226 rtex
->array_mode
[level
] = V_0280A0_ARRAY_1D_TILED_THIN1
;
228 rtex
->array_mode
[level
] = array_mode
;
234 static void r600_setup_miptree(struct pipe_screen
*screen
,
235 struct r600_resource_texture
*rtex
,
238 struct pipe_resource
*ptex
= &rtex
->resource
.base
.b
;
239 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
240 enum chip_class chipc
= r600_get_family_class(radeon
);
241 unsigned pitch
, size
, layer_size
, i
, offset
;
244 for (i
= 0, offset
= 0; i
<= ptex
->last_level
; i
++) {
245 r600_texture_set_array_mode(screen
, rtex
, i
, array_mode
);
247 pitch
= r600_texture_get_stride(screen
, rtex
, i
);
248 nblocksy
= r600_texture_get_nblocksy(screen
, rtex
, i
);
250 layer_size
= pitch
* nblocksy
;
252 if (ptex
->target
== PIPE_TEXTURE_CUBE
) {
254 size
= layer_size
* 8;
256 size
= layer_size
* 6;
259 size
= layer_size
* u_minify(ptex
->depth0
, i
);
260 rtex
->offset
[i
] = offset
;
261 rtex
->layer_size
[i
] = layer_size
;
262 rtex
->pitch_in_bytes
[i
] = pitch
;
263 rtex
->pitch_in_pixels
[i
] = pitch_to_width(ptex
->format
, pitch
);
269 static struct r600_resource_texture
*
270 r600_texture_create_object(struct pipe_screen
*screen
,
271 const struct pipe_resource
*base
,
273 unsigned pitch_in_bytes_override
,
274 unsigned max_buffer_size
,
277 struct r600_resource_texture
*rtex
;
278 struct r600_resource
*resource
;
279 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
281 rtex
= CALLOC_STRUCT(r600_resource_texture
);
285 resource
= &rtex
->resource
;
286 resource
->base
.b
= *base
;
287 resource
->base
.vtbl
= &r600_texture_vtbl
;
288 pipe_reference_init(&resource
->base
.b
.reference
, 1);
289 resource
->base
.b
.screen
= screen
;
291 rtex
->pitch_override
= pitch_in_bytes_override
;
295 r600_setup_miptree(screen
, rtex
, array_mode
);
297 resource
->size
= rtex
->size
;
300 resource
->bo
= r600_bo(radeon
, rtex
->size
, 4096, base
->bind
, base
->usage
);
309 struct pipe_resource
*r600_texture_create(struct pipe_screen
*screen
,
310 const struct pipe_resource
*templ
)
312 unsigned array_mode
= 0;
313 static int force_tiling
= -1;
315 /* Would like some magic "get_bool_option_once" routine.
317 if (force_tiling
== -1)
318 force_tiling
= debug_get_bool_option("R600_FORCE_TILING", FALSE
);
321 if (!(templ
->flags
& R600_RESOURCE_FLAG_TRANSFER
) &&
322 !(templ
->bind
& PIPE_BIND_SCANOUT
)) {
323 array_mode
= V_038000_ARRAY_2D_TILED_THIN1
;
327 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
, array_mode
,
332 static void r600_texture_destroy(struct pipe_screen
*screen
,
333 struct pipe_resource
*ptex
)
335 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)ptex
;
336 struct r600_resource
*resource
= &rtex
->resource
;
337 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
339 if (rtex
->flushed_depth_texture
)
340 pipe_resource_reference((struct pipe_resource
**)&rtex
->flushed_depth_texture
, NULL
);
343 r600_bo_reference(radeon
, &resource
->bo
, NULL
);
348 static boolean
r600_texture_get_handle(struct pipe_screen
* screen
,
349 struct pipe_resource
*ptex
,
350 struct winsys_handle
*whandle
)
352 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)ptex
;
353 struct r600_resource
*resource
= &rtex
->resource
;
354 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
356 return r600_bo_get_winsys_handle(radeon
, resource
->bo
,
357 rtex
->pitch_in_bytes
[0], whandle
);
360 static struct pipe_surface
*r600_get_tex_surface(struct pipe_screen
*screen
,
361 struct pipe_resource
*texture
,
362 unsigned face
, unsigned level
,
363 unsigned zslice
, unsigned flags
)
365 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
366 struct r600_surface
*surface
= CALLOC_STRUCT(r600_surface
);
367 unsigned offset
, tile_height
;
371 offset
= r600_texture_get_offset(rtex
, level
, zslice
, face
);
372 pipe_reference_init(&surface
->base
.reference
, 1);
373 pipe_resource_reference(&surface
->base
.texture
, texture
);
374 surface
->base
.format
= texture
->format
;
375 surface
->base
.width
= mip_minify(texture
->width0
, level
);
376 surface
->base
.height
= mip_minify(texture
->height0
, level
);
377 surface
->base
.offset
= offset
;
378 surface
->base
.usage
= flags
;
379 surface
->base
.zslice
= zslice
;
380 surface
->base
.texture
= texture
;
381 surface
->base
.face
= face
;
382 surface
->base
.level
= level
;
384 tile_height
= r600_get_height_alignment(screen
, rtex
->array_mode
[level
]);
385 surface
->aligned_height
= align(surface
->base
.height
, tile_height
);
386 return &surface
->base
;
389 static void r600_tex_surface_destroy(struct pipe_surface
*surface
)
391 pipe_resource_reference(&surface
->texture
, NULL
);
396 struct pipe_resource
*r600_texture_from_handle(struct pipe_screen
*screen
,
397 const struct pipe_resource
*templ
,
398 struct winsys_handle
*whandle
)
400 struct radeon
*rw
= (struct radeon
*)screen
->winsys
;
401 struct r600_bo
*bo
= NULL
;
402 unsigned array_mode
= 0;
404 /* Support only 2D textures without mipmaps */
405 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
) ||
406 templ
->depth0
!= 1 || templ
->last_level
!= 0)
409 bo
= r600_bo_handle(rw
, whandle
->handle
, &array_mode
);
414 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
, array_mode
,
420 static unsigned int r600_texture_is_referenced(struct pipe_context
*context
,
421 struct pipe_resource
*texture
,
422 unsigned face
, unsigned level
)
425 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
428 int (*r600_blit_uncompress_depth_ptr
)(struct pipe_context
*ctx
, struct r600_resource_texture
*texture
);
430 int r600_texture_depth_flush(struct pipe_context
*ctx
,
431 struct pipe_resource
*texture
)
433 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
434 struct pipe_resource resource
;
436 if (rtex
->flushed_depth_texture
)
439 resource
.target
= PIPE_TEXTURE_2D
;
440 resource
.format
= texture
->format
;
441 resource
.width0
= texture
->width0
;
442 resource
.height0
= texture
->height0
;
444 resource
.last_level
= 0;
445 resource
.nr_samples
= 0;
446 resource
.usage
= PIPE_USAGE_DYNAMIC
;
448 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
;
450 resource
.bind
|= PIPE_BIND_DEPTH_STENCIL
;
452 rtex
->flushed_depth_texture
= (struct r600_resource_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
453 if (rtex
->flushed_depth_texture
== NULL
) {
454 R600_ERR("failed to create temporary texture to hold untiled copy\n");
459 /* XXX: only do this if the depth texture has actually changed:
461 r600_blit_uncompress_depth_ptr(ctx
, rtex
);
465 /* Needs adjustment for pixelformat:
467 static INLINE
unsigned u_box_volume( const struct pipe_box
*box
)
469 return box
->width
* box
->depth
* box
->height
;
473 /* Figure out whether u_blitter will fallback to a transfer operation.
474 * If so, don't use a staging resource.
476 static boolean
permit_hardware_blit(struct pipe_screen
*screen
,
477 struct pipe_resource
*res
)
481 if (util_format_is_depth_or_stencil(res
->format
))
482 bind
= PIPE_BIND_DEPTH_STENCIL
;
484 bind
= PIPE_BIND_RENDER_TARGET
;
486 /* See r600_resource_copy_region: there is something wrong
487 * with depth resource copies at the moment so avoid them for
490 if (util_format_get_component_bits(res
->format
,
491 UTIL_FORMAT_COLORSPACE_ZS
,
495 if (!screen
->is_format_supported(screen
,
502 if (!screen
->is_format_supported(screen
,
506 PIPE_BIND_SAMPLER_VIEW
, 0))
512 struct pipe_transfer
* r600_texture_get_transfer(struct pipe_context
*ctx
,
513 struct pipe_resource
*texture
,
514 struct pipe_subresource sr
,
516 const struct pipe_box
*box
)
518 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
519 struct pipe_resource resource
;
520 struct r600_transfer
*trans
;
522 boolean use_staging_texture
= FALSE
;
524 /* We cannot map a tiled texture directly because the data is
525 * in a different order, therefore we do detiling using a blit.
527 * Also, use a temporary in GTT memory for read transfers, as
528 * the CPU is much happier reading out of cached system memory
529 * than uncached VRAM.
532 use_staging_texture
= TRUE
;
534 if ((usage
& PIPE_TRANSFER_READ
) &&
535 u_box_volume(box
) > 1024)
536 use_staging_texture
= TRUE
;
538 /* XXX: Use a staging texture for uploads if the underlying BO
539 * is busy. No interface for checking that currently? so do
540 * it eagerly whenever the transfer doesn't require a readback
543 if ((usage
& PIPE_TRANSFER_WRITE
) &&
544 !(usage
& (PIPE_TRANSFER_READ
|
545 PIPE_TRANSFER_DONTBLOCK
|
546 PIPE_TRANSFER_UNSYNCHRONIZED
)))
547 use_staging_texture
= TRUE
;
549 if (!permit_hardware_blit(ctx
->screen
, texture
) ||
550 (texture
->flags
& R600_RESOURCE_FLAG_TRANSFER
))
551 use_staging_texture
= FALSE
;
553 trans
= CALLOC_STRUCT(r600_transfer
);
556 pipe_resource_reference(&trans
->transfer
.resource
, texture
);
557 trans
->transfer
.sr
= sr
;
558 trans
->transfer
.usage
= usage
;
559 trans
->transfer
.box
= *box
;
561 /* XXX: only readback the rectangle which is being mapped?
563 /* XXX: when discard is true, no need to read back from depth texture
565 r
= r600_texture_depth_flush(ctx
, texture
);
567 R600_ERR("failed to create temporary texture to hold untiled copy\n");
568 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
572 } else if (use_staging_texture
) {
573 resource
.target
= PIPE_TEXTURE_2D
;
574 resource
.format
= texture
->format
;
575 resource
.width0
= box
->width
;
576 resource
.height0
= box
->height
;
578 resource
.last_level
= 0;
579 resource
.nr_samples
= 0;
580 resource
.usage
= PIPE_USAGE_STAGING
;
582 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
;
583 /* For texture reading, the temporary (detiled) texture is used as
584 * a render target when blitting from a tiled texture. */
585 if (usage
& PIPE_TRANSFER_READ
) {
586 resource
.bind
|= PIPE_BIND_RENDER_TARGET
;
588 /* For texture writing, the temporary texture is used as a sampler
589 * when blitting into a tiled texture. */
590 if (usage
& PIPE_TRANSFER_WRITE
) {
591 resource
.bind
|= PIPE_BIND_SAMPLER_VIEW
;
593 /* Create the temporary texture. */
594 trans
->staging_texture
= ctx
->screen
->resource_create(ctx
->screen
, &resource
);
595 if (trans
->staging_texture
== NULL
) {
596 R600_ERR("failed to create temporary texture to hold untiled copy\n");
597 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
602 trans
->transfer
.stride
=
603 ((struct r600_resource_texture
*)trans
->staging_texture
)->pitch_in_bytes
[0];
604 if (usage
& PIPE_TRANSFER_READ
) {
605 r600_copy_to_staging_texture(ctx
, trans
);
606 /* Always referenced in the blit. */
607 ctx
->flush(ctx
, 0, NULL
);
609 return &trans
->transfer
;
611 trans
->transfer
.stride
= rtex
->pitch_in_bytes
[sr
.level
];
612 trans
->offset
= r600_texture_get_offset(rtex
, sr
.level
, box
->z
, sr
.face
);
613 return &trans
->transfer
;
616 void r600_texture_transfer_destroy(struct pipe_context
*ctx
,
617 struct pipe_transfer
*transfer
)
619 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
620 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
622 if (rtransfer
->staging_texture
) {
623 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
624 r600_copy_from_staging_texture(ctx
, rtransfer
);
626 pipe_resource_reference(&rtransfer
->staging_texture
, NULL
);
628 if (rtex
->flushed_depth_texture
) {
629 pipe_resource_reference((struct pipe_resource
**)&rtex
->flushed_depth_texture
, NULL
);
631 pipe_resource_reference(&transfer
->resource
, NULL
);
635 void* r600_texture_transfer_map(struct pipe_context
*ctx
,
636 struct pipe_transfer
* transfer
)
638 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
640 enum pipe_format format
= transfer
->resource
->format
;
641 struct radeon
*radeon
= (struct radeon
*)ctx
->screen
->winsys
;
646 if (rtransfer
->staging_texture
) {
647 bo
= ((struct r600_resource
*)rtransfer
->staging_texture
)->bo
;
649 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
651 if (rtex
->flushed_depth_texture
)
652 bo
= ((struct r600_resource
*)rtex
->flushed_depth_texture
)->bo
;
654 bo
= ((struct r600_resource
*)transfer
->resource
)->bo
;
656 offset
= rtransfer
->offset
+
657 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
658 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
661 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
662 usage
|= PB_USAGE_CPU_WRITE
;
664 if (transfer
->usage
& PIPE_TRANSFER_DISCARD
) {
667 if (transfer
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
) {
671 if (transfer
->usage
& PIPE_TRANSFER_READ
) {
672 usage
|= PB_USAGE_CPU_READ
;
675 if (transfer
->usage
& PIPE_TRANSFER_DONTBLOCK
) {
676 usage
|= PB_USAGE_DONTBLOCK
;
679 if (transfer
->usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) {
680 usage
|= PB_USAGE_UNSYNCHRONIZED
;
683 map
= r600_bo_map(radeon
, bo
, usage
, ctx
);
691 void r600_texture_transfer_unmap(struct pipe_context
*ctx
,
692 struct pipe_transfer
* transfer
)
694 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
695 struct radeon
*radeon
= (struct radeon
*)ctx
->screen
->winsys
;
698 if (rtransfer
->staging_texture
) {
699 bo
= ((struct r600_resource
*)rtransfer
->staging_texture
)->bo
;
701 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
703 if (rtex
->flushed_depth_texture
) {
704 bo
= ((struct r600_resource
*)rtex
->flushed_depth_texture
)->bo
;
706 bo
= ((struct r600_resource
*)transfer
->resource
)->bo
;
709 r600_bo_unmap(radeon
, bo
);
712 struct u_resource_vtbl r600_texture_vtbl
=
714 r600_texture_get_handle
, /* get_handle */
715 r600_texture_destroy
, /* resource_destroy */
716 r600_texture_is_referenced
, /* is_resource_referenced */
717 r600_texture_get_transfer
, /* get_transfer */
718 r600_texture_transfer_destroy
, /* transfer_destroy */
719 r600_texture_transfer_map
, /* transfer_map */
720 u_default_transfer_flush_region
,/* transfer_flush_region */
721 r600_texture_transfer_unmap
, /* transfer_unmap */
722 u_default_transfer_inline_write
/* transfer_inline_write */
725 void r600_init_screen_texture_functions(struct pipe_screen
*screen
)
727 screen
->get_tex_surface
= r600_get_tex_surface
;
728 screen
->tex_surface_destroy
= r600_tex_surface_destroy
;
731 static unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format
,
732 const unsigned char *swizzle_view
)
735 unsigned char swizzle
[4];
737 const uint32_t swizzle_shift
[4] = {
740 const uint32_t swizzle_bit
[4] = {
745 /* Combine two sets of swizzles. */
746 for (i
= 0; i
< 4; i
++) {
747 swizzle
[i
] = swizzle_view
[i
] <= UTIL_FORMAT_SWIZZLE_W
?
748 swizzle_format
[swizzle_view
[i
]] : swizzle_view
[i
];
751 memcpy(swizzle
, swizzle_format
, 4);
755 for (i
= 0; i
< 4; i
++) {
756 switch (swizzle
[i
]) {
757 case UTIL_FORMAT_SWIZZLE_Y
:
758 result
|= swizzle_bit
[1] << swizzle_shift
[i
];
760 case UTIL_FORMAT_SWIZZLE_Z
:
761 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
763 case UTIL_FORMAT_SWIZZLE_W
:
764 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
766 case UTIL_FORMAT_SWIZZLE_0
:
767 result
|= V_038010_SQ_SEL_0
<< swizzle_shift
[i
];
769 case UTIL_FORMAT_SWIZZLE_1
:
770 result
|= V_038010_SQ_SEL_1
<< swizzle_shift
[i
];
772 default: /* UTIL_FORMAT_SWIZZLE_X */
773 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
779 /* texture format translate */
780 uint32_t r600_translate_texformat(enum pipe_format format
,
781 const unsigned char *swizzle_view
,
782 uint32_t *word4_p
, uint32_t *yuv_format_p
)
784 uint32_t result
= 0, word4
= 0, yuv_format
= 0;
785 const struct util_format_description
*desc
;
786 boolean uniform
= TRUE
;
788 const uint32_t sign_bit
[4] = {
789 S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED
),
790 S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED
),
791 S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED
),
792 S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED
)
794 desc
= util_format_description(format
);
796 word4
|= r600_get_swizzle_combined(desc
->swizzle
, swizzle_view
);
798 /* Colorspace (return non-RGB formats directly). */
799 switch (desc
->colorspace
) {
800 /* Depth stencil formats */
801 case UTIL_FORMAT_COLORSPACE_ZS
:
803 case PIPE_FORMAT_Z16_UNORM
:
806 case PIPE_FORMAT_X24S8_USCALED
:
807 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
808 case PIPE_FORMAT_Z24X8_UNORM
:
809 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
812 case PIPE_FORMAT_S8X24_USCALED
:
813 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
814 case PIPE_FORMAT_X8Z24_UNORM
:
815 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
818 case PIPE_FORMAT_S8_USCALED
:
820 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
826 case UTIL_FORMAT_COLORSPACE_YUV
:
827 yuv_format
|= (1 << 30);
829 case PIPE_FORMAT_UYVY
:
830 case PIPE_FORMAT_YUYV
:
834 goto out_unknown
; /* TODO */
836 case UTIL_FORMAT_COLORSPACE_SRGB
:
837 word4
|= S_038010_FORCE_DEGAMMA(1);
838 if (format
== PIPE_FORMAT_L8A8_SRGB
|| format
== PIPE_FORMAT_L8_SRGB
)
839 goto out_unknown
; /* fails for some reason - TODO */
846 /* S3TC formats. TODO */
847 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
848 static int r600_enable_s3tc
= -1;
850 if (r600_enable_s3tc
== -1)
852 debug_get_bool_option("R600_ENABLE_S3TC", FALSE
);
854 if (!r600_enable_s3tc
)
858 case PIPE_FORMAT_DXT1_RGB
:
859 case PIPE_FORMAT_DXT1_RGBA
:
862 case PIPE_FORMAT_DXT3_RGBA
:
865 case PIPE_FORMAT_DXT5_RGBA
:
874 for (i
= 0; i
< desc
->nr_channels
; i
++) {
875 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
876 word4
|= sign_bit
[i
];
880 /* R8G8Bx_SNORM - TODO CxV8U8 */
884 /* See whether the components are of the same size. */
885 for (i
= 1; i
< desc
->nr_channels
; i
++) {
886 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
889 /* Non-uniform formats. */
891 switch(desc
->nr_channels
) {
893 if (desc
->channel
[0].size
== 5 &&
894 desc
->channel
[1].size
== 6 &&
895 desc
->channel
[2].size
== 5) {
901 if (desc
->channel
[0].size
== 5 &&
902 desc
->channel
[1].size
== 5 &&
903 desc
->channel
[2].size
== 5 &&
904 desc
->channel
[3].size
== 1) {
905 result
= FMT_1_5_5_5
;
908 if (desc
->channel
[0].size
== 10 &&
909 desc
->channel
[1].size
== 10 &&
910 desc
->channel
[2].size
== 10 &&
911 desc
->channel
[3].size
== 2) {
912 result
= FMT_10_10_10_2
;
920 /* Find the first non-VOID channel. */
921 for (i
= 0; i
< 4; i
++) {
922 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
930 /* uniform formats */
931 switch (desc
->channel
[i
].type
) {
932 case UTIL_FORMAT_TYPE_UNSIGNED
:
933 case UTIL_FORMAT_TYPE_SIGNED
:
934 if (!desc
->channel
[i
].normalized
&&
935 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
939 switch (desc
->channel
[i
].size
) {
941 switch (desc
->nr_channels
) {
946 result
= FMT_4_4_4_4
;
951 switch (desc
->nr_channels
) {
959 result
= FMT_8_8_8_8
;
964 switch (desc
->nr_channels
) {
972 result
= FMT_16_16_16_16
;
978 case UTIL_FORMAT_TYPE_FLOAT
:
979 switch (desc
->channel
[i
].size
) {
981 switch (desc
->nr_channels
) {
983 result
= FMT_16_FLOAT
;
986 result
= FMT_16_16_FLOAT
;
989 result
= FMT_16_16_16_16_FLOAT
;
994 switch (desc
->nr_channels
) {
996 result
= FMT_32_FLOAT
;
999 result
= FMT_32_32_FLOAT
;
1002 result
= FMT_32_32_32_32_FLOAT
;
1012 *yuv_format_p
= yuv_format
;
1015 // R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format));