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 )
52 #include "pixeltransfer.h"
54 #include "glformats.h"
55 #include "format_utils.h"
56 #include "format_pack.h"
60 * Flip the 8 bits in each byte of the given array.
63 * \param n number of bytes.
65 * \todo try this trick to flip bytes someday:
67 * v = ((v & 0x55555555) << 1) | ((v >> 1) & 0x55555555);
68 * v = ((v & 0x33333333) << 2) | ((v >> 2) & 0x33333333);
69 * v = ((v & 0x0f0f0f0f) << 4) | ((v >> 4) & 0x0f0f0f0f);
73 flip_bytes( GLubyte
*p
, GLuint n
)
76 for (i
= 0; i
< n
; i
++) {
77 b
= (GLuint
) p
[i
]; /* words are often faster than bytes */
78 a
= ((b
& 0x01) << 7) |
93 * Unpack a 32x32 pixel polygon stipple from user memory using the
94 * current pixel unpack settings.
97 _mesa_unpack_polygon_stipple( const GLubyte
*pattern
, GLuint dest
[32],
98 const struct gl_pixelstore_attrib
*unpacking
)
100 GLubyte
*ptrn
= (GLubyte
*) _mesa_unpack_image(2, 32, 32, 1, GL_COLOR_INDEX
,
101 GL_BITMAP
, pattern
, unpacking
);
103 /* Convert pattern from GLubytes to GLuints and handle big/little
108 for (i
= 0; i
< 32; i
++) {
109 dest
[i
] = (p
[0] << 24)
121 * Pack polygon stipple into user memory given current pixel packing
125 _mesa_pack_polygon_stipple( const GLuint pattern
[32], GLubyte
*dest
,
126 const struct gl_pixelstore_attrib
*packing
)
128 /* Convert pattern from GLuints to GLubytes to handle big/little
129 * endian differences.
133 for (i
= 0; i
< 32; i
++) {
134 ptrn
[i
* 4 + 0] = (GLubyte
) ((pattern
[i
] >> 24) & 0xff);
135 ptrn
[i
* 4 + 1] = (GLubyte
) ((pattern
[i
] >> 16) & 0xff);
136 ptrn
[i
* 4 + 2] = (GLubyte
) ((pattern
[i
] >> 8 ) & 0xff);
137 ptrn
[i
* 4 + 3] = (GLubyte
) ((pattern
[i
] ) & 0xff);
140 _mesa_pack_bitmap(32, 32, ptrn
, dest
, packing
);
148 _mesa_pack_bitmap( GLint width
, GLint height
, const GLubyte
*source
,
149 GLubyte
*dest
, const struct gl_pixelstore_attrib
*packing
)
151 GLint row
, width_in_bytes
;
157 width_in_bytes
= DIV_ROUND_UP( width
, 8 );
159 for (row
= 0; row
< height
; row
++) {
160 GLubyte
*dst
= (GLubyte
*) _mesa_image_address2d(packing
, dest
,
161 width
, height
, GL_COLOR_INDEX
, GL_BITMAP
, row
, 0);
165 if ((packing
->SkipPixels
& 7) == 0) {
166 memcpy( dst
, src
, width_in_bytes
);
167 if (packing
->LsbFirst
) {
168 flip_bytes( dst
, width_in_bytes
);
172 /* handling SkipPixels is a bit tricky (no pun intended!) */
174 if (packing
->LsbFirst
) {
175 GLubyte srcMask
= 128;
176 GLubyte dstMask
= 1 << (packing
->SkipPixels
& 0x7);
177 const GLubyte
*s
= src
;
180 for (i
= 0; i
< width
; i
++) {
189 srcMask
= srcMask
>> 1;
191 if (dstMask
== 128) {
197 dstMask
= dstMask
<< 1;
202 GLubyte srcMask
= 128;
203 GLubyte dstMask
= 128 >> (packing
->SkipPixels
& 0x7);
204 const GLubyte
*s
= src
;
207 for (i
= 0; i
< width
; i
++) {
216 srcMask
= srcMask
>> 1;
224 dstMask
= dstMask
>> 1;
229 src
+= width_in_bytes
;
234 #define SWAP2BYTE(VALUE) \
236 GLubyte *bytes = (GLubyte *) &(VALUE); \
237 GLubyte tmp = bytes[0]; \
238 bytes[0] = bytes[1]; \
242 #define SWAP4BYTE(VALUE) \
244 GLubyte *bytes = (GLubyte *) &(VALUE); \
245 GLubyte tmp = bytes[0]; \
246 bytes[0] = bytes[3]; \
249 bytes[1] = bytes[2]; \
255 extract_uint_indexes(GLuint n
, GLuint indexes
[],
256 GLenum srcFormat
, GLenum srcType
, const GLvoid
*src
,
257 const struct gl_pixelstore_attrib
*unpack
)
259 assert(srcFormat
== GL_COLOR_INDEX
|| srcFormat
== GL_STENCIL_INDEX
);
261 assert(srcType
== GL_BITMAP
||
262 srcType
== GL_UNSIGNED_BYTE
||
263 srcType
== GL_BYTE
||
264 srcType
== GL_UNSIGNED_SHORT
||
265 srcType
== GL_SHORT
||
266 srcType
== GL_UNSIGNED_INT
||
268 srcType
== GL_UNSIGNED_INT_24_8_EXT
||
269 srcType
== GL_HALF_FLOAT_ARB
||
270 srcType
== GL_HALF_FLOAT_OES
||
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
:
410 case GL_HALF_FLOAT_OES
:
413 const GLhalfARB
*s
= (const GLhalfARB
*) src
;
414 if (unpack
->SwapBytes
) {
415 for (i
= 0; i
< n
; i
++) {
416 GLhalfARB value
= s
[i
];
418 indexes
[i
] = (GLuint
) _mesa_half_to_float(value
);
422 for (i
= 0; i
< n
; i
++)
423 indexes
[i
] = (GLuint
) _mesa_half_to_float(s
[i
]);
427 case GL_UNSIGNED_INT_24_8_EXT
:
430 const GLuint
*s
= (const GLuint
*) src
;
431 if (unpack
->SwapBytes
) {
432 for (i
= 0; i
< n
; i
++) {
435 indexes
[i
] = value
& 0xff; /* lower 8 bits */
439 for (i
= 0; i
< n
; i
++)
440 indexes
[i
] = s
[i
] & 0xff; /* lower 8 bits */
444 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
447 const GLuint
*s
= (const GLuint
*) src
;
448 if (unpack
->SwapBytes
) {
449 for (i
= 0; i
< n
; i
++) {
450 GLuint value
= s
[i
*2+1];
452 indexes
[i
] = value
& 0xff; /* lower 8 bits */
456 for (i
= 0; i
< n
; i
++)
457 indexes
[i
] = s
[i
*2+1] & 0xff; /* lower 8 bits */
463 _mesa_problem(NULL
, "bad srcType in extract_uint_indexes");
470 clamp_float_to_uint(GLfloat f
)
472 return f
< 0.0F
? 0 : _mesa_lroundevenf(f
);
477 clamp_half_to_uint(GLhalfARB h
)
479 GLfloat f
= _mesa_half_to_float(h
);
480 return f
< 0.0F
? 0 : _mesa_lroundevenf(f
);
485 * Unpack a row of stencil data from a client buffer according to
486 * the pixel unpacking parameters.
487 * This is (or will be) used by glDrawPixels
489 * Args: ctx - the context
490 * n - number of pixels
491 * dstType - destination data type
492 * dest - destination array
493 * srcType - source pixel type
494 * source - source data pointer
495 * srcPacking - pixel unpacking parameters
496 * transferOps - apply offset/bias/lookup ops?
499 _mesa_unpack_stencil_span( struct gl_context
*ctx
, GLuint n
,
500 GLenum dstType
, GLvoid
*dest
,
501 GLenum srcType
, const GLvoid
*source
,
502 const struct gl_pixelstore_attrib
*srcPacking
,
503 GLbitfield transferOps
)
505 assert(srcType
== GL_BITMAP
||
506 srcType
== GL_UNSIGNED_BYTE
||
507 srcType
== GL_BYTE
||
508 srcType
== GL_UNSIGNED_SHORT
||
509 srcType
== GL_SHORT
||
510 srcType
== GL_UNSIGNED_INT
||
512 srcType
== GL_UNSIGNED_INT_24_8_EXT
||
513 srcType
== GL_HALF_FLOAT_ARB
||
514 srcType
== GL_HALF_FLOAT_OES
||
515 srcType
== GL_FLOAT
||
516 srcType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
518 assert(dstType
== GL_UNSIGNED_BYTE
||
519 dstType
== GL_UNSIGNED_SHORT
||
520 dstType
== GL_UNSIGNED_INT
||
521 dstType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
523 /* only shift and offset apply to stencil */
524 transferOps
&= IMAGE_SHIFT_OFFSET_BIT
;
527 * Try simple cases first
529 if (transferOps
== 0 &&
530 !ctx
->Pixel
.MapStencilFlag
&&
531 srcType
== GL_UNSIGNED_BYTE
&&
532 dstType
== GL_UNSIGNED_BYTE
) {
533 memcpy(dest
, source
, n
* sizeof(GLubyte
));
535 else if (transferOps
== 0 &&
536 !ctx
->Pixel
.MapStencilFlag
&&
537 srcType
== GL_UNSIGNED_INT
&&
538 dstType
== GL_UNSIGNED_INT
&&
539 !srcPacking
->SwapBytes
) {
540 memcpy(dest
, source
, n
* sizeof(GLuint
));
546 GLuint
*indexes
= malloc(n
* sizeof(GLuint
));
549 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "stencil unpacking");
553 extract_uint_indexes(n
, indexes
, GL_STENCIL_INDEX
, srcType
, source
,
556 if (transferOps
& IMAGE_SHIFT_OFFSET_BIT
) {
557 /* shift and offset indexes */
558 _mesa_shift_and_offset_ci(ctx
, n
, indexes
);
561 if (ctx
->Pixel
.MapStencilFlag
) {
562 /* Apply stencil lookup table */
563 const GLuint mask
= ctx
->PixelMaps
.StoS
.Size
- 1;
565 for (i
= 0; i
< n
; i
++) {
566 indexes
[i
] = (GLuint
)ctx
->PixelMaps
.StoS
.Map
[ indexes
[i
] & mask
];
570 /* convert to dest type */
572 case GL_UNSIGNED_BYTE
:
574 GLubyte
*dst
= (GLubyte
*) dest
;
576 for (i
= 0; i
< n
; i
++) {
577 dst
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
581 case GL_UNSIGNED_SHORT
:
583 GLuint
*dst
= (GLuint
*) dest
;
585 for (i
= 0; i
< n
; i
++) {
586 dst
[i
] = (GLushort
) (indexes
[i
] & 0xffff);
590 case GL_UNSIGNED_INT
:
591 memcpy(dest
, indexes
, n
* sizeof(GLuint
));
593 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
595 GLuint
*dst
= (GLuint
*) dest
;
597 for (i
= 0; i
< n
; i
++) {
598 dst
[i
*2+1] = indexes
[i
] & 0xff; /* lower 8 bits */
603 _mesa_problem(ctx
, "bad dstType in _mesa_unpack_stencil_span");
612 _mesa_pack_stencil_span( struct gl_context
*ctx
, GLuint n
,
613 GLenum dstType
, GLvoid
*dest
, const GLubyte
*source
,
614 const struct gl_pixelstore_attrib
*dstPacking
)
616 GLubyte
*stencil
= malloc(n
* sizeof(GLubyte
));
619 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "stencil packing");
623 if (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
||
624 ctx
->Pixel
.MapStencilFlag
) {
625 /* make a copy of input */
626 memcpy(stencil
, source
, n
* sizeof(GLubyte
));
627 _mesa_apply_stencil_transfer_ops(ctx
, n
, stencil
);
632 case GL_UNSIGNED_BYTE
:
633 memcpy(dest
, source
, n
);
637 GLbyte
*dst
= (GLbyte
*) dest
;
640 dst
[i
] = (GLbyte
) (source
[i
] & 0x7f);
644 case GL_UNSIGNED_SHORT
:
646 GLushort
*dst
= (GLushort
*) dest
;
649 dst
[i
] = (GLushort
) source
[i
];
651 if (dstPacking
->SwapBytes
) {
652 _mesa_swap2( (GLushort
*) dst
, n
);
658 GLshort
*dst
= (GLshort
*) dest
;
661 dst
[i
] = (GLshort
) source
[i
];
663 if (dstPacking
->SwapBytes
) {
664 _mesa_swap2( (GLushort
*) dst
, n
);
668 case GL_UNSIGNED_INT
:
670 GLuint
*dst
= (GLuint
*) dest
;
673 dst
[i
] = (GLuint
) source
[i
];
675 if (dstPacking
->SwapBytes
) {
676 _mesa_swap4( (GLuint
*) dst
, n
);
682 GLint
*dst
= (GLint
*) dest
;
685 dst
[i
] = (GLint
) source
[i
];
687 if (dstPacking
->SwapBytes
) {
688 _mesa_swap4( (GLuint
*) dst
, n
);
694 GLfloat
*dst
= (GLfloat
*) dest
;
697 dst
[i
] = (GLfloat
) source
[i
];
699 if (dstPacking
->SwapBytes
) {
700 _mesa_swap4( (GLuint
*) dst
, n
);
704 case GL_HALF_FLOAT_ARB
:
705 case GL_HALF_FLOAT_OES
:
707 GLhalfARB
*dst
= (GLhalfARB
*) dest
;
710 dst
[i
] = _mesa_float_to_half( (float) source
[i
] );
712 if (dstPacking
->SwapBytes
) {
713 _mesa_swap2( (GLushort
*) dst
, n
);
718 if (dstPacking
->LsbFirst
) {
719 GLubyte
*dst
= (GLubyte
*) dest
;
722 for (i
= 0; i
< n
; i
++) {
725 *dst
|= ((source
[i
] != 0) << shift
);
734 GLubyte
*dst
= (GLubyte
*) dest
;
737 for (i
= 0; i
< n
; i
++) {
740 *dst
|= ((source
[i
] != 0) << shift
);
750 _mesa_problem(ctx
, "bad type in _mesa_pack_index_span");
756 #define DEPTH_VALUES(GLTYPE, GLTYPE2FLOAT) \
759 const GLTYPE *src = (const GLTYPE *)source; \
760 for (i = 0; i < n; i++) { \
761 GLTYPE value = src[i]; \
762 if (srcPacking->SwapBytes) { \
763 if (sizeof(GLTYPE) == 2) { \
765 } else if (sizeof(GLTYPE) == 4) { \
769 depthValues[i] = GLTYPE2FLOAT(value); \
775 * Unpack a row of depth/z values from memory, returning GLushort, GLuint
777 * The glPixelTransfer (scale/bias) params will be applied.
779 * \param dstType one of GL_UNSIGNED_SHORT, GL_UNSIGNED_INT, GL_FLOAT
780 * \param depthMax max value for returned GLushort or GLuint values
781 * (ignored for GLfloat).
784 _mesa_unpack_depth_span( struct gl_context
*ctx
, GLuint n
,
785 GLenum dstType
, GLvoid
*dest
, GLuint depthMax
,
786 GLenum srcType
, const GLvoid
*source
,
787 const struct gl_pixelstore_attrib
*srcPacking
)
789 GLfloat
*depthTemp
= NULL
, *depthValues
;
790 GLboolean needClamp
= GL_FALSE
;
792 /* Look for special cases first.
793 * Not only are these faster, they're less prone to numeric conversion
794 * problems. Otherwise, converting from an int type to a float then
795 * back to an int type can introduce errors that will show up as
796 * artifacts in things like depth peeling which uses glCopyTexImage.
798 if (ctx
->Pixel
.DepthScale
== 1.0F
&& ctx
->Pixel
.DepthBias
== 0.0F
) {
799 if (srcType
== GL_UNSIGNED_INT
&& dstType
== GL_UNSIGNED_SHORT
) {
800 const GLuint
*src
= (const GLuint
*) source
;
801 GLushort
*dst
= (GLushort
*) dest
;
803 for (i
= 0; i
< n
; i
++) {
804 dst
[i
] = src
[i
] >> 16;
808 if (srcType
== GL_UNSIGNED_SHORT
809 && dstType
== GL_UNSIGNED_INT
810 && depthMax
== 0xffffffff) {
811 const GLushort
*src
= (const GLushort
*) source
;
812 GLuint
*dst
= (GLuint
*) dest
;
814 for (i
= 0; i
< n
; i
++) {
815 dst
[i
] = src
[i
] | (src
[i
] << 16);
819 if (srcType
== GL_UNSIGNED_INT_24_8
820 && dstType
== GL_UNSIGNED_INT
821 && depthMax
== 0xffffff) {
822 const GLuint
*src
= (const GLuint
*) source
;
823 GLuint
*dst
= (GLuint
*) dest
;
825 for (i
= 0; i
< n
; i
++) {
826 dst
[i
] = src
[i
] >> 8;
830 /* XXX may want to add additional cases here someday */
833 /* general case path follows */
835 if (dstType
== GL_FLOAT
) {
836 depthValues
= (GLfloat
*) dest
;
839 depthTemp
= malloc(n
* sizeof(GLfloat
));
841 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
845 depthValues
= depthTemp
;
848 /* Convert incoming values to GLfloat. Some conversions will require
853 DEPTH_VALUES(GLbyte
, BYTE_TO_FLOATZ
);
856 case GL_UNSIGNED_BYTE
:
857 DEPTH_VALUES(GLubyte
, UBYTE_TO_FLOAT
);
860 DEPTH_VALUES(GLshort
, SHORT_TO_FLOATZ
);
863 case GL_UNSIGNED_SHORT
:
864 DEPTH_VALUES(GLushort
, USHORT_TO_FLOAT
);
867 DEPTH_VALUES(GLint
, INT_TO_FLOAT
);
870 case GL_UNSIGNED_INT
:
871 DEPTH_VALUES(GLuint
, UINT_TO_FLOAT
);
873 case GL_UNSIGNED_INT_24_8_EXT
: /* GL_EXT_packed_depth_stencil */
874 if (dstType
== GL_UNSIGNED_INT_24_8_EXT
&&
875 depthMax
== 0xffffff &&
876 ctx
->Pixel
.DepthScale
== 1.0F
&&
877 ctx
->Pixel
.DepthBias
== 0.0F
) {
878 const GLuint
*src
= (const GLuint
*) source
;
879 GLuint
*zValues
= (GLuint
*) dest
;
881 for (i
= 0; i
< n
; i
++) {
882 GLuint value
= src
[i
];
883 if (srcPacking
->SwapBytes
) {
886 zValues
[i
] = value
& 0xffffff00;
892 const GLuint
*src
= (const GLuint
*) source
;
893 const GLfloat scale
= 1.0f
/ 0xffffff;
895 for (i
= 0; i
< n
; i
++) {
896 GLuint value
= src
[i
];
897 if (srcPacking
->SwapBytes
) {
900 depthValues
[i
] = (value
>> 8) * scale
;
904 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
907 const GLfloat
*src
= (const GLfloat
*)source
;
908 for (i
= 0; i
< n
; i
++) {
909 GLfloat value
= src
[i
* 2];
910 if (srcPacking
->SwapBytes
) {
913 depthValues
[i
] = value
;
919 DEPTH_VALUES(GLfloat
, 1*);
922 case GL_HALF_FLOAT_ARB
:
923 case GL_HALF_FLOAT_OES
:
926 const GLhalfARB
*src
= (const GLhalfARB
*) source
;
927 for (i
= 0; i
< n
; i
++) {
928 GLhalfARB value
= src
[i
];
929 if (srcPacking
->SwapBytes
) {
932 depthValues
[i
] = _mesa_half_to_float(value
);
938 _mesa_problem(NULL
, "bad type in _mesa_unpack_depth_span()");
943 /* apply depth scale and bias */
945 const GLfloat scale
= ctx
->Pixel
.DepthScale
;
946 const GLfloat bias
= ctx
->Pixel
.DepthBias
;
947 if (scale
!= 1.0F
|| bias
!= 0.0F
) {
949 for (i
= 0; i
< n
; i
++) {
950 depthValues
[i
] = depthValues
[i
] * scale
+ bias
;
956 /* clamp to [0, 1] */
959 for (i
= 0; i
< n
; i
++) {
960 depthValues
[i
] = CLAMP(depthValues
[i
], 0.0F
, 1.0F
);
965 * Convert values to dstType
967 if (dstType
== GL_UNSIGNED_INT
) {
968 GLuint
*zValues
= (GLuint
*) dest
;
970 if (depthMax
<= 0xffffff) {
971 /* no overflow worries */
972 for (i
= 0; i
< n
; i
++) {
973 zValues
[i
] = (GLuint
) (depthValues
[i
] * (GLfloat
) depthMax
);
977 /* need to use double precision to prevent overflow problems */
978 for (i
= 0; i
< n
; i
++) {
979 GLdouble z
= depthValues
[i
] * (GLdouble
) depthMax
;
980 if (z
>= (GLdouble
) 0xffffffff)
981 zValues
[i
] = 0xffffffff;
983 zValues
[i
] = (GLuint
) z
;
987 else if (dstType
== GL_UNSIGNED_SHORT
) {
988 GLushort
*zValues
= (GLushort
*) dest
;
990 assert(depthMax
<= 0xffff);
991 for (i
= 0; i
< n
; i
++) {
992 zValues
[i
] = (GLushort
) (depthValues
[i
] * (GLfloat
) depthMax
);
995 else if (dstType
== GL_FLOAT
) {
996 /* Nothing to do. depthValues is pointing to dest. */
998 else if (dstType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
) {
999 GLfloat
*zValues
= (GLfloat
*) dest
;
1001 for (i
= 0; i
< n
; i
++) {
1002 zValues
[i
*2] = depthValues
[i
];
1014 * Pack an array of depth values. The values are floats in [0,1].
1017 _mesa_pack_depth_span( struct gl_context
*ctx
, GLuint n
, GLvoid
*dest
,
1018 GLenum dstType
, const GLfloat
*depthSpan
,
1019 const struct gl_pixelstore_attrib
*dstPacking
)
1021 GLfloat
*depthCopy
= malloc(n
* sizeof(GLfloat
));
1023 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
1027 if (ctx
->Pixel
.DepthScale
!= 1.0F
|| ctx
->Pixel
.DepthBias
!= 0.0F
) {
1028 memcpy(depthCopy
, depthSpan
, n
* sizeof(GLfloat
));
1029 _mesa_scale_and_bias_depth(ctx
, n
, depthCopy
);
1030 depthSpan
= depthCopy
;
1034 case GL_UNSIGNED_BYTE
:
1036 GLubyte
*dst
= (GLubyte
*) dest
;
1038 for (i
= 0; i
< n
; i
++) {
1039 dst
[i
] = FLOAT_TO_UBYTE( depthSpan
[i
] );
1045 GLbyte
*dst
= (GLbyte
*) dest
;
1047 for (i
= 0; i
< n
; i
++) {
1048 dst
[i
] = FLOAT_TO_BYTE( depthSpan
[i
] );
1052 case GL_UNSIGNED_SHORT
:
1054 GLushort
*dst
= (GLushort
*) dest
;
1056 for (i
= 0; i
< n
; i
++) {
1057 CLAMPED_FLOAT_TO_USHORT(dst
[i
], depthSpan
[i
]);
1059 if (dstPacking
->SwapBytes
) {
1060 _mesa_swap2( (GLushort
*) dst
, n
);
1066 GLshort
*dst
= (GLshort
*) dest
;
1068 for (i
= 0; i
< n
; i
++) {
1069 dst
[i
] = FLOAT_TO_SHORT( depthSpan
[i
] );
1071 if (dstPacking
->SwapBytes
) {
1072 _mesa_swap2( (GLushort
*) 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 _mesa_problem(ctx
, "bad type in _mesa_pack_depth_span (%s)",
1127 _mesa_enum_to_string(dstType
));
1136 * Pack depth and stencil values as GL_DEPTH_STENCIL (GL_UNSIGNED_INT_24_8 etc)
1139 _mesa_pack_depth_stencil_span(struct gl_context
*ctx
,GLuint n
,
1140 GLenum dstType
, GLuint
*dest
,
1141 const GLfloat
*depthVals
,
1142 const GLubyte
*stencilVals
,
1143 const struct gl_pixelstore_attrib
*dstPacking
)
1145 GLfloat
*depthCopy
= malloc(n
* sizeof(GLfloat
));
1146 GLubyte
*stencilCopy
= malloc(n
* sizeof(GLubyte
));
1149 if (!depthCopy
|| !stencilCopy
) {
1150 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
1156 if (ctx
->Pixel
.DepthScale
!= 1.0F
|| ctx
->Pixel
.DepthBias
!= 0.0F
) {
1157 memcpy(depthCopy
, depthVals
, n
* sizeof(GLfloat
));
1158 _mesa_scale_and_bias_depth(ctx
, n
, depthCopy
);
1159 depthVals
= depthCopy
;
1162 if (ctx
->Pixel
.IndexShift
||
1163 ctx
->Pixel
.IndexOffset
||
1164 ctx
->Pixel
.MapStencilFlag
) {
1165 memcpy(stencilCopy
, stencilVals
, n
* sizeof(GLubyte
));
1166 _mesa_apply_stencil_transfer_ops(ctx
, n
, stencilCopy
);
1167 stencilVals
= stencilCopy
;
1171 case GL_UNSIGNED_INT_24_8
:
1172 for (i
= 0; i
< n
; i
++) {
1173 GLuint z
= (GLuint
) (depthVals
[i
] * 0xffffff);
1174 dest
[i
] = (z
<< 8) | (stencilVals
[i
] & 0xff);
1177 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
1178 for (i
= 0; i
< n
; i
++) {
1179 ((GLfloat
*)dest
)[i
*2] = depthVals
[i
];
1180 dest
[i
*2+1] = stencilVals
[i
] & 0xff;
1185 if (dstPacking
->SwapBytes
) {
1186 _mesa_swap4(dest
, n
);
1196 * Unpack image data. Apply byte swapping, byte flipping (bitmap).
1197 * Return all image data in a contiguous block. This is used when we
1198 * compile glDrawPixels, glTexImage, etc into a display list. We
1199 * need a copy of the data in a standard format.
1202 _mesa_unpack_image( GLuint dimensions
,
1203 GLsizei width
, GLsizei height
, GLsizei depth
,
1204 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1205 const struct gl_pixelstore_attrib
*unpack
)
1207 GLint bytesPerRow
, compsPerRow
;
1208 GLboolean flipBytes
, swap2
, swap4
;
1211 return NULL
; /* not necessarily an error */
1213 if (width
<= 0 || height
<= 0 || depth
<= 0)
1214 return NULL
; /* generate error later */
1216 if (type
== GL_BITMAP
) {
1217 bytesPerRow
= (width
+ 7) >> 3;
1218 flipBytes
= unpack
->LsbFirst
;
1219 swap2
= swap4
= GL_FALSE
;
1223 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1224 GLint components
= _mesa_components_in_format(format
);
1227 if (_mesa_type_is_packed(type
))
1230 if (bytesPerPixel
<= 0 || components
<= 0)
1231 return NULL
; /* bad format or type. generate error later */
1232 bytesPerRow
= bytesPerPixel
* width
;
1233 bytesPerComp
= bytesPerPixel
/ components
;
1234 flipBytes
= GL_FALSE
;
1235 swap2
= (bytesPerComp
== 2) && unpack
->SwapBytes
;
1236 swap4
= (bytesPerComp
== 4) && unpack
->SwapBytes
;
1237 compsPerRow
= components
* width
;
1238 assert(compsPerRow
>= width
);
1243 = malloc(bytesPerRow
* height
* depth
);
1247 return NULL
; /* generate GL_OUT_OF_MEMORY later */
1250 for (img
= 0; img
< depth
; img
++) {
1251 for (row
= 0; row
< height
; row
++) {
1252 const GLvoid
*src
= _mesa_image_address(dimensions
, unpack
, pixels
,
1253 width
, height
, format
, type
, img
, row
, 0);
1255 if ((type
== GL_BITMAP
) && (unpack
->SkipPixels
& 0x7)) {
1257 flipBytes
= GL_FALSE
;
1258 if (unpack
->LsbFirst
) {
1259 GLubyte srcMask
= 1 << (unpack
->SkipPixels
& 0x7);
1260 GLubyte dstMask
= 128;
1261 const GLubyte
*s
= src
;
1264 for (i
= 0; i
< width
; i
++) {
1268 if (srcMask
== 128) {
1273 srcMask
= srcMask
<< 1;
1281 dstMask
= dstMask
>> 1;
1286 GLubyte srcMask
= 128 >> (unpack
->SkipPixels
& 0x7);
1287 GLubyte dstMask
= 128;
1288 const GLubyte
*s
= src
;
1291 for (i
= 0; i
< width
; i
++) {
1300 srcMask
= srcMask
>> 1;
1308 dstMask
= dstMask
>> 1;
1314 memcpy(dst
, src
, bytesPerRow
);
1317 /* byte flipping/swapping */
1319 flip_bytes((GLubyte
*) dst
, bytesPerRow
);
1322 _mesa_swap2((GLushort
*) dst
, compsPerRow
);
1325 _mesa_swap4((GLuint
*) dst
, compsPerRow
);
1335 _mesa_pack_luminance_from_rgba_float(GLuint n
, GLfloat rgba
[][4],
1336 GLvoid
*dstAddr
, GLenum dst_format
,
1337 GLbitfield transferOps
)
1340 GLfloat
*dst
= (GLfloat
*) dstAddr
;
1342 switch (dst_format
) {
1344 if (transferOps
& IMAGE_CLAMP_BIT
) {
1345 for (i
= 0; i
< n
; i
++) {
1346 GLfloat sum
= rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
1347 dst
[i
] = CLAMP(sum
, 0.0F
, 1.0F
);
1350 for (i
= 0; i
< n
; i
++) {
1351 dst
[i
] = rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
1355 case GL_LUMINANCE_ALPHA
:
1356 if (transferOps
& IMAGE_CLAMP_BIT
) {
1357 for (i
= 0; i
< n
; i
++) {
1358 GLfloat sum
= rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
1359 dst
[2*i
] = CLAMP(sum
, 0.0F
, 1.0F
);
1360 dst
[2*i
+1] = rgba
[i
][ACOMP
];
1363 for (i
= 0; i
< n
; i
++) {
1364 dst
[2*i
] = rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
1365 dst
[2*i
+1] = rgba
[i
][ACOMP
];
1370 assert(!"Unsupported format");
1375 clamp_sint64_to_sint32(int64_t src
)
1377 return CLAMP(src
, INT32_MIN
, INT32_MAX
);
1381 clamp_sint64_to_uint32(int64_t src
)
1383 return CLAMP(src
, 0, UINT32_MAX
);
1387 clamp_uint64_to_uint32(uint64_t src
)
1389 return MIN2(src
, UINT32_MAX
);
1393 clamp_uint64_to_sint32(uint64_t src
)
1395 return MIN2(src
, INT32_MAX
);
1399 convert_integer_luminance64(int64_t src64
, int bits
,
1400 bool dst_is_signed
, bool src_is_signed
)
1404 /* Clamp Luminance value from 64-bit to 32-bit. Consider if we need
1405 * any signed<->unsigned conversion too.
1407 if (src_is_signed
&& dst_is_signed
)
1408 src32
= clamp_sint64_to_sint32(src64
);
1409 else if (src_is_signed
&& !dst_is_signed
)
1410 src32
= clamp_sint64_to_uint32(src64
);
1411 else if (!src_is_signed
&& dst_is_signed
)
1412 src32
= clamp_uint64_to_sint32(src64
);
1414 src32
= clamp_uint64_to_uint32(src64
);
1416 /* If the dst type is < 32-bit, we need an extra clamp */
1421 return _mesa_signed_to_signed(src32
, bits
);
1423 return _mesa_unsigned_to_unsigned(src32
, bits
);
1428 convert_integer(int32_t src
, int bits
, bool dst_is_signed
, bool src_is_signed
)
1430 if (src_is_signed
&& dst_is_signed
)
1431 return _mesa_signed_to_signed(src
, bits
);
1432 else if (src_is_signed
&& !dst_is_signed
)
1433 return _mesa_signed_to_unsigned(src
, bits
);
1434 else if (!src_is_signed
&& dst_is_signed
)
1435 return _mesa_unsigned_to_signed(src
, bits
);
1437 return _mesa_unsigned_to_unsigned(src
, bits
);
1441 _mesa_pack_luminance_from_rgba_integer(GLuint n
,
1442 GLuint rgba
[][4], bool rgba_is_signed
,
1449 int32_t lum32
, alpha
;
1453 assert(dst_format
== GL_LUMINANCE_INTEGER_EXT
||
1454 dst_format
== GL_LUMINANCE_ALPHA_INTEGER_EXT
);
1456 /* We first compute luminance values as a 64-bit addition of the
1457 * 32-bit R,G,B components, then we clamp the result to the dst type size.
1459 * Notice that this operation involves casting the 32-bit R,G,B components
1460 * to 64-bit before the addition. Since rgba is defined as a GLuint array
1461 * we need to be careful when rgba packs signed data and make sure
1462 * that we cast to a 32-bit signed integer values before casting them to
1463 * 64-bit signed integers.
1465 dst_is_signed
= (dst_type
== GL_BYTE
|| dst_type
== GL_SHORT
||
1466 dst_type
== GL_INT
);
1468 dst_bits
= _mesa_sizeof_type(dst_type
) * 8;
1469 assert(dst_bits
> 0);
1471 switch (dst_format
) {
1472 case GL_LUMINANCE_INTEGER_EXT
:
1473 for (i
= 0; i
< n
; i
++) {
1474 if (!rgba_is_signed
) {
1475 lum64
= (uint64_t) rgba
[i
][RCOMP
] +
1476 (uint64_t) rgba
[i
][GCOMP
] +
1477 (uint64_t) rgba
[i
][BCOMP
];
1479 lum64
= (int64_t) ((int32_t) rgba
[i
][RCOMP
]) +
1480 (int64_t) ((int32_t) rgba
[i
][GCOMP
]) +
1481 (int64_t) ((int32_t) rgba
[i
][BCOMP
]);
1483 lum32
= convert_integer_luminance64(lum64
, dst_bits
,
1484 dst_is_signed
, rgba_is_signed
);
1487 case GL_UNSIGNED_BYTE
: {
1488 GLbyte
*dst
= (GLbyte
*) dstAddr
;
1493 case GL_UNSIGNED_SHORT
: {
1494 GLshort
*dst
= (GLshort
*) dstAddr
;
1499 case GL_UNSIGNED_INT
: {
1500 GLint
*dst
= (GLint
*) dstAddr
;
1507 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
1508 for (i
= 0; i
< n
; i
++) {
1509 if (!rgba_is_signed
) {
1510 lum64
= (uint64_t) rgba
[i
][RCOMP
] +
1511 (uint64_t) rgba
[i
][GCOMP
] +
1512 (uint64_t) rgba
[i
][BCOMP
];
1514 lum64
= (int64_t) ((int32_t) rgba
[i
][RCOMP
]) +
1515 (int64_t) ((int32_t) rgba
[i
][GCOMP
]) +
1516 (int64_t) ((int32_t) rgba
[i
][BCOMP
]);
1518 lum32
= convert_integer_luminance64(lum64
, dst_bits
,
1519 dst_is_signed
, rgba_is_signed
);
1520 alpha
= convert_integer(rgba
[i
][ACOMP
], dst_bits
,
1521 dst_is_signed
, rgba_is_signed
);
1524 case GL_UNSIGNED_BYTE
: {
1525 GLbyte
*dst
= (GLbyte
*) dstAddr
;
1531 case GL_UNSIGNED_SHORT
: {
1532 GLshort
*dst
= (GLshort
*) dstAddr
;
1538 case GL_UNSIGNED_INT
: {
1539 GLint
*dst
= (GLint
*) dstAddr
;
1551 _mesa_unpack_color_index_to_rgba_float(struct gl_context
*ctx
, GLuint dims
,
1552 const void *src
, GLenum srcFormat
, GLenum srcType
,
1553 int srcWidth
, int srcHeight
, int srcDepth
,
1554 const struct gl_pixelstore_attrib
*srcPacking
,
1555 GLbitfield transferOps
)
1559 GLfloat
*rgba
, *dstPtr
;
1561 count
= srcWidth
* srcHeight
;
1562 indexes
= malloc(count
* sizeof(GLuint
));
1564 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
1568 rgba
= malloc(4 * count
* srcDepth
* sizeof(GLfloat
));
1571 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
1575 /* Convert indexes to RGBA float */
1577 for (img
= 0; img
< srcDepth
; img
++) {
1578 const GLubyte
*srcPtr
=
1579 (const GLubyte
*) _mesa_image_address(dims
, srcPacking
, src
,
1580 srcWidth
, srcHeight
,
1584 extract_uint_indexes(count
, indexes
, srcFormat
, srcType
, srcPtr
, srcPacking
);
1586 if (transferOps
& IMAGE_SHIFT_OFFSET_BIT
)
1587 _mesa_shift_and_offset_ci(ctx
, count
, indexes
);
1589 _mesa_map_ci_to_rgba(ctx
, count
, indexes
, (float (*)[4])dstPtr
);
1591 /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
1592 * with color indexes.
1594 transferOps
&= ~(IMAGE_SCALE_BIAS_BIT
| IMAGE_MAP_COLOR_BIT
);
1595 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, count
, (float (*)[4])dstPtr
);
1597 dstPtr
+= srcHeight
* srcWidth
* 4;
1606 _mesa_unpack_color_index_to_rgba_ubyte(struct gl_context
*ctx
, GLuint dims
,
1607 const void *src
, GLenum srcFormat
, GLenum srcType
,
1608 int srcWidth
, int srcHeight
, int srcDepth
,
1609 const struct gl_pixelstore_attrib
*srcPacking
,
1610 GLbitfield transferOps
)
1616 transferOps
|= IMAGE_CLAMP_BIT
;
1617 rgba
= _mesa_unpack_color_index_to_rgba_float(ctx
, dims
,
1618 src
, srcFormat
, srcType
,
1619 srcWidth
, srcHeight
, srcDepth
,
1620 srcPacking
, transferOps
);
1622 count
= srcWidth
* srcHeight
* srcDepth
;
1623 dst
= malloc(count
* 4 * sizeof(GLubyte
));
1624 for (i
= 0; i
< count
; i
++) {
1625 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 0], rgba
[i
* 4 + 0]);
1626 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 1], rgba
[i
* 4 + 1]);
1627 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 2], rgba
[i
* 4 + 2]);
1628 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 3], rgba
[i
* 4 + 3]);