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