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"
37 * Describe how to best pack/unpack pixels into/from the prescribed format.
39 * These are used for automatic code generation of pixel packing and unpacking
40 * routines (in compile time, e.g., u_format_access.py, or in runtime, like
43 * Thumb rule is: if you're not code generating pixel packing/unpacking then
44 * these are irrelevant for you.
46 * Note that this can be deduced from other values in util_format_description
47 * structure. This is by design, to make code generation of pixel
48 * packing/unpacking/sampling routines simple and efficient.
50 * XXX: This should be renamed to something like util_format_pack.
52 enum util_format_layout
{
54 * Single scalar component.
56 UTIL_FORMAT_LAYOUT_SCALAR
= 0,
59 * One or more components of mixed integer formats, arithmetically encoded
60 * in a word up to 32bits.
62 UTIL_FORMAT_LAYOUT_ARITH
= 1,
65 * One or more components, no mixed formats, each with equal power of two
68 UTIL_FORMAT_LAYOUT_ARRAY
= 2,
71 * XXX: Not used yet. These might go away and be replaced by a single entry,
72 * for formats where multiple pixels have to be
73 * read in order to determine a single pixel value (i.e., block.width > 1
74 * || block.height > 1)
76 UTIL_FORMAT_LAYOUT_YUV
= 3,
77 UTIL_FORMAT_LAYOUT_DXT
= 4
81 struct util_format_block
83 /** Block width in pixels */
86 /** Block height in pixels */
89 /** Block size in bits */
94 enum util_format_type
{
95 UTIL_FORMAT_TYPE_VOID
= 0,
96 UTIL_FORMAT_TYPE_UNSIGNED
= 1,
97 UTIL_FORMAT_TYPE_SIGNED
= 2,
98 UTIL_FORMAT_TYPE_FIXED
= 3,
99 UTIL_FORMAT_TYPE_FLOAT
= 4
103 enum util_format_swizzle
{
104 UTIL_FORMAT_SWIZZLE_X
= 0,
105 UTIL_FORMAT_SWIZZLE_Y
= 1,
106 UTIL_FORMAT_SWIZZLE_Z
= 2,
107 UTIL_FORMAT_SWIZZLE_W
= 3,
108 UTIL_FORMAT_SWIZZLE_0
= 4,
109 UTIL_FORMAT_SWIZZLE_1
= 5,
110 UTIL_FORMAT_SWIZZLE_NONE
= 6
114 enum util_format_colorspace
{
115 UTIL_FORMAT_COLORSPACE_RGB
= 0,
116 UTIL_FORMAT_COLORSPACE_SRGB
= 1,
117 UTIL_FORMAT_COLORSPACE_YUV
= 2,
118 UTIL_FORMAT_COLORSPACE_ZS
= 3,
122 struct util_format_channel_description
125 unsigned normalized
:1;
130 struct util_format_description
132 enum pipe_format format
;
134 struct util_format_block block
;
135 enum util_format_layout layout
;
136 struct util_format_channel_description channel
[4];
137 unsigned char swizzle
[4];
138 enum util_format_colorspace colorspace
;
142 extern const struct util_format_description
143 util_format_description_table
[];
146 const struct util_format_description
*
147 util_format_description(enum pipe_format format
);
151 * Format query functions.
154 static INLINE boolean
155 util_format_is_compressed(enum pipe_format format
)
157 const struct util_format_description
*desc
= util_format_description(format
);
164 return desc
->layout
== UTIL_FORMAT_LAYOUT_DXT
? TRUE
: FALSE
;
167 static INLINE boolean
168 util_format_is_depth_or_stencil(enum pipe_format format
)
170 const struct util_format_description
*desc
= util_format_description(format
);
177 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
? TRUE
: FALSE
;
180 static INLINE boolean
181 util_format_is_depth_and_stencil(enum pipe_format format
)
183 const struct util_format_description
*desc
= util_format_description(format
);
190 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
) {
194 return (desc
->swizzle
[0] != UTIL_FORMAT_SWIZZLE_NONE
&&
195 desc
->swizzle
[1] != UTIL_FORMAT_SWIZZLE_NONE
) ? TRUE
: FALSE
;
200 * Return total bits needed for the pixel format.
203 util_format_get_blocksizebits(enum pipe_format format
)
205 const struct util_format_description
*desc
= util_format_description(format
);
212 return desc
->block
.bits
/ (desc
->block
.width
* desc
->block
.height
);
216 * Return bytes per pixel for the given format.
219 util_format_get_blocksize(enum pipe_format format
)
221 uint bits
= util_format_get_blocksizebits(format
);
223 assert(bits
% 8 == 0);
229 util_format_get_blockwidth(enum pipe_format format
)
231 const struct util_format_description
*desc
= util_format_description(format
);
238 switch (desc
->layout
) {
239 case UTIL_FORMAT_LAYOUT_YUV
:
241 case UTIL_FORMAT_LAYOUT_DXT
:
249 util_format_get_blockheight(enum pipe_format format
)
251 const struct util_format_description
*desc
= util_format_description(format
);
258 switch (desc
->layout
) {
259 case UTIL_FORMAT_LAYOUT_DXT
:
266 static INLINE
unsigned
267 util_format_get_nblocksx(enum pipe_format format
,
270 unsigned blockwidth
= util_format_get_blockwidth(format
);
271 return (x
+ blockwidth
- 1) / blockwidth
;
274 static INLINE
unsigned
275 util_format_get_nblocksy(enum pipe_format format
,
278 unsigned blockheight
= util_format_get_blockheight(format
);
279 return (y
+ blockheight
- 1) / blockheight
;
282 static INLINE
unsigned
283 util_format_get_nblocks(enum pipe_format format
,
287 return util_format_get_nblocksx(format
, width
) * util_format_get_nblocksy(format
, height
);
291 util_format_get_stride(enum pipe_format format
,
294 return util_format_get_nblocksx(format
, width
) * util_format_get_blocksize(format
);
298 util_format_get_2d_size(enum pipe_format format
,
302 return util_format_get_nblocksy(format
, height
) * stride
;
306 util_format_get_component_bits(enum pipe_format format
,
307 enum util_format_colorspace colorspace
,
310 const struct util_format_description
*desc
= util_format_description(format
);
311 enum util_format_colorspace desc_colorspace
;
318 assert(component
< 4);
320 /* Treat RGB and SRGB as equivalent. */
321 if (colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
322 colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
324 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
325 desc_colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
327 desc_colorspace
= desc
->colorspace
;
330 if (desc_colorspace
!= colorspace
) {
334 switch (desc
->swizzle
[component
]) {
335 case UTIL_FORMAT_SWIZZLE_X
:
336 return desc
->channel
[0].size
;
337 case UTIL_FORMAT_SWIZZLE_Y
:
338 return desc
->channel
[1].size
;
339 case UTIL_FORMAT_SWIZZLE_Z
:
340 return desc
->channel
[2].size
;
341 case UTIL_FORMAT_SWIZZLE_W
:
342 return desc
->channel
[3].size
;
350 * Format access functions.
354 util_format_read_4f(enum pipe_format format
,
355 float *dst
, unsigned dst_stride
,
356 const void *src
, unsigned src_stride
,
357 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
360 util_format_write_4f(enum pipe_format format
,
361 const float *src
, unsigned src_stride
,
362 void *dst
, unsigned dst_stride
,
363 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
366 util_format_read_4ub(enum pipe_format format
,
367 uint8_t *dst
, unsigned dst_stride
,
368 const void *src
, unsigned src_stride
,
369 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
372 util_format_write_4ub(enum pipe_format format
,
373 const uint8_t *src
, unsigned src_stride
,
374 void *dst
, unsigned dst_stride
,
375 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
377 #endif /* ! U_FORMAT_H */