1 /**************************************************************************
2  *
3  * Copyright 2009-2010 Vmware, Inc.
4  * All Rights Reserved.
5  *
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:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
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.
25  *
26  **************************************************************************/
27 
28 
29 #ifndef U_FORMAT_H
30 #define U_FORMAT_H
31 
32 
33 #include "pipe/p_format.h"
34 #include "pipe/p_defines.h"
35 #include "util/u_debug.h"
36 
37 union pipe_color_union;
38 struct pipe_screen;
39 
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 
46 /**
47  * Describe how to pack/unpack pixels into/from the prescribed format.
48  *
49  * XXX: This could be renamed to something like util_format_pack, or broke down
50  * in flags inside util_format_block that said exactly what we want.
51  */
52 enum util_format_layout {
53    /**
54     * Formats with util_format_block::width == util_format_block::height == 1
55     * that can be described as an ordinary data structure.
56     */
57    UTIL_FORMAT_LAYOUT_PLAIN,
58 
59    /**
60     * Formats with sub-sampled channels.
61     *
62     * This is for formats like YVYU where there is less than one sample per
63     * pixel.
64     */
65    UTIL_FORMAT_LAYOUT_SUBSAMPLED,
66 
67    /**
68     * S3 Texture Compression formats.
69     */
70    UTIL_FORMAT_LAYOUT_S3TC,
71 
72    /**
73     * Red-Green Texture Compression formats.
74     */
75    UTIL_FORMAT_LAYOUT_RGTC,
76 
77    /**
78     * Ericsson Texture Compression
79     */
80    UTIL_FORMAT_LAYOUT_ETC,
81 
82    /**
83     * BC6/7 Texture Compression
84     */
85    UTIL_FORMAT_LAYOUT_BPTC,
86 
87    UTIL_FORMAT_LAYOUT_ASTC,
88 
89    UTIL_FORMAT_LAYOUT_ATC,
90 
91    /** Formats with 2 or more planes. */
92    UTIL_FORMAT_LAYOUT_PLANAR2,
93    UTIL_FORMAT_LAYOUT_PLANAR3,
94 
95    UTIL_FORMAT_LAYOUT_FXT1 = 10,
96 
97    /**
98     * Everything else that doesn't fit in any of the above layouts.
99     */
100    UTIL_FORMAT_LAYOUT_OTHER,
101 };
102 
103 
104 struct util_format_block
105 {
106    /** Block width in pixels */
107    unsigned width;
108 
109    /** Block height in pixels */
110    unsigned height;
111 
112    /** Block depth in pixels */
113    unsigned depth;
114 
115    /** Block size in bits */
116    unsigned bits;
117 };
118 
119 
120 enum util_format_type {
121    UTIL_FORMAT_TYPE_VOID = 0,
122    UTIL_FORMAT_TYPE_UNSIGNED = 1,
123    UTIL_FORMAT_TYPE_SIGNED = 2,
124    UTIL_FORMAT_TYPE_FIXED = 3,
125    UTIL_FORMAT_TYPE_FLOAT = 4
126 };
127 
128 
129 enum util_format_colorspace {
130    UTIL_FORMAT_COLORSPACE_RGB = 0,
131    UTIL_FORMAT_COLORSPACE_SRGB = 1,
132    UTIL_FORMAT_COLORSPACE_YUV = 2,
133    UTIL_FORMAT_COLORSPACE_ZS = 3
134 };
135 
136 
137 struct util_format_channel_description
138 {
139    unsigned type:5;        /**< UTIL_FORMAT_TYPE_x */
140    unsigned normalized:1;
141    unsigned pure_integer:1;
142    unsigned size:9;        /**< bits per channel */
143    unsigned shift:16;      /** number of bits from lsb */
144 };
145 
146 
147 struct util_format_description
148 {
149    enum pipe_format format;
150 
151    const char *name;
152 
153    /**
154     * Short name, striped of the prefix, lower case.
155     */
156    const char *short_name;
157 
158    /**
159     * Pixel block dimensions.
160     */
161    struct util_format_block block;
162 
163    enum util_format_layout layout;
164 
165    /**
166     * The number of channels.
167     */
168    unsigned nr_channels:3;
169 
170    /**
171     * Whether all channels have the same number of (whole) bytes and type.
172     */
173    unsigned is_array:1;
174 
175    /**
176     * Whether the pixel format can be described as a bitfield structure.
177     *
178     * In particular:
179     * - pixel depth must be 8, 16, or 32 bits;
180     * - all channels must be unsigned, signed, or void
181     */
182    unsigned is_bitmask:1;
183 
184    /**
185     * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
186     */
187    unsigned is_mixed:1;
188 
189    /**
190     * Whether the format contains UNORM channels
191     */
192    unsigned is_unorm:1;
193 
194    /**
195     * Whether the format contains SNORM channels
196     */
197    unsigned is_snorm:1;
198 
199    /**
200     * Input channel description, in the order XYZW.
201     *
202     * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
203     *
204     * If each channel is accessed as an individual N-byte value, X is always
205     * at the lowest address in memory, Y is always next, and so on.  For all
206     * currently-defined formats, the N-byte value has native endianness.
207     *
208     * If instead a group of channels is accessed as a single N-byte value,
209     * the order of the channels within that value depends on endianness.
210     * For big-endian targets, X is the most significant subvalue,
211     * otherwise it is the least significant one.
212     *
213     * For example, if X is 8 bits and Y is 24 bits, the memory order is:
214     *
215     *                 0  1  2  3
216     *  little-endian: X  Yl Ym Yu    (l = lower, m = middle, u = upper)
217     *  big-endian:    X  Yu Ym Yl
218     *
219     * If X is 5 bits, Y is 5 bits, Z is 5 bits and W is 1 bit, the layout is:
220     *
221     *                        0        1
222     *                 msb  lsb msb  lsb
223     *  little-endian: YYYXXXXX WZZZZZYY
224     *  big-endian:    XXXXXYYY YYZZZZZW
225     */
226    struct util_format_channel_description channel[4];
227 
228    /**
229     * Output channel swizzle.
230     *
231     * The order is either:
232     * - RGBA
233     * - YUV(A)
234     * - ZS
235     * depending on the colorspace.
236     */
237    unsigned char swizzle[4];
238 
239    /**
240     * Colorspace transformation.
241     */
242    enum util_format_colorspace colorspace;
243 
244    /**
245     * Unpack pixel blocks to R8G8B8A8_UNORM.
246     * Note: strides are in bytes.
247     *
248     * Only defined for non-depth-stencil formats.
249     */
250    void
251    (*unpack_rgba_8unorm)(uint8_t *dst, unsigned dst_stride,
252                          const uint8_t *src, unsigned src_stride,
253                          unsigned width, unsigned height);
254 
255    /**
256     * Pack pixel blocks from R8G8B8A8_UNORM.
257     * Note: strides are in bytes.
258     *
259     * Only defined for non-depth-stencil formats.
260     */
261    void
262    (*pack_rgba_8unorm)(uint8_t *dst, unsigned dst_stride,
263                        const uint8_t *src, unsigned src_stride,
264                        unsigned width, unsigned height);
265 
266    /**
267     * Fetch a single pixel (i, j) from a block.
268     *
269     * XXX: Only defined for a very few select formats.
270     */
271    void
272    (*fetch_rgba_8unorm)(uint8_t *dst,
273                         const uint8_t *src,
274                         unsigned i, unsigned j);
275 
276    /**
277     * Unpack pixel blocks to R32G32B32A32_UINT/_INT_FLOAT based on whether the
278     * type is pure uint, int, or other.
279     *
280     * Note: strides are in bytes.
281     *
282     * Only defined for non-depth-stencil formats.
283     */
284    void
285    (*unpack_rgba)(void *dst, unsigned dst_stride,
286                   const uint8_t *src, unsigned src_stride,
287                   unsigned width, unsigned height);
288 
289    /**
290     * Pack pixel blocks from R32G32B32A32_FLOAT.
291     * Note: strides are in bytes.
292     *
293     * Only defined for non-depth-stencil formats.
294     */
295    void
296    (*pack_rgba_float)(uint8_t *dst, unsigned dst_stride,
297                       const float *src, unsigned src_stride,
298                       unsigned width, unsigned height);
299 
300    /**
301     * Fetch a single pixel (i, j) from a block.
302     *
303     * Only defined for non-depth-stencil and non-integer formats.
304     */
305    void
306    (*fetch_rgba_float)(float *dst,
307                        const uint8_t *src,
308                        unsigned i, unsigned j);
309 
310    /**
311     * Unpack pixels to Z32_UNORM.
312     * Note: strides are in bytes.
313     *
314     * Only defined for depth formats.
315     */
316    void
317    (*unpack_z_32unorm)(uint32_t *dst, unsigned dst_stride,
318                        const uint8_t *src, unsigned src_stride,
319                        unsigned width, unsigned height);
320 
321    /**
322     * Pack pixels from Z32_FLOAT.
323     * Note: strides are in bytes.
324     *
325     * Only defined for depth formats.
326     */
327    void
328    (*pack_z_32unorm)(uint8_t *dst, unsigned dst_stride,
329                      const uint32_t *src, unsigned src_stride,
330                      unsigned width, unsigned height);
331 
332    /**
333     * Unpack pixels to Z32_FLOAT.
334     * Note: strides are in bytes.
335     *
336     * Only defined for depth formats.
337     */
338    void
339    (*unpack_z_float)(float *dst, unsigned dst_stride,
340                      const uint8_t *src, unsigned src_stride,
341                      unsigned width, unsigned height);
342 
343    /**
344     * Pack pixels from Z32_FLOAT.
345     * Note: strides are in bytes.
346     *
347     * Only defined for depth formats.
348     */
349    void
350    (*pack_z_float)(uint8_t *dst, unsigned dst_stride,
351                    const float *src, unsigned src_stride,
352                    unsigned width, unsigned height);
353 
354    /**
355     * Unpack pixels to S8_UINT.
356     * Note: strides are in bytes.
357     *
358     * Only defined for stencil formats.
359     */
360    void
361    (*unpack_s_8uint)(uint8_t *dst, unsigned dst_stride,
362                      const uint8_t *src, unsigned src_stride,
363                      unsigned width, unsigned height);
364 
365    /**
366     * Pack pixels from S8_UINT.
367     * Note: strides are in bytes.
368     *
369     * Only defined for stencil formats.
370     */
371    void
372    (*pack_s_8uint)(uint8_t *dst, unsigned dst_stride,
373                    const uint8_t *src, unsigned src_stride,
374                    unsigned width, unsigned height);
375 
376    void
377    (*pack_rgba_uint)(uint8_t *dst, unsigned dst_stride,
378                      const uint32_t *src, unsigned src_stride,
379                      unsigned width, unsigned height);
380 
381    void
382    (*pack_rgba_sint)(uint8_t *dst, unsigned dst_stride,
383                      const int32_t *src, unsigned src_stride,
384                      unsigned width, unsigned height);
385 
386    /**
387     * Fetch a single pixel (i, j) from a block.
388     *
389     * Only defined for unsigned (pure) integer formats.
390     */
391    void
392    (*fetch_rgba_uint)(uint32_t *dst,
393                       const uint8_t *src,
394                       unsigned i, unsigned j);
395 
396    /**
397     * Fetch a single pixel (i, j) from a block.
398     *
399     * Only defined for signed (pure) integer formats.
400     */
401    void
402    (*fetch_rgba_sint)(int32_t *dst,
403                       const uint8_t *src,
404                       unsigned i, unsigned j);
405 };
406 
407 
408 const struct util_format_description *
409 util_format_description(enum pipe_format format) ATTRIBUTE_CONST;
410 
411 
412 /*
413  * Format query functions.
414  */
415 
416 static inline const char *
util_format_name(enum pipe_format format)417 util_format_name(enum pipe_format format)
418 {
419    const struct util_format_description *desc = util_format_description(format);
420 
421    assert(desc);
422    if (!desc) {
423       return "PIPE_FORMAT_???";
424    }
425 
426    return desc->name;
427 }
428 
429 static inline const char *
util_format_short_name(enum pipe_format format)430 util_format_short_name(enum pipe_format format)
431 {
432    const struct util_format_description *desc = util_format_description(format);
433 
434    assert(desc);
435    if (!desc) {
436       return "???";
437    }
438 
439    return desc->short_name;
440 }
441 
442 /**
443  * Whether this format is plain, see UTIL_FORMAT_LAYOUT_PLAIN for more info.
444  */
445 static inline boolean
util_format_is_plain(enum pipe_format format)446 util_format_is_plain(enum pipe_format format)
447 {
448    const struct util_format_description *desc = util_format_description(format);
449 
450    if (!format) {
451       return FALSE;
452    }
453 
454    return desc->layout == UTIL_FORMAT_LAYOUT_PLAIN ? TRUE : FALSE;
455 }
456 
457 static inline boolean
util_format_is_compressed(enum pipe_format format)458 util_format_is_compressed(enum pipe_format format)
459 {
460    const struct util_format_description *desc = util_format_description(format);
461 
462    assert(desc);
463    if (!desc) {
464       return FALSE;
465    }
466 
467    switch (desc->layout) {
468    case UTIL_FORMAT_LAYOUT_S3TC:
469    case UTIL_FORMAT_LAYOUT_RGTC:
470    case UTIL_FORMAT_LAYOUT_ETC:
471    case UTIL_FORMAT_LAYOUT_BPTC:
472    case UTIL_FORMAT_LAYOUT_ASTC:
473    case UTIL_FORMAT_LAYOUT_ATC:
474    case UTIL_FORMAT_LAYOUT_FXT1:
475       /* XXX add other formats in the future */
476       return TRUE;
477    default:
478       return FALSE;
479    }
480 }
481 
482 static inline boolean
util_format_is_s3tc(enum pipe_format format)483 util_format_is_s3tc(enum pipe_format format)
484 {
485    const struct util_format_description *desc = util_format_description(format);
486 
487    assert(desc);
488    if (!desc) {
489       return FALSE;
490    }
491 
492    return desc->layout == UTIL_FORMAT_LAYOUT_S3TC ? TRUE : FALSE;
493 }
494 
495 static inline boolean
util_format_is_etc(enum pipe_format format)496 util_format_is_etc(enum pipe_format format)
497 {
498    const struct util_format_description *desc = util_format_description(format);
499 
500    assert(desc);
501    if (!desc) {
502       return FALSE;
503    }
504 
505    return desc->layout == UTIL_FORMAT_LAYOUT_ETC ? TRUE : FALSE;
506 }
507 
508 static inline boolean
util_format_is_srgb(enum pipe_format format)509 util_format_is_srgb(enum pipe_format format)
510 {
511    const struct util_format_description *desc = util_format_description(format);
512    return desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB;
513 }
514 
515 static inline boolean
util_format_has_depth(const struct util_format_description * desc)516 util_format_has_depth(const struct util_format_description *desc)
517 {
518    return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS &&
519           desc->swizzle[0] != PIPE_SWIZZLE_NONE;
520 }
521 
522 static inline boolean
util_format_has_stencil(const struct util_format_description * desc)523 util_format_has_stencil(const struct util_format_description *desc)
524 {
525    return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS &&
526           desc->swizzle[1] != PIPE_SWIZZLE_NONE;
527 }
528 
529 static inline boolean
util_format_is_depth_or_stencil(enum pipe_format format)530 util_format_is_depth_or_stencil(enum pipe_format format)
531 {
532    const struct util_format_description *desc = util_format_description(format);
533 
534    assert(desc);
535    if (!desc) {
536       return FALSE;
537    }
538 
539    return util_format_has_depth(desc) ||
540           util_format_has_stencil(desc);
541 }
542 
543 static inline boolean
util_format_is_depth_and_stencil(enum pipe_format format)544 util_format_is_depth_and_stencil(enum pipe_format format)
545 {
546    const struct util_format_description *desc = util_format_description(format);
547 
548    assert(desc);
549    if (!desc) {
550       return FALSE;
551    }
552 
553    return util_format_has_depth(desc) &&
554           util_format_has_stencil(desc);
555 }
556 
557 /**
558  * For depth-stencil formats, return the equivalent depth-only format.
559  */
560 static inline enum pipe_format
util_format_get_depth_only(enum pipe_format format)561 util_format_get_depth_only(enum pipe_format format)
562 {
563    switch (format) {
564    case PIPE_FORMAT_Z24_UNORM_S8_UINT:
565       return PIPE_FORMAT_Z24X8_UNORM;
566 
567    case PIPE_FORMAT_S8_UINT_Z24_UNORM:
568       return PIPE_FORMAT_X8Z24_UNORM;
569 
570    case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
571       return PIPE_FORMAT_Z32_FLOAT;
572 
573    default:
574       return format;
575    }
576 }
577 
578 static inline boolean
util_format_is_yuv(enum pipe_format format)579 util_format_is_yuv(enum pipe_format format)
580 {
581    const struct util_format_description *desc = util_format_description(format);
582 
583    assert(desc);
584    if (!desc) {
585       return FALSE;
586    }
587 
588    return desc->colorspace == UTIL_FORMAT_COLORSPACE_YUV;
589 }
590 
591 /**
592  * Calculates the depth format type based upon the incoming format description.
593  */
594 static inline unsigned
util_get_depth_format_type(const struct util_format_description * desc)595 util_get_depth_format_type(const struct util_format_description *desc)
596 {
597    unsigned depth_channel = desc->swizzle[0];
598    if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS &&
599        depth_channel != PIPE_SWIZZLE_NONE) {
600       return desc->channel[depth_channel].type;
601    } else {
602       return UTIL_FORMAT_TYPE_VOID;
603    }
604 }
605 
606 
607 /**
608  * Calculates the MRD for the depth format. MRD is used in depth bias
609  * for UNORM and unbound depth buffers. When the depth buffer is floating
610  * point, the depth bias calculation does not use the MRD. However, the
611  * default MRD will be 1.0 / ((1 << 24) - 1).
612  */
613 double
614 util_get_depth_format_mrd(const struct util_format_description *desc);
615 
616 
617 /**
618  * Return whether this is an RGBA, Z, S, or combined ZS format.
619  * Useful for initializing pipe_blit_info::mask.
620  */
621 static inline unsigned
util_format_get_mask(enum pipe_format format)622 util_format_get_mask(enum pipe_format format)
623 {
624    const struct util_format_description *desc =
625       util_format_description(format);
626 
627    if (!desc)
628       return 0;
629 
630    if (util_format_has_depth(desc)) {
631       if (util_format_has_stencil(desc)) {
632          return PIPE_MASK_ZS;
633       } else {
634          return PIPE_MASK_Z;
635       }
636    } else {
637       if (util_format_has_stencil(desc)) {
638          return PIPE_MASK_S;
639       } else {
640          return PIPE_MASK_RGBA;
641       }
642    }
643 }
644 
645 /**
646  * Give the RGBA colormask of the channels that can be represented in this
647  * format.
648  *
649  * That is, the channels whose values are preserved.
650  */
651 static inline unsigned
util_format_colormask(const struct util_format_description * desc)652 util_format_colormask(const struct util_format_description *desc)
653 {
654    unsigned colormask;
655    unsigned chan;
656 
657    switch (desc->colorspace) {
658    case UTIL_FORMAT_COLORSPACE_RGB:
659    case UTIL_FORMAT_COLORSPACE_SRGB:
660    case UTIL_FORMAT_COLORSPACE_YUV:
661       colormask = 0;
662       for (chan = 0; chan < 4; ++chan) {
663          if (desc->swizzle[chan] < 4) {
664             colormask |= (1 << chan);
665          }
666       }
667       return colormask;
668    case UTIL_FORMAT_COLORSPACE_ZS:
669       return 0;
670    default:
671       assert(0);
672       return 0;
673    }
674 }
675 
676 
677 /**
678  * Checks if color mask covers every channel for the specified format
679  *
680  * @param desc       a format description to check colormask with
681  * @param colormask  a bit mask for channels, matches format of PIPE_MASK_RGBA
682  */
683 static inline boolean
util_format_colormask_full(const struct util_format_description * desc,unsigned colormask)684 util_format_colormask_full(const struct util_format_description *desc, unsigned colormask)
685 {
686    return (~colormask & util_format_colormask(desc)) == 0;
687 }
688 
689 
690 boolean
691 util_format_is_float(enum pipe_format format);
692 
693 
694 boolean
695 util_format_has_alpha(enum pipe_format format);
696 
697 
698 boolean
699 util_format_is_luminance(enum pipe_format format);
700 
701 boolean
702 util_format_is_alpha(enum pipe_format format);
703 
704 boolean
705 util_format_is_luminance_alpha(enum pipe_format format);
706 
707 
708 boolean
709 util_format_is_intensity(enum pipe_format format);
710 
711 boolean
712 util_format_is_subsampled_422(enum pipe_format format);
713 
714 boolean
715 util_format_is_pure_integer(enum pipe_format format);
716 
717 boolean
718 util_format_is_pure_sint(enum pipe_format format);
719 
720 boolean
721 util_format_is_pure_uint(enum pipe_format format);
722 
723 boolean
724 util_format_is_snorm(enum pipe_format format);
725 
726 boolean
727 util_format_is_unorm(enum pipe_format format);
728 
729 boolean
730 util_format_is_snorm8(enum pipe_format format);
731 
732 /**
733  * Check if the src format can be blitted to the destination format with
734  * a simple memcpy.  For example, blitting from RGBA to RGBx is OK, but not
735  * the reverse.
736  */
737 boolean
738 util_is_format_compatible(const struct util_format_description *src_desc,
739                           const struct util_format_description *dst_desc);
740 
741 /**
742  * Whether this format is a rgab8 variant.
743  *
744  * That is, any format that matches the
745  *
746  *   PIPE_FORMAT_?8?8?8?8_UNORM
747  */
748 static inline boolean
util_format_is_rgba8_variant(const struct util_format_description * desc)749 util_format_is_rgba8_variant(const struct util_format_description *desc)
750 {
751    unsigned chan;
752 
753    if(desc->block.width != 1 ||
754       desc->block.height != 1 ||
755       desc->block.bits != 32)
756       return FALSE;
757 
758    for(chan = 0; chan < 4; ++chan) {
759       if(desc->channel[chan].type != UTIL_FORMAT_TYPE_UNSIGNED &&
760          desc->channel[chan].type != UTIL_FORMAT_TYPE_VOID)
761          return FALSE;
762       if(desc->channel[chan].type == UTIL_FORMAT_TYPE_UNSIGNED &&
763          !desc->channel[chan].normalized)
764          return FALSE;
765       if(desc->channel[chan].size != 8)
766          return FALSE;
767    }
768 
769    return TRUE;
770 }
771 
772 /**
773  * Return total bits needed for the pixel format per block.
774  */
775 static inline uint
util_format_get_blocksizebits(enum pipe_format format)776 util_format_get_blocksizebits(enum pipe_format format)
777 {
778    const struct util_format_description *desc = util_format_description(format);
779 
780    assert(desc);
781    if (!desc) {
782       return 0;
783    }
784 
785    return desc->block.bits;
786 }
787 
788 /**
789  * Return bytes per block (not pixel) for the given format.
790  */
791 static inline uint
util_format_get_blocksize(enum pipe_format format)792 util_format_get_blocksize(enum pipe_format format)
793 {
794    uint bits = util_format_get_blocksizebits(format);
795    uint bytes = bits / 8;
796 
797    assert(bits % 8 == 0);
798    assert(bytes > 0);
799    if (bytes == 0) {
800       bytes = 1;
801    }
802 
803    return bytes;
804 }
805 
806 static inline uint
util_format_get_blockwidth(enum pipe_format format)807 util_format_get_blockwidth(enum pipe_format format)
808 {
809    const struct util_format_description *desc = util_format_description(format);
810 
811    assert(desc);
812    if (!desc) {
813       return 1;
814    }
815 
816    return desc->block.width;
817 }
818 
819 static inline uint
util_format_get_blockheight(enum pipe_format format)820 util_format_get_blockheight(enum pipe_format format)
821 {
822    const struct util_format_description *desc = util_format_description(format);
823 
824    assert(desc);
825    if (!desc) {
826       return 1;
827    }
828 
829    return desc->block.height;
830 }
831 
832 static inline uint
util_format_get_blockdepth(enum pipe_format format)833 util_format_get_blockdepth(enum pipe_format format)
834 {
835    const struct util_format_description *desc = util_format_description(format);
836 
837    assert(desc);
838    if (!desc) {
839       return 1;
840    }
841 
842    return desc->block.depth;
843 }
844 
845 static inline unsigned
util_format_get_nblocksx(enum pipe_format format,unsigned x)846 util_format_get_nblocksx(enum pipe_format format,
847                          unsigned x)
848 {
849    unsigned blockwidth = util_format_get_blockwidth(format);
850    return (x + blockwidth - 1) / blockwidth;
851 }
852 
853 static inline unsigned
util_format_get_nblocksy(enum pipe_format format,unsigned y)854 util_format_get_nblocksy(enum pipe_format format,
855                          unsigned y)
856 {
857    unsigned blockheight = util_format_get_blockheight(format);
858    return (y + blockheight - 1) / blockheight;
859 }
860 
861 static inline unsigned
util_format_get_nblocksz(enum pipe_format format,unsigned z)862 util_format_get_nblocksz(enum pipe_format format,
863                          unsigned z)
864 {
865    unsigned blockdepth = util_format_get_blockdepth(format);
866    return (z + blockdepth - 1) / blockdepth;
867 }
868 
869 static inline unsigned
util_format_get_nblocks(enum pipe_format format,unsigned width,unsigned height)870 util_format_get_nblocks(enum pipe_format format,
871                         unsigned width,
872                         unsigned height)
873 {
874    assert(util_format_get_blockdepth(format) == 1);
875    return util_format_get_nblocksx(format, width) * util_format_get_nblocksy(format, height);
876 }
877 
878 static inline size_t
util_format_get_stride(enum pipe_format format,unsigned width)879 util_format_get_stride(enum pipe_format format,
880                        unsigned width)
881 {
882    return (size_t)util_format_get_nblocksx(format, width) * util_format_get_blocksize(format);
883 }
884 
885 static inline size_t
util_format_get_2d_size(enum pipe_format format,size_t stride,unsigned height)886 util_format_get_2d_size(enum pipe_format format,
887                         size_t stride,
888                         unsigned height)
889 {
890    return util_format_get_nblocksy(format, height) * stride;
891 }
892 
893 static inline uint
util_format_get_component_bits(enum pipe_format format,enum util_format_colorspace colorspace,uint component)894 util_format_get_component_bits(enum pipe_format format,
895                                enum util_format_colorspace colorspace,
896                                uint component)
897 {
898    const struct util_format_description *desc = util_format_description(format);
899    enum util_format_colorspace desc_colorspace;
900 
901    assert(format);
902    if (!format) {
903       return 0;
904    }
905 
906    assert(component < 4);
907 
908    /* Treat RGB and SRGB as equivalent. */
909    if (colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
910       colorspace = UTIL_FORMAT_COLORSPACE_RGB;
911    }
912    if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
913       desc_colorspace = UTIL_FORMAT_COLORSPACE_RGB;
914    } else {
915       desc_colorspace = desc->colorspace;
916    }
917 
918    if (desc_colorspace != colorspace) {
919       return 0;
920    }
921 
922    switch (desc->swizzle[component]) {
923    case PIPE_SWIZZLE_X:
924       return desc->channel[0].size;
925    case PIPE_SWIZZLE_Y:
926       return desc->channel[1].size;
927    case PIPE_SWIZZLE_Z:
928       return desc->channel[2].size;
929    case PIPE_SWIZZLE_W:
930       return desc->channel[3].size;
931    default:
932       return 0;
933    }
934 }
935 
936 /**
937  * Given a linear RGB colorspace format, return the corresponding SRGB
938  * format, or PIPE_FORMAT_NONE if none.
939  */
940 static inline enum pipe_format
util_format_srgb(enum pipe_format format)941 util_format_srgb(enum pipe_format format)
942 {
943    if (util_format_is_srgb(format))
944       return format;
945 
946    switch (format) {
947    case PIPE_FORMAT_L8_UNORM:
948       return PIPE_FORMAT_L8_SRGB;
949    case PIPE_FORMAT_R8_UNORM:
950       return PIPE_FORMAT_R8_SRGB;
951    case PIPE_FORMAT_L8A8_UNORM:
952       return PIPE_FORMAT_L8A8_SRGB;
953    case PIPE_FORMAT_R8G8_UNORM:
954       return PIPE_FORMAT_R8G8_SRGB;
955    case PIPE_FORMAT_R8G8B8_UNORM:
956       return PIPE_FORMAT_R8G8B8_SRGB;
957    case PIPE_FORMAT_B8G8R8_UNORM:
958       return PIPE_FORMAT_B8G8R8_SRGB;
959    case PIPE_FORMAT_A8B8G8R8_UNORM:
960       return PIPE_FORMAT_A8B8G8R8_SRGB;
961    case PIPE_FORMAT_X8B8G8R8_UNORM:
962       return PIPE_FORMAT_X8B8G8R8_SRGB;
963    case PIPE_FORMAT_B8G8R8A8_UNORM:
964       return PIPE_FORMAT_B8G8R8A8_SRGB;
965    case PIPE_FORMAT_B8G8R8X8_UNORM:
966       return PIPE_FORMAT_B8G8R8X8_SRGB;
967    case PIPE_FORMAT_A8R8G8B8_UNORM:
968       return PIPE_FORMAT_A8R8G8B8_SRGB;
969    case PIPE_FORMAT_X8R8G8B8_UNORM:
970       return PIPE_FORMAT_X8R8G8B8_SRGB;
971    case PIPE_FORMAT_R8G8B8A8_UNORM:
972       return PIPE_FORMAT_R8G8B8A8_SRGB;
973    case PIPE_FORMAT_R8G8B8X8_UNORM:
974       return PIPE_FORMAT_R8G8B8X8_SRGB;
975    case PIPE_FORMAT_DXT1_RGB:
976       return PIPE_FORMAT_DXT1_SRGB;
977    case PIPE_FORMAT_DXT1_RGBA:
978       return PIPE_FORMAT_DXT1_SRGBA;
979    case PIPE_FORMAT_DXT3_RGBA:
980       return PIPE_FORMAT_DXT3_SRGBA;
981    case PIPE_FORMAT_DXT5_RGBA:
982       return PIPE_FORMAT_DXT5_SRGBA;
983    case PIPE_FORMAT_B5G6R5_UNORM:
984       return PIPE_FORMAT_B5G6R5_SRGB;
985    case PIPE_FORMAT_BPTC_RGBA_UNORM:
986       return PIPE_FORMAT_BPTC_SRGBA;
987    case PIPE_FORMAT_ETC2_RGB8:
988       return PIPE_FORMAT_ETC2_SRGB8;
989    case PIPE_FORMAT_ETC2_RGB8A1:
990       return PIPE_FORMAT_ETC2_SRGB8A1;
991    case PIPE_FORMAT_ETC2_RGBA8:
992       return PIPE_FORMAT_ETC2_SRGBA8;
993    case PIPE_FORMAT_ASTC_4x4:
994       return PIPE_FORMAT_ASTC_4x4_SRGB;
995    case PIPE_FORMAT_ASTC_5x4:
996       return PIPE_FORMAT_ASTC_5x4_SRGB;
997    case PIPE_FORMAT_ASTC_5x5:
998       return PIPE_FORMAT_ASTC_5x5_SRGB;
999    case PIPE_FORMAT_ASTC_6x5:
1000       return PIPE_FORMAT_ASTC_6x5_SRGB;
1001    case PIPE_FORMAT_ASTC_6x6:
1002       return PIPE_FORMAT_ASTC_6x6_SRGB;
1003    case PIPE_FORMAT_ASTC_8x5:
1004       return PIPE_FORMAT_ASTC_8x5_SRGB;
1005    case PIPE_FORMAT_ASTC_8x6:
1006       return PIPE_FORMAT_ASTC_8x6_SRGB;
1007    case PIPE_FORMAT_ASTC_8x8:
1008       return PIPE_FORMAT_ASTC_8x8_SRGB;
1009    case PIPE_FORMAT_ASTC_10x5:
1010       return PIPE_FORMAT_ASTC_10x5_SRGB;
1011    case PIPE_FORMAT_ASTC_10x6:
1012       return PIPE_FORMAT_ASTC_10x6_SRGB;
1013    case PIPE_FORMAT_ASTC_10x8:
1014       return PIPE_FORMAT_ASTC_10x8_SRGB;
1015    case PIPE_FORMAT_ASTC_10x10:
1016       return PIPE_FORMAT_ASTC_10x10_SRGB;
1017    case PIPE_FORMAT_ASTC_12x10:
1018       return PIPE_FORMAT_ASTC_12x10_SRGB;
1019    case PIPE_FORMAT_ASTC_12x12:
1020       return PIPE_FORMAT_ASTC_12x12_SRGB;
1021    case PIPE_FORMAT_ASTC_3x3x3:
1022       return PIPE_FORMAT_ASTC_3x3x3_SRGB;
1023    case PIPE_FORMAT_ASTC_4x3x3:
1024       return PIPE_FORMAT_ASTC_4x3x3_SRGB;
1025    case PIPE_FORMAT_ASTC_4x4x3:
1026       return PIPE_FORMAT_ASTC_4x4x3_SRGB;
1027    case PIPE_FORMAT_ASTC_4x4x4:
1028       return PIPE_FORMAT_ASTC_4x4x4_SRGB;
1029    case PIPE_FORMAT_ASTC_5x4x4:
1030       return PIPE_FORMAT_ASTC_5x4x4_SRGB;
1031    case PIPE_FORMAT_ASTC_5x5x4:
1032       return PIPE_FORMAT_ASTC_5x5x4_SRGB;
1033    case PIPE_FORMAT_ASTC_5x5x5:
1034       return PIPE_FORMAT_ASTC_5x5x5_SRGB;
1035    case PIPE_FORMAT_ASTC_6x5x5:
1036       return PIPE_FORMAT_ASTC_6x5x5_SRGB;
1037    case PIPE_FORMAT_ASTC_6x6x5:
1038       return PIPE_FORMAT_ASTC_6x6x5_SRGB;
1039    case PIPE_FORMAT_ASTC_6x6x6:
1040       return PIPE_FORMAT_ASTC_6x6x6_SRGB;
1041 
1042    default:
1043       return PIPE_FORMAT_NONE;
1044    }
1045 }
1046 
1047 /**
1048  * Given an sRGB format, return the corresponding linear colorspace format.
1049  * For non sRGB formats, return the format unchanged.
1050  */
1051 static inline enum pipe_format
util_format_linear(enum pipe_format format)1052 util_format_linear(enum pipe_format format)
1053 {
1054    switch (format) {
1055    case PIPE_FORMAT_L8_SRGB:
1056       return PIPE_FORMAT_L8_UNORM;
1057    case PIPE_FORMAT_R8_SRGB:
1058       return PIPE_FORMAT_R8_UNORM;
1059    case PIPE_FORMAT_L8A8_SRGB:
1060       return PIPE_FORMAT_L8A8_UNORM;
1061    case PIPE_FORMAT_R8G8_SRGB:
1062       return PIPE_FORMAT_R8G8_UNORM;
1063    case PIPE_FORMAT_R8G8B8_SRGB:
1064       return PIPE_FORMAT_R8G8B8_UNORM;
1065    case PIPE_FORMAT_B8G8R8_SRGB:
1066       return PIPE_FORMAT_B8G8R8_UNORM;
1067    case PIPE_FORMAT_A8B8G8R8_SRGB:
1068       return PIPE_FORMAT_A8B8G8R8_UNORM;
1069    case PIPE_FORMAT_X8B8G8R8_SRGB:
1070       return PIPE_FORMAT_X8B8G8R8_UNORM;
1071    case PIPE_FORMAT_B8G8R8A8_SRGB:
1072       return PIPE_FORMAT_B8G8R8A8_UNORM;
1073    case PIPE_FORMAT_B8G8R8X8_SRGB:
1074       return PIPE_FORMAT_B8G8R8X8_UNORM;
1075    case PIPE_FORMAT_A8R8G8B8_SRGB:
1076       return PIPE_FORMAT_A8R8G8B8_UNORM;
1077    case PIPE_FORMAT_X8R8G8B8_SRGB:
1078       return PIPE_FORMAT_X8R8G8B8_UNORM;
1079    case PIPE_FORMAT_R8G8B8A8_SRGB:
1080       return PIPE_FORMAT_R8G8B8A8_UNORM;
1081    case PIPE_FORMAT_R8G8B8X8_SRGB:
1082       return PIPE_FORMAT_R8G8B8X8_UNORM;
1083    case PIPE_FORMAT_DXT1_SRGB:
1084       return PIPE_FORMAT_DXT1_RGB;
1085    case PIPE_FORMAT_DXT1_SRGBA:
1086       return PIPE_FORMAT_DXT1_RGBA;
1087    case PIPE_FORMAT_DXT3_SRGBA:
1088       return PIPE_FORMAT_DXT3_RGBA;
1089    case PIPE_FORMAT_DXT5_SRGBA:
1090       return PIPE_FORMAT_DXT5_RGBA;
1091    case PIPE_FORMAT_B5G6R5_SRGB:
1092       return PIPE_FORMAT_B5G6R5_UNORM;
1093    case PIPE_FORMAT_BPTC_SRGBA:
1094       return PIPE_FORMAT_BPTC_RGBA_UNORM;
1095    case PIPE_FORMAT_ETC2_SRGB8:
1096       return PIPE_FORMAT_ETC2_RGB8;
1097    case PIPE_FORMAT_ETC2_SRGB8A1:
1098       return PIPE_FORMAT_ETC2_RGB8A1;
1099    case PIPE_FORMAT_ETC2_SRGBA8:
1100       return PIPE_FORMAT_ETC2_RGBA8;
1101    case PIPE_FORMAT_ASTC_4x4_SRGB:
1102       return PIPE_FORMAT_ASTC_4x4;
1103    case PIPE_FORMAT_ASTC_5x4_SRGB:
1104       return PIPE_FORMAT_ASTC_5x4;
1105    case PIPE_FORMAT_ASTC_5x5_SRGB:
1106       return PIPE_FORMAT_ASTC_5x5;
1107    case PIPE_FORMAT_ASTC_6x5_SRGB:
1108       return PIPE_FORMAT_ASTC_6x5;
1109    case PIPE_FORMAT_ASTC_6x6_SRGB:
1110       return PIPE_FORMAT_ASTC_6x6;
1111    case PIPE_FORMAT_ASTC_8x5_SRGB:
1112       return PIPE_FORMAT_ASTC_8x5;
1113    case PIPE_FORMAT_ASTC_8x6_SRGB:
1114       return PIPE_FORMAT_ASTC_8x6;
1115    case PIPE_FORMAT_ASTC_8x8_SRGB:
1116       return PIPE_FORMAT_ASTC_8x8;
1117    case PIPE_FORMAT_ASTC_10x5_SRGB:
1118       return PIPE_FORMAT_ASTC_10x5;
1119    case PIPE_FORMAT_ASTC_10x6_SRGB:
1120       return PIPE_FORMAT_ASTC_10x6;
1121    case PIPE_FORMAT_ASTC_10x8_SRGB:
1122       return PIPE_FORMAT_ASTC_10x8;
1123    case PIPE_FORMAT_ASTC_10x10_SRGB:
1124       return PIPE_FORMAT_ASTC_10x10;
1125    case PIPE_FORMAT_ASTC_12x10_SRGB:
1126       return PIPE_FORMAT_ASTC_12x10;
1127    case PIPE_FORMAT_ASTC_12x12_SRGB:
1128       return PIPE_FORMAT_ASTC_12x12;
1129    case PIPE_FORMAT_ASTC_3x3x3_SRGB:
1130       return PIPE_FORMAT_ASTC_3x3x3;
1131    case PIPE_FORMAT_ASTC_4x3x3_SRGB:
1132       return PIPE_FORMAT_ASTC_4x3x3;
1133    case PIPE_FORMAT_ASTC_4x4x3_SRGB:
1134       return PIPE_FORMAT_ASTC_4x4x3;
1135    case PIPE_FORMAT_ASTC_4x4x4_SRGB:
1136       return PIPE_FORMAT_ASTC_4x4x4;
1137    case PIPE_FORMAT_ASTC_5x4x4_SRGB:
1138       return PIPE_FORMAT_ASTC_5x4x4;
1139    case PIPE_FORMAT_ASTC_5x5x4_SRGB:
1140       return PIPE_FORMAT_ASTC_5x5x4;
1141    case PIPE_FORMAT_ASTC_5x5x5_SRGB:
1142       return PIPE_FORMAT_ASTC_5x5x5;
1143    case PIPE_FORMAT_ASTC_6x5x5_SRGB:
1144       return PIPE_FORMAT_ASTC_6x5x5;
1145    case PIPE_FORMAT_ASTC_6x6x5_SRGB:
1146       return PIPE_FORMAT_ASTC_6x6x5;
1147    case PIPE_FORMAT_ASTC_6x6x6_SRGB:
1148       return PIPE_FORMAT_ASTC_6x6x6;
1149    default:
1150       return format;
1151    }
1152 }
1153 
1154 /**
1155  * Given a depth-stencil format, return the corresponding stencil-only format.
1156  * For stencil-only formats, return the format unchanged.
1157  */
1158 static inline enum pipe_format
util_format_stencil_only(enum pipe_format format)1159 util_format_stencil_only(enum pipe_format format)
1160 {
1161    switch (format) {
1162    /* mask out the depth component */
1163    case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1164       return PIPE_FORMAT_X24S8_UINT;
1165    case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1166       return PIPE_FORMAT_S8X24_UINT;
1167    case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1168       return PIPE_FORMAT_X32_S8X24_UINT;
1169 
1170    /* stencil only formats */
1171    case PIPE_FORMAT_X24S8_UINT:
1172    case PIPE_FORMAT_S8X24_UINT:
1173    case PIPE_FORMAT_X32_S8X24_UINT:
1174    case PIPE_FORMAT_S8_UINT:
1175       return format;
1176 
1177    default:
1178       assert(0);
1179       return PIPE_FORMAT_NONE;
1180    }
1181 }
1182 
1183 /**
1184  * Converts PIPE_FORMAT_*I* to PIPE_FORMAT_*R*.
1185  * This is identity for non-intensity formats.
1186  */
1187 static inline enum pipe_format
util_format_intensity_to_red(enum pipe_format format)1188 util_format_intensity_to_red(enum pipe_format format)
1189 {
1190    switch (format) {
1191    case PIPE_FORMAT_I8_UNORM:
1192       return PIPE_FORMAT_R8_UNORM;
1193    case PIPE_FORMAT_I8_SNORM:
1194       return PIPE_FORMAT_R8_SNORM;
1195    case PIPE_FORMAT_I16_UNORM:
1196       return PIPE_FORMAT_R16_UNORM;
1197    case PIPE_FORMAT_I16_SNORM:
1198       return PIPE_FORMAT_R16_SNORM;
1199    case PIPE_FORMAT_I16_FLOAT:
1200       return PIPE_FORMAT_R16_FLOAT;
1201    case PIPE_FORMAT_I32_FLOAT:
1202       return PIPE_FORMAT_R32_FLOAT;
1203    case PIPE_FORMAT_I8_UINT:
1204       return PIPE_FORMAT_R8_UINT;
1205    case PIPE_FORMAT_I8_SINT:
1206       return PIPE_FORMAT_R8_SINT;
1207    case PIPE_FORMAT_I16_UINT:
1208       return PIPE_FORMAT_R16_UINT;
1209    case PIPE_FORMAT_I16_SINT:
1210       return PIPE_FORMAT_R16_SINT;
1211    case PIPE_FORMAT_I32_UINT:
1212       return PIPE_FORMAT_R32_UINT;
1213    case PIPE_FORMAT_I32_SINT:
1214       return PIPE_FORMAT_R32_SINT;
1215    default:
1216       assert(!util_format_is_intensity(format));
1217       return format;
1218    }
1219 }
1220 
1221 /**
1222  * Converts PIPE_FORMAT_*L* to PIPE_FORMAT_*R*.
1223  * This is identity for non-luminance formats.
1224  */
1225 static inline enum pipe_format
util_format_luminance_to_red(enum pipe_format format)1226 util_format_luminance_to_red(enum pipe_format format)
1227 {
1228    switch (format) {
1229    case PIPE_FORMAT_L8_UNORM:
1230       return PIPE_FORMAT_R8_UNORM;
1231    case PIPE_FORMAT_L8_SNORM:
1232       return PIPE_FORMAT_R8_SNORM;
1233    case PIPE_FORMAT_L16_UNORM:
1234       return PIPE_FORMAT_R16_UNORM;
1235    case PIPE_FORMAT_L16_SNORM:
1236       return PIPE_FORMAT_R16_SNORM;
1237    case PIPE_FORMAT_L16_FLOAT:
1238       return PIPE_FORMAT_R16_FLOAT;
1239    case PIPE_FORMAT_L32_FLOAT:
1240       return PIPE_FORMAT_R32_FLOAT;
1241    case PIPE_FORMAT_L8_UINT:
1242       return PIPE_FORMAT_R8_UINT;
1243    case PIPE_FORMAT_L8_SINT:
1244       return PIPE_FORMAT_R8_SINT;
1245    case PIPE_FORMAT_L16_UINT:
1246       return PIPE_FORMAT_R16_UINT;
1247    case PIPE_FORMAT_L16_SINT:
1248       return PIPE_FORMAT_R16_SINT;
1249    case PIPE_FORMAT_L32_UINT:
1250       return PIPE_FORMAT_R32_UINT;
1251    case PIPE_FORMAT_L32_SINT:
1252       return PIPE_FORMAT_R32_SINT;
1253 
1254    case PIPE_FORMAT_LATC1_UNORM:
1255       return PIPE_FORMAT_RGTC1_UNORM;
1256    case PIPE_FORMAT_LATC1_SNORM:
1257       return PIPE_FORMAT_RGTC1_SNORM;
1258 
1259    case PIPE_FORMAT_L4A4_UNORM:
1260       return PIPE_FORMAT_R4A4_UNORM;
1261 
1262    case PIPE_FORMAT_L8A8_UNORM:
1263       return PIPE_FORMAT_R8A8_UNORM;
1264    case PIPE_FORMAT_L8A8_SNORM:
1265       return PIPE_FORMAT_R8A8_SNORM;
1266    case PIPE_FORMAT_L16A16_UNORM:
1267       return PIPE_FORMAT_R16A16_UNORM;
1268    case PIPE_FORMAT_L16A16_SNORM:
1269       return PIPE_FORMAT_R16A16_SNORM;
1270    case PIPE_FORMAT_L16A16_FLOAT:
1271       return PIPE_FORMAT_R16A16_FLOAT;
1272    case PIPE_FORMAT_L32A32_FLOAT:
1273       return PIPE_FORMAT_R32A32_FLOAT;
1274    case PIPE_FORMAT_L8A8_UINT:
1275       return PIPE_FORMAT_R8A8_UINT;
1276    case PIPE_FORMAT_L8A8_SINT:
1277       return PIPE_FORMAT_R8A8_SINT;
1278    case PIPE_FORMAT_L16A16_UINT:
1279       return PIPE_FORMAT_R16A16_UINT;
1280    case PIPE_FORMAT_L16A16_SINT:
1281       return PIPE_FORMAT_R16A16_SINT;
1282    case PIPE_FORMAT_L32A32_UINT:
1283       return PIPE_FORMAT_R32A32_UINT;
1284    case PIPE_FORMAT_L32A32_SINT:
1285       return PIPE_FORMAT_R32A32_SINT;
1286 
1287    /* We don't have compressed red-alpha variants for these. */
1288    case PIPE_FORMAT_LATC2_UNORM:
1289    case PIPE_FORMAT_LATC2_SNORM:
1290       return PIPE_FORMAT_NONE;
1291 
1292    default:
1293       assert(!util_format_is_luminance(format) &&
1294 	     !util_format_is_luminance_alpha(format));
1295       return format;
1296    }
1297 }
1298 
1299 static inline unsigned
util_format_get_num_planes(enum pipe_format format)1300 util_format_get_num_planes(enum pipe_format format)
1301 {
1302    switch (util_format_description(format)->layout) {
1303    case UTIL_FORMAT_LAYOUT_PLANAR3:
1304       return 3;
1305    case UTIL_FORMAT_LAYOUT_PLANAR2:
1306       return 2;
1307    default:
1308       return 1;
1309    }
1310 }
1311 
1312 static inline enum pipe_format
util_format_get_plane_format(enum pipe_format format,unsigned plane)1313 util_format_get_plane_format(enum pipe_format format, unsigned plane)
1314 {
1315    switch (format) {
1316    case PIPE_FORMAT_YV12:
1317    case PIPE_FORMAT_YV16:
1318    case PIPE_FORMAT_IYUV:
1319    case PIPE_FORMAT_Y8_U8_V8_422_UNORM:
1320    case PIPE_FORMAT_Y8_U8_V8_444_UNORM:
1321       return PIPE_FORMAT_R8_UNORM;
1322    case PIPE_FORMAT_NV12:
1323    case PIPE_FORMAT_Y8_U8V8_422_UNORM:
1324       return !plane ? PIPE_FORMAT_R8_UNORM : PIPE_FORMAT_RG88_UNORM;
1325    case PIPE_FORMAT_NV21:
1326       return !plane ? PIPE_FORMAT_R8_UNORM : PIPE_FORMAT_GR88_UNORM;
1327    case PIPE_FORMAT_Y16_U16_V16_420_UNORM:
1328    case PIPE_FORMAT_Y16_U16_V16_422_UNORM:
1329    case PIPE_FORMAT_Y16_U16_V16_444_UNORM:
1330       return PIPE_FORMAT_R16_UNORM;
1331    case PIPE_FORMAT_P010:
1332    case PIPE_FORMAT_P016:
1333    case PIPE_FORMAT_Y16_U16V16_422_UNORM:
1334       return !plane ? PIPE_FORMAT_R16_UNORM : PIPE_FORMAT_R16G16_UNORM;
1335    default:
1336       return format;
1337    }
1338 }
1339 
1340 static inline unsigned
util_format_get_plane_width(enum pipe_format format,unsigned plane,unsigned width)1341 util_format_get_plane_width(enum pipe_format format, unsigned plane,
1342                             unsigned width)
1343 {
1344    switch (format) {
1345    case PIPE_FORMAT_YV12:
1346    case PIPE_FORMAT_YV16:
1347    case PIPE_FORMAT_IYUV:
1348    case PIPE_FORMAT_NV12:
1349    case PIPE_FORMAT_NV21:
1350    case PIPE_FORMAT_P010:
1351    case PIPE_FORMAT_P016:
1352    case PIPE_FORMAT_Y8_U8_V8_422_UNORM:
1353    case PIPE_FORMAT_Y8_U8V8_422_UNORM:
1354    case PIPE_FORMAT_Y16_U16_V16_420_UNORM:
1355    case PIPE_FORMAT_Y16_U16_V16_422_UNORM:
1356    case PIPE_FORMAT_Y16_U16V16_422_UNORM:
1357       return !plane ? width : (width + 1) / 2;
1358    default:
1359       return width;
1360    }
1361 }
1362 
1363 static inline unsigned
util_format_get_plane_height(enum pipe_format format,unsigned plane,unsigned height)1364 util_format_get_plane_height(enum pipe_format format, unsigned plane,
1365                              unsigned height)
1366 {
1367    switch (format) {
1368    case PIPE_FORMAT_YV12:
1369    case PIPE_FORMAT_IYUV:
1370    case PIPE_FORMAT_NV12:
1371    case PIPE_FORMAT_NV21:
1372    case PIPE_FORMAT_P010:
1373    case PIPE_FORMAT_P016:
1374    case PIPE_FORMAT_Y16_U16_V16_420_UNORM:
1375       return !plane ? height : (height + 1) / 2;
1376    case PIPE_FORMAT_YV16:
1377    default:
1378       return height;
1379    }
1380 }
1381 
1382 /**
1383  * Return the number of components stored.
1384  * Formats with block size != 1x1 will always have 1 component (the block).
1385  */
1386 static inline unsigned
util_format_get_nr_components(enum pipe_format format)1387 util_format_get_nr_components(enum pipe_format format)
1388 {
1389    const struct util_format_description *desc = util_format_description(format);
1390    return desc->nr_channels;
1391 }
1392 
1393 /**
1394  * Return the index of the first non-void channel
1395  * -1 if no non-void channels
1396  */
1397 static inline int
util_format_get_first_non_void_channel(enum pipe_format format)1398 util_format_get_first_non_void_channel(enum pipe_format format)
1399 {
1400    const struct util_format_description *desc = util_format_description(format);
1401    int i;
1402 
1403    for (i = 0; i < 4; i++)
1404       if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID)
1405          break;
1406 
1407    if (i == 4)
1408        return -1;
1409 
1410    return i;
1411 }
1412 
1413 /**
1414  * Whether this format is any 8-bit UNORM variant. Looser than
1415  * util_is_rgba8_variant (also includes alpha textures, for instance).
1416  */
1417 
1418 static inline bool
util_format_is_unorm8(const struct util_format_description * desc)1419 util_format_is_unorm8(const struct util_format_description *desc)
1420 {
1421    int c = util_format_get_first_non_void_channel(desc->format);
1422 
1423    if (c == -1)
1424       return false;
1425 
1426    return desc->is_unorm && desc->is_array && desc->channel[c].size == 8;
1427 }
1428 
1429 static inline void
util_format_unpack_z_float(enum pipe_format format,float * dst,const void * src,unsigned w)1430 util_format_unpack_z_float(enum pipe_format format, float *dst,
1431                            const void *src, unsigned w)
1432 {
1433    const struct util_format_description *desc = util_format_description(format);
1434 
1435    desc->unpack_z_float(dst, 0, (const uint8_t *)src, 0, w, 1);
1436 }
1437 
1438 static inline void
util_format_unpack_z_32unorm(enum pipe_format format,uint32_t * dst,const void * src,unsigned w)1439 util_format_unpack_z_32unorm(enum pipe_format format, uint32_t *dst,
1440                              const void *src, unsigned w)
1441 {
1442    const struct util_format_description *desc = util_format_description(format);
1443 
1444    desc->unpack_z_32unorm(dst, 0, (const uint8_t *)src, 0, w, 1);
1445 }
1446 
1447 static inline void
util_format_unpack_s_8uint(enum pipe_format format,uint8_t * dst,const void * src,unsigned w)1448 util_format_unpack_s_8uint(enum pipe_format format, uint8_t *dst,
1449                            const void *src, unsigned w)
1450 {
1451    const struct util_format_description *desc = util_format_description(format);
1452 
1453    desc->unpack_s_8uint(dst, 0, (const uint8_t *)src, 0, w, 1);
1454 }
1455 
1456 /**
1457  * Unpacks a row of color data to 32-bit RGBA, either integers for pure
1458  * integer formats (sign-extended for signed data), or 32-bit floats.
1459  */
1460 static inline void
util_format_unpack_rgba(enum pipe_format format,void * dst,const void * src,unsigned w)1461 util_format_unpack_rgba(enum pipe_format format, void *dst,
1462                         const void *src, unsigned w)
1463 {
1464    const struct util_format_description *desc = util_format_description(format);
1465 
1466    desc->unpack_rgba(dst, 0, (const uint8_t *)src, 0, w, 1);
1467 }
1468 
1469 static inline void
util_format_pack_z_float(enum pipe_format format,void * dst,const float * src,unsigned w)1470 util_format_pack_z_float(enum pipe_format format, void *dst,
1471                          const float *src, unsigned w)
1472 {
1473    const struct util_format_description *desc = util_format_description(format);
1474 
1475    desc->pack_z_float((uint8_t *)dst, 0, src, 0, w, 1);
1476 }
1477 
1478 static inline void
util_format_pack_z_32unorm(enum pipe_format format,void * dst,const uint32_t * src,unsigned w)1479 util_format_pack_z_32unorm(enum pipe_format format, void *dst,
1480                            const uint32_t *src, unsigned w)
1481 {
1482    const struct util_format_description *desc = util_format_description(format);
1483 
1484    desc->pack_z_32unorm((uint8_t *)dst, 0, src, 0, w, 1);
1485 }
1486 
1487 static inline void
util_format_pack_s_8uint(enum pipe_format format,void * dst,const uint8_t * src,unsigned w)1488 util_format_pack_s_8uint(enum pipe_format format, void *dst,
1489                          const uint8_t *src, unsigned w)
1490 {
1491    const struct util_format_description *desc = util_format_description(format);
1492 
1493    desc->pack_s_8uint((uint8_t *)dst, 0, src, 0, w, 1);
1494 }
1495 
1496 /**
1497  * Packs a row of color data from 32-bit RGBA, either integers for pure
1498  * integer formats, or 32-bit floats.  Values are clamped to the packed
1499  * representation's range.
1500  */
1501 static inline void
util_format_pack_rgba(enum pipe_format format,void * dst,const void * src,unsigned w)1502 util_format_pack_rgba(enum pipe_format format, void *dst,
1503                         const void *src, unsigned w)
1504 {
1505    const struct util_format_description *desc = util_format_description(format);
1506 
1507    if (util_format_is_pure_uint(format))
1508       desc->pack_rgba_uint((uint8_t *)dst, 0, (const uint32_t *)src, 0, w, 1);
1509    else if (util_format_is_pure_sint(format))
1510       desc->pack_rgba_sint((uint8_t *)dst, 0, (const int32_t *)src, 0, w, 1);
1511    else
1512       desc->pack_rgba_float((uint8_t *)dst, 0, (const float *)src, 0, w, 1);
1513 }
1514 
1515 /*
1516  * Format access functions for subrectangles
1517  */
1518 
1519 void
1520 util_format_read_4(enum pipe_format format,
1521                    void *dst, unsigned dst_stride,
1522                    const void *src, unsigned src_stride,
1523                    unsigned x, unsigned y, unsigned w, unsigned h);
1524 
1525 void
1526 util_format_write_4(enum pipe_format format,
1527                     const void *src, unsigned src_stride,
1528                     void *dst, unsigned dst_stride,
1529                     unsigned x, unsigned y, unsigned w, unsigned h);
1530 
1531 void
1532 util_format_read_4ub(enum pipe_format format,
1533                      uint8_t *dst, unsigned dst_stride,
1534                      const void *src, unsigned src_stride,
1535                      unsigned x, unsigned y, unsigned w, unsigned h);
1536 
1537 void
1538 util_format_write_4ub(enum pipe_format format,
1539                       const uint8_t *src, unsigned src_stride,
1540                       void *dst, unsigned dst_stride,
1541                       unsigned x, unsigned y, unsigned w, unsigned h);
1542 
1543 /*
1544  * Generic format conversion;
1545  */
1546 
1547 boolean
1548 util_format_fits_8unorm(const struct util_format_description *format_desc);
1549 
1550 boolean
1551 util_format_translate(enum pipe_format dst_format,
1552                       void *dst, unsigned dst_stride,
1553                       unsigned dst_x, unsigned dst_y,
1554                       enum pipe_format src_format,
1555                       const void *src, unsigned src_stride,
1556                       unsigned src_x, unsigned src_y,
1557                       unsigned width, unsigned height);
1558 
1559 boolean
1560 util_format_translate_3d(enum pipe_format dst_format,
1561                          void *dst, unsigned dst_stride,
1562                          unsigned dst_slice_stride,
1563                          unsigned dst_x, unsigned dst_y,
1564                          unsigned dst_z,
1565                          enum pipe_format src_format,
1566                          const void *src, unsigned src_stride,
1567                          unsigned src_slice_stride,
1568                          unsigned src_x, unsigned src_y,
1569                          unsigned src_z, unsigned width,
1570                          unsigned height, unsigned depth);
1571 
1572 /*
1573  * Swizzle operations.
1574  */
1575 
1576 /* Compose two sets of swizzles.
1577  * If V is a 4D vector and the function parameters represent functions that
1578  * swizzle vector components, this holds:
1579  *     swz2(swz1(V)) = dst(V)
1580  */
1581 void util_format_compose_swizzles(const unsigned char swz1[4],
1582                                   const unsigned char swz2[4],
1583                                   unsigned char dst[4]);
1584 
1585 /* Apply the swizzle provided in \param swz (which is one of PIPE_SWIZZLE_x)
1586  * to \param src and store the result in \param dst.
1587  * \param is_integer determines the value written for PIPE_SWIZZLE_1.
1588  */
1589 void util_format_apply_color_swizzle(union pipe_color_union *dst,
1590                                      const union pipe_color_union *src,
1591                                      const unsigned char swz[4],
1592                                      const boolean is_integer);
1593 
1594 void pipe_swizzle_4f(float *dst, const float *src,
1595                             const unsigned char swz[4]);
1596 
1597 void util_format_unswizzle_4f(float *dst, const float *src,
1598                               const unsigned char swz[4]);
1599 
1600 enum pipe_format
1601 util_format_snorm8_to_sint8(enum pipe_format format);
1602 
1603 
1604 extern void
1605 util_copy_rect(ubyte * dst, enum pipe_format format,
1606                unsigned dst_stride, unsigned dst_x, unsigned dst_y,
1607                unsigned width, unsigned height, const ubyte * src,
1608                int src_stride, unsigned src_x, unsigned src_y);
1609 
1610 #ifdef __cplusplus
1611 } // extern "C" {
1612 #endif
1613 
1614 #endif /* ! U_FORMAT_H */
1615