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
;
65 * Return the number of logical channels in the given format by
67 * XXX this could be made into a public function if useful elsewhere.
70 nr_logical_channels(const struct util_format_description
*desc
)
72 boolean swizzle_used
[UTIL_FORMAT_SWIZZLE_MAX
];
74 memset(swizzle_used
, 0, sizeof(swizzle_used
));
76 swizzle_used
[desc
->swizzle
[0]] = TRUE
;
77 swizzle_used
[desc
->swizzle
[1]] = TRUE
;
78 swizzle_used
[desc
->swizzle
[2]] = TRUE
;
79 swizzle_used
[desc
->swizzle
[3]] = TRUE
;
81 return (swizzle_used
[UTIL_FORMAT_SWIZZLE_X
] +
82 swizzle_used
[UTIL_FORMAT_SWIZZLE_Y
] +
83 swizzle_used
[UTIL_FORMAT_SWIZZLE_Z
] +
84 swizzle_used
[UTIL_FORMAT_SWIZZLE_W
]);
88 /** Test if the format contains RGB, but not alpha */
90 util_format_is_rgb_no_alpha(enum pipe_format format
)
92 const struct util_format_description
*desc
=
93 util_format_description(format
);
95 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
96 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
97 nr_logical_channels(desc
) == 3) {
105 util_format_is_luminance(enum pipe_format format
)
107 const struct util_format_description
*desc
=
108 util_format_description(format
);
110 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
111 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
112 desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_X
&&
113 desc
->swizzle
[1] == UTIL_FORMAT_SWIZZLE_X
&&
114 desc
->swizzle
[2] == UTIL_FORMAT_SWIZZLE_X
&&
115 desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_1
) {
122 util_format_is_pure_integer(enum pipe_format format
)
124 const struct util_format_description
*desc
= util_format_description(format
);
127 /* Find the first non-void channel. */
128 i
= util_format_get_first_non_void_channel(format
);
132 return desc
->channel
[i
].pure_integer
? TRUE
: FALSE
;
136 util_format_is_pure_sint(enum pipe_format format
)
138 const struct util_format_description
*desc
= util_format_description(format
);
141 i
= util_format_get_first_non_void_channel(format
);
145 return (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
&& desc
->channel
[i
].pure_integer
) ? TRUE
: FALSE
;
149 util_format_is_pure_uint(enum pipe_format format
)
151 const struct util_format_description
*desc
= util_format_description(format
);
154 i
= util_format_get_first_non_void_channel(format
);
158 return (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
&& desc
->channel
[i
].pure_integer
) ? TRUE
: FALSE
;
162 util_format_is_array(const struct util_format_description
*desc
)
166 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
||
167 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
||
168 desc
->block
.width
!= 1 ||
169 desc
->block
.height
!= 1) {
173 for (chan
= 0; chan
< desc
->nr_channels
; ++chan
) {
174 if (desc
->swizzle
[chan
] != chan
)
177 if (desc
->channel
[chan
].type
!= desc
->channel
[0].type
)
180 if (desc
->channel
[chan
].normalized
!= desc
->channel
[0].normalized
)
183 if (desc
->channel
[chan
].pure_integer
!= desc
->channel
[0].pure_integer
)
186 if (desc
->channel
[chan
].size
!= desc
->channel
[0].size
)
194 util_format_is_luminance_alpha(enum pipe_format format
)
196 const struct util_format_description
*desc
=
197 util_format_description(format
);
199 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
200 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
201 desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_X
&&
202 desc
->swizzle
[1] == UTIL_FORMAT_SWIZZLE_X
&&
203 desc
->swizzle
[2] == UTIL_FORMAT_SWIZZLE_X
&&
204 desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_Y
) {
212 util_format_is_intensity(enum pipe_format format
)
214 const struct util_format_description
*desc
=
215 util_format_description(format
);
217 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
218 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
219 desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_X
&&
220 desc
->swizzle
[1] == UTIL_FORMAT_SWIZZLE_X
&&
221 desc
->swizzle
[2] == UTIL_FORMAT_SWIZZLE_X
&&
222 desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_X
) {
230 util_format_is_supported(enum pipe_format format
, unsigned bind
)
232 if (util_format_is_s3tc(format
) && !util_format_s3tc_enabled
) {
236 #ifndef TEXTURE_FLOAT_ENABLED
237 if ((bind
& PIPE_BIND_RENDER_TARGET
) &&
238 format
!= PIPE_FORMAT_R9G9B9E5_FLOAT
&&
239 format
!= PIPE_FORMAT_R11G11B10_FLOAT
&&
240 util_format_is_float(format
)) {
250 util_format_read_4f(enum pipe_format format
,
251 float *dst
, unsigned dst_stride
,
252 const void *src
, unsigned src_stride
,
253 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
255 const struct util_format_description
*format_desc
;
256 const uint8_t *src_row
;
259 format_desc
= util_format_description(format
);
261 assert(x
% format_desc
->block
.width
== 0);
262 assert(y
% format_desc
->block
.height
== 0);
264 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
267 format_desc
->unpack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
272 util_format_write_4f(enum pipe_format format
,
273 const float *src
, unsigned src_stride
,
274 void *dst
, unsigned dst_stride
,
275 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
277 const struct util_format_description
*format_desc
;
279 const float *src_row
;
281 format_desc
= util_format_description(format
);
283 assert(x
% format_desc
->block
.width
== 0);
284 assert(y
% format_desc
->block
.height
== 0);
286 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
289 format_desc
->pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
294 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
)
296 const struct util_format_description
*format_desc
;
297 const uint8_t *src_row
;
300 format_desc
= util_format_description(format
);
302 assert(x
% format_desc
->block
.width
== 0);
303 assert(y
% format_desc
->block
.height
== 0);
305 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
308 format_desc
->unpack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
313 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
)
315 const struct util_format_description
*format_desc
;
317 const uint8_t *src_row
;
319 format_desc
= util_format_description(format
);
321 assert(x
% format_desc
->block
.width
== 0);
322 assert(y
% format_desc
->block
.height
== 0);
324 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
327 format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
331 util_format_read_4ui(enum pipe_format format
,
332 unsigned *dst
, unsigned dst_stride
,
333 const void *src
, unsigned src_stride
,
334 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
336 const struct util_format_description
*format_desc
;
337 const uint8_t *src_row
;
340 format_desc
= util_format_description(format
);
342 assert(x
% format_desc
->block
.width
== 0);
343 assert(y
% format_desc
->block
.height
== 0);
345 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
348 format_desc
->unpack_rgba_uint(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
352 util_format_write_4ui(enum pipe_format format
,
353 const unsigned int *src
, unsigned src_stride
,
354 void *dst
, unsigned dst_stride
,
355 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
357 const struct util_format_description
*format_desc
;
359 const unsigned *src_row
;
361 format_desc
= util_format_description(format
);
363 assert(x
% format_desc
->block
.width
== 0);
364 assert(y
% format_desc
->block
.height
== 0);
366 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
369 format_desc
->pack_rgba_uint(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
373 util_format_read_4i(enum pipe_format format
,
374 int *dst
, unsigned dst_stride
,
375 const void *src
, unsigned src_stride
,
376 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
378 const struct util_format_description
*format_desc
;
379 const uint8_t *src_row
;
382 format_desc
= util_format_description(format
);
384 assert(x
% format_desc
->block
.width
== 0);
385 assert(y
% format_desc
->block
.height
== 0);
387 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
390 format_desc
->unpack_rgba_sint(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
394 util_format_write_4i(enum pipe_format format
,
395 const int *src
, unsigned src_stride
,
396 void *dst
, unsigned dst_stride
,
397 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
399 const struct util_format_description
*format_desc
;
403 format_desc
= util_format_description(format
);
405 assert(x
% format_desc
->block
.width
== 0);
406 assert(y
% format_desc
->block
.height
== 0);
408 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
411 format_desc
->pack_rgba_sint(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
415 util_is_format_compatible(const struct util_format_description
*src_desc
,
416 const struct util_format_description
*dst_desc
)
420 if (src_desc
->format
== dst_desc
->format
) {
424 if (src_desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
||
425 dst_desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
) {
429 if (src_desc
->block
.bits
!= dst_desc
->block
.bits
||
430 src_desc
->nr_channels
!= dst_desc
->nr_channels
||
431 src_desc
->colorspace
!= dst_desc
->colorspace
) {
435 for (chan
= 0; chan
< 4; ++chan
) {
436 if (src_desc
->channel
[chan
].size
!=
437 dst_desc
->channel
[chan
].size
) {
442 for (chan
= 0; chan
< 4; ++chan
) {
443 enum util_format_swizzle swizzle
= dst_desc
->swizzle
[chan
];
446 if (src_desc
->swizzle
[chan
] != swizzle
) {
449 if ((src_desc
->channel
[swizzle
].type
!=
450 dst_desc
->channel
[swizzle
].type
) ||
451 (src_desc
->channel
[swizzle
].normalized
!=
452 dst_desc
->channel
[swizzle
].normalized
)) {
463 util_format_fits_8unorm(const struct util_format_description
*format_desc
)
468 * After linearized sRGB values require more than 8bits.
471 if (format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
475 switch (format_desc
->layout
) {
477 case UTIL_FORMAT_LAYOUT_S3TC
:
479 * These are straight forward.
482 case UTIL_FORMAT_LAYOUT_RGTC
:
483 if (format_desc
->format
== PIPE_FORMAT_RGTC1_SNORM
||
484 format_desc
->format
== PIPE_FORMAT_RGTC2_SNORM
||
485 format_desc
->format
== PIPE_FORMAT_LATC1_SNORM
||
486 format_desc
->format
== PIPE_FORMAT_LATC2_SNORM
)
490 case UTIL_FORMAT_LAYOUT_PLAIN
:
492 * For these we can find a generic rule.
495 for (chan
= 0; chan
< format_desc
->nr_channels
; ++chan
) {
496 switch (format_desc
->channel
[chan
].type
) {
497 case UTIL_FORMAT_TYPE_VOID
:
499 case UTIL_FORMAT_TYPE_UNSIGNED
:
500 if (!format_desc
->channel
[chan
].normalized
||
501 format_desc
->channel
[chan
].size
> 8) {
513 * Handle all others on a case by case basis.
516 switch (format_desc
->format
) {
517 case PIPE_FORMAT_R1_UNORM
:
518 case PIPE_FORMAT_UYVY
:
519 case PIPE_FORMAT_YUYV
:
520 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
521 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
532 util_format_translate(enum pipe_format dst_format
,
533 void *dst
, unsigned dst_stride
,
534 unsigned dst_x
, unsigned dst_y
,
535 enum pipe_format src_format
,
536 const void *src
, unsigned src_stride
,
537 unsigned src_x
, unsigned src_y
,
538 unsigned width
, unsigned height
)
540 const struct util_format_description
*dst_format_desc
;
541 const struct util_format_description
*src_format_desc
;
543 const uint8_t *src_row
;
544 unsigned x_step
, y_step
;
548 dst_format_desc
= util_format_description(dst_format
);
549 src_format_desc
= util_format_description(src_format
);
551 if (util_is_format_compatible(src_format_desc
, dst_format_desc
)) {
556 util_copy_rect(dst
, dst_format
, dst_stride
, dst_x
, dst_y
,
557 width
, height
, src
, (int)src_stride
,
562 assert(dst_x
% dst_format_desc
->block
.width
== 0);
563 assert(dst_y
% dst_format_desc
->block
.height
== 0);
564 assert(src_x
% src_format_desc
->block
.width
== 0);
565 assert(src_y
% src_format_desc
->block
.height
== 0);
567 dst_row
= (uint8_t *)dst
+ dst_y
*dst_stride
+ dst_x
*(dst_format_desc
->block
.bits
/8);
568 src_row
= (const uint8_t *)src
+ src_y
*src_stride
+ src_x
*(src_format_desc
->block
.bits
/8);
571 * This works because all pixel formats have pixel blocks with power of two
575 y_step
= MAX2(dst_format_desc
->block
.height
, src_format_desc
->block
.height
);
576 x_step
= MAX2(dst_format_desc
->block
.width
, src_format_desc
->block
.width
);
577 assert(y_step
% dst_format_desc
->block
.height
== 0);
578 assert(y_step
% src_format_desc
->block
.height
== 0);
580 dst_step
= y_step
/ dst_format_desc
->block
.height
* dst_stride
;
581 src_step
= y_step
/ src_format_desc
->block
.height
* src_stride
;
584 * TODO: double formats will loose precision
585 * TODO: Add a special case for formats that are mere swizzles of each other
588 if (src_format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
||
589 dst_format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) {
591 uint8_t *tmp_s
= NULL
;
596 if (src_format_desc
->unpack_z_float
&&
597 dst_format_desc
->pack_z_float
) {
598 tmp_z
= MALLOC(width
* sizeof *tmp_z
);
601 if (src_format_desc
->unpack_s_8uint
&&
602 dst_format_desc
->pack_s_8uint
) {
603 tmp_s
= MALLOC(width
* sizeof *tmp_s
);
608 src_format_desc
->unpack_z_float(tmp_z
, 0, src_row
, src_stride
, width
, 1);
609 dst_format_desc
->pack_z_float(dst_row
, dst_stride
, tmp_z
, 0, width
, 1);
613 src_format_desc
->unpack_s_8uint(tmp_s
, 0, src_row
, src_stride
, width
, 1);
614 dst_format_desc
->pack_s_8uint(dst_row
, dst_stride
, tmp_s
, 0, width
, 1);
632 if (util_format_fits_8unorm(src_format_desc
) ||
633 util_format_fits_8unorm(dst_format_desc
)) {
637 tmp_stride
= MAX2(width
, x_step
) * 4 * sizeof *tmp_row
;
638 tmp_row
= MALLOC(y_step
* tmp_stride
);
642 while (height
>= y_step
) {
643 src_format_desc
->unpack_rgba_8unorm(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, y_step
);
644 dst_format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, y_step
);
652 src_format_desc
->unpack_rgba_8unorm(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, height
);
653 dst_format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, height
);
662 tmp_stride
= MAX2(width
, x_step
) * 4 * sizeof *tmp_row
;
663 tmp_row
= MALLOC(y_step
* tmp_stride
);
667 while (height
>= y_step
) {
668 src_format_desc
->unpack_rgba_float(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, y_step
);
669 dst_format_desc
->pack_rgba_float(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, y_step
);
677 src_format_desc
->unpack_rgba_float(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, height
);
678 dst_format_desc
->pack_rgba_float(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, height
);
685 void util_format_compose_swizzles(const unsigned char swz1
[4],
686 const unsigned char swz2
[4],
687 unsigned char dst
[4])
691 for (i
= 0; i
< 4; i
++) {
692 dst
[i
] = swz2
[i
] <= UTIL_FORMAT_SWIZZLE_W
?
693 swz1
[swz2
[i
]] : swz2
[i
];
697 void util_format_swizzle_4f(float *dst
, const float *src
,
698 const unsigned char swz
[4])
702 for (i
= 0; i
< 4; i
++) {
703 if (swz
[i
] <= UTIL_FORMAT_SWIZZLE_W
)
704 dst
[i
] = src
[swz
[i
]];
705 else if (swz
[i
] == UTIL_FORMAT_SWIZZLE_0
)
707 else if (swz
[i
] == UTIL_FORMAT_SWIZZLE_1
)
712 void util_format_unswizzle_4f(float *dst
, const float *src
,
713 const unsigned char swz
[4])
717 for (i
= 0; i
< 4; i
++) {
719 case UTIL_FORMAT_SWIZZLE_X
:
722 case UTIL_FORMAT_SWIZZLE_Y
:
725 case UTIL_FORMAT_SWIZZLE_Z
:
728 case UTIL_FORMAT_SWIZZLE_W
: