util: fix a typo in util_format_swizzle_4f
[mesa.git] / src / gallium / auxiliary / util / u_format.c
1 /**************************************************************************
2 *
3 * Copyright 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 * @file
30 * Pixel format accessor functions.
31 *
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 */
34
35 #include "u_math.h"
36 #include "u_memory.h"
37 #include "u_rect.h"
38 #include "u_format.h"
39 #include "u_format_s3tc.h"
40
41 #include "pipe/p_defines.h"
42
43
44 boolean
45 util_format_is_float(enum pipe_format format)
46 {
47 const struct util_format_description *desc = util_format_description(format);
48 unsigned i;
49
50 assert(desc);
51 if (!desc) {
52 return FALSE;
53 }
54
55 /* Find the first non-void channel. */
56 for (i = 0; i < 4; i++) {
57 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
58 break;
59 }
60 }
61
62 if (i == 4) {
63 return FALSE;
64 }
65
66 return desc->channel[i].type == UTIL_FORMAT_TYPE_FLOAT ? TRUE : FALSE;
67 }
68
69
70 boolean
71 util_format_is_supported(enum pipe_format format, unsigned bind)
72 {
73 if (util_format_is_s3tc(format) && !util_format_s3tc_enabled) {
74 return FALSE;
75 }
76
77 #ifndef TEXTURE_FLOAT_ENABLED
78 if ((bind & PIPE_BIND_RENDER_TARGET) &&
79 format != PIPE_FORMAT_R9G9B9E5_FLOAT &&
80 format != PIPE_FORMAT_R11G11B10_FLOAT &&
81 util_format_is_float(format)) {
82 return FALSE;
83 }
84 #endif
85
86 return TRUE;
87 }
88
89
90 void
91 util_format_read_4f(enum pipe_format format,
92 float *dst, unsigned dst_stride,
93 const void *src, unsigned src_stride,
94 unsigned x, unsigned y, unsigned w, unsigned h)
95 {
96 const struct util_format_description *format_desc;
97 const uint8_t *src_row;
98 float *dst_row;
99
100 format_desc = util_format_description(format);
101
102 assert(x % format_desc->block.width == 0);
103 assert(y % format_desc->block.height == 0);
104
105 src_row = (const uint8_t *)src + y*src_stride + x*(format_desc->block.bits/8);
106 dst_row = dst;
107
108 format_desc->unpack_rgba_float(dst_row, dst_stride, src_row, src_stride, w, h);
109 }
110
111
112 void
113 util_format_write_4f(enum pipe_format format,
114 const float *src, unsigned src_stride,
115 void *dst, unsigned dst_stride,
116 unsigned x, unsigned y, unsigned w, unsigned h)
117 {
118 const struct util_format_description *format_desc;
119 uint8_t *dst_row;
120 const float *src_row;
121
122 format_desc = util_format_description(format);
123
124 assert(x % format_desc->block.width == 0);
125 assert(y % format_desc->block.height == 0);
126
127 dst_row = (uint8_t *)dst + y*dst_stride + x*(format_desc->block.bits/8);
128 src_row = src;
129
130 format_desc->pack_rgba_float(dst_row, dst_stride, src_row, src_stride, w, h);
131 }
132
133
134 void
135 util_format_read_4ub(enum pipe_format format, uint8_t *dst, unsigned dst_stride, const void *src, unsigned src_stride, unsigned x, unsigned y, unsigned w, unsigned h)
136 {
137 const struct util_format_description *format_desc;
138 const uint8_t *src_row;
139 uint8_t *dst_row;
140
141 format_desc = util_format_description(format);
142
143 assert(x % format_desc->block.width == 0);
144 assert(y % format_desc->block.height == 0);
145
146 src_row = (const uint8_t *)src + y*src_stride + x*(format_desc->block.bits/8);
147 dst_row = dst;
148
149 format_desc->unpack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, w, h);
150 }
151
152
153 void
154 util_format_write_4ub(enum pipe_format format, const uint8_t *src, unsigned src_stride, void *dst, unsigned dst_stride, unsigned x, unsigned y, unsigned w, unsigned h)
155 {
156 const struct util_format_description *format_desc;
157 uint8_t *dst_row;
158 const uint8_t *src_row;
159
160 format_desc = util_format_description(format);
161
162 assert(x % format_desc->block.width == 0);
163 assert(y % format_desc->block.height == 0);
164
165 dst_row = (uint8_t *)dst + y*dst_stride + x*(format_desc->block.bits/8);
166 src_row = src;
167
168 format_desc->pack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, w, h);
169 }
170
171
172 boolean
173 util_is_format_compatible(const struct util_format_description *src_desc,
174 const struct util_format_description *dst_desc)
175 {
176 unsigned chan;
177
178 if (src_desc->format == dst_desc->format) {
179 return TRUE;
180 }
181
182 if (src_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN ||
183 dst_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN) {
184 return FALSE;
185 }
186
187 if (src_desc->block.bits != dst_desc->block.bits ||
188 src_desc->nr_channels != dst_desc->nr_channels ||
189 src_desc->colorspace != dst_desc->colorspace) {
190 return FALSE;
191 }
192
193 for (chan = 0; chan < 4; ++chan) {
194 if (src_desc->channel[chan].size !=
195 dst_desc->channel[chan].size) {
196 return FALSE;
197 }
198 }
199
200 for (chan = 0; chan < 4; ++chan) {
201 enum util_format_swizzle swizzle = dst_desc->swizzle[chan];
202
203 if (swizzle < 4) {
204 if (src_desc->swizzle[chan] != swizzle) {
205 return FALSE;
206 }
207 if ((src_desc->channel[swizzle].type !=
208 dst_desc->channel[swizzle].type) ||
209 (src_desc->channel[swizzle].normalized !=
210 dst_desc->channel[swizzle].normalized)) {
211 return FALSE;
212 }
213 }
214 }
215
216 return TRUE;
217 }
218
219
220 boolean
221 util_format_fits_8unorm(const struct util_format_description *format_desc)
222 {
223 unsigned chan;
224
225 /*
226 * After linearized sRGB values require more than 8bits.
227 */
228
229 if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
230 return FALSE;
231 }
232
233 switch (format_desc->layout) {
234
235 case UTIL_FORMAT_LAYOUT_S3TC:
236 case UTIL_FORMAT_LAYOUT_RGTC:
237 /*
238 * These are straight forward.
239 */
240
241 return TRUE;
242
243 case UTIL_FORMAT_LAYOUT_PLAIN:
244 /*
245 * For these we can find a generic rule.
246 */
247
248 for (chan = 0; chan < format_desc->nr_channels; ++chan) {
249 switch (format_desc->channel[chan].type) {
250 case UTIL_FORMAT_TYPE_VOID:
251 break;
252 case UTIL_FORMAT_TYPE_UNSIGNED:
253 if (!format_desc->channel[chan].normalized ||
254 format_desc->channel[chan].size > 8) {
255 return FALSE;
256 }
257 break;
258 default:
259 return FALSE;
260 }
261 }
262 return TRUE;
263
264 default:
265 /*
266 * Handle all others on a case by case basis.
267 */
268
269 switch (format_desc->format) {
270 case PIPE_FORMAT_R1_UNORM:
271 case PIPE_FORMAT_UYVY:
272 case PIPE_FORMAT_YUYV:
273 case PIPE_FORMAT_R8G8_B8G8_UNORM:
274 case PIPE_FORMAT_G8R8_G8B8_UNORM:
275 return TRUE;
276
277 default:
278 return FALSE;
279 }
280 }
281 }
282
283
284 void
285 util_format_translate(enum pipe_format dst_format,
286 void *dst, unsigned dst_stride,
287 unsigned dst_x, unsigned dst_y,
288 enum pipe_format src_format,
289 const void *src, unsigned src_stride,
290 unsigned src_x, unsigned src_y,
291 unsigned width, unsigned height)
292 {
293 const struct util_format_description *dst_format_desc;
294 const struct util_format_description *src_format_desc;
295 uint8_t *dst_row;
296 const uint8_t *src_row;
297 unsigned x_step, y_step;
298 unsigned dst_step;
299 unsigned src_step;
300
301 dst_format_desc = util_format_description(dst_format);
302 src_format_desc = util_format_description(src_format);
303
304 if (util_is_format_compatible(src_format_desc, dst_format_desc)) {
305 /*
306 * Trivial case.
307 */
308
309 util_copy_rect(dst, dst_format, dst_stride, dst_x, dst_y,
310 width, height, src, (int)src_stride,
311 src_x, src_y);
312 return;
313 }
314
315 assert(dst_x % dst_format_desc->block.width == 0);
316 assert(dst_y % dst_format_desc->block.height == 0);
317 assert(src_x % src_format_desc->block.width == 0);
318 assert(src_y % src_format_desc->block.height == 0);
319
320 dst_row = (uint8_t *)dst + dst_y*dst_stride + dst_x*(dst_format_desc->block.bits/8);
321 src_row = (const uint8_t *)src + src_y*src_stride + src_x*(src_format_desc->block.bits/8);
322
323 /*
324 * This works because all pixel formats have pixel blocks with power of two
325 * sizes.
326 */
327
328 y_step = MAX2(dst_format_desc->block.height, src_format_desc->block.height);
329 x_step = MAX2(dst_format_desc->block.width, src_format_desc->block.width);
330 assert(y_step % dst_format_desc->block.height == 0);
331 assert(y_step % src_format_desc->block.height == 0);
332
333 dst_step = y_step / dst_format_desc->block.height * dst_stride;
334 src_step = y_step / src_format_desc->block.height * src_stride;
335
336 /*
337 * TODO: double formats will loose precision
338 * TODO: Add a special case for formats that are mere swizzles of each other
339 */
340
341 if (util_format_fits_8unorm(src_format_desc) ||
342 util_format_fits_8unorm(dst_format_desc)) {
343 unsigned tmp_stride;
344 uint8_t *tmp_row;
345
346 tmp_stride = MAX2(width, x_step) * 4 * sizeof *tmp_row;
347 tmp_row = MALLOC(y_step * tmp_stride);
348 if (!tmp_row)
349 return;
350
351 while (height >= y_step) {
352 src_format_desc->unpack_rgba_8unorm(tmp_row, tmp_stride, src_row, src_stride, width, y_step);
353 dst_format_desc->pack_rgba_8unorm(dst_row, dst_stride, tmp_row, tmp_stride, width, y_step);
354
355 dst_row += dst_step;
356 src_row += src_step;
357 height -= y_step;
358 }
359
360 if (height) {
361 src_format_desc->unpack_rgba_8unorm(tmp_row, tmp_stride, src_row, src_stride, width, height);
362 dst_format_desc->pack_rgba_8unorm(dst_row, dst_stride, tmp_row, tmp_stride, width, height);
363 }
364
365 FREE(tmp_row);
366 }
367 else {
368 unsigned tmp_stride;
369 float *tmp_row;
370
371 tmp_stride = MAX2(width, x_step) * 4 * sizeof *tmp_row;
372 tmp_row = MALLOC(y_step * tmp_stride);
373 if (!tmp_row)
374 return;
375
376 while (height >= y_step) {
377 src_format_desc->unpack_rgba_float(tmp_row, tmp_stride, src_row, src_stride, width, y_step);
378 dst_format_desc->pack_rgba_float(dst_row, dst_stride, tmp_row, tmp_stride, width, y_step);
379
380 dst_row += dst_step;
381 src_row += src_step;
382 height -= y_step;
383 }
384
385 if (height) {
386 src_format_desc->unpack_rgba_float(tmp_row, tmp_stride, src_row, src_stride, width, height);
387 dst_format_desc->pack_rgba_float(dst_row, dst_stride, tmp_row, tmp_stride, width, height);
388 }
389
390 FREE(tmp_row);
391 }
392 }
393
394 void util_format_compose_swizzles(const unsigned char swz1[4],
395 const unsigned char swz2[4],
396 unsigned char dst[4])
397 {
398 unsigned i;
399
400 for (i = 0; i < 4; i++) {
401 dst[i] = swz2[i] <= UTIL_FORMAT_SWIZZLE_W ?
402 swz1[swz2[i]] : swz2[i];
403 }
404 }
405
406 void util_format_swizzle_4f(float *dst, const float *src,
407 const unsigned char swz[4])
408 {
409 unsigned i;
410
411 for (i = 0; i < 4; i++) {
412 if (swz[i] <= UTIL_FORMAT_SWIZZLE_W)
413 dst[i] = src[swz[i]];
414 else if (swz[i] == UTIL_FORMAT_SWIZZLE_0)
415 dst[i] = 0;
416 else if (swz[i] == UTIL_FORMAT_SWIZZLE_1)
417 dst[i] = 1;
418 }
419 }
420
421 void util_format_unswizzle_4f(float *dst, const float *src,
422 const unsigned char swz[4])
423 {
424 unsigned i;
425
426 for (i = 0; i < 4; i++) {
427 switch (swz[i]) {
428 case UTIL_FORMAT_SWIZZLE_X:
429 dst[0] = src[i];
430 break;
431 case UTIL_FORMAT_SWIZZLE_Y:
432 dst[1] = src[i];
433 break;
434 case UTIL_FORMAT_SWIZZLE_Z:
435 dst[2] = src[i];
436 break;
437 case UTIL_FORMAT_SWIZZLE_W:
438 dst[3] = src[i];
439 break;
440 }
441 }
442 }