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_SHORT_8_8_MESA
:
62 case GL_UNSIGNED_SHORT_8_8_REV_MESA
:
63 case GL_UNSIGNED_INT_24_8_EXT
:
64 case GL_UNSIGNED_INT_5_9_9_9_REV
:
65 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
66 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
76 * Flip the order of the 2 bytes in each word in the given array.
79 * \param n number of words.
82 _mesa_swap2( GLushort
*p
, GLuint n
)
85 for (i
= 0; i
< n
; i
++) {
86 p
[i
] = (p
[i
] >> 8) | ((p
[i
] << 8) & 0xff00);
93 * Flip the order of the 4 bytes in each word in the given array.
96 _mesa_swap4( GLuint
*p
, GLuint n
)
99 for (i
= 0; i
< n
; i
++) {
102 | ((b
>> 8) & 0xff00)
103 | ((b
<< 8) & 0xff0000)
104 | ((b
<< 24) & 0xff000000);
111 * Get the size of a GL data type.
113 * \param type GL data type.
115 * \return the size, in bytes, of the given data type, 0 if a GL_BITMAP, or -1
116 * if an invalid type enum.
119 _mesa_sizeof_type( GLenum type
)
124 case GL_UNSIGNED_BYTE
:
125 return sizeof(GLubyte
);
127 return sizeof(GLbyte
);
128 case GL_UNSIGNED_SHORT
:
129 return sizeof(GLushort
);
131 return sizeof(GLshort
);
132 case GL_UNSIGNED_INT
:
133 return sizeof(GLuint
);
135 return sizeof(GLint
);
137 return sizeof(GLfloat
);
139 return sizeof(GLdouble
);
140 case GL_HALF_FLOAT_ARB
:
141 return sizeof(GLhalfARB
);
143 return sizeof(GLfixed
);
151 * Same as _mesa_sizeof_type() but also accepting the packed pixel
155 _mesa_sizeof_packed_type( GLenum type
)
160 case GL_UNSIGNED_BYTE
:
161 return sizeof(GLubyte
);
163 return sizeof(GLbyte
);
164 case GL_UNSIGNED_SHORT
:
165 return sizeof(GLushort
);
167 return sizeof(GLshort
);
168 case GL_UNSIGNED_INT
:
169 return sizeof(GLuint
);
171 return sizeof(GLint
);
172 case GL_HALF_FLOAT_ARB
:
173 return sizeof(GLhalfARB
);
175 return sizeof(GLfloat
);
176 case GL_UNSIGNED_BYTE_3_3_2
:
177 case GL_UNSIGNED_BYTE_2_3_3_REV
:
178 case MESA_UNSIGNED_BYTE_4_4
:
179 return sizeof(GLubyte
);
180 case GL_UNSIGNED_SHORT_5_6_5
:
181 case GL_UNSIGNED_SHORT_5_6_5_REV
:
182 case GL_UNSIGNED_SHORT_4_4_4_4
:
183 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
184 case GL_UNSIGNED_SHORT_5_5_5_1
:
185 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
186 case GL_UNSIGNED_SHORT_8_8_MESA
:
187 case GL_UNSIGNED_SHORT_8_8_REV_MESA
:
188 return sizeof(GLushort
);
189 case GL_UNSIGNED_INT_8_8_8_8
:
190 case GL_UNSIGNED_INT_8_8_8_8_REV
:
191 case GL_UNSIGNED_INT_24_8_EXT
:
192 case GL_UNSIGNED_INT_5_9_9_9_REV
:
193 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
194 return sizeof(GLuint
);
195 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
204 * Get the number of components in a pixel format.
206 * \param format pixel format.
208 * \return the number of components in the given format, or -1 if a bad format.
211 _mesa_components_in_format( GLenum format
)
215 case GL_STENCIL_INDEX
:
216 case GL_DEPTH_COMPONENT
:
218 case GL_RED_INTEGER_EXT
:
220 case GL_GREEN_INTEGER_EXT
:
222 case GL_BLUE_INTEGER_EXT
:
224 case GL_ALPHA_INTEGER_EXT
:
226 case GL_LUMINANCE_INTEGER_EXT
:
230 case GL_LUMINANCE_ALPHA
:
231 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
234 case GL_DEPTH_STENCIL_EXT
:
242 case GL_RGB_INTEGER_EXT
:
243 case GL_BGR_INTEGER_EXT
:
249 case GL_RGBA_INTEGER_EXT
:
250 case GL_BGRA_INTEGER_EXT
:
260 * Get the bytes per pixel of pixel format type pair.
262 * \param format pixel format.
263 * \param type pixel type.
265 * \return bytes per pixel, or -1 if a bad format or type was given.
268 _mesa_bytes_per_pixel( GLenum format
, GLenum type
)
270 GLint comps
= _mesa_components_in_format( format
);
276 return 0; /* special case */
278 case GL_UNSIGNED_BYTE
:
279 return comps
* sizeof(GLubyte
);
281 case GL_UNSIGNED_SHORT
:
282 return comps
* sizeof(GLshort
);
284 case GL_UNSIGNED_INT
:
285 return comps
* sizeof(GLint
);
287 return comps
* sizeof(GLfloat
);
288 case GL_HALF_FLOAT_ARB
:
289 return comps
* sizeof(GLhalfARB
);
290 case GL_UNSIGNED_BYTE_3_3_2
:
291 case GL_UNSIGNED_BYTE_2_3_3_REV
:
292 if (format
== GL_RGB
|| format
== GL_BGR
||
293 format
== GL_RGB_INTEGER_EXT
|| format
== GL_BGR_INTEGER_EXT
)
294 return sizeof(GLubyte
);
296 return -1; /* error */
297 case GL_UNSIGNED_SHORT_5_6_5
:
298 case GL_UNSIGNED_SHORT_5_6_5_REV
:
299 if (format
== GL_RGB
|| format
== GL_BGR
||
300 format
== GL_RGB_INTEGER_EXT
|| format
== GL_BGR_INTEGER_EXT
)
301 return sizeof(GLushort
);
303 return -1; /* error */
304 case GL_UNSIGNED_SHORT_4_4_4_4
:
305 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
306 case GL_UNSIGNED_SHORT_5_5_5_1
:
307 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
308 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
||
309 format
== GL_RGBA_INTEGER_EXT
|| format
== GL_BGRA_INTEGER_EXT
)
310 return sizeof(GLushort
);
313 case GL_UNSIGNED_INT_8_8_8_8
:
314 case GL_UNSIGNED_INT_8_8_8_8_REV
:
315 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
||
316 format
== GL_RGBA_INTEGER_EXT
|| format
== GL_BGRA_INTEGER_EXT
)
317 return sizeof(GLuint
);
320 case GL_UNSIGNED_SHORT_8_8_MESA
:
321 case GL_UNSIGNED_SHORT_8_8_REV_MESA
:
322 if (format
== GL_YCBCR_MESA
)
323 return sizeof(GLushort
);
326 case GL_UNSIGNED_INT_24_8_EXT
:
327 if (format
== GL_DEPTH_STENCIL_EXT
)
328 return sizeof(GLuint
);
331 case GL_UNSIGNED_INT_5_9_9_9_REV
:
332 if (format
== GL_RGB
)
333 return sizeof(GLuint
);
336 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
337 if (format
== GL_RGB
)
338 return sizeof(GLuint
);
341 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
342 if (format
== GL_DEPTH_STENCIL
)
353 * Do error checking of format/type combinations for glReadPixels,
354 * glDrawPixels and glTex[Sub]Image. Note that depending on the format
355 * and type values, we may either generate GL_INVALID_OPERATION or
358 * \param format pixel format.
359 * \param type pixel type.
361 * \return GL_INVALID_ENUM, GL_INVALID_OPERATION or GL_NO_ERROR
364 _mesa_error_check_format_and_type(const struct gl_context
*ctx
,
365 GLenum format
, GLenum type
)
367 /* special type-based checks (see glReadPixels, glDrawPixels error lists) */
370 if (format
!= GL_COLOR_INDEX
&& format
!= GL_STENCIL_INDEX
) {
371 return GL_INVALID_ENUM
;
375 case GL_UNSIGNED_BYTE_3_3_2
:
376 case GL_UNSIGNED_BYTE_2_3_3_REV
:
377 case GL_UNSIGNED_SHORT_5_6_5
:
378 case GL_UNSIGNED_SHORT_5_6_5_REV
:
379 if (format
== GL_RGB
) {
382 return GL_INVALID_OPERATION
;
384 case GL_UNSIGNED_SHORT_4_4_4_4
:
385 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
386 case GL_UNSIGNED_SHORT_5_5_5_1
:
387 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
388 case GL_UNSIGNED_INT_8_8_8_8
:
389 case GL_UNSIGNED_INT_8_8_8_8_REV
:
390 if (format
== GL_RGBA
||
392 format
== GL_ABGR_EXT
) {
395 return GL_INVALID_OPERATION
;
397 case GL_UNSIGNED_INT_24_8
:
398 if (!ctx
->Extensions
.EXT_packed_depth_stencil
) {
399 return GL_INVALID_ENUM
;
401 if (format
!= GL_DEPTH_STENCIL
) {
402 return GL_INVALID_OPERATION
;
406 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
407 if (!ctx
->Extensions
.ARB_depth_buffer_float
) {
408 return GL_INVALID_ENUM
;
410 if (format
!= GL_DEPTH_STENCIL
) {
411 return GL_INVALID_OPERATION
;
419 /* now, for each format, check the type for compatibility */
422 case GL_STENCIL_INDEX
:
426 case GL_UNSIGNED_BYTE
:
428 case GL_UNSIGNED_SHORT
:
430 case GL_UNSIGNED_INT
:
434 return ctx
->Extensions
.ARB_half_float_pixel
435 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
437 return GL_INVALID_ENUM
;
444 #if 0 /* not legal! see table 3.6 of the 1.5 spec */
448 case GL_LUMINANCE_ALPHA
:
449 case GL_DEPTH_COMPONENT
:
452 case GL_UNSIGNED_BYTE
:
454 case GL_UNSIGNED_SHORT
:
456 case GL_UNSIGNED_INT
:
460 return ctx
->Extensions
.ARB_half_float_pixel
461 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
463 return GL_INVALID_ENUM
;
469 case GL_UNSIGNED_BYTE
:
471 case GL_UNSIGNED_SHORT
:
473 case GL_UNSIGNED_INT
:
475 case GL_UNSIGNED_BYTE_3_3_2
:
476 case GL_UNSIGNED_BYTE_2_3_3_REV
:
477 case GL_UNSIGNED_SHORT_5_6_5
:
478 case GL_UNSIGNED_SHORT_5_6_5_REV
:
481 return ctx
->Extensions
.ARB_half_float_pixel
482 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
484 return GL_INVALID_ENUM
;
489 /* NOTE: no packed types are supported with BGR. That's
490 * intentional, according to the GL spec.
493 case GL_UNSIGNED_BYTE
:
495 case GL_UNSIGNED_SHORT
:
497 case GL_UNSIGNED_INT
:
501 return ctx
->Extensions
.ARB_half_float_pixel
502 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
504 return GL_INVALID_ENUM
;
512 case GL_UNSIGNED_BYTE
:
514 case GL_UNSIGNED_SHORT
:
516 case GL_UNSIGNED_INT
:
518 case GL_UNSIGNED_SHORT_4_4_4_4
:
519 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
520 case GL_UNSIGNED_SHORT_5_5_5_1
:
521 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
522 case GL_UNSIGNED_INT_8_8_8_8
:
523 case GL_UNSIGNED_INT_8_8_8_8_REV
:
526 return ctx
->Extensions
.ARB_half_float_pixel
527 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
529 return GL_INVALID_ENUM
;
533 if (!ctx
->Extensions
.MESA_ycbcr_texture
)
534 return GL_INVALID_ENUM
;
535 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
||
536 type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
)
539 return GL_INVALID_OPERATION
;
541 case GL_DEPTH_STENCIL_EXT
:
542 if (ctx
->Extensions
.EXT_packed_depth_stencil
&&
543 type
== GL_UNSIGNED_INT_24_8
)
545 else if (ctx
->Extensions
.ARB_depth_buffer_float
&&
546 type
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
)
549 return GL_INVALID_ENUM
;
553 if (!ctx
->Extensions
.ATI_envmap_bumpmap
)
554 return GL_INVALID_ENUM
;
557 case GL_UNSIGNED_BYTE
:
559 case GL_UNSIGNED_SHORT
:
561 case GL_UNSIGNED_INT
:
565 return GL_INVALID_ENUM
;
568 /* integer-valued formats */
569 case GL_RED_INTEGER_EXT
:
570 case GL_GREEN_INTEGER_EXT
:
571 case GL_BLUE_INTEGER_EXT
:
572 case GL_ALPHA_INTEGER_EXT
:
576 case GL_UNSIGNED_BYTE
:
578 case GL_UNSIGNED_SHORT
:
580 case GL_UNSIGNED_INT
:
581 return (ctx
->VersionMajor
>= 3 ||
582 ctx
->Extensions
.EXT_texture_integer
)
583 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
585 return GL_INVALID_ENUM
;
588 case GL_RGB_INTEGER_EXT
:
591 case GL_UNSIGNED_BYTE
:
593 case GL_UNSIGNED_SHORT
:
595 case GL_UNSIGNED_INT
:
596 return (ctx
->VersionMajor
>= 3 ||
597 ctx
->Extensions
.EXT_texture_integer
)
598 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
600 return GL_INVALID_ENUM
;
603 case GL_BGR_INTEGER_EXT
:
606 case GL_UNSIGNED_BYTE
:
608 case GL_UNSIGNED_SHORT
:
610 case GL_UNSIGNED_INT
:
611 /* NOTE: no packed formats w/ BGR format */
612 return (ctx
->VersionMajor
>= 3 ||
613 ctx
->Extensions
.EXT_texture_integer
)
614 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
616 return GL_INVALID_ENUM
;
619 case GL_RGBA_INTEGER_EXT
:
620 case GL_BGRA_INTEGER_EXT
:
623 case GL_UNSIGNED_BYTE
:
625 case GL_UNSIGNED_SHORT
:
627 case GL_UNSIGNED_INT
:
628 return (ctx
->VersionMajor
>= 3 ||
629 ctx
->Extensions
.EXT_texture_integer
)
630 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
632 return GL_INVALID_ENUM
;
635 case GL_LUMINANCE_INTEGER_EXT
:
636 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
639 case GL_UNSIGNED_BYTE
:
641 case GL_UNSIGNED_SHORT
:
643 case GL_UNSIGNED_INT
:
644 return ctx
->Extensions
.EXT_texture_integer
645 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
647 return GL_INVALID_ENUM
;
651 return GL_INVALID_ENUM
;
658 * Test if the given image format is a color/RGBA format (i.e., not color
659 * index, depth, stencil, etc).
660 * \param format the image format value (may by an internal texture format)
661 * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
664 _mesa_is_color_format(GLenum format
)
682 case GL_LUMINANCE_ALPHA
:
683 case GL_LUMINANCE4_ALPHA4
:
684 case GL_LUMINANCE6_ALPHA2
:
685 case GL_LUMINANCE8_ALPHA8
:
686 case GL_LUMINANCE12_ALPHA4
:
687 case GL_LUMINANCE12_ALPHA12
:
688 case GL_LUMINANCE16_ALPHA16
:
720 /* float texture formats */
721 case GL_ALPHA16F_ARB
:
722 case GL_ALPHA32F_ARB
:
723 case GL_LUMINANCE16F_ARB
:
724 case GL_LUMINANCE32F_ARB
:
725 case GL_LUMINANCE_ALPHA16F_ARB
:
726 case GL_LUMINANCE_ALPHA32F_ARB
:
727 case GL_INTENSITY16F_ARB
:
728 case GL_INTENSITY32F_ARB
:
737 #if FEATURE_EXT_texture_sRGB
740 case GL_SRGB_ALPHA_EXT
:
741 case GL_SRGB8_ALPHA8_EXT
:
742 case GL_SLUMINANCE_ALPHA_EXT
:
743 case GL_SLUMINANCE8_ALPHA8_EXT
:
744 case GL_SLUMINANCE_EXT
:
745 case GL_SLUMINANCE8_EXT
:
746 #endif /* FEATURE_EXT_texture_sRGB */
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
:
756 case GL_LUMINANCE_INTEGER_EXT
:
757 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
758 /* sized integer formats */
759 case GL_RGBA32UI_EXT
:
761 case GL_ALPHA32UI_EXT
:
762 case GL_INTENSITY32UI_EXT
:
763 case GL_LUMINANCE32UI_EXT
:
764 case GL_LUMINANCE_ALPHA32UI_EXT
:
765 case GL_RGBA16UI_EXT
:
767 case GL_ALPHA16UI_EXT
:
768 case GL_INTENSITY16UI_EXT
:
769 case GL_LUMINANCE16UI_EXT
:
770 case GL_LUMINANCE_ALPHA16UI_EXT
:
773 case GL_ALPHA8UI_EXT
:
774 case GL_INTENSITY8UI_EXT
:
775 case GL_LUMINANCE8UI_EXT
:
776 case GL_LUMINANCE_ALPHA8UI_EXT
:
779 case GL_ALPHA32I_EXT
:
780 case GL_INTENSITY32I_EXT
:
781 case GL_LUMINANCE32I_EXT
:
782 case GL_LUMINANCE_ALPHA32I_EXT
:
785 case GL_ALPHA16I_EXT
:
786 case GL_INTENSITY16I_EXT
:
787 case GL_LUMINANCE16I_EXT
:
788 case GL_LUMINANCE_ALPHA16I_EXT
:
792 case GL_INTENSITY8I_EXT
:
793 case GL_LUMINANCE8I_EXT
:
794 case GL_LUMINANCE_ALPHA8I_EXT
:
795 case GL_R11F_G11F_B10F
:
798 case GL_YCBCR_MESA
: /* not considered to be RGB */
807 * Test if the given image format is a depth component format.
810 _mesa_is_depth_format(GLenum format
)
813 case GL_DEPTH_COMPONENT
:
814 case GL_DEPTH_COMPONENT16
:
815 case GL_DEPTH_COMPONENT24
:
816 case GL_DEPTH_COMPONENT32
:
817 case GL_DEPTH_COMPONENT32F
:
826 * Test if the given image format is a stencil format.
829 _mesa_is_stencil_format(GLenum format
)
832 case GL_STENCIL_INDEX
:
841 * Test if the given image format is a YCbCr format.
844 _mesa_is_ycbcr_format(GLenum format
)
856 * Test if the given image format is a depth+stencil format.
859 _mesa_is_depthstencil_format(GLenum format
)
862 case GL_DEPTH24_STENCIL8_EXT
:
863 case GL_DEPTH_STENCIL_EXT
:
864 case GL_DEPTH32F_STENCIL8
:
873 * Test if the given image format is a depth or stencil format.
876 _mesa_is_depth_or_stencil_format(GLenum format
)
879 case GL_DEPTH_COMPONENT
:
880 case GL_DEPTH_COMPONENT16
:
881 case GL_DEPTH_COMPONENT24
:
882 case GL_DEPTH_COMPONENT32
:
883 case GL_STENCIL_INDEX
:
884 case GL_STENCIL_INDEX1_EXT
:
885 case GL_STENCIL_INDEX4_EXT
:
886 case GL_STENCIL_INDEX8_EXT
:
887 case GL_STENCIL_INDEX16_EXT
:
888 case GL_DEPTH_STENCIL_EXT
:
889 case GL_DEPTH24_STENCIL8_EXT
:
890 case GL_DEPTH_COMPONENT32F
:
891 case GL_DEPTH32F_STENCIL8
:
900 * Test if the given image format is a dudv format.
903 _mesa_is_dudv_format(GLenum format
)
916 * Test if the given format is an integer (non-normalized) format.
919 _mesa_is_integer_format(GLenum format
)
922 /* generic integer formats */
923 case GL_RED_INTEGER_EXT
:
924 case GL_GREEN_INTEGER_EXT
:
925 case GL_BLUE_INTEGER_EXT
:
926 case GL_ALPHA_INTEGER_EXT
:
927 case GL_RGB_INTEGER_EXT
:
928 case GL_RGBA_INTEGER_EXT
:
929 case GL_BGR_INTEGER_EXT
:
930 case GL_BGRA_INTEGER_EXT
:
931 case GL_LUMINANCE_INTEGER_EXT
:
932 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
934 /* specific integer formats */
935 case GL_RGBA32UI_EXT
:
939 case GL_ALPHA32UI_EXT
:
940 case GL_INTENSITY32UI_EXT
:
941 case GL_LUMINANCE32UI_EXT
:
942 case GL_LUMINANCE_ALPHA32UI_EXT
:
943 case GL_RGBA16UI_EXT
:
947 case GL_ALPHA16UI_EXT
:
948 case GL_INTENSITY16UI_EXT
:
949 case GL_LUMINANCE16UI_EXT
:
950 case GL_LUMINANCE_ALPHA16UI_EXT
:
955 case GL_ALPHA8UI_EXT
:
956 case GL_INTENSITY8UI_EXT
:
957 case GL_LUMINANCE8UI_EXT
:
958 case GL_LUMINANCE_ALPHA8UI_EXT
:
963 case GL_ALPHA32I_EXT
:
964 case GL_INTENSITY32I_EXT
:
965 case GL_LUMINANCE32I_EXT
:
966 case GL_LUMINANCE_ALPHA32I_EXT
:
971 case GL_ALPHA16I_EXT
:
972 case GL_INTENSITY16I_EXT
:
973 case GL_LUMINANCE16I_EXT
:
974 case GL_LUMINANCE_ALPHA16I_EXT
:
980 case GL_INTENSITY8I_EXT
:
981 case GL_LUMINANCE8I_EXT
:
982 case GL_LUMINANCE_ALPHA8I_EXT
:
992 * Does the given base texture/renderbuffer format have the channel
996 _mesa_base_format_has_channel(GLenum base_format
, GLenum pname
)
999 case GL_TEXTURE_RED_SIZE
:
1000 case GL_TEXTURE_RED_TYPE
:
1001 case GL_RENDERBUFFER_RED_SIZE_EXT
:
1002 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
1003 if (base_format
== GL_RED
||
1004 base_format
== GL_RG
||
1005 base_format
== GL_RGB
||
1006 base_format
== GL_RGBA
) {
1010 case GL_TEXTURE_GREEN_SIZE
:
1011 case GL_TEXTURE_GREEN_TYPE
:
1012 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
1013 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
1014 if (base_format
== GL_RG
||
1015 base_format
== GL_RGB
||
1016 base_format
== GL_RGBA
) {
1020 case GL_TEXTURE_BLUE_SIZE
:
1021 case GL_TEXTURE_BLUE_TYPE
:
1022 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
1023 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
1024 if (base_format
== GL_RGB
||
1025 base_format
== GL_RGBA
) {
1029 case GL_TEXTURE_ALPHA_SIZE
:
1030 case GL_TEXTURE_ALPHA_TYPE
:
1031 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
1032 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
1033 if (base_format
== GL_RGBA
||
1034 base_format
== GL_ALPHA
||
1035 base_format
== GL_LUMINANCE_ALPHA
) {
1039 case GL_TEXTURE_LUMINANCE_SIZE
:
1040 case GL_TEXTURE_LUMINANCE_TYPE
:
1041 if (base_format
== GL_LUMINANCE
||
1042 base_format
== GL_LUMINANCE_ALPHA
) {
1046 case GL_TEXTURE_INTENSITY_SIZE
:
1047 case GL_TEXTURE_INTENSITY_TYPE
:
1048 if (base_format
== GL_INTENSITY
) {
1052 case GL_TEXTURE_DEPTH_SIZE
:
1053 case GL_TEXTURE_DEPTH_TYPE
:
1054 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
1055 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
1056 if (base_format
== GL_DEPTH_STENCIL
||
1057 base_format
== GL_DEPTH_COMPONENT
) {
1061 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
1062 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
1063 if (base_format
== GL_DEPTH_STENCIL
||
1064 base_format
== GL_STENCIL_INDEX
) {
1069 _mesa_warning(NULL
, "%s: Unexpected channel token 0x%x\n",
1070 __FUNCTION__
, pname
);
1079 * Return the byte offset of a specific pixel in an image (1D, 2D or 3D).
1081 * Pixel unpacking/packing parameters are observed according to \p packing.
1083 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
1084 * \param packing the pixelstore attributes
1085 * \param width the image width
1086 * \param height the image height
1087 * \param format the pixel format (must be validated beforehand)
1088 * \param type the pixel data type (must be validated beforehand)
1089 * \param img which image in the volume (0 for 1D or 2D images)
1090 * \param row row of pixel in the image (0 for 1D images)
1091 * \param column column of pixel in the image
1093 * \return offset of pixel.
1095 * \sa gl_pixelstore_attrib.
1098 _mesa_image_offset( GLuint dimensions
,
1099 const struct gl_pixelstore_attrib
*packing
,
1100 GLsizei width
, GLsizei height
,
1101 GLenum format
, GLenum type
,
1102 GLint img
, GLint row
, GLint column
)
1104 GLint alignment
; /* 1, 2 or 4 */
1105 GLint pixels_per_row
;
1106 GLint rows_per_image
;
1109 GLint skipimages
; /* for 3-D volume images */
1112 ASSERT(dimensions
>= 1 && dimensions
<= 3);
1114 alignment
= packing
->Alignment
;
1115 if (packing
->RowLength
> 0) {
1116 pixels_per_row
= packing
->RowLength
;
1119 pixels_per_row
= width
;
1121 if (packing
->ImageHeight
> 0) {
1122 rows_per_image
= packing
->ImageHeight
;
1125 rows_per_image
= height
;
1128 skippixels
= packing
->SkipPixels
;
1129 /* Note: SKIP_ROWS _is_ used for 1D images */
1130 skiprows
= packing
->SkipRows
;
1131 /* Note: SKIP_IMAGES is only used for 3D images */
1132 skipimages
= (dimensions
== 3) ? packing
->SkipImages
: 0;
1134 if (type
== GL_BITMAP
) {
1136 GLint bytes_per_row
;
1137 GLint bytes_per_image
;
1138 /* components per pixel for color or stencil index: */
1139 const GLint comp_per_pixel
= 1;
1141 /* The pixel type and format should have been error checked earlier */
1142 assert(format
== GL_COLOR_INDEX
|| format
== GL_STENCIL_INDEX
);
1144 bytes_per_row
= alignment
1145 * CEILING( comp_per_pixel
*pixels_per_row
, 8*alignment
);
1147 bytes_per_image
= bytes_per_row
* rows_per_image
;
1149 offset
= (skipimages
+ img
) * bytes_per_image
1150 + (skiprows
+ row
) * bytes_per_row
1151 + (skippixels
+ column
) / 8;
1154 /* Non-BITMAP data */
1155 GLint bytes_per_pixel
, bytes_per_row
, remainder
, bytes_per_image
;
1158 bytes_per_pixel
= _mesa_bytes_per_pixel( format
, type
);
1160 /* The pixel type and format should have been error checked earlier */
1161 assert(bytes_per_pixel
> 0);
1163 bytes_per_row
= pixels_per_row
* bytes_per_pixel
;
1164 remainder
= bytes_per_row
% alignment
;
1166 bytes_per_row
+= (alignment
- remainder
);
1168 ASSERT(bytes_per_row
% alignment
== 0);
1170 bytes_per_image
= bytes_per_row
* rows_per_image
;
1172 if (packing
->Invert
) {
1173 /* set pixel_addr to the last row */
1174 topOfImage
= bytes_per_row
* (height
- 1);
1175 bytes_per_row
= -bytes_per_row
;
1181 /* compute final pixel address */
1182 offset
= (skipimages
+ img
) * bytes_per_image
1184 + (skiprows
+ row
) * bytes_per_row
1185 + (skippixels
+ column
) * bytes_per_pixel
;
1193 * Return the address of a specific pixel in an image (1D, 2D or 3D).
1195 * Pixel unpacking/packing parameters are observed according to \p packing.
1197 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
1198 * \param packing the pixelstore attributes
1199 * \param image starting address of image data
1200 * \param width the image width
1201 * \param height the image height
1202 * \param format the pixel format (must be validated beforehand)
1203 * \param type the pixel data type (must be validated beforehand)
1204 * \param img which image in the volume (0 for 1D or 2D images)
1205 * \param row row of pixel in the image (0 for 1D images)
1206 * \param column column of pixel in the image
1208 * \return address of pixel.
1210 * \sa gl_pixelstore_attrib.
1213 _mesa_image_address( GLuint dimensions
,
1214 const struct gl_pixelstore_attrib
*packing
,
1215 const GLvoid
*image
,
1216 GLsizei width
, GLsizei height
,
1217 GLenum format
, GLenum type
,
1218 GLint img
, GLint row
, GLint column
)
1220 const GLubyte
*addr
= (const GLubyte
*) image
;
1222 addr
+= _mesa_image_offset(dimensions
, packing
, width
, height
,
1223 format
, type
, img
, row
, column
);
1225 return (GLvoid
*) addr
;
1230 _mesa_image_address1d( const struct gl_pixelstore_attrib
*packing
,
1231 const GLvoid
*image
,
1233 GLenum format
, GLenum type
,
1236 return _mesa_image_address(1, packing
, image
, width
, 1,
1237 format
, type
, 0, 0, column
);
1242 _mesa_image_address2d( const struct gl_pixelstore_attrib
*packing
,
1243 const GLvoid
*image
,
1244 GLsizei width
, GLsizei height
,
1245 GLenum format
, GLenum type
,
1246 GLint row
, GLint column
)
1248 return _mesa_image_address(2, packing
, image
, width
, height
,
1249 format
, type
, 0, row
, column
);
1254 _mesa_image_address3d( const struct gl_pixelstore_attrib
*packing
,
1255 const GLvoid
*image
,
1256 GLsizei width
, GLsizei height
,
1257 GLenum format
, GLenum type
,
1258 GLint img
, GLint row
, GLint column
)
1260 return _mesa_image_address(3, packing
, image
, width
, height
,
1261 format
, type
, img
, row
, column
);
1267 * Compute the stride (in bytes) between image rows.
1269 * \param packing the pixelstore attributes
1270 * \param width image width.
1271 * \param format pixel format.
1272 * \param type pixel data type.
1274 * \return the stride in bytes for the given parameters, or -1 if error
1277 _mesa_image_row_stride( const struct gl_pixelstore_attrib
*packing
,
1278 GLint width
, GLenum format
, GLenum type
)
1280 GLint bytesPerRow
, remainder
;
1284 if (type
== GL_BITMAP
) {
1285 if (packing
->RowLength
== 0) {
1286 bytesPerRow
= (width
+ 7) / 8;
1289 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
1293 /* Non-BITMAP data */
1294 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1295 if (bytesPerPixel
<= 0)
1296 return -1; /* error */
1297 if (packing
->RowLength
== 0) {
1298 bytesPerRow
= bytesPerPixel
* width
;
1301 bytesPerRow
= bytesPerPixel
* packing
->RowLength
;
1305 remainder
= bytesPerRow
% packing
->Alignment
;
1306 if (remainder
> 0) {
1307 bytesPerRow
+= (packing
->Alignment
- remainder
);
1310 if (packing
->Invert
) {
1311 /* negate the bytes per row (negative row stride) */
1312 bytesPerRow
= -bytesPerRow
;
1320 * Compute the stride between images in a 3D texture (in bytes) for the given
1321 * pixel packing parameters and image width, format and type.
1324 _mesa_image_image_stride( const struct gl_pixelstore_attrib
*packing
,
1325 GLint width
, GLint height
,
1326 GLenum format
, GLenum type
)
1328 GLint bytesPerRow
, bytesPerImage
, remainder
;
1332 if (type
== GL_BITMAP
) {
1333 if (packing
->RowLength
== 0) {
1334 bytesPerRow
= (width
+ 7) / 8;
1337 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
1341 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1343 if (bytesPerPixel
<= 0)
1344 return -1; /* error */
1345 if (packing
->RowLength
== 0) {
1346 bytesPerRow
= bytesPerPixel
* width
;
1349 bytesPerRow
= bytesPerPixel
* packing
->RowLength
;
1353 remainder
= bytesPerRow
% packing
->Alignment
;
1355 bytesPerRow
+= (packing
->Alignment
- remainder
);
1357 if (packing
->ImageHeight
== 0)
1358 bytesPerImage
= bytesPerRow
* height
;
1360 bytesPerImage
= bytesPerRow
* packing
->ImageHeight
;
1362 return bytesPerImage
;
1368 * "Expand" a bitmap from 1-bit per pixel to 8-bits per pixel.
1369 * This is typically used to convert a bitmap into a GLubyte/pixel texture.
1370 * "On" bits will set texels to \p onValue.
1371 * "Off" bits will not modify texels.
1372 * \param width src bitmap width in pixels
1373 * \param height src bitmap height in pixels
1374 * \param unpack bitmap unpacking state
1375 * \param bitmap the src bitmap data
1376 * \param destBuffer start of dest buffer
1377 * \param destStride row stride in dest buffer
1378 * \param onValue if bit is 1, set destBuffer pixel to this value
1381 _mesa_expand_bitmap(GLsizei width
, GLsizei height
,
1382 const struct gl_pixelstore_attrib
*unpack
,
1383 const GLubyte
*bitmap
,
1384 GLubyte
*destBuffer
, GLint destStride
,
1387 const GLubyte
*srcRow
= (const GLubyte
*)
1388 _mesa_image_address2d(unpack
, bitmap
, width
, height
,
1389 GL_COLOR_INDEX
, GL_BITMAP
, 0, 0);
1390 const GLint srcStride
= _mesa_image_row_stride(unpack
, width
,
1391 GL_COLOR_INDEX
, GL_BITMAP
);
1394 #define SET_PIXEL(COL, ROW) \
1395 destBuffer[(ROW) * destStride + (COL)] = onValue;
1397 for (row
= 0; row
< height
; row
++) {
1398 const GLubyte
*src
= srcRow
;
1400 if (unpack
->LsbFirst
) {
1402 GLubyte mask
= 1U << (unpack
->SkipPixels
& 0x7);
1403 for (col
= 0; col
< width
; col
++) {
1406 SET_PIXEL(col
, row
);
1418 /* get ready for next row */
1424 GLubyte mask
= 128U >> (unpack
->SkipPixels
& 0x7);
1425 for (col
= 0; col
< width
; col
++) {
1428 SET_PIXEL(col
, row
);
1440 /* get ready for next row */
1445 srcRow
+= srcStride
;
1455 * Convert an array of RGBA colors from one datatype to another.
1456 * NOTE: src may equal dst. In that case, we use a temporary buffer.
1459 _mesa_convert_colors(GLenum srcType
, const GLvoid
*src
,
1460 GLenum dstType
, GLvoid
*dst
,
1461 GLuint count
, const GLubyte mask
[])
1464 const GLboolean useTemp
= (src
== dst
);
1466 tempBuffer
= malloc(count
* MAX_PIXEL_BYTES
);
1470 ASSERT(srcType
!= dstType
);
1473 case GL_UNSIGNED_BYTE
:
1474 if (dstType
== GL_UNSIGNED_SHORT
) {
1475 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
1476 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
1478 for (i
= 0; i
< count
; i
++) {
1479 if (!mask
|| mask
[i
]) {
1480 dst2
[i
][RCOMP
] = UBYTE_TO_USHORT(src1
[i
][RCOMP
]);
1481 dst2
[i
][GCOMP
] = UBYTE_TO_USHORT(src1
[i
][GCOMP
]);
1482 dst2
[i
][BCOMP
] = UBYTE_TO_USHORT(src1
[i
][BCOMP
]);
1483 dst2
[i
][ACOMP
] = UBYTE_TO_USHORT(src1
[i
][ACOMP
]);
1487 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
1490 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
1491 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
1493 ASSERT(dstType
== GL_FLOAT
);
1494 for (i
= 0; i
< count
; i
++) {
1495 if (!mask
|| mask
[i
]) {
1496 dst4
[i
][RCOMP
] = UBYTE_TO_FLOAT(src1
[i
][RCOMP
]);
1497 dst4
[i
][GCOMP
] = UBYTE_TO_FLOAT(src1
[i
][GCOMP
]);
1498 dst4
[i
][BCOMP
] = UBYTE_TO_FLOAT(src1
[i
][BCOMP
]);
1499 dst4
[i
][ACOMP
] = UBYTE_TO_FLOAT(src1
[i
][ACOMP
]);
1503 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
1506 case GL_UNSIGNED_SHORT
:
1507 if (dstType
== GL_UNSIGNED_BYTE
) {
1508 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
1509 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
1511 for (i
= 0; i
< count
; i
++) {
1512 if (!mask
|| mask
[i
]) {
1513 dst1
[i
][RCOMP
] = USHORT_TO_UBYTE(src2
[i
][RCOMP
]);
1514 dst1
[i
][GCOMP
] = USHORT_TO_UBYTE(src2
[i
][GCOMP
]);
1515 dst1
[i
][BCOMP
] = USHORT_TO_UBYTE(src2
[i
][BCOMP
]);
1516 dst1
[i
][ACOMP
] = USHORT_TO_UBYTE(src2
[i
][ACOMP
]);
1520 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
1523 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
1524 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
1526 ASSERT(dstType
== GL_FLOAT
);
1527 for (i
= 0; i
< count
; i
++) {
1528 if (!mask
|| mask
[i
]) {
1529 dst4
[i
][RCOMP
] = USHORT_TO_FLOAT(src2
[i
][RCOMP
]);
1530 dst4
[i
][GCOMP
] = USHORT_TO_FLOAT(src2
[i
][GCOMP
]);
1531 dst4
[i
][BCOMP
] = USHORT_TO_FLOAT(src2
[i
][BCOMP
]);
1532 dst4
[i
][ACOMP
] = USHORT_TO_FLOAT(src2
[i
][ACOMP
]);
1536 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
1540 if (dstType
== GL_UNSIGNED_BYTE
) {
1541 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
1542 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
1544 for (i
= 0; i
< count
; i
++) {
1545 if (!mask
|| mask
[i
])
1546 _mesa_unclamped_float_rgba_to_ubyte(dst1
[i
], src4
[i
]);
1549 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
1552 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
1553 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
1555 ASSERT(dstType
== GL_UNSIGNED_SHORT
);
1556 for (i
= 0; i
< count
; i
++) {
1557 if (!mask
|| mask
[i
]) {
1558 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][RCOMP
], src4
[i
][RCOMP
]);
1559 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][GCOMP
], src4
[i
][GCOMP
]);
1560 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][BCOMP
], src4
[i
][BCOMP
]);
1561 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][ACOMP
], src4
[i
][ACOMP
]);
1565 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
1569 _mesa_problem(NULL
, "Invalid datatype in _mesa_convert_colors");
1579 * Perform basic clipping for glDrawPixels. The image's position and size
1580 * and the unpack SkipPixels and SkipRows are adjusted so that the image
1581 * region is entirely within the window and scissor bounds.
1582 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
1583 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
1584 * we'll actually write. Beforehand, *destY-1 is the first drawing row.
1586 * \return GL_TRUE if image is ready for drawing or
1587 * GL_FALSE if image was completely clipped away (draw nothing)
1590 _mesa_clip_drawpixels(const struct gl_context
*ctx
,
1591 GLint
*destX
, GLint
*destY
,
1592 GLsizei
*width
, GLsizei
*height
,
1593 struct gl_pixelstore_attrib
*unpack
)
1595 const struct gl_framebuffer
*buffer
= ctx
->DrawBuffer
;
1597 if (unpack
->RowLength
== 0) {
1598 unpack
->RowLength
= *width
;
1601 ASSERT(ctx
->Pixel
.ZoomX
== 1.0F
);
1602 ASSERT(ctx
->Pixel
.ZoomY
== 1.0F
|| ctx
->Pixel
.ZoomY
== -1.0F
);
1605 if (*destX
< buffer
->_Xmin
) {
1606 unpack
->SkipPixels
+= (buffer
->_Xmin
- *destX
);
1607 *width
-= (buffer
->_Xmin
- *destX
);
1608 *destX
= buffer
->_Xmin
;
1610 /* right clipping */
1611 if (*destX
+ *width
> buffer
->_Xmax
)
1612 *width
-= (*destX
+ *width
- buffer
->_Xmax
);
1617 if (ctx
->Pixel
.ZoomY
== 1.0F
) {
1618 /* bottom clipping */
1619 if (*destY
< buffer
->_Ymin
) {
1620 unpack
->SkipRows
+= (buffer
->_Ymin
- *destY
);
1621 *height
-= (buffer
->_Ymin
- *destY
);
1622 *destY
= buffer
->_Ymin
;
1625 if (*destY
+ *height
> buffer
->_Ymax
)
1626 *height
-= (*destY
+ *height
- buffer
->_Ymax
);
1628 else { /* upside down */
1630 if (*destY
> buffer
->_Ymax
) {
1631 unpack
->SkipRows
+= (*destY
- buffer
->_Ymax
);
1632 *height
-= (*destY
- buffer
->_Ymax
);
1633 *destY
= buffer
->_Ymax
;
1635 /* bottom clipping */
1636 if (*destY
- *height
< buffer
->_Ymin
)
1637 *height
-= (buffer
->_Ymin
- (*destY
- *height
));
1638 /* adjust destY so it's the first row to write to */
1650 * Perform clipping for glReadPixels. The image's window position
1651 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
1652 * so that the image region is entirely within the window bounds.
1653 * Note: this is different from _mesa_clip_drawpixels() in that the
1654 * scissor box is ignored, and we use the bounds of the current readbuffer
1657 * \return GL_TRUE if region to read is in bounds
1658 * GL_FALSE if region is completely out of bounds (nothing to read)
1661 _mesa_clip_readpixels(const struct gl_context
*ctx
,
1662 GLint
*srcX
, GLint
*srcY
,
1663 GLsizei
*width
, GLsizei
*height
,
1664 struct gl_pixelstore_attrib
*pack
)
1666 const struct gl_framebuffer
*buffer
= ctx
->ReadBuffer
;
1668 if (pack
->RowLength
== 0) {
1669 pack
->RowLength
= *width
;
1674 pack
->SkipPixels
+= (0 - *srcX
);
1675 *width
-= (0 - *srcX
);
1678 /* right clipping */
1679 if (*srcX
+ *width
> (GLsizei
) buffer
->Width
)
1680 *width
-= (*srcX
+ *width
- buffer
->Width
);
1685 /* bottom clipping */
1687 pack
->SkipRows
+= (0 - *srcY
);
1688 *height
-= (0 - *srcY
);
1692 if (*srcY
+ *height
> (GLsizei
) buffer
->Height
)
1693 *height
-= (*srcY
+ *height
- buffer
->Height
);
1703 * Do clipping for a glCopyTexSubImage call.
1704 * The framebuffer source region might extend outside the framebuffer
1705 * bounds. Clip the source region against the framebuffer bounds and
1706 * adjust the texture/dest position and size accordingly.
1708 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
1711 _mesa_clip_copytexsubimage(const struct gl_context
*ctx
,
1712 GLint
*destX
, GLint
*destY
,
1713 GLint
*srcX
, GLint
*srcY
,
1714 GLsizei
*width
, GLsizei
*height
)
1716 const struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1717 const GLint srcX0
= *srcX
, srcY0
= *srcY
;
1719 if (_mesa_clip_to_region(0, 0, fb
->Width
, fb
->Height
,
1720 srcX
, srcY
, width
, height
)) {
1721 *destX
= *destX
+ *srcX
- srcX0
;
1722 *destY
= *destY
+ *srcY
- srcY0
;
1734 * Clip the rectangle defined by (x, y, width, height) against the bounds
1735 * specified by [xmin, xmax) and [ymin, ymax).
1736 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
1739 _mesa_clip_to_region(GLint xmin
, GLint ymin
,
1740 GLint xmax
, GLint ymax
,
1742 GLsizei
*width
, GLsizei
*height
)
1746 *width
-= (xmin
- *x
);
1750 /* right clipping */
1751 if (*x
+ *width
> xmax
)
1752 *width
-= (*x
+ *width
- xmax
);
1757 /* bottom (or top) clipping */
1759 *height
-= (ymin
- *y
);
1763 /* top (or bottom) clipping */
1764 if (*y
+ *height
> ymax
)
1765 *height
-= (*y
+ *height
- ymax
);
1775 * Clip dst coords against Xmax (or Ymax).
1778 clip_right_or_top(GLint
*srcX0
, GLint
*srcX1
,
1779 GLint
*dstX0
, GLint
*dstX1
,
1784 if (*dstX1
> maxValue
) {
1785 /* X1 outside right edge */
1786 ASSERT(*dstX0
< maxValue
); /* X0 should be inside right edge */
1787 t
= (GLfloat
) (maxValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
1788 /* chop off [t, 1] part */
1789 ASSERT(t
>= 0.0 && t
<= 1.0);
1791 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
1792 *srcX1
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
1794 else if (*dstX0
> maxValue
) {
1795 /* X0 outside right edge */
1796 ASSERT(*dstX1
< maxValue
); /* X1 should be inside right edge */
1797 t
= (GLfloat
) (maxValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
1798 /* chop off [t, 1] part */
1799 ASSERT(t
>= 0.0 && t
<= 1.0);
1801 bias
= (*srcX0
< *srcX1
) ? -0.5F
: 0.5F
;
1802 *srcX0
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
1808 * Clip dst coords against Xmin (or Ymin).
1811 clip_left_or_bottom(GLint
*srcX0
, GLint
*srcX1
,
1812 GLint
*dstX0
, GLint
*dstX1
,
1817 if (*dstX0
< minValue
) {
1818 /* X0 outside left edge */
1819 ASSERT(*dstX1
> minValue
); /* X1 should be inside left edge */
1820 t
= (GLfloat
) (minValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
1821 /* chop off [0, t] part */
1822 ASSERT(t
>= 0.0 && t
<= 1.0);
1824 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
; /* flipped??? */
1825 *srcX0
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
1827 else if (*dstX1
< minValue
) {
1828 /* X1 outside left edge */
1829 ASSERT(*dstX0
> minValue
); /* X0 should be inside left edge */
1830 t
= (GLfloat
) (minValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
1831 /* chop off [0, t] part */
1832 ASSERT(t
>= 0.0 && t
<= 1.0);
1834 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
1835 *srcX1
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
1841 * Do clipping of blit src/dest rectangles.
1842 * The dest rect is clipped against both the buffer bounds and scissor bounds.
1843 * The src rect is just clipped against the buffer bounds.
1845 * When either the src or dest rect is clipped, the other is also clipped
1848 * Note that X0 need not be less than X1 (same for Y) for either the source
1849 * and dest rects. That makes the clipping a little trickier.
1851 * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped
1854 _mesa_clip_blit(struct gl_context
*ctx
,
1855 GLint
*srcX0
, GLint
*srcY0
, GLint
*srcX1
, GLint
*srcY1
,
1856 GLint
*dstX0
, GLint
*dstY0
, GLint
*dstX1
, GLint
*dstY1
)
1858 const GLint srcXmin
= 0;
1859 const GLint srcXmax
= ctx
->ReadBuffer
->Width
;
1860 const GLint srcYmin
= 0;
1861 const GLint srcYmax
= ctx
->ReadBuffer
->Height
;
1863 /* these include scissor bounds */
1864 const GLint dstXmin
= ctx
->DrawBuffer
->_Xmin
;
1865 const GLint dstXmax
= ctx
->DrawBuffer
->_Xmax
;
1866 const GLint dstYmin
= ctx
->DrawBuffer
->_Ymin
;
1867 const GLint dstYmax
= ctx
->DrawBuffer
->_Ymax
;
1870 printf("PreClipX: src: %d .. %d dst: %d .. %d\n",
1871 *srcX0, *srcX1, *dstX0, *dstX1);
1872 printf("PreClipY: src: %d .. %d dst: %d .. %d\n",
1873 *srcY0, *srcY1, *dstY0, *dstY1);
1876 /* trivial rejection tests */
1877 if (*dstX0
== *dstX1
)
1878 return GL_FALSE
; /* no width */
1879 if (*dstX0
<= dstXmin
&& *dstX1
<= dstXmin
)
1880 return GL_FALSE
; /* totally out (left) of bounds */
1881 if (*dstX0
>= dstXmax
&& *dstX1
>= dstXmax
)
1882 return GL_FALSE
; /* totally out (right) of bounds */
1884 if (*dstY0
== *dstY1
)
1886 if (*dstY0
<= dstYmin
&& *dstY1
<= dstYmin
)
1888 if (*dstY0
>= dstYmax
&& *dstY1
>= dstYmax
)
1891 if (*srcX0
== *srcX1
)
1893 if (*srcX0
<= srcXmin
&& *srcX1
<= srcXmin
)
1895 if (*srcX0
>= srcXmax
&& *srcX1
>= srcXmax
)
1898 if (*srcY0
== *srcY1
)
1900 if (*srcY0
<= srcYmin
&& *srcY1
<= srcYmin
)
1902 if (*srcY0
>= srcYmax
&& *srcY1
>= srcYmax
)
1908 clip_right_or_top(srcX0
, srcX1
, dstX0
, dstX1
, dstXmax
);
1909 clip_right_or_top(srcY0
, srcY1
, dstY0
, dstY1
, dstYmax
);
1910 clip_left_or_bottom(srcX0
, srcX1
, dstX0
, dstX1
, dstXmin
);
1911 clip_left_or_bottom(srcY0
, srcY1
, dstY0
, dstY1
, dstYmin
);
1914 * src clip (just swap src/dst values from above)
1916 clip_right_or_top(dstX0
, dstX1
, srcX0
, srcX1
, srcXmax
);
1917 clip_right_or_top(dstY0
, dstY1
, srcY0
, srcY1
, srcYmax
);
1918 clip_left_or_bottom(dstX0
, dstX1
, srcX0
, srcX1
, srcXmin
);
1919 clip_left_or_bottom(dstY0
, dstY1
, srcY0
, srcY1
, srcYmin
);
1922 printf("PostClipX: src: %d .. %d dst: %d .. %d\n",
1923 *srcX0, *srcX1, *dstX0, *dstX1);
1924 printf("PostClipY: src: %d .. %d dst: %d .. %d\n",
1925 *srcY0, *srcY1, *dstY0, *dstY1);
1928 ASSERT(*dstX0
>= dstXmin
);
1929 ASSERT(*dstX0
<= dstXmax
);
1930 ASSERT(*dstX1
>= dstXmin
);
1931 ASSERT(*dstX1
<= dstXmax
);
1933 ASSERT(*dstY0
>= dstYmin
);
1934 ASSERT(*dstY0
<= dstYmax
);
1935 ASSERT(*dstY1
>= dstYmin
);
1936 ASSERT(*dstY1
<= dstYmax
);
1938 ASSERT(*srcX0
>= srcXmin
);
1939 ASSERT(*srcX0
<= srcXmax
);
1940 ASSERT(*srcX1
>= srcXmin
);
1941 ASSERT(*srcX1
<= srcXmax
);
1943 ASSERT(*srcY0
>= srcYmin
);
1944 ASSERT(*srcY0
<= srcYmax
);
1945 ASSERT(*srcY1
>= srcYmin
);
1946 ASSERT(*srcY1
<= srcYmax
);