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 util_format_is_supported(enum pipe_format format
, unsigned bind
)
73 if (util_format_is_s3tc(format
) && !util_format_s3tc_enabled
) {
77 #ifndef TEXTURE_FLOAT_ENABLED
78 if ((bind
& PIPE_BIND_RENDER_TARGET
) &&
79 format
!= PIPE_FORMAT_R9G9B9E5_FLOAT
&&
80 format
!= PIPE_FORMAT_R11G11B10_FLOAT
&&
81 util_format_is_float(format
)) {
91 util_format_read_4f(enum pipe_format format
,
92 float *dst
, unsigned dst_stride
,
93 const void *src
, unsigned src_stride
,
94 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
96 const struct util_format_description
*format_desc
;
97 const uint8_t *src_row
;
100 format_desc
= util_format_description(format
);
102 assert(x
% format_desc
->block
.width
== 0);
103 assert(y
% format_desc
->block
.height
== 0);
105 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
108 format_desc
->unpack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
113 util_format_write_4f(enum pipe_format format
,
114 const float *src
, unsigned src_stride
,
115 void *dst
, unsigned dst_stride
,
116 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
118 const struct util_format_description
*format_desc
;
120 const float *src_row
;
122 format_desc
= util_format_description(format
);
124 assert(x
% format_desc
->block
.width
== 0);
125 assert(y
% format_desc
->block
.height
== 0);
127 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
130 format_desc
->pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
135 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
)
137 const struct util_format_description
*format_desc
;
138 const uint8_t *src_row
;
141 format_desc
= util_format_description(format
);
143 assert(x
% format_desc
->block
.width
== 0);
144 assert(y
% format_desc
->block
.height
== 0);
146 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
149 format_desc
->unpack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
154 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
)
156 const struct util_format_description
*format_desc
;
158 const uint8_t *src_row
;
160 format_desc
= util_format_description(format
);
162 assert(x
% format_desc
->block
.width
== 0);
163 assert(y
% format_desc
->block
.height
== 0);
165 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
168 format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
173 util_is_format_compatible(const struct util_format_description
*src_desc
,
174 const struct util_format_description
*dst_desc
)
178 if (src_desc
->format
== dst_desc
->format
) {
182 if (src_desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
||
183 dst_desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
) {
187 if (src_desc
->block
.bits
!= dst_desc
->block
.bits
||
188 src_desc
->nr_channels
!= dst_desc
->nr_channels
||
189 src_desc
->colorspace
!= dst_desc
->colorspace
) {
193 for (chan
= 0; chan
< 4; ++chan
) {
194 if (src_desc
->channel
[chan
].size
!=
195 dst_desc
->channel
[chan
].size
) {
200 for (chan
= 0; chan
< 4; ++chan
) {
201 enum util_format_swizzle swizzle
= dst_desc
->swizzle
[chan
];
204 if (src_desc
->swizzle
[chan
] != swizzle
) {
207 if ((src_desc
->channel
[swizzle
].type
!=
208 dst_desc
->channel
[swizzle
].type
) ||
209 (src_desc
->channel
[swizzle
].normalized
!=
210 dst_desc
->channel
[swizzle
].normalized
)) {
221 util_format_fits_8unorm(const struct util_format_description
*format_desc
)
226 * After linearized sRGB values require more than 8bits.
229 if (format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
233 switch (format_desc
->layout
) {
235 case UTIL_FORMAT_LAYOUT_S3TC
:
236 case UTIL_FORMAT_LAYOUT_RGTC
:
238 * These are straight forward.
243 case UTIL_FORMAT_LAYOUT_PLAIN
:
245 * For these we can find a generic rule.
248 for (chan
= 0; chan
< format_desc
->nr_channels
; ++chan
) {
249 switch (format_desc
->channel
[chan
].type
) {
250 case UTIL_FORMAT_TYPE_VOID
:
252 case UTIL_FORMAT_TYPE_UNSIGNED
:
253 if (!format_desc
->channel
[chan
].normalized
||
254 format_desc
->channel
[chan
].size
> 8) {
266 * Handle all others on a case by case basis.
269 switch (format_desc
->format
) {
270 case PIPE_FORMAT_R1_UNORM
:
271 case PIPE_FORMAT_UYVY
:
272 case PIPE_FORMAT_YUYV
:
273 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
274 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
285 util_format_translate(enum pipe_format dst_format
,
286 void *dst
, unsigned dst_stride
,
287 unsigned dst_x
, unsigned dst_y
,
288 enum pipe_format src_format
,
289 const void *src
, unsigned src_stride
,
290 unsigned src_x
, unsigned src_y
,
291 unsigned width
, unsigned height
)
293 const struct util_format_description
*dst_format_desc
;
294 const struct util_format_description
*src_format_desc
;
296 const uint8_t *src_row
;
297 unsigned x_step
, y_step
;
301 dst_format_desc
= util_format_description(dst_format
);
302 src_format_desc
= util_format_description(src_format
);
304 if (util_is_format_compatible(src_format_desc
, dst_format_desc
)) {
309 util_copy_rect(dst
, dst_format
, dst_stride
, dst_x
, dst_y
,
310 width
, height
, src
, (int)src_stride
,
315 assert(dst_x
% dst_format_desc
->block
.width
== 0);
316 assert(dst_y
% dst_format_desc
->block
.height
== 0);
317 assert(src_x
% src_format_desc
->block
.width
== 0);
318 assert(src_y
% src_format_desc
->block
.height
== 0);
320 dst_row
= (uint8_t *)dst
+ dst_y
*dst_stride
+ dst_x
*(dst_format_desc
->block
.bits
/8);
321 src_row
= (const uint8_t *)src
+ src_y
*src_stride
+ src_x
*(src_format_desc
->block
.bits
/8);
324 * This works because all pixel formats have pixel blocks with power of two
328 y_step
= MAX2(dst_format_desc
->block
.height
, src_format_desc
->block
.height
);
329 x_step
= MAX2(dst_format_desc
->block
.width
, src_format_desc
->block
.width
);
330 assert(y_step
% dst_format_desc
->block
.height
== 0);
331 assert(y_step
% src_format_desc
->block
.height
== 0);
333 dst_step
= y_step
/ dst_format_desc
->block
.height
* dst_stride
;
334 src_step
= y_step
/ src_format_desc
->block
.height
* src_stride
;
337 * TODO: double formats will loose precision
338 * TODO: Add a special case for formats that are mere swizzles of each other
341 if (util_format_fits_8unorm(src_format_desc
) ||
342 util_format_fits_8unorm(dst_format_desc
)) {
346 tmp_stride
= MAX2(width
, x_step
) * 4 * sizeof *tmp_row
;
347 tmp_row
= MALLOC(y_step
* tmp_stride
);
351 while (height
>= y_step
) {
352 src_format_desc
->unpack_rgba_8unorm(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, y_step
);
353 dst_format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, y_step
);
361 src_format_desc
->unpack_rgba_8unorm(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, height
);
362 dst_format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, height
);
371 tmp_stride
= MAX2(width
, x_step
) * 4 * sizeof *tmp_row
;
372 tmp_row
= MALLOC(y_step
* tmp_stride
);
376 while (height
>= y_step
) {
377 src_format_desc
->unpack_rgba_float(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, y_step
);
378 dst_format_desc
->pack_rgba_float(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, y_step
);
386 src_format_desc
->unpack_rgba_float(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, height
);
387 dst_format_desc
->pack_rgba_float(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, height
);