2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009-2010 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THEA AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Image and pixel span packing and unpacking.
33 * XXX: MSVC takes forever to compile this module for x86_64 unless we disable
34 * this global optimization.
37 * - http://msdn.microsoft.com/en-us/library/1yk3ydd7.aspx
38 * - http://msdn.microsoft.com/en-us/library/chh3fb0k.aspx
40 #if defined(_MSC_VER) && defined(_M_X64)
41 # pragma optimize( "g", off )
53 #include "pixeltransfer.h"
55 #include "glformats.h"
56 #include "format_utils.h"
57 #include "format_pack.h"
61 * Flip the 8 bits in each byte of the given array.
64 * \param n number of bytes.
66 * \todo try this trick to flip bytes someday:
68 * v = ((v & 0x55555555) << 1) | ((v >> 1) & 0x55555555);
69 * v = ((v & 0x33333333) << 2) | ((v >> 2) & 0x33333333);
70 * v = ((v & 0x0f0f0f0f) << 4) | ((v >> 4) & 0x0f0f0f0f);
74 flip_bytes( GLubyte
*p
, GLuint n
)
77 for (i
= 0; i
< n
; i
++) {
78 b
= (GLuint
) p
[i
]; /* words are often faster than bytes */
79 a
= ((b
& 0x01) << 7) |
94 * Unpack a 32x32 pixel polygon stipple from user memory using the
95 * current pixel unpack settings.
98 _mesa_unpack_polygon_stipple( const GLubyte
*pattern
, GLuint dest
[32],
99 const struct gl_pixelstore_attrib
*unpacking
)
101 GLubyte
*ptrn
= (GLubyte
*) _mesa_unpack_image(2, 32, 32, 1, GL_COLOR_INDEX
,
102 GL_BITMAP
, pattern
, unpacking
);
104 /* Convert pattern from GLubytes to GLuints and handle big/little
109 for (i
= 0; i
< 32; i
++) {
110 dest
[i
] = (p
[0] << 24)
122 * Pack polygon stipple into user memory given current pixel packing
126 _mesa_pack_polygon_stipple( const GLuint pattern
[32], GLubyte
*dest
,
127 const struct gl_pixelstore_attrib
*packing
)
129 /* Convert pattern from GLuints to GLubytes to handle big/little
130 * endian differences.
134 for (i
= 0; i
< 32; i
++) {
135 ptrn
[i
* 4 + 0] = (GLubyte
) ((pattern
[i
] >> 24) & 0xff);
136 ptrn
[i
* 4 + 1] = (GLubyte
) ((pattern
[i
] >> 16) & 0xff);
137 ptrn
[i
* 4 + 2] = (GLubyte
) ((pattern
[i
] >> 8 ) & 0xff);
138 ptrn
[i
* 4 + 3] = (GLubyte
) ((pattern
[i
] ) & 0xff);
141 _mesa_pack_bitmap(32, 32, ptrn
, dest
, packing
);
149 _mesa_pack_bitmap( GLint width
, GLint height
, const GLubyte
*source
,
150 GLubyte
*dest
, const struct gl_pixelstore_attrib
*packing
)
152 GLint row
, width_in_bytes
;
158 width_in_bytes
= CEILING( width
, 8 );
160 for (row
= 0; row
< height
; row
++) {
161 GLubyte
*dst
= (GLubyte
*) _mesa_image_address2d(packing
, dest
,
162 width
, height
, GL_COLOR_INDEX
, GL_BITMAP
, row
, 0);
166 if ((packing
->SkipPixels
& 7) == 0) {
167 memcpy( dst
, src
, width_in_bytes
);
168 if (packing
->LsbFirst
) {
169 flip_bytes( dst
, width_in_bytes
);
173 /* handling SkipPixels is a bit tricky (no pun intended!) */
175 if (packing
->LsbFirst
) {
176 GLubyte srcMask
= 128;
177 GLubyte dstMask
= 1 << (packing
->SkipPixels
& 0x7);
178 const GLubyte
*s
= src
;
181 for (i
= 0; i
< width
; i
++) {
190 srcMask
= srcMask
>> 1;
192 if (dstMask
== 128) {
198 dstMask
= dstMask
<< 1;
203 GLubyte srcMask
= 128;
204 GLubyte dstMask
= 128 >> (packing
->SkipPixels
& 0x7);
205 const GLubyte
*s
= src
;
208 for (i
= 0; i
< width
; i
++) {
217 srcMask
= srcMask
>> 1;
225 dstMask
= dstMask
>> 1;
230 src
+= width_in_bytes
;
235 #define SWAP2BYTE(VALUE) \
237 GLubyte *bytes = (GLubyte *) &(VALUE); \
238 GLubyte tmp = bytes[0]; \
239 bytes[0] = bytes[1]; \
243 #define SWAP4BYTE(VALUE) \
245 GLubyte *bytes = (GLubyte *) &(VALUE); \
246 GLubyte tmp = bytes[0]; \
247 bytes[0] = bytes[3]; \
250 bytes[1] = bytes[2]; \
256 extract_uint_indexes(GLuint n
, GLuint indexes
[],
257 GLenum srcFormat
, GLenum srcType
, const GLvoid
*src
,
258 const struct gl_pixelstore_attrib
*unpack
)
260 ASSERT(srcFormat
== GL_COLOR_INDEX
|| srcFormat
== GL_STENCIL_INDEX
);
262 ASSERT(srcType
== GL_BITMAP
||
263 srcType
== GL_UNSIGNED_BYTE
||
264 srcType
== GL_BYTE
||
265 srcType
== GL_UNSIGNED_SHORT
||
266 srcType
== GL_SHORT
||
267 srcType
== GL_UNSIGNED_INT
||
269 srcType
== GL_UNSIGNED_INT_24_8_EXT
||
270 srcType
== GL_HALF_FLOAT_ARB
||
271 srcType
== GL_FLOAT
||
272 srcType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
277 GLubyte
*ubsrc
= (GLubyte
*) src
;
278 if (unpack
->LsbFirst
) {
279 GLubyte mask
= 1 << (unpack
->SkipPixels
& 0x7);
281 for (i
= 0; i
< n
; i
++) {
282 indexes
[i
] = (*ubsrc
& mask
) ? 1 : 0;
293 GLubyte mask
= 128 >> (unpack
->SkipPixels
& 0x7);
295 for (i
= 0; i
< n
; i
++) {
296 indexes
[i
] = (*ubsrc
& mask
) ? 1 : 0;
308 case GL_UNSIGNED_BYTE
:
311 const GLubyte
*s
= (const GLubyte
*) src
;
312 for (i
= 0; i
< n
; i
++)
319 const GLbyte
*s
= (const GLbyte
*) src
;
320 for (i
= 0; i
< n
; i
++)
324 case GL_UNSIGNED_SHORT
:
327 const GLushort
*s
= (const GLushort
*) src
;
328 if (unpack
->SwapBytes
) {
329 for (i
= 0; i
< n
; i
++) {
330 GLushort value
= s
[i
];
336 for (i
= 0; i
< n
; i
++)
344 const GLshort
*s
= (const GLshort
*) src
;
345 if (unpack
->SwapBytes
) {
346 for (i
= 0; i
< n
; i
++) {
347 GLshort value
= s
[i
];
353 for (i
= 0; i
< n
; i
++)
358 case GL_UNSIGNED_INT
:
361 const GLuint
*s
= (const GLuint
*) src
;
362 if (unpack
->SwapBytes
) {
363 for (i
= 0; i
< n
; i
++) {
370 for (i
= 0; i
< n
; i
++)
378 const GLint
*s
= (const GLint
*) src
;
379 if (unpack
->SwapBytes
) {
380 for (i
= 0; i
< n
; i
++) {
387 for (i
= 0; i
< n
; i
++)
395 const GLfloat
*s
= (const GLfloat
*) src
;
396 if (unpack
->SwapBytes
) {
397 for (i
= 0; i
< n
; i
++) {
398 GLfloat value
= s
[i
];
400 indexes
[i
] = (GLuint
) value
;
404 for (i
= 0; i
< n
; i
++)
405 indexes
[i
] = (GLuint
) s
[i
];
409 case GL_HALF_FLOAT_ARB
:
412 const GLhalfARB
*s
= (const GLhalfARB
*) src
;
413 if (unpack
->SwapBytes
) {
414 for (i
= 0; i
< n
; i
++) {
415 GLhalfARB value
= s
[i
];
417 indexes
[i
] = (GLuint
) _mesa_half_to_float(value
);
421 for (i
= 0; i
< n
; i
++)
422 indexes
[i
] = (GLuint
) _mesa_half_to_float(s
[i
]);
426 case GL_UNSIGNED_INT_24_8_EXT
:
429 const GLuint
*s
= (const GLuint
*) src
;
430 if (unpack
->SwapBytes
) {
431 for (i
= 0; i
< n
; i
++) {
434 indexes
[i
] = value
& 0xff; /* lower 8 bits */
438 for (i
= 0; i
< n
; i
++)
439 indexes
[i
] = s
[i
] & 0xff; /* lower 8 bits */
443 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
446 const GLuint
*s
= (const GLuint
*) src
;
447 if (unpack
->SwapBytes
) {
448 for (i
= 0; i
< n
; i
++) {
449 GLuint value
= s
[i
*2+1];
451 indexes
[i
] = value
& 0xff; /* lower 8 bits */
455 for (i
= 0; i
< n
; i
++)
456 indexes
[i
] = s
[i
*2+1] & 0xff; /* lower 8 bits */
462 _mesa_problem(NULL
, "bad srcType in extract_uint_indexes");
469 clamp_float_to_uint(GLfloat f
)
471 return f
< 0.0F
? 0 : F_TO_I(f
);
476 clamp_half_to_uint(GLhalfARB h
)
478 GLfloat f
= _mesa_half_to_float(h
);
479 return f
< 0.0F
? 0 : F_TO_I(f
);
484 * Unpack a row of stencil data from a client buffer according to
485 * the pixel unpacking parameters.
486 * This is (or will be) used by glDrawPixels
488 * Args: ctx - the context
489 * n - number of pixels
490 * dstType - destination data type
491 * dest - destination array
492 * srcType - source pixel type
493 * source - source data pointer
494 * srcPacking - pixel unpacking parameters
495 * transferOps - apply offset/bias/lookup ops?
498 _mesa_unpack_stencil_span( struct gl_context
*ctx
, GLuint n
,
499 GLenum dstType
, GLvoid
*dest
,
500 GLenum srcType
, const GLvoid
*source
,
501 const struct gl_pixelstore_attrib
*srcPacking
,
502 GLbitfield transferOps
)
504 ASSERT(srcType
== GL_BITMAP
||
505 srcType
== GL_UNSIGNED_BYTE
||
506 srcType
== GL_BYTE
||
507 srcType
== GL_UNSIGNED_SHORT
||
508 srcType
== GL_SHORT
||
509 srcType
== GL_UNSIGNED_INT
||
511 srcType
== GL_UNSIGNED_INT_24_8_EXT
||
512 srcType
== GL_HALF_FLOAT_ARB
||
513 srcType
== GL_FLOAT
||
514 srcType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
516 ASSERT(dstType
== GL_UNSIGNED_BYTE
||
517 dstType
== GL_UNSIGNED_SHORT
||
518 dstType
== GL_UNSIGNED_INT
||
519 dstType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
521 /* only shift and offset apply to stencil */
522 transferOps
&= IMAGE_SHIFT_OFFSET_BIT
;
525 * Try simple cases first
527 if (transferOps
== 0 &&
528 !ctx
->Pixel
.MapStencilFlag
&&
529 srcType
== GL_UNSIGNED_BYTE
&&
530 dstType
== GL_UNSIGNED_BYTE
) {
531 memcpy(dest
, source
, n
* sizeof(GLubyte
));
533 else if (transferOps
== 0 &&
534 !ctx
->Pixel
.MapStencilFlag
&&
535 srcType
== GL_UNSIGNED_INT
&&
536 dstType
== GL_UNSIGNED_INT
&&
537 !srcPacking
->SwapBytes
) {
538 memcpy(dest
, source
, n
* sizeof(GLuint
));
544 GLuint
*indexes
= malloc(n
* sizeof(GLuint
));
547 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "stencil unpacking");
551 extract_uint_indexes(n
, indexes
, GL_STENCIL_INDEX
, srcType
, source
,
554 if (transferOps
& IMAGE_SHIFT_OFFSET_BIT
) {
555 /* shift and offset indexes */
556 _mesa_shift_and_offset_ci(ctx
, n
, indexes
);
559 if (ctx
->Pixel
.MapStencilFlag
) {
560 /* Apply stencil lookup table */
561 const GLuint mask
= ctx
->PixelMaps
.StoS
.Size
- 1;
563 for (i
= 0; i
< n
; i
++) {
564 indexes
[i
] = (GLuint
)ctx
->PixelMaps
.StoS
.Map
[ indexes
[i
] & mask
];
568 /* convert to dest type */
570 case GL_UNSIGNED_BYTE
:
572 GLubyte
*dst
= (GLubyte
*) dest
;
574 for (i
= 0; i
< n
; i
++) {
575 dst
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
579 case GL_UNSIGNED_SHORT
:
581 GLuint
*dst
= (GLuint
*) dest
;
583 for (i
= 0; i
< n
; i
++) {
584 dst
[i
] = (GLushort
) (indexes
[i
] & 0xffff);
588 case GL_UNSIGNED_INT
:
589 memcpy(dest
, indexes
, n
* sizeof(GLuint
));
591 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
593 GLuint
*dst
= (GLuint
*) dest
;
595 for (i
= 0; i
< n
; i
++) {
596 dst
[i
*2+1] = indexes
[i
] & 0xff; /* lower 8 bits */
601 _mesa_problem(ctx
, "bad dstType in _mesa_unpack_stencil_span");
610 _mesa_pack_stencil_span( struct gl_context
*ctx
, GLuint n
,
611 GLenum dstType
, GLvoid
*dest
, const GLubyte
*source
,
612 const struct gl_pixelstore_attrib
*dstPacking
)
614 GLubyte
*stencil
= malloc(n
* sizeof(GLubyte
));
617 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "stencil packing");
621 if (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
||
622 ctx
->Pixel
.MapStencilFlag
) {
623 /* make a copy of input */
624 memcpy(stencil
, source
, n
* sizeof(GLubyte
));
625 _mesa_apply_stencil_transfer_ops(ctx
, n
, stencil
);
630 case GL_UNSIGNED_BYTE
:
631 memcpy(dest
, source
, n
);
635 GLbyte
*dst
= (GLbyte
*) dest
;
638 dst
[i
] = (GLbyte
) (source
[i
] & 0x7f);
642 case GL_UNSIGNED_SHORT
:
644 GLushort
*dst
= (GLushort
*) dest
;
647 dst
[i
] = (GLushort
) source
[i
];
649 if (dstPacking
->SwapBytes
) {
650 _mesa_swap2( (GLushort
*) dst
, n
);
656 GLshort
*dst
= (GLshort
*) dest
;
659 dst
[i
] = (GLshort
) source
[i
];
661 if (dstPacking
->SwapBytes
) {
662 _mesa_swap2( (GLushort
*) dst
, n
);
666 case GL_UNSIGNED_INT
:
668 GLuint
*dst
= (GLuint
*) dest
;
671 dst
[i
] = (GLuint
) source
[i
];
673 if (dstPacking
->SwapBytes
) {
674 _mesa_swap4( (GLuint
*) dst
, n
);
680 GLint
*dst
= (GLint
*) dest
;
683 dst
[i
] = (GLint
) source
[i
];
685 if (dstPacking
->SwapBytes
) {
686 _mesa_swap4( (GLuint
*) dst
, n
);
692 GLfloat
*dst
= (GLfloat
*) dest
;
695 dst
[i
] = (GLfloat
) source
[i
];
697 if (dstPacking
->SwapBytes
) {
698 _mesa_swap4( (GLuint
*) dst
, n
);
702 case GL_HALF_FLOAT_ARB
:
704 GLhalfARB
*dst
= (GLhalfARB
*) dest
;
707 dst
[i
] = _mesa_float_to_half( (float) source
[i
] );
709 if (dstPacking
->SwapBytes
) {
710 _mesa_swap2( (GLushort
*) dst
, n
);
715 if (dstPacking
->LsbFirst
) {
716 GLubyte
*dst
= (GLubyte
*) dest
;
719 for (i
= 0; i
< n
; i
++) {
722 *dst
|= ((source
[i
] != 0) << shift
);
731 GLubyte
*dst
= (GLubyte
*) dest
;
734 for (i
= 0; i
< n
; i
++) {
737 *dst
|= ((source
[i
] != 0) << shift
);
747 _mesa_problem(ctx
, "bad type in _mesa_pack_index_span");
753 #define DEPTH_VALUES(GLTYPE, GLTYPE2FLOAT) \
756 const GLTYPE *src = (const GLTYPE *)source; \
757 for (i = 0; i < n; i++) { \
758 GLTYPE value = src[i]; \
759 if (srcPacking->SwapBytes) { \
760 if (sizeof(GLTYPE) == 2) { \
762 } else if (sizeof(GLTYPE) == 4) { \
766 depthValues[i] = GLTYPE2FLOAT(value); \
772 * Unpack a row of depth/z values from memory, returning GLushort, GLuint
774 * The glPixelTransfer (scale/bias) params will be applied.
776 * \param dstType one of GL_UNSIGNED_SHORT, GL_UNSIGNED_INT, GL_FLOAT
777 * \param depthMax max value for returned GLushort or GLuint values
778 * (ignored for GLfloat).
781 _mesa_unpack_depth_span( struct gl_context
*ctx
, GLuint n
,
782 GLenum dstType
, GLvoid
*dest
, GLuint depthMax
,
783 GLenum srcType
, const GLvoid
*source
,
784 const struct gl_pixelstore_attrib
*srcPacking
)
786 GLfloat
*depthTemp
= NULL
, *depthValues
;
787 GLboolean needClamp
= GL_FALSE
;
789 /* Look for special cases first.
790 * Not only are these faster, they're less prone to numeric conversion
791 * problems. Otherwise, converting from an int type to a float then
792 * back to an int type can introduce errors that will show up as
793 * artifacts in things like depth peeling which uses glCopyTexImage.
795 if (ctx
->Pixel
.DepthScale
== 1.0 && ctx
->Pixel
.DepthBias
== 0.0) {
796 if (srcType
== GL_UNSIGNED_INT
&& dstType
== GL_UNSIGNED_SHORT
) {
797 const GLuint
*src
= (const GLuint
*) source
;
798 GLushort
*dst
= (GLushort
*) dest
;
800 for (i
= 0; i
< n
; i
++) {
801 dst
[i
] = src
[i
] >> 16;
805 if (srcType
== GL_UNSIGNED_SHORT
806 && dstType
== GL_UNSIGNED_INT
807 && depthMax
== 0xffffffff) {
808 const GLushort
*src
= (const GLushort
*) source
;
809 GLuint
*dst
= (GLuint
*) dest
;
811 for (i
= 0; i
< n
; i
++) {
812 dst
[i
] = src
[i
] | (src
[i
] << 16);
816 if (srcType
== GL_UNSIGNED_INT_24_8
817 && dstType
== GL_UNSIGNED_INT
818 && depthMax
== 0xffffff) {
819 const GLuint
*src
= (const GLuint
*) source
;
820 GLuint
*dst
= (GLuint
*) dest
;
822 for (i
= 0; i
< n
; i
++) {
823 dst
[i
] = src
[i
] >> 8;
827 /* XXX may want to add additional cases here someday */
830 /* general case path follows */
832 if (dstType
== GL_FLOAT
) {
833 depthValues
= (GLfloat
*) dest
;
836 depthTemp
= malloc(n
* sizeof(GLfloat
));
838 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
842 depthValues
= depthTemp
;
845 /* Convert incoming values to GLfloat. Some conversions will require
850 DEPTH_VALUES(GLbyte
, BYTE_TO_FLOATZ
);
853 case GL_UNSIGNED_BYTE
:
854 DEPTH_VALUES(GLubyte
, UBYTE_TO_FLOAT
);
857 DEPTH_VALUES(GLshort
, SHORT_TO_FLOATZ
);
860 case GL_UNSIGNED_SHORT
:
861 DEPTH_VALUES(GLushort
, USHORT_TO_FLOAT
);
864 DEPTH_VALUES(GLint
, INT_TO_FLOAT
);
867 case GL_UNSIGNED_INT
:
868 DEPTH_VALUES(GLuint
, UINT_TO_FLOAT
);
870 case GL_UNSIGNED_INT_24_8_EXT
: /* GL_EXT_packed_depth_stencil */
871 if (dstType
== GL_UNSIGNED_INT_24_8_EXT
&&
872 depthMax
== 0xffffff &&
873 ctx
->Pixel
.DepthScale
== 1.0 &&
874 ctx
->Pixel
.DepthBias
== 0.0) {
875 const GLuint
*src
= (const GLuint
*) source
;
876 GLuint
*zValues
= (GLuint
*) dest
;
878 for (i
= 0; i
< n
; i
++) {
879 GLuint value
= src
[i
];
880 if (srcPacking
->SwapBytes
) {
883 zValues
[i
] = value
& 0xffffff00;
889 const GLuint
*src
= (const GLuint
*) source
;
890 const GLfloat scale
= 1.0f
/ 0xffffff;
892 for (i
= 0; i
< n
; i
++) {
893 GLuint value
= src
[i
];
894 if (srcPacking
->SwapBytes
) {
897 depthValues
[i
] = (value
>> 8) * scale
;
901 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
904 const GLfloat
*src
= (const GLfloat
*)source
;
905 for (i
= 0; i
< n
; i
++) {
906 GLfloat value
= src
[i
* 2];
907 if (srcPacking
->SwapBytes
) {
910 depthValues
[i
] = value
;
916 DEPTH_VALUES(GLfloat
, 1*);
919 case GL_HALF_FLOAT_ARB
:
922 const GLhalfARB
*src
= (const GLhalfARB
*) source
;
923 for (i
= 0; i
< n
; i
++) {
924 GLhalfARB value
= src
[i
];
925 if (srcPacking
->SwapBytes
) {
928 depthValues
[i
] = _mesa_half_to_float(value
);
934 _mesa_problem(NULL
, "bad type in _mesa_unpack_depth_span()");
939 /* apply depth scale and bias */
941 const GLfloat scale
= ctx
->Pixel
.DepthScale
;
942 const GLfloat bias
= ctx
->Pixel
.DepthBias
;
943 if (scale
!= 1.0 || bias
!= 0.0) {
945 for (i
= 0; i
< n
; i
++) {
946 depthValues
[i
] = depthValues
[i
] * scale
+ bias
;
952 /* clamp to [0, 1] */
955 for (i
= 0; i
< n
; i
++) {
956 depthValues
[i
] = (GLfloat
)CLAMP(depthValues
[i
], 0.0, 1.0);
961 * Convert values to dstType
963 if (dstType
== GL_UNSIGNED_INT
) {
964 GLuint
*zValues
= (GLuint
*) dest
;
966 if (depthMax
<= 0xffffff) {
967 /* no overflow worries */
968 for (i
= 0; i
< n
; i
++) {
969 zValues
[i
] = (GLuint
) (depthValues
[i
] * (GLfloat
) depthMax
);
973 /* need to use double precision to prevent overflow problems */
974 for (i
= 0; i
< n
; i
++) {
975 GLdouble z
= depthValues
[i
] * (GLdouble
) depthMax
;
976 if (z
>= (GLdouble
) 0xffffffff)
977 zValues
[i
] = 0xffffffff;
979 zValues
[i
] = (GLuint
) z
;
983 else if (dstType
== GL_UNSIGNED_SHORT
) {
984 GLushort
*zValues
= (GLushort
*) dest
;
986 ASSERT(depthMax
<= 0xffff);
987 for (i
= 0; i
< n
; i
++) {
988 zValues
[i
] = (GLushort
) (depthValues
[i
] * (GLfloat
) depthMax
);
991 else if (dstType
== GL_FLOAT
) {
992 /* Nothing to do. depthValues is pointing to dest. */
994 else if (dstType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
) {
995 GLfloat
*zValues
= (GLfloat
*) dest
;
997 for (i
= 0; i
< n
; i
++) {
998 zValues
[i
*2] = depthValues
[i
];
1010 * Pack an array of depth values. The values are floats in [0,1].
1013 _mesa_pack_depth_span( struct gl_context
*ctx
, GLuint n
, GLvoid
*dest
,
1014 GLenum dstType
, const GLfloat
*depthSpan
,
1015 const struct gl_pixelstore_attrib
*dstPacking
)
1017 GLfloat
*depthCopy
= malloc(n
* sizeof(GLfloat
));
1019 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
1023 if (ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0) {
1024 memcpy(depthCopy
, depthSpan
, n
* sizeof(GLfloat
));
1025 _mesa_scale_and_bias_depth(ctx
, n
, depthCopy
);
1026 depthSpan
= depthCopy
;
1030 case GL_UNSIGNED_BYTE
:
1032 GLubyte
*dst
= (GLubyte
*) dest
;
1034 for (i
= 0; i
< n
; i
++) {
1035 dst
[i
] = FLOAT_TO_UBYTE( depthSpan
[i
] );
1041 GLbyte
*dst
= (GLbyte
*) dest
;
1043 for (i
= 0; i
< n
; i
++) {
1044 dst
[i
] = FLOAT_TO_BYTE( depthSpan
[i
] );
1048 case GL_UNSIGNED_SHORT
:
1050 GLushort
*dst
= (GLushort
*) dest
;
1052 for (i
= 0; i
< n
; i
++) {
1053 CLAMPED_FLOAT_TO_USHORT(dst
[i
], depthSpan
[i
]);
1055 if (dstPacking
->SwapBytes
) {
1056 _mesa_swap2( (GLushort
*) dst
, n
);
1062 GLshort
*dst
= (GLshort
*) dest
;
1064 for (i
= 0; i
< n
; i
++) {
1065 dst
[i
] = FLOAT_TO_SHORT( depthSpan
[i
] );
1067 if (dstPacking
->SwapBytes
) {
1068 _mesa_swap2( (GLushort
*) dst
, n
);
1072 case GL_UNSIGNED_INT
:
1074 GLuint
*dst
= (GLuint
*) dest
;
1076 for (i
= 0; i
< n
; i
++) {
1077 dst
[i
] = FLOAT_TO_UINT( depthSpan
[i
] );
1079 if (dstPacking
->SwapBytes
) {
1080 _mesa_swap4( (GLuint
*) dst
, n
);
1086 GLint
*dst
= (GLint
*) dest
;
1088 for (i
= 0; i
< n
; i
++) {
1089 dst
[i
] = FLOAT_TO_INT( depthSpan
[i
] );
1091 if (dstPacking
->SwapBytes
) {
1092 _mesa_swap4( (GLuint
*) dst
, n
);
1098 GLfloat
*dst
= (GLfloat
*) dest
;
1100 for (i
= 0; i
< n
; i
++) {
1101 dst
[i
] = depthSpan
[i
];
1103 if (dstPacking
->SwapBytes
) {
1104 _mesa_swap4( (GLuint
*) dst
, n
);
1108 case GL_HALF_FLOAT_ARB
:
1110 GLhalfARB
*dst
= (GLhalfARB
*) dest
;
1112 for (i
= 0; i
< n
; i
++) {
1113 dst
[i
] = _mesa_float_to_half(depthSpan
[i
]);
1115 if (dstPacking
->SwapBytes
) {
1116 _mesa_swap2( (GLushort
*) dst
, n
);
1121 _mesa_problem(ctx
, "bad type in _mesa_pack_depth_span");
1130 * Pack depth and stencil values as GL_DEPTH_STENCIL (GL_UNSIGNED_INT_24_8 etc)
1133 _mesa_pack_depth_stencil_span(struct gl_context
*ctx
,GLuint n
,
1134 GLenum dstType
, GLuint
*dest
,
1135 const GLfloat
*depthVals
,
1136 const GLubyte
*stencilVals
,
1137 const struct gl_pixelstore_attrib
*dstPacking
)
1139 GLfloat
*depthCopy
= malloc(n
* sizeof(GLfloat
));
1140 GLubyte
*stencilCopy
= malloc(n
* sizeof(GLubyte
));
1143 if (!depthCopy
|| !stencilCopy
) {
1144 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
1150 if (ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0) {
1151 memcpy(depthCopy
, depthVals
, n
* sizeof(GLfloat
));
1152 _mesa_scale_and_bias_depth(ctx
, n
, depthCopy
);
1153 depthVals
= depthCopy
;
1156 if (ctx
->Pixel
.IndexShift
||
1157 ctx
->Pixel
.IndexOffset
||
1158 ctx
->Pixel
.MapStencilFlag
) {
1159 memcpy(stencilCopy
, stencilVals
, n
* sizeof(GLubyte
));
1160 _mesa_apply_stencil_transfer_ops(ctx
, n
, stencilCopy
);
1161 stencilVals
= stencilCopy
;
1165 case GL_UNSIGNED_INT_24_8
:
1166 for (i
= 0; i
< n
; i
++) {
1167 GLuint z
= (GLuint
) (depthVals
[i
] * 0xffffff);
1168 dest
[i
] = (z
<< 8) | (stencilVals
[i
] & 0xff);
1171 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
1172 for (i
= 0; i
< n
; i
++) {
1173 ((GLfloat
*)dest
)[i
*2] = depthVals
[i
];
1174 dest
[i
*2+1] = stencilVals
[i
] & 0xff;
1179 if (dstPacking
->SwapBytes
) {
1180 _mesa_swap4(dest
, n
);
1190 * Unpack image data. Apply byte swapping, byte flipping (bitmap).
1191 * Return all image data in a contiguous block. This is used when we
1192 * compile glDrawPixels, glTexImage, etc into a display list. We
1193 * need a copy of the data in a standard format.
1196 _mesa_unpack_image( GLuint dimensions
,
1197 GLsizei width
, GLsizei height
, GLsizei depth
,
1198 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1199 const struct gl_pixelstore_attrib
*unpack
)
1201 GLint bytesPerRow
, compsPerRow
;
1202 GLboolean flipBytes
, swap2
, swap4
;
1205 return NULL
; /* not necessarily an error */
1207 if (width
<= 0 || height
<= 0 || depth
<= 0)
1208 return NULL
; /* generate error later */
1210 if (type
== GL_BITMAP
) {
1211 bytesPerRow
= (width
+ 7) >> 3;
1212 flipBytes
= unpack
->LsbFirst
;
1213 swap2
= swap4
= GL_FALSE
;
1217 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1218 GLint components
= _mesa_components_in_format(format
);
1221 if (_mesa_type_is_packed(type
))
1224 if (bytesPerPixel
<= 0 || components
<= 0)
1225 return NULL
; /* bad format or type. generate error later */
1226 bytesPerRow
= bytesPerPixel
* width
;
1227 bytesPerComp
= bytesPerPixel
/ components
;
1228 flipBytes
= GL_FALSE
;
1229 swap2
= (bytesPerComp
== 2) && unpack
->SwapBytes
;
1230 swap4
= (bytesPerComp
== 4) && unpack
->SwapBytes
;
1231 compsPerRow
= components
* width
;
1232 assert(compsPerRow
>= width
);
1237 = malloc(bytesPerRow
* height
* depth
);
1241 return NULL
; /* generate GL_OUT_OF_MEMORY later */
1244 for (img
= 0; img
< depth
; img
++) {
1245 for (row
= 0; row
< height
; row
++) {
1246 const GLvoid
*src
= _mesa_image_address(dimensions
, unpack
, pixels
,
1247 width
, height
, format
, type
, img
, row
, 0);
1249 if ((type
== GL_BITMAP
) && (unpack
->SkipPixels
& 0x7)) {
1251 flipBytes
= GL_FALSE
;
1252 if (unpack
->LsbFirst
) {
1253 GLubyte srcMask
= 1 << (unpack
->SkipPixels
& 0x7);
1254 GLubyte dstMask
= 128;
1255 const GLubyte
*s
= src
;
1258 for (i
= 0; i
< width
; i
++) {
1262 if (srcMask
== 128) {
1267 srcMask
= srcMask
<< 1;
1275 dstMask
= dstMask
>> 1;
1280 GLubyte srcMask
= 128 >> (unpack
->SkipPixels
& 0x7);
1281 GLubyte dstMask
= 128;
1282 const GLubyte
*s
= src
;
1285 for (i
= 0; i
< width
; i
++) {
1294 srcMask
= srcMask
>> 1;
1302 dstMask
= dstMask
>> 1;
1308 memcpy(dst
, src
, bytesPerRow
);
1311 /* byte flipping/swapping */
1313 flip_bytes((GLubyte
*) dst
, bytesPerRow
);
1316 _mesa_swap2((GLushort
*) dst
, compsPerRow
);
1319 _mesa_swap4((GLuint
*) dst
, compsPerRow
);
1329 _mesa_pack_luminance_from_rgba_float(GLuint n
, GLfloat rgba
[][4],
1330 GLvoid
*dstAddr
, GLenum dst_format
,
1331 GLbitfield transferOps
)
1334 GLfloat
*dst
= (GLfloat
*) dstAddr
;
1336 switch (dst_format
) {
1338 if (transferOps
& IMAGE_CLAMP_BIT
) {
1339 for (i
= 0; i
< n
; i
++) {
1340 GLfloat sum
= rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
1341 dst
[i
] = CLAMP(sum
, 0.0F
, 1.0F
);
1344 for (i
= 0; i
< n
; i
++) {
1345 dst
[i
] = rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
1349 case GL_LUMINANCE_ALPHA
:
1350 if (transferOps
& IMAGE_CLAMP_BIT
) {
1351 for (i
= 0; i
< n
; i
++) {
1352 GLfloat sum
= rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
1353 dst
[2*i
] = CLAMP(sum
, 0.0F
, 1.0F
);
1354 dst
[2*i
+1] = rgba
[i
][ACOMP
];
1357 for (i
= 0; i
< n
; i
++) {
1358 dst
[2*i
] = rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
1359 dst
[2*i
+1] = rgba
[i
][ACOMP
];
1364 assert(!"Unsupported format");
1369 clamp_sint64_to_sint32(int64_t src
)
1371 return CLAMP(src
, INT32_MIN
, INT32_MAX
);
1375 clamp_sint64_to_uint32(int64_t src
)
1377 return CLAMP(src
, 0, UINT32_MAX
);
1381 clamp_uint64_to_uint32(uint64_t src
)
1383 return MIN2(src
, UINT32_MAX
);
1387 clamp_uint64_to_sint32(uint64_t src
)
1389 return MIN2(src
, INT32_MAX
);
1393 convert_integer_luminance64(int64_t src64
, int bits
,
1394 bool dst_is_signed
, bool src_is_signed
)
1398 /* Clamp Luminance value from 64-bit to 32-bit. Consider if we need
1399 * any signed<->unsigned conversion too.
1401 if (src_is_signed
&& dst_is_signed
)
1402 src32
= clamp_sint64_to_sint32(src64
);
1403 else if (src_is_signed
&& !dst_is_signed
)
1404 src32
= clamp_sint64_to_uint32(src64
);
1405 else if (!src_is_signed
&& dst_is_signed
)
1406 src32
= clamp_uint64_to_sint32(src64
);
1408 src32
= clamp_uint64_to_uint32(src64
);
1410 /* If the dst type is < 32-bit, we need an extra clamp */
1415 return _mesa_signed_to_signed(src32
, bits
);
1417 return _mesa_unsigned_to_unsigned(src32
, bits
);
1422 convert_integer(int32_t src
, int bits
, bool dst_is_signed
, bool src_is_signed
)
1424 if (src_is_signed
&& dst_is_signed
)
1425 return _mesa_signed_to_signed(src
, bits
);
1426 else if (src_is_signed
&& !dst_is_signed
)
1427 return _mesa_signed_to_unsigned(src
, bits
);
1428 else if (!src_is_signed
&& dst_is_signed
)
1429 return _mesa_unsigned_to_signed(src
, bits
);
1431 return _mesa_unsigned_to_unsigned(src
, bits
);
1435 _mesa_pack_luminance_from_rgba_integer(GLuint n
,
1436 GLuint rgba
[][4], bool rgba_is_signed
,
1441 assert(dst_format
== GL_LUMINANCE_INTEGER_EXT
||
1442 dst_format
== GL_LUMINANCE_ALPHA_INTEGER_EXT
);
1446 int32_t lum32
, alpha
;
1450 /* We first compute luminance values as a 64-bit addition of the
1451 * 32-bit R,G,B components, then we clamp the result to the dst type size.
1453 * Notice that this operation involves casting the 32-bit R,G,B components
1454 * to 64-bit before the addition. Since rgba is defined as a GLuint array
1455 * we need to be careful when rgba packs signed data and make sure
1456 * that we cast to a 32-bit signed integer values before casting them to
1457 * 64-bit signed integers.
1459 dst_is_signed
= (dst_type
== GL_BYTE
|| dst_type
== GL_SHORT
||
1460 dst_type
== GL_INT
);
1462 dst_bits
= _mesa_sizeof_type(dst_type
) * 8;
1463 assert(dst_bits
> 0);
1465 switch (dst_format
) {
1466 case GL_LUMINANCE_INTEGER_EXT
:
1467 for (i
= 0; i
< n
; i
++) {
1468 if (!rgba_is_signed
) {
1469 lum64
= (uint64_t) rgba
[i
][RCOMP
] +
1470 (uint64_t) rgba
[i
][GCOMP
] +
1471 (uint64_t) rgba
[i
][BCOMP
];
1473 lum64
= (int64_t) ((int32_t) rgba
[i
][RCOMP
]) +
1474 (int64_t) ((int32_t) rgba
[i
][GCOMP
]) +
1475 (int64_t) ((int32_t) rgba
[i
][BCOMP
]);
1477 lum32
= convert_integer_luminance64(lum64
, dst_bits
,
1478 dst_is_signed
, rgba_is_signed
);
1481 case GL_UNSIGNED_BYTE
: {
1482 GLbyte
*dst
= (GLbyte
*) dstAddr
;
1487 case GL_UNSIGNED_SHORT
: {
1488 GLshort
*dst
= (GLshort
*) dstAddr
;
1493 case GL_UNSIGNED_INT
: {
1494 GLint
*dst
= (GLint
*) dstAddr
;
1501 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
1502 for (i
= 0; i
< n
; i
++) {
1503 if (!rgba_is_signed
) {
1504 lum64
= (uint64_t) rgba
[i
][RCOMP
] +
1505 (uint64_t) rgba
[i
][GCOMP
] +
1506 (uint64_t) rgba
[i
][BCOMP
];
1508 lum64
= (int64_t) ((int32_t) rgba
[i
][RCOMP
]) +
1509 (int64_t) ((int32_t) rgba
[i
][GCOMP
]) +
1510 (int64_t) ((int32_t) rgba
[i
][BCOMP
]);
1512 lum32
= convert_integer_luminance64(lum64
, dst_bits
,
1513 dst_is_signed
, rgba_is_signed
);
1514 alpha
= convert_integer(rgba
[i
][ACOMP
], dst_bits
,
1515 dst_is_signed
, rgba_is_signed
);
1518 case GL_UNSIGNED_BYTE
: {
1519 GLbyte
*dst
= (GLbyte
*) dstAddr
;
1524 case GL_UNSIGNED_SHORT
: {
1525 GLshort
*dst
= (GLshort
*) dstAddr
;
1531 case GL_UNSIGNED_INT
: {
1532 GLint
*dst
= (GLint
*) dstAddr
;
1544 _mesa_unpack_color_index_to_rgba_float(struct gl_context
*ctx
, GLuint dims
,
1545 const void *src
, GLenum srcFormat
, GLenum srcType
,
1546 int srcWidth
, int srcHeight
, int srcDepth
,
1547 const struct gl_pixelstore_attrib
*srcPacking
,
1548 GLbitfield transferOps
)
1552 GLfloat
*rgba
, *dstPtr
;
1554 count
= srcWidth
* srcHeight
;
1555 indexes
= malloc(count
* sizeof(GLuint
));
1557 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
1561 rgba
= malloc(4 * count
* srcDepth
* sizeof(GLfloat
));
1564 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
1568 /* Convert indexes to RGBA float */
1570 for (img
= 0; img
< srcDepth
; img
++) {
1571 const GLubyte
*srcPtr
=
1572 (const GLubyte
*) _mesa_image_address(dims
, srcPacking
, src
,
1573 srcWidth
, srcHeight
,
1577 extract_uint_indexes(count
, indexes
, srcFormat
, srcType
, srcPtr
, srcPacking
);
1579 if (transferOps
& IMAGE_SHIFT_OFFSET_BIT
)
1580 _mesa_shift_and_offset_ci(ctx
, count
, indexes
);
1582 _mesa_map_ci_to_rgba(ctx
, count
, indexes
, (float (*)[4])dstPtr
);
1584 /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
1585 * with color indexes.
1587 transferOps
&= ~(IMAGE_SCALE_BIAS_BIT
| IMAGE_MAP_COLOR_BIT
);
1588 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, count
, (float (*)[4])dstPtr
);
1590 dstPtr
+= srcHeight
* srcWidth
* 4;
1599 _mesa_unpack_color_index_to_rgba_ubyte(struct gl_context
*ctx
, GLuint dims
,
1600 const void *src
, GLenum srcFormat
, GLenum srcType
,
1601 int srcWidth
, int srcHeight
, int srcDepth
,
1602 const struct gl_pixelstore_attrib
*srcPacking
,
1603 GLbitfield transferOps
)
1609 transferOps
|= IMAGE_CLAMP_BIT
;
1610 rgba
= _mesa_unpack_color_index_to_rgba_float(ctx
, dims
,
1611 src
, srcFormat
, srcType
,
1612 srcWidth
, srcHeight
, srcDepth
,
1613 srcPacking
, transferOps
);
1615 count
= srcWidth
* srcHeight
* srcDepth
;
1616 dst
= malloc(count
* 4 * sizeof(GLubyte
));
1617 for (i
= 0; i
< count
; i
++) {
1618 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 0], rgba
[i
* 4 + 0]);
1619 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 1], rgba
[i
* 4 + 1]);
1620 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 2], rgba
[i
* 4 + 2]);
1621 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 3], rgba
[i
* 4 + 3]);