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"
35 #include "util/u_format_s3tc.h"
43 * Describe how to pack/unpack pixels into/from the prescribed format.
45 * XXX: This could be renamed to something like util_format_pack, or broke down
46 * in flags inside util_format_block that said exactly what we want.
48 enum util_format_layout
{
50 * Formats with util_format_block::width == util_format_block::height == 1
51 * that can be described as an ordinary data structure.
53 UTIL_FORMAT_LAYOUT_PLAIN
= 0,
56 * Formats with sub-sampled channels.
58 * This is for formats like YV12 where there is less than one sample per
61 UTIL_FORMAT_LAYOUT_SUBSAMPLED
= 3,
64 * S3 Texture Compression formats.
66 UTIL_FORMAT_LAYOUT_S3TC
= 4,
69 * Red-Green Texture Compression formats.
71 UTIL_FORMAT_LAYOUT_RGTC
= 5,
74 * Everything else that doesn't fit in any of the above layouts.
76 UTIL_FORMAT_LAYOUT_OTHER
= 6
80 struct util_format_block
82 /** Block width in pixels */
85 /** Block height in pixels */
88 /** Block size in bits */
93 enum util_format_type
{
94 UTIL_FORMAT_TYPE_VOID
= 0,
95 UTIL_FORMAT_TYPE_UNSIGNED
= 1,
96 UTIL_FORMAT_TYPE_SIGNED
= 2,
97 UTIL_FORMAT_TYPE_FIXED
= 3,
98 UTIL_FORMAT_TYPE_FLOAT
= 4
102 enum util_format_swizzle
{
103 UTIL_FORMAT_SWIZZLE_X
= 0,
104 UTIL_FORMAT_SWIZZLE_Y
= 1,
105 UTIL_FORMAT_SWIZZLE_Z
= 2,
106 UTIL_FORMAT_SWIZZLE_W
= 3,
107 UTIL_FORMAT_SWIZZLE_0
= 4,
108 UTIL_FORMAT_SWIZZLE_1
= 5,
109 UTIL_FORMAT_SWIZZLE_NONE
= 6
113 enum util_format_colorspace
{
114 UTIL_FORMAT_COLORSPACE_RGB
= 0,
115 UTIL_FORMAT_COLORSPACE_SRGB
= 1,
116 UTIL_FORMAT_COLORSPACE_YUV
= 2,
117 UTIL_FORMAT_COLORSPACE_ZS
= 3
121 struct util_format_channel_description
124 unsigned normalized
:1;
129 struct util_format_description
131 enum pipe_format format
;
136 * Short name, striped of the prefix, lower case.
138 const char *short_name
;
141 * Pixel block dimensions.
143 struct util_format_block block
;
145 enum util_format_layout layout
;
148 * The number of channels.
150 unsigned nr_channels
:3;
153 * Whether all channels have the same number of (whole) bytes.
158 * Whether the pixel format can be described as a bitfield structure.
161 * - pixel depth must be 8, 16, or 32 bits;
162 * - all channels must be unsigned, signed, or void
164 unsigned is_bitmask
:1;
167 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
172 * Whether the pack/unpack functions actually work.
174 * Call util_format_is_supported instead of using this directly.
176 unsigned is_supported
:1;
179 * Input channel description.
181 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
183 struct util_format_channel_description channel
[4];
186 * Output channel swizzle.
188 * The order is either:
192 * depending on the colorspace.
194 unsigned char swizzle
[4];
197 * Colorspace transformation.
199 enum util_format_colorspace colorspace
;
202 * Unpack pixel blocks to R8G8B8A8_UNORM.
205 (*unpack_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
206 const uint8_t *src
, unsigned src_stride
,
207 unsigned width
, unsigned height
);
210 * Pack pixel blocks from R8G8B8A8_UNORM.
213 (*pack_8unorm
)(uint8_t *dst
, unsigned dst_stride
,
214 const uint8_t *src
, unsigned src_stride
,
215 unsigned width
, unsigned height
);
218 * Unpack pixel blocks to R32G32B32A32_FLOAT.
221 (*unpack_float
)(float *dst
, unsigned dst_stride
,
222 const uint8_t *src
, unsigned src_stride
,
223 unsigned width
, unsigned height
);
226 * Pack pixel blocks from R32G32B32A32_FLOAT.
229 (*pack_float
)(uint8_t *dst
, unsigned dst_stride
,
230 const float *src
, unsigned src_stride
,
231 unsigned width
, unsigned height
);
234 * Fetch a single pixel (i, j) from a block.
237 (*fetch_float
)(float *dst
,
239 unsigned i
, unsigned j
);
243 extern const struct util_format_description
244 util_format_description_table
[];
247 const struct util_format_description
*
248 util_format_description(enum pipe_format format
);
252 * Format query functions.
255 static INLINE
const char *
256 util_format_name(enum pipe_format format
)
258 const struct util_format_description
*desc
= util_format_description(format
);
268 static INLINE boolean
269 util_format_is_s3tc(enum pipe_format format
)
271 const struct util_format_description
*desc
= util_format_description(format
);
278 return desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
? TRUE
: FALSE
;
281 static INLINE boolean
282 util_format_is_depth_or_stencil(enum pipe_format format
)
284 const struct util_format_description
*desc
= util_format_description(format
);
291 return desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
? TRUE
: FALSE
;
294 static INLINE boolean
295 util_format_is_depth_and_stencil(enum pipe_format format
)
297 const struct util_format_description
*desc
= util_format_description(format
);
304 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
) {
308 return (desc
->swizzle
[0] != UTIL_FORMAT_SWIZZLE_NONE
&&
309 desc
->swizzle
[1] != UTIL_FORMAT_SWIZZLE_NONE
) ? TRUE
: FALSE
;
313 * Whether this format is a rgab8 variant.
315 * That is, any format that matches the
317 * PIPE_FORMAT_?8?8?8?8_UNORM
319 static INLINE boolean
320 util_format_is_rgba8_variant(const struct util_format_description
*desc
)
324 if(desc
->block
.width
!= 1 ||
325 desc
->block
.height
!= 1 ||
326 desc
->block
.bits
!= 32)
329 for(chan
= 0; chan
< 4; ++chan
) {
330 if(desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_UNSIGNED
&&
331 desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_VOID
)
333 if(desc
->channel
[chan
].size
!= 8)
342 * Return total bits needed for the pixel format per block.
345 util_format_get_blocksizebits(enum pipe_format format
)
347 const struct util_format_description
*desc
= util_format_description(format
);
354 return desc
->block
.bits
;
358 * Return bytes per block (not pixel) for the given format.
361 util_format_get_blocksize(enum pipe_format format
)
363 uint bits
= util_format_get_blocksizebits(format
);
365 assert(bits
% 8 == 0);
371 util_format_get_blockwidth(enum pipe_format format
)
373 const struct util_format_description
*desc
= util_format_description(format
);
380 return desc
->block
.width
;
384 util_format_get_blockheight(enum pipe_format format
)
386 const struct util_format_description
*desc
= util_format_description(format
);
393 return desc
->block
.height
;
396 static INLINE
unsigned
397 util_format_get_nblocksx(enum pipe_format format
,
400 unsigned blockwidth
= util_format_get_blockwidth(format
);
401 return (x
+ blockwidth
- 1) / blockwidth
;
404 static INLINE
unsigned
405 util_format_get_nblocksy(enum pipe_format format
,
408 unsigned blockheight
= util_format_get_blockheight(format
);
409 return (y
+ blockheight
- 1) / blockheight
;
412 static INLINE
unsigned
413 util_format_get_nblocks(enum pipe_format format
,
417 return util_format_get_nblocksx(format
, width
) * util_format_get_nblocksy(format
, height
);
421 util_format_get_stride(enum pipe_format format
,
424 return util_format_get_nblocksx(format
, width
) * util_format_get_blocksize(format
);
428 util_format_get_2d_size(enum pipe_format format
,
432 return util_format_get_nblocksy(format
, height
) * stride
;
436 util_format_get_component_bits(enum pipe_format format
,
437 enum util_format_colorspace colorspace
,
440 const struct util_format_description
*desc
= util_format_description(format
);
441 enum util_format_colorspace desc_colorspace
;
448 assert(component
< 4);
450 /* Treat RGB and SRGB as equivalent. */
451 if (colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
452 colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
454 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
455 desc_colorspace
= UTIL_FORMAT_COLORSPACE_RGB
;
457 desc_colorspace
= desc
->colorspace
;
460 if (desc_colorspace
!= colorspace
) {
464 switch (desc
->swizzle
[component
]) {
465 case UTIL_FORMAT_SWIZZLE_X
:
466 return desc
->channel
[0].size
;
467 case UTIL_FORMAT_SWIZZLE_Y
:
468 return desc
->channel
[1].size
;
469 case UTIL_FORMAT_SWIZZLE_Z
:
470 return desc
->channel
[2].size
;
471 case UTIL_FORMAT_SWIZZLE_W
:
472 return desc
->channel
[3].size
;
478 static INLINE boolean
479 util_format_has_alpha(enum pipe_format format
)
481 const struct util_format_description
*desc
= util_format_description(format
);
488 switch (desc
->colorspace
) {
489 case UTIL_FORMAT_COLORSPACE_RGB
:
490 case UTIL_FORMAT_COLORSPACE_SRGB
:
491 return desc
->swizzle
[3] != UTIL_FORMAT_SWIZZLE_1
;
492 case UTIL_FORMAT_COLORSPACE_YUV
:
494 case UTIL_FORMAT_COLORSPACE_ZS
:
503 * Return the number of components stored.
504 * Formats with block size != 1x1 will always have 1 component (the block).
506 static INLINE
unsigned
507 util_format_get_nr_components(enum pipe_format format
)
509 const struct util_format_description
*desc
= util_format_description(format
);
510 return desc
->nr_channels
;
514 * Format access functions.
517 static INLINE boolean
518 util_format_is_supported(enum pipe_format format
)
520 const struct util_format_description
*desc
= util_format_description(format
);
525 if(desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
)
526 util_format_s3tc_init();
528 return desc
->is_supported
;
532 util_format_read_4f(enum pipe_format format
,
533 float *dst
, unsigned dst_stride
,
534 const void *src
, unsigned src_stride
,
535 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
538 util_format_write_4f(enum pipe_format format
,
539 const float *src
, unsigned src_stride
,
540 void *dst
, unsigned dst_stride
,
541 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
544 util_format_read_4ub(enum pipe_format format
,
545 uint8_t *dst
, unsigned dst_stride
,
546 const void *src
, unsigned src_stride
,
547 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
550 util_format_write_4ub(enum pipe_format format
,
551 const uint8_t *src
, unsigned src_stride
,
552 void *dst
, unsigned dst_stride
,
553 unsigned x
, unsigned y
, unsigned w
, unsigned h
);
555 UTIL_INLINE_INIT(util_format
);
561 #endif /* ! U_FORMAT_H */