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"
35 #include "util/u_inline_init.h"
43 * Describe how to pack/unpack pixels into/from the prescribed format.
45 * XXX: This could be renamed to something like util_format_pack, or broke down
46 * in flags inside util_format_block that said exactly what we want.
48 enum util_format_layout
{
50 * Formats with util_format_block::width == util_format_block::height == 1
51 * that can be described as an ordinary data structure.
53 UTIL_FORMAT_LAYOUT_PLAIN
= 0,
56 * Formats with sub-sampled channels.
58 * This is for formats like YV12 where there is less than one sample per
61 UTIL_FORMAT_LAYOUT_SUBSAMPLED
= 3,
64 * S3 Texture Compression formats.
66 UTIL_FORMAT_LAYOUT_S3TC
= 4,
69 * Red-Green Texture Compression formats.
71 UTIL_FORMAT_LAYOUT_RGTC
= 5,
74 * Everything else that doesn't fit in any of the above layouts.
76 UTIL_FORMAT_LAYOUT_OTHER
= 6
80 struct util_format_block
82 /** Block width in pixels */
85 /** Block height in pixels */
88 /** Block size in bits */
93 enum util_format_type
{
94 UTIL_FORMAT_TYPE_VOID
= 0,
95 UTIL_FORMAT_TYPE_UNSIGNED
= 1,
96 UTIL_FORMAT_TYPE_SIGNED
= 2,
97 UTIL_FORMAT_TYPE_FIXED
= 3,
98 UTIL_FORMAT_TYPE_FLOAT
= 4
102 enum util_format_swizzle
{
103 UTIL_FORMAT_SWIZZLE_X
= 0,
104 UTIL_FORMAT_SWIZZLE_Y
= 1,
105 UTIL_FORMAT_SWIZZLE_Z
= 2,
106 UTIL_FORMAT_SWIZZLE_W
= 3,
107 UTIL_FORMAT_SWIZZLE_0
= 4,
108 UTIL_FORMAT_SWIZZLE_1
= 5,
109 UTIL_FORMAT_SWIZZLE_NONE
= 6
113 enum util_format_colorspace
{
114 UTIL_FORMAT_COLORSPACE_RGB
= 0,
115 UTIL_FORMAT_COLORSPACE_SRGB
= 1,
116 UTIL_FORMAT_COLORSPACE_YUV
= 2,
117 UTIL_FORMAT_COLORSPACE_ZS
= 3
121 struct util_format_channel_description
124 unsigned normalized
:1;
129 struct util_format_description
131 enum pipe_format format
;
136 * Short name, striped of the prefix, lower case.
138 const char *short_name
;
141 * Pixel block dimensions.
143 struct util_format_block block
;
145 enum util_format_layout layout
;
148 * The number of channels.
150 unsigned nr_channels
:3;
153 * Whether all channels have the same number of (whole) bytes.
158 * Whether the pixel format can be described as a bitfield structure.
161 * - pixel depth must be 8, 16, or 32 bits;
162 * - all channels must be unsigned, signed, or void
164 unsigned is_bitmask
:1;
167 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
172 * Input channel description.
174 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
176 struct util_format_channel_description channel
[4];
179 * Output channel swizzle.
181 * The order is either:
185 * depending on the colorspace.
187 unsigned char swizzle
[4];
190 * Colorspace transformation.
192 enum util_format_colorspace colorspace
;
195 * Unpack pixel blocks to R8G8B8A8_UNORM.
198 (*unpack_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
199 const uint8_t *src
, unsigned src_stride
,
200 unsigned width
, unsigned height
);
203 * Pack pixel blocks from R8G8B8A8_UNORM.
206 (*pack_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
207 const uint8_t *src
, unsigned src_stride
,
208 unsigned width
, unsigned height
);
211 * Unpack pixel blocks to R32G32B32A32_FLOAT.
214 (*unpack_float
)(float *dst
, unsigned dst_stride
,
215 const uint8_t *src
, unsigned src_stride
,
216 unsigned width
, unsigned height
);
219 * Pack pixel blocks from R32G32B32A32_FLOAT.
222 (*pack_float
)(uint8_t *dst
, unsigned dst_stride
,
223 const float *src
, unsigned src_stride
,
224 unsigned width
, unsigned height
);
227 * Fetch a single pixel (i, j) from a block.
230 (*fetch_float
)(float *dst
,
232 unsigned i
, unsigned j
);
236 extern const struct util_format_description
237 util_format_description_table
[];
240 const struct util_format_description
*
241 util_format_description(enum pipe_format format
);
245 * Format query functions.
248 static INLINE
const char *
249 util_format_name(enum pipe_format format
)
251 const struct util_format_description
*desc
= util_format_description(format
);
261 static INLINE boolean
262 util_format_is_s3tc(enum pipe_format format
)
264 const struct util_format_description
*desc
= util_format_description(format
);
271 return desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
? TRUE
: FALSE
;
274 static INLINE boolean
275 util_format_is_depth_or_stencil(enum pipe_format format
)
277 const struct util_format_description
*desc
= util_format_description(format
);
284 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
? TRUE
: FALSE
;
287 static INLINE boolean
288 util_format_is_depth_and_stencil(enum pipe_format format
)
290 const struct util_format_description
*desc
= util_format_description(format
);
297 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
) {
301 return (desc
->swizzle
[0] != UTIL_FORMAT_SWIZZLE_NONE
&&
302 desc
->swizzle
[1] != UTIL_FORMAT_SWIZZLE_NONE
) ? TRUE
: FALSE
;
306 * Whether this format is a rgab8 variant.
308 * That is, any format that matches the
310 * PIPE_FORMAT_?8?8?8?8_UNORM
312 static INLINE boolean
313 util_format_is_rgba8_variant(const struct util_format_description
*desc
)
317 if(desc
->block
.width
!= 1 ||
318 desc
->block
.height
!= 1 ||
319 desc
->block
.bits
!= 32)
322 for(chan
= 0; chan
< 4; ++chan
) {
323 if(desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_UNSIGNED
&&
324 desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_VOID
)
326 if(desc
->channel
[chan
].size
!= 8)
335 * Return total bits needed for the pixel format per block.
338 util_format_get_blocksizebits(enum pipe_format format
)
340 const struct util_format_description
*desc
= util_format_description(format
);
347 return desc
->block
.bits
;
351 * Return bytes per block (not pixel) for the given format.
354 util_format_get_blocksize(enum pipe_format format
)
356 uint bits
= util_format_get_blocksizebits(format
);
358 assert(bits
% 8 == 0);
364 util_format_get_blockwidth(enum pipe_format format
)
366 const struct util_format_description
*desc
= util_format_description(format
);
373 return desc
->block
.width
;
377 util_format_get_blockheight(enum pipe_format format
)
379 const struct util_format_description
*desc
= util_format_description(format
);
386 return desc
->block
.height
;
389 static INLINE
unsigned
390 util_format_get_nblocksx(enum pipe_format format
,
393 unsigned blockwidth
= util_format_get_blockwidth(format
);
394 return (x
+ blockwidth
- 1) / blockwidth
;
397 static INLINE
unsigned
398 util_format_get_nblocksy(enum pipe_format format
,
401 unsigned blockheight
= util_format_get_blockheight(format
);
402 return (y
+ blockheight
- 1) / blockheight
;
405 static INLINE
unsigned
406 util_format_get_nblocks(enum pipe_format format
,
410 return util_format_get_nblocksx(format
, width
) * util_format_get_nblocksy(format
, height
);
414 util_format_get_stride(enum pipe_format format
,
417 return util_format_get_nblocksx(format
, width
) * util_format_get_blocksize(format
);
421 util_format_get_2d_size(enum pipe_format format
,
425 return util_format_get_nblocksy(format
, height
) * stride
;
429 util_format_get_component_bits(enum pipe_format format
,
430 enum util_format_colorspace colorspace
,
433 const struct util_format_description
*desc
= util_format_description(format
);
434 enum util_format_colorspace desc_colorspace
;
441 assert(component
< 4);
443 /* Treat RGB and SRGB as equivalent. */
444 if (colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
445 colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
447 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
448 desc_colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
450 desc_colorspace
= desc
->colorspace
;
453 if (desc_colorspace
!= colorspace
) {
457 switch (desc
->swizzle
[component
]) {
458 case UTIL_FORMAT_SWIZZLE_X
:
459 return desc
->channel
[0].size
;
460 case UTIL_FORMAT_SWIZZLE_Y
:
461 return desc
->channel
[1].size
;
462 case UTIL_FORMAT_SWIZZLE_Z
:
463 return desc
->channel
[2].size
;
464 case UTIL_FORMAT_SWIZZLE_W
:
465 return desc
->channel
[3].size
;
471 static INLINE boolean
472 util_format_has_alpha(enum pipe_format format
)
474 const struct util_format_description
*desc
= util_format_description(format
);
481 switch (desc
->colorspace
) {
482 case UTIL_FORMAT_COLORSPACE_RGB
:
483 case UTIL_FORMAT_COLORSPACE_SRGB
:
484 return desc
->swizzle
[3] != UTIL_FORMAT_SWIZZLE_1
;
485 case UTIL_FORMAT_COLORSPACE_YUV
:
487 case UTIL_FORMAT_COLORSPACE_ZS
:
496 * Return the number of components stored.
497 * Formats with block size != 1x1 will always have 1 component (the block).
499 static INLINE
unsigned
500 util_format_get_nr_components(enum pipe_format format
)
502 const struct util_format_description
*desc
= util_format_description(format
);
503 return desc
->nr_channels
;
507 * Format access functions.
511 util_format_read_4f(enum pipe_format format
,
512 float *dst
, unsigned dst_stride
,
513 const void *src
, unsigned src_stride
,
514 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
517 util_format_write_4f(enum pipe_format format
,
518 const float *src
, unsigned src_stride
,
519 void *dst
, unsigned dst_stride
,
520 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
523 util_format_read_4ub(enum pipe_format format
,
524 uint8_t *dst
, unsigned dst_stride
,
525 const void *src
, unsigned src_stride
,
526 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
529 util_format_write_4ub(enum pipe_format format
,
530 const uint8_t *src
, unsigned src_stride
,
531 void *dst
, unsigned dst_stride
,
532 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
534 UTIL_INLINE_INIT(util_format
);
540 #endif /* ! U_FORMAT_H */