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
:
94 * Flip the order of the 2 bytes in each word in the given array.
97 * \param n number of words.
100 _mesa_swap2( GLushort
*p
, GLuint n
)
103 for (i
= 0; i
< n
; i
++) {
104 p
[i
] = (p
[i
] >> 8) | ((p
[i
] << 8) & 0xff00);
111 * Flip the order of the 4 bytes in each word in the given array.
114 _mesa_swap4( GLuint
*p
, GLuint n
)
117 for (i
= 0; i
< n
; i
++) {
120 | ((b
>> 8) & 0xff00)
121 | ((b
<< 8) & 0xff0000)
122 | ((b
<< 24) & 0xff000000);
129 * Get the size of a GL data type.
131 * \param type GL data type.
133 * \return the size, in bytes, of the given data type, 0 if a GL_BITMAP, or -1
134 * if an invalid type enum.
137 _mesa_sizeof_type( GLenum type
)
142 case GL_UNSIGNED_BYTE
:
143 return sizeof(GLubyte
);
145 return sizeof(GLbyte
);
146 case GL_UNSIGNED_SHORT
:
147 return sizeof(GLushort
);
149 return sizeof(GLshort
);
150 case GL_UNSIGNED_INT
:
151 return sizeof(GLuint
);
153 return sizeof(GLint
);
155 return sizeof(GLfloat
);
157 return sizeof(GLdouble
);
158 case GL_HALF_FLOAT_ARB
:
159 return sizeof(GLhalfARB
);
161 return sizeof(GLfixed
);
169 * Same as _mesa_sizeof_type() but also accepting the packed pixel
173 _mesa_sizeof_packed_type( GLenum type
)
178 case GL_UNSIGNED_BYTE
:
179 return sizeof(GLubyte
);
181 return sizeof(GLbyte
);
182 case GL_UNSIGNED_SHORT
:
183 return sizeof(GLushort
);
185 return sizeof(GLshort
);
186 case GL_UNSIGNED_INT
:
187 return sizeof(GLuint
);
189 return sizeof(GLint
);
190 case GL_HALF_FLOAT_ARB
:
191 return sizeof(GLhalfARB
);
193 return sizeof(GLfloat
);
194 case GL_UNSIGNED_BYTE_3_3_2
:
195 return sizeof(GLubyte
);
196 case GL_UNSIGNED_BYTE_2_3_3_REV
:
197 return sizeof(GLubyte
);
198 case MESA_UNSIGNED_BYTE_4_4
:
199 return sizeof(GLubyte
);
200 case GL_UNSIGNED_SHORT_5_6_5
:
201 return sizeof(GLushort
);
202 case GL_UNSIGNED_SHORT_5_6_5_REV
:
203 return sizeof(GLushort
);
204 case GL_UNSIGNED_SHORT_4_4_4_4
:
205 return sizeof(GLushort
);
206 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
207 return sizeof(GLushort
);
208 case GL_UNSIGNED_SHORT_5_5_5_1
:
209 return sizeof(GLushort
);
210 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
211 return sizeof(GLushort
);
212 case GL_UNSIGNED_INT_8_8_8_8
:
213 return sizeof(GLuint
);
214 case GL_UNSIGNED_INT_8_8_8_8_REV
:
215 return sizeof(GLuint
);
216 case GL_UNSIGNED_INT_10_10_10_2
:
217 return sizeof(GLuint
);
218 case GL_UNSIGNED_INT_2_10_10_10_REV
:
219 return sizeof(GLuint
);
220 case GL_UNSIGNED_SHORT_8_8_MESA
:
221 case GL_UNSIGNED_SHORT_8_8_REV_MESA
:
222 return sizeof(GLushort
);
223 case GL_UNSIGNED_INT_24_8_EXT
:
224 return sizeof(GLuint
);
232 * Get the number of components in a pixel format.
234 * \param format pixel format.
236 * \return the number of components in the given format, or -1 if a bad format.
239 _mesa_components_in_format( GLenum format
)
243 case GL_COLOR_INDEX1_EXT
:
244 case GL_COLOR_INDEX2_EXT
:
245 case GL_COLOR_INDEX4_EXT
:
246 case GL_COLOR_INDEX8_EXT
:
247 case GL_COLOR_INDEX12_EXT
:
248 case GL_COLOR_INDEX16_EXT
:
249 case GL_STENCIL_INDEX
:
250 case GL_DEPTH_COMPONENT
:
252 case GL_RED_INTEGER_EXT
:
254 case GL_GREEN_INTEGER_EXT
:
256 case GL_BLUE_INTEGER_EXT
:
258 case GL_ALPHA_INTEGER_EXT
:
260 case GL_LUMINANCE_INTEGER_EXT
:
263 case GL_LUMINANCE_ALPHA
:
264 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
268 case GL_RGB_INTEGER_EXT
:
271 case GL_RGBA_INTEGER_EXT
:
281 case GL_DEPTH_STENCIL_EXT
:
293 * Get the bytes per pixel of pixel format type pair.
295 * \param format pixel format.
296 * \param type pixel type.
298 * \return bytes per pixel, or -1 if a bad format or type was given.
301 _mesa_bytes_per_pixel( GLenum format
, GLenum type
)
303 GLint comps
= _mesa_components_in_format( format
);
309 return 0; /* special case */
311 case GL_UNSIGNED_BYTE
:
312 return comps
* sizeof(GLubyte
);
314 case GL_UNSIGNED_SHORT
:
315 return comps
* sizeof(GLshort
);
317 case GL_UNSIGNED_INT
:
318 return comps
* sizeof(GLint
);
320 return comps
* sizeof(GLfloat
);
321 case GL_HALF_FLOAT_ARB
:
322 return comps
* sizeof(GLhalfARB
);
323 case GL_UNSIGNED_BYTE_3_3_2
:
324 case GL_UNSIGNED_BYTE_2_3_3_REV
:
325 if (format
== GL_RGB
|| format
== GL_BGR
||
326 format
== GL_RGB_INTEGER_EXT
|| format
== GL_BGR_INTEGER_EXT
)
327 return sizeof(GLubyte
);
329 return -1; /* error */
330 case GL_UNSIGNED_SHORT_5_6_5
:
331 case GL_UNSIGNED_SHORT_5_6_5_REV
:
332 if (format
== GL_RGB
|| format
== GL_BGR
||
333 format
== GL_RGB_INTEGER_EXT
|| format
== GL_BGR_INTEGER_EXT
)
334 return sizeof(GLushort
);
336 return -1; /* error */
337 case GL_UNSIGNED_SHORT_4_4_4_4
:
338 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
339 case GL_UNSIGNED_SHORT_5_5_5_1
:
340 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
341 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
||
342 format
== GL_RGBA_INTEGER_EXT
|| format
== GL_BGRA_INTEGER_EXT
)
343 return sizeof(GLushort
);
346 case GL_UNSIGNED_INT_8_8_8_8
:
347 case GL_UNSIGNED_INT_8_8_8_8_REV
:
348 case GL_UNSIGNED_INT_10_10_10_2
:
349 case GL_UNSIGNED_INT_2_10_10_10_REV
:
350 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
||
351 format
== GL_RGBA_INTEGER_EXT
|| format
== GL_BGRA_INTEGER_EXT
)
352 return sizeof(GLuint
);
355 case GL_UNSIGNED_SHORT_8_8_MESA
:
356 case GL_UNSIGNED_SHORT_8_8_REV_MESA
:
357 if (format
== GL_YCBCR_MESA
)
358 return sizeof(GLushort
);
361 case GL_UNSIGNED_INT_24_8_EXT
:
362 if (format
== GL_DEPTH_STENCIL_EXT
)
363 return sizeof(GLuint
);
373 * Test for a legal pixel format and type.
375 * \param format pixel format.
376 * \param type pixel type.
378 * \return GL_TRUE if the given pixel format and type are legal, or GL_FALSE
382 _mesa_is_legal_format_and_type(const struct gl_context
*ctx
,
383 GLenum format
, GLenum type
)
387 case GL_STENCIL_INDEX
:
391 case GL_UNSIGNED_BYTE
:
393 case GL_UNSIGNED_SHORT
:
395 case GL_UNSIGNED_INT
:
398 case GL_HALF_FLOAT_ARB
:
399 return ctx
->Extensions
.ARB_half_float_pixel
;
407 #if 0 /* not legal! see table 3.6 of the 1.5 spec */
411 case GL_LUMINANCE_ALPHA
:
412 case GL_DEPTH_COMPONENT
:
415 case GL_UNSIGNED_BYTE
:
417 case GL_UNSIGNED_SHORT
:
419 case GL_UNSIGNED_INT
:
422 case GL_HALF_FLOAT_ARB
:
423 return ctx
->Extensions
.ARB_half_float_pixel
;
428 if (!ctx
->Extensions
.ARB_texture_rg
)
433 case GL_UNSIGNED_BYTE
:
435 case GL_UNSIGNED_SHORT
:
437 case GL_UNSIGNED_INT
:
440 case GL_HALF_FLOAT_ARB
:
441 return ctx
->Extensions
.ARB_half_float_pixel
;
448 case GL_UNSIGNED_BYTE
:
450 case GL_UNSIGNED_SHORT
:
452 case GL_UNSIGNED_INT
:
454 case GL_UNSIGNED_BYTE_3_3_2
:
455 case GL_UNSIGNED_BYTE_2_3_3_REV
:
456 case GL_UNSIGNED_SHORT_5_6_5
:
457 case GL_UNSIGNED_SHORT_5_6_5_REV
:
459 case GL_HALF_FLOAT_ARB
:
460 return ctx
->Extensions
.ARB_half_float_pixel
;
466 /* NOTE: no packed types are supported with BGR. That's
467 * intentional, according to the GL spec.
470 case GL_UNSIGNED_BYTE
:
472 case GL_UNSIGNED_SHORT
:
474 case GL_UNSIGNED_INT
:
477 case GL_HALF_FLOAT_ARB
:
478 return ctx
->Extensions
.ARB_half_float_pixel
;
487 case GL_UNSIGNED_BYTE
:
489 case GL_UNSIGNED_SHORT
:
491 case GL_UNSIGNED_INT
:
493 case GL_UNSIGNED_SHORT_4_4_4_4
:
494 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
495 case GL_UNSIGNED_SHORT_5_5_5_1
:
496 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
497 case GL_UNSIGNED_INT_8_8_8_8
:
498 case GL_UNSIGNED_INT_8_8_8_8_REV
:
499 case GL_UNSIGNED_INT_10_10_10_2
:
500 case GL_UNSIGNED_INT_2_10_10_10_REV
:
502 case GL_HALF_FLOAT_ARB
:
503 return ctx
->Extensions
.ARB_half_float_pixel
;
508 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
||
509 type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
)
513 case GL_DEPTH_STENCIL_EXT
:
514 if (ctx
->Extensions
.EXT_packed_depth_stencil
515 && type
== GL_UNSIGNED_INT_24_8_EXT
)
523 case GL_UNSIGNED_BYTE
:
525 case GL_UNSIGNED_SHORT
:
527 case GL_UNSIGNED_INT
:
534 /* integer-valued formats */
535 case GL_RED_INTEGER_EXT
:
536 case GL_GREEN_INTEGER_EXT
:
537 case GL_BLUE_INTEGER_EXT
:
538 case GL_ALPHA_INTEGER_EXT
:
541 case GL_UNSIGNED_BYTE
:
543 case GL_UNSIGNED_SHORT
:
545 case GL_UNSIGNED_INT
:
546 return ctx
->Extensions
.EXT_texture_integer
;
551 case GL_RGB_INTEGER_EXT
:
554 case GL_UNSIGNED_BYTE
:
556 case GL_UNSIGNED_SHORT
:
558 case GL_UNSIGNED_INT
:
559 case GL_UNSIGNED_BYTE_3_3_2
:
560 case GL_UNSIGNED_BYTE_2_3_3_REV
:
561 case GL_UNSIGNED_SHORT_5_6_5
:
562 case GL_UNSIGNED_SHORT_5_6_5_REV
:
563 return ctx
->Extensions
.EXT_texture_integer
;
568 case GL_BGR_INTEGER_EXT
:
571 case GL_UNSIGNED_BYTE
:
573 case GL_UNSIGNED_SHORT
:
575 case GL_UNSIGNED_INT
:
576 /* NOTE: no packed formats w/ BGR format */
577 return ctx
->Extensions
.EXT_texture_integer
;
582 case GL_RGBA_INTEGER_EXT
:
583 case GL_BGRA_INTEGER_EXT
:
586 case GL_UNSIGNED_BYTE
:
588 case GL_UNSIGNED_SHORT
:
590 case GL_UNSIGNED_INT
:
591 case GL_UNSIGNED_SHORT_4_4_4_4
:
592 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
593 case GL_UNSIGNED_SHORT_5_5_5_1
:
594 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
595 case GL_UNSIGNED_INT_8_8_8_8
:
596 case GL_UNSIGNED_INT_8_8_8_8_REV
:
597 case GL_UNSIGNED_INT_10_10_10_2
:
598 case GL_UNSIGNED_INT_2_10_10_10_REV
:
599 return ctx
->Extensions
.EXT_texture_integer
;
604 case GL_LUMINANCE_INTEGER_EXT
:
605 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
608 case GL_UNSIGNED_BYTE
:
610 case GL_UNSIGNED_SHORT
:
612 case GL_UNSIGNED_INT
:
613 return ctx
->Extensions
.EXT_texture_integer
;
626 * Test if the given image format is a color/RGBA format (i.e., not color
627 * index, depth, stencil, etc).
628 * \param format the image format value (may by an internal texture format)
629 * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
632 _mesa_is_color_format(GLenum format
)
650 case GL_LUMINANCE_ALPHA
:
651 case GL_LUMINANCE4_ALPHA4
:
652 case GL_LUMINANCE6_ALPHA2
:
653 case GL_LUMINANCE8_ALPHA8
:
654 case GL_LUMINANCE12_ALPHA4
:
655 case GL_LUMINANCE12_ALPHA12
:
656 case GL_LUMINANCE16_ALPHA16
:
688 /* float texture formats */
689 case GL_ALPHA16F_ARB
:
690 case GL_ALPHA32F_ARB
:
691 case GL_LUMINANCE16F_ARB
:
692 case GL_LUMINANCE32F_ARB
:
693 case GL_LUMINANCE_ALPHA16F_ARB
:
694 case GL_LUMINANCE_ALPHA32F_ARB
:
695 case GL_INTENSITY16F_ARB
:
696 case GL_INTENSITY32F_ARB
:
705 /* compressed formats */
706 case GL_COMPRESSED_ALPHA
:
707 case GL_COMPRESSED_LUMINANCE
:
708 case GL_COMPRESSED_LUMINANCE_ALPHA
:
709 case GL_COMPRESSED_INTENSITY
:
710 case GL_COMPRESSED_RED
:
711 case GL_COMPRESSED_RG
:
712 case GL_COMPRESSED_RGB
:
713 case GL_COMPRESSED_RGBA
:
718 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
719 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
720 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
721 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
722 case GL_COMPRESSED_RGB_FXT1_3DFX
:
723 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
724 #if FEATURE_EXT_texture_sRGB
727 case GL_SRGB_ALPHA_EXT
:
728 case GL_SRGB8_ALPHA8_EXT
:
729 case GL_SLUMINANCE_ALPHA_EXT
:
730 case GL_SLUMINANCE8_ALPHA8_EXT
:
731 case GL_SLUMINANCE_EXT
:
732 case GL_SLUMINANCE8_EXT
:
733 case GL_COMPRESSED_SRGB_EXT
:
734 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
735 case GL_COMPRESSED_SRGB_ALPHA_EXT
:
736 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
737 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
738 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
739 case GL_COMPRESSED_SLUMINANCE_EXT
:
740 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT
:
741 #endif /* FEATURE_EXT_texture_sRGB */
742 case GL_COMPRESSED_RED_RGTC1
:
743 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
744 case GL_COMPRESSED_RG_RGTC2
:
745 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
746 /* signed, normalized texture formats */
749 /* generic integer formats */
750 case GL_RED_INTEGER_EXT
:
751 case GL_GREEN_INTEGER_EXT
:
752 case GL_BLUE_INTEGER_EXT
:
753 case GL_ALPHA_INTEGER_EXT
:
754 case GL_RGB_INTEGER_EXT
:
755 case GL_RGBA_INTEGER_EXT
:
756 case GL_BGR_INTEGER_EXT
:
757 case GL_BGRA_INTEGER_EXT
:
758 case GL_LUMINANCE_INTEGER_EXT
:
759 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
760 /* sized integer formats */
761 case GL_RGBA32UI_EXT
:
763 case GL_ALPHA32UI_EXT
:
764 case GL_INTENSITY32UI_EXT
:
765 case GL_LUMINANCE32UI_EXT
:
766 case GL_LUMINANCE_ALPHA32UI_EXT
:
767 case GL_RGBA16UI_EXT
:
769 case GL_ALPHA16UI_EXT
:
770 case GL_INTENSITY16UI_EXT
:
771 case GL_LUMINANCE16UI_EXT
:
772 case GL_LUMINANCE_ALPHA16UI_EXT
:
775 case GL_ALPHA8UI_EXT
:
776 case GL_INTENSITY8UI_EXT
:
777 case GL_LUMINANCE8UI_EXT
:
778 case GL_LUMINANCE_ALPHA8UI_EXT
:
781 case GL_ALPHA32I_EXT
:
782 case GL_INTENSITY32I_EXT
:
783 case GL_LUMINANCE32I_EXT
:
784 case GL_LUMINANCE_ALPHA32I_EXT
:
787 case GL_ALPHA16I_EXT
:
788 case GL_INTENSITY16I_EXT
:
789 case GL_LUMINANCE16I_EXT
:
790 case GL_LUMINANCE_ALPHA16I_EXT
:
794 case GL_INTENSITY8I_EXT
:
795 case GL_LUMINANCE8I_EXT
:
796 case GL_LUMINANCE_ALPHA8I_EXT
:
798 case GL_YCBCR_MESA
: /* not considered to be RGB */
807 * Test if the given image format is a color index format.
810 _mesa_is_index_format(GLenum format
)
814 case GL_COLOR_INDEX1_EXT
:
815 case GL_COLOR_INDEX2_EXT
:
816 case GL_COLOR_INDEX4_EXT
:
817 case GL_COLOR_INDEX8_EXT
:
818 case GL_COLOR_INDEX12_EXT
:
819 case GL_COLOR_INDEX16_EXT
:
828 * Test if the given image format is a depth component format.
831 _mesa_is_depth_format(GLenum format
)
834 case GL_DEPTH_COMPONENT
:
835 case GL_DEPTH_COMPONENT16
:
836 case GL_DEPTH_COMPONENT24
:
837 case GL_DEPTH_COMPONENT32
:
846 * Test if the given image format is a stencil format.
849 _mesa_is_stencil_format(GLenum format
)
852 case GL_STENCIL_INDEX
:
853 case GL_DEPTH_STENCIL
:
862 * Test if the given image format is a YCbCr format.
865 _mesa_is_ycbcr_format(GLenum format
)
877 * Test if the given image format is a depth+stencil format.
880 _mesa_is_depthstencil_format(GLenum format
)
883 case GL_DEPTH24_STENCIL8_EXT
:
884 case GL_DEPTH_STENCIL_EXT
:
893 * Test if the given image format is a depth or stencil format.
896 _mesa_is_depth_or_stencil_format(GLenum format
)
899 case GL_DEPTH_COMPONENT
:
900 case GL_DEPTH_COMPONENT16
:
901 case GL_DEPTH_COMPONENT24
:
902 case GL_DEPTH_COMPONENT32
:
903 case GL_STENCIL_INDEX
:
904 case GL_STENCIL_INDEX1_EXT
:
905 case GL_STENCIL_INDEX4_EXT
:
906 case GL_STENCIL_INDEX8_EXT
:
907 case GL_STENCIL_INDEX16_EXT
:
908 case GL_DEPTH_STENCIL_EXT
:
909 case GL_DEPTH24_STENCIL8_EXT
:
918 * Test if the given image format is a dudv format.
921 _mesa_is_dudv_format(GLenum format
)
934 * Test if the given format is an integer (non-normalized) format.
937 _mesa_is_integer_format(GLenum format
)
940 /* generic integer formats */
941 case GL_RED_INTEGER_EXT
:
942 case GL_GREEN_INTEGER_EXT
:
943 case GL_BLUE_INTEGER_EXT
:
944 case GL_ALPHA_INTEGER_EXT
:
945 case GL_RGB_INTEGER_EXT
:
946 case GL_RGBA_INTEGER_EXT
:
947 case GL_BGR_INTEGER_EXT
:
948 case GL_BGRA_INTEGER_EXT
:
949 case GL_LUMINANCE_INTEGER_EXT
:
950 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
951 /* specific integer formats */
952 case GL_RGBA32UI_EXT
:
954 case GL_ALPHA32UI_EXT
:
955 case GL_INTENSITY32UI_EXT
:
956 case GL_LUMINANCE32UI_EXT
:
957 case GL_LUMINANCE_ALPHA32UI_EXT
:
958 case GL_RGBA16UI_EXT
:
960 case GL_ALPHA16UI_EXT
:
961 case GL_INTENSITY16UI_EXT
:
962 case GL_LUMINANCE16UI_EXT
:
963 case GL_LUMINANCE_ALPHA16UI_EXT
:
966 case GL_ALPHA8UI_EXT
:
967 case GL_INTENSITY8UI_EXT
:
968 case GL_LUMINANCE8UI_EXT
:
969 case GL_LUMINANCE_ALPHA8UI_EXT
:
972 case GL_ALPHA32I_EXT
:
973 case GL_INTENSITY32I_EXT
:
974 case GL_LUMINANCE32I_EXT
:
975 case GL_LUMINANCE_ALPHA32I_EXT
:
978 case GL_ALPHA16I_EXT
:
979 case GL_INTENSITY16I_EXT
:
980 case GL_LUMINANCE16I_EXT
:
981 case GL_LUMINANCE_ALPHA16I_EXT
:
985 case GL_INTENSITY8I_EXT
:
986 case GL_LUMINANCE8I_EXT
:
987 case GL_LUMINANCE_ALPHA8I_EXT
:
996 * Test if an image format is a supported compressed format.
997 * \param format the internal format token provided by the user.
998 * \return GL_TRUE if compressed, GL_FALSE if uncompressed
1001 _mesa_is_compressed_format(struct gl_context
*ctx
, GLenum format
)
1004 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
1005 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
1006 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
1007 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
1008 return ctx
->Extensions
.EXT_texture_compression_s3tc
;
1013 return ctx
->Extensions
.S3_s3tc
;
1014 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
1015 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
1016 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
1017 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
1018 return ctx
->Extensions
.EXT_texture_sRGB
1019 && ctx
->Extensions
.EXT_texture_compression_s3tc
;
1020 case GL_COMPRESSED_RGB_FXT1_3DFX
:
1021 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
1022 return ctx
->Extensions
.TDFX_texture_compression_FXT1
;
1023 case GL_COMPRESSED_RED_RGTC1
:
1024 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
1025 case GL_COMPRESSED_RG_RGTC2
:
1026 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
1027 return ctx
->Extensions
.ARB_texture_compression_rgtc
;
1035 * Return the address of a specific pixel in an image (1D, 2D or 3D).
1037 * Pixel unpacking/packing parameters are observed according to \p packing.
1039 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
1040 * \param image starting address of image data
1041 * \param width the image width
1042 * \param height theimage height
1043 * \param format the pixel format
1044 * \param type the pixel data type
1045 * \param packing the pixelstore attributes
1046 * \param img which image in the volume (0 for 1D or 2D images)
1047 * \param row row of pixel in the image (0 for 1D images)
1048 * \param column column of pixel in the image
1050 * \return address of pixel on success, or NULL on error.
1052 * \sa gl_pixelstore_attrib.
1055 _mesa_image_address( GLuint dimensions
,
1056 const struct gl_pixelstore_attrib
*packing
,
1057 const GLvoid
*image
,
1058 GLsizei width
, GLsizei height
,
1059 GLenum format
, GLenum type
,
1060 GLint img
, GLint row
, GLint column
)
1062 GLint alignment
; /* 1, 2 or 4 */
1063 GLint pixels_per_row
;
1064 GLint rows_per_image
;
1067 GLint skipimages
; /* for 3-D volume images */
1068 GLubyte
*pixel_addr
;
1070 ASSERT(dimensions
>= 1 && dimensions
<= 3);
1072 alignment
= packing
->Alignment
;
1073 if (packing
->RowLength
> 0) {
1074 pixels_per_row
= packing
->RowLength
;
1077 pixels_per_row
= width
;
1079 if (packing
->ImageHeight
> 0) {
1080 rows_per_image
= packing
->ImageHeight
;
1083 rows_per_image
= height
;
1086 skippixels
= packing
->SkipPixels
;
1087 /* Note: SKIP_ROWS _is_ used for 1D images */
1088 skiprows
= packing
->SkipRows
;
1089 /* Note: SKIP_IMAGES is only used for 3D images */
1090 skipimages
= (dimensions
== 3) ? packing
->SkipImages
: 0;
1092 if (type
== GL_BITMAP
) {
1094 GLint comp_per_pixel
; /* components per pixel */
1095 GLint bytes_per_comp
; /* bytes per component */
1096 GLint bytes_per_row
;
1097 GLint bytes_per_image
;
1099 /* Compute bytes per component */
1100 bytes_per_comp
= _mesa_sizeof_packed_type( type
);
1101 if (bytes_per_comp
< 0) {
1105 /* Compute number of components per pixel */
1106 comp_per_pixel
= _mesa_components_in_format( format
);
1107 if (comp_per_pixel
< 0) {
1111 bytes_per_row
= alignment
1112 * CEILING( comp_per_pixel
*pixels_per_row
, 8*alignment
);
1114 bytes_per_image
= bytes_per_row
* rows_per_image
;
1116 pixel_addr
= (GLubyte
*) image
1117 + (skipimages
+ img
) * bytes_per_image
1118 + (skiprows
+ row
) * bytes_per_row
1119 + (skippixels
+ column
) / 8;
1122 /* Non-BITMAP data */
1123 GLint bytes_per_pixel
, bytes_per_row
, remainder
, bytes_per_image
;
1126 bytes_per_pixel
= _mesa_bytes_per_pixel( format
, type
);
1128 /* The pixel type and format should have been error checked earlier */
1129 assert(bytes_per_pixel
> 0);
1131 bytes_per_row
= pixels_per_row
* bytes_per_pixel
;
1132 remainder
= bytes_per_row
% alignment
;
1134 bytes_per_row
+= (alignment
- remainder
);
1136 ASSERT(bytes_per_row
% alignment
== 0);
1138 bytes_per_image
= bytes_per_row
* rows_per_image
;
1140 if (packing
->Invert
) {
1141 /* set pixel_addr to the last row */
1142 topOfImage
= bytes_per_row
* (height
- 1);
1143 bytes_per_row
= -bytes_per_row
;
1149 /* compute final pixel address */
1150 pixel_addr
= (GLubyte
*) image
1151 + (skipimages
+ img
) * bytes_per_image
1153 + (skiprows
+ row
) * bytes_per_row
1154 + (skippixels
+ column
) * bytes_per_pixel
;
1157 return (GLvoid
*) pixel_addr
;
1162 _mesa_image_address1d( const struct gl_pixelstore_attrib
*packing
,
1163 const GLvoid
*image
,
1165 GLenum format
, GLenum type
,
1168 return _mesa_image_address(1, packing
, image
, width
, 1,
1169 format
, type
, 0, 0, column
);
1174 _mesa_image_address2d( const struct gl_pixelstore_attrib
*packing
,
1175 const GLvoid
*image
,
1176 GLsizei width
, GLsizei height
,
1177 GLenum format
, GLenum type
,
1178 GLint row
, GLint column
)
1180 return _mesa_image_address(2, packing
, image
, width
, height
,
1181 format
, type
, 0, row
, column
);
1186 _mesa_image_address3d( const struct gl_pixelstore_attrib
*packing
,
1187 const GLvoid
*image
,
1188 GLsizei width
, GLsizei height
,
1189 GLenum format
, GLenum type
,
1190 GLint img
, GLint row
, GLint column
)
1192 return _mesa_image_address(3, packing
, image
, width
, height
,
1193 format
, type
, img
, row
, column
);
1199 * Compute the stride (in bytes) between image rows.
1201 * \param packing the pixelstore attributes
1202 * \param width image width.
1203 * \param format pixel format.
1204 * \param type pixel data type.
1206 * \return the stride in bytes for the given parameters, or -1 if error
1209 _mesa_image_row_stride( const struct gl_pixelstore_attrib
*packing
,
1210 GLint width
, GLenum format
, GLenum type
)
1212 GLint bytesPerRow
, remainder
;
1216 if (type
== GL_BITMAP
) {
1217 if (packing
->RowLength
== 0) {
1218 bytesPerRow
= (width
+ 7) / 8;
1221 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
1225 /* Non-BITMAP data */
1226 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1227 if (bytesPerPixel
<= 0)
1228 return -1; /* error */
1229 if (packing
->RowLength
== 0) {
1230 bytesPerRow
= bytesPerPixel
* width
;
1233 bytesPerRow
= bytesPerPixel
* packing
->RowLength
;
1237 remainder
= bytesPerRow
% packing
->Alignment
;
1238 if (remainder
> 0) {
1239 bytesPerRow
+= (packing
->Alignment
- remainder
);
1242 if (packing
->Invert
) {
1243 /* negate the bytes per row (negative row stride) */
1244 bytesPerRow
= -bytesPerRow
;
1252 * Compute the stride between images in a 3D texture (in bytes) for the given
1253 * pixel packing parameters and image width, format and type.
1256 _mesa_image_image_stride( const struct gl_pixelstore_attrib
*packing
,
1257 GLint width
, GLint height
,
1258 GLenum format
, GLenum type
)
1260 GLint bytesPerRow
, bytesPerImage
, remainder
;
1264 if (type
== GL_BITMAP
) {
1265 if (packing
->RowLength
== 0) {
1266 bytesPerRow
= (width
+ 7) / 8;
1269 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
1273 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1275 if (bytesPerPixel
<= 0)
1276 return -1; /* error */
1277 if (packing
->RowLength
== 0) {
1278 bytesPerRow
= bytesPerPixel
* width
;
1281 bytesPerRow
= bytesPerPixel
* packing
->RowLength
;
1285 remainder
= bytesPerRow
% packing
->Alignment
;
1287 bytesPerRow
+= (packing
->Alignment
- remainder
);
1289 if (packing
->ImageHeight
== 0)
1290 bytesPerImage
= bytesPerRow
* height
;
1292 bytesPerImage
= bytesPerRow
* packing
->ImageHeight
;
1294 return bytesPerImage
;
1300 * "Expand" a bitmap from 1-bit per pixel to 8-bits per pixel.
1301 * This is typically used to convert a bitmap into a GLubyte/pixel texture.
1302 * "On" bits will set texels to \p onValue.
1303 * "Off" bits will not modify texels.
1304 * \param width src bitmap width in pixels
1305 * \param height src bitmap height in pixels
1306 * \param unpack bitmap unpacking state
1307 * \param bitmap the src bitmap data
1308 * \param destBuffer start of dest buffer
1309 * \param destStride row stride in dest buffer
1310 * \param onValue if bit is 1, set destBuffer pixel to this value
1313 _mesa_expand_bitmap(GLsizei width
, GLsizei height
,
1314 const struct gl_pixelstore_attrib
*unpack
,
1315 const GLubyte
*bitmap
,
1316 GLubyte
*destBuffer
, GLint destStride
,
1319 const GLubyte
*srcRow
= (const GLubyte
*)
1320 _mesa_image_address2d(unpack
, bitmap
, width
, height
,
1321 GL_COLOR_INDEX
, GL_BITMAP
, 0, 0);
1322 const GLint srcStride
= _mesa_image_row_stride(unpack
, width
,
1323 GL_COLOR_INDEX
, GL_BITMAP
);
1326 #define SET_PIXEL(COL, ROW) \
1327 destBuffer[(ROW) * destStride + (COL)] = onValue;
1329 for (row
= 0; row
< height
; row
++) {
1330 const GLubyte
*src
= srcRow
;
1332 if (unpack
->LsbFirst
) {
1334 GLubyte mask
= 1U << (unpack
->SkipPixels
& 0x7);
1335 for (col
= 0; col
< width
; col
++) {
1338 SET_PIXEL(col
, row
);
1350 /* get ready for next row */
1356 GLubyte mask
= 128U >> (unpack
->SkipPixels
& 0x7);
1357 for (col
= 0; col
< width
; col
++) {
1360 SET_PIXEL(col
, row
);
1372 /* get ready for next row */
1377 srcRow
+= srcStride
;
1387 * Convert an array of RGBA colors from one datatype to another.
1388 * NOTE: src may equal dst. In that case, we use a temporary buffer.
1391 _mesa_convert_colors(GLenum srcType
, const GLvoid
*src
,
1392 GLenum dstType
, GLvoid
*dst
,
1393 GLuint count
, const GLubyte mask
[])
1395 GLuint tempBuffer
[MAX_WIDTH
][4];
1396 const GLboolean useTemp
= (src
== dst
);
1398 ASSERT(srcType
!= dstType
);
1401 case GL_UNSIGNED_BYTE
:
1402 if (dstType
== GL_UNSIGNED_SHORT
) {
1403 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
1404 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
1406 for (i
= 0; i
< count
; i
++) {
1407 if (!mask
|| mask
[i
]) {
1408 dst2
[i
][RCOMP
] = UBYTE_TO_USHORT(src1
[i
][RCOMP
]);
1409 dst2
[i
][GCOMP
] = UBYTE_TO_USHORT(src1
[i
][GCOMP
]);
1410 dst2
[i
][BCOMP
] = UBYTE_TO_USHORT(src1
[i
][BCOMP
]);
1411 dst2
[i
][ACOMP
] = UBYTE_TO_USHORT(src1
[i
][ACOMP
]);
1415 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
1418 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
1419 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
1421 ASSERT(dstType
== GL_FLOAT
);
1422 for (i
= 0; i
< count
; i
++) {
1423 if (!mask
|| mask
[i
]) {
1424 dst4
[i
][RCOMP
] = UBYTE_TO_FLOAT(src1
[i
][RCOMP
]);
1425 dst4
[i
][GCOMP
] = UBYTE_TO_FLOAT(src1
[i
][GCOMP
]);
1426 dst4
[i
][BCOMP
] = UBYTE_TO_FLOAT(src1
[i
][BCOMP
]);
1427 dst4
[i
][ACOMP
] = UBYTE_TO_FLOAT(src1
[i
][ACOMP
]);
1431 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
1434 case GL_UNSIGNED_SHORT
:
1435 if (dstType
== GL_UNSIGNED_BYTE
) {
1436 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
1437 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
1439 for (i
= 0; i
< count
; i
++) {
1440 if (!mask
|| mask
[i
]) {
1441 dst1
[i
][RCOMP
] = USHORT_TO_UBYTE(src2
[i
][RCOMP
]);
1442 dst1
[i
][GCOMP
] = USHORT_TO_UBYTE(src2
[i
][GCOMP
]);
1443 dst1
[i
][BCOMP
] = USHORT_TO_UBYTE(src2
[i
][BCOMP
]);
1444 dst1
[i
][ACOMP
] = USHORT_TO_UBYTE(src2
[i
][ACOMP
]);
1448 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
1451 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
1452 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
1454 ASSERT(dstType
== GL_FLOAT
);
1455 for (i
= 0; i
< count
; i
++) {
1456 if (!mask
|| mask
[i
]) {
1457 dst4
[i
][RCOMP
] = USHORT_TO_FLOAT(src2
[i
][RCOMP
]);
1458 dst4
[i
][GCOMP
] = USHORT_TO_FLOAT(src2
[i
][GCOMP
]);
1459 dst4
[i
][BCOMP
] = USHORT_TO_FLOAT(src2
[i
][BCOMP
]);
1460 dst4
[i
][ACOMP
] = USHORT_TO_FLOAT(src2
[i
][ACOMP
]);
1464 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
1468 if (dstType
== GL_UNSIGNED_BYTE
) {
1469 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
1470 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
1472 for (i
= 0; i
< count
; i
++) {
1473 if (!mask
|| mask
[i
]) {
1474 UNCLAMPED_FLOAT_TO_UBYTE(dst1
[i
][RCOMP
], src4
[i
][RCOMP
]);
1475 UNCLAMPED_FLOAT_TO_UBYTE(dst1
[i
][GCOMP
], src4
[i
][GCOMP
]);
1476 UNCLAMPED_FLOAT_TO_UBYTE(dst1
[i
][BCOMP
], src4
[i
][BCOMP
]);
1477 UNCLAMPED_FLOAT_TO_UBYTE(dst1
[i
][ACOMP
], src4
[i
][ACOMP
]);
1481 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
1484 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
1485 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
1487 ASSERT(dstType
== GL_UNSIGNED_SHORT
);
1488 for (i
= 0; i
< count
; i
++) {
1489 if (!mask
|| mask
[i
]) {
1490 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][RCOMP
], src4
[i
][RCOMP
]);
1491 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][GCOMP
], src4
[i
][GCOMP
]);
1492 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][BCOMP
], src4
[i
][BCOMP
]);
1493 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][ACOMP
], src4
[i
][ACOMP
]);
1497 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
1501 _mesa_problem(NULL
, "Invalid datatype in _mesa_convert_colors");
1509 * Perform basic clipping for glDrawPixels. The image's position and size
1510 * and the unpack SkipPixels and SkipRows are adjusted so that the image
1511 * region is entirely within the window and scissor bounds.
1512 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
1513 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
1514 * we'll actually write. Beforehand, *destY-1 is the first drawing row.
1516 * \return GL_TRUE if image is ready for drawing or
1517 * GL_FALSE if image was completely clipped away (draw nothing)
1520 _mesa_clip_drawpixels(const struct gl_context
*ctx
,
1521 GLint
*destX
, GLint
*destY
,
1522 GLsizei
*width
, GLsizei
*height
,
1523 struct gl_pixelstore_attrib
*unpack
)
1525 const struct gl_framebuffer
*buffer
= ctx
->DrawBuffer
;
1527 if (unpack
->RowLength
== 0) {
1528 unpack
->RowLength
= *width
;
1531 ASSERT(ctx
->Pixel
.ZoomX
== 1.0F
);
1532 ASSERT(ctx
->Pixel
.ZoomY
== 1.0F
|| ctx
->Pixel
.ZoomY
== -1.0F
);
1535 if (*destX
< buffer
->_Xmin
) {
1536 unpack
->SkipPixels
+= (buffer
->_Xmin
- *destX
);
1537 *width
-= (buffer
->_Xmin
- *destX
);
1538 *destX
= buffer
->_Xmin
;
1540 /* right clipping */
1541 if (*destX
+ *width
> buffer
->_Xmax
)
1542 *width
-= (*destX
+ *width
- buffer
->_Xmax
);
1547 if (ctx
->Pixel
.ZoomY
== 1.0F
) {
1548 /* bottom clipping */
1549 if (*destY
< buffer
->_Ymin
) {
1550 unpack
->SkipRows
+= (buffer
->_Ymin
- *destY
);
1551 *height
-= (buffer
->_Ymin
- *destY
);
1552 *destY
= buffer
->_Ymin
;
1555 if (*destY
+ *height
> buffer
->_Ymax
)
1556 *height
-= (*destY
+ *height
- buffer
->_Ymax
);
1558 else { /* upside down */
1560 if (*destY
> buffer
->_Ymax
) {
1561 unpack
->SkipRows
+= (*destY
- buffer
->_Ymax
);
1562 *height
-= (*destY
- buffer
->_Ymax
);
1563 *destY
= buffer
->_Ymax
;
1565 /* bottom clipping */
1566 if (*destY
- *height
< buffer
->_Ymin
)
1567 *height
-= (buffer
->_Ymin
- (*destY
- *height
));
1568 /* adjust destY so it's the first row to write to */
1580 * Perform clipping for glReadPixels. The image's window position
1581 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
1582 * so that the image region is entirely within the window bounds.
1583 * Note: this is different from _mesa_clip_drawpixels() in that the
1584 * scissor box is ignored, and we use the bounds of the current readbuffer
1587 * \return GL_TRUE if region to read is in bounds
1588 * GL_FALSE if region is completely out of bounds (nothing to read)
1591 _mesa_clip_readpixels(const struct gl_context
*ctx
,
1592 GLint
*srcX
, GLint
*srcY
,
1593 GLsizei
*width
, GLsizei
*height
,
1594 struct gl_pixelstore_attrib
*pack
)
1596 const struct gl_framebuffer
*buffer
= ctx
->ReadBuffer
;
1598 if (pack
->RowLength
== 0) {
1599 pack
->RowLength
= *width
;
1604 pack
->SkipPixels
+= (0 - *srcX
);
1605 *width
-= (0 - *srcX
);
1608 /* right clipping */
1609 if (*srcX
+ *width
> (GLsizei
) buffer
->Width
)
1610 *width
-= (*srcX
+ *width
- buffer
->Width
);
1615 /* bottom clipping */
1617 pack
->SkipRows
+= (0 - *srcY
);
1618 *height
-= (0 - *srcY
);
1622 if (*srcY
+ *height
> (GLsizei
) buffer
->Height
)
1623 *height
-= (*srcY
+ *height
- buffer
->Height
);
1633 * Do clipping for a glCopyTexSubImage call.
1634 * The framebuffer source region might extend outside the framebuffer
1635 * bounds. Clip the source region against the framebuffer bounds and
1636 * adjust the texture/dest position and size accordingly.
1638 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
1641 _mesa_clip_copytexsubimage(const struct gl_context
*ctx
,
1642 GLint
*destX
, GLint
*destY
,
1643 GLint
*srcX
, GLint
*srcY
,
1644 GLsizei
*width
, GLsizei
*height
)
1646 const struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1647 const GLint srcX0
= *srcX
, srcY0
= *srcY
;
1649 if (_mesa_clip_to_region(0, 0, fb
->Width
, fb
->Height
,
1650 srcX
, srcY
, width
, height
)) {
1651 *destX
= *destX
+ *srcX
- srcX0
;
1652 *destY
= *destY
+ *srcY
- srcY0
;
1664 * Clip the rectangle defined by (x, y, width, height) against the bounds
1665 * specified by [xmin, xmax) and [ymin, ymax).
1666 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
1669 _mesa_clip_to_region(GLint xmin
, GLint ymin
,
1670 GLint xmax
, GLint ymax
,
1672 GLsizei
*width
, GLsizei
*height
)
1676 *width
-= (xmin
- *x
);
1680 /* right clipping */
1681 if (*x
+ *width
> xmax
)
1682 *width
-= (*x
+ *width
- xmax
);
1687 /* bottom (or top) clipping */
1689 *height
-= (ymin
- *y
);
1693 /* top (or bottom) clipping */
1694 if (*y
+ *height
> ymax
)
1695 *height
-= (*y
+ *height
- ymax
);
1705 * Clip dst coords against Xmax (or Ymax).
1708 clip_right_or_top(GLint
*srcX0
, GLint
*srcX1
,
1709 GLint
*dstX0
, GLint
*dstX1
,
1714 if (*dstX1
> maxValue
) {
1715 /* X1 outside right edge */
1716 ASSERT(*dstX0
< maxValue
); /* X0 should be inside right edge */
1717 t
= (GLfloat
) (maxValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
1718 /* chop off [t, 1] part */
1719 ASSERT(t
>= 0.0 && t
<= 1.0);
1721 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
1722 *srcX1
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
1724 else if (*dstX0
> maxValue
) {
1725 /* X0 outside right edge */
1726 ASSERT(*dstX1
< maxValue
); /* X1 should be inside right edge */
1727 t
= (GLfloat
) (maxValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
1728 /* chop off [t, 1] part */
1729 ASSERT(t
>= 0.0 && t
<= 1.0);
1731 bias
= (*srcX0
< *srcX1
) ? -0.5F
: 0.5F
;
1732 *srcX0
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
1738 * Clip dst coords against Xmin (or Ymin).
1741 clip_left_or_bottom(GLint
*srcX0
, GLint
*srcX1
,
1742 GLint
*dstX0
, GLint
*dstX1
,
1747 if (*dstX0
< minValue
) {
1748 /* X0 outside left edge */
1749 ASSERT(*dstX1
> minValue
); /* X1 should be inside left edge */
1750 t
= (GLfloat
) (minValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
1751 /* chop off [0, t] part */
1752 ASSERT(t
>= 0.0 && t
<= 1.0);
1754 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
; /* flipped??? */
1755 *srcX0
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
1757 else if (*dstX1
< minValue
) {
1758 /* X1 outside left edge */
1759 ASSERT(*dstX0
> minValue
); /* X0 should be inside left edge */
1760 t
= (GLfloat
) (minValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
1761 /* chop off [0, t] part */
1762 ASSERT(t
>= 0.0 && t
<= 1.0);
1764 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
1765 *srcX1
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
1771 * Do clipping of blit src/dest rectangles.
1772 * The dest rect is clipped against both the buffer bounds and scissor bounds.
1773 * The src rect is just clipped against the buffer bounds.
1775 * When either the src or dest rect is clipped, the other is also clipped
1778 * Note that X0 need not be less than X1 (same for Y) for either the source
1779 * and dest rects. That makes the clipping a little trickier.
1781 * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped
1784 _mesa_clip_blit(struct gl_context
*ctx
,
1785 GLint
*srcX0
, GLint
*srcY0
, GLint
*srcX1
, GLint
*srcY1
,
1786 GLint
*dstX0
, GLint
*dstY0
, GLint
*dstX1
, GLint
*dstY1
)
1788 const GLint srcXmin
= 0;
1789 const GLint srcXmax
= ctx
->ReadBuffer
->Width
;
1790 const GLint srcYmin
= 0;
1791 const GLint srcYmax
= ctx
->ReadBuffer
->Height
;
1793 /* these include scissor bounds */
1794 const GLint dstXmin
= ctx
->DrawBuffer
->_Xmin
;
1795 const GLint dstXmax
= ctx
->DrawBuffer
->_Xmax
;
1796 const GLint dstYmin
= ctx
->DrawBuffer
->_Ymin
;
1797 const GLint dstYmax
= ctx
->DrawBuffer
->_Ymax
;
1800 printf("PreClipX: src: %d .. %d dst: %d .. %d\n",
1801 *srcX0, *srcX1, *dstX0, *dstX1);
1802 printf("PreClipY: src: %d .. %d dst: %d .. %d\n",
1803 *srcY0, *srcY1, *dstY0, *dstY1);
1806 /* trivial rejection tests */
1807 if (*dstX0
== *dstX1
)
1808 return GL_FALSE
; /* no width */
1809 if (*dstX0
<= dstXmin
&& *dstX1
<= dstXmin
)
1810 return GL_FALSE
; /* totally out (left) of bounds */
1811 if (*dstX0
>= dstXmax
&& *dstX1
>= dstXmax
)
1812 return GL_FALSE
; /* totally out (right) of bounds */
1814 if (*dstY0
== *dstY1
)
1816 if (*dstY0
<= dstYmin
&& *dstY1
<= dstYmin
)
1818 if (*dstY0
>= dstYmax
&& *dstY1
>= dstYmax
)
1821 if (*srcX0
== *srcX1
)
1823 if (*srcX0
<= srcXmin
&& *srcX1
<= srcXmin
)
1825 if (*srcX0
>= srcXmax
&& *srcX1
>= srcXmax
)
1828 if (*srcY0
== *srcY1
)
1830 if (*srcY0
<= srcYmin
&& *srcY1
<= srcYmin
)
1832 if (*srcY0
>= srcYmax
&& *srcY1
>= srcYmax
)
1838 clip_right_or_top(srcX0
, srcX1
, dstX0
, dstX1
, dstXmax
);
1839 clip_right_or_top(srcY0
, srcY1
, dstY0
, dstY1
, dstYmax
);
1840 clip_left_or_bottom(srcX0
, srcX1
, dstX0
, dstX1
, dstXmin
);
1841 clip_left_or_bottom(srcY0
, srcY1
, dstY0
, dstY1
, dstYmin
);
1844 * src clip (just swap src/dst values from above)
1846 clip_right_or_top(dstX0
, dstX1
, srcX0
, srcX1
, srcXmax
);
1847 clip_right_or_top(dstY0
, dstY1
, srcY0
, srcY1
, srcYmax
);
1848 clip_left_or_bottom(dstX0
, dstX1
, srcX0
, srcX1
, srcXmin
);
1849 clip_left_or_bottom(dstY0
, dstY1
, srcY0
, srcY1
, srcYmin
);
1852 printf("PostClipX: src: %d .. %d dst: %d .. %d\n",
1853 *srcX0, *srcX1, *dstX0, *dstX1);
1854 printf("PostClipY: src: %d .. %d dst: %d .. %d\n",
1855 *srcY0, *srcY1, *dstY0, *dstY1);
1858 ASSERT(*dstX0
>= dstXmin
);
1859 ASSERT(*dstX0
<= dstXmax
);
1860 ASSERT(*dstX1
>= dstXmin
);
1861 ASSERT(*dstX1
<= dstXmax
);
1863 ASSERT(*dstY0
>= dstYmin
);
1864 ASSERT(*dstY0
<= dstYmax
);
1865 ASSERT(*dstY1
>= dstYmin
);
1866 ASSERT(*dstY1
<= dstYmax
);
1868 ASSERT(*srcX0
>= srcXmin
);
1869 ASSERT(*srcX0
<= srcXmax
);
1870 ASSERT(*srcX1
>= srcXmin
);
1871 ASSERT(*srcX1
<= srcXmax
);
1873 ASSERT(*srcY0
>= srcYmin
);
1874 ASSERT(*srcY0
<= srcYmax
);
1875 ASSERT(*srcY1
>= srcYmin
);
1876 ASSERT(*srcY1
<= srcYmax
);