u_format.h revision 0b0f4628d6fb8276a9f1c336a785a838b602bca8
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 "util/u_debug.h"
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40
41/**
42 * Describe how to pack/unpack pixels into/from the prescribed format.
43 *
44 * XXX: This could be renamed to something like util_format_pack, or broke down
45 * in flags inside util_format_block that said exactly what we want.
46 */
47enum util_format_layout {
48   /**
49    * Formats with util_format_block::width == util_format_block::height == 1
50    * that can be described as an ordinary data structure.
51    */
52   UTIL_FORMAT_LAYOUT_PLAIN = 0,
53
54   /**
55    * Formats with sub-sampled channels.
56    *
57    * This is for formats like YVYU where there is less than one sample per
58    * pixel.
59    */
60   UTIL_FORMAT_LAYOUT_SUBSAMPLED = 3,
61
62   /**
63    * S3 Texture Compression formats.
64    */
65   UTIL_FORMAT_LAYOUT_S3TC = 4,
66
67   /**
68    * Red-Green Texture Compression formats.
69    */
70   UTIL_FORMAT_LAYOUT_RGTC = 5,
71
72   /**
73    * Ericsson Texture Compression
74    */
75   UTIL_FORMAT_LAYOUT_ETC = 6,
76
77   /**
78    * Everything else that doesn't fit in any of the above layouts.
79    */
80   UTIL_FORMAT_LAYOUT_OTHER = 7
81};
82
83
84struct util_format_block
85{
86   /** Block width in pixels */
87   unsigned width;
88
89   /** Block height in pixels */
90   unsigned height;
91
92   /** Block size in bits */
93   unsigned bits;
94};
95
96
97enum util_format_type {
98   UTIL_FORMAT_TYPE_VOID = 0,
99   UTIL_FORMAT_TYPE_UNSIGNED = 1,
100   UTIL_FORMAT_TYPE_SIGNED = 2,
101   UTIL_FORMAT_TYPE_FIXED = 3,
102   UTIL_FORMAT_TYPE_FLOAT = 4
103};
104
105
106enum util_format_swizzle {
107   UTIL_FORMAT_SWIZZLE_X = 0,
108   UTIL_FORMAT_SWIZZLE_Y = 1,
109   UTIL_FORMAT_SWIZZLE_Z = 2,
110   UTIL_FORMAT_SWIZZLE_W = 3,
111   UTIL_FORMAT_SWIZZLE_0 = 4,
112   UTIL_FORMAT_SWIZZLE_1 = 5,
113   UTIL_FORMAT_SWIZZLE_NONE = 6,
114   UTIL_FORMAT_SWIZZLE_MAX = 7  /**< Number of enums counter (must be last) */
115};
116
117
118enum 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
123};
124
125
126struct util_format_channel_description
127{
128   unsigned type:5;        /**< UTIL_FORMAT_TYPE_x */
129   unsigned normalized:1;
130   unsigned pure_integer:1;
131   unsigned size:9;        /**< bits per channel */
132};
133
134
135struct util_format_description
136{
137   enum pipe_format format;
138
139   const char *name;
140
141   /**
142    * Short name, striped of the prefix, lower case.
143    */
144   const char *short_name;
145
146   /**
147    * Pixel block dimensions.
148    */
149   struct util_format_block block;
150
151   enum util_format_layout layout;
152
153   /**
154    * The number of channels.
155    */
156   unsigned nr_channels:3;
157
158   /**
159    * Whether all channels have the same number of (whole) bytes.
160    */
161   unsigned is_array:1;
162
163   /**
164    * Whether the pixel format can be described as a bitfield structure.
165    *
166    * In particular:
167    * - pixel depth must be 8, 16, or 32 bits;
168    * - all channels must be unsigned, signed, or void
169    */
170   unsigned is_bitmask:1;
171
172   /**
173    * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
174    */
175   unsigned is_mixed:1;
176
177   /**
178    * Input channel description.
179    *
180    * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
181    */
182   struct util_format_channel_description channel[4];
183
184   /**
185    * Output channel swizzle.
186    *
187    * The order is either:
188    * - RGBA
189    * - YUV(A)
190    * - ZS
191    * depending on the colorspace.
192    */
193   unsigned char swizzle[4];
194
195   /**
196    * Colorspace transformation.
197    */
198   enum util_format_colorspace colorspace;
199
200   /**
201    * Unpack pixel blocks to R8G8B8A8_UNORM.
202    * Note: strides are in bytes.
203    *
204    * Only defined for non-depth-stencil formats.
205    */
206   void
207   (*unpack_rgba_8unorm)(uint8_t *dst, unsigned dst_stride,
208                         const uint8_t *src, unsigned src_stride,
209                         unsigned width, unsigned height);
210
211   /**
212    * Pack pixel blocks from R8G8B8A8_UNORM.
213    * Note: strides are in bytes.
214    *
215    * Only defined for non-depth-stencil formats.
216    */
217   void
218   (*pack_rgba_8unorm)(uint8_t *dst, unsigned dst_stride,
219                       const uint8_t *src, unsigned src_stride,
220                       unsigned width, unsigned height);
221
222   /**
223    * Fetch a single pixel (i, j) from a block.
224    *
225    * XXX: Only defined for a very few select formats.
226    */
227   void
228   (*fetch_rgba_8unorm)(uint8_t *dst,
229                        const uint8_t *src,
230                        unsigned i, unsigned j);
231
232   /**
233    * Unpack pixel blocks to R32G32B32A32_FLOAT.
234    * Note: strides are in bytes.
235    *
236    * Only defined for non-depth-stencil formats.
237    */
238   void
239   (*unpack_rgba_float)(float *dst, unsigned dst_stride,
240                        const uint8_t *src, unsigned src_stride,
241                        unsigned width, unsigned height);
242
243   /**
244    * Pack pixel blocks from R32G32B32A32_FLOAT.
245    * Note: strides are in bytes.
246    *
247    * Only defined for non-depth-stencil formats.
248    */
249   void
250   (*pack_rgba_float)(uint8_t *dst, unsigned dst_stride,
251                      const float *src, unsigned src_stride,
252                      unsigned width, unsigned height);
253
254   /**
255    * Fetch a single pixel (i, j) from a block.
256    *
257    * Only defined for non-depth-stencil and non-integer formats.
258    */
259   void
260   (*fetch_rgba_float)(float *dst,
261                       const uint8_t *src,
262                       unsigned i, unsigned j);
263
264   /**
265    * Unpack pixels to Z32_UNORM.
266    * Note: strides are in bytes.
267    *
268    * Only defined for depth formats.
269    */
270   void
271   (*unpack_z_32unorm)(uint32_t *dst, unsigned dst_stride,
272                       const uint8_t *src, unsigned src_stride,
273                       unsigned width, unsigned height);
274
275   /**
276    * Pack pixels from Z32_FLOAT.
277    * Note: strides are in bytes.
278    *
279    * Only defined for depth formats.
280    */
281   void
282   (*pack_z_32unorm)(uint8_t *dst, unsigned dst_stride,
283                     const uint32_t *src, unsigned src_stride,
284                     unsigned width, unsigned height);
285
286   /**
287    * Unpack pixels to Z32_FLOAT.
288    * Note: strides are in bytes.
289    *
290    * Only defined for depth formats.
291    */
292   void
293   (*unpack_z_float)(float *dst, unsigned dst_stride,
294                     const uint8_t *src, unsigned src_stride,
295                     unsigned width, unsigned height);
296
297   /**
298    * Pack pixels from Z32_FLOAT.
299    * Note: strides are in bytes.
300    *
301    * Only defined for depth formats.
302    */
303   void
304   (*pack_z_float)(uint8_t *dst, unsigned dst_stride,
305                   const float *src, unsigned src_stride,
306                   unsigned width, unsigned height);
307
308   /**
309    * Unpack pixels to S8_UINT.
310    * Note: strides are in bytes.
311    *
312    * Only defined for stencil formats.
313    */
314   void
315   (*unpack_s_8uint)(uint8_t *dst, unsigned dst_stride,
316                     const uint8_t *src, unsigned src_stride,
317                     unsigned width, unsigned height);
318
319   /**
320    * Pack pixels from S8_UINT.
321    * Note: strides are in bytes.
322    *
323    * Only defined for stencil formats.
324    */
325   void
326   (*pack_s_8uint)(uint8_t *dst, unsigned dst_stride,
327                   const uint8_t *src, unsigned src_stride,
328                   unsigned width, unsigned height);
329
330  /**
331    * Unpack pixel blocks to R32G32B32A32_UINT.
332    * Note: strides are in bytes.
333    *
334    * Only defined for INT formats.
335    */
336   void
337   (*unpack_rgba_uint)(unsigned *dst, unsigned dst_stride,
338                       const uint8_t *src, unsigned src_stride,
339                       unsigned width, unsigned height);
340
341   void
342   (*pack_rgba_uint)(uint8_t *dst, unsigned dst_stride,
343                     const unsigned *src, unsigned src_stride,
344                     unsigned width, unsigned height);
345
346  /**
347    * Unpack pixel blocks to R32G32B32A32_SINT.
348    * Note: strides are in bytes.
349    *
350    * Only defined for INT formats.
351    */
352   void
353   (*unpack_rgba_sint)(signed *dst, unsigned dst_stride,
354                       const uint8_t *src, unsigned src_stride,
355                       unsigned width, unsigned height);
356
357   void
358   (*pack_rgba_sint)(uint8_t *dst, unsigned dst_stride,
359                     const int *src, unsigned src_stride,
360                     unsigned width, unsigned height);
361
362   /**
363    * Fetch a single pixel (i, j) from a block.
364    *
365    * Only defined for unsigned (pure) integer formats.
366    */
367   void
368   (*fetch_rgba_uint)(uint32_t *dst,
369                      const uint8_t *src,
370                      unsigned i, unsigned j);
371
372   /**
373    * Fetch a single pixel (i, j) from a block.
374    *
375    * Only defined for signed (pure) integer formats.
376    */
377   void
378   (*fetch_rgba_sint)(int32_t *dst,
379                      const uint8_t *src,
380                      unsigned i, unsigned j);
381};
382
383
384extern const struct util_format_description
385util_format_description_table[];
386
387
388const struct util_format_description *
389util_format_description(enum pipe_format format);
390
391
392/*
393 * Format query functions.
394 */
395
396static INLINE const char *
397util_format_name(enum pipe_format format)
398{
399   const struct util_format_description *desc = util_format_description(format);
400
401   assert(desc);
402   if (!desc) {
403      return "PIPE_FORMAT_???";
404   }
405
406   return desc->name;
407}
408
409static INLINE const char *
410util_format_short_name(enum pipe_format format)
411{
412   const struct util_format_description *desc = util_format_description(format);
413
414   assert(desc);
415   if (!desc) {
416      return "???";
417   }
418
419   return desc->short_name;
420}
421
422/**
423 * Whether this format is plain, see UTIL_FORMAT_LAYOUT_PLAIN for more info.
424 */
425static INLINE boolean
426util_format_is_plain(enum pipe_format format)
427{
428   const struct util_format_description *desc = util_format_description(format);
429
430   if (!format) {
431      return FALSE;
432   }
433
434   return desc->layout == UTIL_FORMAT_LAYOUT_PLAIN ? TRUE : FALSE;
435}
436
437static INLINE boolean
438util_format_is_compressed(enum pipe_format format)
439{
440   const struct util_format_description *desc = util_format_description(format);
441
442   assert(desc);
443   if (!desc) {
444      return FALSE;
445   }
446
447   switch (desc->layout) {
448   case UTIL_FORMAT_LAYOUT_S3TC:
449   case UTIL_FORMAT_LAYOUT_RGTC:
450      /* XXX add other formats in the future */
451      return TRUE;
452   default:
453      return FALSE;
454   }
455}
456
457static INLINE boolean
458util_format_is_s3tc(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   return desc->layout == UTIL_FORMAT_LAYOUT_S3TC ? TRUE : FALSE;
468}
469
470static INLINE boolean
471util_format_is_srgb(enum pipe_format format)
472{
473   const struct util_format_description *desc = util_format_description(format);
474   return desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB;
475}
476
477static INLINE boolean
478util_format_has_depth(const struct util_format_description *desc)
479{
480   return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS &&
481          desc->swizzle[0] != UTIL_FORMAT_SWIZZLE_NONE;
482}
483
484static INLINE boolean
485util_format_has_stencil(const struct util_format_description *desc)
486{
487   return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS &&
488          desc->swizzle[1] != UTIL_FORMAT_SWIZZLE_NONE;
489}
490
491static INLINE boolean
492util_format_is_depth_or_stencil(enum pipe_format format)
493{
494   const struct util_format_description *desc = util_format_description(format);
495
496   assert(desc);
497   if (!desc) {
498      return FALSE;
499   }
500
501   return util_format_has_depth(desc) ||
502          util_format_has_stencil(desc);
503}
504
505static INLINE boolean
506util_format_is_depth_and_stencil(enum pipe_format format)
507{
508   const struct util_format_description *desc = util_format_description(format);
509
510   assert(desc);
511   if (!desc) {
512      return FALSE;
513   }
514
515   return util_format_has_depth(desc) &&
516          util_format_has_stencil(desc);
517}
518
519
520/**
521 * Give the RGBA colormask of the channels that can be represented in this
522 * format.
523 *
524 * That is, the channels whose values are preserved.
525 */
526static INLINE unsigned
527util_format_colormask(const struct util_format_description *desc)
528{
529   unsigned colormask;
530   unsigned chan;
531
532   switch (desc->colorspace) {
533   case UTIL_FORMAT_COLORSPACE_RGB:
534   case UTIL_FORMAT_COLORSPACE_SRGB:
535   case UTIL_FORMAT_COLORSPACE_YUV:
536      colormask = 0;
537      for (chan = 0; chan < 4; ++chan) {
538         if (desc->swizzle[chan] < 4) {
539            colormask |= (1 << chan);
540         }
541      }
542      return colormask;
543   case UTIL_FORMAT_COLORSPACE_ZS:
544      return 0;
545   default:
546      assert(0);
547      return 0;
548   }
549}
550
551
552/**
553 * Checks if color mask covers every channel for the specified format
554 *
555 * @param desc       a format description to check colormask with
556 * @param colormask  a bit mask for channels, matches format of PIPE_MASK_RGBA
557 */
558static INLINE boolean
559util_format_colormask_full(const struct util_format_description *desc, unsigned colormask)
560{
561   return (~colormask & util_format_colormask(desc)) == 0;
562}
563
564
565boolean
566util_format_is_float(enum pipe_format format);
567
568
569boolean
570util_format_is_rgb_no_alpha(enum pipe_format format);
571
572
573boolean
574util_format_is_luminance(enum pipe_format format);
575
576
577boolean
578util_format_is_luminance_alpha(enum pipe_format format);
579
580
581boolean
582util_format_is_intensity(enum pipe_format format);
583
584boolean
585util_format_is_pure_integer(enum pipe_format format);
586
587boolean
588util_format_is_pure_sint(enum pipe_format format);
589
590boolean
591util_format_is_pure_uint(enum pipe_format format);
592
593/**
594 * Check if the src format can be blitted to the destination format with
595 * a simple memcpy.  For example, blitting from RGBA to RGBx is OK, but not
596 * the reverse.
597 */
598boolean
599util_is_format_compatible(const struct util_format_description *src_desc,
600                          const struct util_format_description *dst_desc);
601
602/**
603 * Whether the format is supported by Gallium for the given bindings.
604 * This covers S3TC textures and floating-point render targets.
605 */
606boolean
607util_format_is_supported(enum pipe_format format, unsigned bind);
608
609/**
610 * Whether this format is a rgab8 variant.
611 *
612 * That is, any format that matches the
613 *
614 *   PIPE_FORMAT_?8?8?8?8_UNORM
615 */
616static INLINE boolean
617util_format_is_rgba8_variant(const struct util_format_description *desc)
618{
619   unsigned chan;
620
621   if(desc->block.width != 1 ||
622      desc->block.height != 1 ||
623      desc->block.bits != 32)
624      return FALSE;
625
626   for(chan = 0; chan < 4; ++chan) {
627      if(desc->channel[chan].type != UTIL_FORMAT_TYPE_UNSIGNED &&
628         desc->channel[chan].type != UTIL_FORMAT_TYPE_VOID)
629         return FALSE;
630      if(desc->channel[chan].size != 8)
631         return FALSE;
632   }
633
634   return TRUE;
635}
636
637
638/**
639 * Return total bits needed for the pixel format per block.
640 */
641static INLINE uint
642util_format_get_blocksizebits(enum pipe_format format)
643{
644   const struct util_format_description *desc = util_format_description(format);
645
646   assert(desc);
647   if (!desc) {
648      return 0;
649   }
650
651   return desc->block.bits;
652}
653
654/**
655 * Return bytes per block (not pixel) for the given format.
656 */
657static INLINE uint
658util_format_get_blocksize(enum pipe_format format)
659{
660   uint bits = util_format_get_blocksizebits(format);
661
662   assert(bits % 8 == 0);
663
664   return bits / 8;
665}
666
667static INLINE uint
668util_format_get_blockwidth(enum pipe_format format)
669{
670   const struct util_format_description *desc = util_format_description(format);
671
672   assert(desc);
673   if (!desc) {
674      return 1;
675   }
676
677   return desc->block.width;
678}
679
680static INLINE uint
681util_format_get_blockheight(enum pipe_format format)
682{
683   const struct util_format_description *desc = util_format_description(format);
684
685   assert(desc);
686   if (!desc) {
687      return 1;
688   }
689
690   return desc->block.height;
691}
692
693static INLINE unsigned
694util_format_get_nblocksx(enum pipe_format format,
695                         unsigned x)
696{
697   unsigned blockwidth = util_format_get_blockwidth(format);
698   return (x + blockwidth - 1) / blockwidth;
699}
700
701static INLINE unsigned
702util_format_get_nblocksy(enum pipe_format format,
703                         unsigned y)
704{
705   unsigned blockheight = util_format_get_blockheight(format);
706   return (y + blockheight - 1) / blockheight;
707}
708
709static INLINE unsigned
710util_format_get_nblocks(enum pipe_format format,
711                        unsigned width,
712                        unsigned height)
713{
714   return util_format_get_nblocksx(format, width) * util_format_get_nblocksy(format, height);
715}
716
717static INLINE size_t
718util_format_get_stride(enum pipe_format format,
719                       unsigned width)
720{
721   return util_format_get_nblocksx(format, width) * util_format_get_blocksize(format);
722}
723
724static INLINE size_t
725util_format_get_2d_size(enum pipe_format format,
726                        size_t stride,
727                        unsigned height)
728{
729   return util_format_get_nblocksy(format, height) * stride;
730}
731
732static INLINE uint
733util_format_get_component_bits(enum pipe_format format,
734                               enum util_format_colorspace colorspace,
735                               uint component)
736{
737   const struct util_format_description *desc = util_format_description(format);
738   enum util_format_colorspace desc_colorspace;
739
740   assert(format);
741   if (!format) {
742      return 0;
743   }
744
745   assert(component < 4);
746
747   /* Treat RGB and SRGB as equivalent. */
748   if (colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
749      colorspace = UTIL_FORMAT_COLORSPACE_RGB;
750   }
751   if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
752      desc_colorspace = UTIL_FORMAT_COLORSPACE_RGB;
753   } else {
754      desc_colorspace = desc->colorspace;
755   }
756
757   if (desc_colorspace != colorspace) {
758      return 0;
759   }
760
761   switch (desc->swizzle[component]) {
762   case UTIL_FORMAT_SWIZZLE_X:
763      return desc->channel[0].size;
764   case UTIL_FORMAT_SWIZZLE_Y:
765      return desc->channel[1].size;
766   case UTIL_FORMAT_SWIZZLE_Z:
767      return desc->channel[2].size;
768   case UTIL_FORMAT_SWIZZLE_W:
769      return desc->channel[3].size;
770   default:
771      return 0;
772   }
773}
774
775static INLINE boolean
776util_format_has_alpha(enum pipe_format format)
777{
778   const struct util_format_description *desc = util_format_description(format);
779
780   assert(format);
781   if (!format) {
782      return FALSE;
783   }
784
785   switch (desc->colorspace) {
786   case UTIL_FORMAT_COLORSPACE_RGB:
787   case UTIL_FORMAT_COLORSPACE_SRGB:
788      return desc->swizzle[3] != UTIL_FORMAT_SWIZZLE_1;
789   case UTIL_FORMAT_COLORSPACE_YUV:
790      return FALSE;
791   case UTIL_FORMAT_COLORSPACE_ZS:
792      return FALSE;
793   default:
794      assert(0);
795      return FALSE;
796   }
797}
798
799/**
800 * Given a linear RGB colorspace format, return the corresponding SRGB
801 * format, or PIPE_FORMAT_NONE if none.
802 */
803static INLINE enum pipe_format
804util_format_srgb(enum pipe_format format)
805{
806   switch (format) {
807   case PIPE_FORMAT_L8_UNORM:
808      return PIPE_FORMAT_L8_SRGB;
809   case PIPE_FORMAT_L8A8_UNORM:
810      return PIPE_FORMAT_L8A8_SRGB;
811   case PIPE_FORMAT_R8G8B8_UNORM:
812      return PIPE_FORMAT_R8G8B8_SRGB;
813   case PIPE_FORMAT_A8B8G8R8_UNORM:
814      return PIPE_FORMAT_A8B8G8R8_SRGB;
815   case PIPE_FORMAT_X8B8G8R8_UNORM:
816      return PIPE_FORMAT_X8B8G8R8_SRGB;
817   case PIPE_FORMAT_B8G8R8A8_UNORM:
818      return PIPE_FORMAT_B8G8R8A8_SRGB;
819   case PIPE_FORMAT_B8G8R8X8_UNORM:
820      return PIPE_FORMAT_B8G8R8X8_SRGB;
821   case PIPE_FORMAT_A8R8G8B8_UNORM:
822      return PIPE_FORMAT_A8R8G8B8_SRGB;
823   case PIPE_FORMAT_X8R8G8B8_UNORM:
824      return PIPE_FORMAT_X8R8G8B8_SRGB;
825   case PIPE_FORMAT_DXT1_RGB:
826      return PIPE_FORMAT_DXT1_SRGB;
827   case PIPE_FORMAT_DXT1_RGBA:
828      return PIPE_FORMAT_DXT1_SRGBA;
829   case PIPE_FORMAT_DXT3_RGBA:
830      return PIPE_FORMAT_DXT3_SRGBA;
831   case PIPE_FORMAT_DXT5_RGBA:
832      return PIPE_FORMAT_DXT5_SRGBA;
833   default:
834      return PIPE_FORMAT_NONE;
835   }
836}
837
838/**
839 * Given an sRGB format, return the corresponding linear colorspace format.
840 * For non sRGB formats, return the format unchanged.
841 */
842static INLINE enum pipe_format
843util_format_linear(enum pipe_format format)
844{
845   switch (format) {
846   case PIPE_FORMAT_L8_SRGB:
847      return PIPE_FORMAT_L8_UNORM;
848   case PIPE_FORMAT_L8A8_SRGB:
849      return PIPE_FORMAT_L8A8_UNORM;
850   case PIPE_FORMAT_R8G8B8_SRGB:
851      return PIPE_FORMAT_R8G8B8_UNORM;
852   case PIPE_FORMAT_A8B8G8R8_SRGB:
853      return PIPE_FORMAT_A8B8G8R8_UNORM;
854   case PIPE_FORMAT_X8B8G8R8_SRGB:
855      return PIPE_FORMAT_X8B8G8R8_UNORM;
856   case PIPE_FORMAT_B8G8R8A8_SRGB:
857      return PIPE_FORMAT_B8G8R8A8_UNORM;
858   case PIPE_FORMAT_B8G8R8X8_SRGB:
859      return PIPE_FORMAT_B8G8R8X8_UNORM;
860   case PIPE_FORMAT_A8R8G8B8_SRGB:
861      return PIPE_FORMAT_A8R8G8B8_UNORM;
862   case PIPE_FORMAT_X8R8G8B8_SRGB:
863      return PIPE_FORMAT_X8R8G8B8_UNORM;
864   case PIPE_FORMAT_DXT1_SRGB:
865      return PIPE_FORMAT_DXT1_RGB;
866   case PIPE_FORMAT_DXT1_SRGBA:
867      return PIPE_FORMAT_DXT1_RGBA;
868   case PIPE_FORMAT_DXT3_SRGBA:
869      return PIPE_FORMAT_DXT3_RGBA;
870   case PIPE_FORMAT_DXT5_SRGBA:
871      return PIPE_FORMAT_DXT5_RGBA;
872   default:
873      return format;
874   }
875}
876
877/**
878 * Return the number of components stored.
879 * Formats with block size != 1x1 will always have 1 component (the block).
880 */
881static INLINE unsigned
882util_format_get_nr_components(enum pipe_format format)
883{
884   const struct util_format_description *desc = util_format_description(format);
885   return desc->nr_channels;
886}
887
888/**
889 * Return the index of the first non-void channel
890 * -1 if no non-void channels
891 */
892static INLINE int
893util_format_get_first_non_void_channel(enum pipe_format format)
894{
895   const struct util_format_description *desc = util_format_description(format);
896   int i;
897
898   for (i = 0; i < 4; i++)
899      if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID)
900         break;
901
902   if (i == 4)
903       return -1;
904
905   return i;
906}
907
908/*
909 * Format access functions.
910 */
911
912void
913util_format_read_4f(enum pipe_format format,
914                    float *dst, unsigned dst_stride,
915                    const void *src, unsigned src_stride,
916                    unsigned x, unsigned y, unsigned w, unsigned h);
917
918void
919util_format_write_4f(enum pipe_format format,
920                     const float *src, unsigned src_stride,
921                     void *dst, unsigned dst_stride,
922                     unsigned x, unsigned y, unsigned w, unsigned h);
923
924void
925util_format_read_4ub(enum pipe_format format,
926                     uint8_t *dst, unsigned dst_stride,
927                     const void *src, unsigned src_stride,
928                     unsigned x, unsigned y, unsigned w, unsigned h);
929
930void
931util_format_write_4ub(enum pipe_format format,
932                      const uint8_t *src, unsigned src_stride,
933                      void *dst, unsigned dst_stride,
934                      unsigned x, unsigned y, unsigned w, unsigned h);
935
936void
937util_format_read_4ui(enum pipe_format format,
938                     unsigned *dst, unsigned dst_stride,
939                     const void *src, unsigned src_stride,
940                     unsigned x, unsigned y, unsigned w, unsigned h);
941
942void
943util_format_write_4ui(enum pipe_format format,
944                      const unsigned int *src, unsigned src_stride,
945                      void *dst, unsigned dst_stride,
946                      unsigned x, unsigned y, unsigned w, unsigned h);
947
948void
949util_format_read_4i(enum pipe_format format,
950                    int *dst, unsigned dst_stride,
951                    const void *src, unsigned src_stride,
952                    unsigned x, unsigned y, unsigned w, unsigned h);
953
954void
955util_format_write_4i(enum pipe_format format,
956                     const int *src, unsigned src_stride,
957                     void *dst, unsigned dst_stride,
958                     unsigned x, unsigned y, unsigned w, unsigned h);
959
960/*
961 * Generic format conversion;
962 */
963
964boolean
965util_format_fits_8unorm(const struct util_format_description *format_desc);
966
967void
968util_format_translate(enum pipe_format dst_format,
969                      void *dst, unsigned dst_stride,
970                      unsigned dst_x, unsigned dst_y,
971                      enum pipe_format src_format,
972                      const void *src, unsigned src_stride,
973                      unsigned src_x, unsigned src_y,
974                      unsigned width, unsigned height);
975
976/*
977 * Swizzle operations.
978 */
979
980/* Compose two sets of swizzles.
981 * If V is a 4D vector and the function parameters represent functions that
982 * swizzle vector components, this holds:
983 *     swz2(swz1(V)) = dst(V)
984 */
985void util_format_compose_swizzles(const unsigned char swz1[4],
986                                  const unsigned char swz2[4],
987                                  unsigned char dst[4]);
988
989void util_format_swizzle_4f(float *dst, const float *src,
990                            const unsigned char swz[4]);
991
992void util_format_unswizzle_4f(float *dst, const float *src,
993                              const unsigned char swz[4]);
994
995#ifdef __cplusplus
996} // extern "C" {
997#endif
998
999#endif /* ! U_FORMAT_H */
1000