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
:
245 case GL_RGB_INTEGER_EXT
:
251 case GL_RGBA_INTEGER_EXT
:
261 * Get the bytes per pixel of pixel format type pair.
263 * \param format pixel format.
264 * \param type pixel type.
266 * \return bytes per pixel, or -1 if a bad format or type was given.
269 _mesa_bytes_per_pixel( GLenum format
, GLenum type
)
271 GLint comps
= _mesa_components_in_format( format
);
277 return 0; /* special case */
279 case GL_UNSIGNED_BYTE
:
280 return comps
* sizeof(GLubyte
);
282 case GL_UNSIGNED_SHORT
:
283 return comps
* sizeof(GLshort
);
285 case GL_UNSIGNED_INT
:
286 return comps
* sizeof(GLint
);
288 return comps
* sizeof(GLfloat
);
289 case GL_HALF_FLOAT_ARB
:
290 return comps
* sizeof(GLhalfARB
);
291 case GL_UNSIGNED_BYTE_3_3_2
:
292 case GL_UNSIGNED_BYTE_2_3_3_REV
:
293 if (format
== GL_RGB
|| format
== GL_BGR
||
294 format
== GL_RGB_INTEGER_EXT
|| format
== GL_BGR_INTEGER_EXT
)
295 return sizeof(GLubyte
);
297 return -1; /* error */
298 case GL_UNSIGNED_SHORT_5_6_5
:
299 case GL_UNSIGNED_SHORT_5_6_5_REV
:
300 if (format
== GL_RGB
|| format
== GL_BGR
||
301 format
== GL_RGB_INTEGER_EXT
|| format
== GL_BGR_INTEGER_EXT
)
302 return sizeof(GLushort
);
304 return -1; /* error */
305 case GL_UNSIGNED_SHORT_4_4_4_4
:
306 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
307 case GL_UNSIGNED_SHORT_5_5_5_1
:
308 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
309 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
||
310 format
== GL_RGBA_INTEGER_EXT
|| format
== GL_BGRA_INTEGER_EXT
)
311 return sizeof(GLushort
);
314 case GL_UNSIGNED_INT_8_8_8_8
:
315 case GL_UNSIGNED_INT_8_8_8_8_REV
:
316 case GL_UNSIGNED_INT_10_10_10_2
:
317 case GL_UNSIGNED_INT_2_10_10_10_REV
:
318 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
||
319 format
== GL_RGBA_INTEGER_EXT
|| format
== GL_BGRA_INTEGER_EXT
)
320 return sizeof(GLuint
);
323 case GL_UNSIGNED_SHORT_8_8_MESA
:
324 case GL_UNSIGNED_SHORT_8_8_REV_MESA
:
325 if (format
== GL_YCBCR_MESA
)
326 return sizeof(GLushort
);
329 case GL_UNSIGNED_INT_24_8_EXT
:
330 if (format
== GL_DEPTH_STENCIL_EXT
)
331 return sizeof(GLuint
);
334 case GL_UNSIGNED_INT_5_9_9_9_REV
:
335 if (format
== GL_RGB
)
336 return sizeof(GLuint
);
339 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
340 if (format
== GL_RGB
)
341 return sizeof(GLuint
);
344 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
345 if (format
== GL_DEPTH_STENCIL
)
356 * Test for a legal pixel format and type.
358 * \param format pixel format.
359 * \param type pixel type.
361 * \return GL_TRUE if the given pixel format and type are legal, or GL_FALSE
365 _mesa_is_legal_format_and_type(const struct gl_context
*ctx
,
366 GLenum format
, GLenum type
)
370 case GL_STENCIL_INDEX
:
374 case GL_UNSIGNED_BYTE
:
376 case GL_UNSIGNED_SHORT
:
378 case GL_UNSIGNED_INT
:
381 case GL_HALF_FLOAT_ARB
:
382 return ctx
->Extensions
.ARB_half_float_pixel
;
390 #if 0 /* not legal! see table 3.6 of the 1.5 spec */
394 case GL_LUMINANCE_ALPHA
:
395 case GL_DEPTH_COMPONENT
:
398 case GL_UNSIGNED_BYTE
:
400 case GL_UNSIGNED_SHORT
:
402 case GL_UNSIGNED_INT
:
405 case GL_HALF_FLOAT_ARB
:
406 return ctx
->Extensions
.ARB_half_float_pixel
;
411 if (!ctx
->Extensions
.ARB_texture_rg
)
416 case GL_UNSIGNED_BYTE
:
418 case GL_UNSIGNED_SHORT
:
420 case GL_UNSIGNED_INT
:
423 case GL_HALF_FLOAT_ARB
:
424 return ctx
->Extensions
.ARB_half_float_pixel
;
431 case GL_UNSIGNED_BYTE
:
433 case GL_UNSIGNED_SHORT
:
435 case GL_UNSIGNED_INT
:
437 case GL_UNSIGNED_BYTE_3_3_2
:
438 case GL_UNSIGNED_BYTE_2_3_3_REV
:
439 case GL_UNSIGNED_SHORT_5_6_5
:
440 case GL_UNSIGNED_SHORT_5_6_5_REV
:
442 case GL_HALF_FLOAT_ARB
:
443 return ctx
->Extensions
.ARB_half_float_pixel
;
444 case GL_UNSIGNED_INT_5_9_9_9_REV
:
445 return ctx
->Extensions
.EXT_texture_shared_exponent
;
446 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
447 return ctx
->Extensions
.EXT_packed_float
;
453 /* NOTE: no packed types are supported with BGR. That's
454 * intentional, according to the GL spec.
457 case GL_UNSIGNED_BYTE
:
459 case GL_UNSIGNED_SHORT
:
461 case GL_UNSIGNED_INT
:
464 case GL_HALF_FLOAT_ARB
:
465 return ctx
->Extensions
.ARB_half_float_pixel
;
474 case GL_UNSIGNED_BYTE
:
476 case GL_UNSIGNED_SHORT
:
478 case GL_UNSIGNED_INT
:
480 case GL_UNSIGNED_SHORT_4_4_4_4
:
481 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
482 case GL_UNSIGNED_SHORT_5_5_5_1
:
483 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
484 case GL_UNSIGNED_INT_8_8_8_8
:
485 case GL_UNSIGNED_INT_8_8_8_8_REV
:
486 case GL_UNSIGNED_INT_10_10_10_2
:
487 case GL_UNSIGNED_INT_2_10_10_10_REV
:
489 case GL_HALF_FLOAT_ARB
:
490 return ctx
->Extensions
.ARB_half_float_pixel
;
495 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
||
496 type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
)
500 case GL_DEPTH_STENCIL_EXT
:
501 if ((ctx
->Extensions
.EXT_packed_depth_stencil
&&
502 type
== GL_UNSIGNED_INT_24_8_EXT
) ||
503 (ctx
->Extensions
.ARB_depth_buffer_float
&&
504 type
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
))
512 case GL_UNSIGNED_BYTE
:
514 case GL_UNSIGNED_SHORT
:
516 case GL_UNSIGNED_INT
:
523 /* integer-valued formats */
524 case GL_RED_INTEGER_EXT
:
525 case GL_GREEN_INTEGER_EXT
:
526 case GL_BLUE_INTEGER_EXT
:
527 case GL_ALPHA_INTEGER_EXT
:
530 case GL_UNSIGNED_BYTE
:
532 case GL_UNSIGNED_SHORT
:
534 case GL_UNSIGNED_INT
:
535 return ctx
->Extensions
.EXT_texture_integer
;
540 case GL_RGB_INTEGER_EXT
:
543 case GL_UNSIGNED_BYTE
:
545 case GL_UNSIGNED_SHORT
:
547 case GL_UNSIGNED_INT
:
548 case GL_UNSIGNED_BYTE_3_3_2
:
549 case GL_UNSIGNED_BYTE_2_3_3_REV
:
550 case GL_UNSIGNED_SHORT_5_6_5
:
551 case GL_UNSIGNED_SHORT_5_6_5_REV
:
552 return ctx
->Extensions
.EXT_texture_integer
;
557 case GL_BGR_INTEGER_EXT
:
560 case GL_UNSIGNED_BYTE
:
562 case GL_UNSIGNED_SHORT
:
564 case GL_UNSIGNED_INT
:
565 /* NOTE: no packed formats w/ BGR format */
566 return ctx
->Extensions
.EXT_texture_integer
;
571 case GL_RGBA_INTEGER_EXT
:
572 case GL_BGRA_INTEGER_EXT
:
575 case GL_UNSIGNED_BYTE
:
577 case GL_UNSIGNED_SHORT
:
579 case GL_UNSIGNED_INT
:
580 case GL_UNSIGNED_SHORT_4_4_4_4
:
581 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
582 case GL_UNSIGNED_SHORT_5_5_5_1
:
583 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
584 case GL_UNSIGNED_INT_8_8_8_8
:
585 case GL_UNSIGNED_INT_8_8_8_8_REV
:
586 case GL_UNSIGNED_INT_10_10_10_2
:
587 case GL_UNSIGNED_INT_2_10_10_10_REV
:
588 return ctx
->Extensions
.EXT_texture_integer
;
593 case GL_LUMINANCE_INTEGER_EXT
:
594 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
597 case GL_UNSIGNED_BYTE
:
599 case GL_UNSIGNED_SHORT
:
601 case GL_UNSIGNED_INT
:
602 return ctx
->Extensions
.EXT_texture_integer
;
615 * Test if the given image format is a color/RGBA format (i.e., not color
616 * index, depth, stencil, etc).
617 * \param format the image format value (may by an internal texture format)
618 * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
621 _mesa_is_color_format(GLenum format
)
639 case GL_LUMINANCE_ALPHA
:
640 case GL_LUMINANCE4_ALPHA4
:
641 case GL_LUMINANCE6_ALPHA2
:
642 case GL_LUMINANCE8_ALPHA8
:
643 case GL_LUMINANCE12_ALPHA4
:
644 case GL_LUMINANCE12_ALPHA12
:
645 case GL_LUMINANCE16_ALPHA16
:
677 /* float texture formats */
678 case GL_ALPHA16F_ARB
:
679 case GL_ALPHA32F_ARB
:
680 case GL_LUMINANCE16F_ARB
:
681 case GL_LUMINANCE32F_ARB
:
682 case GL_LUMINANCE_ALPHA16F_ARB
:
683 case GL_LUMINANCE_ALPHA32F_ARB
:
684 case GL_INTENSITY16F_ARB
:
685 case GL_INTENSITY32F_ARB
:
694 /* compressed formats */
695 case GL_COMPRESSED_ALPHA
:
696 case GL_COMPRESSED_LUMINANCE
:
697 case GL_COMPRESSED_LUMINANCE_ALPHA
:
698 case GL_COMPRESSED_INTENSITY
:
699 case GL_COMPRESSED_RED
:
700 case GL_COMPRESSED_RG
:
701 case GL_COMPRESSED_RGB
:
702 case GL_COMPRESSED_RGBA
:
707 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
708 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
709 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
710 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
711 case GL_COMPRESSED_RGB_FXT1_3DFX
:
712 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
713 #if FEATURE_EXT_texture_sRGB
716 case GL_SRGB_ALPHA_EXT
:
717 case GL_SRGB8_ALPHA8_EXT
:
718 case GL_SLUMINANCE_ALPHA_EXT
:
719 case GL_SLUMINANCE8_ALPHA8_EXT
:
720 case GL_SLUMINANCE_EXT
:
721 case GL_SLUMINANCE8_EXT
:
722 case GL_COMPRESSED_SRGB_EXT
:
723 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
724 case GL_COMPRESSED_SRGB_ALPHA_EXT
:
725 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
726 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
727 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
728 case GL_COMPRESSED_SLUMINANCE_EXT
:
729 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT
:
730 #endif /* FEATURE_EXT_texture_sRGB */
731 case GL_COMPRESSED_RED_RGTC1
:
732 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
733 case GL_COMPRESSED_RG_RGTC2
:
734 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
735 case GL_COMPRESSED_LUMINANCE_LATC1_EXT
:
736 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT
:
737 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT
:
738 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT
:
739 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI
:
740 /* generic integer formats */
741 case GL_RED_INTEGER_EXT
:
742 case GL_GREEN_INTEGER_EXT
:
743 case GL_BLUE_INTEGER_EXT
:
744 case GL_ALPHA_INTEGER_EXT
:
745 case GL_RGB_INTEGER_EXT
:
746 case GL_RGBA_INTEGER_EXT
:
747 case GL_BGR_INTEGER_EXT
:
748 case GL_BGRA_INTEGER_EXT
:
749 case GL_LUMINANCE_INTEGER_EXT
:
750 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
751 /* sized integer formats */
752 case GL_RGBA32UI_EXT
:
754 case GL_ALPHA32UI_EXT
:
755 case GL_INTENSITY32UI_EXT
:
756 case GL_LUMINANCE32UI_EXT
:
757 case GL_LUMINANCE_ALPHA32UI_EXT
:
758 case GL_RGBA16UI_EXT
:
760 case GL_ALPHA16UI_EXT
:
761 case GL_INTENSITY16UI_EXT
:
762 case GL_LUMINANCE16UI_EXT
:
763 case GL_LUMINANCE_ALPHA16UI_EXT
:
766 case GL_ALPHA8UI_EXT
:
767 case GL_INTENSITY8UI_EXT
:
768 case GL_LUMINANCE8UI_EXT
:
769 case GL_LUMINANCE_ALPHA8UI_EXT
:
772 case GL_ALPHA32I_EXT
:
773 case GL_INTENSITY32I_EXT
:
774 case GL_LUMINANCE32I_EXT
:
775 case GL_LUMINANCE_ALPHA32I_EXT
:
778 case GL_ALPHA16I_EXT
:
779 case GL_INTENSITY16I_EXT
:
780 case GL_LUMINANCE16I_EXT
:
781 case GL_LUMINANCE_ALPHA16I_EXT
:
785 case GL_INTENSITY8I_EXT
:
786 case GL_LUMINANCE8I_EXT
:
787 case GL_LUMINANCE_ALPHA8I_EXT
:
788 /* signed, normalized texture formats */
800 case GL_RGBA16_SNORM
:
802 case GL_ALPHA8_SNORM
:
803 case GL_ALPHA16_SNORM
:
804 case GL_LUMINANCE_SNORM
:
805 case GL_LUMINANCE8_SNORM
:
806 case GL_LUMINANCE16_SNORM
:
807 case GL_LUMINANCE_ALPHA_SNORM
:
808 case GL_LUMINANCE8_ALPHA8_SNORM
:
809 case GL_LUMINANCE16_ALPHA16_SNORM
:
810 case GL_INTENSITY_SNORM
:
811 case GL_INTENSITY8_SNORM
:
812 case GL_INTENSITY16_SNORM
:
814 case GL_R11F_G11F_B10F
:
816 case GL_YCBCR_MESA
: /* not considered to be RGB */
825 * Test if the given image format is a depth component format.
828 _mesa_is_depth_format(GLenum format
)
831 case GL_DEPTH_COMPONENT
:
832 case GL_DEPTH_COMPONENT16
:
833 case GL_DEPTH_COMPONENT24
:
834 case GL_DEPTH_COMPONENT32
:
835 case GL_DEPTH_COMPONENT32F
:
844 * Test if the given image format is a stencil format.
847 _mesa_is_stencil_format(GLenum format
)
850 case GL_STENCIL_INDEX
:
851 case GL_DEPTH_STENCIL
:
860 * Test if the given image format is a YCbCr format.
863 _mesa_is_ycbcr_format(GLenum format
)
875 * Test if the given image format is a depth+stencil format.
878 _mesa_is_depthstencil_format(GLenum format
)
881 case GL_DEPTH24_STENCIL8_EXT
:
882 case GL_DEPTH_STENCIL_EXT
:
883 case GL_DEPTH32F_STENCIL8
:
892 * Test if the given image format is a depth or stencil format.
895 _mesa_is_depth_or_stencil_format(GLenum format
)
898 case GL_DEPTH_COMPONENT
:
899 case GL_DEPTH_COMPONENT16
:
900 case GL_DEPTH_COMPONENT24
:
901 case GL_DEPTH_COMPONENT32
:
902 case GL_STENCIL_INDEX
:
903 case GL_STENCIL_INDEX1_EXT
:
904 case GL_STENCIL_INDEX4_EXT
:
905 case GL_STENCIL_INDEX8_EXT
:
906 case GL_STENCIL_INDEX16_EXT
:
907 case GL_DEPTH_STENCIL_EXT
:
908 case GL_DEPTH24_STENCIL8_EXT
:
909 case GL_DEPTH_COMPONENT32F
:
910 case GL_DEPTH32F_STENCIL8
:
919 * Test if the given image format is a dudv format.
922 _mesa_is_dudv_format(GLenum format
)
935 * Test if the given format is an integer (non-normalized) format.
938 _mesa_is_integer_format(GLenum format
)
941 /* generic integer formats */
942 case GL_RED_INTEGER_EXT
:
943 case GL_GREEN_INTEGER_EXT
:
944 case GL_BLUE_INTEGER_EXT
:
945 case GL_ALPHA_INTEGER_EXT
:
946 case GL_RGB_INTEGER_EXT
:
947 case GL_RGBA_INTEGER_EXT
:
948 case GL_BGR_INTEGER_EXT
:
949 case GL_BGRA_INTEGER_EXT
:
950 case GL_LUMINANCE_INTEGER_EXT
:
951 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
952 /* specific integer formats */
953 case GL_RGBA32UI_EXT
:
955 case GL_ALPHA32UI_EXT
:
956 case GL_INTENSITY32UI_EXT
:
957 case GL_LUMINANCE32UI_EXT
:
958 case GL_LUMINANCE_ALPHA32UI_EXT
:
959 case GL_RGBA16UI_EXT
:
961 case GL_ALPHA16UI_EXT
:
962 case GL_INTENSITY16UI_EXT
:
963 case GL_LUMINANCE16UI_EXT
:
964 case GL_LUMINANCE_ALPHA16UI_EXT
:
967 case GL_ALPHA8UI_EXT
:
968 case GL_INTENSITY8UI_EXT
:
969 case GL_LUMINANCE8UI_EXT
:
970 case GL_LUMINANCE_ALPHA8UI_EXT
:
973 case GL_ALPHA32I_EXT
:
974 case GL_INTENSITY32I_EXT
:
975 case GL_LUMINANCE32I_EXT
:
976 case GL_LUMINANCE_ALPHA32I_EXT
:
979 case GL_ALPHA16I_EXT
:
980 case GL_INTENSITY16I_EXT
:
981 case GL_LUMINANCE16I_EXT
:
982 case GL_LUMINANCE_ALPHA16I_EXT
:
986 case GL_INTENSITY8I_EXT
:
987 case GL_LUMINANCE8I_EXT
:
988 case GL_LUMINANCE_ALPHA8I_EXT
:
997 * Test if an image format is a supported compressed format.
998 * \param format the internal format token provided by the user.
999 * \return GL_TRUE if compressed, GL_FALSE if uncompressed
1002 _mesa_is_compressed_format(struct gl_context
*ctx
, GLenum format
)
1005 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
1006 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
1007 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
1008 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
1009 return ctx
->Extensions
.EXT_texture_compression_s3tc
;
1014 return ctx
->Extensions
.S3_s3tc
;
1015 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
1016 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
1017 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
1018 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
1019 return ctx
->Extensions
.EXT_texture_sRGB
1020 && ctx
->Extensions
.EXT_texture_compression_s3tc
;
1021 case GL_COMPRESSED_RGB_FXT1_3DFX
:
1022 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
1023 return ctx
->Extensions
.TDFX_texture_compression_FXT1
;
1024 case GL_COMPRESSED_RED_RGTC1
:
1025 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
1026 case GL_COMPRESSED_RG_RGTC2
:
1027 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
1028 return ctx
->Extensions
.ARB_texture_compression_rgtc
;
1029 case GL_COMPRESSED_LUMINANCE_LATC1_EXT
:
1030 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT
:
1031 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT
:
1032 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT
:
1033 return ctx
->Extensions
.EXT_texture_compression_latc
;
1034 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI
:
1035 return ctx
->Extensions
.ATI_texture_compression_3dc
;
1037 case GL_PALETTE4_RGB8_OES
:
1038 case GL_PALETTE4_RGBA8_OES
:
1039 case GL_PALETTE4_R5_G6_B5_OES
:
1040 case GL_PALETTE4_RGBA4_OES
:
1041 case GL_PALETTE4_RGB5_A1_OES
:
1042 case GL_PALETTE8_RGB8_OES
:
1043 case GL_PALETTE8_RGBA8_OES
:
1044 case GL_PALETTE8_R5_G6_B5_OES
:
1045 case GL_PALETTE8_RGBA4_OES
:
1046 case GL_PALETTE8_RGB5_A1_OES
:
1047 return ctx
->API
== API_OPENGLES
;
1056 * Return the address of a specific pixel in an image (1D, 2D or 3D).
1058 * Pixel unpacking/packing parameters are observed according to \p packing.
1060 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
1061 * \param image starting address of image data
1062 * \param width the image width
1063 * \param height theimage height
1064 * \param format the pixel format
1065 * \param type the pixel data type
1066 * \param packing the pixelstore attributes
1067 * \param img which image in the volume (0 for 1D or 2D images)
1068 * \param row row of pixel in the image (0 for 1D images)
1069 * \param column column of pixel in the image
1071 * \return address of pixel on success, or NULL on error.
1073 * \sa gl_pixelstore_attrib.
1076 _mesa_image_address( GLuint dimensions
,
1077 const struct gl_pixelstore_attrib
*packing
,
1078 const GLvoid
*image
,
1079 GLsizei width
, GLsizei height
,
1080 GLenum format
, GLenum type
,
1081 GLint img
, GLint row
, GLint column
)
1083 GLint alignment
; /* 1, 2 or 4 */
1084 GLint pixels_per_row
;
1085 GLint rows_per_image
;
1088 GLint skipimages
; /* for 3-D volume images */
1089 GLubyte
*pixel_addr
;
1091 ASSERT(dimensions
>= 1 && dimensions
<= 3);
1093 alignment
= packing
->Alignment
;
1094 if (packing
->RowLength
> 0) {
1095 pixels_per_row
= packing
->RowLength
;
1098 pixels_per_row
= width
;
1100 if (packing
->ImageHeight
> 0) {
1101 rows_per_image
= packing
->ImageHeight
;
1104 rows_per_image
= height
;
1107 skippixels
= packing
->SkipPixels
;
1108 /* Note: SKIP_ROWS _is_ used for 1D images */
1109 skiprows
= packing
->SkipRows
;
1110 /* Note: SKIP_IMAGES is only used for 3D images */
1111 skipimages
= (dimensions
== 3) ? packing
->SkipImages
: 0;
1113 if (type
== GL_BITMAP
) {
1115 GLint comp_per_pixel
; /* components per pixel */
1116 GLint bytes_per_comp
; /* bytes per component */
1117 GLint bytes_per_row
;
1118 GLint bytes_per_image
;
1120 /* Compute bytes per component */
1121 bytes_per_comp
= _mesa_sizeof_packed_type( type
);
1122 if (bytes_per_comp
< 0) {
1126 /* Compute number of components per pixel */
1127 comp_per_pixel
= _mesa_components_in_format( format
);
1128 if (comp_per_pixel
< 0) {
1132 bytes_per_row
= alignment
1133 * CEILING( comp_per_pixel
*pixels_per_row
, 8*alignment
);
1135 bytes_per_image
= bytes_per_row
* rows_per_image
;
1137 pixel_addr
= (GLubyte
*) image
1138 + (skipimages
+ img
) * bytes_per_image
1139 + (skiprows
+ row
) * bytes_per_row
1140 + (skippixels
+ column
) / 8;
1143 /* Non-BITMAP data */
1144 GLint bytes_per_pixel
, bytes_per_row
, remainder
, bytes_per_image
;
1147 bytes_per_pixel
= _mesa_bytes_per_pixel( format
, type
);
1149 /* The pixel type and format should have been error checked earlier */
1150 assert(bytes_per_pixel
> 0);
1152 bytes_per_row
= pixels_per_row
* bytes_per_pixel
;
1153 remainder
= bytes_per_row
% alignment
;
1155 bytes_per_row
+= (alignment
- remainder
);
1157 ASSERT(bytes_per_row
% alignment
== 0);
1159 bytes_per_image
= bytes_per_row
* rows_per_image
;
1161 if (packing
->Invert
) {
1162 /* set pixel_addr to the last row */
1163 topOfImage
= bytes_per_row
* (height
- 1);
1164 bytes_per_row
= -bytes_per_row
;
1170 /* compute final pixel address */
1171 pixel_addr
= (GLubyte
*) image
1172 + (skipimages
+ img
) * bytes_per_image
1174 + (skiprows
+ row
) * bytes_per_row
1175 + (skippixels
+ column
) * bytes_per_pixel
;
1178 return (GLvoid
*) pixel_addr
;
1183 _mesa_image_address1d( const struct gl_pixelstore_attrib
*packing
,
1184 const GLvoid
*image
,
1186 GLenum format
, GLenum type
,
1189 return _mesa_image_address(1, packing
, image
, width
, 1,
1190 format
, type
, 0, 0, column
);
1195 _mesa_image_address2d( const struct gl_pixelstore_attrib
*packing
,
1196 const GLvoid
*image
,
1197 GLsizei width
, GLsizei height
,
1198 GLenum format
, GLenum type
,
1199 GLint row
, GLint column
)
1201 return _mesa_image_address(2, packing
, image
, width
, height
,
1202 format
, type
, 0, row
, column
);
1207 _mesa_image_address3d( const struct gl_pixelstore_attrib
*packing
,
1208 const GLvoid
*image
,
1209 GLsizei width
, GLsizei height
,
1210 GLenum format
, GLenum type
,
1211 GLint img
, GLint row
, GLint column
)
1213 return _mesa_image_address(3, packing
, image
, width
, height
,
1214 format
, type
, img
, row
, column
);
1220 * Compute the stride (in bytes) between image rows.
1222 * \param packing the pixelstore attributes
1223 * \param width image width.
1224 * \param format pixel format.
1225 * \param type pixel data type.
1227 * \return the stride in bytes for the given parameters, or -1 if error
1230 _mesa_image_row_stride( const struct gl_pixelstore_attrib
*packing
,
1231 GLint width
, GLenum format
, GLenum type
)
1233 GLint bytesPerRow
, remainder
;
1237 if (type
== GL_BITMAP
) {
1238 if (packing
->RowLength
== 0) {
1239 bytesPerRow
= (width
+ 7) / 8;
1242 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
1246 /* Non-BITMAP data */
1247 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1248 if (bytesPerPixel
<= 0)
1249 return -1; /* error */
1250 if (packing
->RowLength
== 0) {
1251 bytesPerRow
= bytesPerPixel
* width
;
1254 bytesPerRow
= bytesPerPixel
* packing
->RowLength
;
1258 remainder
= bytesPerRow
% packing
->Alignment
;
1259 if (remainder
> 0) {
1260 bytesPerRow
+= (packing
->Alignment
- remainder
);
1263 if (packing
->Invert
) {
1264 /* negate the bytes per row (negative row stride) */
1265 bytesPerRow
= -bytesPerRow
;
1273 * Compute the stride between images in a 3D texture (in bytes) for the given
1274 * pixel packing parameters and image width, format and type.
1277 _mesa_image_image_stride( const struct gl_pixelstore_attrib
*packing
,
1278 GLint width
, GLint height
,
1279 GLenum format
, GLenum type
)
1281 GLint bytesPerRow
, bytesPerImage
, remainder
;
1285 if (type
== GL_BITMAP
) {
1286 if (packing
->RowLength
== 0) {
1287 bytesPerRow
= (width
+ 7) / 8;
1290 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
1294 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1296 if (bytesPerPixel
<= 0)
1297 return -1; /* error */
1298 if (packing
->RowLength
== 0) {
1299 bytesPerRow
= bytesPerPixel
* width
;
1302 bytesPerRow
= bytesPerPixel
* packing
->RowLength
;
1306 remainder
= bytesPerRow
% packing
->Alignment
;
1308 bytesPerRow
+= (packing
->Alignment
- remainder
);
1310 if (packing
->ImageHeight
== 0)
1311 bytesPerImage
= bytesPerRow
* height
;
1313 bytesPerImage
= bytesPerRow
* packing
->ImageHeight
;
1315 return bytesPerImage
;
1321 * "Expand" a bitmap from 1-bit per pixel to 8-bits per pixel.
1322 * This is typically used to convert a bitmap into a GLubyte/pixel texture.
1323 * "On" bits will set texels to \p onValue.
1324 * "Off" bits will not modify texels.
1325 * \param width src bitmap width in pixels
1326 * \param height src bitmap height in pixels
1327 * \param unpack bitmap unpacking state
1328 * \param bitmap the src bitmap data
1329 * \param destBuffer start of dest buffer
1330 * \param destStride row stride in dest buffer
1331 * \param onValue if bit is 1, set destBuffer pixel to this value
1334 _mesa_expand_bitmap(GLsizei width
, GLsizei height
,
1335 const struct gl_pixelstore_attrib
*unpack
,
1336 const GLubyte
*bitmap
,
1337 GLubyte
*destBuffer
, GLint destStride
,
1340 const GLubyte
*srcRow
= (const GLubyte
*)
1341 _mesa_image_address2d(unpack
, bitmap
, width
, height
,
1342 GL_COLOR_INDEX
, GL_BITMAP
, 0, 0);
1343 const GLint srcStride
= _mesa_image_row_stride(unpack
, width
,
1344 GL_COLOR_INDEX
, GL_BITMAP
);
1347 #define SET_PIXEL(COL, ROW) \
1348 destBuffer[(ROW) * destStride + (COL)] = onValue;
1350 for (row
= 0; row
< height
; row
++) {
1351 const GLubyte
*src
= srcRow
;
1353 if (unpack
->LsbFirst
) {
1355 GLubyte mask
= 1U << (unpack
->SkipPixels
& 0x7);
1356 for (col
= 0; col
< width
; col
++) {
1359 SET_PIXEL(col
, row
);
1371 /* get ready for next row */
1377 GLubyte mask
= 128U >> (unpack
->SkipPixels
& 0x7);
1378 for (col
= 0; col
< width
; col
++) {
1381 SET_PIXEL(col
, row
);
1393 /* get ready for next row */
1398 srcRow
+= srcStride
;
1408 * Convert an array of RGBA colors from one datatype to another.
1409 * NOTE: src may equal dst. In that case, we use a temporary buffer.
1412 _mesa_convert_colors(GLenum srcType
, const GLvoid
*src
,
1413 GLenum dstType
, GLvoid
*dst
,
1414 GLuint count
, const GLubyte mask
[])
1416 GLuint tempBuffer
[MAX_WIDTH
][4];
1417 const GLboolean useTemp
= (src
== dst
);
1419 ASSERT(srcType
!= dstType
);
1422 case GL_UNSIGNED_BYTE
:
1423 if (dstType
== GL_UNSIGNED_SHORT
) {
1424 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
1425 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
1427 for (i
= 0; i
< count
; i
++) {
1428 if (!mask
|| mask
[i
]) {
1429 dst2
[i
][RCOMP
] = UBYTE_TO_USHORT(src1
[i
][RCOMP
]);
1430 dst2
[i
][GCOMP
] = UBYTE_TO_USHORT(src1
[i
][GCOMP
]);
1431 dst2
[i
][BCOMP
] = UBYTE_TO_USHORT(src1
[i
][BCOMP
]);
1432 dst2
[i
][ACOMP
] = UBYTE_TO_USHORT(src1
[i
][ACOMP
]);
1436 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
1439 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
1440 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
1442 ASSERT(dstType
== GL_FLOAT
);
1443 for (i
= 0; i
< count
; i
++) {
1444 if (!mask
|| mask
[i
]) {
1445 dst4
[i
][RCOMP
] = UBYTE_TO_FLOAT(src1
[i
][RCOMP
]);
1446 dst4
[i
][GCOMP
] = UBYTE_TO_FLOAT(src1
[i
][GCOMP
]);
1447 dst4
[i
][BCOMP
] = UBYTE_TO_FLOAT(src1
[i
][BCOMP
]);
1448 dst4
[i
][ACOMP
] = UBYTE_TO_FLOAT(src1
[i
][ACOMP
]);
1452 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
1455 case GL_UNSIGNED_SHORT
:
1456 if (dstType
== GL_UNSIGNED_BYTE
) {
1457 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
1458 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
1460 for (i
= 0; i
< count
; i
++) {
1461 if (!mask
|| mask
[i
]) {
1462 dst1
[i
][RCOMP
] = USHORT_TO_UBYTE(src2
[i
][RCOMP
]);
1463 dst1
[i
][GCOMP
] = USHORT_TO_UBYTE(src2
[i
][GCOMP
]);
1464 dst1
[i
][BCOMP
] = USHORT_TO_UBYTE(src2
[i
][BCOMP
]);
1465 dst1
[i
][ACOMP
] = USHORT_TO_UBYTE(src2
[i
][ACOMP
]);
1469 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
1472 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
1473 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
1475 ASSERT(dstType
== GL_FLOAT
);
1476 for (i
= 0; i
< count
; i
++) {
1477 if (!mask
|| mask
[i
]) {
1478 dst4
[i
][RCOMP
] = USHORT_TO_FLOAT(src2
[i
][RCOMP
]);
1479 dst4
[i
][GCOMP
] = USHORT_TO_FLOAT(src2
[i
][GCOMP
]);
1480 dst4
[i
][BCOMP
] = USHORT_TO_FLOAT(src2
[i
][BCOMP
]);
1481 dst4
[i
][ACOMP
] = USHORT_TO_FLOAT(src2
[i
][ACOMP
]);
1485 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
1489 if (dstType
== GL_UNSIGNED_BYTE
) {
1490 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
1491 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
1493 for (i
= 0; i
< count
; i
++) {
1494 if (!mask
|| mask
[i
])
1495 _mesa_unclamped_float_rgba_to_ubyte(dst1
[i
], src4
[i
]);
1498 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
1501 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
1502 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
1504 ASSERT(dstType
== GL_UNSIGNED_SHORT
);
1505 for (i
= 0; i
< count
; i
++) {
1506 if (!mask
|| mask
[i
]) {
1507 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][RCOMP
], src4
[i
][RCOMP
]);
1508 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][GCOMP
], src4
[i
][GCOMP
]);
1509 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][BCOMP
], src4
[i
][BCOMP
]);
1510 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][ACOMP
], src4
[i
][ACOMP
]);
1514 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
1518 _mesa_problem(NULL
, "Invalid datatype in _mesa_convert_colors");
1526 * Perform basic clipping for glDrawPixels. The image's position and size
1527 * and the unpack SkipPixels and SkipRows are adjusted so that the image
1528 * region is entirely within the window and scissor bounds.
1529 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
1530 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
1531 * we'll actually write. Beforehand, *destY-1 is the first drawing row.
1533 * \return GL_TRUE if image is ready for drawing or
1534 * GL_FALSE if image was completely clipped away (draw nothing)
1537 _mesa_clip_drawpixels(const struct gl_context
*ctx
,
1538 GLint
*destX
, GLint
*destY
,
1539 GLsizei
*width
, GLsizei
*height
,
1540 struct gl_pixelstore_attrib
*unpack
)
1542 const struct gl_framebuffer
*buffer
= ctx
->DrawBuffer
;
1544 if (unpack
->RowLength
== 0) {
1545 unpack
->RowLength
= *width
;
1548 ASSERT(ctx
->Pixel
.ZoomX
== 1.0F
);
1549 ASSERT(ctx
->Pixel
.ZoomY
== 1.0F
|| ctx
->Pixel
.ZoomY
== -1.0F
);
1552 if (*destX
< buffer
->_Xmin
) {
1553 unpack
->SkipPixels
+= (buffer
->_Xmin
- *destX
);
1554 *width
-= (buffer
->_Xmin
- *destX
);
1555 *destX
= buffer
->_Xmin
;
1557 /* right clipping */
1558 if (*destX
+ *width
> buffer
->_Xmax
)
1559 *width
-= (*destX
+ *width
- buffer
->_Xmax
);
1564 if (ctx
->Pixel
.ZoomY
== 1.0F
) {
1565 /* bottom clipping */
1566 if (*destY
< buffer
->_Ymin
) {
1567 unpack
->SkipRows
+= (buffer
->_Ymin
- *destY
);
1568 *height
-= (buffer
->_Ymin
- *destY
);
1569 *destY
= buffer
->_Ymin
;
1572 if (*destY
+ *height
> buffer
->_Ymax
)
1573 *height
-= (*destY
+ *height
- buffer
->_Ymax
);
1575 else { /* upside down */
1577 if (*destY
> buffer
->_Ymax
) {
1578 unpack
->SkipRows
+= (*destY
- buffer
->_Ymax
);
1579 *height
-= (*destY
- buffer
->_Ymax
);
1580 *destY
= buffer
->_Ymax
;
1582 /* bottom clipping */
1583 if (*destY
- *height
< buffer
->_Ymin
)
1584 *height
-= (buffer
->_Ymin
- (*destY
- *height
));
1585 /* adjust destY so it's the first row to write to */
1597 * Perform clipping for glReadPixels. The image's window position
1598 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
1599 * so that the image region is entirely within the window bounds.
1600 * Note: this is different from _mesa_clip_drawpixels() in that the
1601 * scissor box is ignored, and we use the bounds of the current readbuffer
1604 * \return GL_TRUE if region to read is in bounds
1605 * GL_FALSE if region is completely out of bounds (nothing to read)
1608 _mesa_clip_readpixels(const struct gl_context
*ctx
,
1609 GLint
*srcX
, GLint
*srcY
,
1610 GLsizei
*width
, GLsizei
*height
,
1611 struct gl_pixelstore_attrib
*pack
)
1613 const struct gl_framebuffer
*buffer
= ctx
->ReadBuffer
;
1615 if (pack
->RowLength
== 0) {
1616 pack
->RowLength
= *width
;
1621 pack
->SkipPixels
+= (0 - *srcX
);
1622 *width
-= (0 - *srcX
);
1625 /* right clipping */
1626 if (*srcX
+ *width
> (GLsizei
) buffer
->Width
)
1627 *width
-= (*srcX
+ *width
- buffer
->Width
);
1632 /* bottom clipping */
1634 pack
->SkipRows
+= (0 - *srcY
);
1635 *height
-= (0 - *srcY
);
1639 if (*srcY
+ *height
> (GLsizei
) buffer
->Height
)
1640 *height
-= (*srcY
+ *height
- buffer
->Height
);
1650 * Do clipping for a glCopyTexSubImage call.
1651 * The framebuffer source region might extend outside the framebuffer
1652 * bounds. Clip the source region against the framebuffer bounds and
1653 * adjust the texture/dest position and size accordingly.
1655 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
1658 _mesa_clip_copytexsubimage(const struct gl_context
*ctx
,
1659 GLint
*destX
, GLint
*destY
,
1660 GLint
*srcX
, GLint
*srcY
,
1661 GLsizei
*width
, GLsizei
*height
)
1663 const struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1664 const GLint srcX0
= *srcX
, srcY0
= *srcY
;
1666 if (_mesa_clip_to_region(0, 0, fb
->Width
, fb
->Height
,
1667 srcX
, srcY
, width
, height
)) {
1668 *destX
= *destX
+ *srcX
- srcX0
;
1669 *destY
= *destY
+ *srcY
- srcY0
;
1681 * Clip the rectangle defined by (x, y, width, height) against the bounds
1682 * specified by [xmin, xmax) and [ymin, ymax).
1683 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
1686 _mesa_clip_to_region(GLint xmin
, GLint ymin
,
1687 GLint xmax
, GLint ymax
,
1689 GLsizei
*width
, GLsizei
*height
)
1693 *width
-= (xmin
- *x
);
1697 /* right clipping */
1698 if (*x
+ *width
> xmax
)
1699 *width
-= (*x
+ *width
- xmax
);
1704 /* bottom (or top) clipping */
1706 *height
-= (ymin
- *y
);
1710 /* top (or bottom) clipping */
1711 if (*y
+ *height
> ymax
)
1712 *height
-= (*y
+ *height
- ymax
);
1722 * Clip dst coords against Xmax (or Ymax).
1725 clip_right_or_top(GLint
*srcX0
, GLint
*srcX1
,
1726 GLint
*dstX0
, GLint
*dstX1
,
1731 if (*dstX1
> maxValue
) {
1732 /* X1 outside right edge */
1733 ASSERT(*dstX0
< maxValue
); /* X0 should be inside right edge */
1734 t
= (GLfloat
) (maxValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
1735 /* chop off [t, 1] part */
1736 ASSERT(t
>= 0.0 && t
<= 1.0);
1738 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
1739 *srcX1
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
1741 else if (*dstX0
> maxValue
) {
1742 /* X0 outside right edge */
1743 ASSERT(*dstX1
< maxValue
); /* X1 should be inside right edge */
1744 t
= (GLfloat
) (maxValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
1745 /* chop off [t, 1] part */
1746 ASSERT(t
>= 0.0 && t
<= 1.0);
1748 bias
= (*srcX0
< *srcX1
) ? -0.5F
: 0.5F
;
1749 *srcX0
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
1755 * Clip dst coords against Xmin (or Ymin).
1758 clip_left_or_bottom(GLint
*srcX0
, GLint
*srcX1
,
1759 GLint
*dstX0
, GLint
*dstX1
,
1764 if (*dstX0
< minValue
) {
1765 /* X0 outside left edge */
1766 ASSERT(*dstX1
> minValue
); /* X1 should be inside left edge */
1767 t
= (GLfloat
) (minValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
1768 /* chop off [0, t] part */
1769 ASSERT(t
>= 0.0 && t
<= 1.0);
1771 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
; /* flipped??? */
1772 *srcX0
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
1774 else if (*dstX1
< minValue
) {
1775 /* X1 outside left edge */
1776 ASSERT(*dstX0
> minValue
); /* X0 should be inside left edge */
1777 t
= (GLfloat
) (minValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
1778 /* chop off [0, t] part */
1779 ASSERT(t
>= 0.0 && t
<= 1.0);
1781 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
1782 *srcX1
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
1788 * Do clipping of blit src/dest rectangles.
1789 * The dest rect is clipped against both the buffer bounds and scissor bounds.
1790 * The src rect is just clipped against the buffer bounds.
1792 * When either the src or dest rect is clipped, the other is also clipped
1795 * Note that X0 need not be less than X1 (same for Y) for either the source
1796 * and dest rects. That makes the clipping a little trickier.
1798 * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped
1801 _mesa_clip_blit(struct gl_context
*ctx
,
1802 GLint
*srcX0
, GLint
*srcY0
, GLint
*srcX1
, GLint
*srcY1
,
1803 GLint
*dstX0
, GLint
*dstY0
, GLint
*dstX1
, GLint
*dstY1
)
1805 const GLint srcXmin
= 0;
1806 const GLint srcXmax
= ctx
->ReadBuffer
->Width
;
1807 const GLint srcYmin
= 0;
1808 const GLint srcYmax
= ctx
->ReadBuffer
->Height
;
1810 /* these include scissor bounds */
1811 const GLint dstXmin
= ctx
->DrawBuffer
->_Xmin
;
1812 const GLint dstXmax
= ctx
->DrawBuffer
->_Xmax
;
1813 const GLint dstYmin
= ctx
->DrawBuffer
->_Ymin
;
1814 const GLint dstYmax
= ctx
->DrawBuffer
->_Ymax
;
1817 printf("PreClipX: src: %d .. %d dst: %d .. %d\n",
1818 *srcX0, *srcX1, *dstX0, *dstX1);
1819 printf("PreClipY: src: %d .. %d dst: %d .. %d\n",
1820 *srcY0, *srcY1, *dstY0, *dstY1);
1823 /* trivial rejection tests */
1824 if (*dstX0
== *dstX1
)
1825 return GL_FALSE
; /* no width */
1826 if (*dstX0
<= dstXmin
&& *dstX1
<= dstXmin
)
1827 return GL_FALSE
; /* totally out (left) of bounds */
1828 if (*dstX0
>= dstXmax
&& *dstX1
>= dstXmax
)
1829 return GL_FALSE
; /* totally out (right) of bounds */
1831 if (*dstY0
== *dstY1
)
1833 if (*dstY0
<= dstYmin
&& *dstY1
<= dstYmin
)
1835 if (*dstY0
>= dstYmax
&& *dstY1
>= dstYmax
)
1838 if (*srcX0
== *srcX1
)
1840 if (*srcX0
<= srcXmin
&& *srcX1
<= srcXmin
)
1842 if (*srcX0
>= srcXmax
&& *srcX1
>= srcXmax
)
1845 if (*srcY0
== *srcY1
)
1847 if (*srcY0
<= srcYmin
&& *srcY1
<= srcYmin
)
1849 if (*srcY0
>= srcYmax
&& *srcY1
>= srcYmax
)
1855 clip_right_or_top(srcX0
, srcX1
, dstX0
, dstX1
, dstXmax
);
1856 clip_right_or_top(srcY0
, srcY1
, dstY0
, dstY1
, dstYmax
);
1857 clip_left_or_bottom(srcX0
, srcX1
, dstX0
, dstX1
, dstXmin
);
1858 clip_left_or_bottom(srcY0
, srcY1
, dstY0
, dstY1
, dstYmin
);
1861 * src clip (just swap src/dst values from above)
1863 clip_right_or_top(dstX0
, dstX1
, srcX0
, srcX1
, srcXmax
);
1864 clip_right_or_top(dstY0
, dstY1
, srcY0
, srcY1
, srcYmax
);
1865 clip_left_or_bottom(dstX0
, dstX1
, srcX0
, srcX1
, srcXmin
);
1866 clip_left_or_bottom(dstY0
, dstY1
, srcY0
, srcY1
, srcYmin
);
1869 printf("PostClipX: src: %d .. %d dst: %d .. %d\n",
1870 *srcX0, *srcX1, *dstX0, *dstX1);
1871 printf("PostClipY: src: %d .. %d dst: %d .. %d\n",
1872 *srcY0, *srcY1, *dstY0, *dstY1);
1875 ASSERT(*dstX0
>= dstXmin
);
1876 ASSERT(*dstX0
<= dstXmax
);
1877 ASSERT(*dstX1
>= dstXmin
);
1878 ASSERT(*dstX1
<= dstXmax
);
1880 ASSERT(*dstY0
>= dstYmin
);
1881 ASSERT(*dstY0
<= dstYmax
);
1882 ASSERT(*dstY1
>= dstYmin
);
1883 ASSERT(*dstY1
<= dstYmax
);
1885 ASSERT(*srcX0
>= srcXmin
);
1886 ASSERT(*srcX0
<= srcXmax
);
1887 ASSERT(*srcX1
>= srcXmin
);
1888 ASSERT(*srcX1
<= srcXmax
);
1890 ASSERT(*srcY0
>= srcYmin
);
1891 ASSERT(*srcY0
<= srcYmax
);
1892 ASSERT(*srcY1
>= srcYmin
);
1893 ASSERT(*srcY1
<= srcYmax
);