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"
41 * Describe how to best pack/unpack pixels into/from the prescribed format.
43 * These are used for automatic code generation of pixel packing and unpacking
44 * routines (in compile time, e.g., u_format_access.py, or in runtime, like
47 * Thumb rule is: if you're not code generating pixel packing/unpacking then
48 * these are irrelevant for you.
50 * Note that this can be deduced from other values in util_format_description
51 * structure. This is by design, to make code generation of pixel
52 * packing/unpacking/sampling routines simple and efficient.
54 * XXX: This should be renamed to something like util_format_pack.
56 enum util_format_layout
{
58 * Single scalar component.
60 UTIL_FORMAT_LAYOUT_SCALAR
= 0,
63 * One or more components of mixed integer formats, arithmetically encoded
64 * in a word up to 32bits.
66 UTIL_FORMAT_LAYOUT_ARITH
= 1,
69 * One or more components, no mixed formats, each with equal power of two
72 UTIL_FORMAT_LAYOUT_ARRAY
= 2,
75 * XXX: Not used yet. These might go away and be replaced by a single entry,
76 * for formats where multiple pixels have to be
77 * read in order to determine a single pixel value (i.e., block.width > 1
78 * || block.height > 1)
80 UTIL_FORMAT_LAYOUT_YUV
= 3,
81 UTIL_FORMAT_LAYOUT_DXT
= 4
85 struct util_format_block
87 /** Block width in pixels */
90 /** Block height in pixels */
93 /** Block size in bits */
98 enum util_format_type
{
99 UTIL_FORMAT_TYPE_VOID
= 0,
100 UTIL_FORMAT_TYPE_UNSIGNED
= 1,
101 UTIL_FORMAT_TYPE_SIGNED
= 2,
102 UTIL_FORMAT_TYPE_FIXED
= 3,
103 UTIL_FORMAT_TYPE_FLOAT
= 4
107 enum util_format_swizzle
{
108 UTIL_FORMAT_SWIZZLE_X
= 0,
109 UTIL_FORMAT_SWIZZLE_Y
= 1,
110 UTIL_FORMAT_SWIZZLE_Z
= 2,
111 UTIL_FORMAT_SWIZZLE_W
= 3,
112 UTIL_FORMAT_SWIZZLE_0
= 4,
113 UTIL_FORMAT_SWIZZLE_1
= 5,
114 UTIL_FORMAT_SWIZZLE_NONE
= 6
118 enum util_format_colorspace
{
119 UTIL_FORMAT_COLORSPACE_RGB
= 0,
120 UTIL_FORMAT_COLORSPACE_SRGB
= 1,
121 UTIL_FORMAT_COLORSPACE_YUV
= 2,
122 UTIL_FORMAT_COLORSPACE_ZS
= 3,
126 struct util_format_channel_description
129 unsigned normalized
:1;
134 struct util_format_description
136 enum pipe_format format
;
138 struct util_format_block block
;
139 enum util_format_layout layout
;
140 struct util_format_channel_description channel
[4];
141 unsigned char swizzle
[4];
142 enum util_format_colorspace colorspace
;
146 extern const struct util_format_description
147 util_format_description_table
[];
150 const struct util_format_description
*
151 util_format_description(enum pipe_format format
);
155 * Format query functions.
158 static INLINE boolean
159 util_format_is_compressed(enum pipe_format format
)
161 const struct util_format_description
*desc
= util_format_description(format
);
168 return desc
->layout
== UTIL_FORMAT_LAYOUT_DXT
? TRUE
: FALSE
;
171 static INLINE boolean
172 util_format_is_depth_or_stencil(enum pipe_format format
)
174 const struct util_format_description
*desc
= util_format_description(format
);
181 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
? TRUE
: FALSE
;
184 static INLINE boolean
185 util_format_is_depth_and_stencil(enum pipe_format format
)
187 const struct util_format_description
*desc
= util_format_description(format
);
194 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
) {
198 return (desc
->swizzle
[0] != UTIL_FORMAT_SWIZZLE_NONE
&&
199 desc
->swizzle
[1] != UTIL_FORMAT_SWIZZLE_NONE
) ? TRUE
: FALSE
;
204 * Return total bits needed for the pixel format per block.
207 util_format_get_blocksizebits(enum pipe_format format
)
209 const struct util_format_description
*desc
= util_format_description(format
);
216 return desc
->block
.bits
;
220 * Return bytes per block (not pixel) for the given format.
223 util_format_get_blocksize(enum pipe_format format
)
225 uint bits
= util_format_get_blocksizebits(format
);
227 assert(bits
% 8 == 0);
233 util_format_get_blockwidth(enum pipe_format format
)
235 const struct util_format_description
*desc
= util_format_description(format
);
242 switch (desc
->layout
) {
243 case UTIL_FORMAT_LAYOUT_YUV
:
245 case UTIL_FORMAT_LAYOUT_DXT
:
253 util_format_get_blockheight(enum pipe_format format
)
255 const struct util_format_description
*desc
= util_format_description(format
);
262 switch (desc
->layout
) {
263 case UTIL_FORMAT_LAYOUT_DXT
:
270 static INLINE
unsigned
271 util_format_get_nblocksx(enum pipe_format format
,
274 unsigned blockwidth
= util_format_get_blockwidth(format
);
275 return (x
+ blockwidth
- 1) / blockwidth
;
278 static INLINE
unsigned
279 util_format_get_nblocksy(enum pipe_format format
,
282 unsigned blockheight
= util_format_get_blockheight(format
);
283 return (y
+ blockheight
- 1) / blockheight
;
286 static INLINE
unsigned
287 util_format_get_nblocks(enum pipe_format format
,
291 return util_format_get_nblocksx(format
, width
) * util_format_get_nblocksy(format
, height
);
295 util_format_get_stride(enum pipe_format format
,
298 return util_format_get_nblocksx(format
, width
) * util_format_get_blocksize(format
);
302 util_format_get_2d_size(enum pipe_format format
,
306 return util_format_get_nblocksy(format
, height
) * stride
;
310 util_format_get_component_bits(enum pipe_format format
,
311 enum util_format_colorspace colorspace
,
314 const struct util_format_description
*desc
= util_format_description(format
);
315 enum util_format_colorspace desc_colorspace
;
322 assert(component
< 4);
324 /* Treat RGB and SRGB as equivalent. */
325 if (colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
326 colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
328 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
329 desc_colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
331 desc_colorspace
= desc
->colorspace
;
334 if (desc_colorspace
!= colorspace
) {
338 switch (desc
->swizzle
[component
]) {
339 case UTIL_FORMAT_SWIZZLE_X
:
340 return desc
->channel
[0].size
;
341 case UTIL_FORMAT_SWIZZLE_Y
:
342 return desc
->channel
[1].size
;
343 case UTIL_FORMAT_SWIZZLE_Z
:
344 return desc
->channel
[2].size
;
345 case UTIL_FORMAT_SWIZZLE_W
:
346 return desc
->channel
[3].size
;
352 static INLINE boolean
353 util_format_has_alpha(enum pipe_format format
)
355 const struct util_format_description
*desc
= util_format_description(format
);
362 switch (desc
->layout
) {
363 case UTIL_FORMAT_LAYOUT_SCALAR
:
364 case UTIL_FORMAT_LAYOUT_ARITH
:
365 case UTIL_FORMAT_LAYOUT_ARRAY
:
366 /* FIXME: pf_get_component_bits( PIPE_FORMAT_A8L8_UNORM, PIPE_FORMAT_COMP_A ) should not return 0 right? */
367 if (format
== PIPE_FORMAT_A8_UNORM
||
368 format
== PIPE_FORMAT_A8L8_UNORM
||
369 format
== PIPE_FORMAT_A8L8_SRGB
) {
372 return util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_RGB
, 3) != 0;
373 case UTIL_FORMAT_LAYOUT_YUV
:
375 case UTIL_FORMAT_LAYOUT_DXT
:
377 case PIPE_FORMAT_DXT1_RGBA
:
378 case PIPE_FORMAT_DXT3_RGBA
:
379 case PIPE_FORMAT_DXT5_RGBA
:
380 case PIPE_FORMAT_DXT1_SRGBA
:
381 case PIPE_FORMAT_DXT3_SRGBA
:
382 case PIPE_FORMAT_DXT5_SRGBA
:
395 * Format access functions.
399 util_format_read_4f(enum pipe_format format
,
400 float *dst
, unsigned dst_stride
,
401 const void *src
, unsigned src_stride
,
402 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
405 util_format_write_4f(enum pipe_format format
,
406 const float *src
, unsigned src_stride
,
407 void *dst
, unsigned dst_stride
,
408 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
411 util_format_read_4ub(enum pipe_format format
,
412 uint8_t *dst
, unsigned dst_stride
,
413 const void *src
, unsigned src_stride
,
414 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
417 util_format_write_4ub(enum pipe_format format
,
418 const uint8_t *src
, unsigned src_stride
,
419 void *dst
, unsigned dst_stride
,
420 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
426 #endif /* ! U_FORMAT_H */