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.
196 * Only defined for non-depth-stencil formats.
199 (*unpack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
200 const uint8_t *src
, unsigned src_stride
,
201 unsigned width
, unsigned height
);
204 * Pack pixel blocks from R8G8B8A8_UNORM.
206 * Only defined for non-depth-stencil formats.
209 (*pack_rgba_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
210 const uint8_t *src
, unsigned src_stride
,
211 unsigned width
, unsigned height
);
214 * Unpack pixel blocks to R32G32B32A32_FLOAT.
216 * Only defined for non-depth-stencil formats.
219 (*unpack_rgba_float
)(float *dst
, unsigned dst_stride
,
220 const uint8_t *src
, unsigned src_stride
,
221 unsigned width
, unsigned height
);
224 * Pack pixel blocks from R32G32B32A32_FLOAT.
226 * Only defined for non-depth-stencil formats.
229 (*pack_rgba_float
)(uint8_t *dst
, unsigned dst_stride
,
230 const float *src
, unsigned src_stride
,
231 unsigned width
, unsigned height
);
234 * Fetch a single pixel (i, j) from a block.
236 * Only defined for non-depth-stencil formats.
239 (*fetch_rgba_float
)(float *dst
,
241 unsigned i
, unsigned j
);
244 * Unpack pixels to Z32_UNORM.
246 * Only defined for depth formats.
249 (*unpack_z_32unorm
)(uint32_t *dst
, unsigned dst_stride
,
250 const uint8_t *src
, unsigned src_stride
,
251 unsigned width
, unsigned height
);
254 * Pack pixels from Z32_FLOAT.
256 * Only defined for depth formats.
259 (*pack_z_32unorm
)(uint8_t *dst
, unsigned dst_stride
,
260 const uint32_t *src
, unsigned src_stride
,
261 unsigned width
, unsigned height
);
264 * Unpack pixels to Z32_FLOAT.
266 * Only defined for depth formats.
269 (*unpack_z_float
)(float *dst
, unsigned dst_stride
,
270 const uint8_t *src
, unsigned src_stride
,
271 unsigned width
, unsigned height
);
274 * Pack pixels from Z32_FLOAT.
276 * Only defined for depth formats.
279 (*pack_z_float
)(uint8_t *dst
, unsigned dst_stride
,
280 const float *src
, unsigned src_stride
,
281 unsigned width
, unsigned height
);
284 * Unpack pixels to S8_USCALED.
286 * Only defined for stencil formats.
289 (*unpack_s_8uscaled
)(uint8_t *dst
, unsigned dst_stride
,
290 const uint8_t *src
, unsigned src_stride
,
291 unsigned width
, unsigned height
);
294 * Pack pixels from S8_USCALED.
296 * Only defined for stencil formats.
299 (*pack_s_8uscaled
)(uint8_t *dst
, unsigned dst_stride
,
300 const uint8_t *src
, unsigned src_stride
,
301 unsigned width
, unsigned height
);
306 extern const struct util_format_description
307 util_format_description_table
[];
310 const struct util_format_description
*
311 util_format_description(enum pipe_format format
);
315 * Format query functions.
318 static INLINE
const char *
319 util_format_name(enum pipe_format format
)
321 const struct util_format_description
*desc
= util_format_description(format
);
331 static INLINE boolean
332 util_format_is_s3tc(enum pipe_format format
)
334 const struct util_format_description
*desc
= util_format_description(format
);
341 return desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
? TRUE
: FALSE
;
344 static INLINE boolean
345 util_format_is_depth_or_stencil(enum pipe_format format
)
347 const struct util_format_description
*desc
= util_format_description(format
);
354 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
? TRUE
: FALSE
;
357 static INLINE boolean
358 util_format_is_depth_and_stencil(enum pipe_format format
)
360 const struct util_format_description
*desc
= util_format_description(format
);
367 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
) {
371 return (desc
->swizzle
[0] != UTIL_FORMAT_SWIZZLE_NONE
&&
372 desc
->swizzle
[1] != UTIL_FORMAT_SWIZZLE_NONE
) ? TRUE
: FALSE
;
376 * Whether this format is a rgab8 variant.
378 * That is, any format that matches the
380 * PIPE_FORMAT_?8?8?8?8_UNORM
382 static INLINE boolean
383 util_format_is_rgba8_variant(const struct util_format_description
*desc
)
387 if(desc
->block
.width
!= 1 ||
388 desc
->block
.height
!= 1 ||
389 desc
->block
.bits
!= 32)
392 for(chan
= 0; chan
< 4; ++chan
) {
393 if(desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_UNSIGNED
&&
394 desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_VOID
)
396 if(desc
->channel
[chan
].size
!= 8)
405 * Return total bits needed for the pixel format per block.
408 util_format_get_blocksizebits(enum pipe_format format
)
410 const struct util_format_description
*desc
= util_format_description(format
);
417 return desc
->block
.bits
;
421 * Return bytes per block (not pixel) for the given format.
424 util_format_get_blocksize(enum pipe_format format
)
426 uint bits
= util_format_get_blocksizebits(format
);
428 assert(bits
% 8 == 0);
434 util_format_get_blockwidth(enum pipe_format format
)
436 const struct util_format_description
*desc
= util_format_description(format
);
443 return desc
->block
.width
;
447 util_format_get_blockheight(enum pipe_format format
)
449 const struct util_format_description
*desc
= util_format_description(format
);
456 return desc
->block
.height
;
459 static INLINE
unsigned
460 util_format_get_nblocksx(enum pipe_format format
,
463 unsigned blockwidth
= util_format_get_blockwidth(format
);
464 return (x
+ blockwidth
- 1) / blockwidth
;
467 static INLINE
unsigned
468 util_format_get_nblocksy(enum pipe_format format
,
471 unsigned blockheight
= util_format_get_blockheight(format
);
472 return (y
+ blockheight
- 1) / blockheight
;
475 static INLINE
unsigned
476 util_format_get_nblocks(enum pipe_format format
,
480 return util_format_get_nblocksx(format
, width
) * util_format_get_nblocksy(format
, height
);
484 util_format_get_stride(enum pipe_format format
,
487 return util_format_get_nblocksx(format
, width
) * util_format_get_blocksize(format
);
491 util_format_get_2d_size(enum pipe_format format
,
495 return util_format_get_nblocksy(format
, height
) * stride
;
499 util_format_get_component_bits(enum pipe_format format
,
500 enum util_format_colorspace colorspace
,
503 const struct util_format_description
*desc
= util_format_description(format
);
504 enum util_format_colorspace desc_colorspace
;
511 assert(component
< 4);
513 /* Treat RGB and SRGB as equivalent. */
514 if (colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
515 colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
517 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
518 desc_colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
520 desc_colorspace
= desc
->colorspace
;
523 if (desc_colorspace
!= colorspace
) {
527 switch (desc
->swizzle
[component
]) {
528 case UTIL_FORMAT_SWIZZLE_X
:
529 return desc
->channel
[0].size
;
530 case UTIL_FORMAT_SWIZZLE_Y
:
531 return desc
->channel
[1].size
;
532 case UTIL_FORMAT_SWIZZLE_Z
:
533 return desc
->channel
[2].size
;
534 case UTIL_FORMAT_SWIZZLE_W
:
535 return desc
->channel
[3].size
;
541 static INLINE boolean
542 util_format_has_alpha(enum pipe_format format
)
544 const struct util_format_description
*desc
= util_format_description(format
);
551 switch (desc
->colorspace
) {
552 case UTIL_FORMAT_COLORSPACE_RGB
:
553 case UTIL_FORMAT_COLORSPACE_SRGB
:
554 return desc
->swizzle
[3] != UTIL_FORMAT_SWIZZLE_1
;
555 case UTIL_FORMAT_COLORSPACE_YUV
:
557 case UTIL_FORMAT_COLORSPACE_ZS
:
566 * Return the number of components stored.
567 * Formats with block size != 1x1 will always have 1 component (the block).
569 static INLINE
unsigned
570 util_format_get_nr_components(enum pipe_format format
)
572 const struct util_format_description
*desc
= util_format_description(format
);
573 return desc
->nr_channels
;
577 * Format access functions.
581 util_format_read_4f(enum pipe_format format
,
582 float *dst
, unsigned dst_stride
,
583 const void *src
, unsigned src_stride
,
584 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
587 util_format_write_4f(enum pipe_format format
,
588 const float *src
, unsigned src_stride
,
589 void *dst
, unsigned dst_stride
,
590 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
593 util_format_read_4ub(enum pipe_format format
,
594 uint8_t *dst
, unsigned dst_stride
,
595 const void *src
, unsigned src_stride
,
596 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
599 util_format_write_4ub(enum pipe_format format
,
600 const uint8_t *src
, unsigned src_stride
,
601 void *dst
, unsigned dst_stride
,
602 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
605 * Generic format conversion;
609 util_format_translate(enum pipe_format dst_format
,
610 void *dst
, unsigned dst_stride
,
611 unsigned dst_x
, unsigned dst_y
,
612 enum pipe_format src_format
,
613 const void *src
, unsigned src_stride
,
614 unsigned src_x
, unsigned src_y
,
615 unsigned width
, unsigned height
);
621 #endif /* ! U_FORMAT_H */