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"
34 #include "util/u_debug.h"
42 * Describe how to best pack/unpack pixels into/from the prescribed format.
44 * These are used for automatic code generation of pixel packing and unpacking
45 * routines (in compile time, e.g., u_format_access.py, or in runtime, like
48 * Thumb rule is: if you're not code generating pixel packing/unpacking then
49 * these are irrelevant for you.
51 * Note that this can be deduced from other values in util_format_description
52 * structure. This is by design, to make code generation of pixel
53 * packing/unpacking/sampling routines simple and efficient.
55 * XXX: This should be renamed to something like util_format_pack.
57 enum util_format_layout
{
59 * Single scalar component.
61 UTIL_FORMAT_LAYOUT_SCALAR
= 0,
64 * One or more components of mixed integer formats, arithmetically encoded
65 * in a word up to 32bits.
67 UTIL_FORMAT_LAYOUT_ARITH
= 1,
70 * One or more components, no mixed formats, each with equal power of two
73 UTIL_FORMAT_LAYOUT_ARRAY
= 2,
76 * XXX: Not used yet. These might go away and be replaced by a single entry,
77 * for formats where multiple pixels have to be
78 * read in order to determine a single pixel value (i.e., block.width > 1
79 * || block.height > 1)
81 UTIL_FORMAT_LAYOUT_YUV
= 3,
82 UTIL_FORMAT_LAYOUT_DXT
= 4
86 struct util_format_block
88 /** Block width in pixels */
91 /** Block height in pixels */
94 /** Block size in bits */
99 enum util_format_type
{
100 UTIL_FORMAT_TYPE_VOID
= 0,
101 UTIL_FORMAT_TYPE_UNSIGNED
= 1,
102 UTIL_FORMAT_TYPE_SIGNED
= 2,
103 UTIL_FORMAT_TYPE_FIXED
= 3,
104 UTIL_FORMAT_TYPE_FLOAT
= 4
108 enum util_format_swizzle
{
109 UTIL_FORMAT_SWIZZLE_X
= 0,
110 UTIL_FORMAT_SWIZZLE_Y
= 1,
111 UTIL_FORMAT_SWIZZLE_Z
= 2,
112 UTIL_FORMAT_SWIZZLE_W
= 3,
113 UTIL_FORMAT_SWIZZLE_0
= 4,
114 UTIL_FORMAT_SWIZZLE_1
= 5,
115 UTIL_FORMAT_SWIZZLE_NONE
= 6
119 enum util_format_colorspace
{
120 UTIL_FORMAT_COLORSPACE_RGB
= 0,
121 UTIL_FORMAT_COLORSPACE_SRGB
= 1,
122 UTIL_FORMAT_COLORSPACE_YUV
= 2,
123 UTIL_FORMAT_COLORSPACE_ZS
= 3
127 struct util_format_channel_description
130 unsigned normalized
:1;
135 struct util_format_description
137 enum pipe_format format
;
139 struct util_format_block block
;
140 enum util_format_layout layout
;
143 * The number of channels.
145 unsigned nr_channels
:3;
148 * Whether all channels have the same number of whole bytes.
153 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
157 struct util_format_channel_description channel
[4];
159 unsigned char swizzle
[4];
161 enum util_format_colorspace colorspace
;
165 extern const struct util_format_description
166 util_format_description_table
[];
169 const struct util_format_description
*
170 util_format_description(enum pipe_format format
);
174 * Format query functions.
177 static INLINE
const char *
178 util_format_name(enum pipe_format format
)
180 const struct util_format_description
*desc
= util_format_description(format
);
190 static INLINE boolean
191 util_format_is_compressed(enum pipe_format format
)
193 const struct util_format_description
*desc
= util_format_description(format
);
200 return desc
->layout
== UTIL_FORMAT_LAYOUT_DXT
? TRUE
: FALSE
;
203 static INLINE boolean
204 util_format_is_depth_or_stencil(enum pipe_format format
)
206 const struct util_format_description
*desc
= util_format_description(format
);
213 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
? TRUE
: FALSE
;
216 static INLINE boolean
217 util_format_is_depth_and_stencil(enum pipe_format format
)
219 const struct util_format_description
*desc
= util_format_description(format
);
226 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
) {
230 return (desc
->swizzle
[0] != UTIL_FORMAT_SWIZZLE_NONE
&&
231 desc
->swizzle
[1] != UTIL_FORMAT_SWIZZLE_NONE
) ? TRUE
: FALSE
;
236 * Return total bits needed for the pixel format per block.
239 util_format_get_blocksizebits(enum pipe_format format
)
241 const struct util_format_description
*desc
= util_format_description(format
);
248 return desc
->block
.bits
;
252 * Return bytes per block (not pixel) for the given format.
255 util_format_get_blocksize(enum pipe_format format
)
257 uint bits
= util_format_get_blocksizebits(format
);
259 assert(bits
% 8 == 0);
265 util_format_get_blockwidth(enum pipe_format format
)
267 const struct util_format_description
*desc
= util_format_description(format
);
274 switch (desc
->layout
) {
275 case UTIL_FORMAT_LAYOUT_YUV
:
277 case UTIL_FORMAT_LAYOUT_DXT
:
285 util_format_get_blockheight(enum pipe_format format
)
287 const struct util_format_description
*desc
= util_format_description(format
);
294 switch (desc
->layout
) {
295 case UTIL_FORMAT_LAYOUT_DXT
:
302 static INLINE
unsigned
303 util_format_get_nblocksx(enum pipe_format format
,
306 unsigned blockwidth
= util_format_get_blockwidth(format
);
307 return (x
+ blockwidth
- 1) / blockwidth
;
310 static INLINE
unsigned
311 util_format_get_nblocksy(enum pipe_format format
,
314 unsigned blockheight
= util_format_get_blockheight(format
);
315 return (y
+ blockheight
- 1) / blockheight
;
318 static INLINE
unsigned
319 util_format_get_nblocks(enum pipe_format format
,
323 return util_format_get_nblocksx(format
, width
) * util_format_get_nblocksy(format
, height
);
327 util_format_get_stride(enum pipe_format format
,
330 return util_format_get_nblocksx(format
, width
) * util_format_get_blocksize(format
);
334 util_format_get_2d_size(enum pipe_format format
,
338 return util_format_get_nblocksy(format
, height
) * stride
;
342 util_format_get_component_bits(enum pipe_format format
,
343 enum util_format_colorspace colorspace
,
346 const struct util_format_description
*desc
= util_format_description(format
);
347 enum util_format_colorspace desc_colorspace
;
354 assert(component
< 4);
356 /* Treat RGB and SRGB as equivalent. */
357 if (colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
358 colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
360 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
361 desc_colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
363 desc_colorspace
= desc
->colorspace
;
366 if (desc_colorspace
!= colorspace
) {
370 switch (desc
->swizzle
[component
]) {
371 case UTIL_FORMAT_SWIZZLE_X
:
372 return desc
->channel
[0].size
;
373 case UTIL_FORMAT_SWIZZLE_Y
:
374 return desc
->channel
[1].size
;
375 case UTIL_FORMAT_SWIZZLE_Z
:
376 return desc
->channel
[2].size
;
377 case UTIL_FORMAT_SWIZZLE_W
:
378 return desc
->channel
[3].size
;
384 static INLINE boolean
385 util_format_has_alpha(enum pipe_format format
)
387 const struct util_format_description
*desc
= util_format_description(format
);
394 switch (desc
->layout
) {
395 case UTIL_FORMAT_LAYOUT_SCALAR
:
396 case UTIL_FORMAT_LAYOUT_ARITH
:
397 case UTIL_FORMAT_LAYOUT_ARRAY
:
398 /* FIXME: pf_get_component_bits( PIPE_FORMAT_A8L8_UNORM, PIPE_FORMAT_COMP_A ) should not return 0 right? */
399 if (format
== PIPE_FORMAT_A8_UNORM
||
400 format
== PIPE_FORMAT_A8L8_UNORM
||
401 format
== PIPE_FORMAT_A8L8_SRGB
) {
404 return util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_RGB
, 3) != 0;
405 case UTIL_FORMAT_LAYOUT_YUV
:
407 case UTIL_FORMAT_LAYOUT_DXT
:
409 case PIPE_FORMAT_DXT1_RGBA
:
410 case PIPE_FORMAT_DXT3_RGBA
:
411 case PIPE_FORMAT_DXT5_RGBA
:
412 case PIPE_FORMAT_DXT1_SRGBA
:
413 case PIPE_FORMAT_DXT3_SRGBA
:
414 case PIPE_FORMAT_DXT5_SRGBA
:
427 * Format access functions.
431 util_format_read_4f(enum pipe_format format
,
432 float *dst
, unsigned dst_stride
,
433 const void *src
, unsigned src_stride
,
434 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
437 util_format_write_4f(enum pipe_format format
,
438 const float *src
, unsigned src_stride
,
439 void *dst
, unsigned dst_stride
,
440 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
443 util_format_read_4ub(enum pipe_format format
,
444 uint8_t *dst
, unsigned dst_stride
,
445 const void *src
, unsigned src_stride
,
446 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
449 util_format_write_4ub(enum pipe_format format
,
450 const uint8_t *src
, unsigned src_stride
,
451 void *dst
, unsigned dst_stride
,
452 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
458 #endif /* ! U_FORMAT_H */