util: Merge util_format_read_4* functions.
[mesa.git] / src / util / format / 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 "util/format/u_format.h"
36 #include "util/format/u_format_s3tc.h"
37 #include "util/u_math.h"
38
39 #include "pipe/p_defines.h"
40 #include "pipe/p_screen.h"
41
42
43 /**
44 * Copy 2D rect from one place to another.
45 * Position and sizes are in pixels.
46 * src_stride may be negative to do vertical flip of pixels from source.
47 */
48 void
49 util_copy_rect(ubyte * dst,
50 enum pipe_format format,
51 unsigned dst_stride,
52 unsigned dst_x,
53 unsigned dst_y,
54 unsigned width,
55 unsigned height,
56 const ubyte * src,
57 int src_stride,
58 unsigned src_x,
59 unsigned src_y)
60 {
61 unsigned i;
62 int src_stride_pos = src_stride < 0 ? -src_stride : src_stride;
63 int blocksize = util_format_get_blocksize(format);
64 int blockwidth = util_format_get_blockwidth(format);
65 int blockheight = util_format_get_blockheight(format);
66
67 assert(blocksize > 0);
68 assert(blockwidth > 0);
69 assert(blockheight > 0);
70
71 dst_x /= blockwidth;
72 dst_y /= blockheight;
73 width = (width + blockwidth - 1)/blockwidth;
74 height = (height + blockheight - 1)/blockheight;
75 src_x /= blockwidth;
76 src_y /= blockheight;
77
78 dst += dst_x * blocksize;
79 src += src_x * blocksize;
80 dst += dst_y * dst_stride;
81 src += src_y * src_stride_pos;
82 width *= blocksize;
83
84 if (width == dst_stride && width == (unsigned)src_stride)
85 memcpy(dst, src, height * width);
86 else {
87 for (i = 0; i < height; i++) {
88 memcpy(dst, src, width);
89 dst += dst_stride;
90 src += src_stride;
91 }
92 }
93 }
94
95
96 boolean
97 util_format_is_float(enum pipe_format format)
98 {
99 const struct util_format_description *desc = util_format_description(format);
100 int i;
101
102 assert(desc);
103 if (!desc) {
104 return FALSE;
105 }
106
107 i = util_format_get_first_non_void_channel(format);
108 if (i < 0) {
109 return FALSE;
110 }
111
112 return desc->channel[i].type == UTIL_FORMAT_TYPE_FLOAT ? TRUE : FALSE;
113 }
114
115
116 /** Test if the format contains RGB, but not alpha */
117 boolean
118 util_format_has_alpha(enum pipe_format format)
119 {
120 const struct util_format_description *desc =
121 util_format_description(format);
122
123 return (desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB ||
124 desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) &&
125 desc->swizzle[3] != PIPE_SWIZZLE_1;
126 }
127
128
129 boolean
130 util_format_is_luminance(enum pipe_format format)
131 {
132 const struct util_format_description *desc =
133 util_format_description(format);
134
135 if ((desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB ||
136 desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) &&
137 desc->swizzle[0] == PIPE_SWIZZLE_X &&
138 desc->swizzle[1] == PIPE_SWIZZLE_X &&
139 desc->swizzle[2] == PIPE_SWIZZLE_X &&
140 desc->swizzle[3] == PIPE_SWIZZLE_1) {
141 return TRUE;
142 }
143 return FALSE;
144 }
145
146 boolean
147 util_format_is_alpha(enum pipe_format format)
148 {
149 const struct util_format_description *desc =
150 util_format_description(format);
151
152 if ((desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB ||
153 desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) &&
154 desc->swizzle[0] == PIPE_SWIZZLE_0 &&
155 desc->swizzle[1] == PIPE_SWIZZLE_0 &&
156 desc->swizzle[2] == PIPE_SWIZZLE_0 &&
157 desc->swizzle[3] == PIPE_SWIZZLE_X) {
158 return TRUE;
159 }
160 return FALSE;
161 }
162
163 boolean
164 util_format_is_pure_integer(enum pipe_format format)
165 {
166 const struct util_format_description *desc = util_format_description(format);
167 int i;
168
169 /* Find the first non-void channel. */
170 i = util_format_get_first_non_void_channel(format);
171 if (i == -1)
172 return FALSE;
173
174 return desc->channel[i].pure_integer ? TRUE : FALSE;
175 }
176
177 boolean
178 util_format_is_pure_sint(enum pipe_format format)
179 {
180 const struct util_format_description *desc = util_format_description(format);
181 int i;
182
183 i = util_format_get_first_non_void_channel(format);
184 if (i == -1)
185 return FALSE;
186
187 return (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED && desc->channel[i].pure_integer) ? TRUE : FALSE;
188 }
189
190 boolean
191 util_format_is_pure_uint(enum pipe_format format)
192 {
193 const struct util_format_description *desc = util_format_description(format);
194 int i;
195
196 i = util_format_get_first_non_void_channel(format);
197 if (i == -1)
198 return FALSE;
199
200 return (desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED && desc->channel[i].pure_integer) ? TRUE : FALSE;
201 }
202
203 /**
204 * Returns true if the format contains normalized signed channels.
205 */
206 boolean
207 util_format_is_snorm(enum pipe_format format)
208 {
209 const struct util_format_description *desc = util_format_description(format);
210
211 return desc->is_snorm;
212 }
213
214 /**
215 * Returns true if the format contains normalized unsigned channels.
216 */
217 boolean
218 util_format_is_unorm(enum pipe_format format)
219 {
220 const struct util_format_description *desc = util_format_description(format);
221
222 return desc->is_unorm;
223 }
224
225 boolean
226 util_format_is_snorm8(enum pipe_format format)
227 {
228 const struct util_format_description *desc = util_format_description(format);
229 int i;
230
231 if (desc->is_mixed)
232 return FALSE;
233
234 i = util_format_get_first_non_void_channel(format);
235 if (i == -1)
236 return FALSE;
237
238 return desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED &&
239 !desc->channel[i].pure_integer &&
240 desc->channel[i].normalized &&
241 desc->channel[i].size == 8;
242 }
243
244 boolean
245 util_format_is_luminance_alpha(enum pipe_format format)
246 {
247 const struct util_format_description *desc =
248 util_format_description(format);
249
250 if ((desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB ||
251 desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) &&
252 desc->swizzle[0] == PIPE_SWIZZLE_X &&
253 desc->swizzle[1] == PIPE_SWIZZLE_X &&
254 desc->swizzle[2] == PIPE_SWIZZLE_X &&
255 desc->swizzle[3] == PIPE_SWIZZLE_Y) {
256 return TRUE;
257 }
258 return FALSE;
259 }
260
261
262 boolean
263 util_format_is_intensity(enum pipe_format format)
264 {
265 const struct util_format_description *desc =
266 util_format_description(format);
267
268 if ((desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB ||
269 desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) &&
270 desc->swizzle[0] == PIPE_SWIZZLE_X &&
271 desc->swizzle[1] == PIPE_SWIZZLE_X &&
272 desc->swizzle[2] == PIPE_SWIZZLE_X &&
273 desc->swizzle[3] == PIPE_SWIZZLE_X) {
274 return TRUE;
275 }
276 return FALSE;
277 }
278
279 boolean
280 util_format_is_subsampled_422(enum pipe_format format)
281 {
282 const struct util_format_description *desc =
283 util_format_description(format);
284
285 return desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED &&
286 desc->block.width == 2 &&
287 desc->block.height == 1 &&
288 desc->block.bits == 32;
289 }
290
291 /**
292 * Calculates the MRD for the depth format. MRD is used in depth bias
293 * for UNORM and unbound depth buffers. When the depth buffer is floating
294 * point, the depth bias calculation does not use the MRD. However, the
295 * default MRD will be 1.0 / ((1 << 24) - 1).
296 */
297 double
298 util_get_depth_format_mrd(const struct util_format_description *desc)
299 {
300 /*
301 * Depth buffer formats without a depth component OR scenarios
302 * without a bound depth buffer default to D24.
303 */
304 double mrd = 1.0 / ((1 << 24) - 1);
305 unsigned depth_channel;
306
307 assert(desc);
308
309 /*
310 * Some depth formats do not store the depth component in the first
311 * channel, detect the format and adjust the depth channel. Get the
312 * swizzled depth component channel.
313 */
314 depth_channel = desc->swizzle[0];
315
316 if (desc->channel[depth_channel].type == UTIL_FORMAT_TYPE_UNSIGNED &&
317 desc->channel[depth_channel].normalized) {
318 int depth_bits;
319
320 depth_bits = desc->channel[depth_channel].size;
321 mrd = 1.0 / ((1ULL << depth_bits) - 1);
322 }
323
324 return mrd;
325 }
326
327
328 void
329 util_format_read_4(enum pipe_format format,
330 void *dst, unsigned dst_stride,
331 const void *src, unsigned src_stride,
332 unsigned x, unsigned y, unsigned w, unsigned h)
333 {
334 const struct util_format_description *format_desc;
335 const uint8_t *src_row;
336
337 format_desc = util_format_description(format);
338
339 assert(x % format_desc->block.width == 0);
340 assert(y % format_desc->block.height == 0);
341
342 src_row = (const uint8_t *)src + y*src_stride + x*(format_desc->block.bits/8);
343
344 if (util_format_is_pure_uint(format))
345 format_desc->unpack_rgba_sint(dst, dst_stride, src_row, src_stride, w, h);
346 else if (util_format_is_pure_uint(format))
347 format_desc->unpack_rgba_uint(dst, dst_stride, src_row, src_stride, w, h);
348 else
349 format_desc->unpack_rgba_float(dst, dst_stride, src_row, src_stride, w, h);
350 }
351
352
353 void
354 util_format_write_4(enum pipe_format format,
355 const void *src, unsigned src_stride,
356 void *dst, unsigned dst_stride,
357 unsigned x, unsigned y, unsigned w, unsigned h)
358 {
359 const struct util_format_description *format_desc;
360 uint8_t *dst_row;
361
362 format_desc = util_format_description(format);
363
364 assert(x % format_desc->block.width == 0);
365 assert(y % format_desc->block.height == 0);
366
367 dst_row = (uint8_t *)dst + y*dst_stride + x*(format_desc->block.bits/8);
368
369 if (util_format_is_pure_uint(format))
370 format_desc->pack_rgba_uint(dst_row, dst_stride, src, src_stride, w, h);
371 else if (util_format_is_pure_sint(format))
372 format_desc->pack_rgba_sint(dst_row, dst_stride, src, src_stride, w, h);
373 else
374 format_desc->pack_rgba_float(dst_row, dst_stride, src, src_stride, w, h);
375 }
376
377
378 void
379 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)
380 {
381 const struct util_format_description *format_desc;
382 const uint8_t *src_row;
383 uint8_t *dst_row;
384
385 format_desc = util_format_description(format);
386
387 assert(x % format_desc->block.width == 0);
388 assert(y % format_desc->block.height == 0);
389
390 src_row = (const uint8_t *)src + y*src_stride + x*(format_desc->block.bits/8);
391 dst_row = dst;
392
393 format_desc->unpack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, w, h);
394 }
395
396
397 void
398 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)
399 {
400 const struct util_format_description *format_desc;
401 uint8_t *dst_row;
402 const uint8_t *src_row;
403
404 format_desc = util_format_description(format);
405
406 assert(x % format_desc->block.width == 0);
407 assert(y % format_desc->block.height == 0);
408
409 dst_row = (uint8_t *)dst + y*dst_stride + x*(format_desc->block.bits/8);
410 src_row = src;
411
412 format_desc->pack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, w, h);
413 }
414
415 /**
416 * Check if we can safely memcopy from the source format to the dest format.
417 * This basically covers the cases of a "used" channel copied to a typeless
418 * channel, plus some 1-channel cases.
419 * Examples of compatible copy formats include:
420 * b8g8r8a8_unorm -> b8g8r8x8_unorm
421 * a8r8g8b8_unorm -> x8r8g8b8_unorm
422 * b5g5r5a1_unorm -> b5g5r5x1_unorm
423 * b4g4r4a4_unorm -> b4g4r4x4_unorm
424 * l8_unorm -> r8_unorm
425 * i8_unorm -> l8_unorm
426 * i8_unorm -> a8_unorm
427 * i8_unorm -> r8_unorm
428 * l16_unorm -> r16_unorm
429 * z24_unorm_s8_uint -> z24x8_unorm
430 * s8_uint_z24_unorm -> x8z24_unorm
431 * r8g8b8a8_unorm -> r8g8b8x8_unorm
432 * a8b8g8r8_srgb -> x8b8g8r8_srgb
433 * b8g8r8a8_srgb -> b8g8r8x8_srgb
434 * a8r8g8b8_srgb -> x8r8g8b8_srgb
435 * a8b8g8r8_unorm -> x8b8g8r8_unorm
436 * r10g10b10a2_uscaled -> r10g10b10x2_uscaled
437 * r10sg10sb10sa2u_norm -> r10g10b10x2_snorm
438 */
439 boolean
440 util_is_format_compatible(const struct util_format_description *src_desc,
441 const struct util_format_description *dst_desc)
442 {
443 unsigned chan;
444
445 if (src_desc->format == dst_desc->format) {
446 return TRUE;
447 }
448
449 if (src_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN ||
450 dst_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN) {
451 return FALSE;
452 }
453
454 if (src_desc->block.bits != dst_desc->block.bits ||
455 src_desc->nr_channels != dst_desc->nr_channels ||
456 src_desc->colorspace != dst_desc->colorspace) {
457 return FALSE;
458 }
459
460 for (chan = 0; chan < 4; ++chan) {
461 if (src_desc->channel[chan].size !=
462 dst_desc->channel[chan].size) {
463 return FALSE;
464 }
465 }
466
467 for (chan = 0; chan < 4; ++chan) {
468 enum pipe_swizzle swizzle = dst_desc->swizzle[chan];
469
470 if (swizzle < 4) {
471 if (src_desc->swizzle[chan] != swizzle) {
472 return FALSE;
473 }
474 if ((src_desc->channel[swizzle].type !=
475 dst_desc->channel[swizzle].type) ||
476 (src_desc->channel[swizzle].normalized !=
477 dst_desc->channel[swizzle].normalized)) {
478 return FALSE;
479 }
480 }
481 }
482
483 return TRUE;
484 }
485
486
487 boolean
488 util_format_fits_8unorm(const struct util_format_description *format_desc)
489 {
490 unsigned chan;
491
492 /*
493 * After linearized sRGB values require more than 8bits.
494 */
495
496 if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
497 return FALSE;
498 }
499
500 switch (format_desc->layout) {
501
502 case UTIL_FORMAT_LAYOUT_S3TC:
503 /*
504 * These are straight forward.
505 */
506 return TRUE;
507 case UTIL_FORMAT_LAYOUT_RGTC:
508 if (format_desc->format == PIPE_FORMAT_RGTC1_SNORM ||
509 format_desc->format == PIPE_FORMAT_RGTC2_SNORM ||
510 format_desc->format == PIPE_FORMAT_LATC1_SNORM ||
511 format_desc->format == PIPE_FORMAT_LATC2_SNORM)
512 return FALSE;
513 return TRUE;
514 case UTIL_FORMAT_LAYOUT_BPTC:
515 if (format_desc->format == PIPE_FORMAT_BPTC_RGBA_UNORM)
516 return TRUE;
517 return FALSE;
518
519 case UTIL_FORMAT_LAYOUT_ETC:
520 if (format_desc->format == PIPE_FORMAT_ETC1_RGB8)
521 return TRUE;
522 return FALSE;
523
524 case UTIL_FORMAT_LAYOUT_PLAIN:
525 /*
526 * For these we can find a generic rule.
527 */
528
529 for (chan = 0; chan < format_desc->nr_channels; ++chan) {
530 switch (format_desc->channel[chan].type) {
531 case UTIL_FORMAT_TYPE_VOID:
532 break;
533 case UTIL_FORMAT_TYPE_UNSIGNED:
534 if (!format_desc->channel[chan].normalized ||
535 format_desc->channel[chan].size > 8) {
536 return FALSE;
537 }
538 break;
539 default:
540 return FALSE;
541 }
542 }
543 return TRUE;
544
545 default:
546 /*
547 * Handle all others on a case by case basis.
548 */
549
550 switch (format_desc->format) {
551 case PIPE_FORMAT_R1_UNORM:
552 case PIPE_FORMAT_UYVY:
553 case PIPE_FORMAT_YUYV:
554 case PIPE_FORMAT_R8G8_B8G8_UNORM:
555 case PIPE_FORMAT_G8R8_G8B8_UNORM:
556 return TRUE;
557
558 default:
559 return FALSE;
560 }
561 }
562 }
563
564
565 boolean
566 util_format_translate(enum pipe_format dst_format,
567 void *dst, unsigned dst_stride,
568 unsigned dst_x, unsigned dst_y,
569 enum pipe_format src_format,
570 const void *src, unsigned src_stride,
571 unsigned src_x, unsigned src_y,
572 unsigned width, unsigned height)
573 {
574 const struct util_format_description *dst_format_desc;
575 const struct util_format_description *src_format_desc;
576 uint8_t *dst_row;
577 const uint8_t *src_row;
578 unsigned x_step, y_step;
579 unsigned dst_step;
580 unsigned src_step;
581
582 dst_format_desc = util_format_description(dst_format);
583 src_format_desc = util_format_description(src_format);
584
585 if (util_is_format_compatible(src_format_desc, dst_format_desc)) {
586 /*
587 * Trivial case.
588 */
589
590 util_copy_rect(dst, dst_format, dst_stride, dst_x, dst_y,
591 width, height, src, (int)src_stride,
592 src_x, src_y);
593 return TRUE;
594 }
595
596 assert(dst_x % dst_format_desc->block.width == 0);
597 assert(dst_y % dst_format_desc->block.height == 0);
598 assert(src_x % src_format_desc->block.width == 0);
599 assert(src_y % src_format_desc->block.height == 0);
600
601 dst_row = (uint8_t *)dst + dst_y*dst_stride + dst_x*(dst_format_desc->block.bits/8);
602 src_row = (const uint8_t *)src + src_y*src_stride + src_x*(src_format_desc->block.bits/8);
603
604 /*
605 * This works because all pixel formats have pixel blocks with power of two
606 * sizes.
607 */
608
609 y_step = MAX2(dst_format_desc->block.height, src_format_desc->block.height);
610 x_step = MAX2(dst_format_desc->block.width, src_format_desc->block.width);
611 assert(y_step % dst_format_desc->block.height == 0);
612 assert(y_step % src_format_desc->block.height == 0);
613
614 dst_step = y_step / dst_format_desc->block.height * dst_stride;
615 src_step = y_step / src_format_desc->block.height * src_stride;
616
617 /*
618 * TODO: double formats will loose precision
619 * TODO: Add a special case for formats that are mere swizzles of each other
620 */
621
622 if (src_format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS ||
623 dst_format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
624 float *tmp_z = NULL;
625 uint8_t *tmp_s = NULL;
626
627 assert(x_step == 1);
628 assert(y_step == 1);
629
630 if (src_format_desc->unpack_z_float &&
631 dst_format_desc->pack_z_float) {
632 tmp_z = malloc(width * sizeof *tmp_z);
633 }
634
635 if (src_format_desc->unpack_s_8uint &&
636 dst_format_desc->pack_s_8uint) {
637 tmp_s = malloc(width * sizeof *tmp_s);
638 }
639
640 while (height--) {
641 if (tmp_z) {
642 util_format_unpack_z_float(src_format, tmp_z, src_row, width);
643 util_format_pack_z_float(dst_format, dst_row, tmp_z, width);
644 }
645
646 if (tmp_s) {
647 util_format_unpack_s_8uint(src_format, tmp_s, src_row, width);
648 util_format_pack_s_8uint(dst_format, dst_row, tmp_s, width);
649 }
650
651 dst_row += dst_step;
652 src_row += src_step;
653 }
654
655 free(tmp_s);
656
657 free(tmp_z);
658
659 return TRUE;
660 }
661
662 if (util_format_fits_8unorm(src_format_desc) ||
663 util_format_fits_8unorm(dst_format_desc)) {
664 unsigned tmp_stride;
665 uint8_t *tmp_row;
666
667 if (!src_format_desc->unpack_rgba_8unorm ||
668 !dst_format_desc->pack_rgba_8unorm) {
669 return FALSE;
670 }
671
672 tmp_stride = MAX2(width, x_step) * 4 * sizeof *tmp_row;
673 tmp_row = malloc(y_step * tmp_stride);
674 if (!tmp_row)
675 return FALSE;
676
677 while (height >= y_step) {
678 src_format_desc->unpack_rgba_8unorm(tmp_row, tmp_stride, src_row, src_stride, width, y_step);
679 dst_format_desc->pack_rgba_8unorm(dst_row, dst_stride, tmp_row, tmp_stride, width, y_step);
680
681 dst_row += dst_step;
682 src_row += src_step;
683 height -= y_step;
684 }
685
686 if (height) {
687 src_format_desc->unpack_rgba_8unorm(tmp_row, tmp_stride, src_row, src_stride, width, height);
688 dst_format_desc->pack_rgba_8unorm(dst_row, dst_stride, tmp_row, tmp_stride, width, height);
689 }
690
691 free(tmp_row);
692 }
693 else if (util_format_is_pure_sint(src_format) ||
694 util_format_is_pure_sint(dst_format)) {
695 unsigned tmp_stride;
696 int *tmp_row;
697
698 if (!src_format_desc->unpack_rgba_sint ||
699 !dst_format_desc->pack_rgba_sint) {
700 return FALSE;
701 }
702
703 tmp_stride = MAX2(width, x_step) * 4 * sizeof *tmp_row;
704 tmp_row = malloc(y_step * tmp_stride);
705 if (!tmp_row)
706 return FALSE;
707
708 while (height >= y_step) {
709 src_format_desc->unpack_rgba_sint(tmp_row, tmp_stride, src_row, src_stride, width, y_step);
710 dst_format_desc->pack_rgba_sint(dst_row, dst_stride, tmp_row, tmp_stride, width, y_step);
711
712 dst_row += dst_step;
713 src_row += src_step;
714 height -= y_step;
715 }
716
717 if (height) {
718 src_format_desc->unpack_rgba_sint(tmp_row, tmp_stride, src_row, src_stride, width, height);
719 dst_format_desc->pack_rgba_sint(dst_row, dst_stride, tmp_row, tmp_stride, width, height);
720 }
721
722 free(tmp_row);
723 }
724 else if (util_format_is_pure_uint(src_format) ||
725 util_format_is_pure_uint(dst_format)) {
726 unsigned tmp_stride;
727 unsigned int *tmp_row;
728
729 if (!src_format_desc->unpack_rgba_uint ||
730 !dst_format_desc->pack_rgba_uint) {
731 return FALSE;
732 }
733
734 tmp_stride = MAX2(width, x_step) * 4 * sizeof *tmp_row;
735 tmp_row = malloc(y_step * tmp_stride);
736 if (!tmp_row)
737 return FALSE;
738
739 while (height >= y_step) {
740 src_format_desc->unpack_rgba_uint(tmp_row, tmp_stride, src_row, src_stride, width, y_step);
741 dst_format_desc->pack_rgba_uint(dst_row, dst_stride, tmp_row, tmp_stride, width, y_step);
742
743 dst_row += dst_step;
744 src_row += src_step;
745 height -= y_step;
746 }
747
748 if (height) {
749 src_format_desc->unpack_rgba_uint(tmp_row, tmp_stride, src_row, src_stride, width, height);
750 dst_format_desc->pack_rgba_uint(dst_row, dst_stride, tmp_row, tmp_stride, width, height);
751 }
752
753 free(tmp_row);
754 }
755 else {
756 unsigned tmp_stride;
757 float *tmp_row;
758
759 if (!src_format_desc->unpack_rgba_float ||
760 !dst_format_desc->pack_rgba_float) {
761 return FALSE;
762 }
763
764 tmp_stride = MAX2(width, x_step) * 4 * sizeof *tmp_row;
765 tmp_row = malloc(y_step * tmp_stride);
766 if (!tmp_row)
767 return FALSE;
768
769 while (height >= y_step) {
770 src_format_desc->unpack_rgba_float(tmp_row, tmp_stride, src_row, src_stride, width, y_step);
771 dst_format_desc->pack_rgba_float(dst_row, dst_stride, tmp_row, tmp_stride, width, y_step);
772
773 dst_row += dst_step;
774 src_row += src_step;
775 height -= y_step;
776 }
777
778 if (height) {
779 src_format_desc->unpack_rgba_float(tmp_row, tmp_stride, src_row, src_stride, width, height);
780 dst_format_desc->pack_rgba_float(dst_row, dst_stride, tmp_row, tmp_stride, width, height);
781 }
782
783 free(tmp_row);
784 }
785 return TRUE;
786 }
787
788 boolean
789 util_format_translate_3d(enum pipe_format dst_format,
790 void *dst, unsigned dst_stride,
791 unsigned dst_slice_stride,
792 unsigned dst_x, unsigned dst_y,
793 unsigned dst_z,
794 enum pipe_format src_format,
795 const void *src, unsigned src_stride,
796 unsigned src_slice_stride,
797 unsigned src_x, unsigned src_y,
798 unsigned src_z, unsigned width,
799 unsigned height, unsigned depth)
800 {
801 uint8_t *dst_layer;
802 const uint8_t *src_layer;
803 unsigned z;
804 dst_layer = dst;
805 src_layer = src;
806 dst_layer += dst_z * dst_slice_stride;
807 src_layer += src_z * src_slice_stride;
808 for (z = 0; z < depth; ++z) {
809 if (!util_format_translate(dst_format, dst_layer, dst_stride,
810 dst_x, dst_y,
811 src_format, src_layer, src_stride,
812 src_x, src_y,
813 width, height))
814 return FALSE;
815
816 dst_layer += dst_slice_stride;
817 src_layer += src_slice_stride;
818 }
819 return TRUE;
820 }
821
822 void util_format_compose_swizzles(const unsigned char swz1[4],
823 const unsigned char swz2[4],
824 unsigned char dst[4])
825 {
826 unsigned i;
827
828 for (i = 0; i < 4; i++) {
829 dst[i] = swz2[i] <= PIPE_SWIZZLE_W ?
830 swz1[swz2[i]] : swz2[i];
831 }
832 }
833
834 void util_format_apply_color_swizzle(union pipe_color_union *dst,
835 const union pipe_color_union *src,
836 const unsigned char swz[4],
837 const boolean is_integer)
838 {
839 unsigned c;
840
841 if (is_integer) {
842 for (c = 0; c < 4; ++c) {
843 switch (swz[c]) {
844 case PIPE_SWIZZLE_X: dst->ui[c] = src->ui[0]; break;
845 case PIPE_SWIZZLE_Y: dst->ui[c] = src->ui[1]; break;
846 case PIPE_SWIZZLE_Z: dst->ui[c] = src->ui[2]; break;
847 case PIPE_SWIZZLE_W: dst->ui[c] = src->ui[3]; break;
848 default:
849 dst->ui[c] = (swz[c] == PIPE_SWIZZLE_1) ? 1 : 0;
850 break;
851 }
852 }
853 } else {
854 for (c = 0; c < 4; ++c) {
855 switch (swz[c]) {
856 case PIPE_SWIZZLE_X: dst->f[c] = src->f[0]; break;
857 case PIPE_SWIZZLE_Y: dst->f[c] = src->f[1]; break;
858 case PIPE_SWIZZLE_Z: dst->f[c] = src->f[2]; break;
859 case PIPE_SWIZZLE_W: dst->f[c] = src->f[3]; break;
860 default:
861 dst->f[c] = (swz[c] == PIPE_SWIZZLE_1) ? 1.0f : 0.0f;
862 break;
863 }
864 }
865 }
866 }
867
868 void pipe_swizzle_4f(float *dst, const float *src,
869 const unsigned char swz[4])
870 {
871 unsigned i;
872
873 for (i = 0; i < 4; i++) {
874 if (swz[i] <= PIPE_SWIZZLE_W)
875 dst[i] = src[swz[i]];
876 else if (swz[i] == PIPE_SWIZZLE_0)
877 dst[i] = 0;
878 else if (swz[i] == PIPE_SWIZZLE_1)
879 dst[i] = 1;
880 }
881 }
882
883 void util_format_unswizzle_4f(float *dst, const float *src,
884 const unsigned char swz[4])
885 {
886 unsigned i;
887
888 for (i = 0; i < 4; i++) {
889 switch (swz[i]) {
890 case PIPE_SWIZZLE_X:
891 dst[0] = src[i];
892 break;
893 case PIPE_SWIZZLE_Y:
894 dst[1] = src[i];
895 break;
896 case PIPE_SWIZZLE_Z:
897 dst[2] = src[i];
898 break;
899 case PIPE_SWIZZLE_W:
900 dst[3] = src[i];
901 break;
902 }
903 }
904 }
905
906 enum pipe_format
907 util_format_snorm8_to_sint8(enum pipe_format format)
908 {
909 switch (format) {
910 case PIPE_FORMAT_R8_SNORM:
911 return PIPE_FORMAT_R8_SINT;
912 case PIPE_FORMAT_R8G8_SNORM:
913 return PIPE_FORMAT_R8G8_SINT;
914 case PIPE_FORMAT_R8G8B8_SNORM:
915 return PIPE_FORMAT_R8G8B8_SINT;
916 case PIPE_FORMAT_R8G8B8A8_SNORM:
917 return PIPE_FORMAT_R8G8B8A8_SINT;
918
919 case PIPE_FORMAT_A8_SNORM:
920 return PIPE_FORMAT_A8_SINT;
921 case PIPE_FORMAT_L8_SNORM:
922 return PIPE_FORMAT_L8_SINT;
923 case PIPE_FORMAT_L8A8_SNORM:
924 return PIPE_FORMAT_L8A8_SINT;
925 case PIPE_FORMAT_I8_SNORM:
926 return PIPE_FORMAT_I8_SINT;
927
928 case PIPE_FORMAT_R8G8B8X8_SNORM:
929 return PIPE_FORMAT_R8G8B8X8_SINT;
930 case PIPE_FORMAT_R8A8_SNORM:
931 return PIPE_FORMAT_R8A8_SINT;
932 case PIPE_FORMAT_G8R8_SNORM:
933 return PIPE_FORMAT_G8R8_SINT;
934 case PIPE_FORMAT_A8B8G8R8_SNORM:
935 return PIPE_FORMAT_A8B8G8R8_SINT;
936 case PIPE_FORMAT_X8B8G8R8_SNORM:
937 return PIPE_FORMAT_X8B8G8R8_SINT;
938
939 default:
940 return format;
941 }
942 }