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 * XXX: This could actually b
62 UTIL_FORMAT_LAYOUT_SUBSAMPLED
= 3,
65 * An unspecified compression algorithm.
67 UTIL_FORMAT_LAYOUT_COMPRESSED
= 4
71 struct util_format_block
73 /** Block width in pixels */
76 /** Block height in pixels */
79 /** Block size in bits */
84 enum util_format_type
{
85 UTIL_FORMAT_TYPE_VOID
= 0,
86 UTIL_FORMAT_TYPE_UNSIGNED
= 1,
87 UTIL_FORMAT_TYPE_SIGNED
= 2,
88 UTIL_FORMAT_TYPE_FIXED
= 3,
89 UTIL_FORMAT_TYPE_FLOAT
= 4
93 enum util_format_swizzle
{
94 UTIL_FORMAT_SWIZZLE_X
= 0,
95 UTIL_FORMAT_SWIZZLE_Y
= 1,
96 UTIL_FORMAT_SWIZZLE_Z
= 2,
97 UTIL_FORMAT_SWIZZLE_W
= 3,
98 UTIL_FORMAT_SWIZZLE_0
= 4,
99 UTIL_FORMAT_SWIZZLE_1
= 5,
100 UTIL_FORMAT_SWIZZLE_NONE
= 6
104 enum util_format_colorspace
{
105 UTIL_FORMAT_COLORSPACE_RGB
= 0,
106 UTIL_FORMAT_COLORSPACE_SRGB
= 1,
107 UTIL_FORMAT_COLORSPACE_YUV
= 2,
108 UTIL_FORMAT_COLORSPACE_ZS
= 3
112 struct util_format_channel_description
115 unsigned normalized
:1;
120 struct util_format_description
122 enum pipe_format format
;
127 * Short name, striped of the prefix, lower case.
129 const char *short_name
;
132 * Pixel block dimensions.
134 struct util_format_block block
;
136 enum util_format_layout layout
;
139 * The number of channels.
141 unsigned nr_channels
:3;
144 * Whether all channels have the same number of (whole) bytes.
149 * Whether the pixel format can be described as a bitfield structure.
152 * - pixel depth must be 8, 16, or 32 bits;
153 * - all channels must be unsigned, signed, or void
155 unsigned is_bitmask
:1;
158 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
163 * Input channel description.
165 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
167 struct util_format_channel_description channel
[4];
170 * Output channel swizzle.
172 * The order is either:
176 * depending on the colorspace.
178 unsigned char swizzle
[4];
181 * Colorspace transformation.
183 enum util_format_colorspace colorspace
;
187 extern const struct util_format_description
188 util_format_description_table
[];
191 const struct util_format_description
*
192 util_format_description(enum pipe_format format
);
196 * Format query functions.
199 static INLINE
const char *
200 util_format_name(enum pipe_format format
)
202 const struct util_format_description
*desc
= util_format_description(format
);
212 static INLINE boolean
213 util_format_is_compressed(enum pipe_format format
)
215 const struct util_format_description
*desc
= util_format_description(format
);
222 return desc
->layout
== UTIL_FORMAT_LAYOUT_COMPRESSED
? TRUE
: FALSE
;
225 static INLINE boolean
226 util_format_is_depth_or_stencil(enum pipe_format format
)
228 const struct util_format_description
*desc
= util_format_description(format
);
235 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
? TRUE
: FALSE
;
238 static INLINE boolean
239 util_format_is_depth_and_stencil(enum pipe_format format
)
241 const struct util_format_description
*desc
= util_format_description(format
);
248 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
) {
252 return (desc
->swizzle
[0] != UTIL_FORMAT_SWIZZLE_NONE
&&
253 desc
->swizzle
[1] != UTIL_FORMAT_SWIZZLE_NONE
) ? TRUE
: FALSE
;
258 * Return total bits needed for the pixel format per block.
261 util_format_get_blocksizebits(enum pipe_format format
)
263 const struct util_format_description
*desc
= util_format_description(format
);
270 return desc
->block
.bits
;
274 * Return bytes per block (not pixel) for the given format.
277 util_format_get_blocksize(enum pipe_format format
)
279 uint bits
= util_format_get_blocksizebits(format
);
281 assert(bits
% 8 == 0);
287 util_format_get_blockwidth(enum pipe_format format
)
289 const struct util_format_description
*desc
= util_format_description(format
);
296 return desc
->block
.width
;
300 util_format_get_blockheight(enum pipe_format format
)
302 const struct util_format_description
*desc
= util_format_description(format
);
309 return desc
->block
.height
;
312 static INLINE
unsigned
313 util_format_get_nblocksx(enum pipe_format format
,
316 unsigned blockwidth
= util_format_get_blockwidth(format
);
317 return (x
+ blockwidth
- 1) / blockwidth
;
320 static INLINE
unsigned
321 util_format_get_nblocksy(enum pipe_format format
,
324 unsigned blockheight
= util_format_get_blockheight(format
);
325 return (y
+ blockheight
- 1) / blockheight
;
328 static INLINE
unsigned
329 util_format_get_nblocks(enum pipe_format format
,
333 return util_format_get_nblocksx(format
, width
) * util_format_get_nblocksy(format
, height
);
337 util_format_get_stride(enum pipe_format format
,
340 return util_format_get_nblocksx(format
, width
) * util_format_get_blocksize(format
);
344 util_format_get_2d_size(enum pipe_format format
,
348 return util_format_get_nblocksy(format
, height
) * stride
;
352 util_format_get_component_bits(enum pipe_format format
,
353 enum util_format_colorspace colorspace
,
356 const struct util_format_description
*desc
= util_format_description(format
);
357 enum util_format_colorspace desc_colorspace
;
364 assert(component
< 4);
366 /* Treat RGB and SRGB as equivalent. */
367 if (colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
368 colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
370 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
371 desc_colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
373 desc_colorspace
= desc
->colorspace
;
376 if (desc_colorspace
!= colorspace
) {
380 switch (desc
->swizzle
[component
]) {
381 case UTIL_FORMAT_SWIZZLE_X
:
382 return desc
->channel
[0].size
;
383 case UTIL_FORMAT_SWIZZLE_Y
:
384 return desc
->channel
[1].size
;
385 case UTIL_FORMAT_SWIZZLE_Z
:
386 return desc
->channel
[2].size
;
387 case UTIL_FORMAT_SWIZZLE_W
:
388 return desc
->channel
[3].size
;
394 static INLINE boolean
395 util_format_has_alpha(enum pipe_format format
)
397 const struct util_format_description
*desc
= util_format_description(format
);
404 switch (desc
->colorspace
) {
405 case UTIL_FORMAT_COLORSPACE_RGB
:
406 case UTIL_FORMAT_COLORSPACE_SRGB
:
407 return desc
->swizzle
[3] != UTIL_FORMAT_SWIZZLE_1
;
408 case UTIL_FORMAT_COLORSPACE_YUV
:
410 case UTIL_FORMAT_COLORSPACE_ZS
:
419 * Return the number of components stored.
420 * Formats with block size != 1x1 will always have 1 component (the block).
422 static INLINE
unsigned
423 util_format_get_nr_components(enum pipe_format format
)
425 const struct util_format_description
*desc
= util_format_description(format
);
426 return desc
->nr_channels
;
430 * Format access functions.
434 util_format_read_4f(enum pipe_format format
,
435 float *dst
, unsigned dst_stride
,
436 const void *src
, unsigned src_stride
,
437 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
440 util_format_write_4f(enum pipe_format format
,
441 const float *src
, unsigned src_stride
,
442 void *dst
, unsigned dst_stride
,
443 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
446 util_format_read_4ub(enum pipe_format format
,
447 uint8_t *dst
, unsigned dst_stride
,
448 const void *src
, unsigned src_stride
,
449 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
452 util_format_write_4ub(enum pipe_format format
,
453 const uint8_t *src
, unsigned src_stride
,
454 void *dst
, unsigned dst_stride
,
455 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
461 #endif /* ! U_FORMAT_H */