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