1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Pixel format accessor functions.
32 * @author Jose Fonseca <jfonseca@vmware.com>
38 #include "u_format_s3tc.h"
39 #include "u_surface.h"
41 #include "pipe/p_defines.h"
45 util_format_is_float(enum pipe_format format
)
47 const struct util_format_description
*desc
= util_format_description(format
);
55 i
= util_format_get_first_non_void_channel(format
);
60 return desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_FLOAT
? TRUE
: FALSE
;
64 /** Test if the format contains RGB, but not alpha */
66 util_format_has_alpha(enum pipe_format format
)
68 const struct util_format_description
*desc
=
69 util_format_description(format
);
71 return (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
72 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
73 desc
->swizzle
[3] != UTIL_FORMAT_SWIZZLE_1
;
78 util_format_is_luminance(enum pipe_format format
)
80 const struct util_format_description
*desc
=
81 util_format_description(format
);
83 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
84 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
85 desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_X
&&
86 desc
->swizzle
[1] == UTIL_FORMAT_SWIZZLE_X
&&
87 desc
->swizzle
[2] == UTIL_FORMAT_SWIZZLE_X
&&
88 desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_1
) {
95 util_format_is_pure_integer(enum pipe_format format
)
97 const struct util_format_description
*desc
= util_format_description(format
);
100 /* Find the first non-void channel. */
101 i
= util_format_get_first_non_void_channel(format
);
105 return desc
->channel
[i
].pure_integer
? TRUE
: FALSE
;
109 util_format_is_pure_sint(enum pipe_format format
)
111 const struct util_format_description
*desc
= util_format_description(format
);
114 i
= util_format_get_first_non_void_channel(format
);
118 return (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
&& desc
->channel
[i
].pure_integer
) ? TRUE
: FALSE
;
122 util_format_is_pure_uint(enum pipe_format format
)
124 const struct util_format_description
*desc
= util_format_description(format
);
127 i
= util_format_get_first_non_void_channel(format
);
131 return (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
&& desc
->channel
[i
].pure_integer
) ? TRUE
: FALSE
;
135 * Returns true if all non-void channels are normalized signed.
138 util_format_is_snorm(enum pipe_format format
)
140 const struct util_format_description
*desc
= util_format_description(format
);
146 i
= util_format_get_first_non_void_channel(format
);
150 return desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
&&
151 !desc
->channel
[i
].pure_integer
&&
152 desc
->channel
[i
].normalized
;
156 util_format_is_luminance_alpha(enum pipe_format format
)
158 const struct util_format_description
*desc
=
159 util_format_description(format
);
161 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
162 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
163 desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_X
&&
164 desc
->swizzle
[1] == UTIL_FORMAT_SWIZZLE_X
&&
165 desc
->swizzle
[2] == UTIL_FORMAT_SWIZZLE_X
&&
166 desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_Y
) {
174 util_format_is_intensity(enum pipe_format format
)
176 const struct util_format_description
*desc
=
177 util_format_description(format
);
179 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
180 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
181 desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_X
&&
182 desc
->swizzle
[1] == UTIL_FORMAT_SWIZZLE_X
&&
183 desc
->swizzle
[2] == UTIL_FORMAT_SWIZZLE_X
&&
184 desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_X
) {
192 util_format_is_supported(enum pipe_format format
, unsigned bind
)
194 if (util_format_is_s3tc(format
) && !util_format_s3tc_enabled
) {
198 #ifndef TEXTURE_FLOAT_ENABLED
199 if ((bind
& PIPE_BIND_RENDER_TARGET
) &&
200 format
!= PIPE_FORMAT_R9G9B9E5_FLOAT
&&
201 format
!= PIPE_FORMAT_R11G11B10_FLOAT
&&
202 util_format_is_float(format
)) {
212 * Calculates the MRD for the depth format. MRD is used in depth bias
213 * for UNORM and unbound depth buffers. When the depth buffer is floating
214 * point, the depth bias calculation does not use the MRD. However, the
215 * default MRD will be 1.0 / ((1 << 24) - 1).
218 util_get_depth_format_mrd(const struct util_format_description
*desc
)
221 * Depth buffer formats without a depth component OR scenarios
222 * without a bound depth buffer default to D24.
224 double mrd
= 1.0 / ((1 << 24) - 1);
225 unsigned depth_channel
;
230 * Some depth formats do not store the depth component in the first
231 * channel, detect the format and adjust the depth channel. Get the
232 * swizzled depth component channel.
234 depth_channel
= desc
->swizzle
[0];
236 if (desc
->channel
[depth_channel
].type
== UTIL_FORMAT_TYPE_UNSIGNED
&&
237 desc
->channel
[depth_channel
].normalized
) {
240 depth_bits
= desc
->channel
[depth_channel
].size
;
241 mrd
= 1.0 / ((1ULL << depth_bits
) - 1);
249 util_format_read_4f(enum pipe_format format
,
250 float *dst
, unsigned dst_stride
,
251 const void *src
, unsigned src_stride
,
252 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
254 const struct util_format_description
*format_desc
;
255 const uint8_t *src_row
;
258 format_desc
= util_format_description(format
);
260 assert(x
% format_desc
->block
.width
== 0);
261 assert(y
% format_desc
->block
.height
== 0);
263 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
266 format_desc
->unpack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
271 util_format_write_4f(enum pipe_format format
,
272 const float *src
, unsigned src_stride
,
273 void *dst
, unsigned dst_stride
,
274 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
276 const struct util_format_description
*format_desc
;
278 const float *src_row
;
280 format_desc
= util_format_description(format
);
282 assert(x
% format_desc
->block
.width
== 0);
283 assert(y
% format_desc
->block
.height
== 0);
285 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
288 format_desc
->pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
293 util_format_read_4ub(enum pipe_format format
, uint8_t *dst
, unsigned dst_stride
, const void *src
, unsigned src_stride
, unsigned x
, unsigned y
, unsigned w
, unsigned h
)
295 const struct util_format_description
*format_desc
;
296 const uint8_t *src_row
;
299 format_desc
= util_format_description(format
);
301 assert(x
% format_desc
->block
.width
== 0);
302 assert(y
% format_desc
->block
.height
== 0);
304 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
307 format_desc
->unpack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
312 util_format_write_4ub(enum pipe_format format
, const uint8_t *src
, unsigned src_stride
, void *dst
, unsigned dst_stride
, unsigned x
, unsigned y
, unsigned w
, unsigned h
)
314 const struct util_format_description
*format_desc
;
316 const uint8_t *src_row
;
318 format_desc
= util_format_description(format
);
320 assert(x
% format_desc
->block
.width
== 0);
321 assert(y
% format_desc
->block
.height
== 0);
323 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
326 format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
330 util_format_read_4ui(enum pipe_format format
,
331 unsigned *dst
, unsigned dst_stride
,
332 const void *src
, unsigned src_stride
,
333 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
335 const struct util_format_description
*format_desc
;
336 const uint8_t *src_row
;
339 format_desc
= util_format_description(format
);
341 assert(x
% format_desc
->block
.width
== 0);
342 assert(y
% format_desc
->block
.height
== 0);
344 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
347 format_desc
->unpack_rgba_uint(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
351 util_format_write_4ui(enum pipe_format format
,
352 const unsigned int *src
, unsigned src_stride
,
353 void *dst
, unsigned dst_stride
,
354 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
356 const struct util_format_description
*format_desc
;
358 const uint32_t *src_row
;
360 format_desc
= util_format_description(format
);
362 assert(x
% format_desc
->block
.width
== 0);
363 assert(y
% format_desc
->block
.height
== 0);
365 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
368 format_desc
->pack_rgba_uint(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
372 util_format_read_4i(enum pipe_format format
,
373 int *dst
, unsigned dst_stride
,
374 const void *src
, unsigned src_stride
,
375 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
377 const struct util_format_description
*format_desc
;
378 const uint8_t *src_row
;
381 format_desc
= util_format_description(format
);
383 assert(x
% format_desc
->block
.width
== 0);
384 assert(y
% format_desc
->block
.height
== 0);
386 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
389 format_desc
->unpack_rgba_sint(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
393 util_format_write_4i(enum pipe_format format
,
394 const int *src
, unsigned src_stride
,
395 void *dst
, unsigned dst_stride
,
396 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
398 const struct util_format_description
*format_desc
;
400 const int32_t *src_row
;
402 format_desc
= util_format_description(format
);
404 assert(x
% format_desc
->block
.width
== 0);
405 assert(y
% format_desc
->block
.height
== 0);
407 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
410 format_desc
->pack_rgba_sint(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
414 util_is_format_compatible(const struct util_format_description
*src_desc
,
415 const struct util_format_description
*dst_desc
)
419 if (src_desc
->format
== dst_desc
->format
) {
423 if (src_desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
||
424 dst_desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
) {
428 if (src_desc
->block
.bits
!= dst_desc
->block
.bits
||
429 src_desc
->nr_channels
!= dst_desc
->nr_channels
||
430 src_desc
->colorspace
!= dst_desc
->colorspace
) {
434 for (chan
= 0; chan
< 4; ++chan
) {
435 if (src_desc
->channel
[chan
].size
!=
436 dst_desc
->channel
[chan
].size
) {
441 for (chan
= 0; chan
< 4; ++chan
) {
442 enum util_format_swizzle swizzle
= dst_desc
->swizzle
[chan
];
445 if (src_desc
->swizzle
[chan
] != swizzle
) {
448 if ((src_desc
->channel
[swizzle
].type
!=
449 dst_desc
->channel
[swizzle
].type
) ||
450 (src_desc
->channel
[swizzle
].normalized
!=
451 dst_desc
->channel
[swizzle
].normalized
)) {
462 util_format_fits_8unorm(const struct util_format_description
*format_desc
)
467 * After linearized sRGB values require more than 8bits.
470 if (format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
474 switch (format_desc
->layout
) {
476 case UTIL_FORMAT_LAYOUT_S3TC
:
478 * These are straight forward.
481 case UTIL_FORMAT_LAYOUT_RGTC
:
482 if (format_desc
->format
== PIPE_FORMAT_RGTC1_SNORM
||
483 format_desc
->format
== PIPE_FORMAT_RGTC2_SNORM
||
484 format_desc
->format
== PIPE_FORMAT_LATC1_SNORM
||
485 format_desc
->format
== PIPE_FORMAT_LATC2_SNORM
)
489 case UTIL_FORMAT_LAYOUT_PLAIN
:
491 * For these we can find a generic rule.
494 for (chan
= 0; chan
< format_desc
->nr_channels
; ++chan
) {
495 switch (format_desc
->channel
[chan
].type
) {
496 case UTIL_FORMAT_TYPE_VOID
:
498 case UTIL_FORMAT_TYPE_UNSIGNED
:
499 if (!format_desc
->channel
[chan
].normalized
||
500 format_desc
->channel
[chan
].size
> 8) {
512 * Handle all others on a case by case basis.
515 switch (format_desc
->format
) {
516 case PIPE_FORMAT_R1_UNORM
:
517 case PIPE_FORMAT_UYVY
:
518 case PIPE_FORMAT_YUYV
:
519 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
520 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
531 util_format_translate(enum pipe_format dst_format
,
532 void *dst
, unsigned dst_stride
,
533 unsigned dst_x
, unsigned dst_y
,
534 enum pipe_format src_format
,
535 const void *src
, unsigned src_stride
,
536 unsigned src_x
, unsigned src_y
,
537 unsigned width
, unsigned height
)
539 const struct util_format_description
*dst_format_desc
;
540 const struct util_format_description
*src_format_desc
;
542 const uint8_t *src_row
;
543 unsigned x_step
, y_step
;
547 dst_format_desc
= util_format_description(dst_format
);
548 src_format_desc
= util_format_description(src_format
);
550 if (util_is_format_compatible(src_format_desc
, dst_format_desc
)) {
555 util_copy_rect(dst
, dst_format
, dst_stride
, dst_x
, dst_y
,
556 width
, height
, src
, (int)src_stride
,
561 assert(dst_x
% dst_format_desc
->block
.width
== 0);
562 assert(dst_y
% dst_format_desc
->block
.height
== 0);
563 assert(src_x
% src_format_desc
->block
.width
== 0);
564 assert(src_y
% src_format_desc
->block
.height
== 0);
566 dst_row
= (uint8_t *)dst
+ dst_y
*dst_stride
+ dst_x
*(dst_format_desc
->block
.bits
/8);
567 src_row
= (const uint8_t *)src
+ src_y
*src_stride
+ src_x
*(src_format_desc
->block
.bits
/8);
570 * This works because all pixel formats have pixel blocks with power of two
574 y_step
= MAX2(dst_format_desc
->block
.height
, src_format_desc
->block
.height
);
575 x_step
= MAX2(dst_format_desc
->block
.width
, src_format_desc
->block
.width
);
576 assert(y_step
% dst_format_desc
->block
.height
== 0);
577 assert(y_step
% src_format_desc
->block
.height
== 0);
579 dst_step
= y_step
/ dst_format_desc
->block
.height
* dst_stride
;
580 src_step
= y_step
/ src_format_desc
->block
.height
* src_stride
;
583 * TODO: double formats will loose precision
584 * TODO: Add a special case for formats that are mere swizzles of each other
587 if (src_format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
||
588 dst_format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) {
590 uint8_t *tmp_s
= NULL
;
595 if (src_format_desc
->unpack_z_float
&&
596 dst_format_desc
->pack_z_float
) {
597 tmp_z
= MALLOC(width
* sizeof *tmp_z
);
600 if (src_format_desc
->unpack_s_8uint
&&
601 dst_format_desc
->pack_s_8uint
) {
602 tmp_s
= MALLOC(width
* sizeof *tmp_s
);
607 src_format_desc
->unpack_z_float(tmp_z
, 0, src_row
, src_stride
, width
, 1);
608 dst_format_desc
->pack_z_float(dst_row
, dst_stride
, tmp_z
, 0, width
, 1);
612 src_format_desc
->unpack_s_8uint(tmp_s
, 0, src_row
, src_stride
, width
, 1);
613 dst_format_desc
->pack_s_8uint(dst_row
, dst_stride
, tmp_s
, 0, width
, 1);
627 if (util_format_fits_8unorm(src_format_desc
) ||
628 util_format_fits_8unorm(dst_format_desc
)) {
632 tmp_stride
= MAX2(width
, x_step
) * 4 * sizeof *tmp_row
;
633 tmp_row
= MALLOC(y_step
* tmp_stride
);
637 while (height
>= y_step
) {
638 src_format_desc
->unpack_rgba_8unorm(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, y_step
);
639 dst_format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, y_step
);
647 src_format_desc
->unpack_rgba_8unorm(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, height
);
648 dst_format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, height
);
657 tmp_stride
= MAX2(width
, x_step
) * 4 * sizeof *tmp_row
;
658 tmp_row
= MALLOC(y_step
* tmp_stride
);
662 while (height
>= y_step
) {
663 src_format_desc
->unpack_rgba_float(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, y_step
);
664 dst_format_desc
->pack_rgba_float(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, y_step
);
672 src_format_desc
->unpack_rgba_float(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, height
);
673 dst_format_desc
->pack_rgba_float(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, height
);
680 void util_format_compose_swizzles(const unsigned char swz1
[4],
681 const unsigned char swz2
[4],
682 unsigned char dst
[4])
686 for (i
= 0; i
< 4; i
++) {
687 dst
[i
] = swz2
[i
] <= UTIL_FORMAT_SWIZZLE_W
?
688 swz1
[swz2
[i
]] : swz2
[i
];
692 void util_format_apply_color_swizzle(union pipe_color_union
*dst
,
693 const union pipe_color_union
*src
,
694 const unsigned char swz
[4],
695 const boolean is_integer
)
700 for (c
= 0; c
< 4; ++c
) {
702 case PIPE_SWIZZLE_RED
: dst
->ui
[c
] = src
->ui
[0]; break;
703 case PIPE_SWIZZLE_GREEN
: dst
->ui
[c
] = src
->ui
[1]; break;
704 case PIPE_SWIZZLE_BLUE
: dst
->ui
[c
] = src
->ui
[2]; break;
705 case PIPE_SWIZZLE_ALPHA
: dst
->ui
[c
] = src
->ui
[3]; break;
707 dst
->ui
[c
] = (swz
[c
] == PIPE_SWIZZLE_ONE
) ? 1 : 0;
712 for (c
= 0; c
< 4; ++c
) {
714 case PIPE_SWIZZLE_RED
: dst
->f
[c
] = src
->f
[0]; break;
715 case PIPE_SWIZZLE_GREEN
: dst
->f
[c
] = src
->f
[1]; break;
716 case PIPE_SWIZZLE_BLUE
: dst
->f
[c
] = src
->f
[2]; break;
717 case PIPE_SWIZZLE_ALPHA
: dst
->f
[c
] = src
->f
[3]; break;
719 dst
->f
[c
] = (swz
[c
] == PIPE_SWIZZLE_ONE
) ? 1.0f
: 0.0f
;
726 void util_format_swizzle_4f(float *dst
, const float *src
,
727 const unsigned char swz
[4])
731 for (i
= 0; i
< 4; i
++) {
732 if (swz
[i
] <= UTIL_FORMAT_SWIZZLE_W
)
733 dst
[i
] = src
[swz
[i
]];
734 else if (swz
[i
] == UTIL_FORMAT_SWIZZLE_0
)
736 else if (swz
[i
] == UTIL_FORMAT_SWIZZLE_1
)
741 void util_format_unswizzle_4f(float *dst
, const float *src
,
742 const unsigned char swz
[4])
746 for (i
= 0; i
< 4; i
++) {
748 case UTIL_FORMAT_SWIZZLE_X
:
751 case UTIL_FORMAT_SWIZZLE_Y
:
754 case UTIL_FORMAT_SWIZZLE_Z
:
757 case UTIL_FORMAT_SWIZZLE_W
: