1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Pixel format accessor functions.
32 * @author Jose Fonseca <jfonseca@vmware.com>
35 #include "util/format/u_format.h"
36 #include "util/format/u_format_s3tc.h"
37 #include "util/u_math.h"
39 #include "pipe/p_defines.h"
40 #include "pipe/p_screen.h"
44 * Copy 2D rect from one place to another.
45 * Position and sizes are in pixels.
46 * src_stride may be negative to do vertical flip of pixels from source.
49 util_copy_rect(ubyte
* dst
,
50 enum pipe_format format
,
62 int src_stride_pos
= src_stride
< 0 ? -src_stride
: src_stride
;
63 int blocksize
= util_format_get_blocksize(format
);
64 int blockwidth
= util_format_get_blockwidth(format
);
65 int blockheight
= util_format_get_blockheight(format
);
67 assert(blocksize
> 0);
68 assert(blockwidth
> 0);
69 assert(blockheight
> 0);
73 width
= (width
+ blockwidth
- 1)/blockwidth
;
74 height
= (height
+ blockheight
- 1)/blockheight
;
78 dst
+= dst_x
* blocksize
;
79 src
+= src_x
* blocksize
;
80 dst
+= dst_y
* dst_stride
;
81 src
+= src_y
* src_stride_pos
;
84 if (width
== dst_stride
&& width
== (unsigned)src_stride
)
85 memcpy(dst
, src
, height
* width
);
87 for (i
= 0; i
< height
; i
++) {
88 memcpy(dst
, src
, width
);
97 util_format_is_float(enum pipe_format format
)
99 const struct util_format_description
*desc
= util_format_description(format
);
107 i
= util_format_get_first_non_void_channel(format
);
112 return desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_FLOAT
? TRUE
: FALSE
;
116 /** Test if the format contains RGB, but not alpha */
118 util_format_has_alpha(enum pipe_format format
)
120 const struct util_format_description
*desc
=
121 util_format_description(format
);
123 return (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
124 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
125 desc
->swizzle
[3] != PIPE_SWIZZLE_1
;
130 util_format_is_luminance(enum pipe_format format
)
132 const struct util_format_description
*desc
=
133 util_format_description(format
);
135 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
136 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
137 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
138 desc
->swizzle
[1] == PIPE_SWIZZLE_X
&&
139 desc
->swizzle
[2] == PIPE_SWIZZLE_X
&&
140 desc
->swizzle
[3] == PIPE_SWIZZLE_1
) {
147 util_format_is_alpha(enum pipe_format format
)
149 const struct util_format_description
*desc
=
150 util_format_description(format
);
152 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
153 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
154 desc
->swizzle
[0] == PIPE_SWIZZLE_0
&&
155 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
156 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
157 desc
->swizzle
[3] == PIPE_SWIZZLE_X
) {
164 util_format_is_pure_integer(enum pipe_format format
)
166 const struct util_format_description
*desc
= util_format_description(format
);
169 /* Find the first non-void channel. */
170 i
= util_format_get_first_non_void_channel(format
);
174 return desc
->channel
[i
].pure_integer
? TRUE
: FALSE
;
178 util_format_is_pure_sint(enum pipe_format format
)
180 const struct util_format_description
*desc
= util_format_description(format
);
183 i
= util_format_get_first_non_void_channel(format
);
187 return (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
&& desc
->channel
[i
].pure_integer
) ? TRUE
: FALSE
;
191 util_format_is_pure_uint(enum pipe_format format
)
193 const struct util_format_description
*desc
= util_format_description(format
);
196 i
= util_format_get_first_non_void_channel(format
);
200 return (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
&& desc
->channel
[i
].pure_integer
) ? TRUE
: FALSE
;
204 * Returns true if the format contains normalized signed channels.
207 util_format_is_snorm(enum pipe_format format
)
209 const struct util_format_description
*desc
= util_format_description(format
);
211 return desc
->is_snorm
;
215 * Returns true if the format contains normalized unsigned channels.
218 util_format_is_unorm(enum pipe_format format
)
220 const struct util_format_description
*desc
= util_format_description(format
);
222 return desc
->is_unorm
;
226 util_format_is_snorm8(enum pipe_format format
)
228 const struct util_format_description
*desc
= util_format_description(format
);
234 i
= util_format_get_first_non_void_channel(format
);
238 return desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
&&
239 !desc
->channel
[i
].pure_integer
&&
240 desc
->channel
[i
].normalized
&&
241 desc
->channel
[i
].size
== 8;
245 util_format_is_luminance_alpha(enum pipe_format format
)
247 const struct util_format_description
*desc
=
248 util_format_description(format
);
250 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
251 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
252 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
253 desc
->swizzle
[1] == PIPE_SWIZZLE_X
&&
254 desc
->swizzle
[2] == PIPE_SWIZZLE_X
&&
255 desc
->swizzle
[3] == PIPE_SWIZZLE_Y
) {
263 util_format_is_intensity(enum pipe_format format
)
265 const struct util_format_description
*desc
=
266 util_format_description(format
);
268 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
269 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
270 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
271 desc
->swizzle
[1] == PIPE_SWIZZLE_X
&&
272 desc
->swizzle
[2] == PIPE_SWIZZLE_X
&&
273 desc
->swizzle
[3] == PIPE_SWIZZLE_X
) {
280 util_format_is_subsampled_422(enum pipe_format format
)
282 const struct util_format_description
*desc
=
283 util_format_description(format
);
285 return desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
&&
286 desc
->block
.width
== 2 &&
287 desc
->block
.height
== 1 &&
288 desc
->block
.bits
== 32;
292 * Calculates the MRD for the depth format. MRD is used in depth bias
293 * for UNORM and unbound depth buffers. When the depth buffer is floating
294 * point, the depth bias calculation does not use the MRD. However, the
295 * default MRD will be 1.0 / ((1 << 24) - 1).
298 util_get_depth_format_mrd(const struct util_format_description
*desc
)
301 * Depth buffer formats without a depth component OR scenarios
302 * without a bound depth buffer default to D24.
304 double mrd
= 1.0 / ((1 << 24) - 1);
305 unsigned depth_channel
;
310 * Some depth formats do not store the depth component in the first
311 * channel, detect the format and adjust the depth channel. Get the
312 * swizzled depth component channel.
314 depth_channel
= desc
->swizzle
[0];
316 if (desc
->channel
[depth_channel
].type
== UTIL_FORMAT_TYPE_UNSIGNED
&&
317 desc
->channel
[depth_channel
].normalized
) {
320 depth_bits
= desc
->channel
[depth_channel
].size
;
321 mrd
= 1.0 / ((1ULL << depth_bits
) - 1);
329 util_format_read_4(enum pipe_format format
,
330 void *dst
, unsigned dst_stride
,
331 const void *src
, unsigned src_stride
,
332 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
334 const struct util_format_description
*format_desc
;
335 const struct util_format_unpack_description
*unpack
=
336 util_format_unpack_description(format
);
337 const uint8_t *src_row
;
339 format_desc
= util_format_description(format
);
341 assert(x
% format_desc
->block
.width
== 0);
342 assert(y
% format_desc
->block
.height
== 0);
344 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
346 unpack
->unpack_rgba(dst
, dst_stride
, src_row
, src_stride
, w
, h
);
351 util_format_write_4(enum pipe_format format
,
352 const void *src
, unsigned src_stride
,
353 void *dst
, unsigned dst_stride
,
354 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
356 const struct util_format_description
*format_desc
;
357 const struct util_format_pack_description
*pack
=
358 util_format_pack_description(format
);
361 format_desc
= util_format_description(format
);
363 assert(x
% format_desc
->block
.width
== 0);
364 assert(y
% format_desc
->block
.height
== 0);
366 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
368 if (util_format_is_pure_uint(format
))
369 pack
->pack_rgba_uint(dst_row
, dst_stride
, src
, src_stride
, w
, h
);
370 else if (util_format_is_pure_sint(format
))
371 pack
->pack_rgba_sint(dst_row
, dst_stride
, src
, src_stride
, w
, h
);
373 pack
->pack_rgba_float(dst_row
, dst_stride
, src
, src_stride
, w
, h
);
378 util_format_read_4ub(enum pipe_format format
, uint8_t *dst
, unsigned dst_stride
, const void *src
, unsigned src_stride
, unsigned x
, unsigned y
, unsigned w
, unsigned h
)
380 const struct util_format_description
*format_desc
;
381 const struct util_format_unpack_description
*unpack
=
382 util_format_unpack_description(format
);
383 const uint8_t *src_row
;
386 format_desc
= util_format_description(format
);
388 assert(x
% format_desc
->block
.width
== 0);
389 assert(y
% format_desc
->block
.height
== 0);
391 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
394 unpack
->unpack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
399 util_format_write_4ub(enum pipe_format format
, const uint8_t *src
, unsigned src_stride
, void *dst
, unsigned dst_stride
, unsigned x
, unsigned y
, unsigned w
, unsigned h
)
401 const struct util_format_description
*format_desc
;
402 const struct util_format_pack_description
*pack
=
403 util_format_pack_description(format
);
405 const uint8_t *src_row
;
407 format_desc
= util_format_description(format
);
409 assert(x
% format_desc
->block
.width
== 0);
410 assert(y
% format_desc
->block
.height
== 0);
412 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
415 pack
->pack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
419 * Check if we can safely memcopy from the source format to the dest format.
420 * This basically covers the cases of a "used" channel copied to a typeless
421 * channel, plus some 1-channel cases.
422 * Examples of compatible copy formats include:
423 * b8g8r8a8_unorm -> b8g8r8x8_unorm
424 * a8r8g8b8_unorm -> x8r8g8b8_unorm
425 * b5g5r5a1_unorm -> b5g5r5x1_unorm
426 * b4g4r4a4_unorm -> b4g4r4x4_unorm
427 * l8_unorm -> r8_unorm
428 * i8_unorm -> l8_unorm
429 * i8_unorm -> a8_unorm
430 * i8_unorm -> r8_unorm
431 * l16_unorm -> r16_unorm
432 * z24_unorm_s8_uint -> z24x8_unorm
433 * s8_uint_z24_unorm -> x8z24_unorm
434 * r8g8b8a8_unorm -> r8g8b8x8_unorm
435 * a8b8g8r8_srgb -> x8b8g8r8_srgb
436 * b8g8r8a8_srgb -> b8g8r8x8_srgb
437 * a8r8g8b8_srgb -> x8r8g8b8_srgb
438 * a8b8g8r8_unorm -> x8b8g8r8_unorm
439 * r10g10b10a2_uscaled -> r10g10b10x2_uscaled
440 * r10sg10sb10sa2u_norm -> r10g10b10x2_snorm
443 util_is_format_compatible(const struct util_format_description
*src_desc
,
444 const struct util_format_description
*dst_desc
)
448 if (src_desc
->format
== dst_desc
->format
) {
452 if (src_desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
||
453 dst_desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
) {
457 if (src_desc
->block
.bits
!= dst_desc
->block
.bits
||
458 src_desc
->nr_channels
!= dst_desc
->nr_channels
||
459 src_desc
->colorspace
!= dst_desc
->colorspace
) {
463 for (chan
= 0; chan
< 4; ++chan
) {
464 if (src_desc
->channel
[chan
].size
!=
465 dst_desc
->channel
[chan
].size
) {
470 for (chan
= 0; chan
< 4; ++chan
) {
471 enum pipe_swizzle swizzle
= dst_desc
->swizzle
[chan
];
474 if (src_desc
->swizzle
[chan
] != swizzle
) {
477 if ((src_desc
->channel
[swizzle
].type
!=
478 dst_desc
->channel
[swizzle
].type
) ||
479 (src_desc
->channel
[swizzle
].normalized
!=
480 dst_desc
->channel
[swizzle
].normalized
)) {
491 util_format_fits_8unorm(const struct util_format_description
*format_desc
)
496 * After linearized sRGB values require more than 8bits.
499 if (format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
503 switch (format_desc
->layout
) {
505 case UTIL_FORMAT_LAYOUT_S3TC
:
507 * These are straight forward.
510 case UTIL_FORMAT_LAYOUT_RGTC
:
511 if (format_desc
->format
== PIPE_FORMAT_RGTC1_SNORM
||
512 format_desc
->format
== PIPE_FORMAT_RGTC2_SNORM
||
513 format_desc
->format
== PIPE_FORMAT_LATC1_SNORM
||
514 format_desc
->format
== PIPE_FORMAT_LATC2_SNORM
)
517 case UTIL_FORMAT_LAYOUT_BPTC
:
518 if (format_desc
->format
== PIPE_FORMAT_BPTC_RGBA_UNORM
)
522 case UTIL_FORMAT_LAYOUT_ETC
:
523 if (format_desc
->format
== PIPE_FORMAT_ETC1_RGB8
)
527 case UTIL_FORMAT_LAYOUT_PLAIN
:
529 * For these we can find a generic rule.
532 for (chan
= 0; chan
< format_desc
->nr_channels
; ++chan
) {
533 switch (format_desc
->channel
[chan
].type
) {
534 case UTIL_FORMAT_TYPE_VOID
:
536 case UTIL_FORMAT_TYPE_UNSIGNED
:
537 if (!format_desc
->channel
[chan
].normalized
||
538 format_desc
->channel
[chan
].size
> 8) {
550 * Handle all others on a case by case basis.
553 switch (format_desc
->format
) {
554 case PIPE_FORMAT_R1_UNORM
:
555 case PIPE_FORMAT_UYVY
:
556 case PIPE_FORMAT_YUYV
:
557 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
558 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
569 util_format_translate(enum pipe_format dst_format
,
570 void *dst
, unsigned dst_stride
,
571 unsigned dst_x
, unsigned dst_y
,
572 enum pipe_format src_format
,
573 const void *src
, unsigned src_stride
,
574 unsigned src_x
, unsigned src_y
,
575 unsigned width
, unsigned height
)
577 const struct util_format_description
*dst_format_desc
;
578 const struct util_format_description
*src_format_desc
;
579 const struct util_format_pack_description
*pack
=
580 util_format_pack_description(dst_format
);
581 const struct util_format_unpack_description
*unpack
=
582 util_format_unpack_description(src_format
);
584 const uint8_t *src_row
;
585 unsigned x_step
, y_step
;
589 dst_format_desc
= util_format_description(dst_format
);
590 src_format_desc
= util_format_description(src_format
);
592 if (util_is_format_compatible(src_format_desc
, dst_format_desc
)) {
597 util_copy_rect(dst
, dst_format
, dst_stride
, dst_x
, dst_y
,
598 width
, height
, src
, (int)src_stride
,
603 assert(dst_x
% dst_format_desc
->block
.width
== 0);
604 assert(dst_y
% dst_format_desc
->block
.height
== 0);
605 assert(src_x
% src_format_desc
->block
.width
== 0);
606 assert(src_y
% src_format_desc
->block
.height
== 0);
608 dst_row
= (uint8_t *)dst
+ dst_y
*dst_stride
+ dst_x
*(dst_format_desc
->block
.bits
/8);
609 src_row
= (const uint8_t *)src
+ src_y
*src_stride
+ src_x
*(src_format_desc
->block
.bits
/8);
612 * This works because all pixel formats have pixel blocks with power of two
616 y_step
= MAX2(dst_format_desc
->block
.height
, src_format_desc
->block
.height
);
617 x_step
= MAX2(dst_format_desc
->block
.width
, src_format_desc
->block
.width
);
618 assert(y_step
% dst_format_desc
->block
.height
== 0);
619 assert(y_step
% src_format_desc
->block
.height
== 0);
621 dst_step
= y_step
/ dst_format_desc
->block
.height
* dst_stride
;
622 src_step
= y_step
/ src_format_desc
->block
.height
* src_stride
;
625 * TODO: double formats will loose precision
626 * TODO: Add a special case for formats that are mere swizzles of each other
629 if (src_format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
||
630 dst_format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) {
632 uint8_t *tmp_s
= NULL
;
637 if (unpack
->unpack_z_float
&& pack
->pack_z_float
) {
638 tmp_z
= malloc(width
* sizeof *tmp_z
);
641 if (unpack
->unpack_s_8uint
&& pack
->pack_s_8uint
) {
642 tmp_s
= malloc(width
* sizeof *tmp_s
);
647 util_format_unpack_z_float(src_format
, tmp_z
, src_row
, width
);
648 util_format_pack_z_float(dst_format
, dst_row
, tmp_z
, width
);
652 util_format_unpack_s_8uint(src_format
, tmp_s
, src_row
, width
);
653 util_format_pack_s_8uint(dst_format
, dst_row
, tmp_s
, width
);
667 if (util_format_fits_8unorm(src_format_desc
) ||
668 util_format_fits_8unorm(dst_format_desc
)) {
672 if (!unpack
->unpack_rgba_8unorm
||
673 !pack
->pack_rgba_8unorm
) {
677 tmp_stride
= MAX2(width
, x_step
) * 4 * sizeof *tmp_row
;
678 tmp_row
= malloc(y_step
* tmp_stride
);
682 while (height
>= y_step
) {
683 unpack
->unpack_rgba_8unorm(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, y_step
);
684 pack
->pack_rgba_8unorm(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, y_step
);
692 unpack
->unpack_rgba_8unorm(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, height
);
693 pack
->pack_rgba_8unorm(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, height
);
698 else if (util_format_is_pure_sint(src_format
) ||
699 util_format_is_pure_sint(dst_format
)) {
703 if (util_format_is_pure_sint(src_format
) !=
704 util_format_is_pure_sint(dst_format
)) {
708 tmp_stride
= MAX2(width
, x_step
) * 4 * sizeof *tmp_row
;
709 tmp_row
= malloc(y_step
* tmp_stride
);
713 while (height
>= y_step
) {
714 unpack
->unpack_rgba(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, y_step
);
715 pack
->pack_rgba_sint(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, y_step
);
723 unpack
->unpack_rgba(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, height
);
724 pack
->pack_rgba_sint(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, height
);
729 else if (util_format_is_pure_uint(src_format
) ||
730 util_format_is_pure_uint(dst_format
)) {
732 unsigned int *tmp_row
;
734 if (!unpack
->unpack_rgba
||
735 !pack
->pack_rgba_uint
) {
739 tmp_stride
= MAX2(width
, x_step
) * 4 * sizeof *tmp_row
;
740 tmp_row
= malloc(y_step
* tmp_stride
);
744 while (height
>= y_step
) {
745 unpack
->unpack_rgba(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, y_step
);
746 pack
->pack_rgba_uint(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, y_step
);
754 unpack
->unpack_rgba(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, height
);
755 pack
->pack_rgba_uint(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, height
);
764 if (!unpack
->unpack_rgba
||
765 !pack
->pack_rgba_float
) {
769 tmp_stride
= MAX2(width
, x_step
) * 4 * sizeof *tmp_row
;
770 tmp_row
= malloc(y_step
* tmp_stride
);
774 while (height
>= y_step
) {
775 unpack
->unpack_rgba(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, y_step
);
776 pack
->pack_rgba_float(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, y_step
);
784 unpack
->unpack_rgba(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, height
);
785 pack
->pack_rgba_float(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, height
);
794 util_format_translate_3d(enum pipe_format dst_format
,
795 void *dst
, unsigned dst_stride
,
796 unsigned dst_slice_stride
,
797 unsigned dst_x
, unsigned dst_y
,
799 enum pipe_format src_format
,
800 const void *src
, unsigned src_stride
,
801 unsigned src_slice_stride
,
802 unsigned src_x
, unsigned src_y
,
803 unsigned src_z
, unsigned width
,
804 unsigned height
, unsigned depth
)
807 const uint8_t *src_layer
;
811 dst_layer
+= dst_z
* dst_slice_stride
;
812 src_layer
+= src_z
* src_slice_stride
;
813 for (z
= 0; z
< depth
; ++z
) {
814 if (!util_format_translate(dst_format
, dst_layer
, dst_stride
,
816 src_format
, src_layer
, src_stride
,
821 dst_layer
+= dst_slice_stride
;
822 src_layer
+= src_slice_stride
;
827 void util_format_compose_swizzles(const unsigned char swz1
[4],
828 const unsigned char swz2
[4],
829 unsigned char dst
[4])
833 for (i
= 0; i
< 4; i
++) {
834 dst
[i
] = swz2
[i
] <= PIPE_SWIZZLE_W
?
835 swz1
[swz2
[i
]] : swz2
[i
];
839 void util_format_apply_color_swizzle(union pipe_color_union
*dst
,
840 const union pipe_color_union
*src
,
841 const unsigned char swz
[4],
842 const boolean is_integer
)
847 for (c
= 0; c
< 4; ++c
) {
849 case PIPE_SWIZZLE_X
: dst
->ui
[c
] = src
->ui
[0]; break;
850 case PIPE_SWIZZLE_Y
: dst
->ui
[c
] = src
->ui
[1]; break;
851 case PIPE_SWIZZLE_Z
: dst
->ui
[c
] = src
->ui
[2]; break;
852 case PIPE_SWIZZLE_W
: dst
->ui
[c
] = src
->ui
[3]; break;
854 dst
->ui
[c
] = (swz
[c
] == PIPE_SWIZZLE_1
) ? 1 : 0;
859 for (c
= 0; c
< 4; ++c
) {
861 case PIPE_SWIZZLE_X
: dst
->f
[c
] = src
->f
[0]; break;
862 case PIPE_SWIZZLE_Y
: dst
->f
[c
] = src
->f
[1]; break;
863 case PIPE_SWIZZLE_Z
: dst
->f
[c
] = src
->f
[2]; break;
864 case PIPE_SWIZZLE_W
: dst
->f
[c
] = src
->f
[3]; break;
866 dst
->f
[c
] = (swz
[c
] == PIPE_SWIZZLE_1
) ? 1.0f
: 0.0f
;
873 void pipe_swizzle_4f(float *dst
, const float *src
,
874 const unsigned char swz
[4])
878 for (i
= 0; i
< 4; i
++) {
879 if (swz
[i
] <= PIPE_SWIZZLE_W
)
880 dst
[i
] = src
[swz
[i
]];
881 else if (swz
[i
] == PIPE_SWIZZLE_0
)
883 else if (swz
[i
] == PIPE_SWIZZLE_1
)
888 void util_format_unswizzle_4f(float *dst
, const float *src
,
889 const unsigned char swz
[4])
893 for (i
= 0; i
< 4; i
++) {
912 util_format_snorm8_to_sint8(enum pipe_format format
)
915 case PIPE_FORMAT_R8_SNORM
:
916 return PIPE_FORMAT_R8_SINT
;
917 case PIPE_FORMAT_R8G8_SNORM
:
918 return PIPE_FORMAT_R8G8_SINT
;
919 case PIPE_FORMAT_R8G8B8_SNORM
:
920 return PIPE_FORMAT_R8G8B8_SINT
;
921 case PIPE_FORMAT_R8G8B8A8_SNORM
:
922 return PIPE_FORMAT_R8G8B8A8_SINT
;
924 case PIPE_FORMAT_A8_SNORM
:
925 return PIPE_FORMAT_A8_SINT
;
926 case PIPE_FORMAT_L8_SNORM
:
927 return PIPE_FORMAT_L8_SINT
;
928 case PIPE_FORMAT_L8A8_SNORM
:
929 return PIPE_FORMAT_L8A8_SINT
;
930 case PIPE_FORMAT_I8_SNORM
:
931 return PIPE_FORMAT_I8_SINT
;
933 case PIPE_FORMAT_R8G8B8X8_SNORM
:
934 return PIPE_FORMAT_R8G8B8X8_SINT
;
935 case PIPE_FORMAT_R8A8_SNORM
:
936 return PIPE_FORMAT_R8A8_SINT
;
937 case PIPE_FORMAT_G8R8_SNORM
:
938 return PIPE_FORMAT_G8R8_SINT
;
939 case PIPE_FORMAT_A8B8G8R8_SNORM
:
940 return PIPE_FORMAT_A8B8G8R8_SINT
;
941 case PIPE_FORMAT_X8B8G8R8_SNORM
:
942 return PIPE_FORMAT_X8B8G8R8_SINT
;