util/u_format: don't crash in util_format_translate if we can't do translation
[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_format.h"
38 #include "u_format_s3tc.h"
39 #include "u_surface.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 i = util_format_get_first_non_void_channel(format);
56 if (i == -1) {
57 return FALSE;
58 }
59
60 return desc->channel[i].type == UTIL_FORMAT_TYPE_FLOAT ? TRUE : FALSE;
61 }
62
63
64 /** Test if the format contains RGB, but not alpha */
65 boolean
66 util_format_has_alpha(enum pipe_format format)
67 {
68 const struct util_format_description *desc =
69 util_format_description(format);
70
71 return (desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB ||
72 desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) &&
73 desc->swizzle[3] != UTIL_FORMAT_SWIZZLE_1;
74 }
75
76
77 boolean
78 util_format_is_luminance(enum pipe_format format)
79 {
80 const struct util_format_description *desc =
81 util_format_description(format);
82
83 if ((desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB ||
84 desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) &&
85 desc->swizzle[0] == UTIL_FORMAT_SWIZZLE_X &&
86 desc->swizzle[1] == UTIL_FORMAT_SWIZZLE_X &&
87 desc->swizzle[2] == UTIL_FORMAT_SWIZZLE_X &&
88 desc->swizzle[3] == UTIL_FORMAT_SWIZZLE_1) {
89 return TRUE;
90 }
91 return FALSE;
92 }
93
94 boolean
95 util_format_is_pure_integer(enum pipe_format format)
96 {
97 const struct util_format_description *desc = util_format_description(format);
98 int i;
99
100 /* Find the first non-void channel. */
101 i = util_format_get_first_non_void_channel(format);
102 if (i == -1)
103 return FALSE;
104
105 return desc->channel[i].pure_integer ? TRUE : FALSE;
106 }
107
108 boolean
109 util_format_is_pure_sint(enum pipe_format format)
110 {
111 const struct util_format_description *desc = util_format_description(format);
112 int i;
113
114 i = util_format_get_first_non_void_channel(format);
115 if (i == -1)
116 return FALSE;
117
118 return (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED && desc->channel[i].pure_integer) ? TRUE : FALSE;
119 }
120
121 boolean
122 util_format_is_pure_uint(enum pipe_format format)
123 {
124 const struct util_format_description *desc = util_format_description(format);
125 int i;
126
127 i = util_format_get_first_non_void_channel(format);
128 if (i == -1)
129 return FALSE;
130
131 return (desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED && desc->channel[i].pure_integer) ? TRUE : FALSE;
132 }
133
134 /**
135 * Returns true if all non-void channels are normalized signed.
136 */
137 boolean
138 util_format_is_snorm(enum pipe_format format)
139 {
140 const struct util_format_description *desc = util_format_description(format);
141 int i;
142
143 if (desc->is_mixed)
144 return FALSE;
145
146 i = util_format_get_first_non_void_channel(format);
147 if (i == -1)
148 return FALSE;
149
150 return desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED &&
151 !desc->channel[i].pure_integer &&
152 desc->channel[i].normalized;
153 }
154
155 boolean
156 util_format_is_luminance_alpha(enum pipe_format format)
157 {
158 const struct util_format_description *desc =
159 util_format_description(format);
160
161 if ((desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB ||
162 desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) &&
163 desc->swizzle[0] == UTIL_FORMAT_SWIZZLE_X &&
164 desc->swizzle[1] == UTIL_FORMAT_SWIZZLE_X &&
165 desc->swizzle[2] == UTIL_FORMAT_SWIZZLE_X &&
166 desc->swizzle[3] == UTIL_FORMAT_SWIZZLE_Y) {
167 return TRUE;
168 }
169 return FALSE;
170 }
171
172
173 boolean
174 util_format_is_intensity(enum pipe_format format)
175 {
176 const struct util_format_description *desc =
177 util_format_description(format);
178
179 if ((desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB ||
180 desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) &&
181 desc->swizzle[0] == UTIL_FORMAT_SWIZZLE_X &&
182 desc->swizzle[1] == UTIL_FORMAT_SWIZZLE_X &&
183 desc->swizzle[2] == UTIL_FORMAT_SWIZZLE_X &&
184 desc->swizzle[3] == UTIL_FORMAT_SWIZZLE_X) {
185 return TRUE;
186 }
187 return FALSE;
188 }
189
190
191 boolean
192 util_format_is_supported(enum pipe_format format, unsigned bind)
193 {
194 if (util_format_is_s3tc(format) && !util_format_s3tc_enabled) {
195 return FALSE;
196 }
197
198 #ifndef TEXTURE_FLOAT_ENABLED
199 if ((bind & PIPE_BIND_RENDER_TARGET) &&
200 format != PIPE_FORMAT_R9G9B9E5_FLOAT &&
201 format != PIPE_FORMAT_R11G11B10_FLOAT &&
202 util_format_is_float(format)) {
203 return FALSE;
204 }
205 #endif
206
207 return TRUE;
208 }
209
210
211 /**
212 * Calculates the MRD for the depth format. MRD is used in depth bias
213 * for UNORM and unbound depth buffers. When the depth buffer is floating
214 * point, the depth bias calculation does not use the MRD. However, the
215 * default MRD will be 1.0 / ((1 << 24) - 1).
216 */
217 double
218 util_get_depth_format_mrd(const struct util_format_description *desc)
219 {
220 /*
221 * Depth buffer formats without a depth component OR scenarios
222 * without a bound depth buffer default to D24.
223 */
224 double mrd = 1.0 / ((1 << 24) - 1);
225 unsigned depth_channel;
226
227 assert(desc);
228
229 /*
230 * Some depth formats do not store the depth component in the first
231 * channel, detect the format and adjust the depth channel. Get the
232 * swizzled depth component channel.
233 */
234 depth_channel = desc->swizzle[0];
235
236 if (desc->channel[depth_channel].type == UTIL_FORMAT_TYPE_UNSIGNED &&
237 desc->channel[depth_channel].normalized) {
238 int depth_bits;
239
240 depth_bits = desc->channel[depth_channel].size;
241 mrd = 1.0 / ((1ULL << depth_bits) - 1);
242 }
243
244 return mrd;
245 }
246
247
248 void
249 util_format_read_4f(enum pipe_format format,
250 float *dst, unsigned dst_stride,
251 const void *src, unsigned src_stride,
252 unsigned x, unsigned y, unsigned w, unsigned h)
253 {
254 const struct util_format_description *format_desc;
255 const uint8_t *src_row;
256 float *dst_row;
257
258 format_desc = util_format_description(format);
259
260 assert(x % format_desc->block.width == 0);
261 assert(y % format_desc->block.height == 0);
262
263 src_row = (const uint8_t *)src + y*src_stride + x*(format_desc->block.bits/8);
264 dst_row = dst;
265
266 format_desc->unpack_rgba_float(dst_row, dst_stride, src_row, src_stride, w, h);
267 }
268
269
270 void
271 util_format_write_4f(enum pipe_format format,
272 const float *src, unsigned src_stride,
273 void *dst, unsigned dst_stride,
274 unsigned x, unsigned y, unsigned w, unsigned h)
275 {
276 const struct util_format_description *format_desc;
277 uint8_t *dst_row;
278 const float *src_row;
279
280 format_desc = util_format_description(format);
281
282 assert(x % format_desc->block.width == 0);
283 assert(y % format_desc->block.height == 0);
284
285 dst_row = (uint8_t *)dst + y*dst_stride + x*(format_desc->block.bits/8);
286 src_row = src;
287
288 format_desc->pack_rgba_float(dst_row, dst_stride, src_row, src_stride, w, h);
289 }
290
291
292 void
293 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)
294 {
295 const struct util_format_description *format_desc;
296 const uint8_t *src_row;
297 uint8_t *dst_row;
298
299 format_desc = util_format_description(format);
300
301 assert(x % format_desc->block.width == 0);
302 assert(y % format_desc->block.height == 0);
303
304 src_row = (const uint8_t *)src + y*src_stride + x*(format_desc->block.bits/8);
305 dst_row = dst;
306
307 format_desc->unpack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, w, h);
308 }
309
310
311 void
312 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)
313 {
314 const struct util_format_description *format_desc;
315 uint8_t *dst_row;
316 const uint8_t *src_row;
317
318 format_desc = util_format_description(format);
319
320 assert(x % format_desc->block.width == 0);
321 assert(y % format_desc->block.height == 0);
322
323 dst_row = (uint8_t *)dst + y*dst_stride + x*(format_desc->block.bits/8);
324 src_row = src;
325
326 format_desc->pack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, w, h);
327 }
328
329 void
330 util_format_read_4ui(enum pipe_format format,
331 unsigned *dst, unsigned dst_stride,
332 const void *src, unsigned src_stride,
333 unsigned x, unsigned y, unsigned w, unsigned h)
334 {
335 const struct util_format_description *format_desc;
336 const uint8_t *src_row;
337 uint32_t *dst_row;
338
339 format_desc = util_format_description(format);
340
341 assert(x % format_desc->block.width == 0);
342 assert(y % format_desc->block.height == 0);
343
344 src_row = (const uint8_t *)src + y*src_stride + x*(format_desc->block.bits/8);
345 dst_row = dst;
346
347 format_desc->unpack_rgba_uint(dst_row, dst_stride, src_row, src_stride, w, h);
348 }
349
350 void
351 util_format_write_4ui(enum pipe_format format,
352 const unsigned int *src, unsigned src_stride,
353 void *dst, unsigned dst_stride,
354 unsigned x, unsigned y, unsigned w, unsigned h)
355 {
356 const struct util_format_description *format_desc;
357 uint8_t *dst_row;
358 const uint32_t *src_row;
359
360 format_desc = util_format_description(format);
361
362 assert(x % format_desc->block.width == 0);
363 assert(y % format_desc->block.height == 0);
364
365 dst_row = (uint8_t *)dst + y*dst_stride + x*(format_desc->block.bits/8);
366 src_row = src;
367
368 format_desc->pack_rgba_uint(dst_row, dst_stride, src_row, src_stride, w, h);
369 }
370
371 void
372 util_format_read_4i(enum pipe_format format,
373 int *dst, unsigned dst_stride,
374 const void *src, unsigned src_stride,
375 unsigned x, unsigned y, unsigned w, unsigned h)
376 {
377 const struct util_format_description *format_desc;
378 const uint8_t *src_row;
379 int32_t *dst_row;
380
381 format_desc = util_format_description(format);
382
383 assert(x % format_desc->block.width == 0);
384 assert(y % format_desc->block.height == 0);
385
386 src_row = (const uint8_t *)src + y*src_stride + x*(format_desc->block.bits/8);
387 dst_row = dst;
388
389 format_desc->unpack_rgba_sint(dst_row, dst_stride, src_row, src_stride, w, h);
390 }
391
392 void
393 util_format_write_4i(enum pipe_format format,
394 const int *src, unsigned src_stride,
395 void *dst, unsigned dst_stride,
396 unsigned x, unsigned y, unsigned w, unsigned h)
397 {
398 const struct util_format_description *format_desc;
399 uint8_t *dst_row;
400 const int32_t *src_row;
401
402 format_desc = util_format_description(format);
403
404 assert(x % format_desc->block.width == 0);
405 assert(y % format_desc->block.height == 0);
406
407 dst_row = (uint8_t *)dst + y*dst_stride + x*(format_desc->block.bits/8);
408 src_row = src;
409
410 format_desc->pack_rgba_sint(dst_row, dst_stride, src_row, src_stride, w, h);
411 }
412
413 boolean
414 util_is_format_compatible(const struct util_format_description *src_desc,
415 const struct util_format_description *dst_desc)
416 {
417 unsigned chan;
418
419 if (src_desc->format == dst_desc->format) {
420 return TRUE;
421 }
422
423 if (src_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN ||
424 dst_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN) {
425 return FALSE;
426 }
427
428 if (src_desc->block.bits != dst_desc->block.bits ||
429 src_desc->nr_channels != dst_desc->nr_channels ||
430 src_desc->colorspace != dst_desc->colorspace) {
431 return FALSE;
432 }
433
434 for (chan = 0; chan < 4; ++chan) {
435 if (src_desc->channel[chan].size !=
436 dst_desc->channel[chan].size) {
437 return FALSE;
438 }
439 }
440
441 for (chan = 0; chan < 4; ++chan) {
442 enum util_format_swizzle swizzle = dst_desc->swizzle[chan];
443
444 if (swizzle < 4) {
445 if (src_desc->swizzle[chan] != swizzle) {
446 return FALSE;
447 }
448 if ((src_desc->channel[swizzle].type !=
449 dst_desc->channel[swizzle].type) ||
450 (src_desc->channel[swizzle].normalized !=
451 dst_desc->channel[swizzle].normalized)) {
452 return FALSE;
453 }
454 }
455 }
456
457 return TRUE;
458 }
459
460
461 boolean
462 util_format_fits_8unorm(const struct util_format_description *format_desc)
463 {
464 unsigned chan;
465
466 /*
467 * After linearized sRGB values require more than 8bits.
468 */
469
470 if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
471 return FALSE;
472 }
473
474 switch (format_desc->layout) {
475
476 case UTIL_FORMAT_LAYOUT_S3TC:
477 /*
478 * These are straight forward.
479 */
480 return TRUE;
481 case UTIL_FORMAT_LAYOUT_RGTC:
482 if (format_desc->format == PIPE_FORMAT_RGTC1_SNORM ||
483 format_desc->format == PIPE_FORMAT_RGTC2_SNORM ||
484 format_desc->format == PIPE_FORMAT_LATC1_SNORM ||
485 format_desc->format == PIPE_FORMAT_LATC2_SNORM)
486 return FALSE;
487 return TRUE;
488
489 case UTIL_FORMAT_LAYOUT_PLAIN:
490 /*
491 * For these we can find a generic rule.
492 */
493
494 for (chan = 0; chan < format_desc->nr_channels; ++chan) {
495 switch (format_desc->channel[chan].type) {
496 case UTIL_FORMAT_TYPE_VOID:
497 break;
498 case UTIL_FORMAT_TYPE_UNSIGNED:
499 if (!format_desc->channel[chan].normalized ||
500 format_desc->channel[chan].size > 8) {
501 return FALSE;
502 }
503 break;
504 default:
505 return FALSE;
506 }
507 }
508 return TRUE;
509
510 default:
511 /*
512 * Handle all others on a case by case basis.
513 */
514
515 switch (format_desc->format) {
516 case PIPE_FORMAT_R1_UNORM:
517 case PIPE_FORMAT_UYVY:
518 case PIPE_FORMAT_YUYV:
519 case PIPE_FORMAT_R8G8_B8G8_UNORM:
520 case PIPE_FORMAT_G8R8_G8B8_UNORM:
521 return TRUE;
522
523 default:
524 return FALSE;
525 }
526 }
527 }
528
529
530 boolean
531 util_format_translate(enum pipe_format dst_format,
532 void *dst, unsigned dst_stride,
533 unsigned dst_x, unsigned dst_y,
534 enum pipe_format src_format,
535 const void *src, unsigned src_stride,
536 unsigned src_x, unsigned src_y,
537 unsigned width, unsigned height)
538 {
539 const struct util_format_description *dst_format_desc;
540 const struct util_format_description *src_format_desc;
541 uint8_t *dst_row;
542 const uint8_t *src_row;
543 unsigned x_step, y_step;
544 unsigned dst_step;
545 unsigned src_step;
546
547 dst_format_desc = util_format_description(dst_format);
548 src_format_desc = util_format_description(src_format);
549
550 if (util_is_format_compatible(src_format_desc, dst_format_desc)) {
551 /*
552 * Trivial case.
553 */
554
555 util_copy_rect(dst, dst_format, dst_stride, dst_x, dst_y,
556 width, height, src, (int)src_stride,
557 src_x, src_y);
558 return TRUE;
559 }
560
561 assert(dst_x % dst_format_desc->block.width == 0);
562 assert(dst_y % dst_format_desc->block.height == 0);
563 assert(src_x % src_format_desc->block.width == 0);
564 assert(src_y % src_format_desc->block.height == 0);
565
566 dst_row = (uint8_t *)dst + dst_y*dst_stride + dst_x*(dst_format_desc->block.bits/8);
567 src_row = (const uint8_t *)src + src_y*src_stride + src_x*(src_format_desc->block.bits/8);
568
569 /*
570 * This works because all pixel formats have pixel blocks with power of two
571 * sizes.
572 */
573
574 y_step = MAX2(dst_format_desc->block.height, src_format_desc->block.height);
575 x_step = MAX2(dst_format_desc->block.width, src_format_desc->block.width);
576 assert(y_step % dst_format_desc->block.height == 0);
577 assert(y_step % src_format_desc->block.height == 0);
578
579 dst_step = y_step / dst_format_desc->block.height * dst_stride;
580 src_step = y_step / src_format_desc->block.height * src_stride;
581
582 /*
583 * TODO: double formats will loose precision
584 * TODO: Add a special case for formats that are mere swizzles of each other
585 */
586
587 if (src_format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS ||
588 dst_format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
589 float *tmp_z = NULL;
590 uint8_t *tmp_s = NULL;
591
592 assert(x_step == 1);
593 assert(y_step == 1);
594
595 if (src_format_desc->unpack_z_float &&
596 dst_format_desc->pack_z_float) {
597 tmp_z = MALLOC(width * sizeof *tmp_z);
598 }
599
600 if (src_format_desc->unpack_s_8uint &&
601 dst_format_desc->pack_s_8uint) {
602 tmp_s = MALLOC(width * sizeof *tmp_s);
603 }
604
605 while (height--) {
606 if (tmp_z) {
607 src_format_desc->unpack_z_float(tmp_z, 0, src_row, src_stride, width, 1);
608 dst_format_desc->pack_z_float(dst_row, dst_stride, tmp_z, 0, width, 1);
609 }
610
611 if (tmp_s) {
612 src_format_desc->unpack_s_8uint(tmp_s, 0, src_row, src_stride, width, 1);
613 dst_format_desc->pack_s_8uint(dst_row, dst_stride, tmp_s, 0, width, 1);
614 }
615
616 dst_row += dst_step;
617 src_row += src_step;
618 }
619
620 FREE(tmp_s);
621
622 FREE(tmp_z);
623
624 return TRUE;
625 }
626
627 if (util_format_fits_8unorm(src_format_desc) ||
628 util_format_fits_8unorm(dst_format_desc)) {
629 unsigned tmp_stride;
630 uint8_t *tmp_row;
631
632 if (!src_format_desc->unpack_rgba_8unorm ||
633 !dst_format_desc->pack_rgba_8unorm) {
634 return FALSE;
635 }
636
637 tmp_stride = MAX2(width, x_step) * 4 * sizeof *tmp_row;
638 tmp_row = MALLOC(y_step * tmp_stride);
639 if (!tmp_row)
640 return FALSE;
641
642 while (height >= y_step) {
643 src_format_desc->unpack_rgba_8unorm(tmp_row, tmp_stride, src_row, src_stride, width, y_step);
644 dst_format_desc->pack_rgba_8unorm(dst_row, dst_stride, tmp_row, tmp_stride, width, y_step);
645
646 dst_row += dst_step;
647 src_row += src_step;
648 height -= y_step;
649 }
650
651 if (height) {
652 src_format_desc->unpack_rgba_8unorm(tmp_row, tmp_stride, src_row, src_stride, width, height);
653 dst_format_desc->pack_rgba_8unorm(dst_row, dst_stride, tmp_row, tmp_stride, width, height);
654 }
655
656 FREE(tmp_row);
657 }
658 else {
659 unsigned tmp_stride;
660 float *tmp_row;
661
662 if (!src_format_desc->unpack_rgba_float ||
663 !dst_format_desc->pack_rgba_float) {
664 return FALSE;
665 }
666
667 tmp_stride = MAX2(width, x_step) * 4 * sizeof *tmp_row;
668 tmp_row = MALLOC(y_step * tmp_stride);
669 if (!tmp_row)
670 return FALSE;
671
672 while (height >= y_step) {
673 src_format_desc->unpack_rgba_float(tmp_row, tmp_stride, src_row, src_stride, width, y_step);
674 dst_format_desc->pack_rgba_float(dst_row, dst_stride, tmp_row, tmp_stride, width, y_step);
675
676 dst_row += dst_step;
677 src_row += src_step;
678 height -= y_step;
679 }
680
681 if (height) {
682 src_format_desc->unpack_rgba_float(tmp_row, tmp_stride, src_row, src_stride, width, height);
683 dst_format_desc->pack_rgba_float(dst_row, dst_stride, tmp_row, tmp_stride, width, height);
684 }
685
686 FREE(tmp_row);
687 }
688 return TRUE;
689 }
690
691 void util_format_compose_swizzles(const unsigned char swz1[4],
692 const unsigned char swz2[4],
693 unsigned char dst[4])
694 {
695 unsigned i;
696
697 for (i = 0; i < 4; i++) {
698 dst[i] = swz2[i] <= UTIL_FORMAT_SWIZZLE_W ?
699 swz1[swz2[i]] : swz2[i];
700 }
701 }
702
703 void util_format_apply_color_swizzle(union pipe_color_union *dst,
704 const union pipe_color_union *src,
705 const unsigned char swz[4],
706 const boolean is_integer)
707 {
708 unsigned c;
709
710 if (is_integer) {
711 for (c = 0; c < 4; ++c) {
712 switch (swz[c]) {
713 case PIPE_SWIZZLE_RED: dst->ui[c] = src->ui[0]; break;
714 case PIPE_SWIZZLE_GREEN: dst->ui[c] = src->ui[1]; break;
715 case PIPE_SWIZZLE_BLUE: dst->ui[c] = src->ui[2]; break;
716 case PIPE_SWIZZLE_ALPHA: dst->ui[c] = src->ui[3]; break;
717 default:
718 dst->ui[c] = (swz[c] == PIPE_SWIZZLE_ONE) ? 1 : 0;
719 break;
720 }
721 }
722 } else {
723 for (c = 0; c < 4; ++c) {
724 switch (swz[c]) {
725 case PIPE_SWIZZLE_RED: dst->f[c] = src->f[0]; break;
726 case PIPE_SWIZZLE_GREEN: dst->f[c] = src->f[1]; break;
727 case PIPE_SWIZZLE_BLUE: dst->f[c] = src->f[2]; break;
728 case PIPE_SWIZZLE_ALPHA: dst->f[c] = src->f[3]; break;
729 default:
730 dst->f[c] = (swz[c] == PIPE_SWIZZLE_ONE) ? 1.0f : 0.0f;
731 break;
732 }
733 }
734 }
735 }
736
737 void util_format_swizzle_4f(float *dst, const float *src,
738 const unsigned char swz[4])
739 {
740 unsigned i;
741
742 for (i = 0; i < 4; i++) {
743 if (swz[i] <= UTIL_FORMAT_SWIZZLE_W)
744 dst[i] = src[swz[i]];
745 else if (swz[i] == UTIL_FORMAT_SWIZZLE_0)
746 dst[i] = 0;
747 else if (swz[i] == UTIL_FORMAT_SWIZZLE_1)
748 dst[i] = 1;
749 }
750 }
751
752 void util_format_unswizzle_4f(float *dst, const float *src,
753 const unsigned char swz[4])
754 {
755 unsigned i;
756
757 for (i = 0; i < 4; i++) {
758 switch (swz[i]) {
759 case UTIL_FORMAT_SWIZZLE_X:
760 dst[0] = src[i];
761 break;
762 case UTIL_FORMAT_SWIZZLE_Y:
763 dst[1] = src[i];
764 break;
765 case UTIL_FORMAT_SWIZZLE_Z:
766 dst[2] = src[i];
767 break;
768 case UTIL_FORMAT_SWIZZLE_W:
769 dst[3] = src[i];
770 break;
771 }
772 }
773 }