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
;
126 * Pixel block dimensions.
128 struct util_format_block block
;
130 enum util_format_layout layout
;
133 * The number of channels.
135 unsigned nr_channels
:3;
138 * Whether all channels have the same number of (whole) bytes.
143 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
148 * Input channel description.
150 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
152 struct util_format_channel_description channel
[4];
155 * Output channel swizzle.
157 * The order is either:
161 * depending on the colorspace.
163 unsigned char swizzle
[4];
166 * Colorspace transformation.
168 enum util_format_colorspace colorspace
;
172 extern const struct util_format_description
173 util_format_description_table
[];
176 const struct util_format_description
*
177 util_format_description(enum pipe_format format
);
181 * Format query functions.
184 static INLINE
const char *
185 util_format_name(enum pipe_format format
)
187 const struct util_format_description
*desc
= util_format_description(format
);
197 static INLINE boolean
198 util_format_is_compressed(enum pipe_format format
)
200 const struct util_format_description
*desc
= util_format_description(format
);
207 return desc
->layout
== UTIL_FORMAT_LAYOUT_COMPRESSED
? TRUE
: FALSE
;
210 static INLINE boolean
211 util_format_is_depth_or_stencil(enum pipe_format format
)
213 const struct util_format_description
*desc
= util_format_description(format
);
220 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
? TRUE
: FALSE
;
223 static INLINE boolean
224 util_format_is_depth_and_stencil(enum pipe_format format
)
226 const struct util_format_description
*desc
= util_format_description(format
);
233 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
) {
237 return (desc
->swizzle
[0] != UTIL_FORMAT_SWIZZLE_NONE
&&
238 desc
->swizzle
[1] != UTIL_FORMAT_SWIZZLE_NONE
) ? TRUE
: FALSE
;
243 * Return total bits needed for the pixel format per block.
246 util_format_get_blocksizebits(enum pipe_format format
)
248 const struct util_format_description
*desc
= util_format_description(format
);
255 return desc
->block
.bits
;
259 * Return bytes per block (not pixel) for the given format.
262 util_format_get_blocksize(enum pipe_format format
)
264 uint bits
= util_format_get_blocksizebits(format
);
266 assert(bits
% 8 == 0);
272 util_format_get_blockwidth(enum pipe_format format
)
274 const struct util_format_description
*desc
= util_format_description(format
);
281 return desc
->block
.width
;
285 util_format_get_blockheight(enum pipe_format format
)
287 const struct util_format_description
*desc
= util_format_description(format
);
294 return desc
->block
.height
;
297 static INLINE
unsigned
298 util_format_get_nblocksx(enum pipe_format format
,
301 unsigned blockwidth
= util_format_get_blockwidth(format
);
302 return (x
+ blockwidth
- 1) / blockwidth
;
305 static INLINE
unsigned
306 util_format_get_nblocksy(enum pipe_format format
,
309 unsigned blockheight
= util_format_get_blockheight(format
);
310 return (y
+ blockheight
- 1) / blockheight
;
313 static INLINE
unsigned
314 util_format_get_nblocks(enum pipe_format format
,
318 return util_format_get_nblocksx(format
, width
) * util_format_get_nblocksy(format
, height
);
322 util_format_get_stride(enum pipe_format format
,
325 return util_format_get_nblocksx(format
, width
) * util_format_get_blocksize(format
);
329 util_format_get_2d_size(enum pipe_format format
,
333 return util_format_get_nblocksy(format
, height
) * stride
;
337 util_format_get_component_bits(enum pipe_format format
,
338 enum util_format_colorspace colorspace
,
341 const struct util_format_description
*desc
= util_format_description(format
);
342 enum util_format_colorspace desc_colorspace
;
349 assert(component
< 4);
351 /* Treat RGB and SRGB as equivalent. */
352 if (colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
353 colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
355 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
356 desc_colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
358 desc_colorspace
= desc
->colorspace
;
361 if (desc_colorspace
!= colorspace
) {
365 switch (desc
->swizzle
[component
]) {
366 case UTIL_FORMAT_SWIZZLE_X
:
367 return desc
->channel
[0].size
;
368 case UTIL_FORMAT_SWIZZLE_Y
:
369 return desc
->channel
[1].size
;
370 case UTIL_FORMAT_SWIZZLE_Z
:
371 return desc
->channel
[2].size
;
372 case UTIL_FORMAT_SWIZZLE_W
:
373 return desc
->channel
[3].size
;
379 static INLINE boolean
380 util_format_has_alpha(enum pipe_format format
)
382 const struct util_format_description
*desc
= util_format_description(format
);
389 switch (desc
->colorspace
) {
390 case UTIL_FORMAT_COLORSPACE_RGB
:
391 case UTIL_FORMAT_COLORSPACE_SRGB
:
392 return desc
->swizzle
[3] != UTIL_FORMAT_SWIZZLE_1
;
393 case UTIL_FORMAT_COLORSPACE_YUV
:
395 case UTIL_FORMAT_COLORSPACE_ZS
:
405 * Format access functions.
409 util_format_read_4f(enum pipe_format format
,
410 float *dst
, unsigned dst_stride
,
411 const void *src
, unsigned src_stride
,
412 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
415 util_format_write_4f(enum pipe_format format
,
416 const float *src
, unsigned src_stride
,
417 void *dst
, unsigned dst_stride
,
418 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
421 util_format_read_4ub(enum pipe_format format
,
422 uint8_t *dst
, unsigned dst_stride
,
423 const void *src
, unsigned src_stride
,
424 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
427 util_format_write_4ub(enum pipe_format format
,
428 const uint8_t *src
, unsigned src_stride
,
429 void *dst
, unsigned dst_stride
,
430 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
436 #endif /* ! U_FORMAT_H */