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>
39 #include "u_format_s3tc.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
;
136 util_format_is_luminance_alpha(enum pipe_format format
)
138 const struct util_format_description
*desc
=
139 util_format_description(format
);
141 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
142 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
143 desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_X
&&
144 desc
->swizzle
[1] == UTIL_FORMAT_SWIZZLE_X
&&
145 desc
->swizzle
[2] == UTIL_FORMAT_SWIZZLE_X
&&
146 desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_Y
) {
154 util_format_is_intensity(enum pipe_format format
)
156 const struct util_format_description
*desc
=
157 util_format_description(format
);
159 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
160 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
161 desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_X
&&
162 desc
->swizzle
[1] == UTIL_FORMAT_SWIZZLE_X
&&
163 desc
->swizzle
[2] == UTIL_FORMAT_SWIZZLE_X
&&
164 desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_X
) {
172 util_format_is_supported(enum pipe_format format
, unsigned bind
)
174 if (util_format_is_s3tc(format
) && !util_format_s3tc_enabled
) {
178 #ifndef TEXTURE_FLOAT_ENABLED
179 if ((bind
& PIPE_BIND_RENDER_TARGET
) &&
180 format
!= PIPE_FORMAT_R9G9B9E5_FLOAT
&&
181 format
!= PIPE_FORMAT_R11G11B10_FLOAT
&&
182 util_format_is_float(format
)) {
192 util_format_read_4f(enum pipe_format format
,
193 float *dst
, unsigned dst_stride
,
194 const void *src
, unsigned src_stride
,
195 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
197 const struct util_format_description
*format_desc
;
198 const uint8_t *src_row
;
201 format_desc
= util_format_description(format
);
203 assert(x
% format_desc
->block
.width
== 0);
204 assert(y
% format_desc
->block
.height
== 0);
206 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
209 format_desc
->unpack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
214 util_format_write_4f(enum pipe_format format
,
215 const float *src
, unsigned src_stride
,
216 void *dst
, unsigned dst_stride
,
217 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
219 const struct util_format_description
*format_desc
;
221 const float *src_row
;
223 format_desc
= util_format_description(format
);
225 assert(x
% format_desc
->block
.width
== 0);
226 assert(y
% format_desc
->block
.height
== 0);
228 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
231 format_desc
->pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
236 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
)
238 const struct util_format_description
*format_desc
;
239 const uint8_t *src_row
;
242 format_desc
= util_format_description(format
);
244 assert(x
% format_desc
->block
.width
== 0);
245 assert(y
% format_desc
->block
.height
== 0);
247 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
250 format_desc
->unpack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
255 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
)
257 const struct util_format_description
*format_desc
;
259 const uint8_t *src_row
;
261 format_desc
= util_format_description(format
);
263 assert(x
% format_desc
->block
.width
== 0);
264 assert(y
% format_desc
->block
.height
== 0);
266 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
269 format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
273 util_format_read_4ui(enum pipe_format format
,
274 unsigned *dst
, unsigned dst_stride
,
275 const void *src
, unsigned src_stride
,
276 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
278 const struct util_format_description
*format_desc
;
279 const uint8_t *src_row
;
282 format_desc
= util_format_description(format
);
284 assert(x
% format_desc
->block
.width
== 0);
285 assert(y
% format_desc
->block
.height
== 0);
287 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
290 format_desc
->unpack_rgba_uint(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
294 util_format_write_4ui(enum pipe_format format
,
295 const unsigned int *src
, unsigned src_stride
,
296 void *dst
, unsigned dst_stride
,
297 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
299 const struct util_format_description
*format_desc
;
301 const unsigned *src_row
;
303 format_desc
= util_format_description(format
);
305 assert(x
% format_desc
->block
.width
== 0);
306 assert(y
% format_desc
->block
.height
== 0);
308 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
311 format_desc
->pack_rgba_uint(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
315 util_format_read_4i(enum pipe_format format
,
316 int *dst
, unsigned dst_stride
,
317 const void *src
, unsigned src_stride
,
318 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
320 const struct util_format_description
*format_desc
;
321 const uint8_t *src_row
;
324 format_desc
= util_format_description(format
);
326 assert(x
% format_desc
->block
.width
== 0);
327 assert(y
% format_desc
->block
.height
== 0);
329 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
332 format_desc
->unpack_rgba_sint(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
336 util_format_write_4i(enum pipe_format format
,
337 const int *src
, unsigned src_stride
,
338 void *dst
, unsigned dst_stride
,
339 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
341 const struct util_format_description
*format_desc
;
345 format_desc
= util_format_description(format
);
347 assert(x
% format_desc
->block
.width
== 0);
348 assert(y
% format_desc
->block
.height
== 0);
350 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
353 format_desc
->pack_rgba_sint(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
357 util_is_format_compatible(const struct util_format_description
*src_desc
,
358 const struct util_format_description
*dst_desc
)
362 if (src_desc
->format
== dst_desc
->format
) {
366 if (src_desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
||
367 dst_desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
) {
371 if (src_desc
->block
.bits
!= dst_desc
->block
.bits
||
372 src_desc
->nr_channels
!= dst_desc
->nr_channels
||
373 src_desc
->colorspace
!= dst_desc
->colorspace
) {
377 for (chan
= 0; chan
< 4; ++chan
) {
378 if (src_desc
->channel
[chan
].size
!=
379 dst_desc
->channel
[chan
].size
) {
384 for (chan
= 0; chan
< 4; ++chan
) {
385 enum util_format_swizzle swizzle
= dst_desc
->swizzle
[chan
];
388 if (src_desc
->swizzle
[chan
] != swizzle
) {
391 if ((src_desc
->channel
[swizzle
].type
!=
392 dst_desc
->channel
[swizzle
].type
) ||
393 (src_desc
->channel
[swizzle
].normalized
!=
394 dst_desc
->channel
[swizzle
].normalized
)) {
405 util_format_fits_8unorm(const struct util_format_description
*format_desc
)
410 * After linearized sRGB values require more than 8bits.
413 if (format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
417 switch (format_desc
->layout
) {
419 case UTIL_FORMAT_LAYOUT_S3TC
:
421 * These are straight forward.
424 case UTIL_FORMAT_LAYOUT_RGTC
:
425 if (format_desc
->format
== PIPE_FORMAT_RGTC1_SNORM
||
426 format_desc
->format
== PIPE_FORMAT_RGTC2_SNORM
||
427 format_desc
->format
== PIPE_FORMAT_LATC1_SNORM
||
428 format_desc
->format
== PIPE_FORMAT_LATC2_SNORM
)
432 case UTIL_FORMAT_LAYOUT_PLAIN
:
434 * For these we can find a generic rule.
437 for (chan
= 0; chan
< format_desc
->nr_channels
; ++chan
) {
438 switch (format_desc
->channel
[chan
].type
) {
439 case UTIL_FORMAT_TYPE_VOID
:
441 case UTIL_FORMAT_TYPE_UNSIGNED
:
442 if (!format_desc
->channel
[chan
].normalized
||
443 format_desc
->channel
[chan
].size
> 8) {
455 * Handle all others on a case by case basis.
458 switch (format_desc
->format
) {
459 case PIPE_FORMAT_R1_UNORM
:
460 case PIPE_FORMAT_UYVY
:
461 case PIPE_FORMAT_YUYV
:
462 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
463 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
474 util_format_translate(enum pipe_format dst_format
,
475 void *dst
, unsigned dst_stride
,
476 unsigned dst_x
, unsigned dst_y
,
477 enum pipe_format src_format
,
478 const void *src
, unsigned src_stride
,
479 unsigned src_x
, unsigned src_y
,
480 unsigned width
, unsigned height
)
482 const struct util_format_description
*dst_format_desc
;
483 const struct util_format_description
*src_format_desc
;
485 const uint8_t *src_row
;
486 unsigned x_step
, y_step
;
490 dst_format_desc
= util_format_description(dst_format
);
491 src_format_desc
= util_format_description(src_format
);
493 if (util_is_format_compatible(src_format_desc
, dst_format_desc
)) {
498 util_copy_rect(dst
, dst_format
, dst_stride
, dst_x
, dst_y
,
499 width
, height
, src
, (int)src_stride
,
504 assert(dst_x
% dst_format_desc
->block
.width
== 0);
505 assert(dst_y
% dst_format_desc
->block
.height
== 0);
506 assert(src_x
% src_format_desc
->block
.width
== 0);
507 assert(src_y
% src_format_desc
->block
.height
== 0);
509 dst_row
= (uint8_t *)dst
+ dst_y
*dst_stride
+ dst_x
*(dst_format_desc
->block
.bits
/8);
510 src_row
= (const uint8_t *)src
+ src_y
*src_stride
+ src_x
*(src_format_desc
->block
.bits
/8);
513 * This works because all pixel formats have pixel blocks with power of two
517 y_step
= MAX2(dst_format_desc
->block
.height
, src_format_desc
->block
.height
);
518 x_step
= MAX2(dst_format_desc
->block
.width
, src_format_desc
->block
.width
);
519 assert(y_step
% dst_format_desc
->block
.height
== 0);
520 assert(y_step
% src_format_desc
->block
.height
== 0);
522 dst_step
= y_step
/ dst_format_desc
->block
.height
* dst_stride
;
523 src_step
= y_step
/ src_format_desc
->block
.height
* src_stride
;
526 * TODO: double formats will loose precision
527 * TODO: Add a special case for formats that are mere swizzles of each other
530 if (src_format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
||
531 dst_format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) {
533 uint8_t *tmp_s
= NULL
;
538 if (src_format_desc
->unpack_z_float
&&
539 dst_format_desc
->pack_z_float
) {
540 tmp_z
= MALLOC(width
* sizeof *tmp_z
);
543 if (src_format_desc
->unpack_s_8uint
&&
544 dst_format_desc
->pack_s_8uint
) {
545 tmp_s
= MALLOC(width
* sizeof *tmp_s
);
550 src_format_desc
->unpack_z_float(tmp_z
, 0, src_row
, src_stride
, width
, 1);
551 dst_format_desc
->pack_z_float(dst_row
, dst_stride
, tmp_z
, 0, width
, 1);
555 src_format_desc
->unpack_s_8uint(tmp_s
, 0, src_row
, src_stride
, width
, 1);
556 dst_format_desc
->pack_s_8uint(dst_row
, dst_stride
, tmp_s
, 0, width
, 1);
570 if (util_format_fits_8unorm(src_format_desc
) ||
571 util_format_fits_8unorm(dst_format_desc
)) {
575 tmp_stride
= MAX2(width
, x_step
) * 4 * sizeof *tmp_row
;
576 tmp_row
= MALLOC(y_step
* tmp_stride
);
580 while (height
>= y_step
) {
581 src_format_desc
->unpack_rgba_8unorm(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, y_step
);
582 dst_format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, y_step
);
590 src_format_desc
->unpack_rgba_8unorm(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, height
);
591 dst_format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, height
);
600 tmp_stride
= MAX2(width
, x_step
) * 4 * sizeof *tmp_row
;
601 tmp_row
= MALLOC(y_step
* tmp_stride
);
605 while (height
>= y_step
) {
606 src_format_desc
->unpack_rgba_float(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, y_step
);
607 dst_format_desc
->pack_rgba_float(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, y_step
);
615 src_format_desc
->unpack_rgba_float(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, height
);
616 dst_format_desc
->pack_rgba_float(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, height
);
623 void util_format_compose_swizzles(const unsigned char swz1
[4],
624 const unsigned char swz2
[4],
625 unsigned char dst
[4])
629 for (i
= 0; i
< 4; i
++) {
630 dst
[i
] = swz2
[i
] <= UTIL_FORMAT_SWIZZLE_W
?
631 swz1
[swz2
[i
]] : swz2
[i
];
635 void util_format_apply_color_swizzle(union pipe_color_union
*dst
,
636 const union pipe_color_union
*src
,
637 const unsigned char swz
[4],
638 const boolean is_integer
)
643 for (c
= 0; c
< 4; ++c
) {
645 case PIPE_SWIZZLE_RED
: dst
->ui
[c
] = src
->ui
[0]; break;
646 case PIPE_SWIZZLE_GREEN
: dst
->ui
[c
] = src
->ui
[1]; break;
647 case PIPE_SWIZZLE_BLUE
: dst
->ui
[c
] = src
->ui
[2]; break;
648 case PIPE_SWIZZLE_ALPHA
: dst
->ui
[c
] = src
->ui
[3]; break;
650 dst
->ui
[c
] = (swz
[c
] == PIPE_SWIZZLE_ONE
) ? 1 : 0;
655 for (c
= 0; c
< 4; ++c
) {
657 case PIPE_SWIZZLE_RED
: dst
->f
[c
] = src
->f
[0]; break;
658 case PIPE_SWIZZLE_GREEN
: dst
->f
[c
] = src
->f
[1]; break;
659 case PIPE_SWIZZLE_BLUE
: dst
->f
[c
] = src
->f
[2]; break;
660 case PIPE_SWIZZLE_ALPHA
: dst
->f
[c
] = src
->f
[3]; break;
662 dst
->f
[c
] = (swz
[c
] == PIPE_SWIZZLE_ONE
) ? 1.0f
: 0.0f
;
669 void util_format_swizzle_4f(float *dst
, const float *src
,
670 const unsigned char swz
[4])
674 for (i
= 0; i
< 4; i
++) {
675 if (swz
[i
] <= UTIL_FORMAT_SWIZZLE_W
)
676 dst
[i
] = src
[swz
[i
]];
677 else if (swz
[i
] == UTIL_FORMAT_SWIZZLE_0
)
679 else if (swz
[i
] == UTIL_FORMAT_SWIZZLE_1
)
684 void util_format_unswizzle_4f(float *dst
, const float *src
,
685 const unsigned char swz
[4])
689 for (i
= 0; i
< 4; i
++) {
691 case UTIL_FORMAT_SWIZZLE_X
:
694 case UTIL_FORMAT_SWIZZLE_Y
:
697 case UTIL_FORMAT_SWIZZLE_Z
:
700 case UTIL_FORMAT_SWIZZLE_W
: