12af0ed65625c8460d2adb36288135bed957bdaa
[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_4f(enum pipe_format format,
330 float *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 float *dst_row;
337
338 format_desc = util_format_description(format);
339
340 assert(x % format_desc->block.width == 0);
341 assert(y % format_desc->block.height == 0);
342
343 src_row = (const uint8_t *)src + y*src_stride + x*(format_desc->block.bits/8);
344 dst_row = dst;
345
346 format_desc->unpack_rgba_float(dst_row, dst_stride, src_row, src_stride, w, h);
347 }
348
349
350 void
351 util_format_write_4(enum pipe_format format,
352 const void *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
359 format_desc = util_format_description(format);
360
361 assert(x % format_desc->block.width == 0);
362 assert(y % format_desc->block.height == 0);
363
364 dst_row = (uint8_t *)dst + y*dst_stride + x*(format_desc->block.bits/8);
365
366 if (util_format_is_pure_uint(format))
367 format_desc->pack_rgba_uint(dst_row, dst_stride, src, src_stride, w, h);
368 else if (util_format_is_pure_sint(format))
369 format_desc->pack_rgba_sint(dst_row, dst_stride, src, src_stride, w, h);
370 else
371 format_desc->pack_rgba_float(dst_row, dst_stride, src, src_stride, w, h);
372 }
373
374
375 void
376 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)
377 {
378 const struct util_format_description *format_desc;
379 const uint8_t *src_row;
380 uint8_t *dst_row;
381
382 format_desc = util_format_description(format);
383
384 assert(x % format_desc->block.width == 0);
385 assert(y % format_desc->block.height == 0);
386
387 src_row = (const uint8_t *)src + y*src_stride + x*(format_desc->block.bits/8);
388 dst_row = dst;
389
390 format_desc->unpack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, w, h);
391 }
392
393
394 void
395 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)
396 {
397 const struct util_format_description *format_desc;
398 uint8_t *dst_row;
399 const uint8_t *src_row;
400
401 format_desc = util_format_description(format);
402
403 assert(x % format_desc->block.width == 0);
404 assert(y % format_desc->block.height == 0);
405
406 dst_row = (uint8_t *)dst + y*dst_stride + x*(format_desc->block.bits/8);
407 src_row = src;
408
409 format_desc->pack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, w, h);
410 }
411
412 void
413 util_format_read_4ui(enum pipe_format format,
414 unsigned *dst, unsigned dst_stride,
415 const void *src, unsigned src_stride,
416 unsigned x, unsigned y, unsigned w, unsigned h)
417 {
418 const struct util_format_description *format_desc;
419 const uint8_t *src_row;
420 uint32_t *dst_row;
421
422 format_desc = util_format_description(format);
423
424 assert(x % format_desc->block.width == 0);
425 assert(y % format_desc->block.height == 0);
426
427 src_row = (const uint8_t *)src + y*src_stride + x*(format_desc->block.bits/8);
428 dst_row = dst;
429
430 format_desc->unpack_rgba_uint(dst_row, dst_stride, src_row, src_stride, w, h);
431 }
432
433 void
434 util_format_read_4i(enum pipe_format format,
435 int *dst, unsigned dst_stride,
436 const void *src, unsigned src_stride,
437 unsigned x, unsigned y, unsigned w, unsigned h)
438 {
439 const struct util_format_description *format_desc;
440 const uint8_t *src_row;
441 int32_t *dst_row;
442
443 format_desc = util_format_description(format);
444
445 assert(x % format_desc->block.width == 0);
446 assert(y % format_desc->block.height == 0);
447
448 src_row = (const uint8_t *)src + y*src_stride + x*(format_desc->block.bits/8);
449 dst_row = dst;
450
451 format_desc->unpack_rgba_sint(dst_row, dst_stride, src_row, src_stride, w, h);
452 }
453
454 /**
455 * Check if we can safely memcopy from the source format to the dest format.
456 * This basically covers the cases of a "used" channel copied to a typeless
457 * channel, plus some 1-channel cases.
458 * Examples of compatible copy formats include:
459 * b8g8r8a8_unorm -> b8g8r8x8_unorm
460 * a8r8g8b8_unorm -> x8r8g8b8_unorm
461 * b5g5r5a1_unorm -> b5g5r5x1_unorm
462 * b4g4r4a4_unorm -> b4g4r4x4_unorm
463 * l8_unorm -> r8_unorm
464 * i8_unorm -> l8_unorm
465 * i8_unorm -> a8_unorm
466 * i8_unorm -> r8_unorm
467 * l16_unorm -> r16_unorm
468 * z24_unorm_s8_uint -> z24x8_unorm
469 * s8_uint_z24_unorm -> x8z24_unorm
470 * r8g8b8a8_unorm -> r8g8b8x8_unorm
471 * a8b8g8r8_srgb -> x8b8g8r8_srgb
472 * b8g8r8a8_srgb -> b8g8r8x8_srgb
473 * a8r8g8b8_srgb -> x8r8g8b8_srgb
474 * a8b8g8r8_unorm -> x8b8g8r8_unorm
475 * r10g10b10a2_uscaled -> r10g10b10x2_uscaled
476 * r10sg10sb10sa2u_norm -> r10g10b10x2_snorm
477 */
478 boolean
479 util_is_format_compatible(const struct util_format_description *src_desc,
480 const struct util_format_description *dst_desc)
481 {
482 unsigned chan;
483
484 if (src_desc->format == dst_desc->format) {
485 return TRUE;
486 }
487
488 if (src_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN ||
489 dst_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN) {
490 return FALSE;
491 }
492
493 if (src_desc->block.bits != dst_desc->block.bits ||
494 src_desc->nr_channels != dst_desc->nr_channels ||
495 src_desc->colorspace != dst_desc->colorspace) {
496 return FALSE;
497 }
498
499 for (chan = 0; chan < 4; ++chan) {
500 if (src_desc->channel[chan].size !=
501 dst_desc->channel[chan].size) {
502 return FALSE;
503 }
504 }
505
506 for (chan = 0; chan < 4; ++chan) {
507 enum pipe_swizzle swizzle = dst_desc->swizzle[chan];
508
509 if (swizzle < 4) {
510 if (src_desc->swizzle[chan] != swizzle) {
511 return FALSE;
512 }
513 if ((src_desc->channel[swizzle].type !=
514 dst_desc->channel[swizzle].type) ||
515 (src_desc->channel[swizzle].normalized !=
516 dst_desc->channel[swizzle].normalized)) {
517 return FALSE;
518 }
519 }
520 }
521
522 return TRUE;
523 }
524
525
526 boolean
527 util_format_fits_8unorm(const struct util_format_description *format_desc)
528 {
529 unsigned chan;
530
531 /*
532 * After linearized sRGB values require more than 8bits.
533 */
534
535 if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
536 return FALSE;
537 }
538
539 switch (format_desc->layout) {
540
541 case UTIL_FORMAT_LAYOUT_S3TC:
542 /*
543 * These are straight forward.
544 */
545 return TRUE;
546 case UTIL_FORMAT_LAYOUT_RGTC:
547 if (format_desc->format == PIPE_FORMAT_RGTC1_SNORM ||
548 format_desc->format == PIPE_FORMAT_RGTC2_SNORM ||
549 format_desc->format == PIPE_FORMAT_LATC1_SNORM ||
550 format_desc->format == PIPE_FORMAT_LATC2_SNORM)
551 return FALSE;
552 return TRUE;
553 case UTIL_FORMAT_LAYOUT_BPTC:
554 if (format_desc->format == PIPE_FORMAT_BPTC_RGBA_UNORM)
555 return TRUE;
556 return FALSE;
557
558 case UTIL_FORMAT_LAYOUT_ETC:
559 if (format_desc->format == PIPE_FORMAT_ETC1_RGB8)
560 return TRUE;
561 return FALSE;
562
563 case UTIL_FORMAT_LAYOUT_PLAIN:
564 /*
565 * For these we can find a generic rule.
566 */
567
568 for (chan = 0; chan < format_desc->nr_channels; ++chan) {
569 switch (format_desc->channel[chan].type) {
570 case UTIL_FORMAT_TYPE_VOID:
571 break;
572 case UTIL_FORMAT_TYPE_UNSIGNED:
573 if (!format_desc->channel[chan].normalized ||
574 format_desc->channel[chan].size > 8) {
575 return FALSE;
576 }
577 break;
578 default:
579 return FALSE;
580 }
581 }
582 return TRUE;
583
584 default:
585 /*
586 * Handle all others on a case by case basis.
587 */
588
589 switch (format_desc->format) {
590 case PIPE_FORMAT_R1_UNORM:
591 case PIPE_FORMAT_UYVY:
592 case PIPE_FORMAT_YUYV:
593 case PIPE_FORMAT_R8G8_B8G8_UNORM:
594 case PIPE_FORMAT_G8R8_G8B8_UNORM:
595 return TRUE;
596
597 default:
598 return FALSE;
599 }
600 }
601 }
602
603
604 boolean
605 util_format_translate(enum pipe_format dst_format,
606 void *dst, unsigned dst_stride,
607 unsigned dst_x, unsigned dst_y,
608 enum pipe_format src_format,
609 const void *src, unsigned src_stride,
610 unsigned src_x, unsigned src_y,
611 unsigned width, unsigned height)
612 {
613 const struct util_format_description *dst_format_desc;
614 const struct util_format_description *src_format_desc;
615 uint8_t *dst_row;
616 const uint8_t *src_row;
617 unsigned x_step, y_step;
618 unsigned dst_step;
619 unsigned src_step;
620
621 dst_format_desc = util_format_description(dst_format);
622 src_format_desc = util_format_description(src_format);
623
624 if (util_is_format_compatible(src_format_desc, dst_format_desc)) {
625 /*
626 * Trivial case.
627 */
628
629 util_copy_rect(dst, dst_format, dst_stride, dst_x, dst_y,
630 width, height, src, (int)src_stride,
631 src_x, src_y);
632 return TRUE;
633 }
634
635 assert(dst_x % dst_format_desc->block.width == 0);
636 assert(dst_y % dst_format_desc->block.height == 0);
637 assert(src_x % src_format_desc->block.width == 0);
638 assert(src_y % src_format_desc->block.height == 0);
639
640 dst_row = (uint8_t *)dst + dst_y*dst_stride + dst_x*(dst_format_desc->block.bits/8);
641 src_row = (const uint8_t *)src + src_y*src_stride + src_x*(src_format_desc->block.bits/8);
642
643 /*
644 * This works because all pixel formats have pixel blocks with power of two
645 * sizes.
646 */
647
648 y_step = MAX2(dst_format_desc->block.height, src_format_desc->block.height);
649 x_step = MAX2(dst_format_desc->block.width, src_format_desc->block.width);
650 assert(y_step % dst_format_desc->block.height == 0);
651 assert(y_step % src_format_desc->block.height == 0);
652
653 dst_step = y_step / dst_format_desc->block.height * dst_stride;
654 src_step = y_step / src_format_desc->block.height * src_stride;
655
656 /*
657 * TODO: double formats will loose precision
658 * TODO: Add a special case for formats that are mere swizzles of each other
659 */
660
661 if (src_format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS ||
662 dst_format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
663 float *tmp_z = NULL;
664 uint8_t *tmp_s = NULL;
665
666 assert(x_step == 1);
667 assert(y_step == 1);
668
669 if (src_format_desc->unpack_z_float &&
670 dst_format_desc->pack_z_float) {
671 tmp_z = malloc(width * sizeof *tmp_z);
672 }
673
674 if (src_format_desc->unpack_s_8uint &&
675 dst_format_desc->pack_s_8uint) {
676 tmp_s = malloc(width * sizeof *tmp_s);
677 }
678
679 while (height--) {
680 if (tmp_z) {
681 util_format_unpack_z_float(src_format, tmp_z, src_row, width);
682 util_format_pack_z_float(dst_format, dst_row, tmp_z, width);
683 }
684
685 if (tmp_s) {
686 util_format_unpack_s_8uint(src_format, tmp_s, src_row, width);
687 util_format_pack_s_8uint(dst_format, dst_row, tmp_s, width);
688 }
689
690 dst_row += dst_step;
691 src_row += src_step;
692 }
693
694 free(tmp_s);
695
696 free(tmp_z);
697
698 return TRUE;
699 }
700
701 if (util_format_fits_8unorm(src_format_desc) ||
702 util_format_fits_8unorm(dst_format_desc)) {
703 unsigned tmp_stride;
704 uint8_t *tmp_row;
705
706 if (!src_format_desc->unpack_rgba_8unorm ||
707 !dst_format_desc->pack_rgba_8unorm) {
708 return FALSE;
709 }
710
711 tmp_stride = MAX2(width, x_step) * 4 * sizeof *tmp_row;
712 tmp_row = malloc(y_step * tmp_stride);
713 if (!tmp_row)
714 return FALSE;
715
716 while (height >= y_step) {
717 src_format_desc->unpack_rgba_8unorm(tmp_row, tmp_stride, src_row, src_stride, width, y_step);
718 dst_format_desc->pack_rgba_8unorm(dst_row, dst_stride, tmp_row, tmp_stride, width, y_step);
719
720 dst_row += dst_step;
721 src_row += src_step;
722 height -= y_step;
723 }
724
725 if (height) {
726 src_format_desc->unpack_rgba_8unorm(tmp_row, tmp_stride, src_row, src_stride, width, height);
727 dst_format_desc->pack_rgba_8unorm(dst_row, dst_stride, tmp_row, tmp_stride, width, height);
728 }
729
730 free(tmp_row);
731 }
732 else if (util_format_is_pure_sint(src_format) ||
733 util_format_is_pure_sint(dst_format)) {
734 unsigned tmp_stride;
735 int *tmp_row;
736
737 if (!src_format_desc->unpack_rgba_sint ||
738 !dst_format_desc->pack_rgba_sint) {
739 return FALSE;
740 }
741
742 tmp_stride = MAX2(width, x_step) * 4 * sizeof *tmp_row;
743 tmp_row = malloc(y_step * tmp_stride);
744 if (!tmp_row)
745 return FALSE;
746
747 while (height >= y_step) {
748 src_format_desc->unpack_rgba_sint(tmp_row, tmp_stride, src_row, src_stride, width, y_step);
749 dst_format_desc->pack_rgba_sint(dst_row, dst_stride, tmp_row, tmp_stride, width, y_step);
750
751 dst_row += dst_step;
752 src_row += src_step;
753 height -= y_step;
754 }
755
756 if (height) {
757 src_format_desc->unpack_rgba_sint(tmp_row, tmp_stride, src_row, src_stride, width, height);
758 dst_format_desc->pack_rgba_sint(dst_row, dst_stride, tmp_row, tmp_stride, width, height);
759 }
760
761 free(tmp_row);
762 }
763 else if (util_format_is_pure_uint(src_format) ||
764 util_format_is_pure_uint(dst_format)) {
765 unsigned tmp_stride;
766 unsigned int *tmp_row;
767
768 if (!src_format_desc->unpack_rgba_uint ||
769 !dst_format_desc->pack_rgba_uint) {
770 return FALSE;
771 }
772
773 tmp_stride = MAX2(width, x_step) * 4 * sizeof *tmp_row;
774 tmp_row = malloc(y_step * tmp_stride);
775 if (!tmp_row)
776 return FALSE;
777
778 while (height >= y_step) {
779 src_format_desc->unpack_rgba_uint(tmp_row, tmp_stride, src_row, src_stride, width, y_step);
780 dst_format_desc->pack_rgba_uint(dst_row, dst_stride, tmp_row, tmp_stride, width, y_step);
781
782 dst_row += dst_step;
783 src_row += src_step;
784 height -= y_step;
785 }
786
787 if (height) {
788 src_format_desc->unpack_rgba_uint(tmp_row, tmp_stride, src_row, src_stride, width, height);
789 dst_format_desc->pack_rgba_uint(dst_row, dst_stride, tmp_row, tmp_stride, width, height);
790 }
791
792 free(tmp_row);
793 }
794 else {
795 unsigned tmp_stride;
796 float *tmp_row;
797
798 if (!src_format_desc->unpack_rgba_float ||
799 !dst_format_desc->pack_rgba_float) {
800 return FALSE;
801 }
802
803 tmp_stride = MAX2(width, x_step) * 4 * sizeof *tmp_row;
804 tmp_row = malloc(y_step * tmp_stride);
805 if (!tmp_row)
806 return FALSE;
807
808 while (height >= y_step) {
809 src_format_desc->unpack_rgba_float(tmp_row, tmp_stride, src_row, src_stride, width, y_step);
810 dst_format_desc->pack_rgba_float(dst_row, dst_stride, tmp_row, tmp_stride, width, y_step);
811
812 dst_row += dst_step;
813 src_row += src_step;
814 height -= y_step;
815 }
816
817 if (height) {
818 src_format_desc->unpack_rgba_float(tmp_row, tmp_stride, src_row, src_stride, width, height);
819 dst_format_desc->pack_rgba_float(dst_row, dst_stride, tmp_row, tmp_stride, width, height);
820 }
821
822 free(tmp_row);
823 }
824 return TRUE;
825 }
826
827 boolean
828 util_format_translate_3d(enum pipe_format dst_format,
829 void *dst, unsigned dst_stride,
830 unsigned dst_slice_stride,
831 unsigned dst_x, unsigned dst_y,
832 unsigned dst_z,
833 enum pipe_format src_format,
834 const void *src, unsigned src_stride,
835 unsigned src_slice_stride,
836 unsigned src_x, unsigned src_y,
837 unsigned src_z, unsigned width,
838 unsigned height, unsigned depth)
839 {
840 uint8_t *dst_layer;
841 const uint8_t *src_layer;
842 unsigned z;
843 dst_layer = dst;
844 src_layer = src;
845 dst_layer += dst_z * dst_slice_stride;
846 src_layer += src_z * src_slice_stride;
847 for (z = 0; z < depth; ++z) {
848 if (!util_format_translate(dst_format, dst_layer, dst_stride,
849 dst_x, dst_y,
850 src_format, src_layer, src_stride,
851 src_x, src_y,
852 width, height))
853 return FALSE;
854
855 dst_layer += dst_slice_stride;
856 src_layer += src_slice_stride;
857 }
858 return TRUE;
859 }
860
861 void util_format_compose_swizzles(const unsigned char swz1[4],
862 const unsigned char swz2[4],
863 unsigned char dst[4])
864 {
865 unsigned i;
866
867 for (i = 0; i < 4; i++) {
868 dst[i] = swz2[i] <= PIPE_SWIZZLE_W ?
869 swz1[swz2[i]] : swz2[i];
870 }
871 }
872
873 void util_format_apply_color_swizzle(union pipe_color_union *dst,
874 const union pipe_color_union *src,
875 const unsigned char swz[4],
876 const boolean is_integer)
877 {
878 unsigned c;
879
880 if (is_integer) {
881 for (c = 0; c < 4; ++c) {
882 switch (swz[c]) {
883 case PIPE_SWIZZLE_X: dst->ui[c] = src->ui[0]; break;
884 case PIPE_SWIZZLE_Y: dst->ui[c] = src->ui[1]; break;
885 case PIPE_SWIZZLE_Z: dst->ui[c] = src->ui[2]; break;
886 case PIPE_SWIZZLE_W: dst->ui[c] = src->ui[3]; break;
887 default:
888 dst->ui[c] = (swz[c] == PIPE_SWIZZLE_1) ? 1 : 0;
889 break;
890 }
891 }
892 } else {
893 for (c = 0; c < 4; ++c) {
894 switch (swz[c]) {
895 case PIPE_SWIZZLE_X: dst->f[c] = src->f[0]; break;
896 case PIPE_SWIZZLE_Y: dst->f[c] = src->f[1]; break;
897 case PIPE_SWIZZLE_Z: dst->f[c] = src->f[2]; break;
898 case PIPE_SWIZZLE_W: dst->f[c] = src->f[3]; break;
899 default:
900 dst->f[c] = (swz[c] == PIPE_SWIZZLE_1) ? 1.0f : 0.0f;
901 break;
902 }
903 }
904 }
905 }
906
907 void pipe_swizzle_4f(float *dst, const float *src,
908 const unsigned char swz[4])
909 {
910 unsigned i;
911
912 for (i = 0; i < 4; i++) {
913 if (swz[i] <= PIPE_SWIZZLE_W)
914 dst[i] = src[swz[i]];
915 else if (swz[i] == PIPE_SWIZZLE_0)
916 dst[i] = 0;
917 else if (swz[i] == PIPE_SWIZZLE_1)
918 dst[i] = 1;
919 }
920 }
921
922 void util_format_unswizzle_4f(float *dst, const float *src,
923 const unsigned char swz[4])
924 {
925 unsigned i;
926
927 for (i = 0; i < 4; i++) {
928 switch (swz[i]) {
929 case PIPE_SWIZZLE_X:
930 dst[0] = src[i];
931 break;
932 case PIPE_SWIZZLE_Y:
933 dst[1] = src[i];
934 break;
935 case PIPE_SWIZZLE_Z:
936 dst[2] = src[i];
937 break;
938 case PIPE_SWIZZLE_W:
939 dst[3] = src[i];
940 break;
941 }
942 }
943 }
944
945 enum pipe_format
946 util_format_snorm8_to_sint8(enum pipe_format format)
947 {
948 switch (format) {
949 case PIPE_FORMAT_R8_SNORM:
950 return PIPE_FORMAT_R8_SINT;
951 case PIPE_FORMAT_R8G8_SNORM:
952 return PIPE_FORMAT_R8G8_SINT;
953 case PIPE_FORMAT_R8G8B8_SNORM:
954 return PIPE_FORMAT_R8G8B8_SINT;
955 case PIPE_FORMAT_R8G8B8A8_SNORM:
956 return PIPE_FORMAT_R8G8B8A8_SINT;
957
958 case PIPE_FORMAT_A8_SNORM:
959 return PIPE_FORMAT_A8_SINT;
960 case PIPE_FORMAT_L8_SNORM:
961 return PIPE_FORMAT_L8_SINT;
962 case PIPE_FORMAT_L8A8_SNORM:
963 return PIPE_FORMAT_L8A8_SINT;
964 case PIPE_FORMAT_I8_SNORM:
965 return PIPE_FORMAT_I8_SINT;
966
967 case PIPE_FORMAT_R8G8B8X8_SNORM:
968 return PIPE_FORMAT_R8G8B8X8_SINT;
969 case PIPE_FORMAT_R8A8_SNORM:
970 return PIPE_FORMAT_R8A8_SINT;
971 case PIPE_FORMAT_G8R8_SNORM:
972 return PIPE_FORMAT_G8R8_SINT;
973 case PIPE_FORMAT_A8B8G8R8_SNORM:
974 return PIPE_FORMAT_A8B8G8R8_SINT;
975 case PIPE_FORMAT_X8B8G8R8_SNORM:
976 return PIPE_FORMAT_X8B8G8R8_SINT;
977
978 default:
979 return format;
980 }
981 }