1 /**************************************************************************
3 * Copyright 2009 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"
36 enum util_format_layout
{
37 UTIL_FORMAT_LAYOUT_SCALAR
= 0,
38 UTIL_FORMAT_LAYOUT_ARITH
= 1,
39 UTIL_FORMAT_LAYOUT_ARRAY
= 2,
40 UTIL_FORMAT_LAYOUT_YUV
= 3,
41 UTIL_FORMAT_LAYOUT_DXT
= 4
45 struct util_format_block
47 /** Block width in pixels */
50 /** Block height in pixels */
53 /** Block size in bits */
58 enum util_format_type
{
59 UTIL_FORMAT_TYPE_VOID
= 0,
60 UTIL_FORMAT_TYPE_UNSIGNED
= 1,
61 UTIL_FORMAT_TYPE_SIGNED
= 2,
62 UTIL_FORMAT_TYPE_FIXED
= 3,
63 UTIL_FORMAT_TYPE_FLOAT
= 4
67 enum util_format_swizzle
{
68 UTIL_FORMAT_SWIZZLE_X
= 0,
69 UTIL_FORMAT_SWIZZLE_Y
= 1,
70 UTIL_FORMAT_SWIZZLE_Z
= 2,
71 UTIL_FORMAT_SWIZZLE_W
= 3,
72 UTIL_FORMAT_SWIZZLE_0
= 4,
73 UTIL_FORMAT_SWIZZLE_1
= 5,
74 UTIL_FORMAT_SWIZZLE_NONE
= 6
78 enum util_format_colorspace
{
79 UTIL_FORMAT_COLORSPACE_RGB
= 0,
80 UTIL_FORMAT_COLORSPACE_SRGB
= 1,
81 UTIL_FORMAT_COLORSPACE_YUV
= 2,
82 UTIL_FORMAT_COLORSPACE_ZS
= 3,
86 struct util_format_channel_description
89 unsigned normalized
:1;
94 struct util_format_description
96 enum pipe_format format
;
98 struct util_format_block block
;
99 enum util_format_layout layout
;
100 struct util_format_channel_description channel
[4];
101 unsigned char swizzle
[4];
102 enum util_format_colorspace colorspace
;
106 extern const struct util_format_description
107 util_format_description_table
[];
110 const struct util_format_description
*
111 util_format_description(enum pipe_format format
);
115 * Format query functions.
118 static INLINE boolean
119 util_format_is_compressed(enum pipe_format format
)
121 const struct util_format_description
*desc
= util_format_description(format
);
128 return desc
->layout
== UTIL_FORMAT_LAYOUT_DXT
? TRUE
: FALSE
;
131 static INLINE boolean
132 util_format_is_depth_or_stencil(enum pipe_format format
)
134 const struct util_format_description
*desc
= util_format_description(format
);
141 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
? TRUE
: FALSE
;
144 static INLINE boolean
145 util_format_is_depth_and_stencil(enum pipe_format format
)
147 const struct util_format_description
*desc
= util_format_description(format
);
154 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
) {
158 return (desc
->swizzle
[0] != UTIL_FORMAT_SWIZZLE_NONE
&&
159 desc
->swizzle
[1] != UTIL_FORMAT_SWIZZLE_NONE
) ? TRUE
: FALSE
;
163 * Describe pixel format's block.
165 * @sa http://msdn2.microsoft.com/en-us/library/ms796147.aspx
168 util_format_get_block(enum pipe_format format
,
169 struct pipe_format_block
*block
)
171 const struct util_format_description
*desc
= util_format_description(format
);
181 block
->size
= desc
->block
.bits
/ 8;
182 block
->width
= desc
->block
.width
;
183 block
->height
= desc
->block
.height
;
187 * Return total bits needed for the pixel format.
190 util_format_get_bits(enum pipe_format format
)
192 const struct util_format_description
*desc
= util_format_description(format
);
199 return desc
->block
.bits
/ (desc
->block
.width
* desc
->block
.height
);
203 * Return bytes per pixel for the given format.
206 util_format_get_size(enum pipe_format format
)
208 uint bits
= util_format_get_bits(format
);
210 assert(bits
% 8 == 0);
216 util_format_get_component_bits(enum pipe_format format
,
217 enum util_format_colorspace colorspace
,
220 const struct util_format_description
*desc
= util_format_description(format
);
221 enum util_format_colorspace desc_colorspace
;
228 assert(component
< 4);
230 /* Treat RGB and SRGB as equivalent. */
231 if (colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
232 colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
234 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
235 desc_colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
237 desc_colorspace
= desc
->colorspace
;
240 if (desc_colorspace
!= colorspace
) {
244 switch (desc
->swizzle
[component
]) {
245 case UTIL_FORMAT_SWIZZLE_X
:
246 return desc
->channel
[0].size
;
247 case UTIL_FORMAT_SWIZZLE_Y
:
248 return desc
->channel
[1].size
;
249 case UTIL_FORMAT_SWIZZLE_Z
:
250 return desc
->channel
[2].size
;
251 case UTIL_FORMAT_SWIZZLE_W
:
252 return desc
->channel
[3].size
;
260 * Format access functions.
264 util_format_read_4f(enum pipe_format format
,
265 float *dst
, unsigned dst_stride
,
266 const void *src
, unsigned src_stride
,
267 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
270 util_format_write_4f(enum pipe_format format
,
271 const float *src
, unsigned src_stride
,
272 void *dst
, unsigned dst_stride
,
273 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
276 util_format_read_4ub(enum pipe_format format
,
277 uint8_t *dst
, unsigned dst_stride
,
278 const void *src
, unsigned src_stride
,
279 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
282 util_format_write_4ub(enum pipe_format format
,
283 const uint8_t *src
, unsigned src_stride
,
284 void *dst
, unsigned dst_stride
,
285 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
287 #endif /* ! U_FORMAT_H */