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.
27 #include "r600_formats.h"
31 #include "util/u_format_s3tc.h"
32 #include "util/u_memory.h"
34 /* Copy from a full GPU texture to a transfer's staging one. */
35 static void r600_copy_to_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
37 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
38 struct pipe_resource
*texture
= transfer
->resource
;
40 ctx
->resource_copy_region(ctx
, &rtransfer
->staging
->b
.b
,
41 0, 0, 0, 0, texture
, transfer
->level
,
46 /* Copy from a transfer's staging texture to a full GPU one. */
47 static void r600_copy_from_staging_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
49 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
50 struct pipe_resource
*texture
= transfer
->resource
;
53 u_box_origin_2d(transfer
->box
.width
, transfer
->box
.height
, &sbox
);
55 ctx
->resource_copy_region(ctx
, texture
, transfer
->level
,
56 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
57 &rtransfer
->staging
->b
.b
,
61 unsigned r600_texture_get_offset(struct r600_texture
*rtex
,
62 unsigned level
, unsigned layer
)
64 return rtex
->surface
.level
[level
].offset
+
65 layer
* rtex
->surface
.level
[level
].slice_size
;
68 static int r600_init_surface(struct r600_screen
*rscreen
,
69 struct radeon_surface
*surface
,
70 const struct pipe_resource
*ptex
,
72 bool is_transfer
, bool is_flushed_depth
)
74 const struct util_format_description
*desc
=
75 util_format_description(ptex
->format
);
76 bool is_depth
, is_stencil
;
78 is_depth
= util_format_has_depth(desc
);
79 is_stencil
= util_format_has_stencil(desc
);
81 surface
->npix_x
= ptex
->width0
;
82 surface
->npix_y
= ptex
->height0
;
83 surface
->npix_z
= ptex
->depth0
;
84 surface
->blk_w
= util_format_get_blockwidth(ptex
->format
);
85 surface
->blk_h
= util_format_get_blockheight(ptex
->format
);
87 surface
->array_size
= 1;
88 surface
->last_level
= ptex
->last_level
;
90 if (rscreen
->chip_class
>= EVERGREEN
&&
91 !is_transfer
&& !is_flushed_depth
&&
92 ptex
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
) {
93 surface
->bpe
= 4; /* stencil is allocated separately on evergreen */
95 surface
->bpe
= util_format_get_blocksize(ptex
->format
);
96 /* align byte per element on dword */
97 if (surface
->bpe
== 3) {
102 surface
->nsamples
= ptex
->nr_samples
? ptex
->nr_samples
: 1;
105 switch (array_mode
) {
106 case V_038000_ARRAY_1D_TILED_THIN1
:
107 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_1D
, MODE
);
109 case V_038000_ARRAY_2D_TILED_THIN1
:
110 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_2D
, MODE
);
112 case V_038000_ARRAY_LINEAR_ALIGNED
:
113 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_LINEAR_ALIGNED
, MODE
);
115 case V_038000_ARRAY_LINEAR_GENERAL
:
117 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_LINEAR
, MODE
);
120 switch (ptex
->target
) {
121 case PIPE_TEXTURE_1D
:
122 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_1D
, TYPE
);
124 case PIPE_TEXTURE_RECT
:
125 case PIPE_TEXTURE_2D
:
126 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_2D
, TYPE
);
128 case PIPE_TEXTURE_3D
:
129 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_3D
, TYPE
);
131 case PIPE_TEXTURE_1D_ARRAY
:
132 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_1D_ARRAY
, TYPE
);
133 surface
->array_size
= ptex
->array_size
;
135 case PIPE_TEXTURE_2D_ARRAY
:
136 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_2D_ARRAY
, TYPE
);
137 surface
->array_size
= ptex
->array_size
;
139 case PIPE_TEXTURE_CUBE
:
140 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_CUBEMAP
, TYPE
);
146 if (ptex
->bind
& PIPE_BIND_SCANOUT
) {
147 surface
->flags
|= RADEON_SURF_SCANOUT
;
150 if (!is_transfer
&& !is_flushed_depth
&& is_depth
) {
151 surface
->flags
|= RADEON_SURF_ZBUFFER
;
154 surface
->flags
|= RADEON_SURF_SBUFFER
;
160 static int r600_setup_surface(struct pipe_screen
*screen
,
161 struct r600_texture
*rtex
,
162 unsigned pitch_in_bytes_override
)
164 struct pipe_resource
*ptex
= &rtex
->resource
.b
.b
;
165 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
169 r
= rscreen
->ws
->surface_init(rscreen
->ws
, &rtex
->surface
);
173 rtex
->size
= rtex
->surface
.bo_size
;
174 if (pitch_in_bytes_override
&& pitch_in_bytes_override
!= rtex
->surface
.level
[0].pitch_bytes
) {
175 /* old ddx on evergreen over estimate alignment for 1d, only 1 level
178 rtex
->surface
.level
[0].nblk_x
= pitch_in_bytes_override
/ rtex
->surface
.bpe
;
179 rtex
->surface
.level
[0].pitch_bytes
= pitch_in_bytes_override
;
180 rtex
->surface
.level
[0].slice_size
= pitch_in_bytes_override
* rtex
->surface
.level
[0].nblk_y
;
181 if (rtex
->surface
.flags
& RADEON_SURF_SBUFFER
) {
182 rtex
->surface
.stencil_offset
= rtex
->surface
.level
[0].slice_size
;
185 for (i
= 0; i
<= ptex
->last_level
; i
++) {
186 switch (rtex
->surface
.level
[i
].mode
) {
187 case RADEON_SURF_MODE_LINEAR_ALIGNED
:
188 rtex
->array_mode
[i
] = V_038000_ARRAY_LINEAR_ALIGNED
;
190 case RADEON_SURF_MODE_1D
:
191 rtex
->array_mode
[i
] = V_038000_ARRAY_1D_TILED_THIN1
;
193 case RADEON_SURF_MODE_2D
:
194 rtex
->array_mode
[i
] = V_038000_ARRAY_2D_TILED_THIN1
;
197 case RADEON_SURF_MODE_LINEAR
:
198 rtex
->array_mode
[i
] = 0;
205 static boolean
r600_texture_get_handle(struct pipe_screen
* screen
,
206 struct pipe_resource
*ptex
,
207 struct winsys_handle
*whandle
)
209 struct r600_texture
*rtex
= (struct r600_texture
*)ptex
;
210 struct r600_resource
*resource
= &rtex
->resource
;
211 struct radeon_surface
*surface
= &rtex
->surface
;
212 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
214 rscreen
->ws
->buffer_set_tiling(resource
->buf
,
216 surface
->level
[0].mode
>= RADEON_SURF_MODE_1D
?
217 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
,
218 surface
->level
[0].mode
>= RADEON_SURF_MODE_2D
?
219 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
,
220 surface
->bankw
, surface
->bankh
,
222 surface
->stencil_tile_split
,
224 rtex
->surface
.level
[0].pitch_bytes
);
226 return rscreen
->ws
->buffer_get_handle(resource
->buf
,
227 rtex
->surface
.level
[0].pitch_bytes
, whandle
);
230 static void r600_texture_destroy(struct pipe_screen
*screen
,
231 struct pipe_resource
*ptex
)
233 struct r600_texture
*rtex
= (struct r600_texture
*)ptex
;
234 struct r600_resource
*resource
= &rtex
->resource
;
236 if (rtex
->flushed_depth_texture
)
237 pipe_resource_reference((struct pipe_resource
**)&rtex
->flushed_depth_texture
, NULL
);
239 pb_reference(&resource
->buf
, NULL
);
243 static const struct u_resource_vtbl r600_texture_vtbl
=
245 r600_texture_get_handle
, /* get_handle */
246 r600_texture_destroy
, /* resource_destroy */
247 r600_texture_get_transfer
, /* get_transfer */
248 r600_texture_transfer_destroy
, /* transfer_destroy */
249 r600_texture_transfer_map
, /* transfer_map */
250 NULL
, /* transfer_flush_region */
251 r600_texture_transfer_unmap
, /* transfer_unmap */
252 NULL
/* transfer_inline_write */
255 static struct r600_texture
*
256 r600_texture_create_object(struct pipe_screen
*screen
,
257 const struct pipe_resource
*base
,
258 unsigned pitch_in_bytes_override
,
259 struct pb_buffer
*buf
,
261 struct radeon_surface
*surface
)
263 struct r600_texture
*rtex
;
264 struct r600_resource
*resource
;
265 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
268 rtex
= CALLOC_STRUCT(r600_texture
);
272 resource
= &rtex
->resource
;
273 resource
->b
.b
= *base
;
274 resource
->b
.vtbl
= &r600_texture_vtbl
;
275 pipe_reference_init(&resource
->b
.b
.reference
, 1);
276 resource
->b
.b
.screen
= screen
;
277 rtex
->pitch_override
= pitch_in_bytes_override
;
279 /* don't include stencil-only formats which we don't support for rendering */
280 rtex
->is_depth
= util_format_has_depth(util_format_description(rtex
->resource
.b
.b
.format
));
282 rtex
->surface
= *surface
;
283 r
= r600_setup_surface(screen
, rtex
,
284 pitch_in_bytes_override
);
290 /* Now create the backing buffer. */
291 if (!buf
&& alloc_bo
) {
292 unsigned base_align
= rtex
->surface
.bo_alignment
;
293 unsigned usage
= R600_TEX_IS_TILED(rtex
, 0) ? PIPE_USAGE_STATIC
: base
->usage
;
295 if (!r600_init_resource(rscreen
, resource
, rtex
->size
, base_align
, base
->bind
, usage
)) {
301 resource
->cs_buf
= rscreen
->ws
->buffer_get_cs_handle(buf
);
302 resource
->domains
= RADEON_DOMAIN_GTT
| RADEON_DOMAIN_VRAM
;
307 struct pipe_resource
*r600_texture_create(struct pipe_screen
*screen
,
308 const struct pipe_resource
*templ
)
310 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
311 struct radeon_surface surface
;
312 unsigned array_mode
= 0;
315 if (!(templ
->flags
& R600_RESOURCE_FLAG_TRANSFER
)) {
316 if (!(templ
->bind
& PIPE_BIND_SCANOUT
) &&
317 templ
->usage
!= PIPE_USAGE_STAGING
&&
318 templ
->usage
!= PIPE_USAGE_STREAM
) {
319 array_mode
= V_038000_ARRAY_2D_TILED_THIN1
;
320 } else if (util_format_is_compressed(templ
->format
)) {
321 array_mode
= V_038000_ARRAY_1D_TILED_THIN1
;
325 r
= r600_init_surface(rscreen
, &surface
, templ
, array_mode
,
326 templ
->flags
& R600_RESOURCE_FLAG_TRANSFER
,
327 templ
->flags
& R600_RESOURCE_FLAG_FLUSHED_DEPTH
);
331 r
= rscreen
->ws
->surface_best(rscreen
->ws
, &surface
);
335 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
,
336 0, NULL
, TRUE
, &surface
);
339 static struct pipe_surface
*r600_create_surface(struct pipe_context
*pipe
,
340 struct pipe_resource
*texture
,
341 const struct pipe_surface
*templ
)
343 struct r600_surface
*surface
= CALLOC_STRUCT(r600_surface
);
344 unsigned level
= templ
->u
.tex
.level
;
346 assert(templ
->u
.tex
.first_layer
== templ
->u
.tex
.last_layer
);
349 pipe_reference_init(&surface
->base
.reference
, 1);
350 pipe_resource_reference(&surface
->base
.texture
, texture
);
351 surface
->base
.context
= pipe
;
352 surface
->base
.format
= templ
->format
;
353 surface
->base
.width
= u_minify(texture
->width0
, level
);
354 surface
->base
.height
= u_minify(texture
->height0
, level
);
355 surface
->base
.usage
= templ
->usage
;
356 surface
->base
.u
= templ
->u
;
357 return &surface
->base
;
360 static void r600_surface_destroy(struct pipe_context
*pipe
,
361 struct pipe_surface
*surface
)
363 pipe_resource_reference(&surface
->texture
, NULL
);
367 struct pipe_resource
*r600_texture_from_handle(struct pipe_screen
*screen
,
368 const struct pipe_resource
*templ
,
369 struct winsys_handle
*whandle
)
371 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
372 struct pb_buffer
*buf
= NULL
;
374 unsigned array_mode
= 0;
375 enum radeon_bo_layout micro
, macro
;
376 struct radeon_surface surface
;
379 /* Support only 2D textures without mipmaps */
380 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
) ||
381 templ
->depth0
!= 1 || templ
->last_level
!= 0)
384 buf
= rscreen
->ws
->buffer_from_handle(rscreen
->ws
, whandle
, &stride
);
388 rscreen
->ws
->buffer_get_tiling(buf
, µ
, ¯o
,
389 &surface
.bankw
, &surface
.bankh
,
391 &surface
.stencil_tile_split
,
394 if (macro
== RADEON_LAYOUT_TILED
)
395 array_mode
= V_0280A0_ARRAY_2D_TILED_THIN1
;
396 else if (micro
== RADEON_LAYOUT_TILED
)
397 array_mode
= V_0280A0_ARRAY_1D_TILED_THIN1
;
401 r
= r600_init_surface(rscreen
, &surface
, templ
, array_mode
, false, false);
405 return (struct pipe_resource
*)r600_texture_create_object(screen
, templ
,
406 stride
, buf
, FALSE
, &surface
);
409 bool r600_init_flushed_depth_texture(struct pipe_context
*ctx
,
410 struct pipe_resource
*texture
,
411 struct r600_texture
**staging
)
413 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
414 struct pipe_resource resource
;
415 struct r600_texture
**flushed_depth_texture
= staging
?
416 staging
: &rtex
->flushed_depth_texture
;
418 if (!staging
&& rtex
->flushed_depth_texture
)
419 return true; /* it's ready */
421 resource
.target
= texture
->target
;
422 resource
.format
= texture
->format
;
423 resource
.width0
= texture
->width0
;
424 resource
.height0
= texture
->height0
;
425 resource
.depth0
= texture
->depth0
;
426 resource
.array_size
= texture
->array_size
;
427 resource
.last_level
= texture
->last_level
;
428 resource
.nr_samples
= texture
->nr_samples
;
429 resource
.usage
= staging
? PIPE_USAGE_STAGING
: PIPE_USAGE_STATIC
;
430 resource
.bind
= texture
->bind
& ~PIPE_BIND_DEPTH_STENCIL
;
431 resource
.flags
= texture
->flags
| R600_RESOURCE_FLAG_FLUSHED_DEPTH
;
434 resource
.flags
|= R600_RESOURCE_FLAG_TRANSFER
;
436 *flushed_depth_texture
= (struct r600_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
437 if (*flushed_depth_texture
== NULL
) {
438 R600_ERR("failed to create temporary texture to hold flushed depth\n");
442 (*flushed_depth_texture
)->is_flushing_texture
= TRUE
;
446 /* Needs adjustment for pixelformat:
448 static INLINE
unsigned u_box_volume( const struct pipe_box
*box
)
450 return box
->width
* box
->depth
* box
->height
;
453 struct pipe_transfer
* r600_texture_get_transfer(struct pipe_context
*ctx
,
454 struct pipe_resource
*texture
,
457 const struct pipe_box
*box
)
459 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
460 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
461 struct pipe_resource resource
;
462 struct r600_transfer
*trans
;
463 boolean use_staging_texture
= FALSE
;
465 /* We cannot map a tiled texture directly because the data is
466 * in a different order, therefore we do detiling using a blit.
468 * Also, use a temporary in GTT memory for read transfers, as
469 * the CPU is much happier reading out of cached system memory
470 * than uncached VRAM.
472 if (R600_TEX_IS_TILED(rtex
, level
)) {
473 use_staging_texture
= TRUE
;
476 if ((usage
& PIPE_TRANSFER_READ
) && u_box_volume(box
) > 1024)
477 use_staging_texture
= TRUE
;
479 /* Use a staging texture for uploads if the underlying BO is busy. */
480 if (!(usage
& PIPE_TRANSFER_READ
) &&
481 (rctx
->ws
->cs_is_buffer_referenced(rctx
->cs
, rtex
->resource
.cs_buf
, RADEON_USAGE_READWRITE
) ||
482 rctx
->ws
->buffer_is_busy(rtex
->resource
.buf
, RADEON_USAGE_READWRITE
))) {
483 use_staging_texture
= TRUE
;
486 if (texture
->flags
& R600_RESOURCE_FLAG_TRANSFER
) {
487 use_staging_texture
= FALSE
;
490 if (use_staging_texture
&& (usage
& PIPE_TRANSFER_MAP_DIRECTLY
)) {
494 trans
= CALLOC_STRUCT(r600_transfer
);
497 pipe_resource_reference(&trans
->transfer
.resource
, texture
);
498 trans
->transfer
.level
= level
;
499 trans
->transfer
.usage
= usage
;
500 trans
->transfer
.box
= *box
;
501 if (rtex
->is_depth
) {
502 /* XXX: only readback the rectangle which is being mapped?
504 /* XXX: when discard is true, no need to read back from depth texture
506 struct r600_texture
*staging_depth
;
508 if (!r600_init_flushed_depth_texture(ctx
, texture
, &staging_depth
)) {
509 R600_ERR("failed to create temporary texture to hold untiled copy\n");
510 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
515 r600_blit_uncompress_depth(ctx
, rtex
, staging_depth
,
517 box
->z
, box
->z
+ box
->depth
- 1,
520 trans
->transfer
.stride
= staging_depth
->surface
.level
[level
].pitch_bytes
;
521 trans
->offset
= r600_texture_get_offset(staging_depth
, level
, box
->z
);
522 trans
->staging
= (struct r600_resource
*)staging_depth
;
523 return &trans
->transfer
;
524 } else if (use_staging_texture
) {
525 resource
.target
= PIPE_TEXTURE_2D
;
526 resource
.format
= texture
->format
;
527 resource
.width0
= box
->width
;
528 resource
.height0
= box
->height
;
530 resource
.array_size
= 1;
531 resource
.last_level
= 0;
532 resource
.nr_samples
= 0;
533 resource
.usage
= PIPE_USAGE_STAGING
;
535 resource
.flags
= R600_RESOURCE_FLAG_TRANSFER
;
536 /* For texture reading, the temporary (detiled) texture is used as
537 * a render target when blitting from a tiled texture. */
538 if (usage
& PIPE_TRANSFER_READ
) {
539 resource
.bind
|= PIPE_BIND_RENDER_TARGET
;
541 /* For texture writing, the temporary texture is used as a sampler
542 * when blitting into a tiled texture. */
543 if (usage
& PIPE_TRANSFER_WRITE
) {
544 resource
.bind
|= PIPE_BIND_SAMPLER_VIEW
;
546 /* Create the temporary texture. */
547 trans
->staging
= (struct r600_resource
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
548 if (trans
->staging
== NULL
) {
549 R600_ERR("failed to create temporary texture to hold untiled copy\n");
550 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
555 trans
->transfer
.stride
=
556 ((struct r600_texture
*)trans
->staging
)->surface
.level
[0].pitch_bytes
;
557 if (usage
& PIPE_TRANSFER_READ
) {
558 r600_copy_to_staging_texture(ctx
, trans
);
559 /* Always referenced in the blit. */
560 r600_flush(ctx
, NULL
, 0);
562 return &trans
->transfer
;
564 trans
->transfer
.stride
= rtex
->surface
.level
[level
].pitch_bytes
;
565 trans
->transfer
.layer_stride
= rtex
->surface
.level
[level
].slice_size
;
566 trans
->offset
= r600_texture_get_offset(rtex
, level
, box
->z
);
567 return &trans
->transfer
;
570 void r600_texture_transfer_destroy(struct pipe_context
*ctx
,
571 struct pipe_transfer
*transfer
)
573 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
574 struct pipe_resource
*texture
= transfer
->resource
;
575 struct r600_texture
*rtex
= (struct r600_texture
*)texture
;
577 if ((transfer
->usage
& PIPE_TRANSFER_WRITE
) && rtransfer
->staging
) {
578 if (rtex
->is_depth
) {
579 ctx
->resource_copy_region(ctx
, texture
, transfer
->level
,
580 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
581 &rtransfer
->staging
->b
.b
, transfer
->level
,
584 r600_copy_from_staging_texture(ctx
, rtransfer
);
588 if (rtransfer
->staging
)
589 pipe_resource_reference((struct pipe_resource
**)&rtransfer
->staging
, NULL
);
591 pipe_resource_reference(&transfer
->resource
, NULL
);
595 void* r600_texture_transfer_map(struct pipe_context
*ctx
,
596 struct pipe_transfer
* transfer
)
598 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
599 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
600 struct radeon_winsys_cs_handle
*buf
;
601 struct r600_texture
*rtex
=
602 (struct r600_texture
*)transfer
->resource
;
603 enum pipe_format format
= transfer
->resource
->format
;
607 if ((transfer
->resource
->bind
& PIPE_BIND_GLOBAL
) && transfer
->resource
->target
== PIPE_BUFFER
) {
608 return r600_compute_global_transfer_map(ctx
, transfer
);
611 if (rtransfer
->staging
) {
612 buf
= ((struct r600_resource
*)rtransfer
->staging
)->cs_buf
;
614 buf
= ((struct r600_resource
*)transfer
->resource
)->cs_buf
;
617 if (rtex
->is_depth
|| !rtransfer
->staging
)
618 offset
= rtransfer
->offset
+
619 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
620 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
622 if (!(map
= rctx
->ws
->buffer_map(buf
, rctx
->cs
, transfer
->usage
))) {
629 void r600_texture_transfer_unmap(struct pipe_context
*ctx
,
630 struct pipe_transfer
* transfer
)
632 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
633 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
634 struct radeon_winsys_cs_handle
*buf
;
636 if ((transfer
->resource
->bind
& PIPE_BIND_GLOBAL
) && transfer
->resource
->target
== PIPE_BUFFER
) {
637 return r600_compute_global_transfer_unmap(ctx
, transfer
);
640 if (rtransfer
->staging
) {
641 buf
= ((struct r600_resource
*)rtransfer
->staging
)->cs_buf
;
643 buf
= ((struct r600_resource
*)transfer
->resource
)->cs_buf
;
645 rctx
->ws
->buffer_unmap(buf
);
648 void r600_init_surface_functions(struct r600_context
*r600
)
650 r600
->context
.create_surface
= r600_create_surface
;
651 r600
->context
.surface_destroy
= r600_surface_destroy
;
654 static unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format
,
655 const unsigned char *swizzle_view
)
658 unsigned char swizzle
[4];
660 const uint32_t swizzle_shift
[4] = {
663 const uint32_t swizzle_bit
[4] = {
668 util_format_compose_swizzles(swizzle_format
, swizzle_view
, swizzle
);
670 memcpy(swizzle
, swizzle_format
, 4);
674 for (i
= 0; i
< 4; i
++) {
675 switch (swizzle
[i
]) {
676 case UTIL_FORMAT_SWIZZLE_Y
:
677 result
|= swizzle_bit
[1] << swizzle_shift
[i
];
679 case UTIL_FORMAT_SWIZZLE_Z
:
680 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
682 case UTIL_FORMAT_SWIZZLE_W
:
683 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
685 case UTIL_FORMAT_SWIZZLE_0
:
686 result
|= V_038010_SQ_SEL_0
<< swizzle_shift
[i
];
688 case UTIL_FORMAT_SWIZZLE_1
:
689 result
|= V_038010_SQ_SEL_1
<< swizzle_shift
[i
];
691 default: /* UTIL_FORMAT_SWIZZLE_X */
692 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
698 /* texture format translate */
699 uint32_t r600_translate_texformat(struct pipe_screen
*screen
,
700 enum pipe_format format
,
701 const unsigned char *swizzle_view
,
702 uint32_t *word4_p
, uint32_t *yuv_format_p
)
704 uint32_t result
= 0, word4
= 0, yuv_format
= 0;
705 const struct util_format_description
*desc
;
706 boolean uniform
= TRUE
;
707 static int r600_enable_s3tc
= -1;
708 bool is_srgb_valid
= FALSE
;
711 const uint32_t sign_bit
[4] = {
712 S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED
),
713 S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED
),
714 S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED
),
715 S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED
)
717 desc
= util_format_description(format
);
719 word4
|= r600_get_swizzle_combined(desc
->swizzle
, swizzle_view
);
721 /* Colorspace (return non-RGB formats directly). */
722 switch (desc
->colorspace
) {
723 /* Depth stencil formats */
724 case UTIL_FORMAT_COLORSPACE_ZS
:
726 case PIPE_FORMAT_Z16_UNORM
:
729 case PIPE_FORMAT_X24S8_UINT
:
730 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
731 case PIPE_FORMAT_Z24X8_UNORM
:
732 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
735 case PIPE_FORMAT_S8X24_UINT
:
736 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
737 case PIPE_FORMAT_X8Z24_UNORM
:
738 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
741 case PIPE_FORMAT_S8_UINT
:
743 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
745 case PIPE_FORMAT_Z32_FLOAT
:
746 result
= FMT_32_FLOAT
;
748 case PIPE_FORMAT_X32_S8X24_UINT
:
749 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
750 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
751 result
= FMT_X24_8_32_FLOAT
;
757 case UTIL_FORMAT_COLORSPACE_YUV
:
758 yuv_format
|= (1 << 30);
760 case PIPE_FORMAT_UYVY
:
761 case PIPE_FORMAT_YUYV
:
765 goto out_unknown
; /* XXX */
767 case UTIL_FORMAT_COLORSPACE_SRGB
:
768 word4
|= S_038010_FORCE_DEGAMMA(1);
775 if (r600_enable_s3tc
== -1) {
776 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
777 if (rscreen
->info
.drm_minor
>= 9)
778 r600_enable_s3tc
= 1;
780 r600_enable_s3tc
= debug_get_bool_option("R600_ENABLE_S3TC", FALSE
);
783 if (desc
->layout
== UTIL_FORMAT_LAYOUT_RGTC
) {
784 if (!r600_enable_s3tc
)
788 case PIPE_FORMAT_RGTC1_SNORM
:
789 case PIPE_FORMAT_LATC1_SNORM
:
790 word4
|= sign_bit
[0];
791 case PIPE_FORMAT_RGTC1_UNORM
:
792 case PIPE_FORMAT_LATC1_UNORM
:
795 case PIPE_FORMAT_RGTC2_SNORM
:
796 case PIPE_FORMAT_LATC2_SNORM
:
797 word4
|= sign_bit
[0] | sign_bit
[1];
798 case PIPE_FORMAT_RGTC2_UNORM
:
799 case PIPE_FORMAT_LATC2_UNORM
:
807 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
809 if (!r600_enable_s3tc
)
812 if (!util_format_s3tc_enabled
) {
817 case PIPE_FORMAT_DXT1_RGB
:
818 case PIPE_FORMAT_DXT1_RGBA
:
819 case PIPE_FORMAT_DXT1_SRGB
:
820 case PIPE_FORMAT_DXT1_SRGBA
:
822 is_srgb_valid
= TRUE
;
824 case PIPE_FORMAT_DXT3_RGBA
:
825 case PIPE_FORMAT_DXT3_SRGBA
:
827 is_srgb_valid
= TRUE
;
829 case PIPE_FORMAT_DXT5_RGBA
:
830 case PIPE_FORMAT_DXT5_SRGBA
:
832 is_srgb_valid
= TRUE
;
839 if (desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
) {
841 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
842 case PIPE_FORMAT_G8R8_B8R8_UNORM
:
845 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
846 case PIPE_FORMAT_R8G8_R8B8_UNORM
:
854 if (format
== PIPE_FORMAT_R9G9B9E5_FLOAT
) {
855 result
= FMT_5_9_9_9_SHAREDEXP
;
857 } else if (format
== PIPE_FORMAT_R11G11B10_FLOAT
) {
858 result
= FMT_10_11_11_FLOAT
;
863 for (i
= 0; i
< desc
->nr_channels
; i
++) {
864 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
865 word4
|= sign_bit
[i
];
869 /* R8G8Bx_SNORM - XXX CxV8U8 */
871 /* See whether the components are of the same size. */
872 for (i
= 1; i
< desc
->nr_channels
; i
++) {
873 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
876 /* Non-uniform formats. */
878 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
&&
879 desc
->channel
[0].pure_integer
)
880 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
881 switch(desc
->nr_channels
) {
883 if (desc
->channel
[0].size
== 5 &&
884 desc
->channel
[1].size
== 6 &&
885 desc
->channel
[2].size
== 5) {
891 if (desc
->channel
[0].size
== 5 &&
892 desc
->channel
[1].size
== 5 &&
893 desc
->channel
[2].size
== 5 &&
894 desc
->channel
[3].size
== 1) {
895 result
= FMT_1_5_5_5
;
898 if (desc
->channel
[0].size
== 10 &&
899 desc
->channel
[1].size
== 10 &&
900 desc
->channel
[2].size
== 10 &&
901 desc
->channel
[3].size
== 2) {
902 result
= FMT_2_10_10_10
;
910 /* Find the first non-VOID channel. */
911 for (i
= 0; i
< 4; i
++) {
912 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
920 /* uniform formats */
921 switch (desc
->channel
[i
].type
) {
922 case UTIL_FORMAT_TYPE_UNSIGNED
:
923 case UTIL_FORMAT_TYPE_SIGNED
:
925 if (!desc
->channel
[i
].normalized
&&
926 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
930 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
&&
931 desc
->channel
[i
].pure_integer
)
932 word4
|= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT
);
934 switch (desc
->channel
[i
].size
) {
936 switch (desc
->nr_channels
) {
941 result
= FMT_4_4_4_4
;
946 switch (desc
->nr_channels
) {
954 result
= FMT_8_8_8_8
;
955 is_srgb_valid
= TRUE
;
960 switch (desc
->nr_channels
) {
968 result
= FMT_16_16_16_16
;
973 switch (desc
->nr_channels
) {
981 result
= FMT_32_32_32_32
;
987 case UTIL_FORMAT_TYPE_FLOAT
:
988 switch (desc
->channel
[i
].size
) {
990 switch (desc
->nr_channels
) {
992 result
= FMT_16_FLOAT
;
995 result
= FMT_16_16_FLOAT
;
998 result
= FMT_16_16_16_16_FLOAT
;
1003 switch (desc
->nr_channels
) {
1005 result
= FMT_32_FLOAT
;
1008 result
= FMT_32_32_FLOAT
;
1011 result
= FMT_32_32_32_32_FLOAT
;
1020 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
&& !is_srgb_valid
)
1025 *yuv_format_p
= yuv_format
;
1028 /* R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format)); */