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 /* Copy from a full GPU texture to a transfer's staging one. */
42 static void r600_copy_to_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
44 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
45 struct pipe_resource
*texture
= transfer
->resource
;
47 ctx
->resource_copy_region(ctx
, rtransfer
->staging_texture
,
48 0, 0, 0, 0, texture
, transfer
->level
,
53 /* Copy from a transfer's staging texture to a full GPU one. */
54 static void r600_copy_from_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
56 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
57 struct pipe_resource
*texture
= transfer
->resource
;
60 sbox
.x
= sbox
.y
= sbox
.z
= 0;
61 sbox
.width
= transfer
->box
.width
;
62 sbox
.height
= transfer
->box
.height
;
63 /* XXX that might be wrong */
65 ctx
->resource_copy_region(ctx
, texture
, transfer
->level
,
66 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
67 rtransfer
->staging_texture
,
70 ctx
->flush(ctx
, 0, NULL
);
73 unsigned r600_texture_get_offset(struct r600_resource_texture
*rtex
,
74 unsigned level
, unsigned layer
)
76 unsigned offset
= rtex
->offset
[level
];
78 switch (rtex
->resource
.b
.b
.b
.target
) {
80 case PIPE_TEXTURE_CUBE
:
81 return offset
+ layer
* rtex
->layer_size
[level
];
88 static unsigned r600_get_block_alignment(struct pipe_screen
*screen
,
89 enum pipe_format format
,
92 struct r600_screen
* rscreen
= (struct r600_screen
*)screen
;
93 unsigned pixsize
= util_format_get_blocksize(format
);
97 case V_038000_ARRAY_1D_TILED_THIN1
:
99 ((rscreen
->tiling_info
->group_bytes
/ 8 / pixsize
)));
101 case V_038000_ARRAY_2D_TILED_THIN1
:
102 p_align
= MAX2(rscreen
->tiling_info
->num_banks
,
103 (((rscreen
->tiling_info
->group_bytes
/ 8 / pixsize
)) *
104 rscreen
->tiling_info
->num_banks
)) * 8;
106 case V_038000_ARRAY_LINEAR_ALIGNED
:
107 p_align
= MAX2(64, rscreen
->tiling_info
->group_bytes
/ pixsize
);
109 case V_038000_ARRAY_LINEAR_GENERAL
:
111 p_align
= rscreen
->tiling_info
->group_bytes
/ pixsize
;
117 static unsigned r600_get_height_alignment(struct pipe_screen
*screen
,
120 struct r600_screen
* rscreen
= (struct r600_screen
*)screen
;
123 switch (array_mode
) {
124 case V_038000_ARRAY_2D_TILED_THIN1
:
125 h_align
= rscreen
->tiling_info
->num_channels
* 8;
127 case V_038000_ARRAY_1D_TILED_THIN1
:
128 case V_038000_ARRAY_LINEAR_ALIGNED
:
131 case V_038000_ARRAY_LINEAR_GENERAL
:
139 static unsigned r600_get_base_alignment(struct pipe_screen
*screen
,
140 enum pipe_format format
,
143 struct r600_screen
* rscreen
= (struct r600_screen
*)screen
;
144 unsigned pixsize
= util_format_get_blocksize(format
);
145 int p_align
= r600_get_block_alignment(screen
, format
, array_mode
);
146 int h_align
= r600_get_height_alignment(screen
, array_mode
);
149 switch (array_mode
) {
150 case V_038000_ARRAY_2D_TILED_THIN1
:
151 b_align
= MAX2(rscreen
->tiling_info
->num_banks
* rscreen
->tiling_info
->num_channels
* 8 * 8 * pixsize
,
152 p_align
* pixsize
* h_align
);
154 case V_038000_ARRAY_1D_TILED_THIN1
:
155 case V_038000_ARRAY_LINEAR_ALIGNED
:
156 case V_038000_ARRAY_LINEAR_GENERAL
:
158 b_align
= rscreen
->tiling_info
->group_bytes
;
164 static unsigned mip_minify(unsigned size
, unsigned level
)
167 val
= u_minify(size
, level
);
169 val
= util_next_power_of_two(val
);
173 static unsigned r600_texture_get_nblocksx(struct pipe_screen
*screen
,
174 struct r600_resource_texture
*rtex
,
177 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
178 unsigned nblocksx
, block_align
, width
;
179 unsigned blocksize
= util_format_get_blocksize(ptex
->format
);
181 if (rtex
->pitch_override
)
182 return rtex
->pitch_override
/ blocksize
;
184 width
= mip_minify(ptex
->width0
, level
);
185 nblocksx
= util_format_get_nblocksx(ptex
->format
, width
);
187 block_align
= r600_get_block_alignment(screen
, ptex
->format
,
188 rtex
->array_mode
[level
]);
189 nblocksx
= align(nblocksx
, block_align
);
193 static unsigned r600_texture_get_nblocksy(struct pipe_screen
*screen
,
194 struct r600_resource_texture
*rtex
,
197 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
198 unsigned height
, tile_height
;
200 height
= mip_minify(ptex
->height0
, level
);
201 height
= util_format_get_nblocksy(ptex
->format
, height
);
202 tile_height
= r600_get_height_alignment(screen
,
203 rtex
->array_mode
[level
]);
204 height
= align(height
, tile_height
);
208 static void r600_texture_set_array_mode(struct pipe_screen
*screen
,
209 struct r600_resource_texture
*rtex
,
210 unsigned level
, unsigned array_mode
)
212 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
214 switch (array_mode
) {
215 case V_0280A0_ARRAY_LINEAR_GENERAL
:
216 case V_0280A0_ARRAY_LINEAR_ALIGNED
:
217 case V_0280A0_ARRAY_1D_TILED_THIN1
:
219 rtex
->array_mode
[level
] = array_mode
;
221 case V_0280A0_ARRAY_2D_TILED_THIN1
:
223 unsigned w
, h
, tile_height
, tile_width
;
225 tile_height
= r600_get_height_alignment(screen
, array_mode
);
226 tile_width
= r600_get_block_alignment(screen
, ptex
->format
, array_mode
);
228 w
= mip_minify(ptex
->width0
, level
);
229 h
= mip_minify(ptex
->height0
, level
);
230 if (w
< tile_width
|| h
< tile_height
)
231 rtex
->array_mode
[level
] = V_0280A0_ARRAY_1D_TILED_THIN1
;
233 rtex
->array_mode
[level
] = array_mode
;
239 static void r600_setup_miptree(struct pipe_screen
*screen
,
240 struct r600_resource_texture
*rtex
,
243 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
244 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
245 enum chip_class chipc
= r600_get_family_class(radeon
);
246 unsigned size
, layer_size
, i
, offset
;
247 unsigned nblocksx
, nblocksy
;
249 for (i
= 0, offset
= 0; i
<= ptex
->last_level
; i
++) {
250 unsigned blocksize
= util_format_get_blocksize(ptex
->format
);
252 r600_texture_set_array_mode(screen
, rtex
, i
, array_mode
);
254 nblocksx
= r600_texture_get_nblocksx(screen
, rtex
, i
);
255 nblocksy
= r600_texture_get_nblocksy(screen
, rtex
, i
);
257 layer_size
= nblocksx
* nblocksy
* blocksize
;
258 if (ptex
->target
== PIPE_TEXTURE_CUBE
) {
260 size
= layer_size
* 8;
262 size
= layer_size
* 6;
265 size
= layer_size
* u_minify(ptex
->depth0
, i
);
266 /* align base image and start of miptree */
267 if ((i
== 0) || (i
== 1))
268 offset
= align(offset
, r600_get_base_alignment(screen
, ptex
->format
, array_mode
));
269 rtex
->offset
[i
] = offset
;
270 rtex
->layer_size
[i
] = layer_size
;
271 rtex
->pitch_in_blocks
[i
] = nblocksx
; /* CB talks in elements */
272 rtex
->pitch_in_bytes
[i
] = nblocksx
* blocksize
;
279 /* Figure out whether u_blitter will fallback to a transfer operation.
280 * If so, don't use a staging resource.
282 static boolean
permit_hardware_blit(struct pipe_screen
*screen
,
283 const struct pipe_resource
*res
)
287 if (util_format_is_depth_or_stencil(res
->format
))
288 bind
= PIPE_BIND_DEPTH_STENCIL
;
290 bind
= PIPE_BIND_RENDER_TARGET
;
292 if (!screen
->is_format_supported(screen
,
299 if (!screen
->is_format_supported(screen
,
303 PIPE_BIND_SAMPLER_VIEW
, 0))
309 static boolean
r600_texture_get_handle(struct pipe_screen
* screen
,
310 struct pipe_resource
*ptex
,
311 struct winsys_handle
*whandle
)
313 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)ptex
;
314 struct r600_resource
*resource
= &rtex
->resource
;
315 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
317 return r600_bo_get_winsys_handle(radeon
, resource
->bo
,
318 rtex
->pitch_in_bytes
[0], whandle
);
321 static void r600_texture_destroy(struct pipe_screen
*screen
,
322 struct pipe_resource
*ptex
)
324 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)ptex
;
325 struct r600_resource
*resource
= &rtex
->resource
;
326 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
328 if (rtex
->flushed_depth_texture
)
329 pipe_resource_reference((struct pipe_resource
**)&rtex
->flushed_depth_texture
, NULL
);
332 r600_bo_reference(radeon
, &resource
->bo
, NULL
);
337 static unsigned int r600_texture_is_referenced(struct pipe_context
*context
,
338 struct pipe_resource
*texture
,
339 unsigned level
, int layer
)
342 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
345 static const struct u_resource_vtbl r600_texture_vtbl
=
347 r600_texture_get_handle
, /* get_handle */
348 r600_texture_destroy
, /* resource_destroy */
349 r600_texture_is_referenced
, /* is_resource_referenced */
350 r600_texture_get_transfer
, /* get_transfer */
351 r600_texture_transfer_destroy
, /* transfer_destroy */
352 r600_texture_transfer_map
, /* transfer_map */
353 u_default_transfer_flush_region
,/* transfer_flush_region */
354 r600_texture_transfer_unmap
, /* transfer_unmap */
355 u_default_transfer_inline_write
/* transfer_inline_write */
358 static struct r600_resource_texture
*
359 r600_texture_create_object(struct pipe_screen
*screen
,
360 const struct pipe_resource
*base
,
362 unsigned pitch_in_bytes_override
,
363 unsigned max_buffer_size
,
366 struct r600_resource_texture
*rtex
;
367 struct r600_resource
*resource
;
368 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
370 rtex
= CALLOC_STRUCT(r600_resource_texture
);
374 resource
= &rtex
->resource
;
375 resource
->b
.b
.b
= *base
;
376 resource
->b
.b
.vtbl
= &r600_texture_vtbl
;
377 pipe_reference_init(&resource
->b
.b
.b
.reference
, 1);
378 resource
->b
.b
.b
.screen
= screen
;
380 rtex
->pitch_override
= pitch_in_bytes_override
;
381 /* only mark depth textures the HW can hit as depth textures */
382 if (util_format_is_depth_or_stencil(base
->format
) && permit_hardware_blit(screen
, base
))
385 r600_setup_miptree(screen
, rtex
, array_mode
);
387 resource
->size
= rtex
->size
;
390 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
391 int base_align
= r600_get_base_alignment(screen
, ptex
->format
, array_mode
);
393 resource
->bo
= r600_bo(radeon
, rtex
->size
, base_align
, base
->bind
, base
->usage
);
402 struct pipe_resource
*r600_texture_create(struct pipe_screen
*screen
,
403 const struct pipe_resource
*templ
)
405 unsigned array_mode
= 0;
406 static int force_tiling
= -1;
408 /* Would like some magic "get_bool_option_once" routine.
410 if (force_tiling
== -1)
411 force_tiling
= debug_get_bool_option("R600_FORCE_TILING", FALSE
);
413 if (force_tiling
&& permit_hardware_blit(screen
, templ
)) {
414 if (!(templ
->flags
& R600_RESOURCE_FLAG_TRANSFER
) &&
415 !(templ
->bind
& PIPE_BIND_SCANOUT
)) {
416 array_mode
= V_038000_ARRAY_2D_TILED_THIN1
;
420 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
, array_mode
,
425 static struct pipe_surface
*r600_create_surface(struct pipe_context
*pipe
,
426 struct pipe_resource
*texture
,
427 const struct pipe_surface
*surf_tmpl
)
429 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
430 struct r600_surface
*surface
= CALLOC_STRUCT(r600_surface
);
431 unsigned level
= surf_tmpl
->u
.tex
.level
;
433 assert(surf_tmpl
->u
.tex
.first_layer
== surf_tmpl
->u
.tex
.last_layer
);
437 /* offset = r600_texture_get_offset(rtex, level, surf_tmpl->u.tex.first_layer);*/
438 pipe_reference_init(&surface
->base
.reference
, 1);
439 pipe_resource_reference(&surface
->base
.texture
, texture
);
440 surface
->base
.context
= pipe
;
441 surface
->base
.format
= surf_tmpl
->format
;
442 surface
->base
.width
= mip_minify(texture
->width0
, level
);
443 surface
->base
.height
= mip_minify(texture
->height0
, level
);
444 surface
->base
.usage
= surf_tmpl
->usage
;
445 surface
->base
.texture
= texture
;
446 surface
->base
.u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
447 surface
->base
.u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
448 surface
->base
.u
.tex
.level
= level
;
450 surface
->aligned_height
= r600_texture_get_nblocksy(pipe
->screen
,
452 return &surface
->base
;
455 static void r600_surface_destroy(struct pipe_context
*pipe
,
456 struct pipe_surface
*surface
)
458 pipe_resource_reference(&surface
->texture
, NULL
);
463 struct pipe_resource
*r600_texture_from_handle(struct pipe_screen
*screen
,
464 const struct pipe_resource
*templ
,
465 struct winsys_handle
*whandle
)
467 struct radeon
*rw
= (struct radeon
*)screen
->winsys
;
468 struct r600_bo
*bo
= NULL
;
469 unsigned array_mode
= 0;
471 /* Support only 2D textures without mipmaps */
472 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
) ||
473 templ
->depth0
!= 1 || templ
->last_level
!= 0)
476 bo
= r600_bo_handle(rw
, whandle
->handle
, &array_mode
);
481 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
, array_mode
,
487 int r600_texture_depth_flush(struct pipe_context
*ctx
,
488 struct pipe_resource
*texture
, boolean just_create
)
490 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
491 struct pipe_resource resource
;
493 if (rtex
->flushed_depth_texture
)
496 resource
.target
= PIPE_TEXTURE_2D
;
497 resource
.format
= texture
->format
;
498 resource
.width0
= texture
->width0
;
499 resource
.height0
= texture
->height0
;
501 resource
.last_level
= texture
->last_level
;
502 resource
.nr_samples
= 0;
503 resource
.usage
= PIPE_USAGE_DYNAMIC
;
505 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
;
507 resource
.bind
|= PIPE_BIND_DEPTH_STENCIL
;
509 rtex
->flushed_depth_texture
= (struct r600_resource_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
510 if (rtex
->flushed_depth_texture
== NULL
) {
511 R600_ERR("failed to create temporary texture to hold untiled copy\n");
515 ((struct r600_resource_texture
*)rtex
->flushed_depth_texture
)->is_flushing_texture
= TRUE
;
520 /* XXX: only do this if the depth texture has actually changed:
522 r600_blit_uncompress_depth(ctx
, rtex
);
526 /* Needs adjustment for pixelformat:
528 static INLINE
unsigned u_box_volume( const struct pipe_box
*box
)
530 return box
->width
* box
->depth
* box
->height
;
533 struct pipe_transfer
* r600_texture_get_transfer(struct pipe_context
*ctx
,
534 struct pipe_resource
*texture
,
537 const struct pipe_box
*box
)
539 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
540 struct pipe_resource resource
;
541 struct r600_transfer
*trans
;
543 boolean use_staging_texture
= FALSE
;
545 /* We cannot map a tiled texture directly because the data is
546 * in a different order, therefore we do detiling using a blit.
548 * Also, use a temporary in GTT memory for read transfers, as
549 * the CPU is much happier reading out of cached system memory
550 * than uncached VRAM.
552 if (R600_TEX_IS_TILED(rtex
, level
))
553 use_staging_texture
= TRUE
;
555 if ((usage
& PIPE_TRANSFER_READ
) && u_box_volume(box
) > 1024)
556 use_staging_texture
= TRUE
;
558 /* XXX: Use a staging texture for uploads if the underlying BO
559 * is busy. No interface for checking that currently? so do
560 * it eagerly whenever the transfer doesn't require a readback
563 if ((usage
& PIPE_TRANSFER_WRITE
) &&
564 !(usage
& (PIPE_TRANSFER_READ
|
565 PIPE_TRANSFER_DONTBLOCK
|
566 PIPE_TRANSFER_UNSYNCHRONIZED
)))
567 use_staging_texture
= TRUE
;
569 if (!permit_hardware_blit(ctx
->screen
, texture
) ||
570 (texture
->flags
& R600_RESOURCE_FLAG_TRANSFER
))
571 use_staging_texture
= FALSE
;
573 trans
= CALLOC_STRUCT(r600_transfer
);
576 pipe_resource_reference(&trans
->transfer
.resource
, texture
);
577 trans
->transfer
.level
= level
;
578 trans
->transfer
.usage
= usage
;
579 trans
->transfer
.box
= *box
;
581 /* XXX: only readback the rectangle which is being mapped?
583 /* XXX: when discard is true, no need to read back from depth texture
585 r
= r600_texture_depth_flush(ctx
, texture
, FALSE
);
587 R600_ERR("failed to create temporary texture to hold untiled copy\n");
588 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
592 trans
->transfer
.stride
= rtex
->flushed_depth_texture
->pitch_in_bytes
[level
];
593 trans
->offset
= r600_texture_get_offset(rtex
->flushed_depth_texture
, level
, box
->z
);
594 return &trans
->transfer
;
595 } else if (use_staging_texture
) {
596 resource
.target
= PIPE_TEXTURE_2D
;
597 resource
.format
= texture
->format
;
598 resource
.width0
= box
->width
;
599 resource
.height0
= box
->height
;
601 resource
.array_size
= 1;
602 resource
.last_level
= 0;
603 resource
.nr_samples
= 0;
604 resource
.usage
= PIPE_USAGE_STAGING
;
606 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
;
607 /* For texture reading, the temporary (detiled) texture is used as
608 * a render target when blitting from a tiled texture. */
609 if (usage
& PIPE_TRANSFER_READ
) {
610 resource
.bind
|= PIPE_BIND_RENDER_TARGET
;
612 /* For texture writing, the temporary texture is used as a sampler
613 * when blitting into a tiled texture. */
614 if (usage
& PIPE_TRANSFER_WRITE
) {
615 resource
.bind
|= PIPE_BIND_SAMPLER_VIEW
;
617 /* Create the temporary texture. */
618 trans
->staging_texture
= ctx
->screen
->resource_create(ctx
->screen
, &resource
);
619 if (trans
->staging_texture
== NULL
) {
620 R600_ERR("failed to create temporary texture to hold untiled copy\n");
621 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
626 trans
->transfer
.stride
=
627 ((struct r600_resource_texture
*)trans
->staging_texture
)->pitch_in_bytes
[0];
628 if (usage
& PIPE_TRANSFER_READ
) {
629 r600_copy_to_staging_texture(ctx
, trans
);
630 /* Always referenced in the blit. */
631 ctx
->flush(ctx
, 0, NULL
);
633 return &trans
->transfer
;
635 trans
->transfer
.stride
= rtex
->pitch_in_bytes
[level
];
636 trans
->offset
= r600_texture_get_offset(rtex
, level
, box
->z
);
637 return &trans
->transfer
;
640 void r600_texture_transfer_destroy(struct pipe_context
*ctx
,
641 struct pipe_transfer
*transfer
)
643 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
644 struct pipe_resource
*texture
= transfer
->resource
;
645 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
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
);
654 if (rtex
->depth
&& !rtex
->is_flushing_texture
) {
655 if ((transfer
->usage
& PIPE_TRANSFER_WRITE
) && rtex
->flushed_depth_texture
)
656 r600_blit_push_depth(ctx
, rtex
);
659 pipe_resource_reference(&transfer
->resource
, NULL
);
663 void* r600_texture_transfer_map(struct pipe_context
*ctx
,
664 struct pipe_transfer
* transfer
)
666 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
668 enum pipe_format format
= transfer
->resource
->format
;
669 struct radeon
*radeon
= (struct radeon
*)ctx
->screen
->winsys
;
674 if (rtransfer
->staging_texture
) {
675 bo
= ((struct r600_resource
*)rtransfer
->staging_texture
)->bo
;
677 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
679 if (rtex
->flushed_depth_texture
)
680 bo
= ((struct r600_resource
*)rtex
->flushed_depth_texture
)->bo
;
682 bo
= ((struct r600_resource
*)transfer
->resource
)->bo
;
684 offset
= rtransfer
->offset
+
685 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
686 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
689 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
690 usage
|= PB_USAGE_CPU_WRITE
;
692 if (transfer
->usage
& PIPE_TRANSFER_DISCARD
) {
695 if (transfer
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
) {
699 if (transfer
->usage
& PIPE_TRANSFER_READ
) {
700 usage
|= PB_USAGE_CPU_READ
;
703 if (transfer
->usage
& PIPE_TRANSFER_DONTBLOCK
) {
704 usage
|= PB_USAGE_DONTBLOCK
;
707 if (transfer
->usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) {
708 usage
|= PB_USAGE_UNSYNCHRONIZED
;
711 map
= r600_bo_map(radeon
, bo
, usage
, ctx
);
719 void r600_texture_transfer_unmap(struct pipe_context
*ctx
,
720 struct pipe_transfer
* transfer
)
722 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
723 struct radeon
*radeon
= (struct radeon
*)ctx
->screen
->winsys
;
726 if (rtransfer
->staging_texture
) {
727 bo
= ((struct r600_resource
*)rtransfer
->staging_texture
)->bo
;
729 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
731 if (rtex
->flushed_depth_texture
) {
732 bo
= ((struct r600_resource
*)rtex
->flushed_depth_texture
)->bo
;
734 bo
= ((struct r600_resource
*)transfer
->resource
)->bo
;
737 r600_bo_unmap(radeon
, bo
);
740 void r600_init_surface_functions(struct r600_pipe_context
*r600
)
742 r600
->context
.create_surface
= r600_create_surface
;
743 r600
->context
.surface_destroy
= r600_surface_destroy
;
746 static unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format
,
747 const unsigned char *swizzle_view
)
750 unsigned char swizzle
[4];
752 const uint32_t swizzle_shift
[4] = {
755 const uint32_t swizzle_bit
[4] = {
760 /* Combine two sets of swizzles. */
761 for (i
= 0; i
< 4; i
++) {
762 swizzle
[i
] = swizzle_view
[i
] <= UTIL_FORMAT_SWIZZLE_W
?
763 swizzle_format
[swizzle_view
[i
]] : swizzle_view
[i
];
766 memcpy(swizzle
, swizzle_format
, 4);
770 for (i
= 0; i
< 4; i
++) {
771 switch (swizzle
[i
]) {
772 case UTIL_FORMAT_SWIZZLE_Y
:
773 result
|= swizzle_bit
[1] << swizzle_shift
[i
];
775 case UTIL_FORMAT_SWIZZLE_Z
:
776 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
778 case UTIL_FORMAT_SWIZZLE_W
:
779 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
781 case UTIL_FORMAT_SWIZZLE_0
:
782 result
|= V_038010_SQ_SEL_0
<< swizzle_shift
[i
];
784 case UTIL_FORMAT_SWIZZLE_1
:
785 result
|= V_038010_SQ_SEL_1
<< swizzle_shift
[i
];
787 default: /* UTIL_FORMAT_SWIZZLE_X */
788 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
794 /* texture format translate */
795 uint32_t r600_translate_texformat(enum pipe_format format
,
796 const unsigned char *swizzle_view
,
797 uint32_t *word4_p
, uint32_t *yuv_format_p
)
799 uint32_t result
= 0, word4
= 0, yuv_format
= 0;
800 const struct util_format_description
*desc
;
801 boolean uniform
= TRUE
;
803 const uint32_t sign_bit
[4] = {
804 S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED
),
805 S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED
),
806 S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED
),
807 S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED
)
809 desc
= util_format_description(format
);
811 word4
|= r600_get_swizzle_combined(desc
->swizzle
, swizzle_view
);
813 /* Colorspace (return non-RGB formats directly). */
814 switch (desc
->colorspace
) {
815 /* Depth stencil formats */
816 case UTIL_FORMAT_COLORSPACE_ZS
:
818 case PIPE_FORMAT_Z16_UNORM
:
821 case PIPE_FORMAT_X24S8_USCALED
:
822 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
823 case PIPE_FORMAT_Z24X8_UNORM
:
824 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
827 case PIPE_FORMAT_S8X24_USCALED
:
828 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
829 case PIPE_FORMAT_X8Z24_UNORM
:
830 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
833 case PIPE_FORMAT_S8_USCALED
:
835 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
841 case UTIL_FORMAT_COLORSPACE_YUV
:
842 yuv_format
|= (1 << 30);
844 case PIPE_FORMAT_UYVY
:
845 case PIPE_FORMAT_YUYV
:
849 goto out_unknown
; /* TODO */
851 case UTIL_FORMAT_COLORSPACE_SRGB
:
852 word4
|= S_038010_FORCE_DEGAMMA(1);
853 if (format
== PIPE_FORMAT_L8A8_SRGB
|| format
== PIPE_FORMAT_L8_SRGB
)
854 goto out_unknown
; /* fails for some reason - TODO */
861 /* S3TC formats. TODO */
862 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
863 static int r600_enable_s3tc
= -1;
865 if (r600_enable_s3tc
== -1)
867 debug_get_bool_option("R600_ENABLE_S3TC", FALSE
);
869 if (!r600_enable_s3tc
)
873 case PIPE_FORMAT_DXT1_RGB
:
874 case PIPE_FORMAT_DXT1_RGBA
:
877 case PIPE_FORMAT_DXT3_RGBA
:
880 case PIPE_FORMAT_DXT5_RGBA
:
889 for (i
= 0; i
< desc
->nr_channels
; i
++) {
890 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
891 word4
|= sign_bit
[i
];
895 /* R8G8Bx_SNORM - TODO CxV8U8 */
899 /* See whether the components are of the same size. */
900 for (i
= 1; i
< desc
->nr_channels
; i
++) {
901 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
904 /* Non-uniform formats. */
906 switch(desc
->nr_channels
) {
908 if (desc
->channel
[0].size
== 5 &&
909 desc
->channel
[1].size
== 6 &&
910 desc
->channel
[2].size
== 5) {
916 if (desc
->channel
[0].size
== 5 &&
917 desc
->channel
[1].size
== 5 &&
918 desc
->channel
[2].size
== 5 &&
919 desc
->channel
[3].size
== 1) {
920 result
= FMT_1_5_5_5
;
923 if (desc
->channel
[0].size
== 10 &&
924 desc
->channel
[1].size
== 10 &&
925 desc
->channel
[2].size
== 10 &&
926 desc
->channel
[3].size
== 2) {
927 result
= FMT_2_10_10_10
;
935 /* Find the first non-VOID channel. */
936 for (i
= 0; i
< 4; i
++) {
937 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
945 /* uniform formats */
946 switch (desc
->channel
[i
].type
) {
947 case UTIL_FORMAT_TYPE_UNSIGNED
:
948 case UTIL_FORMAT_TYPE_SIGNED
:
949 if (!desc
->channel
[i
].normalized
&&
950 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
954 switch (desc
->channel
[i
].size
) {
956 switch (desc
->nr_channels
) {
961 result
= FMT_4_4_4_4
;
966 switch (desc
->nr_channels
) {
974 result
= FMT_8_8_8_8
;
979 switch (desc
->nr_channels
) {
987 result
= FMT_16_16_16_16
;
992 switch (desc
->nr_channels
) {
1000 result
= FMT_32_32_32_32
;
1006 case UTIL_FORMAT_TYPE_FLOAT
:
1007 switch (desc
->channel
[i
].size
) {
1009 switch (desc
->nr_channels
) {
1011 result
= FMT_16_FLOAT
;
1014 result
= FMT_16_16_FLOAT
;
1017 result
= FMT_16_16_16_16_FLOAT
;
1022 switch (desc
->nr_channels
) {
1024 result
= FMT_32_FLOAT
;
1027 result
= FMT_32_32_FLOAT
;
1030 result
= FMT_32_32_32_32_FLOAT
;
1040 *yuv_format_p
= yuv_format
;
1043 // R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format));