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
= DIV_ROUND_UP( 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_HALF_FLOAT_OES
||
272 srcType
== GL_FLOAT
||
273 srcType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
278 GLubyte
*ubsrc
= (GLubyte
*) src
;
279 if (unpack
->LsbFirst
) {
280 GLubyte mask
= 1 << (unpack
->SkipPixels
& 0x7);
282 for (i
= 0; i
< n
; i
++) {
283 indexes
[i
] = (*ubsrc
& mask
) ? 1 : 0;
294 GLubyte mask
= 128 >> (unpack
->SkipPixels
& 0x7);
296 for (i
= 0; i
< n
; i
++) {
297 indexes
[i
] = (*ubsrc
& mask
) ? 1 : 0;
309 case GL_UNSIGNED_BYTE
:
312 const GLubyte
*s
= (const GLubyte
*) src
;
313 for (i
= 0; i
< n
; i
++)
320 const GLbyte
*s
= (const GLbyte
*) src
;
321 for (i
= 0; i
< n
; i
++)
325 case GL_UNSIGNED_SHORT
:
328 const GLushort
*s
= (const GLushort
*) src
;
329 if (unpack
->SwapBytes
) {
330 for (i
= 0; i
< n
; i
++) {
331 GLushort value
= s
[i
];
337 for (i
= 0; i
< n
; i
++)
345 const GLshort
*s
= (const GLshort
*) src
;
346 if (unpack
->SwapBytes
) {
347 for (i
= 0; i
< n
; i
++) {
348 GLshort value
= s
[i
];
354 for (i
= 0; i
< n
; i
++)
359 case GL_UNSIGNED_INT
:
362 const GLuint
*s
= (const GLuint
*) src
;
363 if (unpack
->SwapBytes
) {
364 for (i
= 0; i
< n
; i
++) {
371 for (i
= 0; i
< n
; i
++)
379 const GLint
*s
= (const GLint
*) src
;
380 if (unpack
->SwapBytes
) {
381 for (i
= 0; i
< n
; i
++) {
388 for (i
= 0; i
< n
; i
++)
396 const GLfloat
*s
= (const GLfloat
*) src
;
397 if (unpack
->SwapBytes
) {
398 for (i
= 0; i
< n
; i
++) {
399 GLfloat value
= s
[i
];
401 indexes
[i
] = (GLuint
) value
;
405 for (i
= 0; i
< n
; i
++)
406 indexes
[i
] = (GLuint
) s
[i
];
410 case GL_HALF_FLOAT_ARB
:
411 case GL_HALF_FLOAT_OES
:
414 const GLhalfARB
*s
= (const GLhalfARB
*) src
;
415 if (unpack
->SwapBytes
) {
416 for (i
= 0; i
< n
; i
++) {
417 GLhalfARB value
= s
[i
];
419 indexes
[i
] = (GLuint
) _mesa_half_to_float(value
);
423 for (i
= 0; i
< n
; i
++)
424 indexes
[i
] = (GLuint
) _mesa_half_to_float(s
[i
]);
428 case GL_UNSIGNED_INT_24_8_EXT
:
431 const GLuint
*s
= (const GLuint
*) src
;
432 if (unpack
->SwapBytes
) {
433 for (i
= 0; i
< n
; i
++) {
436 indexes
[i
] = value
& 0xff; /* lower 8 bits */
440 for (i
= 0; i
< n
; i
++)
441 indexes
[i
] = s
[i
] & 0xff; /* lower 8 bits */
445 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
448 const GLuint
*s
= (const GLuint
*) src
;
449 if (unpack
->SwapBytes
) {
450 for (i
= 0; i
< n
; i
++) {
451 GLuint value
= s
[i
*2+1];
453 indexes
[i
] = value
& 0xff; /* lower 8 bits */
457 for (i
= 0; i
< n
; i
++)
458 indexes
[i
] = s
[i
*2+1] & 0xff; /* lower 8 bits */
464 unreachable("bad srcType in extract_uint_indexes");
470 * Unpack a row of stencil data from a client buffer according to
471 * the pixel unpacking parameters.
472 * This is (or will be) used by glDrawPixels
474 * Args: ctx - the context
475 * n - number of pixels
476 * dstType - destination data type
477 * dest - destination array
478 * srcType - source pixel type
479 * source - source data pointer
480 * srcPacking - pixel unpacking parameters
481 * transferOps - apply offset/bias/lookup ops?
484 _mesa_unpack_stencil_span( struct gl_context
*ctx
, GLuint n
,
485 GLenum dstType
, GLvoid
*dest
,
486 GLenum srcType
, const GLvoid
*source
,
487 const struct gl_pixelstore_attrib
*srcPacking
,
488 GLbitfield transferOps
)
490 assert(srcType
== GL_BITMAP
||
491 srcType
== GL_UNSIGNED_BYTE
||
492 srcType
== GL_BYTE
||
493 srcType
== GL_UNSIGNED_SHORT
||
494 srcType
== GL_SHORT
||
495 srcType
== GL_UNSIGNED_INT
||
497 srcType
== GL_UNSIGNED_INT_24_8_EXT
||
498 srcType
== GL_HALF_FLOAT_ARB
||
499 srcType
== GL_HALF_FLOAT_OES
||
500 srcType
== GL_FLOAT
||
501 srcType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
503 assert(dstType
== GL_UNSIGNED_BYTE
||
504 dstType
== GL_UNSIGNED_SHORT
||
505 dstType
== GL_UNSIGNED_INT
||
506 dstType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
508 /* only shift and offset apply to stencil */
509 transferOps
&= IMAGE_SHIFT_OFFSET_BIT
;
512 * Try simple cases first
514 if (transferOps
== 0 &&
515 !ctx
->Pixel
.MapStencilFlag
&&
516 srcType
== GL_UNSIGNED_BYTE
&&
517 dstType
== GL_UNSIGNED_BYTE
) {
518 memcpy(dest
, source
, n
* sizeof(GLubyte
));
520 else if (transferOps
== 0 &&
521 !ctx
->Pixel
.MapStencilFlag
&&
522 srcType
== GL_UNSIGNED_INT
&&
523 dstType
== GL_UNSIGNED_INT
&&
524 !srcPacking
->SwapBytes
) {
525 memcpy(dest
, source
, n
* sizeof(GLuint
));
531 GLuint
*indexes
= malloc(n
* sizeof(GLuint
));
534 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "stencil unpacking");
538 extract_uint_indexes(n
, indexes
, GL_STENCIL_INDEX
, srcType
, source
,
541 if (transferOps
& IMAGE_SHIFT_OFFSET_BIT
) {
542 /* shift and offset indexes */
543 _mesa_shift_and_offset_ci(ctx
, n
, indexes
);
546 if (ctx
->Pixel
.MapStencilFlag
) {
547 /* Apply stencil lookup table */
548 const GLuint mask
= ctx
->PixelMaps
.StoS
.Size
- 1;
550 for (i
= 0; i
< n
; i
++) {
551 indexes
[i
] = (GLuint
)ctx
->PixelMaps
.StoS
.Map
[ indexes
[i
] & mask
];
555 /* convert to dest type */
557 case GL_UNSIGNED_BYTE
:
559 GLubyte
*dst
= (GLubyte
*) dest
;
561 for (i
= 0; i
< n
; i
++) {
562 dst
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
566 case GL_UNSIGNED_SHORT
:
568 GLuint
*dst
= (GLuint
*) dest
;
570 for (i
= 0; i
< n
; i
++) {
571 dst
[i
] = (GLushort
) (indexes
[i
] & 0xffff);
575 case GL_UNSIGNED_INT
:
576 memcpy(dest
, indexes
, n
* sizeof(GLuint
));
578 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
580 GLuint
*dst
= (GLuint
*) dest
;
582 for (i
= 0; i
< n
; i
++) {
583 dst
[i
*2+1] = indexes
[i
] & 0xff; /* lower 8 bits */
588 unreachable("bad dstType in _mesa_unpack_stencil_span");
597 _mesa_pack_stencil_span( struct gl_context
*ctx
, GLuint n
,
598 GLenum dstType
, GLvoid
*dest
, const GLubyte
*source
,
599 const struct gl_pixelstore_attrib
*dstPacking
)
601 GLubyte
*stencil
= malloc(n
* sizeof(GLubyte
));
604 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "stencil packing");
608 if (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
||
609 ctx
->Pixel
.MapStencilFlag
) {
610 /* make a copy of input */
611 memcpy(stencil
, source
, n
* sizeof(GLubyte
));
612 _mesa_apply_stencil_transfer_ops(ctx
, n
, stencil
);
617 case GL_UNSIGNED_BYTE
:
618 memcpy(dest
, source
, n
);
622 GLbyte
*dst
= (GLbyte
*) dest
;
625 dst
[i
] = (GLbyte
) (source
[i
] & 0x7f);
629 case GL_UNSIGNED_SHORT
:
631 GLushort
*dst
= (GLushort
*) dest
;
634 dst
[i
] = (GLushort
) source
[i
];
636 if (dstPacking
->SwapBytes
) {
637 _mesa_swap2( (GLushort
*) dst
, n
);
643 GLshort
*dst
= (GLshort
*) dest
;
646 dst
[i
] = (GLshort
) source
[i
];
648 if (dstPacking
->SwapBytes
) {
649 _mesa_swap2( (GLushort
*) dst
, n
);
653 case GL_UNSIGNED_INT
:
655 GLuint
*dst
= (GLuint
*) dest
;
658 dst
[i
] = (GLuint
) source
[i
];
660 if (dstPacking
->SwapBytes
) {
661 _mesa_swap4( (GLuint
*) dst
, n
);
667 GLint
*dst
= (GLint
*) dest
;
670 dst
[i
] = (GLint
) source
[i
];
672 if (dstPacking
->SwapBytes
) {
673 _mesa_swap4( (GLuint
*) dst
, n
);
679 GLfloat
*dst
= (GLfloat
*) dest
;
682 dst
[i
] = (GLfloat
) source
[i
];
684 if (dstPacking
->SwapBytes
) {
685 _mesa_swap4( (GLuint
*) dst
, n
);
689 case GL_HALF_FLOAT_ARB
:
690 case GL_HALF_FLOAT_OES
:
692 GLhalfARB
*dst
= (GLhalfARB
*) dest
;
695 dst
[i
] = _mesa_float_to_half( (float) source
[i
] );
697 if (dstPacking
->SwapBytes
) {
698 _mesa_swap2( (GLushort
*) dst
, n
);
703 if (dstPacking
->LsbFirst
) {
704 GLubyte
*dst
= (GLubyte
*) dest
;
707 for (i
= 0; i
< n
; i
++) {
710 *dst
|= ((source
[i
] != 0) << shift
);
719 GLubyte
*dst
= (GLubyte
*) dest
;
722 for (i
= 0; i
< n
; i
++) {
725 *dst
|= ((source
[i
] != 0) << shift
);
735 unreachable("bad type in _mesa_pack_index_span");
741 #define DEPTH_VALUES(GLTYPE, GLTYPE2FLOAT) \
744 const GLTYPE *src = (const GLTYPE *)source; \
745 for (i = 0; i < n; i++) { \
746 GLTYPE value = src[i]; \
747 if (srcPacking->SwapBytes) { \
748 if (sizeof(GLTYPE) == 2) { \
750 } else if (sizeof(GLTYPE) == 4) { \
754 depthValues[i] = GLTYPE2FLOAT(value); \
760 * Unpack a row of depth/z values from memory, returning GLushort, GLuint
762 * The glPixelTransfer (scale/bias) params will be applied.
764 * \param dstType one of GL_UNSIGNED_SHORT, GL_UNSIGNED_INT, GL_FLOAT
765 * \param depthMax max value for returned GLushort or GLuint values
766 * (ignored for GLfloat).
769 _mesa_unpack_depth_span( struct gl_context
*ctx
, GLuint n
,
770 GLenum dstType
, GLvoid
*dest
, GLuint depthMax
,
771 GLenum srcType
, const GLvoid
*source
,
772 const struct gl_pixelstore_attrib
*srcPacking
)
774 GLfloat
*depthTemp
= NULL
, *depthValues
;
775 GLboolean needClamp
= GL_FALSE
;
777 /* Look for special cases first.
778 * Not only are these faster, they're less prone to numeric conversion
779 * problems. Otherwise, converting from an int type to a float then
780 * back to an int type can introduce errors that will show up as
781 * artifacts in things like depth peeling which uses glCopyTexImage.
783 if (ctx
->Pixel
.DepthScale
== 1.0F
&& ctx
->Pixel
.DepthBias
== 0.0F
) {
784 if (srcType
== GL_UNSIGNED_INT
&& dstType
== GL_UNSIGNED_SHORT
) {
785 const GLuint
*src
= (const GLuint
*) source
;
786 GLushort
*dst
= (GLushort
*) dest
;
788 for (i
= 0; i
< n
; i
++) {
789 dst
[i
] = src
[i
] >> 16;
793 if (srcType
== GL_UNSIGNED_SHORT
794 && dstType
== GL_UNSIGNED_INT
795 && depthMax
== 0xffffffff) {
796 const GLushort
*src
= (const GLushort
*) source
;
797 GLuint
*dst
= (GLuint
*) dest
;
799 for (i
= 0; i
< n
; i
++) {
800 dst
[i
] = src
[i
] | (src
[i
] << 16);
804 if (srcType
== GL_UNSIGNED_INT_24_8
805 && dstType
== GL_UNSIGNED_INT
806 && depthMax
== 0xffffff) {
807 const GLuint
*src
= (const GLuint
*) source
;
808 GLuint
*dst
= (GLuint
*) dest
;
810 for (i
= 0; i
< n
; i
++) {
811 dst
[i
] = src
[i
] >> 8;
815 /* XXX may want to add additional cases here someday */
818 /* general case path follows */
820 if (dstType
== GL_FLOAT
) {
821 depthValues
= (GLfloat
*) dest
;
824 depthTemp
= malloc(n
* sizeof(GLfloat
));
826 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
830 depthValues
= depthTemp
;
833 /* Convert incoming values to GLfloat. Some conversions will require
838 DEPTH_VALUES(GLbyte
, BYTE_TO_FLOATZ
);
841 case GL_UNSIGNED_BYTE
:
842 DEPTH_VALUES(GLubyte
, UBYTE_TO_FLOAT
);
845 DEPTH_VALUES(GLshort
, SHORT_TO_FLOATZ
);
848 case GL_UNSIGNED_SHORT
:
849 DEPTH_VALUES(GLushort
, USHORT_TO_FLOAT
);
852 DEPTH_VALUES(GLint
, INT_TO_FLOAT
);
855 case GL_UNSIGNED_INT
:
856 DEPTH_VALUES(GLuint
, UINT_TO_FLOAT
);
858 case GL_UNSIGNED_INT_24_8_EXT
: /* GL_EXT_packed_depth_stencil */
859 if (dstType
== GL_UNSIGNED_INT_24_8_EXT
&&
860 depthMax
== 0xffffff &&
861 ctx
->Pixel
.DepthScale
== 1.0F
&&
862 ctx
->Pixel
.DepthBias
== 0.0F
) {
863 const GLuint
*src
= (const GLuint
*) source
;
864 GLuint
*zValues
= (GLuint
*) dest
;
866 for (i
= 0; i
< n
; i
++) {
867 GLuint value
= src
[i
];
868 if (srcPacking
->SwapBytes
) {
871 zValues
[i
] = value
& 0xffffff00;
877 const GLuint
*src
= (const GLuint
*) source
;
878 const GLfloat scale
= 1.0f
/ 0xffffff;
880 for (i
= 0; i
< n
; i
++) {
881 GLuint value
= src
[i
];
882 if (srcPacking
->SwapBytes
) {
885 depthValues
[i
] = (value
>> 8) * scale
;
889 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
892 const GLfloat
*src
= (const GLfloat
*)source
;
893 for (i
= 0; i
< n
; i
++) {
894 GLfloat value
= src
[i
* 2];
895 if (srcPacking
->SwapBytes
) {
898 depthValues
[i
] = value
;
904 DEPTH_VALUES(GLfloat
, 1*);
907 case GL_HALF_FLOAT_ARB
:
908 case GL_HALF_FLOAT_OES
:
911 const GLhalfARB
*src
= (const GLhalfARB
*) source
;
912 for (i
= 0; i
< n
; i
++) {
913 GLhalfARB value
= src
[i
];
914 if (srcPacking
->SwapBytes
) {
917 depthValues
[i
] = _mesa_half_to_float(value
);
923 _mesa_problem(NULL
, "bad type in _mesa_unpack_depth_span()");
928 /* apply depth scale and bias */
930 const GLfloat scale
= ctx
->Pixel
.DepthScale
;
931 const GLfloat bias
= ctx
->Pixel
.DepthBias
;
932 if (scale
!= 1.0F
|| bias
!= 0.0F
) {
934 for (i
= 0; i
< n
; i
++) {
935 depthValues
[i
] = depthValues
[i
] * scale
+ bias
;
941 /* clamp to [0, 1] */
944 for (i
= 0; i
< n
; i
++) {
945 depthValues
[i
] = CLAMP(depthValues
[i
], 0.0F
, 1.0F
);
950 * Convert values to dstType
952 if (dstType
== GL_UNSIGNED_INT
) {
953 GLuint
*zValues
= (GLuint
*) dest
;
955 if (depthMax
<= 0xffffff) {
956 /* no overflow worries */
957 for (i
= 0; i
< n
; i
++) {
958 zValues
[i
] = (GLuint
) (depthValues
[i
] * (GLfloat
) depthMax
);
962 /* need to use double precision to prevent overflow problems */
963 for (i
= 0; i
< n
; i
++) {
964 GLdouble z
= depthValues
[i
] * (GLdouble
) depthMax
;
965 if (z
>= (GLdouble
) 0xffffffff)
966 zValues
[i
] = 0xffffffff;
968 zValues
[i
] = (GLuint
) z
;
972 else if (dstType
== GL_UNSIGNED_SHORT
) {
973 GLushort
*zValues
= (GLushort
*) dest
;
975 assert(depthMax
<= 0xffff);
976 for (i
= 0; i
< n
; i
++) {
977 zValues
[i
] = (GLushort
) (depthValues
[i
] * (GLfloat
) depthMax
);
980 else if (dstType
== GL_FLOAT
) {
981 /* Nothing to do. depthValues is pointing to dest. */
983 else if (dstType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
) {
984 GLfloat
*zValues
= (GLfloat
*) dest
;
986 for (i
= 0; i
< n
; i
++) {
987 zValues
[i
*2] = depthValues
[i
];
999 * Pack an array of depth values. The values are floats in [0,1].
1002 _mesa_pack_depth_span( struct gl_context
*ctx
, GLuint n
, GLvoid
*dest
,
1003 GLenum dstType
, const GLfloat
*depthSpan
,
1004 const struct gl_pixelstore_attrib
*dstPacking
)
1006 GLfloat
*depthCopy
= malloc(n
* sizeof(GLfloat
));
1008 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
1012 if (ctx
->Pixel
.DepthScale
!= 1.0F
|| ctx
->Pixel
.DepthBias
!= 0.0F
) {
1013 memcpy(depthCopy
, depthSpan
, n
* sizeof(GLfloat
));
1014 _mesa_scale_and_bias_depth(ctx
, n
, depthCopy
);
1015 depthSpan
= depthCopy
;
1019 case GL_UNSIGNED_BYTE
:
1021 GLubyte
*dst
= (GLubyte
*) dest
;
1023 for (i
= 0; i
< n
; i
++) {
1024 dst
[i
] = FLOAT_TO_UBYTE( depthSpan
[i
] );
1030 GLbyte
*dst
= (GLbyte
*) dest
;
1032 for (i
= 0; i
< n
; i
++) {
1033 dst
[i
] = FLOAT_TO_BYTE( depthSpan
[i
] );
1037 case GL_UNSIGNED_SHORT
:
1039 GLushort
*dst
= (GLushort
*) dest
;
1041 for (i
= 0; i
< n
; i
++) {
1042 CLAMPED_FLOAT_TO_USHORT(dst
[i
], depthSpan
[i
]);
1044 if (dstPacking
->SwapBytes
) {
1045 _mesa_swap2( (GLushort
*) dst
, n
);
1051 GLshort
*dst
= (GLshort
*) dest
;
1053 for (i
= 0; i
< n
; i
++) {
1054 dst
[i
] = FLOAT_TO_SHORT( depthSpan
[i
] );
1056 if (dstPacking
->SwapBytes
) {
1057 _mesa_swap2( (GLushort
*) dst
, n
);
1061 case GL_UNSIGNED_INT_24_8
:
1063 const GLdouble scale
= (GLdouble
) 0xffffff;
1064 GLuint
*dst
= (GLuint
*) dest
;
1066 for (i
= 0; i
< n
; i
++) {
1067 GLuint z
= (GLuint
) (depthSpan
[i
] * scale
);
1068 assert(z
<= 0xffffff);
1071 if (dstPacking
->SwapBytes
) {
1072 _mesa_swap4( (GLuint
*) dst
, n
);
1076 case GL_UNSIGNED_INT
:
1078 GLuint
*dst
= (GLuint
*) dest
;
1080 for (i
= 0; i
< n
; i
++) {
1081 dst
[i
] = FLOAT_TO_UINT( depthSpan
[i
] );
1083 if (dstPacking
->SwapBytes
) {
1084 _mesa_swap4( (GLuint
*) dst
, n
);
1090 GLint
*dst
= (GLint
*) dest
;
1092 for (i
= 0; i
< n
; i
++) {
1093 dst
[i
] = FLOAT_TO_INT( depthSpan
[i
] );
1095 if (dstPacking
->SwapBytes
) {
1096 _mesa_swap4( (GLuint
*) dst
, n
);
1102 GLfloat
*dst
= (GLfloat
*) dest
;
1104 for (i
= 0; i
< n
; i
++) {
1105 dst
[i
] = depthSpan
[i
];
1107 if (dstPacking
->SwapBytes
) {
1108 _mesa_swap4( (GLuint
*) dst
, n
);
1112 case GL_HALF_FLOAT_ARB
:
1113 case GL_HALF_FLOAT_OES
:
1115 GLhalfARB
*dst
= (GLhalfARB
*) dest
;
1117 for (i
= 0; i
< n
; i
++) {
1118 dst
[i
] = _mesa_float_to_half(depthSpan
[i
]);
1120 if (dstPacking
->SwapBytes
) {
1121 _mesa_swap2( (GLushort
*) dst
, n
);
1126 unreachable("bad type in _mesa_pack_depth_span()");
1135 * Pack depth and stencil values as GL_DEPTH_STENCIL (GL_UNSIGNED_INT_24_8 etc)
1138 _mesa_pack_depth_stencil_span(struct gl_context
*ctx
,GLuint n
,
1139 GLenum dstType
, GLuint
*dest
,
1140 const GLfloat
*depthVals
,
1141 const GLubyte
*stencilVals
,
1142 const struct gl_pixelstore_attrib
*dstPacking
)
1144 GLfloat
*depthCopy
= malloc(n
* sizeof(GLfloat
));
1145 GLubyte
*stencilCopy
= malloc(n
* sizeof(GLubyte
));
1148 if (!depthCopy
|| !stencilCopy
) {
1149 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
1155 if (ctx
->Pixel
.DepthScale
!= 1.0F
|| ctx
->Pixel
.DepthBias
!= 0.0F
) {
1156 memcpy(depthCopy
, depthVals
, n
* sizeof(GLfloat
));
1157 _mesa_scale_and_bias_depth(ctx
, n
, depthCopy
);
1158 depthVals
= depthCopy
;
1161 if (ctx
->Pixel
.IndexShift
||
1162 ctx
->Pixel
.IndexOffset
||
1163 ctx
->Pixel
.MapStencilFlag
) {
1164 memcpy(stencilCopy
, stencilVals
, n
* sizeof(GLubyte
));
1165 _mesa_apply_stencil_transfer_ops(ctx
, n
, stencilCopy
);
1166 stencilVals
= stencilCopy
;
1170 case GL_UNSIGNED_INT_24_8
:
1171 for (i
= 0; i
< n
; i
++) {
1172 GLuint z
= (GLuint
) (depthVals
[i
] * 0xffffff);
1173 dest
[i
] = (z
<< 8) | (stencilVals
[i
] & 0xff);
1176 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
1177 for (i
= 0; i
< n
; i
++) {
1178 ((GLfloat
*)dest
)[i
*2] = depthVals
[i
];
1179 dest
[i
*2+1] = stencilVals
[i
] & 0xff;
1184 if (dstPacking
->SwapBytes
) {
1185 _mesa_swap4(dest
, n
);
1195 * Unpack image data. Apply byte swapping, byte flipping (bitmap).
1196 * Return all image data in a contiguous block. This is used when we
1197 * compile glDrawPixels, glTexImage, etc into a display list. We
1198 * need a copy of the data in a standard format.
1201 _mesa_unpack_image( GLuint dimensions
,
1202 GLsizei width
, GLsizei height
, GLsizei depth
,
1203 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1204 const struct gl_pixelstore_attrib
*unpack
)
1206 GLint bytesPerRow
, compsPerRow
;
1207 GLboolean flipBytes
, swap2
, swap4
;
1210 return NULL
; /* not necessarily an error */
1212 if (width
<= 0 || height
<= 0 || depth
<= 0)
1213 return NULL
; /* generate error later */
1215 if (type
== GL_BITMAP
) {
1216 bytesPerRow
= (width
+ 7) >> 3;
1217 flipBytes
= unpack
->LsbFirst
;
1218 swap2
= swap4
= GL_FALSE
;
1222 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1223 GLint components
= _mesa_components_in_format(format
);
1226 if (_mesa_type_is_packed(type
))
1229 if (bytesPerPixel
<= 0 || components
<= 0)
1230 return NULL
; /* bad format or type. generate error later */
1231 bytesPerRow
= bytesPerPixel
* width
;
1232 bytesPerComp
= bytesPerPixel
/ components
;
1233 flipBytes
= GL_FALSE
;
1234 swap2
= (bytesPerComp
== 2) && unpack
->SwapBytes
;
1235 swap4
= (bytesPerComp
== 4) && unpack
->SwapBytes
;
1236 compsPerRow
= components
* width
;
1237 assert(compsPerRow
>= width
);
1242 = malloc(bytesPerRow
* height
* depth
);
1246 return NULL
; /* generate GL_OUT_OF_MEMORY later */
1249 for (img
= 0; img
< depth
; img
++) {
1250 for (row
= 0; row
< height
; row
++) {
1251 const GLvoid
*src
= _mesa_image_address(dimensions
, unpack
, pixels
,
1252 width
, height
, format
, type
, img
, row
, 0);
1254 if ((type
== GL_BITMAP
) && (unpack
->SkipPixels
& 0x7)) {
1256 flipBytes
= GL_FALSE
;
1257 if (unpack
->LsbFirst
) {
1258 GLubyte srcMask
= 1 << (unpack
->SkipPixels
& 0x7);
1259 GLubyte dstMask
= 128;
1260 const GLubyte
*s
= src
;
1263 for (i
= 0; i
< width
; i
++) {
1267 if (srcMask
== 128) {
1272 srcMask
= srcMask
<< 1;
1280 dstMask
= dstMask
>> 1;
1285 GLubyte srcMask
= 128 >> (unpack
->SkipPixels
& 0x7);
1286 GLubyte dstMask
= 128;
1287 const GLubyte
*s
= src
;
1290 for (i
= 0; i
< width
; i
++) {
1299 srcMask
= srcMask
>> 1;
1307 dstMask
= dstMask
>> 1;
1313 memcpy(dst
, src
, bytesPerRow
);
1316 /* byte flipping/swapping */
1318 flip_bytes((GLubyte
*) dst
, bytesPerRow
);
1321 _mesa_swap2((GLushort
*) dst
, compsPerRow
);
1324 _mesa_swap4((GLuint
*) dst
, compsPerRow
);
1334 _mesa_pack_luminance_from_rgba_float(GLuint n
, GLfloat rgba
[][4],
1335 GLvoid
*dstAddr
, GLenum dst_format
,
1336 GLbitfield transferOps
)
1339 GLfloat
*dst
= (GLfloat
*) dstAddr
;
1341 switch (dst_format
) {
1343 if (transferOps
& IMAGE_CLAMP_BIT
) {
1344 for (i
= 0; i
< n
; i
++) {
1345 GLfloat sum
= rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
1346 dst
[i
] = CLAMP(sum
, 0.0F
, 1.0F
);
1349 for (i
= 0; i
< n
; i
++) {
1350 dst
[i
] = rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
1354 case GL_LUMINANCE_ALPHA
:
1355 if (transferOps
& IMAGE_CLAMP_BIT
) {
1356 for (i
= 0; i
< n
; i
++) {
1357 GLfloat sum
= rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
1358 dst
[2*i
] = CLAMP(sum
, 0.0F
, 1.0F
);
1359 dst
[2*i
+1] = rgba
[i
][ACOMP
];
1362 for (i
= 0; i
< n
; i
++) {
1363 dst
[2*i
] = rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
1364 dst
[2*i
+1] = rgba
[i
][ACOMP
];
1369 assert(!"Unsupported format");
1374 clamp_sint64_to_sint32(int64_t src
)
1376 return CLAMP(src
, INT32_MIN
, INT32_MAX
);
1380 clamp_sint64_to_uint32(int64_t src
)
1382 return CLAMP(src
, 0, UINT32_MAX
);
1386 clamp_uint64_to_uint32(uint64_t src
)
1388 return MIN2(src
, UINT32_MAX
);
1392 clamp_uint64_to_sint32(uint64_t src
)
1394 return MIN2(src
, INT32_MAX
);
1398 convert_integer_luminance64(int64_t src64
, int bits
,
1399 bool dst_is_signed
, bool src_is_signed
)
1403 /* Clamp Luminance value from 64-bit to 32-bit. Consider if we need
1404 * any signed<->unsigned conversion too.
1406 if (src_is_signed
&& dst_is_signed
)
1407 src32
= clamp_sint64_to_sint32(src64
);
1408 else if (src_is_signed
&& !dst_is_signed
)
1409 src32
= clamp_sint64_to_uint32(src64
);
1410 else if (!src_is_signed
&& dst_is_signed
)
1411 src32
= clamp_uint64_to_sint32(src64
);
1413 src32
= clamp_uint64_to_uint32(src64
);
1415 /* If the dst type is < 32-bit, we need an extra clamp */
1420 return _mesa_signed_to_signed(src32
, bits
);
1422 return _mesa_unsigned_to_unsigned(src32
, bits
);
1427 convert_integer(int32_t src
, int bits
, bool dst_is_signed
, bool src_is_signed
)
1429 if (src_is_signed
&& dst_is_signed
)
1430 return _mesa_signed_to_signed(src
, bits
);
1431 else if (src_is_signed
&& !dst_is_signed
)
1432 return _mesa_signed_to_unsigned(src
, bits
);
1433 else if (!src_is_signed
&& dst_is_signed
)
1434 return _mesa_unsigned_to_signed(src
, bits
);
1436 return _mesa_unsigned_to_unsigned(src
, bits
);
1440 _mesa_pack_luminance_from_rgba_integer(GLuint n
,
1441 GLuint rgba
[][4], bool rgba_is_signed
,
1448 int32_t lum32
, alpha
;
1452 assert(dst_format
== GL_LUMINANCE_INTEGER_EXT
||
1453 dst_format
== GL_LUMINANCE_ALPHA_INTEGER_EXT
);
1455 /* We first compute luminance values as a 64-bit addition of the
1456 * 32-bit R,G,B components, then we clamp the result to the dst type size.
1458 * Notice that this operation involves casting the 32-bit R,G,B components
1459 * to 64-bit before the addition. Since rgba is defined as a GLuint array
1460 * we need to be careful when rgba packs signed data and make sure
1461 * that we cast to a 32-bit signed integer values before casting them to
1462 * 64-bit signed integers.
1464 dst_is_signed
= (dst_type
== GL_BYTE
|| dst_type
== GL_SHORT
||
1465 dst_type
== GL_INT
);
1467 dst_bits
= _mesa_sizeof_type(dst_type
) * 8;
1468 assert(dst_bits
> 0);
1470 switch (dst_format
) {
1471 case GL_LUMINANCE_INTEGER_EXT
:
1472 for (i
= 0; i
< n
; i
++) {
1473 if (!rgba_is_signed
) {
1474 lum64
= (uint64_t) rgba
[i
][RCOMP
] +
1475 (uint64_t) rgba
[i
][GCOMP
] +
1476 (uint64_t) rgba
[i
][BCOMP
];
1478 lum64
= (int64_t) ((int32_t) rgba
[i
][RCOMP
]) +
1479 (int64_t) ((int32_t) rgba
[i
][GCOMP
]) +
1480 (int64_t) ((int32_t) rgba
[i
][BCOMP
]);
1482 lum32
= convert_integer_luminance64(lum64
, dst_bits
,
1483 dst_is_signed
, rgba_is_signed
);
1486 case GL_UNSIGNED_BYTE
: {
1487 GLbyte
*dst
= (GLbyte
*) dstAddr
;
1492 case GL_UNSIGNED_SHORT
: {
1493 GLshort
*dst
= (GLshort
*) dstAddr
;
1498 case GL_UNSIGNED_INT
: {
1499 GLint
*dst
= (GLint
*) dstAddr
;
1506 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
1507 for (i
= 0; i
< n
; i
++) {
1508 if (!rgba_is_signed
) {
1509 lum64
= (uint64_t) rgba
[i
][RCOMP
] +
1510 (uint64_t) rgba
[i
][GCOMP
] +
1511 (uint64_t) rgba
[i
][BCOMP
];
1513 lum64
= (int64_t) ((int32_t) rgba
[i
][RCOMP
]) +
1514 (int64_t) ((int32_t) rgba
[i
][GCOMP
]) +
1515 (int64_t) ((int32_t) rgba
[i
][BCOMP
]);
1517 lum32
= convert_integer_luminance64(lum64
, dst_bits
,
1518 dst_is_signed
, rgba_is_signed
);
1519 alpha
= convert_integer(rgba
[i
][ACOMP
], dst_bits
,
1520 dst_is_signed
, rgba_is_signed
);
1523 case GL_UNSIGNED_BYTE
: {
1524 GLbyte
*dst
= (GLbyte
*) dstAddr
;
1530 case GL_UNSIGNED_SHORT
: {
1531 GLshort
*dst
= (GLshort
*) dstAddr
;
1537 case GL_UNSIGNED_INT
: {
1538 GLint
*dst
= (GLint
*) dstAddr
;
1550 _mesa_unpack_color_index_to_rgba_float(struct gl_context
*ctx
, GLuint dims
,
1551 const void *src
, GLenum srcFormat
, GLenum srcType
,
1552 int srcWidth
, int srcHeight
, int srcDepth
,
1553 const struct gl_pixelstore_attrib
*srcPacking
,
1554 GLbitfield transferOps
)
1558 GLfloat
*rgba
, *dstPtr
;
1560 count
= srcWidth
* srcHeight
;
1561 indexes
= malloc(count
* sizeof(GLuint
));
1563 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
1567 rgba
= malloc(4 * count
* srcDepth
* sizeof(GLfloat
));
1570 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
1574 /* Convert indexes to RGBA float */
1576 for (img
= 0; img
< srcDepth
; img
++) {
1577 const GLubyte
*srcPtr
=
1578 (const GLubyte
*) _mesa_image_address(dims
, srcPacking
, src
,
1579 srcWidth
, srcHeight
,
1583 extract_uint_indexes(count
, indexes
, srcFormat
, srcType
, srcPtr
, srcPacking
);
1585 if (transferOps
& IMAGE_SHIFT_OFFSET_BIT
)
1586 _mesa_shift_and_offset_ci(ctx
, count
, indexes
);
1588 _mesa_map_ci_to_rgba(ctx
, count
, indexes
, (float (*)[4])dstPtr
);
1590 /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
1591 * with color indexes.
1593 transferOps
&= ~(IMAGE_SCALE_BIAS_BIT
| IMAGE_MAP_COLOR_BIT
);
1594 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, count
, (float (*)[4])dstPtr
);
1596 dstPtr
+= srcHeight
* srcWidth
* 4;
1605 _mesa_unpack_color_index_to_rgba_ubyte(struct gl_context
*ctx
, GLuint dims
,
1606 const void *src
, GLenum srcFormat
, GLenum srcType
,
1607 int srcWidth
, int srcHeight
, int srcDepth
,
1608 const struct gl_pixelstore_attrib
*srcPacking
,
1609 GLbitfield transferOps
)
1615 transferOps
|= IMAGE_CLAMP_BIT
;
1616 rgba
= _mesa_unpack_color_index_to_rgba_float(ctx
, dims
,
1617 src
, srcFormat
, srcType
,
1618 srcWidth
, srcHeight
, srcDepth
,
1619 srcPacking
, transferOps
);
1621 count
= srcWidth
* srcHeight
* srcDepth
;
1622 dst
= malloc(count
* 4 * sizeof(GLubyte
));
1623 for (i
= 0; i
< count
; i
++) {
1624 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 0], rgba
[i
* 4 + 0]);
1625 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 1], rgba
[i
* 4 + 1]);
1626 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 2], rgba
[i
* 4 + 2]);
1627 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 3], rgba
[i
* 4 + 3]);