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 /* Find the first non-void channel. */
56 for (i
= 0; i
< 4; i
++) {
57 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
66 return desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_FLOAT
? TRUE
: FALSE
;
71 * Return the number of logical channels in the given format by
73 * XXX this could be made into a public function if useful elsewhere.
76 nr_logical_channels(const struct util_format_description
*desc
)
78 boolean swizzle_used
[UTIL_FORMAT_SWIZZLE_MAX
];
80 memset(swizzle_used
, 0, sizeof(swizzle_used
));
82 swizzle_used
[desc
->swizzle
[0]] = TRUE
;
83 swizzle_used
[desc
->swizzle
[1]] = TRUE
;
84 swizzle_used
[desc
->swizzle
[2]] = TRUE
;
85 swizzle_used
[desc
->swizzle
[3]] = TRUE
;
87 return (swizzle_used
[UTIL_FORMAT_SWIZZLE_X
] +
88 swizzle_used
[UTIL_FORMAT_SWIZZLE_Y
] +
89 swizzle_used
[UTIL_FORMAT_SWIZZLE_Z
] +
90 swizzle_used
[UTIL_FORMAT_SWIZZLE_W
]);
94 /** Test if the format contains RGB, but not alpha */
96 util_format_is_rgb_no_alpha(enum pipe_format format
)
98 const struct util_format_description
*desc
=
99 util_format_description(format
);
101 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
102 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
103 nr_logical_channels(desc
) == 3) {
111 util_format_is_luminance(enum pipe_format format
)
113 const struct util_format_description
*desc
=
114 util_format_description(format
);
116 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
117 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
118 desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_X
&&
119 desc
->swizzle
[1] == UTIL_FORMAT_SWIZZLE_X
&&
120 desc
->swizzle
[2] == UTIL_FORMAT_SWIZZLE_X
&&
121 desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_1
) {
129 util_format_is_luminance_alpha(enum pipe_format format
)
131 const struct util_format_description
*desc
=
132 util_format_description(format
);
134 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
135 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
136 desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_X
&&
137 desc
->swizzle
[1] == UTIL_FORMAT_SWIZZLE_X
&&
138 desc
->swizzle
[2] == UTIL_FORMAT_SWIZZLE_X
&&
139 desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_Y
) {
147 util_format_is_intensity(enum pipe_format format
)
149 const struct util_format_description
*desc
=
150 util_format_description(format
);
152 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
153 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
154 desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_X
&&
155 desc
->swizzle
[1] == UTIL_FORMAT_SWIZZLE_X
&&
156 desc
->swizzle
[2] == UTIL_FORMAT_SWIZZLE_X
&&
157 desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_X
) {
165 util_format_is_supported(enum pipe_format format
, unsigned bind
)
167 if (util_format_is_s3tc(format
) && !util_format_s3tc_enabled
) {
171 #ifndef TEXTURE_FLOAT_ENABLED
172 if ((bind
& PIPE_BIND_RENDER_TARGET
) &&
173 format
!= PIPE_FORMAT_R9G9B9E5_FLOAT
&&
174 format
!= PIPE_FORMAT_R11G11B10_FLOAT
&&
175 util_format_is_float(format
)) {
185 util_format_read_4f(enum pipe_format format
,
186 float *dst
, unsigned dst_stride
,
187 const void *src
, unsigned src_stride
,
188 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
190 const struct util_format_description
*format_desc
;
191 const uint8_t *src_row
;
194 format_desc
= util_format_description(format
);
196 assert(x
% format_desc
->block
.width
== 0);
197 assert(y
% format_desc
->block
.height
== 0);
199 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
202 format_desc
->unpack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
207 util_format_write_4f(enum pipe_format format
,
208 const float *src
, unsigned src_stride
,
209 void *dst
, unsigned dst_stride
,
210 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
212 const struct util_format_description
*format_desc
;
214 const float *src_row
;
216 format_desc
= util_format_description(format
);
218 assert(x
% format_desc
->block
.width
== 0);
219 assert(y
% format_desc
->block
.height
== 0);
221 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
224 format_desc
->pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
229 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
)
231 const struct util_format_description
*format_desc
;
232 const uint8_t *src_row
;
235 format_desc
= util_format_description(format
);
237 assert(x
% format_desc
->block
.width
== 0);
238 assert(y
% format_desc
->block
.height
== 0);
240 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
243 format_desc
->unpack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
248 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
)
250 const struct util_format_description
*format_desc
;
252 const uint8_t *src_row
;
254 format_desc
= util_format_description(format
);
256 assert(x
% format_desc
->block
.width
== 0);
257 assert(y
% format_desc
->block
.height
== 0);
259 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
262 format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
267 util_is_format_compatible(const struct util_format_description
*src_desc
,
268 const struct util_format_description
*dst_desc
)
272 if (src_desc
->format
== dst_desc
->format
) {
276 if (src_desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
||
277 dst_desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
) {
281 if (src_desc
->block
.bits
!= dst_desc
->block
.bits
||
282 src_desc
->nr_channels
!= dst_desc
->nr_channels
||
283 src_desc
->colorspace
!= dst_desc
->colorspace
) {
287 for (chan
= 0; chan
< 4; ++chan
) {
288 if (src_desc
->channel
[chan
].size
!=
289 dst_desc
->channel
[chan
].size
) {
294 for (chan
= 0; chan
< 4; ++chan
) {
295 enum util_format_swizzle swizzle
= dst_desc
->swizzle
[chan
];
298 if (src_desc
->swizzle
[chan
] != swizzle
) {
301 if ((src_desc
->channel
[swizzle
].type
!=
302 dst_desc
->channel
[swizzle
].type
) ||
303 (src_desc
->channel
[swizzle
].normalized
!=
304 dst_desc
->channel
[swizzle
].normalized
)) {
315 util_format_fits_8unorm(const struct util_format_description
*format_desc
)
320 * After linearized sRGB values require more than 8bits.
323 if (format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
327 switch (format_desc
->layout
) {
329 case UTIL_FORMAT_LAYOUT_S3TC
:
330 case UTIL_FORMAT_LAYOUT_RGTC
:
332 * These are straight forward.
337 case UTIL_FORMAT_LAYOUT_PLAIN
:
339 * For these we can find a generic rule.
342 for (chan
= 0; chan
< format_desc
->nr_channels
; ++chan
) {
343 switch (format_desc
->channel
[chan
].type
) {
344 case UTIL_FORMAT_TYPE_VOID
:
346 case UTIL_FORMAT_TYPE_UNSIGNED
:
347 if (!format_desc
->channel
[chan
].normalized
||
348 format_desc
->channel
[chan
].size
> 8) {
360 * Handle all others on a case by case basis.
363 switch (format_desc
->format
) {
364 case PIPE_FORMAT_R1_UNORM
:
365 case PIPE_FORMAT_UYVY
:
366 case PIPE_FORMAT_YUYV
:
367 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
368 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
379 util_format_translate(enum pipe_format dst_format
,
380 void *dst
, unsigned dst_stride
,
381 unsigned dst_x
, unsigned dst_y
,
382 enum pipe_format src_format
,
383 const void *src
, unsigned src_stride
,
384 unsigned src_x
, unsigned src_y
,
385 unsigned width
, unsigned height
)
387 const struct util_format_description
*dst_format_desc
;
388 const struct util_format_description
*src_format_desc
;
390 const uint8_t *src_row
;
391 unsigned x_step
, y_step
;
395 dst_format_desc
= util_format_description(dst_format
);
396 src_format_desc
= util_format_description(src_format
);
398 if (util_is_format_compatible(src_format_desc
, dst_format_desc
)) {
403 util_copy_rect(dst
, dst_format
, dst_stride
, dst_x
, dst_y
,
404 width
, height
, src
, (int)src_stride
,
409 assert(dst_x
% dst_format_desc
->block
.width
== 0);
410 assert(dst_y
% dst_format_desc
->block
.height
== 0);
411 assert(src_x
% src_format_desc
->block
.width
== 0);
412 assert(src_y
% src_format_desc
->block
.height
== 0);
414 dst_row
= (uint8_t *)dst
+ dst_y
*dst_stride
+ dst_x
*(dst_format_desc
->block
.bits
/8);
415 src_row
= (const uint8_t *)src
+ src_y
*src_stride
+ src_x
*(src_format_desc
->block
.bits
/8);
418 * This works because all pixel formats have pixel blocks with power of two
422 y_step
= MAX2(dst_format_desc
->block
.height
, src_format_desc
->block
.height
);
423 x_step
= MAX2(dst_format_desc
->block
.width
, src_format_desc
->block
.width
);
424 assert(y_step
% dst_format_desc
->block
.height
== 0);
425 assert(y_step
% src_format_desc
->block
.height
== 0);
427 dst_step
= y_step
/ dst_format_desc
->block
.height
* dst_stride
;
428 src_step
= y_step
/ src_format_desc
->block
.height
* src_stride
;
431 * TODO: double formats will loose precision
432 * TODO: Add a special case for formats that are mere swizzles of each other
435 if (src_format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
||
436 dst_format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) {
438 uint8_t *tmp_s
= NULL
;
443 if (src_format_desc
->unpack_z_float
&&
444 dst_format_desc
->pack_z_float
) {
445 tmp_z
= MALLOC(width
* sizeof *tmp_z
);
448 if (src_format_desc
->unpack_s_8uscaled
&&
449 dst_format_desc
->pack_s_8uscaled
) {
450 tmp_s
= MALLOC(width
* sizeof *tmp_s
);
455 src_format_desc
->unpack_z_float(tmp_z
, 0, src_row
, src_stride
, width
, 1);
456 dst_format_desc
->pack_z_float(dst_row
, dst_stride
, tmp_z
, 0, width
, 1);
460 src_format_desc
->unpack_s_8uscaled(tmp_s
, 0, src_row
, src_stride
, width
, 1);
461 dst_format_desc
->pack_s_8uscaled(dst_row
, dst_stride
, tmp_s
, 0, width
, 1);
479 if (util_format_fits_8unorm(src_format_desc
) ||
480 util_format_fits_8unorm(dst_format_desc
)) {
484 tmp_stride
= MAX2(width
, x_step
) * 4 * sizeof *tmp_row
;
485 tmp_row
= MALLOC(y_step
* tmp_stride
);
489 while (height
>= y_step
) {
490 src_format_desc
->unpack_rgba_8unorm(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, y_step
);
491 dst_format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, y_step
);
499 src_format_desc
->unpack_rgba_8unorm(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, height
);
500 dst_format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, height
);
509 tmp_stride
= MAX2(width
, x_step
) * 4 * sizeof *tmp_row
;
510 tmp_row
= MALLOC(y_step
* tmp_stride
);
514 while (height
>= y_step
) {
515 src_format_desc
->unpack_rgba_float(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, y_step
);
516 dst_format_desc
->pack_rgba_float(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, y_step
);
524 src_format_desc
->unpack_rgba_float(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, height
);
525 dst_format_desc
->pack_rgba_float(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, height
);
532 void util_format_compose_swizzles(const unsigned char swz1
[4],
533 const unsigned char swz2
[4],
534 unsigned char dst
[4])
538 for (i
= 0; i
< 4; i
++) {
539 dst
[i
] = swz2
[i
] <= UTIL_FORMAT_SWIZZLE_W
?
540 swz1
[swz2
[i
]] : swz2
[i
];
544 void util_format_swizzle_4f(float *dst
, const float *src
,
545 const unsigned char swz
[4])
549 for (i
= 0; i
< 4; i
++) {
550 if (swz
[i
] <= UTIL_FORMAT_SWIZZLE_W
)
551 dst
[i
] = src
[swz
[i
]];
552 else if (swz
[i
] == UTIL_FORMAT_SWIZZLE_0
)
554 else if (swz
[i
] == UTIL_FORMAT_SWIZZLE_1
)
559 void util_format_unswizzle_4f(float *dst
, const float *src
,
560 const unsigned char swz
[4])
564 for (i
= 0; i
< 4; i
++) {
566 case UTIL_FORMAT_SWIZZLE_X
:
569 case UTIL_FORMAT_SWIZZLE_Y
:
572 case UTIL_FORMAT_SWIZZLE_Z
:
575 case UTIL_FORMAT_SWIZZLE_W
: