u_format.h revision cceeab39ea541b1be1521114316d660a77769c2a
1/**************************************************************************
2 *
3 * Copyright 2009 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
35
36enum util_format_layout {
37   UTIL_FORMAT_LAYOUT_SCALAR = 0,
38   UTIL_FORMAT_LAYOUT_ARITH = 1,
39   UTIL_FORMAT_LAYOUT_ARRAY = 2,
40   UTIL_FORMAT_LAYOUT_YUV = 3,
41   UTIL_FORMAT_LAYOUT_DXT = 4
42};
43
44
45struct util_format_block
46{
47   /** Block width in pixels */
48   unsigned width;
49
50   /** Block height in pixels */
51   unsigned height;
52
53   /** Block size in bits */
54   unsigned bits;
55};
56
57
58enum util_format_type {
59   UTIL_FORMAT_TYPE_VOID = 0,
60   UTIL_FORMAT_TYPE_UNSIGNED = 1,
61   UTIL_FORMAT_TYPE_SIGNED = 2,
62   UTIL_FORMAT_TYPE_FIXED = 3,
63   UTIL_FORMAT_TYPE_FLOAT = 4
64};
65
66
67enum util_format_swizzle {
68   UTIL_FORMAT_SWIZZLE_X = 0,
69   UTIL_FORMAT_SWIZZLE_Y = 1,
70   UTIL_FORMAT_SWIZZLE_Z = 2,
71   UTIL_FORMAT_SWIZZLE_W = 3,
72   UTIL_FORMAT_SWIZZLE_0 = 4,
73   UTIL_FORMAT_SWIZZLE_1 = 5,
74   UTIL_FORMAT_SWIZZLE_NONE = 6
75};
76
77
78enum util_format_colorspace {
79   UTIL_FORMAT_COLORSPACE_RGB = 0,
80   UTIL_FORMAT_COLORSPACE_SRGB = 1,
81   UTIL_FORMAT_COLORSPACE_YUV = 2,
82   UTIL_FORMAT_COLORSPACE_ZS = 3,
83};
84
85
86struct util_format_channel_description
87{
88   unsigned type:6;
89   unsigned normalized:1;
90   unsigned size:9;
91};
92
93
94struct util_format_description
95{
96   enum pipe_format format;
97   const char *name;
98   struct util_format_block block;
99   enum util_format_layout layout;
100   struct util_format_channel_description channel[4];
101   unsigned char swizzle[4];
102   enum util_format_colorspace colorspace;
103};
104
105
106extern const struct util_format_description
107util_format_description_table[];
108
109
110const struct util_format_description *
111util_format_description(enum pipe_format format);
112
113
114/*
115 * Format query functions.
116 */
117
118static INLINE boolean
119util_format_is_compressed(enum pipe_format format)
120{
121   const struct util_format_description *desc = util_format_description(format);
122
123   assert(format);
124   if (!format) {
125      return FALSE;
126   }
127
128   return desc->layout == UTIL_FORMAT_LAYOUT_DXT ? TRUE : FALSE;
129}
130
131static INLINE boolean
132util_format_is_depth_or_stencil(enum pipe_format format)
133{
134   const struct util_format_description *desc = util_format_description(format);
135
136   assert(format);
137   if (!format) {
138      return FALSE;
139   }
140
141   return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS ? TRUE : FALSE;
142}
143
144static INLINE boolean
145util_format_is_depth_and_stencil(enum pipe_format format)
146{
147   const struct util_format_description *desc = util_format_description(format);
148
149   assert(format);
150   if (!format) {
151      return FALSE;
152   }
153
154   if (desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS) {
155      return FALSE;
156   }
157
158   return (desc->swizzle[0] != UTIL_FORMAT_SWIZZLE_NONE &&
159           desc->swizzle[1] != UTIL_FORMAT_SWIZZLE_NONE) ? TRUE : FALSE;
160}
161
162/**
163 * Describe pixel format's block.
164 *
165 * @sa http://msdn2.microsoft.com/en-us/library/ms796147.aspx
166 */
167static INLINE void
168util_format_get_block(enum pipe_format format,
169                      struct pipe_format_block *block)
170{
171   const struct util_format_description *desc = util_format_description(format);
172
173   assert(format);
174   if (!format) {
175      block->size = 0;
176      block->width = 1;
177      block->height = 1;
178      return;
179   }
180
181   block->size = desc->block.bits / 8;
182   block->width = desc->block.width;
183   block->height = desc->block.height;
184}
185
186/**
187 * Return total bits needed for the pixel format.
188 */
189static INLINE uint
190util_format_get_bits(enum pipe_format format)
191{
192   const struct util_format_description *desc = util_format_description(format);
193
194   assert(format);
195   if (!format) {
196      return 0;
197   }
198
199   return desc->block.bits / (desc->block.width * desc->block.height);
200}
201
202/**
203 * Return bytes per pixel for the given format.
204 */
205static INLINE uint
206util_format_get_size(enum pipe_format format)
207{
208   uint bits = util_format_get_bits(format);
209
210   assert(bits % 8 == 0);
211
212   return bits / 8;
213}
214
215
216/*
217 * Format access functions.
218 */
219
220void
221util_format_read_4f(enum pipe_format format,
222                    float *dst, unsigned dst_stride,
223                    const void *src, unsigned src_stride,
224                    unsigned x, unsigned y, unsigned w, unsigned h);
225
226void
227util_format_write_4f(enum pipe_format format,
228                     const float *src, unsigned src_stride,
229                     void *dst, unsigned dst_stride,
230                     unsigned x, unsigned y, unsigned w, unsigned h);
231
232void
233util_format_read_4ub(enum pipe_format format,
234                     uint8_t *dst, unsigned dst_stride,
235                     const void *src, unsigned src_stride,
236                     unsigned x, unsigned y, unsigned w, unsigned h);
237
238void
239util_format_write_4ub(enum pipe_format format,
240                      const uint8_t *src, unsigned src_stride,
241                      void *dst, unsigned dst_stride,
242                      unsigned x, unsigned y, unsigned w, unsigned h);
243
244#endif /* ! U_FORMAT_H */
245