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"
58
#include "format_unpack.h"
59
#include "util/format/u_format.h"
62
* Flip the 8 bits in each byte of the given array.
65
* \param n number of bytes.
67
* \todo try this trick to flip bytes someday:
69
* v = ((v & 0x55555555) << 1) | ((v >> 1) & 0x55555555);
70
* v = ((v & 0x33333333) << 2) | ((v >> 2) & 0x33333333);
71
* v = ((v & 0x0f0f0f0f) << 4) | ((v >> 4) & 0x0f0f0f0f);
75
flip_bytes( GLubyte *p, GLuint n )
78
for (i = 0; i < n; i++) {
79
b = (GLuint) p[i]; /* words are often faster than bytes */
80
a = ((b & 0x01) << 7) |
95
* Unpack a 32x32 pixel polygon stipple from user memory using the
96
* current pixel unpack settings.
99
_mesa_unpack_polygon_stipple( const GLubyte *pattern, GLuint dest[32],
100
const struct gl_pixelstore_attrib *unpacking )
102
GLubyte *ptrn = (GLubyte *) _mesa_unpack_image(2, 32, 32, 1, GL_COLOR_INDEX,
103
GL_BITMAP, pattern, unpacking);
105
/* Convert pattern from GLubytes to GLuints and handle big/little
110
for (i = 0; i < 32; i++) {
111
dest[i] = (p[0] << 24)
123
* Pack polygon stipple into user memory given current pixel packing
127
_mesa_pack_polygon_stipple( const GLuint pattern[32], GLubyte *dest,
128
const struct gl_pixelstore_attrib *packing )
130
/* Convert pattern from GLuints to GLubytes to handle big/little
131
* endian differences.
135
for (i = 0; i < 32; i++) {
136
ptrn[i * 4 + 0] = (GLubyte) ((pattern[i] >> 24) & 0xff);
137
ptrn[i * 4 + 1] = (GLubyte) ((pattern[i] >> 16) & 0xff);
138
ptrn[i * 4 + 2] = (GLubyte) ((pattern[i] >> 8 ) & 0xff);
139
ptrn[i * 4 + 3] = (GLubyte) ((pattern[i] ) & 0xff);
142
_mesa_pack_bitmap(32, 32, ptrn, dest, packing);
150
_mesa_pack_bitmap( GLint width, GLint height, const GLubyte *source,
151
GLubyte *dest, const struct gl_pixelstore_attrib *packing )
153
GLint row, width_in_bytes;
159
width_in_bytes = DIV_ROUND_UP( width, 8 );
161
for (row = 0; row < height; row++) {
162
GLubyte *dst = (GLubyte *) _mesa_image_address2d(packing, dest,
163
width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0);
167
if ((packing->SkipPixels & 7) == 0) {
168
memcpy( dst, src, width_in_bytes );
169
if (packing->LsbFirst) {
170
flip_bytes( dst, width_in_bytes );
174
/* handling SkipPixels is a bit tricky (no pun intended!) */
176
if (packing->LsbFirst) {
177
GLubyte srcMask = 128;
178
GLubyte dstMask = 1 << (packing->SkipPixels & 0x7);
179
const GLubyte *s = src;
182
for (i = 0; i < width; i++) {
191
srcMask = srcMask >> 1;
193
if (dstMask == 128) {
199
dstMask = dstMask << 1;
204
GLubyte srcMask = 128;
205
GLubyte dstMask = 128 >> (packing->SkipPixels & 0x7);
206
const GLubyte *s = src;
209
for (i = 0; i < width; i++) {
218
srcMask = srcMask >> 1;
226
dstMask = dstMask >> 1;
231
src += width_in_bytes;
236
#define SWAP2BYTE(VALUE) \
238
GLubyte *bytes = (GLubyte *) &(VALUE); \
239
GLubyte tmp = bytes[0]; \
240
bytes[0] = bytes[1]; \
244
#define SWAP4BYTE(VALUE) \
246
GLubyte *bytes = (GLubyte *) &(VALUE); \
247
GLubyte tmp = bytes[0]; \
248
bytes[0] = bytes[3]; \
251
bytes[1] = bytes[2]; \
257
extract_uint_indexes(GLuint n, GLuint indexes[],
258
GLenum srcFormat, GLenum srcType, const GLvoid *src,
259
const struct gl_pixelstore_attrib *unpack )
261
assert(srcFormat == GL_COLOR_INDEX || srcFormat == GL_STENCIL_INDEX);
263
assert(srcType == GL_BITMAP ||
264
srcType == GL_UNSIGNED_BYTE ||
265
srcType == GL_BYTE ||
266
srcType == GL_UNSIGNED_SHORT ||
267
srcType == GL_SHORT ||
268
srcType == GL_UNSIGNED_INT ||
270
srcType == GL_UNSIGNED_INT_24_8_EXT ||
271
srcType == GL_HALF_FLOAT_ARB ||
272
srcType == GL_HALF_FLOAT_OES ||
273
srcType == GL_FLOAT ||
274
srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
279
GLubyte *ubsrc = (GLubyte *) src;
280
if (unpack->LsbFirst) {
281
GLubyte mask = 1 << (unpack->SkipPixels & 0x7);
283
for (i = 0; i < n; i++) {
284
indexes[i] = (*ubsrc & mask) ? 1 : 0;
295
GLubyte mask = 128 >> (unpack->SkipPixels & 0x7);
297
for (i = 0; i < n; i++) {
298
indexes[i] = (*ubsrc & mask) ? 1 : 0;
310
case GL_UNSIGNED_BYTE:
313
const GLubyte *s = (const GLubyte *) src;
314
for (i = 0; i < n; i++)
321
const GLbyte *s = (const GLbyte *) src;
322
for (i = 0; i < n; i++)
326
case GL_UNSIGNED_SHORT:
329
const GLushort *s = (const GLushort *) src;
330
if (unpack->SwapBytes) {
331
for (i = 0; i < n; i++) {
332
GLushort value = s[i];
338
for (i = 0; i < n; i++)
346
const GLshort *s = (const GLshort *) src;
347
if (unpack->SwapBytes) {
348
for (i = 0; i < n; i++) {
349
GLshort value = s[i];
355
for (i = 0; i < n; i++)
360
case GL_UNSIGNED_INT:
363
const GLuint *s = (const GLuint *) src;
364
if (unpack->SwapBytes) {
365
for (i = 0; i < n; i++) {
372
for (i = 0; i < n; i++)
380
const GLint *s = (const GLint *) src;
381
if (unpack->SwapBytes) {
382
for (i = 0; i < n; i++) {
389
for (i = 0; i < n; i++)
397
const GLfloat *s = (const GLfloat *) src;
398
if (unpack->SwapBytes) {
399
for (i = 0; i < n; i++) {
400
GLfloat value = s[i];
402
indexes[i] = (GLuint) value;
406
for (i = 0; i < n; i++)
407
indexes[i] = (GLuint) s[i];
411
case GL_HALF_FLOAT_ARB:
412
case GL_HALF_FLOAT_OES:
415
const GLhalfARB *s = (const GLhalfARB *) src;
416
if (unpack->SwapBytes) {
417
for (i = 0; i < n; i++) {
418
GLhalfARB value = s[i];
420
indexes[i] = (GLuint) _mesa_half_to_float(value);
424
for (i = 0; i < n; i++)
425
indexes[i] = (GLuint) _mesa_half_to_float(s[i]);
429
case GL_UNSIGNED_INT_24_8_EXT:
432
const GLuint *s = (const GLuint *) src;
433
if (unpack->SwapBytes) {
434
for (i = 0; i < n; i++) {
437
indexes[i] = value & 0xff; /* lower 8 bits */
441
for (i = 0; i < n; i++)
442
indexes[i] = s[i] & 0xff; /* lower 8 bits */
446
case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
449
const GLuint *s = (const GLuint *) src;
450
if (unpack->SwapBytes) {
451
for (i = 0; i < n; i++) {
452
GLuint value = s[i*2+1];
454
indexes[i] = value & 0xff; /* lower 8 bits */
458
for (i = 0; i < n; i++)
459
indexes[i] = s[i*2+1] & 0xff; /* lower 8 bits */
465
unreachable("bad srcType in extract_uint_indexes");
471
* Unpack a row of stencil data from a client buffer according to
472
* the pixel unpacking parameters.
473
* This is (or will be) used by glDrawPixels
475
* Args: ctx - the context
476
* n - number of pixels
477
* dstType - destination data type
478
* dest - destination array
479
* srcType - source pixel type
480
* source - source data pointer
481
* srcPacking - pixel unpacking parameters
482
* transferOps - apply offset/bias/lookup ops?
485
_mesa_unpack_stencil_span( struct gl_context *ctx, GLuint n,
486
GLenum dstType, GLvoid *dest,
487
GLenum srcType, const GLvoid *source,
488
const struct gl_pixelstore_attrib *srcPacking,
489
GLbitfield transferOps )
491
assert(srcType == GL_BITMAP ||
492
srcType == GL_UNSIGNED_BYTE ||
493
srcType == GL_BYTE ||
494
srcType == GL_UNSIGNED_SHORT ||
495
srcType == GL_SHORT ||
496
srcType == GL_UNSIGNED_INT ||
498
srcType == GL_UNSIGNED_INT_24_8_EXT ||
499
srcType == GL_HALF_FLOAT_ARB ||
500
srcType == GL_HALF_FLOAT_OES ||
501
srcType == GL_FLOAT ||
502
srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
504
assert(dstType == GL_UNSIGNED_BYTE ||
505
dstType == GL_UNSIGNED_SHORT ||
506
dstType == GL_UNSIGNED_INT ||
507
dstType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
509
/* only shift and offset apply to stencil */
510
transferOps &= IMAGE_SHIFT_OFFSET_BIT;
513
* Try simple cases first
515
if (transferOps == 0 &&
516
!ctx->Pixel.MapStencilFlag &&
517
srcType == GL_UNSIGNED_BYTE &&
518
dstType == GL_UNSIGNED_BYTE) {
519
memcpy(dest, source, n * sizeof(GLubyte));
521
else if (transferOps == 0 &&
522
!ctx->Pixel.MapStencilFlag &&
523
srcType == GL_UNSIGNED_INT &&
524
dstType == GL_UNSIGNED_INT &&
525
!srcPacking->SwapBytes) {
526
memcpy(dest, source, n * sizeof(GLuint));
532
GLuint *indexes = malloc(n * sizeof(GLuint));
535
_mesa_error(ctx, GL_OUT_OF_MEMORY, "stencil unpacking");
539
extract_uint_indexes(n, indexes, GL_STENCIL_INDEX, srcType, source,
542
if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
543
/* shift and offset indexes */
544
_mesa_shift_and_offset_ci(ctx, n, indexes);
547
if (ctx->Pixel.MapStencilFlag) {
548
/* Apply stencil lookup table */
549
const GLuint mask = ctx->PixelMaps.StoS.Size - 1;
551
for (i = 0; i < n; i++) {
552
indexes[i] = (GLuint)ctx->PixelMaps.StoS.Map[ indexes[i] & mask ];
556
/* convert to dest type */
558
case GL_UNSIGNED_BYTE:
560
GLubyte *dst = (GLubyte *) dest;
562
for (i = 0; i < n; i++) {
563
dst[i] = (GLubyte) (indexes[i] & 0xff);
567
case GL_UNSIGNED_SHORT:
569
GLuint *dst = (GLuint *) dest;
571
for (i = 0; i < n; i++) {
572
dst[i] = (GLushort) (indexes[i] & 0xffff);
576
case GL_UNSIGNED_INT:
577
memcpy(dest, indexes, n * sizeof(GLuint));
579
case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
581
GLuint *dst = (GLuint *) dest;
583
for (i = 0; i < n; i++) {
584
dst[i*2+1] = indexes[i] & 0xff; /* lower 8 bits */
589
unreachable("bad dstType in _mesa_unpack_stencil_span");
598
_mesa_pack_stencil_span( struct gl_context *ctx, GLuint n,
599
GLenum dstType, GLvoid *dest, const GLubyte *source,
600
const struct gl_pixelstore_attrib *dstPacking )
602
GLubyte *stencil = malloc(n * sizeof(GLubyte));
605
_mesa_error(ctx, GL_OUT_OF_MEMORY, "stencil packing");
609
if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset ||
610
ctx->Pixel.MapStencilFlag) {
611
/* make a copy of input */
612
memcpy(stencil, source, n * sizeof(GLubyte));
613
_mesa_apply_stencil_transfer_ops(ctx, n, stencil);
618
case GL_UNSIGNED_BYTE:
619
memcpy(dest, source, n);
623
GLbyte *dst = (GLbyte *) dest;
626
dst[i] = (GLbyte) (source[i] & 0x7f);
630
case GL_UNSIGNED_SHORT:
632
GLushort *dst = (GLushort *) dest;
635
dst[i] = (GLushort) source[i];
637
if (dstPacking->SwapBytes) {
638
_mesa_swap2( (GLushort *) dst, n );
644
GLshort *dst = (GLshort *) dest;
647
dst[i] = (GLshort) source[i];
649
if (dstPacking->SwapBytes) {
650
_mesa_swap2( (GLushort *) dst, n );
654
case GL_UNSIGNED_INT:
656
GLuint *dst = (GLuint *) dest;
659
dst[i] = (GLuint) source[i];
661
if (dstPacking->SwapBytes) {
662
_mesa_swap4( (GLuint *) dst, n );
668
GLint *dst = (GLint *) dest;
671
dst[i] = (GLint) source[i];
673
if (dstPacking->SwapBytes) {
674
_mesa_swap4( (GLuint *) dst, n );
680
GLfloat *dst = (GLfloat *) dest;
683
dst[i] = (GLfloat) source[i];
685
if (dstPacking->SwapBytes) {
686
_mesa_swap4( (GLuint *) dst, n );
690
case GL_HALF_FLOAT_ARB:
691
case GL_HALF_FLOAT_OES:
693
GLhalfARB *dst = (GLhalfARB *) dest;
696
dst[i] = _mesa_float_to_half( (float) source[i] );
698
if (dstPacking->SwapBytes) {
699
_mesa_swap2( (GLushort *) dst, n );
704
if (dstPacking->LsbFirst) {
705
GLubyte *dst = (GLubyte *) dest;
708
for (i = 0; i < n; i++) {
711
*dst |= ((source[i] != 0) << shift);
720
GLubyte *dst = (GLubyte *) dest;
723
for (i = 0; i < n; i++) {
726
*dst |= ((source[i] != 0) << shift);
736
unreachable("bad type in _mesa_pack_index_span");
742
#define DEPTH_VALUES(GLTYPE, GLTYPE2FLOAT) \
745
const GLTYPE *src = (const GLTYPE *)source; \
746
for (i = 0; i < n; i++) { \
747
GLTYPE value = src[i]; \
748
if (srcPacking->SwapBytes) { \
749
if (sizeof(GLTYPE) == 2) { \
751
} else if (sizeof(GLTYPE) == 4) { \
755
depthValues[i] = GLTYPE2FLOAT(value); \
761
* Unpack a row of depth/z values from memory, returning GLushort, GLuint
763
* The glPixelTransfer (scale/bias) params will be applied.
765
* \param dstType one of GL_UNSIGNED_SHORT, GL_UNSIGNED_INT, GL_FLOAT
766
* \param depthMax max value for returned GLushort or GLuint values
767
* (ignored for GLfloat).
770
_mesa_unpack_depth_span( struct gl_context *ctx, GLuint n,
771
GLenum dstType, GLvoid *dest, GLuint depthMax,
772
GLenum srcType, const GLvoid *source,
773
const struct gl_pixelstore_attrib *srcPacking )
775
GLfloat *depthTemp = NULL, *depthValues;
776
GLboolean needClamp = GL_FALSE;
778
/* Look for special cases first.
779
* Not only are these faster, they're less prone to numeric conversion
780
* problems. Otherwise, converting from an int type to a float then
781
* back to an int type can introduce errors that will show up as
782
* artifacts in things like depth peeling which uses glCopyTexImage.
784
if (ctx->Pixel.DepthScale == 1.0F && ctx->Pixel.DepthBias == 0.0F) {
785
if (srcType == GL_UNSIGNED_INT && dstType == GL_UNSIGNED_SHORT) {
786
const GLuint *src = (const GLuint *) source;
787
GLushort *dst = (GLushort *) dest;
789
for (i = 0; i < n; i++) {
790
dst[i] = src[i] >> 16;
794
if (srcType == GL_UNSIGNED_SHORT
795
&& dstType == GL_UNSIGNED_INT
796
&& depthMax == 0xffffffff) {
797
const GLushort *src = (const GLushort *) source;
798
GLuint *dst = (GLuint *) dest;
800
for (i = 0; i < n; i++) {
801
dst[i] = src[i] | (src[i] << 16);
805
if (srcType == GL_UNSIGNED_INT_24_8
806
&& dstType == GL_UNSIGNED_INT
807
&& depthMax == 0xffffff) {
808
const GLuint *src = (const GLuint *) source;
809
GLuint *dst = (GLuint *) dest;
811
for (i = 0; i < n; i++) {
812
dst[i] = src[i] >> 8;
816
/* XXX may want to add additional cases here someday */
819
/* general case path follows */
821
if (dstType == GL_FLOAT) {
822
depthValues = (GLfloat *) dest;
825
depthTemp = malloc(n * sizeof(GLfloat));
827
_mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
831
depthValues = depthTemp;
834
/* Convert incoming values to GLfloat. Some conversions will require
839
DEPTH_VALUES(GLbyte, BYTE_TO_FLOATZ);
842
case GL_UNSIGNED_BYTE:
843
DEPTH_VALUES(GLubyte, UBYTE_TO_FLOAT);
846
DEPTH_VALUES(GLshort, SHORT_TO_FLOATZ);
849
case GL_UNSIGNED_SHORT:
850
DEPTH_VALUES(GLushort, USHORT_TO_FLOAT);
853
DEPTH_VALUES(GLint, INT_TO_FLOAT);
856
case GL_UNSIGNED_INT:
857
DEPTH_VALUES(GLuint, UINT_TO_FLOAT);
859
case GL_UNSIGNED_INT_24_8_EXT: /* GL_EXT_packed_depth_stencil */
860
if (dstType == GL_UNSIGNED_INT_24_8_EXT &&
861
depthMax == 0xffffff &&
862
ctx->Pixel.DepthScale == 1.0F &&
863
ctx->Pixel.DepthBias == 0.0F) {
864
const GLuint *src = (const GLuint *) source;
865
GLuint *zValues = (GLuint *) dest;
867
for (i = 0; i < n; i++) {
868
GLuint value = src[i];
869
if (srcPacking->SwapBytes) {
872
zValues[i] = value & 0xffffff00;
878
const GLuint *src = (const GLuint *) source;
879
const GLfloat scale = 1.0f / 0xffffff;
881
for (i = 0; i < n; i++) {
882
GLuint value = src[i];
883
if (srcPacking->SwapBytes) {
886
depthValues[i] = (value >> 8) * scale;
890
case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
893
const GLfloat *src = (const GLfloat *)source;
894
for (i = 0; i < n; i++) {
895
GLfloat value = src[i * 2];
896
if (srcPacking->SwapBytes) {
899
depthValues[i] = value;
905
DEPTH_VALUES(GLfloat, 1*);
908
case GL_HALF_FLOAT_ARB:
909
case GL_HALF_FLOAT_OES:
912
const GLhalfARB *src = (const GLhalfARB *) source;
913
for (i = 0; i < n; i++) {
914
GLhalfARB value = src[i];
915
if (srcPacking->SwapBytes) {
918
depthValues[i] = _mesa_half_to_float(value);
924
_mesa_problem(NULL, "bad type in _mesa_unpack_depth_span()");
929
/* apply depth scale and bias */
931
const GLfloat scale = ctx->Pixel.DepthScale;
932
const GLfloat bias = ctx->Pixel.DepthBias;
933
if (scale != 1.0F || bias != 0.0F) {
935
for (i = 0; i < n; i++) {
936
depthValues[i] = depthValues[i] * scale + bias;
942
/* clamp to [0, 1] */
945
for (i = 0; i < n; i++) {
946
depthValues[i] = CLAMP(depthValues[i], 0.0F, 1.0F);
951
* Convert values to dstType
953
if (dstType == GL_UNSIGNED_INT) {
954
GLuint *zValues = (GLuint *) dest;
956
if (depthMax <= 0xffffff) {
957
/* no overflow worries */
958
for (i = 0; i < n; i++) {
959
zValues[i] = (GLuint) (depthValues[i] * (GLfloat) depthMax);
963
/* need to use double precision to prevent overflow problems */
964
for (i = 0; i < n; i++) {
965
GLdouble z = depthValues[i] * (GLdouble) depthMax;
966
if (z >= (GLdouble) 0xffffffff)
967
zValues[i] = 0xffffffff;
969
zValues[i] = (GLuint) z;
973
else if (dstType == GL_UNSIGNED_SHORT) {
974
GLushort *zValues = (GLushort *) dest;
976
assert(depthMax <= 0xffff);
977
for (i = 0; i < n; i++) {
978
zValues[i] = (GLushort) (depthValues[i] * (GLfloat) depthMax);
981
else if (dstType == GL_FLOAT) {
982
/* Nothing to do. depthValues is pointing to dest. */
984
else if (dstType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV) {
985
GLfloat *zValues = (GLfloat*) dest;
987
for (i = 0; i < n; i++) {
988
zValues[i*2] = depthValues[i];
1000
* Pack an array of depth values. The values are floats in [0,1].
1003
_mesa_pack_depth_span( struct gl_context *ctx, GLuint n, GLvoid *dest,
1004
GLenum dstType, const GLfloat *depthSpan,
1005
const struct gl_pixelstore_attrib *dstPacking )
1007
GLfloat *depthCopy = malloc(n * sizeof(GLfloat));
1009
_mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
1013
if (ctx->Pixel.DepthScale != 1.0F || ctx->Pixel.DepthBias != 0.0F) {
1014
memcpy(depthCopy, depthSpan, n * sizeof(GLfloat));
1015
_mesa_scale_and_bias_depth(ctx, n, depthCopy);
1016
depthSpan = depthCopy;
1020
case GL_UNSIGNED_BYTE:
1022
GLubyte *dst = (GLubyte *) dest;
1024
for (i = 0; i < n; i++) {
1025
dst[i] = FLOAT_TO_UBYTE( depthSpan[i] );
1031
GLbyte *dst = (GLbyte *) dest;
1033
for (i = 0; i < n; i++) {
1034
dst[i] = FLOAT_TO_BYTE( depthSpan[i] );
1038
case GL_UNSIGNED_SHORT:
1040
GLushort *dst = (GLushort *) dest;
1042
for (i = 0; i < n; i++) {
1043
CLAMPED_FLOAT_TO_USHORT(dst[i], depthSpan[i]);
1045
if (dstPacking->SwapBytes) {
1046
_mesa_swap2( (GLushort *) dst, n );
1052
GLshort *dst = (GLshort *) dest;
1054
for (i = 0; i < n; i++) {
1055
dst[i] = FLOAT_TO_SHORT( depthSpan[i] );
1057
if (dstPacking->SwapBytes) {
1058
_mesa_swap2( (GLushort *) dst, n );
1062
case GL_UNSIGNED_INT_24_8:
1064
const GLdouble scale = (GLdouble) 0xffffff;
1065
GLuint *dst = (GLuint *) dest;
1067
for (i = 0; i < n; i++) {
1068
GLuint z = (GLuint) (depthSpan[i] * scale);
1069
assert(z <= 0xffffff);
1072
if (dstPacking->SwapBytes) {
1073
_mesa_swap4( (GLuint *) dst, n );
1077
case GL_UNSIGNED_INT:
1079
GLuint *dst = (GLuint *) dest;
1081
for (i = 0; i < n; i++) {
1082
dst[i] = FLOAT_TO_UINT( depthSpan[i] );
1084
if (dstPacking->SwapBytes) {
1085
_mesa_swap4( (GLuint *) dst, n );
1091
GLint *dst = (GLint *) dest;
1093
for (i = 0; i < n; i++) {
1094
dst[i] = FLOAT_TO_INT( depthSpan[i] );
1096
if (dstPacking->SwapBytes) {
1097
_mesa_swap4( (GLuint *) dst, n );
1103
GLfloat *dst = (GLfloat *) dest;
1105
for (i = 0; i < n; i++) {
1106
dst[i] = depthSpan[i];
1108
if (dstPacking->SwapBytes) {
1109
_mesa_swap4( (GLuint *) dst, n );
1113
case GL_HALF_FLOAT_ARB:
1114
case GL_HALF_FLOAT_OES:
1116
GLhalfARB *dst = (GLhalfARB *) dest;
1118
for (i = 0; i < n; i++) {
1119
dst[i] = _mesa_float_to_half(depthSpan[i]);
1121
if (dstPacking->SwapBytes) {
1122
_mesa_swap2( (GLushort *) dst, n );
1127
unreachable("bad type in _mesa_pack_depth_span()");
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]);
1637
_mesa_unpack_ubyte_rgba_row(mesa_format format, uint32_t n,
1638
const void *src, uint8_t dst[][4])
1640
const struct util_format_unpack_description *unpack =
1641
util_format_unpack_description((enum pipe_format)format);
1643
if (unpack->unpack_rgba_8unorm) {
1644
unpack->unpack_rgba_8unorm((uint8_t *)dst, src, n);
1646
/* get float values, convert to ubyte */
1648
float *tmp = malloc(n * 4 * sizeof(float));
1651
_mesa_unpack_rgba_row(format, n, src, (float (*)[4]) tmp);
1652
for (i = 0; i < n; i++) {
1653
dst[i][0] = _mesa_float_to_unorm(tmp[i*4+0], 8);
1654
dst[i][1] = _mesa_float_to_unorm(tmp[i*4+1], 8);
1655
dst[i][2] = _mesa_float_to_unorm(tmp[i*4+2], 8);
1656
dst[i][3] = _mesa_float_to_unorm(tmp[i*4+3], 8);
1664
/** Helper struct for MESA_FORMAT_Z32_FLOAT_S8X24_UINT */
1673
unpack_uint_24_8_depth_stencil_Z24_UNORM_S8_UINT(const uint32_t *src, uint32_t *dst, uint32_t n)
1677
for (i = 0; i < n; i++) {
1678
uint32_t val = src[i];
1679
dst[i] = val >> 24 | val << 8;
1684
unpack_uint_24_8_depth_stencil_Z32_S8X24(const uint32_t *src,
1685
uint32_t *dst, uint32_t n)
1689
for (i = 0; i < n; i++) {
1690
/* 8 bytes per pixel (float + uint32) */
1691
float zf = ((float *) src)[i * 2 + 0];
1692
uint32_t z24 = (uint32_t) (zf * (float) 0xffffff);
1693
uint32_t s = src[i * 2 + 1] & 0xff;
1694
dst[i] = (z24 << 8) | s;
1699
unpack_uint_24_8_depth_stencil_S8_UINT_Z24_UNORM(const uint32_t *src, uint32_t *dst, uint32_t n)
1701
memcpy(dst, src, n * 4);
1705
* Unpack depth/stencil returning as GL_UNSIGNED_INT_24_8.
1706
* \param format the source data format
1709
_mesa_unpack_uint_24_8_depth_stencil_row(mesa_format format, uint32_t n,
1710
const void *src, uint32_t *dst)
1713
case MESA_FORMAT_S8_UINT_Z24_UNORM:
1714
unpack_uint_24_8_depth_stencil_S8_UINT_Z24_UNORM(src, dst, n);
1716
case MESA_FORMAT_Z24_UNORM_S8_UINT:
1717
unpack_uint_24_8_depth_stencil_Z24_UNORM_S8_UINT(src, dst, n);
1719
case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
1720
unpack_uint_24_8_depth_stencil_Z32_S8X24(src, dst, n);
1723
unreachable("bad format %s in _mesa_unpack_uint_24_8_depth_stencil_row");
1728
unpack_float_32_uint_24_8_Z24_UNORM_S8_UINT(const uint32_t *src,
1729
uint32_t *dst, uint32_t n)
1732
struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
1733
const double scale = 1.0 / (double) 0xffffff;
1735
for (i = 0; i < n; i++) {
1736
const uint32_t z24 = src[i] & 0xffffff;
1737
d[i].z = z24 * scale;
1738
d[i].x24s8 = src[i] >> 24;
1739
assert(d[i].z >= 0.0f);
1740
assert(d[i].z <= 1.0f);
1745
unpack_float_32_uint_24_8_Z32_FLOAT_S8X24_UINT(const uint32_t *src,
1746
uint32_t *dst, uint32_t n)
1748
memcpy(dst, src, n * sizeof(struct z32f_x24s8));
1752
unpack_float_32_uint_24_8_S8_UINT_Z24_UNORM(const uint32_t *src,
1753
uint32_t *dst, uint32_t n)
1756
struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;
1757
const double scale = 1.0 / (double) 0xffffff;
1759
for (i = 0; i < n; i++) {
1760
const uint32_t z24 = src[i] >> 8;
1761
d[i].z = z24 * scale;
1762
d[i].x24s8 = src[i] & 0xff;
1763
assert(d[i].z >= 0.0f);
1764
assert(d[i].z <= 1.0f);
1769
* Unpack depth/stencil returning as GL_FLOAT_32_UNSIGNED_INT_24_8_REV.
1770
* \param format the source data format
1772
* In GL_FLOAT_32_UNSIGNED_INT_24_8_REV lower 4 bytes contain float
1773
* component and higher 4 bytes contain packed 24-bit and 8-bit
1776
* 31 30 29 28 ... 4 3 2 1 0 31 30 29 ... 9 8 7 6 5 ... 2 1 0
1777
* +-------------------------+ +--------------------------------+
1778
* | Float Component | | Unused | 8 bit stencil |
1779
* +-------------------------+ +--------------------------------+
1780
* lower 4 bytes higher 4 bytes
1783
_mesa_unpack_float_32_uint_24_8_depth_stencil_row(mesa_format format, uint32_t n,
1784
const void *src, uint32_t *dst)
1787
case MESA_FORMAT_S8_UINT_Z24_UNORM:
1788
unpack_float_32_uint_24_8_S8_UINT_Z24_UNORM(src, dst, n);
1790
case MESA_FORMAT_Z24_UNORM_S8_UINT:
1791
unpack_float_32_uint_24_8_Z24_UNORM_S8_UINT(src, dst, n);
1793
case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
1794
unpack_float_32_uint_24_8_Z32_FLOAT_S8X24_UINT(src, dst, n);
1797
unreachable("bad format %s in _mesa_unpack_uint_24_8_depth_stencil_row");