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 * \return GL_TRUE if type is packed pixel type, GL_FALSE otherwise.
47 _mesa_type_is_packed(GLenum type
)
50 case GL_UNSIGNED_BYTE_3_3_2
:
51 case GL_UNSIGNED_BYTE_2_3_3_REV
:
52 case MESA_UNSIGNED_BYTE_4_4
:
53 case GL_UNSIGNED_SHORT_5_6_5
:
54 case GL_UNSIGNED_SHORT_5_6_5_REV
:
55 case GL_UNSIGNED_SHORT_4_4_4_4
:
56 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
57 case GL_UNSIGNED_SHORT_5_5_5_1
:
58 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
59 case GL_UNSIGNED_INT_8_8_8_8
:
60 case GL_UNSIGNED_INT_8_8_8_8_REV
:
61 case GL_UNSIGNED_INT_10_10_10_2
:
62 case GL_UNSIGNED_INT_2_10_10_10_REV
:
63 case GL_UNSIGNED_SHORT_8_8_MESA
:
64 case GL_UNSIGNED_SHORT_8_8_REV_MESA
:
65 case GL_UNSIGNED_INT_24_8_EXT
:
66 case GL_UNSIGNED_INT_5_9_9_9_REV
:
67 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
68 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
78 * Flip the order of the 2 bytes in each word in the given array.
81 * \param n number of words.
84 _mesa_swap2( GLushort
*p
, GLuint n
)
87 for (i
= 0; i
< n
; i
++) {
88 p
[i
] = (p
[i
] >> 8) | ((p
[i
] << 8) & 0xff00);
95 * Flip the order of the 4 bytes in each word in the given array.
98 _mesa_swap4( GLuint
*p
, GLuint n
)
101 for (i
= 0; i
< n
; i
++) {
104 | ((b
>> 8) & 0xff00)
105 | ((b
<< 8) & 0xff0000)
106 | ((b
<< 24) & 0xff000000);
113 * Get the size of a GL data type.
115 * \param type GL data type.
117 * \return the size, in bytes, of the given data type, 0 if a GL_BITMAP, or -1
118 * if an invalid type enum.
121 _mesa_sizeof_type( GLenum type
)
126 case GL_UNSIGNED_BYTE
:
127 return sizeof(GLubyte
);
129 return sizeof(GLbyte
);
130 case GL_UNSIGNED_SHORT
:
131 return sizeof(GLushort
);
133 return sizeof(GLshort
);
134 case GL_UNSIGNED_INT
:
135 return sizeof(GLuint
);
137 return sizeof(GLint
);
139 return sizeof(GLfloat
);
141 return sizeof(GLdouble
);
142 case GL_HALF_FLOAT_ARB
:
143 return sizeof(GLhalfARB
);
145 return sizeof(GLfixed
);
153 * Same as _mesa_sizeof_type() but also accepting the packed pixel
157 _mesa_sizeof_packed_type( GLenum type
)
162 case GL_UNSIGNED_BYTE
:
163 return sizeof(GLubyte
);
165 return sizeof(GLbyte
);
166 case GL_UNSIGNED_SHORT
:
167 return sizeof(GLushort
);
169 return sizeof(GLshort
);
170 case GL_UNSIGNED_INT
:
171 return sizeof(GLuint
);
173 return sizeof(GLint
);
174 case GL_HALF_FLOAT_ARB
:
175 return sizeof(GLhalfARB
);
177 return sizeof(GLfloat
);
178 case GL_UNSIGNED_BYTE_3_3_2
:
179 case GL_UNSIGNED_BYTE_2_3_3_REV
:
180 case MESA_UNSIGNED_BYTE_4_4
:
181 return sizeof(GLubyte
);
182 case GL_UNSIGNED_SHORT_5_6_5
:
183 case GL_UNSIGNED_SHORT_5_6_5_REV
:
184 case GL_UNSIGNED_SHORT_4_4_4_4
:
185 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
186 case GL_UNSIGNED_SHORT_5_5_5_1
:
187 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
188 case GL_UNSIGNED_SHORT_8_8_MESA
:
189 case GL_UNSIGNED_SHORT_8_8_REV_MESA
:
190 return sizeof(GLushort
);
191 case GL_UNSIGNED_INT_8_8_8_8
:
192 case GL_UNSIGNED_INT_8_8_8_8_REV
:
193 case GL_UNSIGNED_INT_10_10_10_2
:
194 case GL_UNSIGNED_INT_2_10_10_10_REV
:
195 case GL_UNSIGNED_INT_24_8_EXT
:
196 case GL_UNSIGNED_INT_5_9_9_9_REV
:
197 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
198 return sizeof(GLuint
);
199 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
208 * Get the number of components in a pixel format.
210 * \param format pixel format.
212 * \return the number of components in the given format, or -1 if a bad format.
215 _mesa_components_in_format( GLenum format
)
219 case GL_STENCIL_INDEX
:
220 case GL_DEPTH_COMPONENT
:
222 case GL_RED_INTEGER_EXT
:
224 case GL_GREEN_INTEGER_EXT
:
226 case GL_BLUE_INTEGER_EXT
:
228 case GL_ALPHA_INTEGER_EXT
:
230 case GL_LUMINANCE_INTEGER_EXT
:
234 case GL_LUMINANCE_ALPHA
:
235 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
238 case GL_DEPTH_STENCIL_EXT
:
246 case GL_RGB_INTEGER_EXT
:
247 case GL_BGR_INTEGER_EXT
:
253 case GL_RGBA_INTEGER_EXT
:
254 case GL_BGRA_INTEGER_EXT
:
264 * Get the bytes per pixel of pixel format type pair.
266 * \param format pixel format.
267 * \param type pixel type.
269 * \return bytes per pixel, or -1 if a bad format or type was given.
272 _mesa_bytes_per_pixel( GLenum format
, GLenum type
)
274 GLint comps
= _mesa_components_in_format( format
);
280 return 0; /* special case */
282 case GL_UNSIGNED_BYTE
:
283 return comps
* sizeof(GLubyte
);
285 case GL_UNSIGNED_SHORT
:
286 return comps
* sizeof(GLshort
);
288 case GL_UNSIGNED_INT
:
289 return comps
* sizeof(GLint
);
291 return comps
* sizeof(GLfloat
);
292 case GL_HALF_FLOAT_ARB
:
293 return comps
* sizeof(GLhalfARB
);
294 case GL_UNSIGNED_BYTE_3_3_2
:
295 case GL_UNSIGNED_BYTE_2_3_3_REV
:
296 if (format
== GL_RGB
|| format
== GL_BGR
||
297 format
== GL_RGB_INTEGER_EXT
|| format
== GL_BGR_INTEGER_EXT
)
298 return sizeof(GLubyte
);
300 return -1; /* error */
301 case GL_UNSIGNED_SHORT_5_6_5
:
302 case GL_UNSIGNED_SHORT_5_6_5_REV
:
303 if (format
== GL_RGB
|| format
== GL_BGR
||
304 format
== GL_RGB_INTEGER_EXT
|| format
== GL_BGR_INTEGER_EXT
)
305 return sizeof(GLushort
);
307 return -1; /* error */
308 case GL_UNSIGNED_SHORT_4_4_4_4
:
309 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
310 case GL_UNSIGNED_SHORT_5_5_5_1
:
311 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
312 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
||
313 format
== GL_RGBA_INTEGER_EXT
|| format
== GL_BGRA_INTEGER_EXT
)
314 return sizeof(GLushort
);
317 case GL_UNSIGNED_INT_8_8_8_8
:
318 case GL_UNSIGNED_INT_8_8_8_8_REV
:
319 case GL_UNSIGNED_INT_10_10_10_2
:
320 case GL_UNSIGNED_INT_2_10_10_10_REV
:
321 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
||
322 format
== GL_RGBA_INTEGER_EXT
|| format
== GL_BGRA_INTEGER_EXT
)
323 return sizeof(GLuint
);
326 case GL_UNSIGNED_SHORT_8_8_MESA
:
327 case GL_UNSIGNED_SHORT_8_8_REV_MESA
:
328 if (format
== GL_YCBCR_MESA
)
329 return sizeof(GLushort
);
332 case GL_UNSIGNED_INT_24_8_EXT
:
333 if (format
== GL_DEPTH_STENCIL_EXT
)
334 return sizeof(GLuint
);
337 case GL_UNSIGNED_INT_5_9_9_9_REV
:
338 if (format
== GL_RGB
)
339 return sizeof(GLuint
);
342 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
343 if (format
== GL_RGB
)
344 return sizeof(GLuint
);
347 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
348 if (format
== GL_DEPTH_STENCIL
)
359 * Test for a legal pixel format and type.
361 * \param format pixel format.
362 * \param type pixel type.
364 * \return GL_TRUE if the given pixel format and type are legal, or GL_FALSE
368 _mesa_is_legal_format_and_type(const struct gl_context
*ctx
,
369 GLenum format
, GLenum type
)
373 case GL_STENCIL_INDEX
:
377 case GL_UNSIGNED_BYTE
:
379 case GL_UNSIGNED_SHORT
:
381 case GL_UNSIGNED_INT
:
384 case GL_HALF_FLOAT_ARB
:
385 return ctx
->Extensions
.ARB_half_float_pixel
;
393 #if 0 /* not legal! see table 3.6 of the 1.5 spec */
397 case GL_LUMINANCE_ALPHA
:
398 case GL_DEPTH_COMPONENT
:
401 case GL_UNSIGNED_BYTE
:
403 case GL_UNSIGNED_SHORT
:
405 case GL_UNSIGNED_INT
:
408 case GL_HALF_FLOAT_ARB
:
409 return ctx
->Extensions
.ARB_half_float_pixel
;
414 if (!ctx
->Extensions
.ARB_texture_rg
)
419 case GL_UNSIGNED_BYTE
:
421 case GL_UNSIGNED_SHORT
:
423 case GL_UNSIGNED_INT
:
426 case GL_HALF_FLOAT_ARB
:
427 return ctx
->Extensions
.ARB_half_float_pixel
;
434 case GL_UNSIGNED_BYTE
:
436 case GL_UNSIGNED_SHORT
:
438 case GL_UNSIGNED_INT
:
440 case GL_UNSIGNED_BYTE_3_3_2
:
441 case GL_UNSIGNED_BYTE_2_3_3_REV
:
442 case GL_UNSIGNED_SHORT_5_6_5
:
443 case GL_UNSIGNED_SHORT_5_6_5_REV
:
445 case GL_HALF_FLOAT_ARB
:
446 return ctx
->Extensions
.ARB_half_float_pixel
;
447 case GL_UNSIGNED_INT_5_9_9_9_REV
:
448 return ctx
->Extensions
.EXT_texture_shared_exponent
;
449 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
450 return ctx
->Extensions
.EXT_packed_float
;
456 /* NOTE: no packed types are supported with BGR. That's
457 * intentional, according to the GL spec.
460 case GL_UNSIGNED_BYTE
:
462 case GL_UNSIGNED_SHORT
:
464 case GL_UNSIGNED_INT
:
467 case GL_HALF_FLOAT_ARB
:
468 return ctx
->Extensions
.ARB_half_float_pixel
;
477 case GL_UNSIGNED_BYTE
:
479 case GL_UNSIGNED_SHORT
:
481 case GL_UNSIGNED_INT
:
483 case GL_UNSIGNED_SHORT_4_4_4_4
:
484 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
485 case GL_UNSIGNED_SHORT_5_5_5_1
:
486 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
487 case GL_UNSIGNED_INT_8_8_8_8
:
488 case GL_UNSIGNED_INT_8_8_8_8_REV
:
489 case GL_UNSIGNED_INT_10_10_10_2
:
490 case GL_UNSIGNED_INT_2_10_10_10_REV
:
492 case GL_HALF_FLOAT_ARB
:
493 return ctx
->Extensions
.ARB_half_float_pixel
;
498 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
||
499 type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
)
503 case GL_DEPTH_STENCIL_EXT
:
504 if ((ctx
->Extensions
.EXT_packed_depth_stencil
&&
505 type
== GL_UNSIGNED_INT_24_8_EXT
) ||
506 (ctx
->Extensions
.ARB_depth_buffer_float
&&
507 type
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
))
515 case GL_UNSIGNED_BYTE
:
517 case GL_UNSIGNED_SHORT
:
519 case GL_UNSIGNED_INT
:
526 /* integer-valued formats */
527 case GL_RED_INTEGER_EXT
:
528 case GL_GREEN_INTEGER_EXT
:
529 case GL_BLUE_INTEGER_EXT
:
530 case GL_ALPHA_INTEGER_EXT
:
534 case GL_UNSIGNED_BYTE
:
536 case GL_UNSIGNED_SHORT
:
538 case GL_UNSIGNED_INT
:
539 return ctx
->Extensions
.EXT_texture_integer
;
544 case GL_RGB_INTEGER_EXT
:
547 case GL_UNSIGNED_BYTE
:
549 case GL_UNSIGNED_SHORT
:
551 case GL_UNSIGNED_INT
:
552 return ctx
->Extensions
.EXT_texture_integer
;
553 case GL_UNSIGNED_BYTE_3_3_2
:
554 case GL_UNSIGNED_BYTE_2_3_3_REV
:
555 case GL_UNSIGNED_SHORT_5_6_5
:
556 case GL_UNSIGNED_SHORT_5_6_5_REV
:
557 return ctx
->Extensions
.ARB_texture_rgb10_a2ui
;
562 case GL_BGR_INTEGER_EXT
:
565 case GL_UNSIGNED_BYTE
:
567 case GL_UNSIGNED_SHORT
:
569 case GL_UNSIGNED_INT
:
570 /* NOTE: no packed formats w/ BGR format */
571 return ctx
->Extensions
.EXT_texture_integer
;
576 case GL_RGBA_INTEGER_EXT
:
577 case GL_BGRA_INTEGER_EXT
:
580 case GL_UNSIGNED_BYTE
:
582 case GL_UNSIGNED_SHORT
:
584 case GL_UNSIGNED_INT
:
585 return ctx
->Extensions
.EXT_texture_integer
;
586 case GL_UNSIGNED_SHORT_4_4_4_4
:
587 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
588 case GL_UNSIGNED_SHORT_5_5_5_1
:
589 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
590 case GL_UNSIGNED_INT_8_8_8_8
:
591 case GL_UNSIGNED_INT_8_8_8_8_REV
:
592 case GL_UNSIGNED_INT_10_10_10_2
:
593 case GL_UNSIGNED_INT_2_10_10_10_REV
:
594 return ctx
->Extensions
.ARB_texture_rgb10_a2ui
;
599 case GL_LUMINANCE_INTEGER_EXT
:
600 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
603 case GL_UNSIGNED_BYTE
:
605 case GL_UNSIGNED_SHORT
:
607 case GL_UNSIGNED_INT
:
608 return ctx
->Extensions
.EXT_texture_integer
;
621 * Test if the given image format is a color/RGBA format (i.e., not color
622 * index, depth, stencil, etc).
623 * \param format the image format value (may by an internal texture format)
624 * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
627 _mesa_is_color_format(GLenum format
)
645 case GL_LUMINANCE_ALPHA
:
646 case GL_LUMINANCE4_ALPHA4
:
647 case GL_LUMINANCE6_ALPHA2
:
648 case GL_LUMINANCE8_ALPHA8
:
649 case GL_LUMINANCE12_ALPHA4
:
650 case GL_LUMINANCE12_ALPHA12
:
651 case GL_LUMINANCE16_ALPHA16
:
683 /* float texture formats */
684 case GL_ALPHA16F_ARB
:
685 case GL_ALPHA32F_ARB
:
686 case GL_LUMINANCE16F_ARB
:
687 case GL_LUMINANCE32F_ARB
:
688 case GL_LUMINANCE_ALPHA16F_ARB
:
689 case GL_LUMINANCE_ALPHA32F_ARB
:
690 case GL_INTENSITY16F_ARB
:
691 case GL_INTENSITY32F_ARB
:
700 /* compressed formats */
701 case GL_COMPRESSED_ALPHA
:
702 case GL_COMPRESSED_LUMINANCE
:
703 case GL_COMPRESSED_LUMINANCE_ALPHA
:
704 case GL_COMPRESSED_INTENSITY
:
705 case GL_COMPRESSED_RED
:
706 case GL_COMPRESSED_RG
:
707 case GL_COMPRESSED_RGB
:
708 case GL_COMPRESSED_RGBA
:
713 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
714 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
715 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
716 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
717 case GL_COMPRESSED_RGB_FXT1_3DFX
:
718 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
719 #if FEATURE_EXT_texture_sRGB
722 case GL_SRGB_ALPHA_EXT
:
723 case GL_SRGB8_ALPHA8_EXT
:
724 case GL_SLUMINANCE_ALPHA_EXT
:
725 case GL_SLUMINANCE8_ALPHA8_EXT
:
726 case GL_SLUMINANCE_EXT
:
727 case GL_SLUMINANCE8_EXT
:
728 case GL_COMPRESSED_SRGB_EXT
:
729 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
730 case GL_COMPRESSED_SRGB_ALPHA_EXT
:
731 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
732 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
733 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
734 case GL_COMPRESSED_SLUMINANCE_EXT
:
735 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT
:
736 #endif /* FEATURE_EXT_texture_sRGB */
737 case GL_COMPRESSED_RED_RGTC1
:
738 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
739 case GL_COMPRESSED_RG_RGTC2
:
740 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
741 case GL_COMPRESSED_LUMINANCE_LATC1_EXT
:
742 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT
:
743 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT
:
744 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT
:
745 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI
:
746 case GL_ETC1_RGB8_OES
:
747 /* generic integer formats */
748 case GL_RED_INTEGER_EXT
:
749 case GL_GREEN_INTEGER_EXT
:
750 case GL_BLUE_INTEGER_EXT
:
751 case GL_ALPHA_INTEGER_EXT
:
752 case GL_RGB_INTEGER_EXT
:
753 case GL_RGBA_INTEGER_EXT
:
754 case GL_BGR_INTEGER_EXT
:
755 case GL_BGRA_INTEGER_EXT
:
757 case GL_LUMINANCE_INTEGER_EXT
:
758 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
759 /* sized integer formats */
760 case GL_RGBA32UI_EXT
:
762 case GL_ALPHA32UI_EXT
:
763 case GL_INTENSITY32UI_EXT
:
764 case GL_LUMINANCE32UI_EXT
:
765 case GL_LUMINANCE_ALPHA32UI_EXT
:
766 case GL_RGBA16UI_EXT
:
768 case GL_ALPHA16UI_EXT
:
769 case GL_INTENSITY16UI_EXT
:
770 case GL_LUMINANCE16UI_EXT
:
771 case GL_LUMINANCE_ALPHA16UI_EXT
:
774 case GL_ALPHA8UI_EXT
:
775 case GL_INTENSITY8UI_EXT
:
776 case GL_LUMINANCE8UI_EXT
:
777 case GL_LUMINANCE_ALPHA8UI_EXT
:
780 case GL_ALPHA32I_EXT
:
781 case GL_INTENSITY32I_EXT
:
782 case GL_LUMINANCE32I_EXT
:
783 case GL_LUMINANCE_ALPHA32I_EXT
:
786 case GL_ALPHA16I_EXT
:
787 case GL_INTENSITY16I_EXT
:
788 case GL_LUMINANCE16I_EXT
:
789 case GL_LUMINANCE_ALPHA16I_EXT
:
793 case GL_INTENSITY8I_EXT
:
794 case GL_LUMINANCE8I_EXT
:
795 case GL_LUMINANCE_ALPHA8I_EXT
:
796 /* signed, normalized texture formats */
808 case GL_RGBA16_SNORM
:
810 case GL_ALPHA8_SNORM
:
811 case GL_ALPHA16_SNORM
:
812 case GL_LUMINANCE_SNORM
:
813 case GL_LUMINANCE8_SNORM
:
814 case GL_LUMINANCE16_SNORM
:
815 case GL_LUMINANCE_ALPHA_SNORM
:
816 case GL_LUMINANCE8_ALPHA8_SNORM
:
817 case GL_LUMINANCE16_ALPHA16_SNORM
:
818 case GL_INTENSITY_SNORM
:
819 case GL_INTENSITY8_SNORM
:
820 case GL_INTENSITY16_SNORM
:
822 case GL_R11F_G11F_B10F
:
825 case GL_YCBCR_MESA
: /* not considered to be RGB */
834 * Test if the given image format is a depth component format.
837 _mesa_is_depth_format(GLenum format
)
840 case GL_DEPTH_COMPONENT
:
841 case GL_DEPTH_COMPONENT16
:
842 case GL_DEPTH_COMPONENT24
:
843 case GL_DEPTH_COMPONENT32
:
844 case GL_DEPTH_COMPONENT32F
:
853 * Test if the given image format is a stencil format.
856 _mesa_is_stencil_format(GLenum format
)
859 case GL_STENCIL_INDEX
:
868 * Test if the given image format is a YCbCr format.
871 _mesa_is_ycbcr_format(GLenum format
)
883 * Test if the given image format is a depth+stencil format.
886 _mesa_is_depthstencil_format(GLenum format
)
889 case GL_DEPTH24_STENCIL8_EXT
:
890 case GL_DEPTH_STENCIL_EXT
:
891 case GL_DEPTH32F_STENCIL8
:
900 * Test if the given image format is a depth or stencil format.
903 _mesa_is_depth_or_stencil_format(GLenum format
)
906 case GL_DEPTH_COMPONENT
:
907 case GL_DEPTH_COMPONENT16
:
908 case GL_DEPTH_COMPONENT24
:
909 case GL_DEPTH_COMPONENT32
:
910 case GL_STENCIL_INDEX
:
911 case GL_STENCIL_INDEX1_EXT
:
912 case GL_STENCIL_INDEX4_EXT
:
913 case GL_STENCIL_INDEX8_EXT
:
914 case GL_STENCIL_INDEX16_EXT
:
915 case GL_DEPTH_STENCIL_EXT
:
916 case GL_DEPTH24_STENCIL8_EXT
:
917 case GL_DEPTH_COMPONENT32F
:
918 case GL_DEPTH32F_STENCIL8
:
927 * Test if the given image format is a dudv format.
930 _mesa_is_dudv_format(GLenum format
)
943 * Test if the given format is an integer (non-normalized) format.
946 _mesa_is_integer_format(GLenum format
)
949 /* generic integer formats */
950 case GL_RED_INTEGER_EXT
:
951 case GL_GREEN_INTEGER_EXT
:
952 case GL_BLUE_INTEGER_EXT
:
953 case GL_ALPHA_INTEGER_EXT
:
954 case GL_RGB_INTEGER_EXT
:
955 case GL_RGBA_INTEGER_EXT
:
956 case GL_BGR_INTEGER_EXT
:
957 case GL_BGRA_INTEGER_EXT
:
958 case GL_LUMINANCE_INTEGER_EXT
:
959 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
961 /* specific integer formats */
962 case GL_RGBA32UI_EXT
:
966 case GL_ALPHA32UI_EXT
:
967 case GL_INTENSITY32UI_EXT
:
968 case GL_LUMINANCE32UI_EXT
:
969 case GL_LUMINANCE_ALPHA32UI_EXT
:
970 case GL_RGBA16UI_EXT
:
974 case GL_ALPHA16UI_EXT
:
975 case GL_INTENSITY16UI_EXT
:
976 case GL_LUMINANCE16UI_EXT
:
977 case GL_LUMINANCE_ALPHA16UI_EXT
:
982 case GL_ALPHA8UI_EXT
:
983 case GL_INTENSITY8UI_EXT
:
984 case GL_LUMINANCE8UI_EXT
:
985 case GL_LUMINANCE_ALPHA8UI_EXT
:
990 case GL_ALPHA32I_EXT
:
991 case GL_INTENSITY32I_EXT
:
992 case GL_LUMINANCE32I_EXT
:
993 case GL_LUMINANCE_ALPHA32I_EXT
:
998 case GL_ALPHA16I_EXT
:
999 case GL_INTENSITY16I_EXT
:
1000 case GL_LUMINANCE16I_EXT
:
1001 case GL_LUMINANCE_ALPHA16I_EXT
:
1006 case GL_ALPHA8I_EXT
:
1007 case GL_INTENSITY8I_EXT
:
1008 case GL_LUMINANCE8I_EXT
:
1009 case GL_LUMINANCE_ALPHA8I_EXT
:
1019 * Test if an image format is a supported compressed format.
1020 * \param format the internal format token provided by the user.
1021 * \return GL_TRUE if compressed, GL_FALSE if uncompressed
1024 _mesa_is_compressed_format(struct gl_context
*ctx
, GLenum format
)
1027 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
1028 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
1029 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
1030 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
1031 return ctx
->Extensions
.EXT_texture_compression_s3tc
;
1036 return ctx
->Extensions
.S3_s3tc
;
1037 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
1038 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
1039 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
1040 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
1041 return ctx
->Extensions
.EXT_texture_sRGB
1042 && ctx
->Extensions
.EXT_texture_compression_s3tc
;
1043 case GL_COMPRESSED_RGB_FXT1_3DFX
:
1044 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
1045 return ctx
->Extensions
.TDFX_texture_compression_FXT1
;
1046 case GL_COMPRESSED_RED_RGTC1
:
1047 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
1048 case GL_COMPRESSED_RG_RGTC2
:
1049 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
1050 return ctx
->Extensions
.ARB_texture_compression_rgtc
;
1051 case GL_COMPRESSED_LUMINANCE_LATC1_EXT
:
1052 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT
:
1053 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT
:
1054 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT
:
1055 return ctx
->Extensions
.EXT_texture_compression_latc
;
1056 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI
:
1057 return ctx
->Extensions
.ATI_texture_compression_3dc
;
1058 case GL_ETC1_RGB8_OES
:
1059 return ctx
->Extensions
.OES_compressed_ETC1_RGB8_texture
;
1061 case GL_PALETTE4_RGB8_OES
:
1062 case GL_PALETTE4_RGBA8_OES
:
1063 case GL_PALETTE4_R5_G6_B5_OES
:
1064 case GL_PALETTE4_RGBA4_OES
:
1065 case GL_PALETTE4_RGB5_A1_OES
:
1066 case GL_PALETTE8_RGB8_OES
:
1067 case GL_PALETTE8_RGBA8_OES
:
1068 case GL_PALETTE8_R5_G6_B5_OES
:
1069 case GL_PALETTE8_RGBA4_OES
:
1070 case GL_PALETTE8_RGB5_A1_OES
:
1071 return ctx
->API
== API_OPENGLES
;
1080 * Does the given base texture/renderbuffer format have the channel
1084 _mesa_base_format_has_channel(GLenum base_format
, GLenum pname
)
1087 case GL_TEXTURE_RED_SIZE
:
1088 case GL_TEXTURE_RED_TYPE
:
1089 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1090 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
1091 if (base_format
== GL_RED
||
1092 base_format
== GL_RG
||
1093 base_format
== GL_RGB
||
1094 base_format
== GL_RGBA
) {
1098 case GL_TEXTURE_GREEN_SIZE
:
1099 case GL_TEXTURE_GREEN_TYPE
:
1100 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1101 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
1102 if (base_format
== GL_RG
||
1103 base_format
== GL_RGB
||
1104 base_format
== GL_RGBA
) {
1108 case GL_TEXTURE_BLUE_SIZE
:
1109 case GL_TEXTURE_BLUE_TYPE
:
1110 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1111 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
1112 if (base_format
== GL_RGB
||
1113 base_format
== GL_RGBA
) {
1117 case GL_TEXTURE_ALPHA_SIZE
:
1118 case GL_TEXTURE_ALPHA_TYPE
:
1119 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1120 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
1121 if (base_format
== GL_RGBA
||
1122 base_format
== GL_ALPHA
||
1123 base_format
== GL_LUMINANCE_ALPHA
) {
1127 case GL_TEXTURE_LUMINANCE_SIZE
:
1128 case GL_TEXTURE_LUMINANCE_TYPE
:
1129 if (base_format
== GL_LUMINANCE
||
1130 base_format
== GL_LUMINANCE_ALPHA
) {
1134 case GL_TEXTURE_INTENSITY_SIZE
:
1135 case GL_TEXTURE_INTENSITY_TYPE
:
1136 if (base_format
== GL_INTENSITY
) {
1140 case GL_TEXTURE_DEPTH_SIZE
:
1141 case GL_TEXTURE_DEPTH_TYPE
:
1142 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1143 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
1144 if (base_format
== GL_DEPTH_STENCIL
||
1145 base_format
== GL_DEPTH_COMPONENT
) {
1149 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1150 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
1151 if (base_format
== GL_DEPTH_STENCIL
||
1152 base_format
== GL_STENCIL_INDEX
) {
1157 _mesa_warning(NULL
, "%s: Unexpected channel token 0x%x\n",
1158 __FUNCTION__
, pname
);
1167 * Return the byte offset of a specific pixel in an image (1D, 2D or 3D).
1169 * Pixel unpacking/packing parameters are observed according to \p packing.
1171 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
1172 * \param packing the pixelstore attributes
1173 * \param width the image width
1174 * \param height the image height
1175 * \param format the pixel format (must be validated beforehand)
1176 * \param type the pixel data type (must be validated beforehand)
1177 * \param img which image in the volume (0 for 1D or 2D images)
1178 * \param row row of pixel in the image (0 for 1D images)
1179 * \param column column of pixel in the image
1181 * \return offset of pixel.
1183 * \sa gl_pixelstore_attrib.
1186 _mesa_image_offset( GLuint dimensions
,
1187 const struct gl_pixelstore_attrib
*packing
,
1188 GLsizei width
, GLsizei height
,
1189 GLenum format
, GLenum type
,
1190 GLint img
, GLint row
, GLint column
)
1192 GLint alignment
; /* 1, 2 or 4 */
1193 GLint pixels_per_row
;
1194 GLint rows_per_image
;
1197 GLint skipimages
; /* for 3-D volume images */
1200 ASSERT(dimensions
>= 1 && dimensions
<= 3);
1202 alignment
= packing
->Alignment
;
1203 if (packing
->RowLength
> 0) {
1204 pixels_per_row
= packing
->RowLength
;
1207 pixels_per_row
= width
;
1209 if (packing
->ImageHeight
> 0) {
1210 rows_per_image
= packing
->ImageHeight
;
1213 rows_per_image
= height
;
1216 skippixels
= packing
->SkipPixels
;
1217 /* Note: SKIP_ROWS _is_ used for 1D images */
1218 skiprows
= packing
->SkipRows
;
1219 /* Note: SKIP_IMAGES is only used for 3D images */
1220 skipimages
= (dimensions
== 3) ? packing
->SkipImages
: 0;
1222 if (type
== GL_BITMAP
) {
1224 GLint bytes_per_row
;
1225 GLint bytes_per_image
;
1226 /* components per pixel for color or stencil index: */
1227 const GLint comp_per_pixel
= 1;
1229 /* The pixel type and format should have been error checked earlier */
1230 assert(format
== GL_COLOR_INDEX
|| format
== GL_STENCIL_INDEX
);
1232 bytes_per_row
= alignment
1233 * CEILING( comp_per_pixel
*pixels_per_row
, 8*alignment
);
1235 bytes_per_image
= bytes_per_row
* rows_per_image
;
1237 offset
= (skipimages
+ img
) * bytes_per_image
1238 + (skiprows
+ row
) * bytes_per_row
1239 + (skippixels
+ column
) / 8;
1242 /* Non-BITMAP data */
1243 GLint bytes_per_pixel
, bytes_per_row
, remainder
, bytes_per_image
;
1246 bytes_per_pixel
= _mesa_bytes_per_pixel( format
, type
);
1248 /* The pixel type and format should have been error checked earlier */
1249 assert(bytes_per_pixel
> 0);
1251 bytes_per_row
= pixels_per_row
* bytes_per_pixel
;
1252 remainder
= bytes_per_row
% alignment
;
1254 bytes_per_row
+= (alignment
- remainder
);
1256 ASSERT(bytes_per_row
% alignment
== 0);
1258 bytes_per_image
= bytes_per_row
* rows_per_image
;
1260 if (packing
->Invert
) {
1261 /* set pixel_addr to the last row */
1262 topOfImage
= bytes_per_row
* (height
- 1);
1263 bytes_per_row
= -bytes_per_row
;
1269 /* compute final pixel address */
1270 offset
= (skipimages
+ img
) * bytes_per_image
1272 + (skiprows
+ row
) * bytes_per_row
1273 + (skippixels
+ column
) * bytes_per_pixel
;
1281 * Return the address of a specific pixel in an image (1D, 2D or 3D).
1283 * Pixel unpacking/packing parameters are observed according to \p packing.
1285 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
1286 * \param packing the pixelstore attributes
1287 * \param image starting address of image data
1288 * \param width the image width
1289 * \param height the image height
1290 * \param format the pixel format (must be validated beforehand)
1291 * \param type the pixel data type (must be validated beforehand)
1292 * \param img which image in the volume (0 for 1D or 2D images)
1293 * \param row row of pixel in the image (0 for 1D images)
1294 * \param column column of pixel in the image
1296 * \return address of pixel.
1298 * \sa gl_pixelstore_attrib.
1301 _mesa_image_address( GLuint dimensions
,
1302 const struct gl_pixelstore_attrib
*packing
,
1303 const GLvoid
*image
,
1304 GLsizei width
, GLsizei height
,
1305 GLenum format
, GLenum type
,
1306 GLint img
, GLint row
, GLint column
)
1308 const GLubyte
*addr
= (const GLubyte
*) image
;
1310 addr
+= _mesa_image_offset(dimensions
, packing
, width
, height
,
1311 format
, type
, img
, row
, column
);
1313 return (GLvoid
*) addr
;
1318 _mesa_image_address1d( const struct gl_pixelstore_attrib
*packing
,
1319 const GLvoid
*image
,
1321 GLenum format
, GLenum type
,
1324 return _mesa_image_address(1, packing
, image
, width
, 1,
1325 format
, type
, 0, 0, column
);
1330 _mesa_image_address2d( const struct gl_pixelstore_attrib
*packing
,
1331 const GLvoid
*image
,
1332 GLsizei width
, GLsizei height
,
1333 GLenum format
, GLenum type
,
1334 GLint row
, GLint column
)
1336 return _mesa_image_address(2, packing
, image
, width
, height
,
1337 format
, type
, 0, row
, column
);
1342 _mesa_image_address3d( const struct gl_pixelstore_attrib
*packing
,
1343 const GLvoid
*image
,
1344 GLsizei width
, GLsizei height
,
1345 GLenum format
, GLenum type
,
1346 GLint img
, GLint row
, GLint column
)
1348 return _mesa_image_address(3, packing
, image
, width
, height
,
1349 format
, type
, img
, row
, column
);
1355 * Compute the stride (in bytes) between image rows.
1357 * \param packing the pixelstore attributes
1358 * \param width image width.
1359 * \param format pixel format.
1360 * \param type pixel data type.
1362 * \return the stride in bytes for the given parameters, or -1 if error
1365 _mesa_image_row_stride( const struct gl_pixelstore_attrib
*packing
,
1366 GLint width
, GLenum format
, GLenum type
)
1368 GLint bytesPerRow
, remainder
;
1372 if (type
== GL_BITMAP
) {
1373 if (packing
->RowLength
== 0) {
1374 bytesPerRow
= (width
+ 7) / 8;
1377 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
1381 /* Non-BITMAP data */
1382 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1383 if (bytesPerPixel
<= 0)
1384 return -1; /* error */
1385 if (packing
->RowLength
== 0) {
1386 bytesPerRow
= bytesPerPixel
* width
;
1389 bytesPerRow
= bytesPerPixel
* packing
->RowLength
;
1393 remainder
= bytesPerRow
% packing
->Alignment
;
1394 if (remainder
> 0) {
1395 bytesPerRow
+= (packing
->Alignment
- remainder
);
1398 if (packing
->Invert
) {
1399 /* negate the bytes per row (negative row stride) */
1400 bytesPerRow
= -bytesPerRow
;
1408 * Compute the stride between images in a 3D texture (in bytes) for the given
1409 * pixel packing parameters and image width, format and type.
1412 _mesa_image_image_stride( const struct gl_pixelstore_attrib
*packing
,
1413 GLint width
, GLint height
,
1414 GLenum format
, GLenum type
)
1416 GLint bytesPerRow
, bytesPerImage
, remainder
;
1420 if (type
== GL_BITMAP
) {
1421 if (packing
->RowLength
== 0) {
1422 bytesPerRow
= (width
+ 7) / 8;
1425 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
1429 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1431 if (bytesPerPixel
<= 0)
1432 return -1; /* error */
1433 if (packing
->RowLength
== 0) {
1434 bytesPerRow
= bytesPerPixel
* width
;
1437 bytesPerRow
= bytesPerPixel
* packing
->RowLength
;
1441 remainder
= bytesPerRow
% packing
->Alignment
;
1443 bytesPerRow
+= (packing
->Alignment
- remainder
);
1445 if (packing
->ImageHeight
== 0)
1446 bytesPerImage
= bytesPerRow
* height
;
1448 bytesPerImage
= bytesPerRow
* packing
->ImageHeight
;
1450 return bytesPerImage
;
1456 * "Expand" a bitmap from 1-bit per pixel to 8-bits per pixel.
1457 * This is typically used to convert a bitmap into a GLubyte/pixel texture.
1458 * "On" bits will set texels to \p onValue.
1459 * "Off" bits will not modify texels.
1460 * \param width src bitmap width in pixels
1461 * \param height src bitmap height in pixels
1462 * \param unpack bitmap unpacking state
1463 * \param bitmap the src bitmap data
1464 * \param destBuffer start of dest buffer
1465 * \param destStride row stride in dest buffer
1466 * \param onValue if bit is 1, set destBuffer pixel to this value
1469 _mesa_expand_bitmap(GLsizei width
, GLsizei height
,
1470 const struct gl_pixelstore_attrib
*unpack
,
1471 const GLubyte
*bitmap
,
1472 GLubyte
*destBuffer
, GLint destStride
,
1475 const GLubyte
*srcRow
= (const GLubyte
*)
1476 _mesa_image_address2d(unpack
, bitmap
, width
, height
,
1477 GL_COLOR_INDEX
, GL_BITMAP
, 0, 0);
1478 const GLint srcStride
= _mesa_image_row_stride(unpack
, width
,
1479 GL_COLOR_INDEX
, GL_BITMAP
);
1482 #define SET_PIXEL(COL, ROW) \
1483 destBuffer[(ROW) * destStride + (COL)] = onValue;
1485 for (row
= 0; row
< height
; row
++) {
1486 const GLubyte
*src
= srcRow
;
1488 if (unpack
->LsbFirst
) {
1490 GLubyte mask
= 1U << (unpack
->SkipPixels
& 0x7);
1491 for (col
= 0; col
< width
; col
++) {
1494 SET_PIXEL(col
, row
);
1506 /* get ready for next row */
1512 GLubyte mask
= 128U >> (unpack
->SkipPixels
& 0x7);
1513 for (col
= 0; col
< width
; col
++) {
1516 SET_PIXEL(col
, row
);
1528 /* get ready for next row */
1533 srcRow
+= srcStride
;
1543 * Convert an array of RGBA colors from one datatype to another.
1544 * NOTE: src may equal dst. In that case, we use a temporary buffer.
1547 _mesa_convert_colors(GLenum srcType
, const GLvoid
*src
,
1548 GLenum dstType
, GLvoid
*dst
,
1549 GLuint count
, const GLubyte mask
[])
1552 const GLboolean useTemp
= (src
== dst
);
1554 tempBuffer
= malloc(count
* MAX_PIXEL_BYTES
);
1558 ASSERT(srcType
!= dstType
);
1561 case GL_UNSIGNED_BYTE
:
1562 if (dstType
== GL_UNSIGNED_SHORT
) {
1563 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
1564 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
1566 for (i
= 0; i
< count
; i
++) {
1567 if (!mask
|| mask
[i
]) {
1568 dst2
[i
][RCOMP
] = UBYTE_TO_USHORT(src1
[i
][RCOMP
]);
1569 dst2
[i
][GCOMP
] = UBYTE_TO_USHORT(src1
[i
][GCOMP
]);
1570 dst2
[i
][BCOMP
] = UBYTE_TO_USHORT(src1
[i
][BCOMP
]);
1571 dst2
[i
][ACOMP
] = UBYTE_TO_USHORT(src1
[i
][ACOMP
]);
1575 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
1578 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
1579 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
1581 ASSERT(dstType
== GL_FLOAT
);
1582 for (i
= 0; i
< count
; i
++) {
1583 if (!mask
|| mask
[i
]) {
1584 dst4
[i
][RCOMP
] = UBYTE_TO_FLOAT(src1
[i
][RCOMP
]);
1585 dst4
[i
][GCOMP
] = UBYTE_TO_FLOAT(src1
[i
][GCOMP
]);
1586 dst4
[i
][BCOMP
] = UBYTE_TO_FLOAT(src1
[i
][BCOMP
]);
1587 dst4
[i
][ACOMP
] = UBYTE_TO_FLOAT(src1
[i
][ACOMP
]);
1591 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
1594 case GL_UNSIGNED_SHORT
:
1595 if (dstType
== GL_UNSIGNED_BYTE
) {
1596 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
1597 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
1599 for (i
= 0; i
< count
; i
++) {
1600 if (!mask
|| mask
[i
]) {
1601 dst1
[i
][RCOMP
] = USHORT_TO_UBYTE(src2
[i
][RCOMP
]);
1602 dst1
[i
][GCOMP
] = USHORT_TO_UBYTE(src2
[i
][GCOMP
]);
1603 dst1
[i
][BCOMP
] = USHORT_TO_UBYTE(src2
[i
][BCOMP
]);
1604 dst1
[i
][ACOMP
] = USHORT_TO_UBYTE(src2
[i
][ACOMP
]);
1608 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
1611 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
1612 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
1614 ASSERT(dstType
== GL_FLOAT
);
1615 for (i
= 0; i
< count
; i
++) {
1616 if (!mask
|| mask
[i
]) {
1617 dst4
[i
][RCOMP
] = USHORT_TO_FLOAT(src2
[i
][RCOMP
]);
1618 dst4
[i
][GCOMP
] = USHORT_TO_FLOAT(src2
[i
][GCOMP
]);
1619 dst4
[i
][BCOMP
] = USHORT_TO_FLOAT(src2
[i
][BCOMP
]);
1620 dst4
[i
][ACOMP
] = USHORT_TO_FLOAT(src2
[i
][ACOMP
]);
1624 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
1628 if (dstType
== GL_UNSIGNED_BYTE
) {
1629 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
1630 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
1632 for (i
= 0; i
< count
; i
++) {
1633 if (!mask
|| mask
[i
])
1634 _mesa_unclamped_float_rgba_to_ubyte(dst1
[i
], src4
[i
]);
1637 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
1640 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
1641 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
1643 ASSERT(dstType
== GL_UNSIGNED_SHORT
);
1644 for (i
= 0; i
< count
; i
++) {
1645 if (!mask
|| mask
[i
]) {
1646 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][RCOMP
], src4
[i
][RCOMP
]);
1647 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][GCOMP
], src4
[i
][GCOMP
]);
1648 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][BCOMP
], src4
[i
][BCOMP
]);
1649 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][ACOMP
], src4
[i
][ACOMP
]);
1653 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
1657 _mesa_problem(NULL
, "Invalid datatype in _mesa_convert_colors");
1667 * Perform basic clipping for glDrawPixels. The image's position and size
1668 * and the unpack SkipPixels and SkipRows are adjusted so that the image
1669 * region is entirely within the window and scissor bounds.
1670 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
1671 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
1672 * we'll actually write. Beforehand, *destY-1 is the first drawing row.
1674 * \return GL_TRUE if image is ready for drawing or
1675 * GL_FALSE if image was completely clipped away (draw nothing)
1678 _mesa_clip_drawpixels(const struct gl_context
*ctx
,
1679 GLint
*destX
, GLint
*destY
,
1680 GLsizei
*width
, GLsizei
*height
,
1681 struct gl_pixelstore_attrib
*unpack
)
1683 const struct gl_framebuffer
*buffer
= ctx
->DrawBuffer
;
1685 if (unpack
->RowLength
== 0) {
1686 unpack
->RowLength
= *width
;
1689 ASSERT(ctx
->Pixel
.ZoomX
== 1.0F
);
1690 ASSERT(ctx
->Pixel
.ZoomY
== 1.0F
|| ctx
->Pixel
.ZoomY
== -1.0F
);
1693 if (*destX
< buffer
->_Xmin
) {
1694 unpack
->SkipPixels
+= (buffer
->_Xmin
- *destX
);
1695 *width
-= (buffer
->_Xmin
- *destX
);
1696 *destX
= buffer
->_Xmin
;
1698 /* right clipping */
1699 if (*destX
+ *width
> buffer
->_Xmax
)
1700 *width
-= (*destX
+ *width
- buffer
->_Xmax
);
1705 if (ctx
->Pixel
.ZoomY
== 1.0F
) {
1706 /* bottom clipping */
1707 if (*destY
< buffer
->_Ymin
) {
1708 unpack
->SkipRows
+= (buffer
->_Ymin
- *destY
);
1709 *height
-= (buffer
->_Ymin
- *destY
);
1710 *destY
= buffer
->_Ymin
;
1713 if (*destY
+ *height
> buffer
->_Ymax
)
1714 *height
-= (*destY
+ *height
- buffer
->_Ymax
);
1716 else { /* upside down */
1718 if (*destY
> buffer
->_Ymax
) {
1719 unpack
->SkipRows
+= (*destY
- buffer
->_Ymax
);
1720 *height
-= (*destY
- buffer
->_Ymax
);
1721 *destY
= buffer
->_Ymax
;
1723 /* bottom clipping */
1724 if (*destY
- *height
< buffer
->_Ymin
)
1725 *height
-= (buffer
->_Ymin
- (*destY
- *height
));
1726 /* adjust destY so it's the first row to write to */
1738 * Perform clipping for glReadPixels. The image's window position
1739 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
1740 * so that the image region is entirely within the window bounds.
1741 * Note: this is different from _mesa_clip_drawpixels() in that the
1742 * scissor box is ignored, and we use the bounds of the current readbuffer
1745 * \return GL_TRUE if region to read is in bounds
1746 * GL_FALSE if region is completely out of bounds (nothing to read)
1749 _mesa_clip_readpixels(const struct gl_context
*ctx
,
1750 GLint
*srcX
, GLint
*srcY
,
1751 GLsizei
*width
, GLsizei
*height
,
1752 struct gl_pixelstore_attrib
*pack
)
1754 const struct gl_framebuffer
*buffer
= ctx
->ReadBuffer
;
1756 if (pack
->RowLength
== 0) {
1757 pack
->RowLength
= *width
;
1762 pack
->SkipPixels
+= (0 - *srcX
);
1763 *width
-= (0 - *srcX
);
1766 /* right clipping */
1767 if (*srcX
+ *width
> (GLsizei
) buffer
->Width
)
1768 *width
-= (*srcX
+ *width
- buffer
->Width
);
1773 /* bottom clipping */
1775 pack
->SkipRows
+= (0 - *srcY
);
1776 *height
-= (0 - *srcY
);
1780 if (*srcY
+ *height
> (GLsizei
) buffer
->Height
)
1781 *height
-= (*srcY
+ *height
- buffer
->Height
);
1791 * Do clipping for a glCopyTexSubImage call.
1792 * The framebuffer source region might extend outside the framebuffer
1793 * bounds. Clip the source region against the framebuffer bounds and
1794 * adjust the texture/dest position and size accordingly.
1796 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
1799 _mesa_clip_copytexsubimage(const struct gl_context
*ctx
,
1800 GLint
*destX
, GLint
*destY
,
1801 GLint
*srcX
, GLint
*srcY
,
1802 GLsizei
*width
, GLsizei
*height
)
1804 const struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1805 const GLint srcX0
= *srcX
, srcY0
= *srcY
;
1807 if (_mesa_clip_to_region(0, 0, fb
->Width
, fb
->Height
,
1808 srcX
, srcY
, width
, height
)) {
1809 *destX
= *destX
+ *srcX
- srcX0
;
1810 *destY
= *destY
+ *srcY
- srcY0
;
1822 * Clip the rectangle defined by (x, y, width, height) against the bounds
1823 * specified by [xmin, xmax) and [ymin, ymax).
1824 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
1827 _mesa_clip_to_region(GLint xmin
, GLint ymin
,
1828 GLint xmax
, GLint ymax
,
1830 GLsizei
*width
, GLsizei
*height
)
1834 *width
-= (xmin
- *x
);
1838 /* right clipping */
1839 if (*x
+ *width
> xmax
)
1840 *width
-= (*x
+ *width
- xmax
);
1845 /* bottom (or top) clipping */
1847 *height
-= (ymin
- *y
);
1851 /* top (or bottom) clipping */
1852 if (*y
+ *height
> ymax
)
1853 *height
-= (*y
+ *height
- ymax
);
1863 * Clip dst coords against Xmax (or Ymax).
1866 clip_right_or_top(GLint
*srcX0
, GLint
*srcX1
,
1867 GLint
*dstX0
, GLint
*dstX1
,
1872 if (*dstX1
> maxValue
) {
1873 /* X1 outside right edge */
1874 ASSERT(*dstX0
< maxValue
); /* X0 should be inside right edge */
1875 t
= (GLfloat
) (maxValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
1876 /* chop off [t, 1] part */
1877 ASSERT(t
>= 0.0 && t
<= 1.0);
1879 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
1880 *srcX1
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
1882 else if (*dstX0
> maxValue
) {
1883 /* X0 outside right edge */
1884 ASSERT(*dstX1
< maxValue
); /* X1 should be inside right edge */
1885 t
= (GLfloat
) (maxValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
1886 /* chop off [t, 1] part */
1887 ASSERT(t
>= 0.0 && t
<= 1.0);
1889 bias
= (*srcX0
< *srcX1
) ? -0.5F
: 0.5F
;
1890 *srcX0
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
1896 * Clip dst coords against Xmin (or Ymin).
1899 clip_left_or_bottom(GLint
*srcX0
, GLint
*srcX1
,
1900 GLint
*dstX0
, GLint
*dstX1
,
1905 if (*dstX0
< minValue
) {
1906 /* X0 outside left edge */
1907 ASSERT(*dstX1
> minValue
); /* X1 should be inside left edge */
1908 t
= (GLfloat
) (minValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
1909 /* chop off [0, t] part */
1910 ASSERT(t
>= 0.0 && t
<= 1.0);
1912 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
; /* flipped??? */
1913 *srcX0
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
1915 else if (*dstX1
< minValue
) {
1916 /* X1 outside left edge */
1917 ASSERT(*dstX0
> minValue
); /* X0 should be inside left edge */
1918 t
= (GLfloat
) (minValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
1919 /* chop off [0, t] part */
1920 ASSERT(t
>= 0.0 && t
<= 1.0);
1922 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
1923 *srcX1
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
1929 * Do clipping of blit src/dest rectangles.
1930 * The dest rect is clipped against both the buffer bounds and scissor bounds.
1931 * The src rect is just clipped against the buffer bounds.
1933 * When either the src or dest rect is clipped, the other is also clipped
1936 * Note that X0 need not be less than X1 (same for Y) for either the source
1937 * and dest rects. That makes the clipping a little trickier.
1939 * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped
1942 _mesa_clip_blit(struct gl_context
*ctx
,
1943 GLint
*srcX0
, GLint
*srcY0
, GLint
*srcX1
, GLint
*srcY1
,
1944 GLint
*dstX0
, GLint
*dstY0
, GLint
*dstX1
, GLint
*dstY1
)
1946 const GLint srcXmin
= 0;
1947 const GLint srcXmax
= ctx
->ReadBuffer
->Width
;
1948 const GLint srcYmin
= 0;
1949 const GLint srcYmax
= ctx
->ReadBuffer
->Height
;
1951 /* these include scissor bounds */
1952 const GLint dstXmin
= ctx
->DrawBuffer
->_Xmin
;
1953 const GLint dstXmax
= ctx
->DrawBuffer
->_Xmax
;
1954 const GLint dstYmin
= ctx
->DrawBuffer
->_Ymin
;
1955 const GLint dstYmax
= ctx
->DrawBuffer
->_Ymax
;
1958 printf("PreClipX: src: %d .. %d dst: %d .. %d\n",
1959 *srcX0, *srcX1, *dstX0, *dstX1);
1960 printf("PreClipY: src: %d .. %d dst: %d .. %d\n",
1961 *srcY0, *srcY1, *dstY0, *dstY1);
1964 /* trivial rejection tests */
1965 if (*dstX0
== *dstX1
)
1966 return GL_FALSE
; /* no width */
1967 if (*dstX0
<= dstXmin
&& *dstX1
<= dstXmin
)
1968 return GL_FALSE
; /* totally out (left) of bounds */
1969 if (*dstX0
>= dstXmax
&& *dstX1
>= dstXmax
)
1970 return GL_FALSE
; /* totally out (right) of bounds */
1972 if (*dstY0
== *dstY1
)
1974 if (*dstY0
<= dstYmin
&& *dstY1
<= dstYmin
)
1976 if (*dstY0
>= dstYmax
&& *dstY1
>= dstYmax
)
1979 if (*srcX0
== *srcX1
)
1981 if (*srcX0
<= srcXmin
&& *srcX1
<= srcXmin
)
1983 if (*srcX0
>= srcXmax
&& *srcX1
>= srcXmax
)
1986 if (*srcY0
== *srcY1
)
1988 if (*srcY0
<= srcYmin
&& *srcY1
<= srcYmin
)
1990 if (*srcY0
>= srcYmax
&& *srcY1
>= srcYmax
)
1996 clip_right_or_top(srcX0
, srcX1
, dstX0
, dstX1
, dstXmax
);
1997 clip_right_or_top(srcY0
, srcY1
, dstY0
, dstY1
, dstYmax
);
1998 clip_left_or_bottom(srcX0
, srcX1
, dstX0
, dstX1
, dstXmin
);
1999 clip_left_or_bottom(srcY0
, srcY1
, dstY0
, dstY1
, dstYmin
);
2002 * src clip (just swap src/dst values from above)
2004 clip_right_or_top(dstX0
, dstX1
, srcX0
, srcX1
, srcXmax
);
2005 clip_right_or_top(dstY0
, dstY1
, srcY0
, srcY1
, srcYmax
);
2006 clip_left_or_bottom(dstX0
, dstX1
, srcX0
, srcX1
, srcXmin
);
2007 clip_left_or_bottom(dstY0
, dstY1
, srcY0
, srcY1
, srcYmin
);
2010 printf("PostClipX: src: %d .. %d dst: %d .. %d\n",
2011 *srcX0, *srcX1, *dstX0, *dstX1);
2012 printf("PostClipY: src: %d .. %d dst: %d .. %d\n",
2013 *srcY0, *srcY1, *dstY0, *dstY1);
2016 ASSERT(*dstX0
>= dstXmin
);
2017 ASSERT(*dstX0
<= dstXmax
);
2018 ASSERT(*dstX1
>= dstXmin
);
2019 ASSERT(*dstX1
<= dstXmax
);
2021 ASSERT(*dstY0
>= dstYmin
);
2022 ASSERT(*dstY0
<= dstYmax
);
2023 ASSERT(*dstY1
>= dstYmin
);
2024 ASSERT(*dstY1
<= dstYmax
);
2026 ASSERT(*srcX0
>= srcXmin
);
2027 ASSERT(*srcX0
<= srcXmax
);
2028 ASSERT(*srcX1
>= srcXmin
);
2029 ASSERT(*srcX1
<= srcXmax
);
2031 ASSERT(*srcY0
>= srcYmin
);
2032 ASSERT(*srcY0
<= srcYmax
);
2033 ASSERT(*srcY1
>= srcYmin
);
2034 ASSERT(*srcY1
<= srcYmax
);