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_is_rgb_no_alpha(enum pipe_format format
)
68 const struct util_format_description
*desc
=
69 util_format_description(format
);
71 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
72 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
73 desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_1
) {
81 util_format_is_luminance(enum pipe_format format
)
83 const struct util_format_description
*desc
=
84 util_format_description(format
);
86 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
87 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
88 desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_X
&&
89 desc
->swizzle
[1] == UTIL_FORMAT_SWIZZLE_X
&&
90 desc
->swizzle
[2] == UTIL_FORMAT_SWIZZLE_X
&&
91 desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_1
) {
98 util_format_is_pure_integer(enum pipe_format format
)
100 const struct util_format_description
*desc
= util_format_description(format
);
103 /* Find the first non-void channel. */
104 i
= util_format_get_first_non_void_channel(format
);
108 return desc
->channel
[i
].pure_integer
? TRUE
: FALSE
;
112 util_format_is_pure_sint(enum pipe_format format
)
114 const struct util_format_description
*desc
= util_format_description(format
);
117 i
= util_format_get_first_non_void_channel(format
);
121 return (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
&& desc
->channel
[i
].pure_integer
) ? TRUE
: FALSE
;
125 util_format_is_pure_uint(enum pipe_format format
)
127 const struct util_format_description
*desc
= util_format_description(format
);
130 i
= util_format_get_first_non_void_channel(format
);
134 return (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
&& desc
->channel
[i
].pure_integer
) ? TRUE
: FALSE
;
139 util_format_is_luminance_alpha(enum pipe_format format
)
141 const struct util_format_description
*desc
=
142 util_format_description(format
);
144 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
145 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
146 desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_X
&&
147 desc
->swizzle
[1] == UTIL_FORMAT_SWIZZLE_X
&&
148 desc
->swizzle
[2] == UTIL_FORMAT_SWIZZLE_X
&&
149 desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_Y
) {
157 util_format_is_intensity(enum pipe_format format
)
159 const struct util_format_description
*desc
=
160 util_format_description(format
);
162 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
163 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
164 desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_X
&&
165 desc
->swizzle
[1] == UTIL_FORMAT_SWIZZLE_X
&&
166 desc
->swizzle
[2] == UTIL_FORMAT_SWIZZLE_X
&&
167 desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_X
) {
175 util_format_is_supported(enum pipe_format format
, unsigned bind
)
177 if (util_format_is_s3tc(format
) && !util_format_s3tc_enabled
) {
181 #ifndef TEXTURE_FLOAT_ENABLED
182 if ((bind
& PIPE_BIND_RENDER_TARGET
) &&
183 format
!= PIPE_FORMAT_R9G9B9E5_FLOAT
&&
184 format
!= PIPE_FORMAT_R11G11B10_FLOAT
&&
185 util_format_is_float(format
)) {
195 util_format_read_4f(enum pipe_format format
,
196 float *dst
, unsigned dst_stride
,
197 const void *src
, unsigned src_stride
,
198 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
200 const struct util_format_description
*format_desc
;
201 const uint8_t *src_row
;
204 format_desc
= util_format_description(format
);
206 assert(x
% format_desc
->block
.width
== 0);
207 assert(y
% format_desc
->block
.height
== 0);
209 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
212 format_desc
->unpack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
217 util_format_write_4f(enum pipe_format format
,
218 const float *src
, unsigned src_stride
,
219 void *dst
, unsigned dst_stride
,
220 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
222 const struct util_format_description
*format_desc
;
224 const float *src_row
;
226 format_desc
= util_format_description(format
);
228 assert(x
% format_desc
->block
.width
== 0);
229 assert(y
% format_desc
->block
.height
== 0);
231 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
234 format_desc
->pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
239 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
)
241 const struct util_format_description
*format_desc
;
242 const uint8_t *src_row
;
245 format_desc
= util_format_description(format
);
247 assert(x
% format_desc
->block
.width
== 0);
248 assert(y
% format_desc
->block
.height
== 0);
250 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
253 format_desc
->unpack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
258 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
)
260 const struct util_format_description
*format_desc
;
262 const uint8_t *src_row
;
264 format_desc
= util_format_description(format
);
266 assert(x
% format_desc
->block
.width
== 0);
267 assert(y
% format_desc
->block
.height
== 0);
269 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
272 format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
276 util_format_read_4ui(enum pipe_format format
,
277 unsigned *dst
, unsigned dst_stride
,
278 const void *src
, unsigned src_stride
,
279 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
281 const struct util_format_description
*format_desc
;
282 const uint8_t *src_row
;
285 format_desc
= util_format_description(format
);
287 assert(x
% format_desc
->block
.width
== 0);
288 assert(y
% format_desc
->block
.height
== 0);
290 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
293 format_desc
->unpack_rgba_uint(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
297 util_format_write_4ui(enum pipe_format format
,
298 const unsigned int *src
, unsigned src_stride
,
299 void *dst
, unsigned dst_stride
,
300 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
302 const struct util_format_description
*format_desc
;
304 const unsigned *src_row
;
306 format_desc
= util_format_description(format
);
308 assert(x
% format_desc
->block
.width
== 0);
309 assert(y
% format_desc
->block
.height
== 0);
311 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
314 format_desc
->pack_rgba_uint(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
318 util_format_read_4i(enum pipe_format format
,
319 int *dst
, unsigned dst_stride
,
320 const void *src
, unsigned src_stride
,
321 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
323 const struct util_format_description
*format_desc
;
324 const uint8_t *src_row
;
327 format_desc
= util_format_description(format
);
329 assert(x
% format_desc
->block
.width
== 0);
330 assert(y
% format_desc
->block
.height
== 0);
332 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
335 format_desc
->unpack_rgba_sint(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
339 util_format_write_4i(enum pipe_format format
,
340 const int *src
, unsigned src_stride
,
341 void *dst
, unsigned dst_stride
,
342 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
344 const struct util_format_description
*format_desc
;
348 format_desc
= util_format_description(format
);
350 assert(x
% format_desc
->block
.width
== 0);
351 assert(y
% format_desc
->block
.height
== 0);
353 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
356 format_desc
->pack_rgba_sint(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
360 util_is_format_compatible(const struct util_format_description
*src_desc
,
361 const struct util_format_description
*dst_desc
)
365 if (src_desc
->format
== dst_desc
->format
) {
369 if (src_desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
||
370 dst_desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
) {
374 if (src_desc
->block
.bits
!= dst_desc
->block
.bits
||
375 src_desc
->nr_channels
!= dst_desc
->nr_channels
||
376 src_desc
->colorspace
!= dst_desc
->colorspace
) {
380 for (chan
= 0; chan
< 4; ++chan
) {
381 if (src_desc
->channel
[chan
].size
!=
382 dst_desc
->channel
[chan
].size
) {
387 for (chan
= 0; chan
< 4; ++chan
) {
388 enum util_format_swizzle swizzle
= dst_desc
->swizzle
[chan
];
391 if (src_desc
->swizzle
[chan
] != swizzle
) {
394 if ((src_desc
->channel
[swizzle
].type
!=
395 dst_desc
->channel
[swizzle
].type
) ||
396 (src_desc
->channel
[swizzle
].normalized
!=
397 dst_desc
->channel
[swizzle
].normalized
)) {
408 util_format_fits_8unorm(const struct util_format_description
*format_desc
)
413 * After linearized sRGB values require more than 8bits.
416 if (format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
420 switch (format_desc
->layout
) {
422 case UTIL_FORMAT_LAYOUT_S3TC
:
424 * These are straight forward.
427 case UTIL_FORMAT_LAYOUT_RGTC
:
428 if (format_desc
->format
== PIPE_FORMAT_RGTC1_SNORM
||
429 format_desc
->format
== PIPE_FORMAT_RGTC2_SNORM
||
430 format_desc
->format
== PIPE_FORMAT_LATC1_SNORM
||
431 format_desc
->format
== PIPE_FORMAT_LATC2_SNORM
)
435 case UTIL_FORMAT_LAYOUT_PLAIN
:
437 * For these we can find a generic rule.
440 for (chan
= 0; chan
< format_desc
->nr_channels
; ++chan
) {
441 switch (format_desc
->channel
[chan
].type
) {
442 case UTIL_FORMAT_TYPE_VOID
:
444 case UTIL_FORMAT_TYPE_UNSIGNED
:
445 if (!format_desc
->channel
[chan
].normalized
||
446 format_desc
->channel
[chan
].size
> 8) {
458 * Handle all others on a case by case basis.
461 switch (format_desc
->format
) {
462 case PIPE_FORMAT_R1_UNORM
:
463 case PIPE_FORMAT_UYVY
:
464 case PIPE_FORMAT_YUYV
:
465 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
466 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
477 util_format_translate(enum pipe_format dst_format
,
478 void *dst
, unsigned dst_stride
,
479 unsigned dst_x
, unsigned dst_y
,
480 enum pipe_format src_format
,
481 const void *src
, unsigned src_stride
,
482 unsigned src_x
, unsigned src_y
,
483 unsigned width
, unsigned height
)
485 const struct util_format_description
*dst_format_desc
;
486 const struct util_format_description
*src_format_desc
;
488 const uint8_t *src_row
;
489 unsigned x_step
, y_step
;
493 dst_format_desc
= util_format_description(dst_format
);
494 src_format_desc
= util_format_description(src_format
);
496 if (util_is_format_compatible(src_format_desc
, dst_format_desc
)) {
501 util_copy_rect(dst
, dst_format
, dst_stride
, dst_x
, dst_y
,
502 width
, height
, src
, (int)src_stride
,
507 assert(dst_x
% dst_format_desc
->block
.width
== 0);
508 assert(dst_y
% dst_format_desc
->block
.height
== 0);
509 assert(src_x
% src_format_desc
->block
.width
== 0);
510 assert(src_y
% src_format_desc
->block
.height
== 0);
512 dst_row
= (uint8_t *)dst
+ dst_y
*dst_stride
+ dst_x
*(dst_format_desc
->block
.bits
/8);
513 src_row
= (const uint8_t *)src
+ src_y
*src_stride
+ src_x
*(src_format_desc
->block
.bits
/8);
516 * This works because all pixel formats have pixel blocks with power of two
520 y_step
= MAX2(dst_format_desc
->block
.height
, src_format_desc
->block
.height
);
521 x_step
= MAX2(dst_format_desc
->block
.width
, src_format_desc
->block
.width
);
522 assert(y_step
% dst_format_desc
->block
.height
== 0);
523 assert(y_step
% src_format_desc
->block
.height
== 0);
525 dst_step
= y_step
/ dst_format_desc
->block
.height
* dst_stride
;
526 src_step
= y_step
/ src_format_desc
->block
.height
* src_stride
;
529 * TODO: double formats will loose precision
530 * TODO: Add a special case for formats that are mere swizzles of each other
533 if (src_format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
||
534 dst_format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) {
536 uint8_t *tmp_s
= NULL
;
541 if (src_format_desc
->unpack_z_float
&&
542 dst_format_desc
->pack_z_float
) {
543 tmp_z
= MALLOC(width
* sizeof *tmp_z
);
546 if (src_format_desc
->unpack_s_8uint
&&
547 dst_format_desc
->pack_s_8uint
) {
548 tmp_s
= MALLOC(width
* sizeof *tmp_s
);
553 src_format_desc
->unpack_z_float(tmp_z
, 0, src_row
, src_stride
, width
, 1);
554 dst_format_desc
->pack_z_float(dst_row
, dst_stride
, tmp_z
, 0, width
, 1);
558 src_format_desc
->unpack_s_8uint(tmp_s
, 0, src_row
, src_stride
, width
, 1);
559 dst_format_desc
->pack_s_8uint(dst_row
, dst_stride
, tmp_s
, 0, width
, 1);
573 if (util_format_fits_8unorm(src_format_desc
) ||
574 util_format_fits_8unorm(dst_format_desc
)) {
578 tmp_stride
= MAX2(width
, x_step
) * 4 * sizeof *tmp_row
;
579 tmp_row
= MALLOC(y_step
* tmp_stride
);
583 while (height
>= y_step
) {
584 src_format_desc
->unpack_rgba_8unorm(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, y_step
);
585 dst_format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, y_step
);
593 src_format_desc
->unpack_rgba_8unorm(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, height
);
594 dst_format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, height
);
603 tmp_stride
= MAX2(width
, x_step
) * 4 * sizeof *tmp_row
;
604 tmp_row
= MALLOC(y_step
* tmp_stride
);
608 while (height
>= y_step
) {
609 src_format_desc
->unpack_rgba_float(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, y_step
);
610 dst_format_desc
->pack_rgba_float(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, y_step
);
618 src_format_desc
->unpack_rgba_float(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, height
);
619 dst_format_desc
->pack_rgba_float(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, height
);
626 void util_format_compose_swizzles(const unsigned char swz1
[4],
627 const unsigned char swz2
[4],
628 unsigned char dst
[4])
632 for (i
= 0; i
< 4; i
++) {
633 dst
[i
] = swz2
[i
] <= UTIL_FORMAT_SWIZZLE_W
?
634 swz1
[swz2
[i
]] : swz2
[i
];
638 void util_format_swizzle_4f(float *dst
, const float *src
,
639 const unsigned char swz
[4])
643 for (i
= 0; i
< 4; i
++) {
644 if (swz
[i
] <= UTIL_FORMAT_SWIZZLE_W
)
645 dst
[i
] = src
[swz
[i
]];
646 else if (swz
[i
] == UTIL_FORMAT_SWIZZLE_0
)
648 else if (swz
[i
] == UTIL_FORMAT_SWIZZLE_1
)
653 void util_format_unswizzle_4f(float *dst
, const float *src
,
654 const unsigned char swz
[4])
658 for (i
= 0; i
< 4; i
++) {
660 case UTIL_FORMAT_SWIZZLE_X
:
663 case UTIL_FORMAT_SWIZZLE_Y
:
666 case UTIL_FORMAT_SWIZZLE_Z
:
669 case UTIL_FORMAT_SWIZZLE_W
: