2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 #include "mfeatures.h"
44 * Normally, BYTE_TO_FLOAT(0) returns 0.00392 That causes problems when
45 * we later convert the float to a packed integer value (such as for
46 * GL_RGB5_A1) because we'll wind up with a non-zero value.
48 * We redefine the macros here so zero is handled correctly.
51 #define BYTE_TO_FLOAT(B) ((B) == 0 ? 0.0F : ((2.0F * (B) + 1.0F) * (1.0F/255.0F)))
54 #define SHORT_TO_FLOAT(S) ((S) == 0 ? 0.0F : ((2.0F * (S) + 1.0F) * (1.0F/65535.0F)))
58 /** Compute ceiling of integer quotient of A divided by B. */
59 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
63 * \return GL_TRUE if type is packed pixel type, GL_FALSE otherwise.
66 _mesa_type_is_packed(GLenum type
)
69 case GL_UNSIGNED_BYTE_3_3_2
:
70 case GL_UNSIGNED_BYTE_2_3_3_REV
:
71 case MESA_UNSIGNED_BYTE_4_4
:
72 case GL_UNSIGNED_SHORT_5_6_5
:
73 case GL_UNSIGNED_SHORT_5_6_5_REV
:
74 case GL_UNSIGNED_SHORT_4_4_4_4
:
75 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
76 case GL_UNSIGNED_SHORT_5_5_5_1
:
77 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
78 case GL_UNSIGNED_INT_8_8_8_8
:
79 case GL_UNSIGNED_INT_8_8_8_8_REV
:
80 case GL_UNSIGNED_INT_10_10_10_2
:
81 case GL_UNSIGNED_INT_2_10_10_10_REV
:
82 case GL_UNSIGNED_SHORT_8_8_MESA
:
83 case GL_UNSIGNED_SHORT_8_8_REV_MESA
:
84 case GL_UNSIGNED_INT_24_8_EXT
:
85 case GL_UNSIGNED_INT_5_9_9_9_REV
:
95 * Flip the order of the 2 bytes in each word in the given array.
98 * \param n number of words.
101 _mesa_swap2( GLushort
*p
, GLuint n
)
104 for (i
= 0; i
< n
; i
++) {
105 p
[i
] = (p
[i
] >> 8) | ((p
[i
] << 8) & 0xff00);
112 * Flip the order of the 4 bytes in each word in the given array.
115 _mesa_swap4( GLuint
*p
, GLuint n
)
118 for (i
= 0; i
< n
; i
++) {
121 | ((b
>> 8) & 0xff00)
122 | ((b
<< 8) & 0xff0000)
123 | ((b
<< 24) & 0xff000000);
130 * Get the size of a GL data type.
132 * \param type GL data type.
134 * \return the size, in bytes, of the given data type, 0 if a GL_BITMAP, or -1
135 * if an invalid type enum.
138 _mesa_sizeof_type( GLenum type
)
143 case GL_UNSIGNED_BYTE
:
144 return sizeof(GLubyte
);
146 return sizeof(GLbyte
);
147 case GL_UNSIGNED_SHORT
:
148 return sizeof(GLushort
);
150 return sizeof(GLshort
);
151 case GL_UNSIGNED_INT
:
152 return sizeof(GLuint
);
154 return sizeof(GLint
);
156 return sizeof(GLfloat
);
158 return sizeof(GLdouble
);
159 case GL_HALF_FLOAT_ARB
:
160 return sizeof(GLhalfARB
);
162 return sizeof(GLfixed
);
170 * Same as _mesa_sizeof_type() but also accepting the packed pixel
174 _mesa_sizeof_packed_type( GLenum type
)
179 case GL_UNSIGNED_BYTE
:
180 return sizeof(GLubyte
);
182 return sizeof(GLbyte
);
183 case GL_UNSIGNED_SHORT
:
184 return sizeof(GLushort
);
186 return sizeof(GLshort
);
187 case GL_UNSIGNED_INT
:
188 return sizeof(GLuint
);
190 return sizeof(GLint
);
191 case GL_HALF_FLOAT_ARB
:
192 return sizeof(GLhalfARB
);
194 return sizeof(GLfloat
);
195 case GL_UNSIGNED_BYTE_3_3_2
:
196 return sizeof(GLubyte
);
197 case GL_UNSIGNED_BYTE_2_3_3_REV
:
198 return sizeof(GLubyte
);
199 case MESA_UNSIGNED_BYTE_4_4
:
200 return sizeof(GLubyte
);
201 case GL_UNSIGNED_SHORT_5_6_5
:
202 return sizeof(GLushort
);
203 case GL_UNSIGNED_SHORT_5_6_5_REV
:
204 return sizeof(GLushort
);
205 case GL_UNSIGNED_SHORT_4_4_4_4
:
206 return sizeof(GLushort
);
207 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
208 return sizeof(GLushort
);
209 case GL_UNSIGNED_SHORT_5_5_5_1
:
210 return sizeof(GLushort
);
211 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
212 return sizeof(GLushort
);
213 case GL_UNSIGNED_INT_8_8_8_8
:
214 return sizeof(GLuint
);
215 case GL_UNSIGNED_INT_8_8_8_8_REV
:
216 return sizeof(GLuint
);
217 case GL_UNSIGNED_INT_10_10_10_2
:
218 return sizeof(GLuint
);
219 case GL_UNSIGNED_INT_2_10_10_10_REV
:
220 return sizeof(GLuint
);
221 case GL_UNSIGNED_SHORT_8_8_MESA
:
222 case GL_UNSIGNED_SHORT_8_8_REV_MESA
:
223 return sizeof(GLushort
);
224 case GL_UNSIGNED_INT_24_8_EXT
:
225 return sizeof(GLuint
);
226 case GL_UNSIGNED_INT_5_9_9_9_REV
:
227 return sizeof(GLuint
);
235 * Get the number of components in a pixel format.
237 * \param format pixel format.
239 * \return the number of components in the given format, or -1 if a bad format.
242 _mesa_components_in_format( GLenum format
)
246 case GL_COLOR_INDEX1_EXT
:
247 case GL_COLOR_INDEX2_EXT
:
248 case GL_COLOR_INDEX4_EXT
:
249 case GL_COLOR_INDEX8_EXT
:
250 case GL_COLOR_INDEX12_EXT
:
251 case GL_COLOR_INDEX16_EXT
:
252 case GL_STENCIL_INDEX
:
253 case GL_DEPTH_COMPONENT
:
255 case GL_RED_INTEGER_EXT
:
257 case GL_GREEN_INTEGER_EXT
:
259 case GL_BLUE_INTEGER_EXT
:
261 case GL_ALPHA_INTEGER_EXT
:
263 case GL_LUMINANCE_INTEGER_EXT
:
266 case GL_LUMINANCE_ALPHA
:
267 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
271 case GL_RGB_INTEGER_EXT
:
274 case GL_RGBA_INTEGER_EXT
:
284 case GL_DEPTH_STENCIL_EXT
:
296 * Get the bytes per pixel of pixel format type pair.
298 * \param format pixel format.
299 * \param type pixel type.
301 * \return bytes per pixel, or -1 if a bad format or type was given.
304 _mesa_bytes_per_pixel( GLenum format
, GLenum type
)
306 GLint comps
= _mesa_components_in_format( format
);
312 return 0; /* special case */
314 case GL_UNSIGNED_BYTE
:
315 return comps
* sizeof(GLubyte
);
317 case GL_UNSIGNED_SHORT
:
318 return comps
* sizeof(GLshort
);
320 case GL_UNSIGNED_INT
:
321 return comps
* sizeof(GLint
);
323 return comps
* sizeof(GLfloat
);
324 case GL_HALF_FLOAT_ARB
:
325 return comps
* sizeof(GLhalfARB
);
326 case GL_UNSIGNED_BYTE_3_3_2
:
327 case GL_UNSIGNED_BYTE_2_3_3_REV
:
328 if (format
== GL_RGB
|| format
== GL_BGR
||
329 format
== GL_RGB_INTEGER_EXT
|| format
== GL_BGR_INTEGER_EXT
)
330 return sizeof(GLubyte
);
332 return -1; /* error */
333 case GL_UNSIGNED_SHORT_5_6_5
:
334 case GL_UNSIGNED_SHORT_5_6_5_REV
:
335 if (format
== GL_RGB
|| format
== GL_BGR
||
336 format
== GL_RGB_INTEGER_EXT
|| format
== GL_BGR_INTEGER_EXT
)
337 return sizeof(GLushort
);
339 return -1; /* error */
340 case GL_UNSIGNED_SHORT_4_4_4_4
:
341 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
342 case GL_UNSIGNED_SHORT_5_5_5_1
:
343 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
344 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
||
345 format
== GL_RGBA_INTEGER_EXT
|| format
== GL_BGRA_INTEGER_EXT
)
346 return sizeof(GLushort
);
349 case GL_UNSIGNED_INT_8_8_8_8
:
350 case GL_UNSIGNED_INT_8_8_8_8_REV
:
351 case GL_UNSIGNED_INT_10_10_10_2
:
352 case GL_UNSIGNED_INT_2_10_10_10_REV
:
353 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
||
354 format
== GL_RGBA_INTEGER_EXT
|| format
== GL_BGRA_INTEGER_EXT
)
355 return sizeof(GLuint
);
358 case GL_UNSIGNED_SHORT_8_8_MESA
:
359 case GL_UNSIGNED_SHORT_8_8_REV_MESA
:
360 if (format
== GL_YCBCR_MESA
)
361 return sizeof(GLushort
);
364 case GL_UNSIGNED_INT_24_8_EXT
:
365 if (format
== GL_DEPTH_STENCIL_EXT
)
366 return sizeof(GLuint
);
369 case GL_UNSIGNED_INT_5_9_9_9_REV
:
370 if (format
== GL_RGB
)
371 return sizeof(GLuint
);
381 * Test for a legal pixel format and type.
383 * \param format pixel format.
384 * \param type pixel type.
386 * \return GL_TRUE if the given pixel format and type are legal, or GL_FALSE
390 _mesa_is_legal_format_and_type(const struct gl_context
*ctx
,
391 GLenum format
, GLenum type
)
395 case GL_STENCIL_INDEX
:
399 case GL_UNSIGNED_BYTE
:
401 case GL_UNSIGNED_SHORT
:
403 case GL_UNSIGNED_INT
:
406 case GL_HALF_FLOAT_ARB
:
407 return ctx
->Extensions
.ARB_half_float_pixel
;
415 #if 0 /* not legal! see table 3.6 of the 1.5 spec */
419 case GL_LUMINANCE_ALPHA
:
420 case GL_DEPTH_COMPONENT
:
423 case GL_UNSIGNED_BYTE
:
425 case GL_UNSIGNED_SHORT
:
427 case GL_UNSIGNED_INT
:
430 case GL_HALF_FLOAT_ARB
:
431 return ctx
->Extensions
.ARB_half_float_pixel
;
436 if (!ctx
->Extensions
.ARB_texture_rg
)
441 case GL_UNSIGNED_BYTE
:
443 case GL_UNSIGNED_SHORT
:
445 case GL_UNSIGNED_INT
:
448 case GL_HALF_FLOAT_ARB
:
449 return ctx
->Extensions
.ARB_half_float_pixel
;
456 case GL_UNSIGNED_BYTE
:
458 case GL_UNSIGNED_SHORT
:
460 case GL_UNSIGNED_INT
:
462 case GL_UNSIGNED_BYTE_3_3_2
:
463 case GL_UNSIGNED_BYTE_2_3_3_REV
:
464 case GL_UNSIGNED_SHORT_5_6_5
:
465 case GL_UNSIGNED_SHORT_5_6_5_REV
:
467 case GL_HALF_FLOAT_ARB
:
468 return ctx
->Extensions
.ARB_half_float_pixel
;
469 case GL_UNSIGNED_INT_5_9_9_9_REV
:
470 return ctx
->Extensions
.EXT_texture_shared_exponent
;
476 /* NOTE: no packed types are supported with BGR. That's
477 * intentional, according to the GL spec.
480 case GL_UNSIGNED_BYTE
:
482 case GL_UNSIGNED_SHORT
:
484 case GL_UNSIGNED_INT
:
487 case GL_HALF_FLOAT_ARB
:
488 return ctx
->Extensions
.ARB_half_float_pixel
;
497 case GL_UNSIGNED_BYTE
:
499 case GL_UNSIGNED_SHORT
:
501 case GL_UNSIGNED_INT
:
503 case GL_UNSIGNED_SHORT_4_4_4_4
:
504 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
505 case GL_UNSIGNED_SHORT_5_5_5_1
:
506 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
507 case GL_UNSIGNED_INT_8_8_8_8
:
508 case GL_UNSIGNED_INT_8_8_8_8_REV
:
509 case GL_UNSIGNED_INT_10_10_10_2
:
510 case GL_UNSIGNED_INT_2_10_10_10_REV
:
512 case GL_HALF_FLOAT_ARB
:
513 return ctx
->Extensions
.ARB_half_float_pixel
;
518 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
||
519 type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
)
523 case GL_DEPTH_STENCIL_EXT
:
524 if (ctx
->Extensions
.EXT_packed_depth_stencil
525 && type
== GL_UNSIGNED_INT_24_8_EXT
)
533 case GL_UNSIGNED_BYTE
:
535 case GL_UNSIGNED_SHORT
:
537 case GL_UNSIGNED_INT
:
544 /* integer-valued formats */
545 case GL_RED_INTEGER_EXT
:
546 case GL_GREEN_INTEGER_EXT
:
547 case GL_BLUE_INTEGER_EXT
:
548 case GL_ALPHA_INTEGER_EXT
:
551 case GL_UNSIGNED_BYTE
:
553 case GL_UNSIGNED_SHORT
:
555 case GL_UNSIGNED_INT
:
556 return ctx
->Extensions
.EXT_texture_integer
;
561 case GL_RGB_INTEGER_EXT
:
564 case GL_UNSIGNED_BYTE
:
566 case GL_UNSIGNED_SHORT
:
568 case GL_UNSIGNED_INT
:
569 case GL_UNSIGNED_BYTE_3_3_2
:
570 case GL_UNSIGNED_BYTE_2_3_3_REV
:
571 case GL_UNSIGNED_SHORT_5_6_5
:
572 case GL_UNSIGNED_SHORT_5_6_5_REV
:
573 return ctx
->Extensions
.EXT_texture_integer
;
578 case GL_BGR_INTEGER_EXT
:
581 case GL_UNSIGNED_BYTE
:
583 case GL_UNSIGNED_SHORT
:
585 case GL_UNSIGNED_INT
:
586 /* NOTE: no packed formats w/ BGR format */
587 return ctx
->Extensions
.EXT_texture_integer
;
592 case GL_RGBA_INTEGER_EXT
:
593 case GL_BGRA_INTEGER_EXT
:
596 case GL_UNSIGNED_BYTE
:
598 case GL_UNSIGNED_SHORT
:
600 case GL_UNSIGNED_INT
:
601 case GL_UNSIGNED_SHORT_4_4_4_4
:
602 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
603 case GL_UNSIGNED_SHORT_5_5_5_1
:
604 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
605 case GL_UNSIGNED_INT_8_8_8_8
:
606 case GL_UNSIGNED_INT_8_8_8_8_REV
:
607 case GL_UNSIGNED_INT_10_10_10_2
:
608 case GL_UNSIGNED_INT_2_10_10_10_REV
:
609 return ctx
->Extensions
.EXT_texture_integer
;
614 case GL_LUMINANCE_INTEGER_EXT
:
615 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
618 case GL_UNSIGNED_BYTE
:
620 case GL_UNSIGNED_SHORT
:
622 case GL_UNSIGNED_INT
:
623 return ctx
->Extensions
.EXT_texture_integer
;
636 * Test if the given image format is a color/RGBA format (i.e., not color
637 * index, depth, stencil, etc).
638 * \param format the image format value (may by an internal texture format)
639 * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
642 _mesa_is_color_format(GLenum format
)
660 case GL_LUMINANCE_ALPHA
:
661 case GL_LUMINANCE4_ALPHA4
:
662 case GL_LUMINANCE6_ALPHA2
:
663 case GL_LUMINANCE8_ALPHA8
:
664 case GL_LUMINANCE12_ALPHA4
:
665 case GL_LUMINANCE12_ALPHA12
:
666 case GL_LUMINANCE16_ALPHA16
:
698 /* float texture formats */
699 case GL_ALPHA16F_ARB
:
700 case GL_ALPHA32F_ARB
:
701 case GL_LUMINANCE16F_ARB
:
702 case GL_LUMINANCE32F_ARB
:
703 case GL_LUMINANCE_ALPHA16F_ARB
:
704 case GL_LUMINANCE_ALPHA32F_ARB
:
705 case GL_INTENSITY16F_ARB
:
706 case GL_INTENSITY32F_ARB
:
715 /* compressed formats */
716 case GL_COMPRESSED_ALPHA
:
717 case GL_COMPRESSED_LUMINANCE
:
718 case GL_COMPRESSED_LUMINANCE_ALPHA
:
719 case GL_COMPRESSED_INTENSITY
:
720 case GL_COMPRESSED_RED
:
721 case GL_COMPRESSED_RG
:
722 case GL_COMPRESSED_RGB
:
723 case GL_COMPRESSED_RGBA
:
728 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
729 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
730 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
731 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
732 case GL_COMPRESSED_RGB_FXT1_3DFX
:
733 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
734 #if FEATURE_EXT_texture_sRGB
737 case GL_SRGB_ALPHA_EXT
:
738 case GL_SRGB8_ALPHA8_EXT
:
739 case GL_SLUMINANCE_ALPHA_EXT
:
740 case GL_SLUMINANCE8_ALPHA8_EXT
:
741 case GL_SLUMINANCE_EXT
:
742 case GL_SLUMINANCE8_EXT
:
743 case GL_COMPRESSED_SRGB_EXT
:
744 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
745 case GL_COMPRESSED_SRGB_ALPHA_EXT
:
746 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
747 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
748 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
749 case GL_COMPRESSED_SLUMINANCE_EXT
:
750 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT
:
751 #endif /* FEATURE_EXT_texture_sRGB */
752 case GL_COMPRESSED_RED_RGTC1
:
753 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
754 case GL_COMPRESSED_RG_RGTC2
:
755 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
756 case GL_COMPRESSED_LUMINANCE_LATC1_EXT
:
757 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT
:
758 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT
:
759 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT
:
760 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI
:
761 /* generic integer formats */
762 case GL_RED_INTEGER_EXT
:
763 case GL_GREEN_INTEGER_EXT
:
764 case GL_BLUE_INTEGER_EXT
:
765 case GL_ALPHA_INTEGER_EXT
:
766 case GL_RGB_INTEGER_EXT
:
767 case GL_RGBA_INTEGER_EXT
:
768 case GL_BGR_INTEGER_EXT
:
769 case GL_BGRA_INTEGER_EXT
:
770 case GL_LUMINANCE_INTEGER_EXT
:
771 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
772 /* sized integer formats */
773 case GL_RGBA32UI_EXT
:
775 case GL_ALPHA32UI_EXT
:
776 case GL_INTENSITY32UI_EXT
:
777 case GL_LUMINANCE32UI_EXT
:
778 case GL_LUMINANCE_ALPHA32UI_EXT
:
779 case GL_RGBA16UI_EXT
:
781 case GL_ALPHA16UI_EXT
:
782 case GL_INTENSITY16UI_EXT
:
783 case GL_LUMINANCE16UI_EXT
:
784 case GL_LUMINANCE_ALPHA16UI_EXT
:
787 case GL_ALPHA8UI_EXT
:
788 case GL_INTENSITY8UI_EXT
:
789 case GL_LUMINANCE8UI_EXT
:
790 case GL_LUMINANCE_ALPHA8UI_EXT
:
793 case GL_ALPHA32I_EXT
:
794 case GL_INTENSITY32I_EXT
:
795 case GL_LUMINANCE32I_EXT
:
796 case GL_LUMINANCE_ALPHA32I_EXT
:
799 case GL_ALPHA16I_EXT
:
800 case GL_INTENSITY16I_EXT
:
801 case GL_LUMINANCE16I_EXT
:
802 case GL_LUMINANCE_ALPHA16I_EXT
:
806 case GL_INTENSITY8I_EXT
:
807 case GL_LUMINANCE8I_EXT
:
808 case GL_LUMINANCE_ALPHA8I_EXT
:
809 /* signed, normalized texture formats */
821 case GL_RGBA16_SNORM
:
823 case GL_ALPHA8_SNORM
:
824 case GL_ALPHA16_SNORM
:
825 case GL_LUMINANCE_SNORM
:
826 case GL_LUMINANCE8_SNORM
:
827 case GL_LUMINANCE16_SNORM
:
828 case GL_LUMINANCE_ALPHA_SNORM
:
829 case GL_LUMINANCE8_ALPHA8_SNORM
:
830 case GL_LUMINANCE16_ALPHA16_SNORM
:
831 case GL_INTENSITY_SNORM
:
832 case GL_INTENSITY8_SNORM
:
833 case GL_INTENSITY16_SNORM
:
836 case GL_YCBCR_MESA
: /* not considered to be RGB */
845 * Test if the given image format is a color index format.
848 _mesa_is_index_format(GLenum format
)
852 case GL_COLOR_INDEX1_EXT
:
853 case GL_COLOR_INDEX2_EXT
:
854 case GL_COLOR_INDEX4_EXT
:
855 case GL_COLOR_INDEX8_EXT
:
856 case GL_COLOR_INDEX12_EXT
:
857 case GL_COLOR_INDEX16_EXT
:
866 * Test if the given image format is a depth component format.
869 _mesa_is_depth_format(GLenum format
)
872 case GL_DEPTH_COMPONENT
:
873 case GL_DEPTH_COMPONENT16
:
874 case GL_DEPTH_COMPONENT24
:
875 case GL_DEPTH_COMPONENT32
:
884 * Test if the given image format is a stencil format.
887 _mesa_is_stencil_format(GLenum format
)
890 case GL_STENCIL_INDEX
:
891 case GL_DEPTH_STENCIL
:
900 * Test if the given image format is a YCbCr format.
903 _mesa_is_ycbcr_format(GLenum format
)
915 * Test if the given image format is a depth+stencil format.
918 _mesa_is_depthstencil_format(GLenum format
)
921 case GL_DEPTH24_STENCIL8_EXT
:
922 case GL_DEPTH_STENCIL_EXT
:
931 * Test if the given image format is a depth or stencil format.
934 _mesa_is_depth_or_stencil_format(GLenum format
)
937 case GL_DEPTH_COMPONENT
:
938 case GL_DEPTH_COMPONENT16
:
939 case GL_DEPTH_COMPONENT24
:
940 case GL_DEPTH_COMPONENT32
:
941 case GL_STENCIL_INDEX
:
942 case GL_STENCIL_INDEX1_EXT
:
943 case GL_STENCIL_INDEX4_EXT
:
944 case GL_STENCIL_INDEX8_EXT
:
945 case GL_STENCIL_INDEX16_EXT
:
946 case GL_DEPTH_STENCIL_EXT
:
947 case GL_DEPTH24_STENCIL8_EXT
:
956 * Test if the given image format is a dudv format.
959 _mesa_is_dudv_format(GLenum format
)
972 * Test if the given format is an integer (non-normalized) format.
975 _mesa_is_integer_format(GLenum format
)
978 /* generic integer formats */
979 case GL_RED_INTEGER_EXT
:
980 case GL_GREEN_INTEGER_EXT
:
981 case GL_BLUE_INTEGER_EXT
:
982 case GL_ALPHA_INTEGER_EXT
:
983 case GL_RGB_INTEGER_EXT
:
984 case GL_RGBA_INTEGER_EXT
:
985 case GL_BGR_INTEGER_EXT
:
986 case GL_BGRA_INTEGER_EXT
:
987 case GL_LUMINANCE_INTEGER_EXT
:
988 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
989 /* specific integer formats */
990 case GL_RGBA32UI_EXT
:
992 case GL_ALPHA32UI_EXT
:
993 case GL_INTENSITY32UI_EXT
:
994 case GL_LUMINANCE32UI_EXT
:
995 case GL_LUMINANCE_ALPHA32UI_EXT
:
996 case GL_RGBA16UI_EXT
:
998 case GL_ALPHA16UI_EXT
:
999 case GL_INTENSITY16UI_EXT
:
1000 case GL_LUMINANCE16UI_EXT
:
1001 case GL_LUMINANCE_ALPHA16UI_EXT
:
1002 case GL_RGBA8UI_EXT
:
1004 case GL_ALPHA8UI_EXT
:
1005 case GL_INTENSITY8UI_EXT
:
1006 case GL_LUMINANCE8UI_EXT
:
1007 case GL_LUMINANCE_ALPHA8UI_EXT
:
1008 case GL_RGBA32I_EXT
:
1010 case GL_ALPHA32I_EXT
:
1011 case GL_INTENSITY32I_EXT
:
1012 case GL_LUMINANCE32I_EXT
:
1013 case GL_LUMINANCE_ALPHA32I_EXT
:
1014 case GL_RGBA16I_EXT
:
1016 case GL_ALPHA16I_EXT
:
1017 case GL_INTENSITY16I_EXT
:
1018 case GL_LUMINANCE16I_EXT
:
1019 case GL_LUMINANCE_ALPHA16I_EXT
:
1022 case GL_ALPHA8I_EXT
:
1023 case GL_INTENSITY8I_EXT
:
1024 case GL_LUMINANCE8I_EXT
:
1025 case GL_LUMINANCE_ALPHA8I_EXT
:
1034 * Test if an image format is a supported compressed format.
1035 * \param format the internal format token provided by the user.
1036 * \return GL_TRUE if compressed, GL_FALSE if uncompressed
1039 _mesa_is_compressed_format(struct gl_context
*ctx
, GLenum format
)
1042 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
1043 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
1044 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
1045 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
1046 return ctx
->Extensions
.EXT_texture_compression_s3tc
;
1051 return ctx
->Extensions
.S3_s3tc
;
1052 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
1053 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
1054 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
1055 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
1056 return ctx
->Extensions
.EXT_texture_sRGB
1057 && ctx
->Extensions
.EXT_texture_compression_s3tc
;
1058 case GL_COMPRESSED_RGB_FXT1_3DFX
:
1059 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
1060 return ctx
->Extensions
.TDFX_texture_compression_FXT1
;
1061 case GL_COMPRESSED_RED_RGTC1
:
1062 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
1063 case GL_COMPRESSED_RG_RGTC2
:
1064 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
1065 return ctx
->Extensions
.ARB_texture_compression_rgtc
;
1066 case GL_COMPRESSED_LUMINANCE_LATC1_EXT
:
1067 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT
:
1068 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT
:
1069 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT
:
1070 return ctx
->Extensions
.EXT_texture_compression_latc
;
1071 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI
:
1072 return ctx
->Extensions
.ATI_texture_compression_3dc
;
1080 * Return the address of a specific pixel in an image (1D, 2D or 3D).
1082 * Pixel unpacking/packing parameters are observed according to \p packing.
1084 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
1085 * \param image starting address of image data
1086 * \param width the image width
1087 * \param height theimage height
1088 * \param format the pixel format
1089 * \param type the pixel data type
1090 * \param packing the pixelstore attributes
1091 * \param img which image in the volume (0 for 1D or 2D images)
1092 * \param row row of pixel in the image (0 for 1D images)
1093 * \param column column of pixel in the image
1095 * \return address of pixel on success, or NULL on error.
1097 * \sa gl_pixelstore_attrib.
1100 _mesa_image_address( GLuint dimensions
,
1101 const struct gl_pixelstore_attrib
*packing
,
1102 const GLvoid
*image
,
1103 GLsizei width
, GLsizei height
,
1104 GLenum format
, GLenum type
,
1105 GLint img
, GLint row
, GLint column
)
1107 GLint alignment
; /* 1, 2 or 4 */
1108 GLint pixels_per_row
;
1109 GLint rows_per_image
;
1112 GLint skipimages
; /* for 3-D volume images */
1113 GLubyte
*pixel_addr
;
1115 ASSERT(dimensions
>= 1 && dimensions
<= 3);
1117 alignment
= packing
->Alignment
;
1118 if (packing
->RowLength
> 0) {
1119 pixels_per_row
= packing
->RowLength
;
1122 pixels_per_row
= width
;
1124 if (packing
->ImageHeight
> 0) {
1125 rows_per_image
= packing
->ImageHeight
;
1128 rows_per_image
= height
;
1131 skippixels
= packing
->SkipPixels
;
1132 /* Note: SKIP_ROWS _is_ used for 1D images */
1133 skiprows
= packing
->SkipRows
;
1134 /* Note: SKIP_IMAGES is only used for 3D images */
1135 skipimages
= (dimensions
== 3) ? packing
->SkipImages
: 0;
1137 if (type
== GL_BITMAP
) {
1139 GLint comp_per_pixel
; /* components per pixel */
1140 GLint bytes_per_comp
; /* bytes per component */
1141 GLint bytes_per_row
;
1142 GLint bytes_per_image
;
1144 /* Compute bytes per component */
1145 bytes_per_comp
= _mesa_sizeof_packed_type( type
);
1146 if (bytes_per_comp
< 0) {
1150 /* Compute number of components per pixel */
1151 comp_per_pixel
= _mesa_components_in_format( format
);
1152 if (comp_per_pixel
< 0) {
1156 bytes_per_row
= alignment
1157 * CEILING( comp_per_pixel
*pixels_per_row
, 8*alignment
);
1159 bytes_per_image
= bytes_per_row
* rows_per_image
;
1161 pixel_addr
= (GLubyte
*) image
1162 + (skipimages
+ img
) * bytes_per_image
1163 + (skiprows
+ row
) * bytes_per_row
1164 + (skippixels
+ column
) / 8;
1167 /* Non-BITMAP data */
1168 GLint bytes_per_pixel
, bytes_per_row
, remainder
, bytes_per_image
;
1171 bytes_per_pixel
= _mesa_bytes_per_pixel( format
, type
);
1173 /* The pixel type and format should have been error checked earlier */
1174 assert(bytes_per_pixel
> 0);
1176 bytes_per_row
= pixels_per_row
* bytes_per_pixel
;
1177 remainder
= bytes_per_row
% alignment
;
1179 bytes_per_row
+= (alignment
- remainder
);
1181 ASSERT(bytes_per_row
% alignment
== 0);
1183 bytes_per_image
= bytes_per_row
* rows_per_image
;
1185 if (packing
->Invert
) {
1186 /* set pixel_addr to the last row */
1187 topOfImage
= bytes_per_row
* (height
- 1);
1188 bytes_per_row
= -bytes_per_row
;
1194 /* compute final pixel address */
1195 pixel_addr
= (GLubyte
*) image
1196 + (skipimages
+ img
) * bytes_per_image
1198 + (skiprows
+ row
) * bytes_per_row
1199 + (skippixels
+ column
) * bytes_per_pixel
;
1202 return (GLvoid
*) pixel_addr
;
1207 _mesa_image_address1d( const struct gl_pixelstore_attrib
*packing
,
1208 const GLvoid
*image
,
1210 GLenum format
, GLenum type
,
1213 return _mesa_image_address(1, packing
, image
, width
, 1,
1214 format
, type
, 0, 0, column
);
1219 _mesa_image_address2d( const struct gl_pixelstore_attrib
*packing
,
1220 const GLvoid
*image
,
1221 GLsizei width
, GLsizei height
,
1222 GLenum format
, GLenum type
,
1223 GLint row
, GLint column
)
1225 return _mesa_image_address(2, packing
, image
, width
, height
,
1226 format
, type
, 0, row
, column
);
1231 _mesa_image_address3d( const struct gl_pixelstore_attrib
*packing
,
1232 const GLvoid
*image
,
1233 GLsizei width
, GLsizei height
,
1234 GLenum format
, GLenum type
,
1235 GLint img
, GLint row
, GLint column
)
1237 return _mesa_image_address(3, packing
, image
, width
, height
,
1238 format
, type
, img
, row
, column
);
1244 * Compute the stride (in bytes) between image rows.
1246 * \param packing the pixelstore attributes
1247 * \param width image width.
1248 * \param format pixel format.
1249 * \param type pixel data type.
1251 * \return the stride in bytes for the given parameters, or -1 if error
1254 _mesa_image_row_stride( const struct gl_pixelstore_attrib
*packing
,
1255 GLint width
, GLenum format
, GLenum type
)
1257 GLint bytesPerRow
, remainder
;
1261 if (type
== GL_BITMAP
) {
1262 if (packing
->RowLength
== 0) {
1263 bytesPerRow
= (width
+ 7) / 8;
1266 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
1270 /* Non-BITMAP data */
1271 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1272 if (bytesPerPixel
<= 0)
1273 return -1; /* error */
1274 if (packing
->RowLength
== 0) {
1275 bytesPerRow
= bytesPerPixel
* width
;
1278 bytesPerRow
= bytesPerPixel
* packing
->RowLength
;
1282 remainder
= bytesPerRow
% packing
->Alignment
;
1283 if (remainder
> 0) {
1284 bytesPerRow
+= (packing
->Alignment
- remainder
);
1287 if (packing
->Invert
) {
1288 /* negate the bytes per row (negative row stride) */
1289 bytesPerRow
= -bytesPerRow
;
1297 * Compute the stride between images in a 3D texture (in bytes) for the given
1298 * pixel packing parameters and image width, format and type.
1301 _mesa_image_image_stride( const struct gl_pixelstore_attrib
*packing
,
1302 GLint width
, GLint height
,
1303 GLenum format
, GLenum type
)
1305 GLint bytesPerRow
, bytesPerImage
, remainder
;
1309 if (type
== GL_BITMAP
) {
1310 if (packing
->RowLength
== 0) {
1311 bytesPerRow
= (width
+ 7) / 8;
1314 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
1318 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1320 if (bytesPerPixel
<= 0)
1321 return -1; /* error */
1322 if (packing
->RowLength
== 0) {
1323 bytesPerRow
= bytesPerPixel
* width
;
1326 bytesPerRow
= bytesPerPixel
* packing
->RowLength
;
1330 remainder
= bytesPerRow
% packing
->Alignment
;
1332 bytesPerRow
+= (packing
->Alignment
- remainder
);
1334 if (packing
->ImageHeight
== 0)
1335 bytesPerImage
= bytesPerRow
* height
;
1337 bytesPerImage
= bytesPerRow
* packing
->ImageHeight
;
1339 return bytesPerImage
;
1345 * "Expand" a bitmap from 1-bit per pixel to 8-bits per pixel.
1346 * This is typically used to convert a bitmap into a GLubyte/pixel texture.
1347 * "On" bits will set texels to \p onValue.
1348 * "Off" bits will not modify texels.
1349 * \param width src bitmap width in pixels
1350 * \param height src bitmap height in pixels
1351 * \param unpack bitmap unpacking state
1352 * \param bitmap the src bitmap data
1353 * \param destBuffer start of dest buffer
1354 * \param destStride row stride in dest buffer
1355 * \param onValue if bit is 1, set destBuffer pixel to this value
1358 _mesa_expand_bitmap(GLsizei width
, GLsizei height
,
1359 const struct gl_pixelstore_attrib
*unpack
,
1360 const GLubyte
*bitmap
,
1361 GLubyte
*destBuffer
, GLint destStride
,
1364 const GLubyte
*srcRow
= (const GLubyte
*)
1365 _mesa_image_address2d(unpack
, bitmap
, width
, height
,
1366 GL_COLOR_INDEX
, GL_BITMAP
, 0, 0);
1367 const GLint srcStride
= _mesa_image_row_stride(unpack
, width
,
1368 GL_COLOR_INDEX
, GL_BITMAP
);
1371 #define SET_PIXEL(COL, ROW) \
1372 destBuffer[(ROW) * destStride + (COL)] = onValue;
1374 for (row
= 0; row
< height
; row
++) {
1375 const GLubyte
*src
= srcRow
;
1377 if (unpack
->LsbFirst
) {
1379 GLubyte mask
= 1U << (unpack
->SkipPixels
& 0x7);
1380 for (col
= 0; col
< width
; col
++) {
1383 SET_PIXEL(col
, row
);
1395 /* get ready for next row */
1401 GLubyte mask
= 128U >> (unpack
->SkipPixels
& 0x7);
1402 for (col
= 0; col
< width
; col
++) {
1405 SET_PIXEL(col
, row
);
1417 /* get ready for next row */
1422 srcRow
+= srcStride
;
1432 * Convert an array of RGBA colors from one datatype to another.
1433 * NOTE: src may equal dst. In that case, we use a temporary buffer.
1436 _mesa_convert_colors(GLenum srcType
, const GLvoid
*src
,
1437 GLenum dstType
, GLvoid
*dst
,
1438 GLuint count
, const GLubyte mask
[])
1440 GLuint tempBuffer
[MAX_WIDTH
][4];
1441 const GLboolean useTemp
= (src
== dst
);
1443 ASSERT(srcType
!= dstType
);
1446 case GL_UNSIGNED_BYTE
:
1447 if (dstType
== GL_UNSIGNED_SHORT
) {
1448 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
1449 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
1451 for (i
= 0; i
< count
; i
++) {
1452 if (!mask
|| mask
[i
]) {
1453 dst2
[i
][RCOMP
] = UBYTE_TO_USHORT(src1
[i
][RCOMP
]);
1454 dst2
[i
][GCOMP
] = UBYTE_TO_USHORT(src1
[i
][GCOMP
]);
1455 dst2
[i
][BCOMP
] = UBYTE_TO_USHORT(src1
[i
][BCOMP
]);
1456 dst2
[i
][ACOMP
] = UBYTE_TO_USHORT(src1
[i
][ACOMP
]);
1460 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
1463 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
1464 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
1466 ASSERT(dstType
== GL_FLOAT
);
1467 for (i
= 0; i
< count
; i
++) {
1468 if (!mask
|| mask
[i
]) {
1469 dst4
[i
][RCOMP
] = UBYTE_TO_FLOAT(src1
[i
][RCOMP
]);
1470 dst4
[i
][GCOMP
] = UBYTE_TO_FLOAT(src1
[i
][GCOMP
]);
1471 dst4
[i
][BCOMP
] = UBYTE_TO_FLOAT(src1
[i
][BCOMP
]);
1472 dst4
[i
][ACOMP
] = UBYTE_TO_FLOAT(src1
[i
][ACOMP
]);
1476 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
1479 case GL_UNSIGNED_SHORT
:
1480 if (dstType
== GL_UNSIGNED_BYTE
) {
1481 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
1482 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
1484 for (i
= 0; i
< count
; i
++) {
1485 if (!mask
|| mask
[i
]) {
1486 dst1
[i
][RCOMP
] = USHORT_TO_UBYTE(src2
[i
][RCOMP
]);
1487 dst1
[i
][GCOMP
] = USHORT_TO_UBYTE(src2
[i
][GCOMP
]);
1488 dst1
[i
][BCOMP
] = USHORT_TO_UBYTE(src2
[i
][BCOMP
]);
1489 dst1
[i
][ACOMP
] = USHORT_TO_UBYTE(src2
[i
][ACOMP
]);
1493 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
1496 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
1497 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
1499 ASSERT(dstType
== GL_FLOAT
);
1500 for (i
= 0; i
< count
; i
++) {
1501 if (!mask
|| mask
[i
]) {
1502 dst4
[i
][RCOMP
] = USHORT_TO_FLOAT(src2
[i
][RCOMP
]);
1503 dst4
[i
][GCOMP
] = USHORT_TO_FLOAT(src2
[i
][GCOMP
]);
1504 dst4
[i
][BCOMP
] = USHORT_TO_FLOAT(src2
[i
][BCOMP
]);
1505 dst4
[i
][ACOMP
] = USHORT_TO_FLOAT(src2
[i
][ACOMP
]);
1509 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
1513 if (dstType
== GL_UNSIGNED_BYTE
) {
1514 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
1515 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
1517 for (i
= 0; i
< count
; i
++) {
1518 if (!mask
|| mask
[i
]) {
1519 UNCLAMPED_FLOAT_TO_UBYTE(dst1
[i
][RCOMP
], src4
[i
][RCOMP
]);
1520 UNCLAMPED_FLOAT_TO_UBYTE(dst1
[i
][GCOMP
], src4
[i
][GCOMP
]);
1521 UNCLAMPED_FLOAT_TO_UBYTE(dst1
[i
][BCOMP
], src4
[i
][BCOMP
]);
1522 UNCLAMPED_FLOAT_TO_UBYTE(dst1
[i
][ACOMP
], src4
[i
][ACOMP
]);
1526 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
1529 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
1530 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
1532 ASSERT(dstType
== GL_UNSIGNED_SHORT
);
1533 for (i
= 0; i
< count
; i
++) {
1534 if (!mask
|| mask
[i
]) {
1535 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][RCOMP
], src4
[i
][RCOMP
]);
1536 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][GCOMP
], src4
[i
][GCOMP
]);
1537 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][BCOMP
], src4
[i
][BCOMP
]);
1538 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][ACOMP
], src4
[i
][ACOMP
]);
1542 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
1546 _mesa_problem(NULL
, "Invalid datatype in _mesa_convert_colors");
1554 * Perform basic clipping for glDrawPixels. The image's position and size
1555 * and the unpack SkipPixels and SkipRows are adjusted so that the image
1556 * region is entirely within the window and scissor bounds.
1557 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
1558 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
1559 * we'll actually write. Beforehand, *destY-1 is the first drawing row.
1561 * \return GL_TRUE if image is ready for drawing or
1562 * GL_FALSE if image was completely clipped away (draw nothing)
1565 _mesa_clip_drawpixels(const struct gl_context
*ctx
,
1566 GLint
*destX
, GLint
*destY
,
1567 GLsizei
*width
, GLsizei
*height
,
1568 struct gl_pixelstore_attrib
*unpack
)
1570 const struct gl_framebuffer
*buffer
= ctx
->DrawBuffer
;
1572 if (unpack
->RowLength
== 0) {
1573 unpack
->RowLength
= *width
;
1576 ASSERT(ctx
->Pixel
.ZoomX
== 1.0F
);
1577 ASSERT(ctx
->Pixel
.ZoomY
== 1.0F
|| ctx
->Pixel
.ZoomY
== -1.0F
);
1580 if (*destX
< buffer
->_Xmin
) {
1581 unpack
->SkipPixels
+= (buffer
->_Xmin
- *destX
);
1582 *width
-= (buffer
->_Xmin
- *destX
);
1583 *destX
= buffer
->_Xmin
;
1585 /* right clipping */
1586 if (*destX
+ *width
> buffer
->_Xmax
)
1587 *width
-= (*destX
+ *width
- buffer
->_Xmax
);
1592 if (ctx
->Pixel
.ZoomY
== 1.0F
) {
1593 /* bottom clipping */
1594 if (*destY
< buffer
->_Ymin
) {
1595 unpack
->SkipRows
+= (buffer
->_Ymin
- *destY
);
1596 *height
-= (buffer
->_Ymin
- *destY
);
1597 *destY
= buffer
->_Ymin
;
1600 if (*destY
+ *height
> buffer
->_Ymax
)
1601 *height
-= (*destY
+ *height
- buffer
->_Ymax
);
1603 else { /* upside down */
1605 if (*destY
> buffer
->_Ymax
) {
1606 unpack
->SkipRows
+= (*destY
- buffer
->_Ymax
);
1607 *height
-= (*destY
- buffer
->_Ymax
);
1608 *destY
= buffer
->_Ymax
;
1610 /* bottom clipping */
1611 if (*destY
- *height
< buffer
->_Ymin
)
1612 *height
-= (buffer
->_Ymin
- (*destY
- *height
));
1613 /* adjust destY so it's the first row to write to */
1625 * Perform clipping for glReadPixels. The image's window position
1626 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
1627 * so that the image region is entirely within the window bounds.
1628 * Note: this is different from _mesa_clip_drawpixels() in that the
1629 * scissor box is ignored, and we use the bounds of the current readbuffer
1632 * \return GL_TRUE if region to read is in bounds
1633 * GL_FALSE if region is completely out of bounds (nothing to read)
1636 _mesa_clip_readpixels(const struct gl_context
*ctx
,
1637 GLint
*srcX
, GLint
*srcY
,
1638 GLsizei
*width
, GLsizei
*height
,
1639 struct gl_pixelstore_attrib
*pack
)
1641 const struct gl_framebuffer
*buffer
= ctx
->ReadBuffer
;
1643 if (pack
->RowLength
== 0) {
1644 pack
->RowLength
= *width
;
1649 pack
->SkipPixels
+= (0 - *srcX
);
1650 *width
-= (0 - *srcX
);
1653 /* right clipping */
1654 if (*srcX
+ *width
> (GLsizei
) buffer
->Width
)
1655 *width
-= (*srcX
+ *width
- buffer
->Width
);
1660 /* bottom clipping */
1662 pack
->SkipRows
+= (0 - *srcY
);
1663 *height
-= (0 - *srcY
);
1667 if (*srcY
+ *height
> (GLsizei
) buffer
->Height
)
1668 *height
-= (*srcY
+ *height
- buffer
->Height
);
1678 * Do clipping for a glCopyTexSubImage call.
1679 * The framebuffer source region might extend outside the framebuffer
1680 * bounds. Clip the source region against the framebuffer bounds and
1681 * adjust the texture/dest position and size accordingly.
1683 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
1686 _mesa_clip_copytexsubimage(const struct gl_context
*ctx
,
1687 GLint
*destX
, GLint
*destY
,
1688 GLint
*srcX
, GLint
*srcY
,
1689 GLsizei
*width
, GLsizei
*height
)
1691 const struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1692 const GLint srcX0
= *srcX
, srcY0
= *srcY
;
1694 if (_mesa_clip_to_region(0, 0, fb
->Width
, fb
->Height
,
1695 srcX
, srcY
, width
, height
)) {
1696 *destX
= *destX
+ *srcX
- srcX0
;
1697 *destY
= *destY
+ *srcY
- srcY0
;
1709 * Clip the rectangle defined by (x, y, width, height) against the bounds
1710 * specified by [xmin, xmax) and [ymin, ymax).
1711 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
1714 _mesa_clip_to_region(GLint xmin
, GLint ymin
,
1715 GLint xmax
, GLint ymax
,
1717 GLsizei
*width
, GLsizei
*height
)
1721 *width
-= (xmin
- *x
);
1725 /* right clipping */
1726 if (*x
+ *width
> xmax
)
1727 *width
-= (*x
+ *width
- xmax
);
1732 /* bottom (or top) clipping */
1734 *height
-= (ymin
- *y
);
1738 /* top (or bottom) clipping */
1739 if (*y
+ *height
> ymax
)
1740 *height
-= (*y
+ *height
- ymax
);
1750 * Clip dst coords against Xmax (or Ymax).
1753 clip_right_or_top(GLint
*srcX0
, GLint
*srcX1
,
1754 GLint
*dstX0
, GLint
*dstX1
,
1759 if (*dstX1
> maxValue
) {
1760 /* X1 outside right edge */
1761 ASSERT(*dstX0
< maxValue
); /* X0 should be inside right edge */
1762 t
= (GLfloat
) (maxValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
1763 /* chop off [t, 1] part */
1764 ASSERT(t
>= 0.0 && t
<= 1.0);
1766 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
1767 *srcX1
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
1769 else if (*dstX0
> maxValue
) {
1770 /* X0 outside right edge */
1771 ASSERT(*dstX1
< maxValue
); /* X1 should be inside right edge */
1772 t
= (GLfloat
) (maxValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
1773 /* chop off [t, 1] part */
1774 ASSERT(t
>= 0.0 && t
<= 1.0);
1776 bias
= (*srcX0
< *srcX1
) ? -0.5F
: 0.5F
;
1777 *srcX0
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
1783 * Clip dst coords against Xmin (or Ymin).
1786 clip_left_or_bottom(GLint
*srcX0
, GLint
*srcX1
,
1787 GLint
*dstX0
, GLint
*dstX1
,
1792 if (*dstX0
< minValue
) {
1793 /* X0 outside left edge */
1794 ASSERT(*dstX1
> minValue
); /* X1 should be inside left edge */
1795 t
= (GLfloat
) (minValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
1796 /* chop off [0, t] part */
1797 ASSERT(t
>= 0.0 && t
<= 1.0);
1799 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
; /* flipped??? */
1800 *srcX0
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
1802 else if (*dstX1
< minValue
) {
1803 /* X1 outside left edge */
1804 ASSERT(*dstX0
> minValue
); /* X0 should be inside left edge */
1805 t
= (GLfloat
) (minValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
1806 /* chop off [0, t] part */
1807 ASSERT(t
>= 0.0 && t
<= 1.0);
1809 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
1810 *srcX1
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
1816 * Do clipping of blit src/dest rectangles.
1817 * The dest rect is clipped against both the buffer bounds and scissor bounds.
1818 * The src rect is just clipped against the buffer bounds.
1820 * When either the src or dest rect is clipped, the other is also clipped
1823 * Note that X0 need not be less than X1 (same for Y) for either the source
1824 * and dest rects. That makes the clipping a little trickier.
1826 * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped
1829 _mesa_clip_blit(struct gl_context
*ctx
,
1830 GLint
*srcX0
, GLint
*srcY0
, GLint
*srcX1
, GLint
*srcY1
,
1831 GLint
*dstX0
, GLint
*dstY0
, GLint
*dstX1
, GLint
*dstY1
)
1833 const GLint srcXmin
= 0;
1834 const GLint srcXmax
= ctx
->ReadBuffer
->Width
;
1835 const GLint srcYmin
= 0;
1836 const GLint srcYmax
= ctx
->ReadBuffer
->Height
;
1838 /* these include scissor bounds */
1839 const GLint dstXmin
= ctx
->DrawBuffer
->_Xmin
;
1840 const GLint dstXmax
= ctx
->DrawBuffer
->_Xmax
;
1841 const GLint dstYmin
= ctx
->DrawBuffer
->_Ymin
;
1842 const GLint dstYmax
= ctx
->DrawBuffer
->_Ymax
;
1845 printf("PreClipX: src: %d .. %d dst: %d .. %d\n",
1846 *srcX0, *srcX1, *dstX0, *dstX1);
1847 printf("PreClipY: src: %d .. %d dst: %d .. %d\n",
1848 *srcY0, *srcY1, *dstY0, *dstY1);
1851 /* trivial rejection tests */
1852 if (*dstX0
== *dstX1
)
1853 return GL_FALSE
; /* no width */
1854 if (*dstX0
<= dstXmin
&& *dstX1
<= dstXmin
)
1855 return GL_FALSE
; /* totally out (left) of bounds */
1856 if (*dstX0
>= dstXmax
&& *dstX1
>= dstXmax
)
1857 return GL_FALSE
; /* totally out (right) of bounds */
1859 if (*dstY0
== *dstY1
)
1861 if (*dstY0
<= dstYmin
&& *dstY1
<= dstYmin
)
1863 if (*dstY0
>= dstYmax
&& *dstY1
>= dstYmax
)
1866 if (*srcX0
== *srcX1
)
1868 if (*srcX0
<= srcXmin
&& *srcX1
<= srcXmin
)
1870 if (*srcX0
>= srcXmax
&& *srcX1
>= srcXmax
)
1873 if (*srcY0
== *srcY1
)
1875 if (*srcY0
<= srcYmin
&& *srcY1
<= srcYmin
)
1877 if (*srcY0
>= srcYmax
&& *srcY1
>= srcYmax
)
1883 clip_right_or_top(srcX0
, srcX1
, dstX0
, dstX1
, dstXmax
);
1884 clip_right_or_top(srcY0
, srcY1
, dstY0
, dstY1
, dstYmax
);
1885 clip_left_or_bottom(srcX0
, srcX1
, dstX0
, dstX1
, dstXmin
);
1886 clip_left_or_bottom(srcY0
, srcY1
, dstY0
, dstY1
, dstYmin
);
1889 * src clip (just swap src/dst values from above)
1891 clip_right_or_top(dstX0
, dstX1
, srcX0
, srcX1
, srcXmax
);
1892 clip_right_or_top(dstY0
, dstY1
, srcY0
, srcY1
, srcYmax
);
1893 clip_left_or_bottom(dstX0
, dstX1
, srcX0
, srcX1
, srcXmin
);
1894 clip_left_or_bottom(dstY0
, dstY1
, srcY0
, srcY1
, srcYmin
);
1897 printf("PostClipX: src: %d .. %d dst: %d .. %d\n",
1898 *srcX0, *srcX1, *dstX0, *dstX1);
1899 printf("PostClipY: src: %d .. %d dst: %d .. %d\n",
1900 *srcY0, *srcY1, *dstY0, *dstY1);
1903 ASSERT(*dstX0
>= dstXmin
);
1904 ASSERT(*dstX0
<= dstXmax
);
1905 ASSERT(*dstX1
>= dstXmin
);
1906 ASSERT(*dstX1
<= dstXmax
);
1908 ASSERT(*dstY0
>= dstYmin
);
1909 ASSERT(*dstY0
<= dstYmax
);
1910 ASSERT(*dstY1
>= dstYmin
);
1911 ASSERT(*dstY1
<= dstYmax
);
1913 ASSERT(*srcX0
>= srcXmin
);
1914 ASSERT(*srcX0
<= srcXmax
);
1915 ASSERT(*srcX1
>= srcXmin
);
1916 ASSERT(*srcX1
<= srcXmax
);
1918 ASSERT(*srcY0
>= srcYmin
);
1919 ASSERT(*srcY0
<= srcYmax
);
1920 ASSERT(*srcY1
>= srcYmin
);
1921 ASSERT(*srcY1
<= srcYmax
);