Move the remaining format pf_get_* functions to u_format.h.
[mesa.git] / src / gallium / auxiliary / util / u_format.h
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
36 /**
37 * Describe how to best pack/unpack pixels into/from the prescribed format.
38 *
39 * These are used for automatic code generation of pixel packing and unpacking
40 * routines (in compile time, e.g., u_format_access.py, or in runtime, like
41 * llvmpipe does).
42 *
43 * Thumb rule is: if you're not code generating pixel packing/unpacking then
44 * these are irrelevant for you.
45 *
46 * Note that this can be deduced from other values in util_format_description
47 * structure. This is by design, to make code generation of pixel
48 * packing/unpacking/sampling routines simple and efficient.
49 *
50 * XXX: This should be renamed to something like util_format_pack.
51 */
52 enum util_format_layout {
53 /**
54 * Single scalar component.
55 */
56 UTIL_FORMAT_LAYOUT_SCALAR = 0,
57
58 /**
59 * One or more components of mixed integer formats, arithmetically encoded
60 * in a word up to 32bits.
61 */
62 UTIL_FORMAT_LAYOUT_ARITH = 1,
63
64 /**
65 * One or more components, no mixed formats, each with equal power of two
66 * number of bytes.
67 */
68 UTIL_FORMAT_LAYOUT_ARRAY = 2,
69
70 /**
71 * XXX: Not used yet. These might go away and be replaced by a single entry,
72 * for formats where multiple pixels have to be
73 * read in order to determine a single pixel value (i.e., block.width > 1
74 * || block.height > 1)
75 */
76 UTIL_FORMAT_LAYOUT_YUV = 3,
77 UTIL_FORMAT_LAYOUT_DXT = 4
78 };
79
80
81 struct util_format_block
82 {
83 /** Block width in pixels */
84 unsigned width;
85
86 /** Block height in pixels */
87 unsigned height;
88
89 /** Block size in bits */
90 unsigned bits;
91 };
92
93
94 enum util_format_type {
95 UTIL_FORMAT_TYPE_VOID = 0,
96 UTIL_FORMAT_TYPE_UNSIGNED = 1,
97 UTIL_FORMAT_TYPE_SIGNED = 2,
98 UTIL_FORMAT_TYPE_FIXED = 3,
99 UTIL_FORMAT_TYPE_FLOAT = 4
100 };
101
102
103 enum util_format_swizzle {
104 UTIL_FORMAT_SWIZZLE_X = 0,
105 UTIL_FORMAT_SWIZZLE_Y = 1,
106 UTIL_FORMAT_SWIZZLE_Z = 2,
107 UTIL_FORMAT_SWIZZLE_W = 3,
108 UTIL_FORMAT_SWIZZLE_0 = 4,
109 UTIL_FORMAT_SWIZZLE_1 = 5,
110 UTIL_FORMAT_SWIZZLE_NONE = 6
111 };
112
113
114 enum util_format_colorspace {
115 UTIL_FORMAT_COLORSPACE_RGB = 0,
116 UTIL_FORMAT_COLORSPACE_SRGB = 1,
117 UTIL_FORMAT_COLORSPACE_YUV = 2,
118 UTIL_FORMAT_COLORSPACE_ZS = 3,
119 };
120
121
122 struct util_format_channel_description
123 {
124 unsigned type:6;
125 unsigned normalized:1;
126 unsigned size:9;
127 };
128
129
130 struct util_format_description
131 {
132 enum pipe_format format;
133 const char *name;
134 struct util_format_block block;
135 enum util_format_layout layout;
136 struct util_format_channel_description channel[4];
137 unsigned char swizzle[4];
138 enum util_format_colorspace colorspace;
139 };
140
141
142 extern const struct util_format_description
143 util_format_description_table[];
144
145
146 const struct util_format_description *
147 util_format_description(enum pipe_format format);
148
149
150 /*
151 * Format query functions.
152 */
153
154 static INLINE boolean
155 util_format_is_compressed(enum pipe_format format)
156 {
157 const struct util_format_description *desc = util_format_description(format);
158
159 assert(format);
160 if (!format) {
161 return FALSE;
162 }
163
164 return desc->layout == UTIL_FORMAT_LAYOUT_DXT ? TRUE : FALSE;
165 }
166
167 static INLINE boolean
168 util_format_is_depth_or_stencil(enum pipe_format format)
169 {
170 const struct util_format_description *desc = util_format_description(format);
171
172 assert(format);
173 if (!format) {
174 return FALSE;
175 }
176
177 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS ? TRUE : FALSE;
178 }
179
180 static INLINE boolean
181 util_format_is_depth_and_stencil(enum pipe_format format)
182 {
183 const struct util_format_description *desc = util_format_description(format);
184
185 assert(format);
186 if (!format) {
187 return FALSE;
188 }
189
190 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS) {
191 return FALSE;
192 }
193
194 return (desc->swizzle[0] != UTIL_FORMAT_SWIZZLE_NONE &&
195 desc->swizzle[1] != UTIL_FORMAT_SWIZZLE_NONE) ? TRUE : FALSE;
196 }
197
198
199 /**
200 * Return total bits needed for the pixel format.
201 */
202 static INLINE uint
203 util_format_get_blocksizebits(enum pipe_format format)
204 {
205 const struct util_format_description *desc = util_format_description(format);
206
207 assert(format);
208 if (!format) {
209 return 0;
210 }
211
212 return desc->block.bits / (desc->block.width * desc->block.height);
213 }
214
215 /**
216 * Return bytes per pixel for the given format.
217 */
218 static INLINE uint
219 util_format_get_blocksize(enum pipe_format format)
220 {
221 uint bits = util_format_get_blocksizebits(format);
222
223 assert(bits % 8 == 0);
224
225 return bits / 8;
226 }
227
228 static INLINE uint
229 util_format_get_blockwidth(enum pipe_format format)
230 {
231 const struct util_format_description *desc = util_format_description(format);
232
233 assert(format);
234 if (!format) {
235 return 1;
236 }
237
238 switch (desc->layout) {
239 case UTIL_FORMAT_LAYOUT_YUV:
240 return 2;
241 case UTIL_FORMAT_LAYOUT_DXT:
242 return 4;
243 default:
244 return 1;
245 }
246 }
247
248 static INLINE uint
249 util_format_get_blockheight(enum pipe_format format)
250 {
251 const struct util_format_description *desc = util_format_description(format);
252
253 assert(format);
254 if (!format) {
255 return 1;
256 }
257
258 switch (desc->layout) {
259 case UTIL_FORMAT_LAYOUT_DXT:
260 return 4;
261 default:
262 return 1;
263 }
264 }
265
266 static INLINE unsigned
267 util_format_get_nblocksx(enum pipe_format format,
268 unsigned x)
269 {
270 unsigned blockwidth = util_format_get_blockwidth(format);
271 return (x + blockwidth - 1) / blockwidth;
272 }
273
274 static INLINE unsigned
275 util_format_get_nblocksy(enum pipe_format format,
276 unsigned y)
277 {
278 unsigned blockheight = util_format_get_blockheight(format);
279 return (y + blockheight - 1) / blockheight;
280 }
281
282 static INLINE unsigned
283 util_format_get_nblocks(enum pipe_format format,
284 unsigned width,
285 unsigned height)
286 {
287 return util_format_get_nblocksx(format, width) * util_format_get_nblocksy(format, height);
288 }
289
290 static INLINE size_t
291 util_format_get_stride(enum pipe_format format,
292 unsigned width)
293 {
294 return util_format_get_nblocksx(format, width) * util_format_get_blocksize(format);
295 }
296
297 static INLINE size_t
298 util_format_get_2d_size(enum pipe_format format,
299 size_t stride,
300 unsigned height)
301 {
302 return util_format_get_nblocksy(format, height) * stride;
303 }
304
305 static INLINE uint
306 util_format_get_component_bits(enum pipe_format format,
307 enum util_format_colorspace colorspace,
308 uint component)
309 {
310 const struct util_format_description *desc = util_format_description(format);
311 enum util_format_colorspace desc_colorspace;
312
313 assert(format);
314 if (!format) {
315 return 0;
316 }
317
318 assert(component < 4);
319
320 /* Treat RGB and SRGB as equivalent. */
321 if (colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
322 colorspace = UTIL_FORMAT_COLORSPACE_RGB;
323 }
324 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
325 desc_colorspace = UTIL_FORMAT_COLORSPACE_RGB;
326 } else {
327 desc_colorspace = desc->colorspace;
328 }
329
330 if (desc_colorspace != colorspace) {
331 return 0;
332 }
333
334 switch (desc->swizzle[component]) {
335 case UTIL_FORMAT_SWIZZLE_X:
336 return desc->channel[0].size;
337 case UTIL_FORMAT_SWIZZLE_Y:
338 return desc->channel[1].size;
339 case UTIL_FORMAT_SWIZZLE_Z:
340 return desc->channel[2].size;
341 case UTIL_FORMAT_SWIZZLE_W:
342 return desc->channel[3].size;
343 default:
344 return 0;
345 }
346 }
347
348
349 /*
350 * Format access functions.
351 */
352
353 void
354 util_format_read_4f(enum pipe_format format,
355 float *dst, unsigned dst_stride,
356 const void *src, unsigned src_stride,
357 unsigned x, unsigned y, unsigned w, unsigned h);
358
359 void
360 util_format_write_4f(enum pipe_format format,
361 const float *src, unsigned src_stride,
362 void *dst, unsigned dst_stride,
363 unsigned x, unsigned y, unsigned w, unsigned h);
364
365 void
366 util_format_read_4ub(enum pipe_format format,
367 uint8_t *dst, unsigned dst_stride,
368 const void *src, unsigned src_stride,
369 unsigned x, unsigned y, unsigned w, unsigned h);
370
371 void
372 util_format_write_4ub(enum pipe_format format,
373 const uint8_t *src, unsigned src_stride,
374 void *dst, unsigned dst_stride,
375 unsigned x, unsigned y, unsigned w, unsigned h);
376
377 #endif /* ! U_FORMAT_H */