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 "pipebuffer/pb_buffer.h"
35 #include "r600_pipe.h"
36 #include "r600_resource.h"
38 #include "r600_formats.h"
40 /* Copy from a full GPU texture to a transfer's staging one. */
41 static void r600_copy_to_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
43 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
44 struct pipe_resource
*texture
= transfer
->resource
;
46 ctx
->resource_copy_region(ctx
, rtransfer
->staging_texture
,
47 0, 0, 0, 0, texture
, transfer
->level
,
52 /* Copy from a transfer's staging texture to a full GPU one. */
53 static void r600_copy_from_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
55 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
56 struct pipe_resource
*texture
= transfer
->resource
;
59 sbox
.x
= sbox
.y
= sbox
.z
= 0;
60 sbox
.width
= transfer
->box
.width
;
61 sbox
.height
= transfer
->box
.height
;
62 /* XXX that might be wrong */
64 ctx
->resource_copy_region(ctx
, texture
, transfer
->level
,
65 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
66 rtransfer
->staging_texture
,
69 ctx
->flush(ctx
, NULL
);
72 unsigned r600_texture_get_offset(struct r600_resource_texture
*rtex
,
73 unsigned level
, unsigned layer
)
75 unsigned offset
= rtex
->offset
[level
];
77 switch (rtex
->resource
.b
.b
.b
.target
) {
79 case PIPE_TEXTURE_CUBE
:
81 return offset
+ layer
* rtex
->layer_size
[level
];
85 static unsigned r600_get_block_alignment(struct pipe_screen
*screen
,
86 enum pipe_format format
,
89 struct r600_screen
* rscreen
= (struct r600_screen
*)screen
;
90 unsigned pixsize
= util_format_get_blocksize(format
);
94 case V_038000_ARRAY_1D_TILED_THIN1
:
96 ((rscreen
->tiling_info
->group_bytes
/ 8 / pixsize
)));
98 case V_038000_ARRAY_2D_TILED_THIN1
:
99 p_align
= MAX2(rscreen
->tiling_info
->num_banks
,
100 (((rscreen
->tiling_info
->group_bytes
/ 8 / pixsize
)) *
101 rscreen
->tiling_info
->num_banks
)) * 8;
103 case V_038000_ARRAY_LINEAR_ALIGNED
:
104 p_align
= MAX2(64, rscreen
->tiling_info
->group_bytes
/ pixsize
);
106 case V_038000_ARRAY_LINEAR_GENERAL
:
108 p_align
= rscreen
->tiling_info
->group_bytes
/ pixsize
;
114 static unsigned r600_get_height_alignment(struct pipe_screen
*screen
,
117 struct r600_screen
* rscreen
= (struct r600_screen
*)screen
;
120 switch (array_mode
) {
121 case V_038000_ARRAY_2D_TILED_THIN1
:
122 h_align
= rscreen
->tiling_info
->num_channels
* 8;
124 case V_038000_ARRAY_1D_TILED_THIN1
:
125 case V_038000_ARRAY_LINEAR_ALIGNED
:
128 case V_038000_ARRAY_LINEAR_GENERAL
:
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_block_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
:
152 case V_038000_ARRAY_LINEAR_ALIGNED
:
153 case V_038000_ARRAY_LINEAR_GENERAL
:
155 b_align
= rscreen
->tiling_info
->group_bytes
;
161 static unsigned mip_minify(unsigned size
, unsigned level
)
164 val
= u_minify(size
, level
);
166 val
= util_next_power_of_two(val
);
170 static unsigned r600_texture_get_nblocksx(struct pipe_screen
*screen
,
171 struct r600_resource_texture
*rtex
,
174 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
175 unsigned nblocksx
, block_align
, width
;
176 unsigned blocksize
= util_format_get_blocksize(ptex
->format
);
178 if (rtex
->pitch_override
)
179 return rtex
->pitch_override
/ blocksize
;
181 width
= mip_minify(ptex
->width0
, level
);
182 nblocksx
= util_format_get_nblocksx(ptex
->format
, width
);
184 block_align
= r600_get_block_alignment(screen
, ptex
->format
,
185 rtex
->array_mode
[level
]);
186 nblocksx
= align(nblocksx
, block_align
);
190 static unsigned r600_texture_get_nblocksy(struct pipe_screen
*screen
,
191 struct r600_resource_texture
*rtex
,
194 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
195 unsigned height
, tile_height
;
197 height
= mip_minify(ptex
->height0
, level
);
198 height
= util_format_get_nblocksy(ptex
->format
, height
);
199 tile_height
= r600_get_height_alignment(screen
,
200 rtex
->array_mode
[level
]);
201 height
= align(height
, tile_height
);
205 static void r600_texture_set_array_mode(struct pipe_screen
*screen
,
206 struct r600_resource_texture
*rtex
,
207 unsigned level
, unsigned array_mode
)
209 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
211 switch (array_mode
) {
212 case V_0280A0_ARRAY_LINEAR_GENERAL
:
213 case V_0280A0_ARRAY_LINEAR_ALIGNED
:
214 case V_0280A0_ARRAY_1D_TILED_THIN1
:
216 rtex
->array_mode
[level
] = array_mode
;
218 case V_0280A0_ARRAY_2D_TILED_THIN1
:
220 unsigned w
, h
, tile_height
, tile_width
;
222 tile_height
= r600_get_height_alignment(screen
, array_mode
);
223 tile_width
= r600_get_block_alignment(screen
, ptex
->format
, array_mode
);
225 w
= mip_minify(ptex
->width0
, level
);
226 h
= mip_minify(ptex
->height0
, level
);
227 if (w
<= tile_width
|| h
<= tile_height
)
228 rtex
->array_mode
[level
] = V_0280A0_ARRAY_1D_TILED_THIN1
;
230 rtex
->array_mode
[level
] = array_mode
;
236 static void r600_setup_miptree(struct pipe_screen
*screen
,
237 struct r600_resource_texture
*rtex
,
240 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
241 struct radeon
*radeon
= ((struct r600_screen
*)screen
)->radeon
;
242 enum chip_class chipc
= r600_get_family_class(radeon
);
243 unsigned size
, layer_size
, i
, offset
;
244 unsigned nblocksx
, nblocksy
, extra_size
= 0;
246 for (i
= 0, offset
= 0; i
<= ptex
->last_level
; i
++) {
247 unsigned blocksize
= util_format_get_blocksize(ptex
->format
);
248 unsigned base_align
= r600_get_base_alignment(screen
, ptex
->format
, array_mode
);
250 r600_texture_set_array_mode(screen
, rtex
, i
, array_mode
);
252 nblocksx
= r600_texture_get_nblocksx(screen
, rtex
, i
);
253 nblocksy
= r600_texture_get_nblocksy(screen
, rtex
, i
);
255 layer_size
= nblocksx
* nblocksy
* blocksize
;
256 if (ptex
->target
== PIPE_TEXTURE_CUBE
) {
258 size
= layer_size
* 8;
260 size
= layer_size
* 6;
262 else if (ptex
->target
== PIPE_TEXTURE_3D
)
263 size
= layer_size
* u_minify(ptex
->depth0
, i
);
265 size
= layer_size
* ptex
->array_size
;
267 /* evergreen stores depth and stencil separately */
268 if ((chipc
>= EVERGREEN
) && util_format_is_depth_or_stencil(ptex
->format
))
269 extra_size
= align(extra_size
+ (nblocksx
* nblocksy
* 1), base_align
);
271 /* align base image and start of miptree */
272 if ((i
== 0) || (i
== 1))
273 offset
= align(offset
, base_align
);
274 rtex
->offset
[i
] = offset
;
275 rtex
->layer_size
[i
] = layer_size
;
276 rtex
->pitch_in_blocks
[i
] = nblocksx
; /* CB talks in elements */
277 rtex
->pitch_in_bytes
[i
] = nblocksx
* blocksize
;
281 rtex
->size
= offset
+ extra_size
;
284 /* Figure out whether u_blitter will fallback to a transfer operation.
285 * If so, don't use a staging resource.
287 static boolean
permit_hardware_blit(struct pipe_screen
*screen
,
288 const struct pipe_resource
*res
)
292 if (util_format_is_depth_or_stencil(res
->format
))
293 bind
= PIPE_BIND_DEPTH_STENCIL
;
295 bind
= PIPE_BIND_RENDER_TARGET
;
297 /* hackaround for S3TC */
298 if (util_format_is_compressed(res
->format
))
301 if (!screen
->is_format_supported(screen
,
308 if (!screen
->is_format_supported(screen
,
312 PIPE_BIND_SAMPLER_VIEW
))
315 switch (res
->usage
) {
316 case PIPE_USAGE_STREAM
:
317 case PIPE_USAGE_STAGING
:
325 static boolean
r600_texture_get_handle(struct pipe_screen
* screen
,
326 struct pipe_resource
*ptex
,
327 struct winsys_handle
*whandle
)
329 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)ptex
;
330 struct r600_resource
*resource
= &rtex
->resource
;
331 struct radeon
*radeon
= ((struct r600_screen
*)screen
)->radeon
;
333 return r600_bo_get_winsys_handle(radeon
, resource
->bo
,
334 rtex
->pitch_in_bytes
[0], whandle
);
337 static void r600_texture_destroy(struct pipe_screen
*screen
,
338 struct pipe_resource
*ptex
)
340 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)ptex
;
341 struct r600_resource
*resource
= &rtex
->resource
;
342 struct radeon
*radeon
= ((struct r600_screen
*)screen
)->radeon
;
344 if (rtex
->flushed_depth_texture
)
345 pipe_resource_reference((struct pipe_resource
**)&rtex
->flushed_depth_texture
, NULL
);
348 r600_bo_reference(radeon
, &resource
->bo
, NULL
);
353 static const struct u_resource_vtbl r600_texture_vtbl
=
355 r600_texture_get_handle
, /* get_handle */
356 r600_texture_destroy
, /* resource_destroy */
357 r600_texture_get_transfer
, /* get_transfer */
358 r600_texture_transfer_destroy
, /* transfer_destroy */
359 r600_texture_transfer_map
, /* transfer_map */
360 u_default_transfer_flush_region
,/* transfer_flush_region */
361 r600_texture_transfer_unmap
, /* transfer_unmap */
362 u_default_transfer_inline_write
/* transfer_inline_write */
365 static struct r600_resource_texture
*
366 r600_texture_create_object(struct pipe_screen
*screen
,
367 const struct pipe_resource
*base
,
369 unsigned pitch_in_bytes_override
,
370 unsigned max_buffer_size
,
373 struct r600_resource_texture
*rtex
;
374 struct r600_resource
*resource
;
375 struct radeon
*radeon
= ((struct r600_screen
*)screen
)->radeon
;
377 rtex
= CALLOC_STRUCT(r600_resource_texture
);
381 resource
= &rtex
->resource
;
382 resource
->b
.b
.b
= *base
;
383 resource
->b
.b
.vtbl
= &r600_texture_vtbl
;
384 pipe_reference_init(&resource
->b
.b
.b
.reference
, 1);
385 resource
->b
.b
.b
.screen
= screen
;
387 rtex
->pitch_override
= pitch_in_bytes_override
;
388 /* only mark depth textures the HW can hit as depth textures */
389 if (util_format_is_depth_or_stencil(base
->format
) && permit_hardware_blit(screen
, base
))
392 r600_setup_miptree(screen
, rtex
, array_mode
);
394 resource
->size
= rtex
->size
;
397 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
.b
;
398 int base_align
= r600_get_base_alignment(screen
, ptex
->format
, array_mode
);
400 resource
->bo
= r600_bo(radeon
, rtex
->size
, base_align
, base
->bind
, base
->usage
);
409 struct pipe_resource
*r600_texture_create(struct pipe_screen
*screen
,
410 const struct pipe_resource
*templ
)
412 unsigned array_mode
= 0;
413 static int force_tiling
= -1;
415 /* Would like some magic "get_bool_option_once" routine.
417 if (force_tiling
== -1) {
419 /* reenable when 2D tiling is fixed better */
420 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
421 if (r600_get_minor_version(rscreen
->radeon
) >= 9)
422 force_tiling
= debug_get_bool_option("R600_TILING", TRUE
);
424 force_tiling
= debug_get_bool_option("R600_TILING", FALSE
);
427 if (force_tiling
&& permit_hardware_blit(screen
, templ
)) {
428 if (!(templ
->flags
& R600_RESOURCE_FLAG_TRANSFER
) &&
429 !(templ
->bind
& PIPE_BIND_SCANOUT
)) {
430 array_mode
= V_038000_ARRAY_2D_TILED_THIN1
;
434 if (!(templ
->flags
& R600_RESOURCE_FLAG_TRANSFER
) &&
435 util_format_is_compressed(templ
->format
))
436 array_mode
= V_038000_ARRAY_1D_TILED_THIN1
;
438 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
, array_mode
,
443 static struct pipe_surface
*r600_create_surface(struct pipe_context
*pipe
,
444 struct pipe_resource
*texture
,
445 const struct pipe_surface
*surf_tmpl
)
447 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
448 struct r600_surface
*surface
= CALLOC_STRUCT(r600_surface
);
449 unsigned level
= surf_tmpl
->u
.tex
.level
;
451 assert(surf_tmpl
->u
.tex
.first_layer
== surf_tmpl
->u
.tex
.last_layer
);
455 /* offset = r600_texture_get_offset(rtex, level, surf_tmpl->u.tex.first_layer);*/
456 pipe_reference_init(&surface
->base
.reference
, 1);
457 pipe_resource_reference(&surface
->base
.texture
, texture
);
458 surface
->base
.context
= pipe
;
459 surface
->base
.format
= surf_tmpl
->format
;
460 surface
->base
.width
= mip_minify(texture
->width0
, level
);
461 surface
->base
.height
= mip_minify(texture
->height0
, level
);
462 surface
->base
.usage
= surf_tmpl
->usage
;
463 surface
->base
.texture
= texture
;
464 surface
->base
.u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
465 surface
->base
.u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
466 surface
->base
.u
.tex
.level
= level
;
468 surface
->aligned_height
= r600_texture_get_nblocksy(pipe
->screen
,
470 return &surface
->base
;
473 static void r600_surface_destroy(struct pipe_context
*pipe
,
474 struct pipe_surface
*surface
)
476 pipe_resource_reference(&surface
->texture
, NULL
);
481 struct pipe_resource
*r600_texture_from_handle(struct pipe_screen
*screen
,
482 const struct pipe_resource
*templ
,
483 struct winsys_handle
*whandle
)
485 struct radeon
*rw
= ((struct r600_screen
*)screen
)->radeon
;
486 struct r600_bo
*bo
= NULL
;
488 unsigned array_mode
= 0;
490 /* Support only 2D textures without mipmaps */
491 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
) ||
492 templ
->depth0
!= 1 || templ
->last_level
!= 0)
495 bo
= r600_bo_handle(rw
, whandle
, &stride
, &array_mode
);
500 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
, array_mode
,
504 int r600_texture_depth_flush(struct pipe_context
*ctx
,
505 struct pipe_resource
*texture
, boolean just_create
)
507 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
508 struct pipe_resource resource
;
510 if (rtex
->flushed_depth_texture
)
513 resource
.target
= PIPE_TEXTURE_2D
;
514 resource
.format
= texture
->format
;
515 resource
.width0
= texture
->width0
;
516 resource
.height0
= texture
->height0
;
518 resource
.array_size
= 1;
519 resource
.last_level
= texture
->last_level
;
520 resource
.nr_samples
= 0;
521 resource
.usage
= PIPE_USAGE_DYNAMIC
;
523 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
;
525 resource
.bind
|= PIPE_BIND_DEPTH_STENCIL
;
527 rtex
->flushed_depth_texture
= (struct r600_resource_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
528 if (rtex
->flushed_depth_texture
== NULL
) {
529 R600_ERR("failed to create temporary texture to hold untiled copy\n");
533 ((struct r600_resource_texture
*)rtex
->flushed_depth_texture
)->is_flushing_texture
= TRUE
;
538 /* XXX: only do this if the depth texture has actually changed:
540 r600_blit_uncompress_depth(ctx
, rtex
);
544 /* Needs adjustment for pixelformat:
546 static INLINE
unsigned u_box_volume( const struct pipe_box
*box
)
548 return box
->width
* box
->depth
* box
->height
;
551 struct pipe_transfer
* r600_texture_get_transfer(struct pipe_context
*ctx
,
552 struct pipe_resource
*texture
,
555 const struct pipe_box
*box
)
557 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
558 struct pipe_resource resource
;
559 struct r600_transfer
*trans
;
561 boolean use_staging_texture
= FALSE
;
563 /* We cannot map a tiled texture directly because the data is
564 * in a different order, therefore we do detiling using a blit.
566 * Also, use a temporary in GTT memory for read transfers, as
567 * the CPU is much happier reading out of cached system memory
568 * than uncached VRAM.
570 if (R600_TEX_IS_TILED(rtex
, level
))
571 use_staging_texture
= TRUE
;
573 if ((usage
& PIPE_TRANSFER_READ
) && u_box_volume(box
) > 1024)
574 use_staging_texture
= TRUE
;
576 /* XXX: Use a staging texture for uploads if the underlying BO
577 * is busy. No interface for checking that currently? so do
578 * it eagerly whenever the transfer doesn't require a readback
581 if ((usage
& PIPE_TRANSFER_WRITE
) &&
582 !(usage
& (PIPE_TRANSFER_READ
|
583 PIPE_TRANSFER_DONTBLOCK
|
584 PIPE_TRANSFER_UNSYNCHRONIZED
)))
585 use_staging_texture
= TRUE
;
587 if (!permit_hardware_blit(ctx
->screen
, texture
) ||
588 (texture
->flags
& R600_RESOURCE_FLAG_TRANSFER
))
589 use_staging_texture
= FALSE
;
591 trans
= CALLOC_STRUCT(r600_transfer
);
594 pipe_resource_reference(&trans
->transfer
.resource
, texture
);
595 trans
->transfer
.level
= level
;
596 trans
->transfer
.usage
= usage
;
597 trans
->transfer
.box
= *box
;
599 /* XXX: only readback the rectangle which is being mapped?
601 /* XXX: when discard is true, no need to read back from depth texture
603 r
= r600_texture_depth_flush(ctx
, texture
, FALSE
);
605 R600_ERR("failed to create temporary texture to hold untiled copy\n");
606 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
610 trans
->transfer
.stride
= rtex
->flushed_depth_texture
->pitch_in_bytes
[level
];
611 trans
->offset
= r600_texture_get_offset(rtex
->flushed_depth_texture
, level
, box
->z
);
612 return &trans
->transfer
;
613 } else if (use_staging_texture
) {
614 resource
.target
= PIPE_TEXTURE_2D
;
615 resource
.format
= texture
->format
;
616 resource
.width0
= box
->width
;
617 resource
.height0
= box
->height
;
619 resource
.array_size
= 1;
620 resource
.last_level
= 0;
621 resource
.nr_samples
= 0;
622 resource
.usage
= PIPE_USAGE_STAGING
;
624 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
;
625 /* For texture reading, the temporary (detiled) texture is used as
626 * a render target when blitting from a tiled texture. */
627 if (usage
& PIPE_TRANSFER_READ
) {
628 resource
.bind
|= PIPE_BIND_RENDER_TARGET
;
630 /* For texture writing, the temporary texture is used as a sampler
631 * when blitting into a tiled texture. */
632 if (usage
& PIPE_TRANSFER_WRITE
) {
633 resource
.bind
|= PIPE_BIND_SAMPLER_VIEW
;
635 /* Create the temporary texture. */
636 trans
->staging_texture
= ctx
->screen
->resource_create(ctx
->screen
, &resource
);
637 if (trans
->staging_texture
== NULL
) {
638 R600_ERR("failed to create temporary texture to hold untiled copy\n");
639 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
644 trans
->transfer
.stride
=
645 ((struct r600_resource_texture
*)trans
->staging_texture
)->pitch_in_bytes
[0];
646 if (usage
& PIPE_TRANSFER_READ
) {
647 r600_copy_to_staging_texture(ctx
, trans
);
648 /* Always referenced in the blit. */
649 ctx
->flush(ctx
, NULL
);
651 return &trans
->transfer
;
653 trans
->transfer
.stride
= rtex
->pitch_in_bytes
[level
];
654 trans
->transfer
.layer_stride
= rtex
->layer_size
[level
];
655 trans
->offset
= r600_texture_get_offset(rtex
, level
, box
->z
);
656 return &trans
->transfer
;
659 void r600_texture_transfer_destroy(struct pipe_context
*ctx
,
660 struct pipe_transfer
*transfer
)
662 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
663 struct pipe_resource
*texture
= transfer
->resource
;
664 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
666 if (rtransfer
->staging_texture
) {
667 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
668 r600_copy_from_staging_texture(ctx
, rtransfer
);
670 pipe_resource_reference(&rtransfer
->staging_texture
, NULL
);
673 if (rtex
->depth
&& !rtex
->is_flushing_texture
) {
674 if ((transfer
->usage
& PIPE_TRANSFER_WRITE
) && rtex
->flushed_depth_texture
)
675 r600_blit_push_depth(ctx
, rtex
);
678 pipe_resource_reference(&transfer
->resource
, NULL
);
682 void* r600_texture_transfer_map(struct pipe_context
*ctx
,
683 struct pipe_transfer
* transfer
)
685 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
687 enum pipe_format format
= transfer
->resource
->format
;
688 struct radeon
*radeon
= ((struct r600_screen
*)ctx
->screen
)->radeon
;
692 if (rtransfer
->staging_texture
) {
693 bo
= ((struct r600_resource
*)rtransfer
->staging_texture
)->bo
;
695 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
697 if (rtex
->flushed_depth_texture
)
698 bo
= ((struct r600_resource
*)rtex
->flushed_depth_texture
)->bo
;
700 bo
= ((struct r600_resource
*)transfer
->resource
)->bo
;
702 offset
= rtransfer
->offset
+
703 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
704 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
707 if (!(map
= r600_bo_map(radeon
, bo
, transfer
->usage
, ctx
))) {
714 void r600_texture_transfer_unmap(struct pipe_context
*ctx
,
715 struct pipe_transfer
* transfer
)
717 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
718 struct radeon
*radeon
= ((struct r600_screen
*)ctx
->screen
)->radeon
;
721 if (rtransfer
->staging_texture
) {
722 bo
= ((struct r600_resource
*)rtransfer
->staging_texture
)->bo
;
724 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
726 if (rtex
->flushed_depth_texture
) {
727 bo
= ((struct r600_resource
*)rtex
->flushed_depth_texture
)->bo
;
729 bo
= ((struct r600_resource
*)transfer
->resource
)->bo
;
732 r600_bo_unmap(radeon
, bo
);
735 void r600_init_surface_functions(struct r600_pipe_context
*r600
)
737 r600
->context
.create_surface
= r600_create_surface
;
738 r600
->context
.surface_destroy
= r600_surface_destroy
;
741 static unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format
,
742 const unsigned char *swizzle_view
)
745 unsigned char swizzle
[4];
747 const uint32_t swizzle_shift
[4] = {
750 const uint32_t swizzle_bit
[4] = {
755 util_format_compose_swizzles(swizzle_format
, swizzle_view
, swizzle
);
757 memcpy(swizzle
, swizzle_format
, 4);
761 for (i
= 0; i
< 4; i
++) {
762 switch (swizzle
[i
]) {
763 case UTIL_FORMAT_SWIZZLE_Y
:
764 result
|= swizzle_bit
[1] << swizzle_shift
[i
];
766 case UTIL_FORMAT_SWIZZLE_Z
:
767 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
769 case UTIL_FORMAT_SWIZZLE_W
:
770 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
772 case UTIL_FORMAT_SWIZZLE_0
:
773 result
|= V_038010_SQ_SEL_0
<< swizzle_shift
[i
];
775 case UTIL_FORMAT_SWIZZLE_1
:
776 result
|= V_038010_SQ_SEL_1
<< swizzle_shift
[i
];
778 default: /* UTIL_FORMAT_SWIZZLE_X */
779 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
785 /* texture format translate */
786 uint32_t r600_translate_texformat(struct pipe_screen
*screen
,
787 enum pipe_format format
,
788 const unsigned char *swizzle_view
,
789 uint32_t *word4_p
, uint32_t *yuv_format_p
)
791 uint32_t result
= 0, word4
= 0, yuv_format
= 0;
792 const struct util_format_description
*desc
;
793 boolean uniform
= TRUE
;
794 static int r600_enable_s3tc
= -1;
797 const uint32_t sign_bit
[4] = {
798 S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED
),
799 S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED
),
800 S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED
),
801 S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED
)
803 desc
= util_format_description(format
);
805 word4
|= r600_get_swizzle_combined(desc
->swizzle
, swizzle_view
);
807 /* Colorspace (return non-RGB formats directly). */
808 switch (desc
->colorspace
) {
809 /* Depth stencil formats */
810 case UTIL_FORMAT_COLORSPACE_ZS
:
812 case PIPE_FORMAT_Z16_UNORM
:
815 case PIPE_FORMAT_X24S8_USCALED
:
816 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
817 case PIPE_FORMAT_Z24X8_UNORM
:
818 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
821 case PIPE_FORMAT_S8X24_USCALED
:
822 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
823 case PIPE_FORMAT_X8Z24_UNORM
:
824 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
827 case PIPE_FORMAT_S8_USCALED
:
829 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
831 case PIPE_FORMAT_Z32_FLOAT
:
832 result
= FMT_32_FLOAT
;
834 case PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED
:
835 result
= FMT_X24_8_32_FLOAT
;
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);
859 if (r600_enable_s3tc
== -1) {
860 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
861 if (r600_get_minor_version(rscreen
->radeon
) >= 9)
862 r600_enable_s3tc
= 1;
864 r600_enable_s3tc
= debug_get_bool_option("R600_ENABLE_S3TC", FALSE
);
867 if (desc
->layout
== UTIL_FORMAT_LAYOUT_RGTC
) {
868 if (!r600_enable_s3tc
)
872 case PIPE_FORMAT_RGTC1_SNORM
:
873 case PIPE_FORMAT_LATC1_SNORM
:
874 word4
|= sign_bit
[0];
875 case PIPE_FORMAT_RGTC1_UNORM
:
876 case PIPE_FORMAT_LATC1_UNORM
:
879 case PIPE_FORMAT_RGTC2_SNORM
:
880 case PIPE_FORMAT_LATC2_SNORM
:
881 word4
|= sign_bit
[0] | sign_bit
[1];
882 case PIPE_FORMAT_RGTC2_UNORM
:
883 case PIPE_FORMAT_LATC2_UNORM
:
891 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
893 if (!r600_enable_s3tc
)
896 if (!util_format_s3tc_enabled
) {
901 case PIPE_FORMAT_DXT1_RGB
:
902 case PIPE_FORMAT_DXT1_RGBA
:
903 case PIPE_FORMAT_DXT1_SRGB
:
904 case PIPE_FORMAT_DXT1_SRGBA
:
907 case PIPE_FORMAT_DXT3_RGBA
:
908 case PIPE_FORMAT_DXT3_SRGBA
:
911 case PIPE_FORMAT_DXT5_RGBA
:
912 case PIPE_FORMAT_DXT5_SRGBA
:
920 if (format
== PIPE_FORMAT_R9G9B9E5_FLOAT
) {
921 result
= FMT_5_9_9_9_SHAREDEXP
;
923 } else if (format
== PIPE_FORMAT_R11G11B10_FLOAT
) {
924 result
= FMT_10_11_11_FLOAT
;
929 for (i
= 0; i
< desc
->nr_channels
; i
++) {
930 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
931 word4
|= sign_bit
[i
];
935 /* R8G8Bx_SNORM - TODO CxV8U8 */
937 /* See whether the components are of the same size. */
938 for (i
= 1; i
< desc
->nr_channels
; i
++) {
939 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
942 /* Non-uniform formats. */
944 switch(desc
->nr_channels
) {
946 if (desc
->channel
[0].size
== 5 &&
947 desc
->channel
[1].size
== 6 &&
948 desc
->channel
[2].size
== 5) {
954 if (desc
->channel
[0].size
== 5 &&
955 desc
->channel
[1].size
== 5 &&
956 desc
->channel
[2].size
== 5 &&
957 desc
->channel
[3].size
== 1) {
958 result
= FMT_1_5_5_5
;
961 if (desc
->channel
[0].size
== 10 &&
962 desc
->channel
[1].size
== 10 &&
963 desc
->channel
[2].size
== 10 &&
964 desc
->channel
[3].size
== 2) {
965 result
= FMT_2_10_10_10
;
973 /* Find the first non-VOID channel. */
974 for (i
= 0; i
< 4; i
++) {
975 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
983 /* uniform formats */
984 switch (desc
->channel
[i
].type
) {
985 case UTIL_FORMAT_TYPE_UNSIGNED
:
986 case UTIL_FORMAT_TYPE_SIGNED
:
987 if (!desc
->channel
[i
].normalized
&&
988 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
992 switch (desc
->channel
[i
].size
) {
994 switch (desc
->nr_channels
) {
999 result
= FMT_4_4_4_4
;
1004 switch (desc
->nr_channels
) {
1012 result
= FMT_8_8_8_8
;
1017 switch (desc
->nr_channels
) {
1025 result
= FMT_16_16_16_16
;
1030 switch (desc
->nr_channels
) {
1038 result
= FMT_32_32_32_32
;
1044 case UTIL_FORMAT_TYPE_FLOAT
:
1045 switch (desc
->channel
[i
].size
) {
1047 switch (desc
->nr_channels
) {
1049 result
= FMT_16_FLOAT
;
1052 result
= FMT_16_16_FLOAT
;
1055 result
= FMT_16_16_16_16_FLOAT
;
1060 switch (desc
->nr_channels
) {
1062 result
= FMT_32_FLOAT
;
1065 result
= FMT_32_32_FLOAT
;
1068 result
= FMT_32_32_32_32_FLOAT
;
1079 *yuv_format_p
= yuv_format
;
1082 /* R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format)); */