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 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
174 enum chip_class chipc
= r600_get_family_class(radeon
);
175 unsigned width
, stride
, tile_width
;
177 if (rtex
->pitch_override
)
178 return rtex
->pitch_override
;
180 width
= mip_minify(ptex
->width0
, level
);
181 if (util_format_is_plain(ptex
->format
)) {
182 tile_width
= r600_get_pixel_alignment(screen
, ptex
->format
,
183 rtex
->array_mode
[level
]);
184 width
= align(width
, tile_width
);
186 stride
= util_format_get_stride(ptex
->format
, width
);
191 static unsigned r600_texture_get_nblocksy(struct pipe_screen
*screen
,
192 struct r600_resource_texture
*rtex
,
195 struct pipe_resource
*ptex
= &rtex
->resource
.base
.b
;
196 unsigned height
, tile_height
;
198 height
= mip_minify(ptex
->height0
, level
);
199 if (util_format_is_plain(ptex
->format
)) {
200 tile_height
= r600_get_height_alignment(screen
,
201 rtex
->array_mode
[level
]);
202 height
= align(height
, tile_height
);
204 return util_format_get_nblocksy(ptex
->format
, height
);
207 /* Get a width in pixels from a stride in bytes. */
208 static unsigned pitch_to_width(enum pipe_format format
, unsigned pitch_in_bytes
)
210 return (pitch_in_bytes
/ util_format_get_blocksize(format
)) *
211 util_format_get_blockwidth(format
);
214 static void r600_texture_set_array_mode(struct pipe_screen
*screen
,
215 struct r600_resource_texture
*rtex
,
216 unsigned level
, unsigned array_mode
)
218 struct pipe_resource
*ptex
= &rtex
->resource
.base
.b
;
220 switch (array_mode
) {
221 case V_0280A0_ARRAY_LINEAR_GENERAL
:
222 case V_0280A0_ARRAY_LINEAR_ALIGNED
:
223 case V_0280A0_ARRAY_1D_TILED_THIN1
:
225 rtex
->array_mode
[level
] = array_mode
;
227 case V_0280A0_ARRAY_2D_TILED_THIN1
:
229 unsigned w
, h
, tile_height
, tile_width
;
231 tile_height
= r600_get_height_alignment(screen
, array_mode
);
232 tile_width
= r600_get_pixel_alignment(screen
, ptex
->format
, array_mode
);
234 w
= mip_minify(ptex
->width0
, level
);
235 h
= mip_minify(ptex
->height0
, level
);
236 if (w
< tile_width
|| h
< tile_height
)
237 rtex
->array_mode
[level
] = V_0280A0_ARRAY_1D_TILED_THIN1
;
239 rtex
->array_mode
[level
] = array_mode
;
245 static void r600_setup_miptree(struct pipe_screen
*screen
,
246 struct r600_resource_texture
*rtex
,
249 struct pipe_resource
*ptex
= &rtex
->resource
.base
.b
;
250 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
251 enum chip_class chipc
= r600_get_family_class(radeon
);
252 unsigned pitch
, size
, layer_size
, i
, offset
;
255 for (i
= 0, offset
= 0; i
<= ptex
->last_level
; i
++) {
256 r600_texture_set_array_mode(screen
, rtex
, i
, array_mode
);
258 pitch
= r600_texture_get_stride(screen
, rtex
, i
);
259 nblocksy
= r600_texture_get_nblocksy(screen
, rtex
, i
);
261 layer_size
= pitch
* nblocksy
;
263 if (ptex
->target
== PIPE_TEXTURE_CUBE
) {
265 size
= layer_size
* 8;
267 size
= layer_size
* 6;
270 size
= layer_size
* u_minify(ptex
->depth0
, i
);
271 /* align base image and start of miptree */
272 if ((i
== 0) || (i
== 1))
273 offset
= align(offset
, r600_get_base_alignment(screen
, ptex
->format
, array_mode
));
274 rtex
->offset
[i
] = offset
;
275 rtex
->layer_size
[i
] = layer_size
;
276 rtex
->pitch_in_bytes
[i
] = pitch
;
277 rtex
->pitch_in_pixels
[i
] = pitch_to_width(ptex
->format
, pitch
);
283 static struct r600_resource_texture
*
284 r600_texture_create_object(struct pipe_screen
*screen
,
285 const struct pipe_resource
*base
,
287 unsigned pitch_in_bytes_override
,
288 unsigned max_buffer_size
,
291 struct r600_resource_texture
*rtex
;
292 struct r600_resource
*resource
;
293 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
295 rtex
= CALLOC_STRUCT(r600_resource_texture
);
299 resource
= &rtex
->resource
;
300 resource
->base
.b
= *base
;
301 resource
->base
.vtbl
= &r600_texture_vtbl
;
302 pipe_reference_init(&resource
->base
.b
.reference
, 1);
303 resource
->base
.b
.screen
= screen
;
305 rtex
->pitch_override
= pitch_in_bytes_override
;
309 r600_setup_miptree(screen
, rtex
, array_mode
);
311 resource
->size
= rtex
->size
;
314 struct pipe_resource
*ptex
= &rtex
->resource
.base
.b
;
315 int base_align
= r600_get_base_alignment(screen
, ptex
->format
, array_mode
);
317 resource
->bo
= r600_bo(radeon
, rtex
->size
, base_align
, base
->bind
, base
->usage
);
326 struct pipe_resource
*r600_texture_create(struct pipe_screen
*screen
,
327 const struct pipe_resource
*templ
)
329 unsigned array_mode
= 0;
330 static int force_tiling
= -1;
332 /* Would like some magic "get_bool_option_once" routine.
334 if (force_tiling
== -1)
335 force_tiling
= debug_get_bool_option("R600_FORCE_TILING", FALSE
);
338 if (!(templ
->flags
& R600_RESOURCE_FLAG_TRANSFER
) &&
339 !(templ
->bind
& PIPE_BIND_SCANOUT
)) {
340 array_mode
= V_038000_ARRAY_2D_TILED_THIN1
;
344 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
, array_mode
,
349 static void r600_texture_destroy(struct pipe_screen
*screen
,
350 struct pipe_resource
*ptex
)
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 if (rtex
->flushed_depth_texture
)
357 pipe_resource_reference((struct pipe_resource
**)&rtex
->flushed_depth_texture
, NULL
);
360 r600_bo_reference(radeon
, &resource
->bo
, NULL
);
365 static boolean
r600_texture_get_handle(struct pipe_screen
* screen
,
366 struct pipe_resource
*ptex
,
367 struct winsys_handle
*whandle
)
369 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)ptex
;
370 struct r600_resource
*resource
= &rtex
->resource
;
371 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
373 return r600_bo_get_winsys_handle(radeon
, resource
->bo
,
374 rtex
->pitch_in_bytes
[0], whandle
);
377 static struct pipe_surface
*r600_create_surface(struct pipe_context
*pipe
,
378 struct pipe_resource
*texture
,
379 const struct pipe_surface
*surf_tmpl
)
381 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
382 struct r600_surface
*surface
= CALLOC_STRUCT(r600_surface
);
383 unsigned tile_height
;
384 unsigned level
= surf_tmpl
->u
.tex
.level
;
386 assert(surf_tmpl
->u
.tex
.first_layer
== surf_tmpl
->u
.tex
.last_layer
);
390 /* offset = r600_texture_get_offset(rtex, level, surf_tmpl->u.tex.first_layer);*/
391 pipe_reference_init(&surface
->base
.reference
, 1);
392 pipe_resource_reference(&surface
->base
.texture
, texture
);
393 surface
->base
.context
= pipe
;
394 surface
->base
.format
= surf_tmpl
->format
;
395 surface
->base
.width
= mip_minify(texture
->width0
, level
);
396 surface
->base
.height
= mip_minify(texture
->height0
, level
);
397 surface
->base
.usage
= surf_tmpl
->usage
;
398 surface
->base
.texture
= texture
;
399 surface
->base
.u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
400 surface
->base
.u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
401 surface
->base
.u
.tex
.level
= level
;
403 tile_height
= r600_get_height_alignment(pipe
->screen
, rtex
->array_mode
[level
]);
404 surface
->aligned_height
= align(surface
->base
.height
, tile_height
);
405 return &surface
->base
;
408 static void r600_surface_destroy(struct pipe_context
*pipe
,
409 struct pipe_surface
*surface
)
411 pipe_resource_reference(&surface
->texture
, NULL
);
416 struct pipe_resource
*r600_texture_from_handle(struct pipe_screen
*screen
,
417 const struct pipe_resource
*templ
,
418 struct winsys_handle
*whandle
)
420 struct radeon
*rw
= (struct radeon
*)screen
->winsys
;
421 struct r600_bo
*bo
= NULL
;
422 unsigned array_mode
= 0;
424 /* Support only 2D textures without mipmaps */
425 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
) ||
426 templ
->depth0
!= 1 || templ
->last_level
!= 0)
429 bo
= r600_bo_handle(rw
, whandle
->handle
, &array_mode
);
434 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
, array_mode
,
440 static unsigned int r600_texture_is_referenced(struct pipe_context
*context
,
441 struct pipe_resource
*texture
,
442 unsigned level
, int layer
)
445 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
448 int (*r600_blit_uncompress_depth_ptr
)(struct pipe_context
*ctx
, struct r600_resource_texture
*texture
);
450 int r600_texture_depth_flush(struct pipe_context
*ctx
,
451 struct pipe_resource
*texture
)
453 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
454 struct pipe_resource resource
;
456 if (rtex
->flushed_depth_texture
)
459 resource
.target
= PIPE_TEXTURE_2D
;
460 resource
.format
= texture
->format
;
461 resource
.width0
= texture
->width0
;
462 resource
.height0
= texture
->height0
;
464 resource
.last_level
= 0;
465 resource
.nr_samples
= 0;
466 resource
.usage
= PIPE_USAGE_DYNAMIC
;
468 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
;
470 resource
.bind
|= PIPE_BIND_DEPTH_STENCIL
;
472 rtex
->flushed_depth_texture
= (struct r600_resource_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
473 if (rtex
->flushed_depth_texture
== NULL
) {
474 R600_ERR("failed to create temporary texture to hold untiled copy\n");
479 /* XXX: only do this if the depth texture has actually changed:
481 r600_blit_uncompress_depth_ptr(ctx
, rtex
);
485 /* Needs adjustment for pixelformat:
487 static INLINE
unsigned u_box_volume( const struct pipe_box
*box
)
489 return box
->width
* box
->depth
* box
->height
;
493 /* Figure out whether u_blitter will fallback to a transfer operation.
494 * If so, don't use a staging resource.
496 static boolean
permit_hardware_blit(struct pipe_screen
*screen
,
497 struct pipe_resource
*res
)
501 if (util_format_is_depth_or_stencil(res
->format
))
502 bind
= PIPE_BIND_DEPTH_STENCIL
;
504 bind
= PIPE_BIND_RENDER_TARGET
;
506 /* See r600_resource_copy_region: there is something wrong
507 * with depth resource copies at the moment so avoid them for
510 if (util_format_get_component_bits(res
->format
,
511 UTIL_FORMAT_COLORSPACE_ZS
,
515 if (!screen
->is_format_supported(screen
,
522 if (!screen
->is_format_supported(screen
,
526 PIPE_BIND_SAMPLER_VIEW
, 0))
532 struct pipe_transfer
* r600_texture_get_transfer(struct pipe_context
*ctx
,
533 struct pipe_resource
*texture
,
536 const struct pipe_box
*box
)
538 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
539 struct pipe_resource resource
;
540 struct r600_transfer
*trans
;
542 boolean use_staging_texture
= FALSE
;
544 /* We cannot map a tiled texture directly because the data is
545 * in a different order, therefore we do detiling using a blit.
547 * Also, use a temporary in GTT memory for read transfers, as
548 * the CPU is much happier reading out of cached system memory
549 * than uncached VRAM.
552 use_staging_texture
= TRUE
;
554 if ((usage
& PIPE_TRANSFER_READ
) && u_box_volume(box
) > 1024)
555 use_staging_texture
= TRUE
;
557 /* XXX: Use a staging texture for uploads if the underlying BO
558 * is busy. No interface for checking that currently? so do
559 * it eagerly whenever the transfer doesn't require a readback
562 if ((usage
& PIPE_TRANSFER_WRITE
) &&
563 !(usage
& (PIPE_TRANSFER_READ
|
564 PIPE_TRANSFER_DONTBLOCK
|
565 PIPE_TRANSFER_UNSYNCHRONIZED
)))
566 use_staging_texture
= TRUE
;
568 if (!permit_hardware_blit(ctx
->screen
, texture
) ||
569 (texture
->flags
& R600_RESOURCE_FLAG_TRANSFER
))
570 use_staging_texture
= FALSE
;
572 trans
= CALLOC_STRUCT(r600_transfer
);
575 pipe_resource_reference(&trans
->transfer
.resource
, texture
);
576 trans
->transfer
.level
= level
;
577 trans
->transfer
.usage
= usage
;
578 trans
->transfer
.box
= *box
;
580 /* XXX: only readback the rectangle which is being mapped?
582 /* XXX: when discard is true, no need to read back from depth texture
584 r
= r600_texture_depth_flush(ctx
, texture
);
586 R600_ERR("failed to create temporary texture to hold untiled copy\n");
587 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
591 } else if (use_staging_texture
) {
592 resource
.target
= PIPE_TEXTURE_2D
;
593 resource
.format
= texture
->format
;
594 resource
.width0
= box
->width
;
595 resource
.height0
= box
->height
;
597 resource
.array_size
= 1;
598 resource
.last_level
= 0;
599 resource
.nr_samples
= 0;
600 resource
.usage
= PIPE_USAGE_STAGING
;
602 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
;
603 /* For texture reading, the temporary (detiled) texture is used as
604 * a render target when blitting from a tiled texture. */
605 if (usage
& PIPE_TRANSFER_READ
) {
606 resource
.bind
|= PIPE_BIND_RENDER_TARGET
;
608 /* For texture writing, the temporary texture is used as a sampler
609 * when blitting into a tiled texture. */
610 if (usage
& PIPE_TRANSFER_WRITE
) {
611 resource
.bind
|= PIPE_BIND_SAMPLER_VIEW
;
613 /* Create the temporary texture. */
614 trans
->staging_texture
= ctx
->screen
->resource_create(ctx
->screen
, &resource
);
615 if (trans
->staging_texture
== NULL
) {
616 R600_ERR("failed to create temporary texture to hold untiled copy\n");
617 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
622 trans
->transfer
.stride
=
623 ((struct r600_resource_texture
*)trans
->staging_texture
)->pitch_in_bytes
[0];
624 if (usage
& PIPE_TRANSFER_READ
) {
625 r600_copy_to_staging_texture(ctx
, trans
);
626 /* Always referenced in the blit. */
627 ctx
->flush(ctx
, 0, NULL
);
629 return &trans
->transfer
;
631 trans
->transfer
.stride
= rtex
->pitch_in_bytes
[level
];
632 trans
->offset
= r600_texture_get_offset(rtex
, level
, box
->z
);
633 return &trans
->transfer
;
636 void r600_texture_transfer_destroy(struct pipe_context
*ctx
,
637 struct pipe_transfer
*transfer
)
639 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
640 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
642 if (rtransfer
->staging_texture
) {
643 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
644 r600_copy_from_staging_texture(ctx
, rtransfer
);
646 pipe_resource_reference(&rtransfer
->staging_texture
, NULL
);
648 if (rtex
->flushed_depth_texture
) {
649 pipe_resource_reference((struct pipe_resource
**)&rtex
->flushed_depth_texture
, NULL
);
651 pipe_resource_reference(&transfer
->resource
, NULL
);
655 void* r600_texture_transfer_map(struct pipe_context
*ctx
,
656 struct pipe_transfer
* transfer
)
658 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
660 enum pipe_format format
= transfer
->resource
->format
;
661 struct radeon
*radeon
= (struct radeon
*)ctx
->screen
->winsys
;
666 if (rtransfer
->staging_texture
) {
667 bo
= ((struct r600_resource
*)rtransfer
->staging_texture
)->bo
;
669 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
671 if (rtex
->flushed_depth_texture
)
672 bo
= ((struct r600_resource
*)rtex
->flushed_depth_texture
)->bo
;
674 bo
= ((struct r600_resource
*)transfer
->resource
)->bo
;
676 offset
= rtransfer
->offset
+
677 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
678 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
681 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
682 usage
|= PB_USAGE_CPU_WRITE
;
684 if (transfer
->usage
& PIPE_TRANSFER_DISCARD
) {
687 if (transfer
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
) {
691 if (transfer
->usage
& PIPE_TRANSFER_READ
) {
692 usage
|= PB_USAGE_CPU_READ
;
695 if (transfer
->usage
& PIPE_TRANSFER_DONTBLOCK
) {
696 usage
|= PB_USAGE_DONTBLOCK
;
699 if (transfer
->usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) {
700 usage
|= PB_USAGE_UNSYNCHRONIZED
;
703 map
= r600_bo_map(radeon
, bo
, usage
, ctx
);
711 void r600_texture_transfer_unmap(struct pipe_context
*ctx
,
712 struct pipe_transfer
* transfer
)
714 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
715 struct radeon
*radeon
= (struct radeon
*)ctx
->screen
->winsys
;
718 if (rtransfer
->staging_texture
) {
719 bo
= ((struct r600_resource
*)rtransfer
->staging_texture
)->bo
;
721 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
723 if (rtex
->flushed_depth_texture
) {
724 bo
= ((struct r600_resource
*)rtex
->flushed_depth_texture
)->bo
;
726 bo
= ((struct r600_resource
*)transfer
->resource
)->bo
;
729 r600_bo_unmap(radeon
, bo
);
732 struct u_resource_vtbl r600_texture_vtbl
=
734 r600_texture_get_handle
, /* get_handle */
735 r600_texture_destroy
, /* resource_destroy */
736 r600_texture_is_referenced
, /* is_resource_referenced */
737 r600_texture_get_transfer
, /* get_transfer */
738 r600_texture_transfer_destroy
, /* transfer_destroy */
739 r600_texture_transfer_map
, /* transfer_map */
740 u_default_transfer_flush_region
,/* transfer_flush_region */
741 r600_texture_transfer_unmap
, /* transfer_unmap */
742 u_default_transfer_inline_write
/* transfer_inline_write */
745 void r600_init_surface_functions(struct r600_pipe_context
*r600
)
747 r600
->context
.create_surface
= r600_create_surface
;
748 r600
->context
.surface_destroy
= r600_surface_destroy
;
751 static unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format
,
752 const unsigned char *swizzle_view
)
755 unsigned char swizzle
[4];
757 const uint32_t swizzle_shift
[4] = {
760 const uint32_t swizzle_bit
[4] = {
765 /* Combine two sets of swizzles. */
766 for (i
= 0; i
< 4; i
++) {
767 swizzle
[i
] = swizzle_view
[i
] <= UTIL_FORMAT_SWIZZLE_W
?
768 swizzle_format
[swizzle_view
[i
]] : swizzle_view
[i
];
771 memcpy(swizzle
, swizzle_format
, 4);
775 for (i
= 0; i
< 4; i
++) {
776 switch (swizzle
[i
]) {
777 case UTIL_FORMAT_SWIZZLE_Y
:
778 result
|= swizzle_bit
[1] << swizzle_shift
[i
];
780 case UTIL_FORMAT_SWIZZLE_Z
:
781 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
783 case UTIL_FORMAT_SWIZZLE_W
:
784 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
786 case UTIL_FORMAT_SWIZZLE_0
:
787 result
|= V_038010_SQ_SEL_0
<< swizzle_shift
[i
];
789 case UTIL_FORMAT_SWIZZLE_1
:
790 result
|= V_038010_SQ_SEL_1
<< swizzle_shift
[i
];
792 default: /* UTIL_FORMAT_SWIZZLE_X */
793 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
799 /* texture format translate */
800 uint32_t r600_translate_texformat(enum pipe_format format
,
801 const unsigned char *swizzle_view
,
802 uint32_t *word4_p
, uint32_t *yuv_format_p
)
804 uint32_t result
= 0, word4
= 0, yuv_format
= 0;
805 const struct util_format_description
*desc
;
806 boolean uniform
= TRUE
;
808 const uint32_t sign_bit
[4] = {
809 S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED
),
810 S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED
),
811 S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED
),
812 S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED
)
814 desc
= util_format_description(format
);
816 word4
|= r600_get_swizzle_combined(desc
->swizzle
, swizzle_view
);
818 /* Colorspace (return non-RGB formats directly). */
819 switch (desc
->colorspace
) {
820 /* Depth stencil formats */
821 case UTIL_FORMAT_COLORSPACE_ZS
:
823 case PIPE_FORMAT_Z16_UNORM
:
826 case PIPE_FORMAT_X24S8_USCALED
:
827 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
828 case PIPE_FORMAT_Z24X8_UNORM
:
829 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
832 case PIPE_FORMAT_S8X24_USCALED
:
833 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
834 case PIPE_FORMAT_X8Z24_UNORM
:
835 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
838 case PIPE_FORMAT_S8_USCALED
:
840 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
846 case UTIL_FORMAT_COLORSPACE_YUV
:
847 yuv_format
|= (1 << 30);
849 case PIPE_FORMAT_UYVY
:
850 case PIPE_FORMAT_YUYV
:
854 goto out_unknown
; /* TODO */
856 case UTIL_FORMAT_COLORSPACE_SRGB
:
857 word4
|= S_038010_FORCE_DEGAMMA(1);
858 if (format
== PIPE_FORMAT_L8A8_SRGB
|| format
== PIPE_FORMAT_L8_SRGB
)
859 goto out_unknown
; /* fails for some reason - TODO */
866 /* S3TC formats. TODO */
867 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
868 static int r600_enable_s3tc
= -1;
870 if (r600_enable_s3tc
== -1)
872 debug_get_bool_option("R600_ENABLE_S3TC", FALSE
);
874 if (!r600_enable_s3tc
)
878 case PIPE_FORMAT_DXT1_RGB
:
879 case PIPE_FORMAT_DXT1_RGBA
:
882 case PIPE_FORMAT_DXT3_RGBA
:
885 case PIPE_FORMAT_DXT5_RGBA
:
894 for (i
= 0; i
< desc
->nr_channels
; i
++) {
895 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
896 word4
|= sign_bit
[i
];
900 /* R8G8Bx_SNORM - TODO CxV8U8 */
904 /* See whether the components are of the same size. */
905 for (i
= 1; i
< desc
->nr_channels
; i
++) {
906 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
909 /* Non-uniform formats. */
911 switch(desc
->nr_channels
) {
913 if (desc
->channel
[0].size
== 5 &&
914 desc
->channel
[1].size
== 6 &&
915 desc
->channel
[2].size
== 5) {
921 if (desc
->channel
[0].size
== 5 &&
922 desc
->channel
[1].size
== 5 &&
923 desc
->channel
[2].size
== 5 &&
924 desc
->channel
[3].size
== 1) {
925 result
= FMT_1_5_5_5
;
928 if (desc
->channel
[0].size
== 10 &&
929 desc
->channel
[1].size
== 10 &&
930 desc
->channel
[2].size
== 10 &&
931 desc
->channel
[3].size
== 2) {
932 result
= FMT_10_10_10_2
;
940 /* Find the first non-VOID channel. */
941 for (i
= 0; i
< 4; i
++) {
942 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
950 /* uniform formats */
951 switch (desc
->channel
[i
].type
) {
952 case UTIL_FORMAT_TYPE_UNSIGNED
:
953 case UTIL_FORMAT_TYPE_SIGNED
:
954 if (!desc
->channel
[i
].normalized
&&
955 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
959 switch (desc
->channel
[i
].size
) {
961 switch (desc
->nr_channels
) {
966 result
= FMT_4_4_4_4
;
971 switch (desc
->nr_channels
) {
979 result
= FMT_8_8_8_8
;
984 switch (desc
->nr_channels
) {
992 result
= FMT_16_16_16_16
;
998 case UTIL_FORMAT_TYPE_FLOAT
:
999 switch (desc
->channel
[i
].size
) {
1001 switch (desc
->nr_channels
) {
1003 result
= FMT_16_FLOAT
;
1006 result
= FMT_16_16_FLOAT
;
1009 result
= FMT_16_16_16_16_FLOAT
;
1014 switch (desc
->nr_channels
) {
1016 result
= FMT_32_FLOAT
;
1019 result
= FMT_32_32_FLOAT
;
1022 result
= FMT_32_32_32_32_FLOAT
;
1032 *yuv_format_p
= yuv_format
;
1035 // R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format));