1 /**************************************************************************
3 * Copyright 2009-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 **************************************************************************/
33 #include "pipe/p_format.h"
34 #include "util/u_debug.h"
42 * Describe how to pack/unpack pixels into/from the prescribed format.
44 * XXX: This could be renamed to something like util_format_pack, or broke down
45 * in flags inside util_format_block that said exactly what we want.
47 enum util_format_layout
{
49 * Formats with util_format_block::width == util_format_block::height == 1
50 * that can be described as an ordinary data structure.
52 UTIL_FORMAT_LAYOUT_PLAIN
= 0,
55 * Formats with sub-sampled channels.
57 * This is for formats like YV12 where there is less than one sample per
60 UTIL_FORMAT_LAYOUT_SUBSAMPLED
= 3,
63 * S3 Texture Compression formats.
65 UTIL_FORMAT_LAYOUT_S3TC
= 4,
68 * Red-Green Texture Compression formats.
70 UTIL_FORMAT_LAYOUT_RGTC
= 5,
73 * Everything else that doesn't fit in any of the above layouts.
75 UTIL_FORMAT_LAYOUT_OTHER
= 6
79 struct util_format_block
81 /** Block width in pixels */
84 /** Block height in pixels */
87 /** Block size in bits */
92 enum util_format_type
{
93 UTIL_FORMAT_TYPE_VOID
= 0,
94 UTIL_FORMAT_TYPE_UNSIGNED
= 1,
95 UTIL_FORMAT_TYPE_SIGNED
= 2,
96 UTIL_FORMAT_TYPE_FIXED
= 3,
97 UTIL_FORMAT_TYPE_FLOAT
= 4
101 enum util_format_swizzle
{
102 UTIL_FORMAT_SWIZZLE_X
= 0,
103 UTIL_FORMAT_SWIZZLE_Y
= 1,
104 UTIL_FORMAT_SWIZZLE_Z
= 2,
105 UTIL_FORMAT_SWIZZLE_W
= 3,
106 UTIL_FORMAT_SWIZZLE_0
= 4,
107 UTIL_FORMAT_SWIZZLE_1
= 5,
108 UTIL_FORMAT_SWIZZLE_NONE
= 6
112 enum util_format_colorspace
{
113 UTIL_FORMAT_COLORSPACE_RGB
= 0,
114 UTIL_FORMAT_COLORSPACE_SRGB
= 1,
115 UTIL_FORMAT_COLORSPACE_YUV
= 2,
116 UTIL_FORMAT_COLORSPACE_ZS
= 3
120 struct util_format_channel_description
123 unsigned normalized
:1;
128 struct util_format_description
130 enum pipe_format format
;
135 * Short name, striped of the prefix, lower case.
137 const char *short_name
;
140 * Pixel block dimensions.
142 struct util_format_block block
;
144 enum util_format_layout layout
;
147 * The number of channels.
149 unsigned nr_channels
:3;
152 * Whether all channels have the same number of (whole) bytes.
157 * Whether the pixel format can be described as a bitfield structure.
160 * - pixel depth must be 8, 16, or 32 bits;
161 * - all channels must be unsigned, signed, or void
163 unsigned is_bitmask
:1;
166 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
171 * Input channel description.
173 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
175 struct util_format_channel_description channel
[4];
178 * Output channel swizzle.
180 * The order is either:
184 * depending on the colorspace.
186 unsigned char swizzle
[4];
189 * Colorspace transformation.
191 enum util_format_colorspace colorspace
;
194 * Unpack pixel blocks to R8G8B8A8_UNORM.
195 * Note: strides are in bytes.
197 * Only defined for non-depth-stencil formats.
200 (*unpack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
201 const uint8_t *src
, unsigned src_stride
,
202 unsigned width
, unsigned height
);
205 * Pack pixel blocks from R8G8B8A8_UNORM.
206 * Note: strides are in bytes.
208 * Only defined for non-depth-stencil formats.
211 (*pack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
212 const uint8_t *src
, unsigned src_stride
,
213 unsigned width
, unsigned height
);
216 * Unpack pixel blocks to R32G32B32A32_FLOAT.
217 * Note: strides are in bytes.
219 * Only defined for non-depth-stencil formats.
222 (*unpack_rgba_float
)(float *dst
, unsigned dst_stride
,
223 const uint8_t *src
, unsigned src_stride
,
224 unsigned width
, unsigned height
);
227 * Pack pixel blocks from R32G32B32A32_FLOAT.
228 * Note: strides are in bytes.
230 * Only defined for non-depth-stencil formats.
233 (*pack_rgba_float
)(uint8_t *dst
, unsigned dst_stride
,
234 const float *src
, unsigned src_stride
,
235 unsigned width
, unsigned height
);
238 * Fetch a single pixel (i, j) from a block.
240 * Only defined for non-depth-stencil formats.
243 (*fetch_rgba_float
)(float *dst
,
245 unsigned i
, unsigned j
);
248 * Unpack pixels to Z32_UNORM.
249 * Note: strides are in bytes.
251 * Only defined for depth formats.
254 (*unpack_z_32unorm
)(uint32_t *dst
, unsigned dst_stride
,
255 const uint8_t *src
, unsigned src_stride
,
256 unsigned width
, unsigned height
);
259 * Pack pixels from Z32_FLOAT.
260 * Note: strides are in bytes.
262 * Only defined for depth formats.
265 (*pack_z_32unorm
)(uint8_t *dst
, unsigned dst_stride
,
266 const uint32_t *src
, unsigned src_stride
,
267 unsigned width
, unsigned height
);
270 * Unpack pixels to Z32_FLOAT.
271 * Note: strides are in bytes.
273 * Only defined for depth formats.
276 (*unpack_z_float
)(float *dst
, unsigned dst_stride
,
277 const uint8_t *src
, unsigned src_stride
,
278 unsigned width
, unsigned height
);
281 * Pack pixels from Z32_FLOAT.
282 * Note: strides are in bytes.
284 * Only defined for depth formats.
287 (*pack_z_float
)(uint8_t *dst
, unsigned dst_stride
,
288 const float *src
, unsigned src_stride
,
289 unsigned width
, unsigned height
);
292 * Unpack pixels to S8_USCALED.
293 * Note: strides are in bytes.
295 * Only defined for stencil formats.
298 (*unpack_s_8uscaled
)(uint8_t *dst
, unsigned dst_stride
,
299 const uint8_t *src
, unsigned src_stride
,
300 unsigned width
, unsigned height
);
303 * Pack pixels from S8_USCALED.
304 * Note: strides are in bytes.
306 * Only defined for stencil formats.
309 (*pack_s_8uscaled
)(uint8_t *dst
, unsigned dst_stride
,
310 const uint8_t *src
, unsigned src_stride
,
311 unsigned width
, unsigned height
);
316 extern const struct util_format_description
317 util_format_description_table
[];
320 const struct util_format_description
*
321 util_format_description(enum pipe_format format
);
325 * Format query functions.
328 static INLINE
const char *
329 util_format_name(enum pipe_format format
)
331 const struct util_format_description
*desc
= util_format_description(format
);
341 static INLINE boolean
342 util_format_is_s3tc(enum pipe_format format
)
344 const struct util_format_description
*desc
= util_format_description(format
);
351 return desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
? TRUE
: FALSE
;
354 static INLINE boolean
355 util_format_is_depth_or_stencil(enum pipe_format format
)
357 const struct util_format_description
*desc
= util_format_description(format
);
364 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
? TRUE
: FALSE
;
367 static INLINE boolean
368 util_format_is_depth_and_stencil(enum pipe_format format
)
370 const struct util_format_description
*desc
= util_format_description(format
);
377 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
) {
381 return (desc
->swizzle
[0] != UTIL_FORMAT_SWIZZLE_NONE
&&
382 desc
->swizzle
[1] != UTIL_FORMAT_SWIZZLE_NONE
) ? TRUE
: FALSE
;
386 * Whether this format is a rgab8 variant.
388 * That is, any format that matches the
390 * PIPE_FORMAT_?8?8?8?8_UNORM
392 static INLINE boolean
393 util_format_is_rgba8_variant(const struct util_format_description
*desc
)
397 if(desc
->block
.width
!= 1 ||
398 desc
->block
.height
!= 1 ||
399 desc
->block
.bits
!= 32)
402 for(chan
= 0; chan
< 4; ++chan
) {
403 if(desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_UNSIGNED
&&
404 desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_VOID
)
406 if(desc
->channel
[chan
].size
!= 8)
415 * Return total bits needed for the pixel format per block.
418 util_format_get_blocksizebits(enum pipe_format format
)
420 const struct util_format_description
*desc
= util_format_description(format
);
427 return desc
->block
.bits
;
431 * Return bytes per block (not pixel) for the given format.
434 util_format_get_blocksize(enum pipe_format format
)
436 uint bits
= util_format_get_blocksizebits(format
);
438 assert(bits
% 8 == 0);
444 util_format_get_blockwidth(enum pipe_format format
)
446 const struct util_format_description
*desc
= util_format_description(format
);
453 return desc
->block
.width
;
457 util_format_get_blockheight(enum pipe_format format
)
459 const struct util_format_description
*desc
= util_format_description(format
);
466 return desc
->block
.height
;
469 static INLINE
unsigned
470 util_format_get_nblocksx(enum pipe_format format
,
473 unsigned blockwidth
= util_format_get_blockwidth(format
);
474 return (x
+ blockwidth
- 1) / blockwidth
;
477 static INLINE
unsigned
478 util_format_get_nblocksy(enum pipe_format format
,
481 unsigned blockheight
= util_format_get_blockheight(format
);
482 return (y
+ blockheight
- 1) / blockheight
;
485 static INLINE
unsigned
486 util_format_get_nblocks(enum pipe_format format
,
490 return util_format_get_nblocksx(format
, width
) * util_format_get_nblocksy(format
, height
);
494 util_format_get_stride(enum pipe_format format
,
497 return util_format_get_nblocksx(format
, width
) * util_format_get_blocksize(format
);
501 util_format_get_2d_size(enum pipe_format format
,
505 return util_format_get_nblocksy(format
, height
) * stride
;
509 util_format_get_component_bits(enum pipe_format format
,
510 enum util_format_colorspace colorspace
,
513 const struct util_format_description
*desc
= util_format_description(format
);
514 enum util_format_colorspace desc_colorspace
;
521 assert(component
< 4);
523 /* Treat RGB and SRGB as equivalent. */
524 if (colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
525 colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
527 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
528 desc_colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
530 desc_colorspace
= desc
->colorspace
;
533 if (desc_colorspace
!= colorspace
) {
537 switch (desc
->swizzle
[component
]) {
538 case UTIL_FORMAT_SWIZZLE_X
:
539 return desc
->channel
[0].size
;
540 case UTIL_FORMAT_SWIZZLE_Y
:
541 return desc
->channel
[1].size
;
542 case UTIL_FORMAT_SWIZZLE_Z
:
543 return desc
->channel
[2].size
;
544 case UTIL_FORMAT_SWIZZLE_W
:
545 return desc
->channel
[3].size
;
551 static INLINE boolean
552 util_format_has_alpha(enum pipe_format format
)
554 const struct util_format_description
*desc
= util_format_description(format
);
561 switch (desc
->colorspace
) {
562 case UTIL_FORMAT_COLORSPACE_RGB
:
563 case UTIL_FORMAT_COLORSPACE_SRGB
:
564 return desc
->swizzle
[3] != UTIL_FORMAT_SWIZZLE_1
;
565 case UTIL_FORMAT_COLORSPACE_YUV
:
567 case UTIL_FORMAT_COLORSPACE_ZS
:
576 * Return the number of components stored.
577 * Formats with block size != 1x1 will always have 1 component (the block).
579 static INLINE
unsigned
580 util_format_get_nr_components(enum pipe_format format
)
582 const struct util_format_description
*desc
= util_format_description(format
);
583 return desc
->nr_channels
;
587 * Format access functions.
591 util_format_read_4f(enum pipe_format format
,
592 float *dst
, unsigned dst_stride
,
593 const void *src
, unsigned src_stride
,
594 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
597 util_format_write_4f(enum pipe_format format
,
598 const float *src
, unsigned src_stride
,
599 void *dst
, unsigned dst_stride
,
600 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
603 util_format_read_4ub(enum pipe_format format
,
604 uint8_t *dst
, unsigned dst_stride
,
605 const void *src
, unsigned src_stride
,
606 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
609 util_format_write_4ub(enum pipe_format format
,
610 const uint8_t *src
, unsigned src_stride
,
611 void *dst
, unsigned dst_stride
,
612 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
615 * Generic format conversion;
619 util_format_translate(enum pipe_format dst_format
,
620 void *dst
, unsigned dst_stride
,
621 unsigned dst_x
, unsigned dst_y
,
622 enum pipe_format src_format
,
623 const void *src
, unsigned src_stride
,
624 unsigned src_x
, unsigned src_y
,
625 unsigned width
, unsigned height
);
631 #endif /* ! U_FORMAT_H */