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_luminance_alpha(enum pipe_format format)
164 const struct util_format_description *desc =
165 util_format_description(format);
167 if ((desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB ||
168 desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) &&
169 desc->swizzle[0] == UTIL_FORMAT_SWIZZLE_X &&
170 desc->swizzle[1] == UTIL_FORMAT_SWIZZLE_X &&
171 desc->swizzle[2] == UTIL_FORMAT_SWIZZLE_X &&
172 desc->swizzle[3] == UTIL_FORMAT_SWIZZLE_Y) {
180 util_format_is_intensity(enum pipe_format format)
182 const struct util_format_description *desc =
183 util_format_description(format);
185 if ((desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB ||
186 desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) &&
187 desc->swizzle[0] == UTIL_FORMAT_SWIZZLE_X &&
188 desc->swizzle[1] == UTIL_FORMAT_SWIZZLE_X &&
189 desc->swizzle[2] == UTIL_FORMAT_SWIZZLE_X &&
190 desc->swizzle[3] == UTIL_FORMAT_SWIZZLE_X) {
198 util_format_is_supported(enum pipe_format format, unsigned bind)
200 if (util_format_is_s3tc(format) && !util_format_s3tc_enabled) {
204 #ifndef TEXTURE_FLOAT_ENABLED
205 if ((bind & PIPE_BIND_RENDER_TARGET) &&
206 format != PIPE_FORMAT_R9G9B9E5_FLOAT &&
207 format != PIPE_FORMAT_R11G11B10_FLOAT &&
208 util_format_is_float(format)) {
218 util_format_read_4f(enum pipe_format format,
219 float *dst, unsigned dst_stride,
220 const void *src, unsigned src_stride,
221 unsigned x, unsigned y, unsigned w, unsigned h)
223 const struct util_format_description *format_desc;
224 const uint8_t *src_row;
227 format_desc = util_format_description(format);
229 assert(x % format_desc->block.width == 0);
230 assert(y % format_desc->block.height == 0);
232 src_row = (const uint8_t *)src + y*src_stride + x*(format_desc->block.bits/8);
235 format_desc->unpack_rgba_float(dst_row, dst_stride, src_row, src_stride, w, h);
240 util_format_write_4f(enum pipe_format format,
241 const float *src, unsigned src_stride,
242 void *dst, unsigned dst_stride,
243 unsigned x, unsigned y, unsigned w, unsigned h)
245 const struct util_format_description *format_desc;
247 const float *src_row;
249 format_desc = util_format_description(format);
251 assert(x % format_desc->block.width == 0);
252 assert(y % format_desc->block.height == 0);
254 dst_row = (uint8_t *)dst + y*dst_stride + x*(format_desc->block.bits/8);
257 format_desc->pack_rgba_float(dst_row, dst_stride, src_row, src_stride, w, h);
262 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)
264 const struct util_format_description *format_desc;
265 const uint8_t *src_row;
268 format_desc = util_format_description(format);
270 assert(x % format_desc->block.width == 0);
271 assert(y % format_desc->block.height == 0);
273 src_row = (const uint8_t *)src + y*src_stride + x*(format_desc->block.bits/8);
276 format_desc->unpack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, w, h);
281 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)
283 const struct util_format_description *format_desc;
285 const uint8_t *src_row;
287 format_desc = util_format_description(format);
289 assert(x % format_desc->block.width == 0);
290 assert(y % format_desc->block.height == 0);
292 dst_row = (uint8_t *)dst + y*dst_stride + x*(format_desc->block.bits/8);
295 format_desc->pack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, w, h);
299 util_format_read_4ui(enum pipe_format format,
300 unsigned *dst, unsigned dst_stride,
301 const void *src, unsigned src_stride,
302 unsigned x, unsigned y, unsigned w, unsigned h)
304 const struct util_format_description *format_desc;
305 const uint8_t *src_row;
308 format_desc = util_format_description(format);
310 assert(x % format_desc->block.width == 0);
311 assert(y % format_desc->block.height == 0);
313 src_row = (const uint8_t *)src + y*src_stride + x*(format_desc->block.bits/8);
316 format_desc->unpack_rgba_uint(dst_row, dst_stride, src_row, src_stride, w, h);
320 util_format_write_4ui(enum pipe_format format,
321 const unsigned int *src, unsigned src_stride,
322 void *dst, unsigned dst_stride,
323 unsigned x, unsigned y, unsigned w, unsigned h)
325 const struct util_format_description *format_desc;
327 const unsigned *src_row;
329 format_desc = util_format_description(format);
331 assert(x % format_desc->block.width == 0);
332 assert(y % format_desc->block.height == 0);
334 dst_row = (uint8_t *)dst + y*dst_stride + x*(format_desc->block.bits/8);
337 format_desc->pack_rgba_uint(dst_row, dst_stride, src_row, src_stride, w, h);
341 util_format_read_4i(enum pipe_format format,
342 int *dst, unsigned dst_stride,
343 const void *src, unsigned src_stride,
344 unsigned x, unsigned y, unsigned w, unsigned h)
346 const struct util_format_description *format_desc;
347 const uint8_t *src_row;
350 format_desc = util_format_description(format);
352 assert(x % format_desc->block.width == 0);
353 assert(y % format_desc->block.height == 0);
355 src_row = (const uint8_t *)src + y*src_stride + x*(format_desc->block.bits/8);
358 format_desc->unpack_rgba_sint(dst_row, dst_stride, src_row, src_stride, w, h);
362 util_format_write_4i(enum pipe_format format,
363 const int *src, unsigned src_stride,
364 void *dst, unsigned dst_stride,
365 unsigned x, unsigned y, unsigned w, unsigned h)
367 const struct util_format_description *format_desc;
371 format_desc = util_format_description(format);
373 assert(x % format_desc->block.width == 0);
374 assert(y % format_desc->block.height == 0);
376 dst_row = (uint8_t *)dst + y*dst_stride + x*(format_desc->block.bits/8);
379 format_desc->pack_rgba_sint(dst_row, dst_stride, src_row, src_stride, w, h);
383 util_is_format_compatible(const struct util_format_description *src_desc,
384 const struct util_format_description *dst_desc)
388 if (src_desc->format == dst_desc->format) {
392 if (src_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN ||
393 dst_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN) {
397 if (src_desc->block.bits != dst_desc->block.bits ||
398 src_desc->nr_channels != dst_desc->nr_channels ||
399 src_desc->colorspace != dst_desc->colorspace) {
403 for (chan = 0; chan < 4; ++chan) {
404 if (src_desc->channel[chan].size !=
405 dst_desc->channel[chan].size) {
410 for (chan = 0; chan < 4; ++chan) {
411 enum util_format_swizzle swizzle = dst_desc->swizzle[chan];
414 if (src_desc->swizzle[chan] != swizzle) {
417 if ((src_desc->channel[swizzle].type !=
418 dst_desc->channel[swizzle].type) ||
419 (src_desc->channel[swizzle].normalized !=
420 dst_desc->channel[swizzle].normalized)) {
431 util_format_fits_8unorm(const struct util_format_description *format_desc)
436 * After linearized sRGB values require more than 8bits.
439 if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
443 switch (format_desc->layout) {
445 case UTIL_FORMAT_LAYOUT_S3TC:
447 * These are straight forward.
450 case UTIL_FORMAT_LAYOUT_RGTC:
451 if (format_desc->format == PIPE_FORMAT_RGTC1_SNORM ||
452 format_desc->format == PIPE_FORMAT_RGTC2_SNORM ||
453 format_desc->format == PIPE_FORMAT_LATC1_SNORM ||
454 format_desc->format == PIPE_FORMAT_LATC2_SNORM)
458 case UTIL_FORMAT_LAYOUT_PLAIN:
460 * For these we can find a generic rule.
463 for (chan = 0; chan < format_desc->nr_channels; ++chan) {
464 switch (format_desc->channel[chan].type) {
465 case UTIL_FORMAT_TYPE_VOID:
467 case UTIL_FORMAT_TYPE_UNSIGNED:
468 if (!format_desc->channel[chan].normalized ||
469 format_desc->channel[chan].size > 8) {
481 * Handle all others on a case by case basis.
484 switch (format_desc->format) {
485 case PIPE_FORMAT_R1_UNORM:
486 case PIPE_FORMAT_UYVY:
487 case PIPE_FORMAT_YUYV:
488 case PIPE_FORMAT_R8G8_B8G8_UNORM:
489 case PIPE_FORMAT_G8R8_G8B8_UNORM:
500 util_format_translate(enum pipe_format dst_format,
501 void *dst, unsigned dst_stride,
502 unsigned dst_x, unsigned dst_y,
503 enum pipe_format src_format,
504 const void *src, unsigned src_stride,
505 unsigned src_x, unsigned src_y,
506 unsigned width, unsigned height)
508 const struct util_format_description *dst_format_desc;
509 const struct util_format_description *src_format_desc;
511 const uint8_t *src_row;
512 unsigned x_step, y_step;
516 dst_format_desc = util_format_description(dst_format);
517 src_format_desc = util_format_description(src_format);
519 if (util_is_format_compatible(src_format_desc, dst_format_desc)) {
524 util_copy_rect(dst, dst_format, dst_stride, dst_x, dst_y,
525 width, height, src, (int)src_stride,
530 assert(dst_x % dst_format_desc->block.width == 0);
531 assert(dst_y % dst_format_desc->block.height == 0);
532 assert(src_x % src_format_desc->block.width == 0);
533 assert(src_y % src_format_desc->block.height == 0);
535 dst_row = (uint8_t *)dst + dst_y*dst_stride + dst_x*(dst_format_desc->block.bits/8);
536 src_row = (const uint8_t *)src + src_y*src_stride + src_x*(src_format_desc->block.bits/8);
539 * This works because all pixel formats have pixel blocks with power of two
543 y_step = MAX2(dst_format_desc->block.height, src_format_desc->block.height);
544 x_step = MAX2(dst_format_desc->block.width, src_format_desc->block.width);
545 assert(y_step % dst_format_desc->block.height == 0);
546 assert(y_step % src_format_desc->block.height == 0);
548 dst_step = y_step / dst_format_desc->block.height * dst_stride;
549 src_step = y_step / src_format_desc->block.height * src_stride;
552 * TODO: double formats will loose precision
553 * TODO: Add a special case for formats that are mere swizzles of each other
556 if (src_format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS ||
557 dst_format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
559 uint8_t *tmp_s = NULL;
564 if (src_format_desc->unpack_z_float &&
565 dst_format_desc->pack_z_float) {
566 tmp_z = MALLOC(width * sizeof *tmp_z);
569 if (src_format_desc->unpack_s_8uint &&
570 dst_format_desc->pack_s_8uint) {
571 tmp_s = MALLOC(width * sizeof *tmp_s);
576 src_format_desc->unpack_z_float(tmp_z, 0, src_row, src_stride, width, 1);
577 dst_format_desc->pack_z_float(dst_row, dst_stride, tmp_z, 0, width, 1);
581 src_format_desc->unpack_s_8uint(tmp_s, 0, src_row, src_stride, width, 1);
582 dst_format_desc->pack_s_8uint(dst_row, dst_stride, tmp_s, 0, width, 1);
600 if (util_format_fits_8unorm(src_format_desc) ||
601 util_format_fits_8unorm(dst_format_desc)) {
605 tmp_stride = MAX2(width, x_step) * 4 * sizeof *tmp_row;
606 tmp_row = MALLOC(y_step * tmp_stride);
610 while (height >= y_step) {
611 src_format_desc->unpack_rgba_8unorm(tmp_row, tmp_stride, src_row, src_stride, width, y_step);
612 dst_format_desc->pack_rgba_8unorm(dst_row, dst_stride, tmp_row, tmp_stride, width, y_step);
620 src_format_desc->unpack_rgba_8unorm(tmp_row, tmp_stride, src_row, src_stride, width, height);
621 dst_format_desc->pack_rgba_8unorm(dst_row, dst_stride, tmp_row, tmp_stride, width, height);
630 tmp_stride = MAX2(width, x_step) * 4 * sizeof *tmp_row;
631 tmp_row = MALLOC(y_step * tmp_stride);
635 while (height >= y_step) {
636 src_format_desc->unpack_rgba_float(tmp_row, tmp_stride, src_row, src_stride, width, y_step);
637 dst_format_desc->pack_rgba_float(dst_row, dst_stride, tmp_row, tmp_stride, width, y_step);
645 src_format_desc->unpack_rgba_float(tmp_row, tmp_stride, src_row, src_stride, width, height);
646 dst_format_desc->pack_rgba_float(dst_row, dst_stride, tmp_row, tmp_stride, width, height);
653 void util_format_compose_swizzles(const unsigned char swz1[4],
654 const unsigned char swz2[4],
655 unsigned char dst[4])
659 for (i = 0; i < 4; i++) {
660 dst[i] = swz2[i] <= UTIL_FORMAT_SWIZZLE_W ?
661 swz1[swz2[i]] : swz2[i];
665 void util_format_swizzle_4f(float *dst, const float *src,
666 const unsigned char swz[4])
670 for (i = 0; i < 4; i++) {
671 if (swz[i] <= UTIL_FORMAT_SWIZZLE_W)
672 dst[i] = src[swz[i]];
673 else if (swz[i] == UTIL_FORMAT_SWIZZLE_0)
675 else if (swz[i] == UTIL_FORMAT_SWIZZLE_1)
680 void util_format_unswizzle_4f(float *dst, const float *src,
681 const unsigned char swz[4])
685 for (i = 0; i < 4; i++) {
687 case UTIL_FORMAT_SWIZZLE_X:
690 case UTIL_FORMAT_SWIZZLE_Y:
693 case UTIL_FORMAT_SWIZZLE_Z:
696 case UTIL_FORMAT_SWIZZLE_W: