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_format_s3tc.h>
31 #include <util/u_math.h>
32 #include <util/u_inlines.h>
33 #include <util/u_memory.h>
34 #include "state_tracker/drm_driver.h"
35 #include "pipebuffer/pb_buffer.h"
36 #include "r600_pipe.h"
37 #include "r600_resource.h"
38 #include "r600_state_inlines.h"
40 #include "r600_formats.h"
42 /* Copy from a full GPU texture to a transfer's staging one. */
43 static void r600_copy_to_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
45 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
46 struct pipe_resource
*texture
= transfer
->resource
;
48 ctx
->resource_copy_region(ctx
, rtransfer
->staging_texture
,
49 0, 0, 0, 0, texture
, transfer
->level
,
54 /* Copy from a transfer's staging texture to a full GPU one. */
55 static void r600_copy_from_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
57 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
58 struct pipe_resource
*texture
= transfer
->resource
;
61 sbox
.x
= sbox
.y
= sbox
.z
= 0;
62 sbox
.width
= transfer
->box
.width
;
63 sbox
.height
= transfer
->box
.height
;
64 /* XXX that might be wrong */
66 ctx
->resource_copy_region(ctx
, texture
, transfer
->level
,
67 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
68 rtransfer
->staging_texture
,
71 ctx
->flush(ctx
, 0, NULL
);
74 unsigned r600_texture_get_offset(struct r600_resource_texture
*rtex
,
75 unsigned level
, unsigned layer
)
77 unsigned offset
= rtex
->offset
[level
];
79 switch (rtex
->resource
.b
.b
.b
.target
) {
81 case PIPE_TEXTURE_CUBE
:
83 return offset
+ layer
* rtex
->layer_size
[level
];
87 static unsigned r600_get_block_alignment(struct pipe_screen
*screen
,
88 enum pipe_format format
,
91 struct r600_screen
* rscreen
= (struct r600_screen
*)screen
;
92 unsigned pixsize
= util_format_get_blocksize(format
);
96 case V_038000_ARRAY_1D_TILED_THIN1
:
98 ((rscreen
->tiling_info
->group_bytes
/ 8 / pixsize
)));
100 case V_038000_ARRAY_2D_TILED_THIN1
:
101 p_align
= MAX2(rscreen
->tiling_info
->num_banks
,
102 (((rscreen
->tiling_info
->group_bytes
/ 8 / pixsize
)) *
103 rscreen
->tiling_info
->num_banks
)) * 8;
105 case V_038000_ARRAY_LINEAR_ALIGNED
:
106 p_align
= MAX2(64, rscreen
->tiling_info
->group_bytes
/ pixsize
);
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
:
127 case V_038000_ARRAY_LINEAR_ALIGNED
:
130 case V_038000_ARRAY_LINEAR_GENERAL
:
138 static unsigned r600_get_base_alignment(struct pipe_screen
*screen
,
139 enum pipe_format format
,
142 struct r600_screen
* rscreen
= (struct r600_screen
*)screen
;
143 unsigned pixsize
= util_format_get_blocksize(format
);
144 int p_align
= r600_get_block_alignment(screen
, format
, array_mode
);
145 int h_align
= r600_get_height_alignment(screen
, array_mode
);
148 switch (array_mode
) {
149 case V_038000_ARRAY_2D_TILED_THIN1
:
150 b_align
= MAX2(rscreen
->tiling_info
->num_banks
* rscreen
->tiling_info
->num_channels
* 8 * 8 * pixsize
,
151 p_align
* pixsize
* h_align
);
153 case V_038000_ARRAY_1D_TILED_THIN1
:
154 case V_038000_ARRAY_LINEAR_ALIGNED
:
155 case V_038000_ARRAY_LINEAR_GENERAL
:
157 b_align
= rscreen
->tiling_info
->group_bytes
;
163 static unsigned mip_minify(unsigned size
, unsigned level
)
166 val
= u_minify(size
, level
);
168 val
= util_next_power_of_two(val
);
172 static unsigned r600_texture_get_nblocksx(struct pipe_screen
*screen
,
173 struct r600_resource_texture
*rtex
,
176 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
177 unsigned nblocksx
, block_align
, width
;
178 unsigned blocksize
= util_format_get_blocksize(ptex
->format
);
180 if (rtex
->pitch_override
)
181 return rtex
->pitch_override
/ blocksize
;
183 width
= mip_minify(ptex
->width0
, level
);
184 nblocksx
= util_format_get_nblocksx(ptex
->format
, width
);
186 block_align
= r600_get_block_alignment(screen
, ptex
->format
,
187 rtex
->array_mode
[level
]);
188 nblocksx
= align(nblocksx
, block_align
);
192 static unsigned r600_texture_get_nblocksy(struct pipe_screen
*screen
,
193 struct r600_resource_texture
*rtex
,
196 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
197 unsigned height
, tile_height
;
199 height
= mip_minify(ptex
->height0
, level
);
200 height
= util_format_get_nblocksy(ptex
->format
, height
);
201 tile_height
= r600_get_height_alignment(screen
,
202 rtex
->array_mode
[level
]);
203 height
= align(height
, tile_height
);
207 static void r600_texture_set_array_mode(struct pipe_screen
*screen
,
208 struct r600_resource_texture
*rtex
,
209 unsigned level
, unsigned array_mode
)
211 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
213 switch (array_mode
) {
214 case V_0280A0_ARRAY_LINEAR_GENERAL
:
215 case V_0280A0_ARRAY_LINEAR_ALIGNED
:
216 case V_0280A0_ARRAY_1D_TILED_THIN1
:
218 rtex
->array_mode
[level
] = array_mode
;
220 case V_0280A0_ARRAY_2D_TILED_THIN1
:
222 unsigned w
, h
, tile_height
, tile_width
;
224 tile_height
= r600_get_height_alignment(screen
, array_mode
);
225 tile_width
= r600_get_block_alignment(screen
, ptex
->format
, array_mode
);
227 w
= mip_minify(ptex
->width0
, level
);
228 h
= mip_minify(ptex
->height0
, level
);
229 if (w
<= tile_width
|| h
<= tile_height
)
230 rtex
->array_mode
[level
] = V_0280A0_ARRAY_1D_TILED_THIN1
;
232 rtex
->array_mode
[level
] = array_mode
;
238 static void r600_setup_miptree(struct pipe_screen
*screen
,
239 struct r600_resource_texture
*rtex
,
242 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
243 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
244 enum chip_class chipc
= r600_get_family_class(radeon
);
245 unsigned size
, layer_size
, i
, offset
;
246 unsigned nblocksx
, nblocksy
;
248 for (i
= 0, offset
= 0; i
<= ptex
->last_level
; i
++) {
249 unsigned blocksize
= util_format_get_blocksize(ptex
->format
);
251 r600_texture_set_array_mode(screen
, rtex
, i
, array_mode
);
253 nblocksx
= r600_texture_get_nblocksx(screen
, rtex
, i
);
254 nblocksy
= r600_texture_get_nblocksy(screen
, rtex
, i
);
256 layer_size
= nblocksx
* nblocksy
* blocksize
;
257 if (ptex
->target
== PIPE_TEXTURE_CUBE
) {
259 size
= layer_size
* 8;
261 size
= layer_size
* 6;
263 else if (ptex
->target
== PIPE_TEXTURE_3D
)
264 size
= layer_size
* u_minify(ptex
->depth0
, i
);
266 size
= layer_size
* ptex
->array_size
;
268 /* align base image and start of miptree */
269 if ((i
== 0) || (i
== 1))
270 offset
= align(offset
, r600_get_base_alignment(screen
, ptex
->format
, array_mode
));
271 rtex
->offset
[i
] = offset
;
272 rtex
->layer_size
[i
] = layer_size
;
273 rtex
->pitch_in_blocks
[i
] = nblocksx
; /* CB talks in elements */
274 rtex
->pitch_in_bytes
[i
] = nblocksx
* blocksize
;
281 /* Figure out whether u_blitter will fallback to a transfer operation.
282 * If so, don't use a staging resource.
284 static boolean
permit_hardware_blit(struct pipe_screen
*screen
,
285 const struct pipe_resource
*res
)
289 if (util_format_is_depth_or_stencil(res
->format
))
290 bind
= PIPE_BIND_DEPTH_STENCIL
;
292 bind
= PIPE_BIND_RENDER_TARGET
;
294 /* hackaround for S3TC */
295 if (util_format_is_compressed(res
->format
))
298 if (!screen
->is_format_supported(screen
,
305 if (!screen
->is_format_supported(screen
,
309 PIPE_BIND_SAMPLER_VIEW
, 0))
312 switch (res
->usage
) {
313 case PIPE_USAGE_STREAM
:
314 case PIPE_USAGE_STAGING
:
315 case PIPE_USAGE_STATIC
:
316 case PIPE_USAGE_IMMUTABLE
:
324 static boolean
r600_texture_get_handle(struct pipe_screen
* screen
,
325 struct pipe_resource
*ptex
,
326 struct winsys_handle
*whandle
)
328 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)ptex
;
329 struct r600_resource
*resource
= &rtex
->resource
;
330 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
332 return r600_bo_get_winsys_handle(radeon
, resource
->bo
,
333 rtex
->pitch_in_bytes
[0], whandle
);
336 static void r600_texture_destroy(struct pipe_screen
*screen
,
337 struct pipe_resource
*ptex
)
339 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)ptex
;
340 struct r600_resource
*resource
= &rtex
->resource
;
341 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
343 if (rtex
->flushed_depth_texture
)
344 pipe_resource_reference((struct pipe_resource
**)&rtex
->flushed_depth_texture
, NULL
);
347 r600_bo_reference(radeon
, &resource
->bo
, NULL
);
352 static unsigned int r600_texture_is_referenced(struct pipe_context
*context
,
353 struct pipe_resource
*texture
,
354 unsigned level
, int layer
)
357 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
360 static const struct u_resource_vtbl r600_texture_vtbl
=
362 r600_texture_get_handle
, /* get_handle */
363 r600_texture_destroy
, /* resource_destroy */
364 r600_texture_is_referenced
, /* is_resource_referenced */
365 r600_texture_get_transfer
, /* get_transfer */
366 r600_texture_transfer_destroy
, /* transfer_destroy */
367 r600_texture_transfer_map
, /* transfer_map */
368 u_default_transfer_flush_region
,/* transfer_flush_region */
369 r600_texture_transfer_unmap
, /* transfer_unmap */
370 u_default_transfer_inline_write
/* transfer_inline_write */
373 static struct r600_resource_texture
*
374 r600_texture_create_object(struct pipe_screen
*screen
,
375 const struct pipe_resource
*base
,
377 unsigned pitch_in_bytes_override
,
378 unsigned max_buffer_size
,
381 struct r600_resource_texture
*rtex
;
382 struct r600_resource
*resource
;
383 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
385 rtex
= CALLOC_STRUCT(r600_resource_texture
);
389 resource
= &rtex
->resource
;
390 resource
->b
.b
.b
= *base
;
391 resource
->b
.b
.vtbl
= &r600_texture_vtbl
;
392 pipe_reference_init(&resource
->b
.b
.b
.reference
, 1);
393 resource
->b
.b
.b
.screen
= screen
;
395 rtex
->pitch_override
= pitch_in_bytes_override
;
396 /* only mark depth textures the HW can hit as depth textures */
397 if (util_format_is_depth_or_stencil(base
->format
) && permit_hardware_blit(screen
, base
))
400 r600_setup_miptree(screen
, rtex
, array_mode
);
402 resource
->size
= rtex
->size
;
405 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
406 int base_align
= r600_get_base_alignment(screen
, ptex
->format
, array_mode
);
408 resource
->bo
= r600_bo(radeon
, rtex
->size
, base_align
, base
->bind
, base
->usage
);
417 struct pipe_resource
*r600_texture_create(struct pipe_screen
*screen
,
418 const struct pipe_resource
*templ
)
420 unsigned array_mode
= 0;
421 static int force_tiling
= -1;
423 /* Would like some magic "get_bool_option_once" routine.
425 if (force_tiling
== -1) {
426 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
427 /* reenable when 2D tiling is fixed better */
428 /*if (r600_get_minor_version(rscreen->radeon) >= 9)
429 force_tiling = debug_get_bool_option("R600_TILING", TRUE);*/
430 force_tiling
= debug_get_bool_option("R600_TILING", FALSE
);
433 if (force_tiling
&& permit_hardware_blit(screen
, templ
)) {
434 if (!(templ
->flags
& R600_RESOURCE_FLAG_TRANSFER
) &&
435 !(templ
->bind
& PIPE_BIND_SCANOUT
)) {
436 array_mode
= V_038000_ARRAY_2D_TILED_THIN1
;
440 if (!(templ
->flags
& R600_RESOURCE_FLAG_TRANSFER
) &&
441 util_format_is_compressed(templ
->format
))
442 array_mode
= V_038000_ARRAY_1D_TILED_THIN1
;
444 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
, array_mode
,
449 static struct pipe_surface
*r600_create_surface(struct pipe_context
*pipe
,
450 struct pipe_resource
*texture
,
451 const struct pipe_surface
*surf_tmpl
)
453 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
454 struct r600_surface
*surface
= CALLOC_STRUCT(r600_surface
);
455 unsigned level
= surf_tmpl
->u
.tex
.level
;
457 assert(surf_tmpl
->u
.tex
.first_layer
== surf_tmpl
->u
.tex
.last_layer
);
461 /* offset = r600_texture_get_offset(rtex, level, surf_tmpl->u.tex.first_layer);*/
462 pipe_reference_init(&surface
->base
.reference
, 1);
463 pipe_resource_reference(&surface
->base
.texture
, texture
);
464 surface
->base
.context
= pipe
;
465 surface
->base
.format
= surf_tmpl
->format
;
466 surface
->base
.width
= mip_minify(texture
->width0
, level
);
467 surface
->base
.height
= mip_minify(texture
->height0
, level
);
468 surface
->base
.usage
= surf_tmpl
->usage
;
469 surface
->base
.texture
= texture
;
470 surface
->base
.u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
471 surface
->base
.u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
472 surface
->base
.u
.tex
.level
= level
;
474 surface
->aligned_height
= r600_texture_get_nblocksy(pipe
->screen
,
476 return &surface
->base
;
479 static void r600_surface_destroy(struct pipe_context
*pipe
,
480 struct pipe_surface
*surface
)
482 pipe_resource_reference(&surface
->texture
, NULL
);
487 struct pipe_resource
*r600_texture_from_handle(struct pipe_screen
*screen
,
488 const struct pipe_resource
*templ
,
489 struct winsys_handle
*whandle
)
491 struct radeon
*rw
= (struct radeon
*)screen
->winsys
;
492 struct r600_bo
*bo
= NULL
;
493 unsigned array_mode
= 0;
495 /* Support only 2D textures without mipmaps */
496 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
) ||
497 templ
->depth0
!= 1 || templ
->last_level
!= 0)
500 bo
= r600_bo_handle(rw
, whandle
->handle
, &array_mode
);
505 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
, array_mode
,
511 int r600_texture_depth_flush(struct pipe_context
*ctx
,
512 struct pipe_resource
*texture
, boolean just_create
)
514 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
515 struct pipe_resource resource
;
517 if (rtex
->flushed_depth_texture
)
520 resource
.target
= PIPE_TEXTURE_2D
;
521 resource
.format
= texture
->format
;
522 resource
.width0
= texture
->width0
;
523 resource
.height0
= texture
->height0
;
525 resource
.array_size
= 1;
526 resource
.last_level
= texture
->last_level
;
527 resource
.nr_samples
= 0;
528 resource
.usage
= PIPE_USAGE_DYNAMIC
;
530 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
;
532 resource
.bind
|= PIPE_BIND_DEPTH_STENCIL
;
534 rtex
->flushed_depth_texture
= (struct r600_resource_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
535 if (rtex
->flushed_depth_texture
== NULL
) {
536 R600_ERR("failed to create temporary texture to hold untiled copy\n");
540 ((struct r600_resource_texture
*)rtex
->flushed_depth_texture
)->is_flushing_texture
= TRUE
;
545 /* XXX: only do this if the depth texture has actually changed:
547 r600_blit_uncompress_depth(ctx
, rtex
);
551 /* Needs adjustment for pixelformat:
553 static INLINE
unsigned u_box_volume( const struct pipe_box
*box
)
555 return box
->width
* box
->depth
* box
->height
;
558 struct pipe_transfer
* r600_texture_get_transfer(struct pipe_context
*ctx
,
559 struct pipe_resource
*texture
,
562 const struct pipe_box
*box
)
564 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
565 struct pipe_resource resource
;
566 struct r600_transfer
*trans
;
568 boolean use_staging_texture
= FALSE
;
570 /* We cannot map a tiled texture directly because the data is
571 * in a different order, therefore we do detiling using a blit.
573 * Also, use a temporary in GTT memory for read transfers, as
574 * the CPU is much happier reading out of cached system memory
575 * than uncached VRAM.
577 if (R600_TEX_IS_TILED(rtex
, level
))
578 use_staging_texture
= TRUE
;
580 if ((usage
& PIPE_TRANSFER_READ
) && u_box_volume(box
) > 1024)
581 use_staging_texture
= TRUE
;
583 /* XXX: Use a staging texture for uploads if the underlying BO
584 * is busy. No interface for checking that currently? so do
585 * it eagerly whenever the transfer doesn't require a readback
588 if ((usage
& PIPE_TRANSFER_WRITE
) &&
589 !(usage
& (PIPE_TRANSFER_READ
|
590 PIPE_TRANSFER_DONTBLOCK
|
591 PIPE_TRANSFER_UNSYNCHRONIZED
)))
592 use_staging_texture
= TRUE
;
594 if (!permit_hardware_blit(ctx
->screen
, texture
) ||
595 (texture
->flags
& R600_RESOURCE_FLAG_TRANSFER
))
596 use_staging_texture
= FALSE
;
598 trans
= CALLOC_STRUCT(r600_transfer
);
601 pipe_resource_reference(&trans
->transfer
.resource
, texture
);
602 trans
->transfer
.level
= level
;
603 trans
->transfer
.usage
= usage
;
604 trans
->transfer
.box
= *box
;
606 /* XXX: only readback the rectangle which is being mapped?
608 /* XXX: when discard is true, no need to read back from depth texture
610 r
= r600_texture_depth_flush(ctx
, texture
, FALSE
);
612 R600_ERR("failed to create temporary texture to hold untiled copy\n");
613 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
617 trans
->transfer
.stride
= rtex
->flushed_depth_texture
->pitch_in_bytes
[level
];
618 trans
->offset
= r600_texture_get_offset(rtex
->flushed_depth_texture
, level
, box
->z
);
619 return &trans
->transfer
;
620 } else if (use_staging_texture
) {
621 resource
.target
= PIPE_TEXTURE_2D
;
622 resource
.format
= texture
->format
;
623 resource
.width0
= box
->width
;
624 resource
.height0
= box
->height
;
626 resource
.array_size
= 1;
627 resource
.last_level
= 0;
628 resource
.nr_samples
= 0;
629 resource
.usage
= PIPE_USAGE_STAGING
;
631 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
;
632 /* For texture reading, the temporary (detiled) texture is used as
633 * a render target when blitting from a tiled texture. */
634 if (usage
& PIPE_TRANSFER_READ
) {
635 resource
.bind
|= PIPE_BIND_RENDER_TARGET
;
637 /* For texture writing, the temporary texture is used as a sampler
638 * when blitting into a tiled texture. */
639 if (usage
& PIPE_TRANSFER_WRITE
) {
640 resource
.bind
|= PIPE_BIND_SAMPLER_VIEW
;
642 /* Create the temporary texture. */
643 trans
->staging_texture
= ctx
->screen
->resource_create(ctx
->screen
, &resource
);
644 if (trans
->staging_texture
== NULL
) {
645 R600_ERR("failed to create temporary texture to hold untiled copy\n");
646 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
651 trans
->transfer
.stride
=
652 ((struct r600_resource_texture
*)trans
->staging_texture
)->pitch_in_bytes
[0];
653 if (usage
& PIPE_TRANSFER_READ
) {
654 r600_copy_to_staging_texture(ctx
, trans
);
655 /* Always referenced in the blit. */
656 ctx
->flush(ctx
, 0, NULL
);
658 return &trans
->transfer
;
660 trans
->transfer
.stride
= rtex
->pitch_in_bytes
[level
];
661 trans
->transfer
.layer_stride
= rtex
->layer_size
[level
];
662 trans
->offset
= r600_texture_get_offset(rtex
, level
, box
->z
);
663 return &trans
->transfer
;
666 void r600_texture_transfer_destroy(struct pipe_context
*ctx
,
667 struct pipe_transfer
*transfer
)
669 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
670 struct pipe_resource
*texture
= transfer
->resource
;
671 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
673 if (rtransfer
->staging_texture
) {
674 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
675 r600_copy_from_staging_texture(ctx
, rtransfer
);
677 pipe_resource_reference(&rtransfer
->staging_texture
, NULL
);
680 if (rtex
->depth
&& !rtex
->is_flushing_texture
) {
681 if ((transfer
->usage
& PIPE_TRANSFER_WRITE
) && rtex
->flushed_depth_texture
)
682 r600_blit_push_depth(ctx
, rtex
);
685 pipe_resource_reference(&transfer
->resource
, NULL
);
689 void* r600_texture_transfer_map(struct pipe_context
*ctx
,
690 struct pipe_transfer
* transfer
)
692 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
694 enum pipe_format format
= transfer
->resource
->format
;
695 struct radeon
*radeon
= (struct radeon
*)ctx
->screen
->winsys
;
700 if (rtransfer
->staging_texture
) {
701 bo
= ((struct r600_resource
*)rtransfer
->staging_texture
)->bo
;
703 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
705 if (rtex
->flushed_depth_texture
)
706 bo
= ((struct r600_resource
*)rtex
->flushed_depth_texture
)->bo
;
708 bo
= ((struct r600_resource
*)transfer
->resource
)->bo
;
710 offset
= rtransfer
->offset
+
711 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
712 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
715 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
716 usage
|= PB_USAGE_CPU_WRITE
;
718 if (transfer
->usage
& PIPE_TRANSFER_DISCARD
) {
721 if (transfer
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
) {
725 if (transfer
->usage
& PIPE_TRANSFER_READ
) {
726 usage
|= PB_USAGE_CPU_READ
;
729 if (transfer
->usage
& PIPE_TRANSFER_DONTBLOCK
) {
730 usage
|= PB_USAGE_DONTBLOCK
;
733 if (transfer
->usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) {
734 usage
|= PB_USAGE_UNSYNCHRONIZED
;
737 map
= r600_bo_map(radeon
, bo
, usage
, ctx
);
745 void r600_texture_transfer_unmap(struct pipe_context
*ctx
,
746 struct pipe_transfer
* transfer
)
748 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
749 struct radeon
*radeon
= (struct radeon
*)ctx
->screen
->winsys
;
752 if (rtransfer
->staging_texture
) {
753 bo
= ((struct r600_resource
*)rtransfer
->staging_texture
)->bo
;
755 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
757 if (rtex
->flushed_depth_texture
) {
758 bo
= ((struct r600_resource
*)rtex
->flushed_depth_texture
)->bo
;
760 bo
= ((struct r600_resource
*)transfer
->resource
)->bo
;
763 r600_bo_unmap(radeon
, bo
);
766 void r600_init_surface_functions(struct r600_pipe_context
*r600
)
768 r600
->context
.create_surface
= r600_create_surface
;
769 r600
->context
.surface_destroy
= r600_surface_destroy
;
772 static unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format
,
773 const unsigned char *swizzle_view
)
776 unsigned char swizzle
[4];
778 const uint32_t swizzle_shift
[4] = {
781 const uint32_t swizzle_bit
[4] = {
786 /* Combine two sets of swizzles. */
787 for (i
= 0; i
< 4; i
++) {
788 swizzle
[i
] = swizzle_view
[i
] <= UTIL_FORMAT_SWIZZLE_W
?
789 swizzle_format
[swizzle_view
[i
]] : swizzle_view
[i
];
792 memcpy(swizzle
, swizzle_format
, 4);
796 for (i
= 0; i
< 4; i
++) {
797 switch (swizzle
[i
]) {
798 case UTIL_FORMAT_SWIZZLE_Y
:
799 result
|= swizzle_bit
[1] << swizzle_shift
[i
];
801 case UTIL_FORMAT_SWIZZLE_Z
:
802 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
804 case UTIL_FORMAT_SWIZZLE_W
:
805 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
807 case UTIL_FORMAT_SWIZZLE_0
:
808 result
|= V_038010_SQ_SEL_0
<< swizzle_shift
[i
];
810 case UTIL_FORMAT_SWIZZLE_1
:
811 result
|= V_038010_SQ_SEL_1
<< swizzle_shift
[i
];
813 default: /* UTIL_FORMAT_SWIZZLE_X */
814 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
820 /* texture format translate */
821 uint32_t r600_translate_texformat(struct pipe_screen
*screen
,
822 enum pipe_format format
,
823 const unsigned char *swizzle_view
,
824 uint32_t *word4_p
, uint32_t *yuv_format_p
)
826 uint32_t result
= 0, word4
= 0, yuv_format
= 0;
827 const struct util_format_description
*desc
;
828 boolean uniform
= TRUE
;
829 static int r600_enable_s3tc
= -1;
832 const uint32_t sign_bit
[4] = {
833 S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED
),
834 S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED
),
835 S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED
),
836 S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED
)
838 desc
= util_format_description(format
);
840 word4
|= r600_get_swizzle_combined(desc
->swizzle
, swizzle_view
);
842 /* Colorspace (return non-RGB formats directly). */
843 switch (desc
->colorspace
) {
844 /* Depth stencil formats */
845 case UTIL_FORMAT_COLORSPACE_ZS
:
847 case PIPE_FORMAT_Z16_UNORM
:
850 case PIPE_FORMAT_X24S8_USCALED
:
851 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
852 case PIPE_FORMAT_Z24X8_UNORM
:
853 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
856 case PIPE_FORMAT_S8X24_USCALED
:
857 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
858 case PIPE_FORMAT_X8Z24_UNORM
:
859 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
862 case PIPE_FORMAT_S8_USCALED
:
864 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
870 case UTIL_FORMAT_COLORSPACE_YUV
:
871 yuv_format
|= (1 << 30);
873 case PIPE_FORMAT_UYVY
:
874 case PIPE_FORMAT_YUYV
:
878 goto out_unknown
; /* TODO */
880 case UTIL_FORMAT_COLORSPACE_SRGB
:
881 word4
|= S_038010_FORCE_DEGAMMA(1);
888 if (r600_enable_s3tc
== -1) {
889 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
890 if (r600_get_minor_version(rscreen
->radeon
) >= 9)
891 r600_enable_s3tc
= 1;
893 r600_enable_s3tc
= debug_get_bool_option("R600_ENABLE_S3TC", FALSE
);
896 if (desc
->layout
== UTIL_FORMAT_LAYOUT_RGTC
) {
897 if (!r600_enable_s3tc
)
901 case PIPE_FORMAT_RGTC1_SNORM
:
902 word4
|= sign_bit
[0];
903 case PIPE_FORMAT_RGTC1_UNORM
:
906 case PIPE_FORMAT_RGTC2_SNORM
:
907 word4
|= sign_bit
[0] | sign_bit
[1];
908 case PIPE_FORMAT_RGTC2_UNORM
:
916 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
918 if (!r600_enable_s3tc
)
921 if (!util_format_s3tc_enabled
) {
926 case PIPE_FORMAT_DXT1_RGB
:
927 case PIPE_FORMAT_DXT1_RGBA
:
928 case PIPE_FORMAT_DXT1_SRGB
:
929 case PIPE_FORMAT_DXT1_SRGBA
:
932 case PIPE_FORMAT_DXT3_RGBA
:
933 case PIPE_FORMAT_DXT3_SRGBA
:
936 case PIPE_FORMAT_DXT5_RGBA
:
937 case PIPE_FORMAT_DXT5_SRGBA
:
946 for (i
= 0; i
< desc
->nr_channels
; i
++) {
947 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
948 word4
|= sign_bit
[i
];
952 /* R8G8Bx_SNORM - TODO CxV8U8 */
954 /* See whether the components are of the same size. */
955 for (i
= 1; i
< desc
->nr_channels
; i
++) {
956 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
959 /* Non-uniform formats. */
961 switch(desc
->nr_channels
) {
963 if (desc
->channel
[0].size
== 5 &&
964 desc
->channel
[1].size
== 6 &&
965 desc
->channel
[2].size
== 5) {
971 if (desc
->channel
[0].size
== 5 &&
972 desc
->channel
[1].size
== 5 &&
973 desc
->channel
[2].size
== 5 &&
974 desc
->channel
[3].size
== 1) {
975 result
= FMT_1_5_5_5
;
978 if (desc
->channel
[0].size
== 10 &&
979 desc
->channel
[1].size
== 10 &&
980 desc
->channel
[2].size
== 10 &&
981 desc
->channel
[3].size
== 2) {
982 result
= FMT_2_10_10_10
;
990 /* Find the first non-VOID channel. */
991 for (i
= 0; i
< 4; i
++) {
992 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
1000 /* uniform formats */
1001 switch (desc
->channel
[i
].type
) {
1002 case UTIL_FORMAT_TYPE_UNSIGNED
:
1003 case UTIL_FORMAT_TYPE_SIGNED
:
1004 if (!desc
->channel
[i
].normalized
&&
1005 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
1009 switch (desc
->channel
[i
].size
) {
1011 switch (desc
->nr_channels
) {
1016 result
= FMT_4_4_4_4
;
1021 switch (desc
->nr_channels
) {
1029 result
= FMT_8_8_8_8
;
1034 switch (desc
->nr_channels
) {
1042 result
= FMT_16_16_16_16
;
1047 switch (desc
->nr_channels
) {
1055 result
= FMT_32_32_32_32
;
1061 case UTIL_FORMAT_TYPE_FLOAT
:
1062 switch (desc
->channel
[i
].size
) {
1064 switch (desc
->nr_channels
) {
1066 result
= FMT_16_FLOAT
;
1069 result
= FMT_16_16_FLOAT
;
1072 result
= FMT_16_16_16_16_FLOAT
;
1077 switch (desc
->nr_channels
) {
1079 result
= FMT_32_FLOAT
;
1082 result
= FMT_32_32_FLOAT
;
1085 result
= FMT_32_32_32_32_FLOAT
;
1095 *yuv_format_p
= yuv_format
;
1098 // R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format));