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_5_9_9_9_REV
:
64 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
74 * Flip the order of the 2 bytes in each word in the given array.
77 * \param n number of words.
80 _mesa_swap2( GLushort
*p
, GLuint n
)
83 for (i
= 0; i
< n
; i
++) {
84 p
[i
] = (p
[i
] >> 8) | ((p
[i
] << 8) & 0xff00);
91 * Flip the order of the 4 bytes in each word in the given array.
94 _mesa_swap4( GLuint
*p
, GLuint n
)
97 for (i
= 0; i
< n
; i
++) {
100 | ((b
>> 8) & 0xff00)
101 | ((b
<< 8) & 0xff0000)
102 | ((b
<< 24) & 0xff000000);
109 * Get the size of a GL data type.
111 * \param type GL data type.
113 * \return the size, in bytes, of the given data type, 0 if a GL_BITMAP, or -1
114 * if an invalid type enum.
117 _mesa_sizeof_type( GLenum type
)
122 case GL_UNSIGNED_BYTE
:
123 return sizeof(GLubyte
);
125 return sizeof(GLbyte
);
126 case GL_UNSIGNED_SHORT
:
127 return sizeof(GLushort
);
129 return sizeof(GLshort
);
130 case GL_UNSIGNED_INT
:
131 return sizeof(GLuint
);
133 return sizeof(GLint
);
135 return sizeof(GLfloat
);
137 return sizeof(GLdouble
);
138 case GL_HALF_FLOAT_ARB
:
139 return sizeof(GLhalfARB
);
141 return sizeof(GLfixed
);
149 * Same as _mesa_sizeof_type() but also accepting the packed pixel
153 _mesa_sizeof_packed_type( GLenum type
)
158 case GL_UNSIGNED_BYTE
:
159 return sizeof(GLubyte
);
161 return sizeof(GLbyte
);
162 case GL_UNSIGNED_SHORT
:
163 return sizeof(GLushort
);
165 return sizeof(GLshort
);
166 case GL_UNSIGNED_INT
:
167 return sizeof(GLuint
);
169 return sizeof(GLint
);
170 case GL_HALF_FLOAT_ARB
:
171 return sizeof(GLhalfARB
);
173 return sizeof(GLfloat
);
174 case GL_UNSIGNED_BYTE_3_3_2
:
175 case GL_UNSIGNED_BYTE_2_3_3_REV
:
176 case MESA_UNSIGNED_BYTE_4_4
:
177 return sizeof(GLubyte
);
178 case GL_UNSIGNED_SHORT_5_6_5
:
179 case GL_UNSIGNED_SHORT_5_6_5_REV
:
180 case GL_UNSIGNED_SHORT_4_4_4_4
:
181 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
182 case GL_UNSIGNED_SHORT_5_5_5_1
:
183 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
184 case GL_UNSIGNED_SHORT_8_8_MESA
:
185 case GL_UNSIGNED_SHORT_8_8_REV_MESA
:
186 return sizeof(GLushort
);
187 case GL_UNSIGNED_INT_8_8_8_8
:
188 case GL_UNSIGNED_INT_8_8_8_8_REV
:
189 case GL_UNSIGNED_INT_5_9_9_9_REV
:
190 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
191 return sizeof(GLuint
);
199 * Get the number of components in a pixel format.
201 * \param format pixel format.
203 * \return the number of components in the given format, or -1 if a bad format.
206 _mesa_components_in_format( GLenum format
)
210 case GL_STENCIL_INDEX
:
211 case GL_DEPTH_COMPONENT
:
213 case GL_RED_INTEGER_EXT
:
215 case GL_GREEN_INTEGER_EXT
:
217 case GL_BLUE_INTEGER_EXT
:
219 case GL_ALPHA_INTEGER_EXT
:
221 case GL_LUMINANCE_INTEGER_EXT
:
225 case GL_LUMINANCE_ALPHA
:
226 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
234 case GL_RGB_INTEGER_EXT
:
235 case GL_BGR_INTEGER_EXT
:
241 case GL_RGBA_INTEGER_EXT
:
242 case GL_BGRA_INTEGER_EXT
:
252 * Get the bytes per pixel of pixel format type pair.
254 * \param format pixel format.
255 * \param type pixel type.
257 * \return bytes per pixel, or -1 if a bad format or type was given.
260 _mesa_bytes_per_pixel( GLenum format
, GLenum type
)
262 GLint comps
= _mesa_components_in_format( format
);
268 return 0; /* special case */
270 case GL_UNSIGNED_BYTE
:
271 return comps
* sizeof(GLubyte
);
273 case GL_UNSIGNED_SHORT
:
274 return comps
* sizeof(GLshort
);
276 case GL_UNSIGNED_INT
:
277 return comps
* sizeof(GLint
);
279 return comps
* sizeof(GLfloat
);
280 case GL_HALF_FLOAT_ARB
:
281 return comps
* sizeof(GLhalfARB
);
282 case GL_UNSIGNED_BYTE_3_3_2
:
283 case GL_UNSIGNED_BYTE_2_3_3_REV
:
284 if (format
== GL_RGB
|| format
== GL_BGR
||
285 format
== GL_RGB_INTEGER_EXT
|| format
== GL_BGR_INTEGER_EXT
)
286 return sizeof(GLubyte
);
288 return -1; /* error */
289 case GL_UNSIGNED_SHORT_5_6_5
:
290 case GL_UNSIGNED_SHORT_5_6_5_REV
:
291 if (format
== GL_RGB
|| format
== GL_BGR
||
292 format
== GL_RGB_INTEGER_EXT
|| format
== GL_BGR_INTEGER_EXT
)
293 return sizeof(GLushort
);
295 return -1; /* error */
296 case GL_UNSIGNED_SHORT_4_4_4_4
:
297 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
298 case GL_UNSIGNED_SHORT_5_5_5_1
:
299 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
300 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
||
301 format
== GL_RGBA_INTEGER_EXT
|| format
== GL_BGRA_INTEGER_EXT
)
302 return sizeof(GLushort
);
305 case GL_UNSIGNED_INT_8_8_8_8
:
306 case GL_UNSIGNED_INT_8_8_8_8_REV
:
307 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
||
308 format
== GL_RGBA_INTEGER_EXT
|| format
== GL_BGRA_INTEGER_EXT
)
309 return sizeof(GLuint
);
312 case GL_UNSIGNED_SHORT_8_8_MESA
:
313 case GL_UNSIGNED_SHORT_8_8_REV_MESA
:
314 if (format
== GL_YCBCR_MESA
)
315 return sizeof(GLushort
);
318 case GL_UNSIGNED_INT_5_9_9_9_REV
:
319 if (format
== GL_RGB
)
320 return sizeof(GLuint
);
323 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
324 if (format
== GL_RGB
)
325 return sizeof(GLuint
);
335 * Do error checking of format/type combinations for glReadPixels,
336 * glDrawPixels and glTex[Sub]Image. Note that depending on the format
337 * and type values, we may either generate GL_INVALID_OPERATION or
340 * \param format pixel format.
341 * \param type pixel type.
343 * \return GL_INVALID_ENUM, GL_INVALID_OPERATION or GL_NO_ERROR
346 _mesa_error_check_format_and_type(const struct gl_context
*ctx
,
347 GLenum format
, GLenum type
)
349 /* special type-based checks (see glReadPixels, glDrawPixels error lists) */
352 if (format
!= GL_COLOR_INDEX
&& format
!= GL_STENCIL_INDEX
) {
353 return GL_INVALID_ENUM
;
357 case GL_UNSIGNED_BYTE_3_3_2
:
358 case GL_UNSIGNED_BYTE_2_3_3_REV
:
359 case GL_UNSIGNED_SHORT_5_6_5
:
360 case GL_UNSIGNED_SHORT_5_6_5_REV
:
361 if (format
== GL_RGB
) {
364 return GL_INVALID_OPERATION
;
366 case GL_UNSIGNED_SHORT_4_4_4_4
:
367 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
368 case GL_UNSIGNED_SHORT_5_5_5_1
:
369 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
370 case GL_UNSIGNED_INT_8_8_8_8
:
371 case GL_UNSIGNED_INT_8_8_8_8_REV
:
372 if (format
== GL_RGBA
||
374 format
== GL_ABGR_EXT
) {
377 return GL_INVALID_OPERATION
;
383 /* now, for each format, check the type for compatibility */
386 case GL_STENCIL_INDEX
:
390 case GL_UNSIGNED_BYTE
:
392 case GL_UNSIGNED_SHORT
:
394 case GL_UNSIGNED_INT
:
398 return ctx
->Extensions
.ARB_half_float_pixel
399 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
401 return GL_INVALID_ENUM
;
408 #if 0 /* not legal! see table 3.6 of the 1.5 spec */
412 case GL_LUMINANCE_ALPHA
:
413 case GL_DEPTH_COMPONENT
:
416 case GL_UNSIGNED_BYTE
:
418 case GL_UNSIGNED_SHORT
:
420 case GL_UNSIGNED_INT
:
424 return ctx
->Extensions
.ARB_half_float_pixel
425 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
427 return GL_INVALID_ENUM
;
433 case GL_UNSIGNED_BYTE
:
435 case GL_UNSIGNED_SHORT
:
437 case GL_UNSIGNED_INT
:
439 case GL_UNSIGNED_BYTE_3_3_2
:
440 case GL_UNSIGNED_BYTE_2_3_3_REV
:
441 case GL_UNSIGNED_SHORT_5_6_5
:
442 case GL_UNSIGNED_SHORT_5_6_5_REV
:
445 return ctx
->Extensions
.ARB_half_float_pixel
446 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
448 return GL_INVALID_ENUM
;
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
:
465 return ctx
->Extensions
.ARB_half_float_pixel
466 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
468 return GL_INVALID_ENUM
;
476 case GL_UNSIGNED_BYTE
:
478 case GL_UNSIGNED_SHORT
:
480 case GL_UNSIGNED_INT
:
482 case GL_UNSIGNED_SHORT_4_4_4_4
:
483 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
484 case GL_UNSIGNED_SHORT_5_5_5_1
:
485 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
486 case GL_UNSIGNED_INT_8_8_8_8
:
487 case GL_UNSIGNED_INT_8_8_8_8_REV
:
490 return ctx
->Extensions
.ARB_half_float_pixel
491 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
493 return GL_INVALID_ENUM
;
497 if (!ctx
->Extensions
.MESA_ycbcr_texture
)
498 return GL_INVALID_ENUM
;
499 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
||
500 type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
)
503 return GL_INVALID_OPERATION
;
505 /* integer-valued formats */
506 case GL_RED_INTEGER_EXT
:
507 case GL_GREEN_INTEGER_EXT
:
508 case GL_BLUE_INTEGER_EXT
:
509 case GL_ALPHA_INTEGER_EXT
:
513 case GL_UNSIGNED_BYTE
:
515 case GL_UNSIGNED_SHORT
:
517 case GL_UNSIGNED_INT
:
518 return (ctx
->VersionMajor
>= 3 ||
519 ctx
->Extensions
.EXT_texture_integer
)
520 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
522 return GL_INVALID_ENUM
;
525 case GL_RGB_INTEGER_EXT
:
528 case GL_UNSIGNED_BYTE
:
530 case GL_UNSIGNED_SHORT
:
532 case GL_UNSIGNED_INT
:
533 return (ctx
->VersionMajor
>= 3 ||
534 ctx
->Extensions
.EXT_texture_integer
)
535 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
537 return GL_INVALID_ENUM
;
540 case GL_BGR_INTEGER_EXT
:
543 case GL_UNSIGNED_BYTE
:
545 case GL_UNSIGNED_SHORT
:
547 case GL_UNSIGNED_INT
:
548 /* NOTE: no packed formats w/ BGR format */
549 return (ctx
->VersionMajor
>= 3 ||
550 ctx
->Extensions
.EXT_texture_integer
)
551 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
553 return GL_INVALID_ENUM
;
556 case GL_RGBA_INTEGER_EXT
:
557 case GL_BGRA_INTEGER_EXT
:
560 case GL_UNSIGNED_BYTE
:
562 case GL_UNSIGNED_SHORT
:
564 case GL_UNSIGNED_INT
:
565 return (ctx
->VersionMajor
>= 3 ||
566 ctx
->Extensions
.EXT_texture_integer
)
567 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
569 return GL_INVALID_ENUM
;
572 case GL_LUMINANCE_INTEGER_EXT
:
573 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
576 case GL_UNSIGNED_BYTE
:
578 case GL_UNSIGNED_SHORT
:
580 case GL_UNSIGNED_INT
:
581 return ctx
->Extensions
.EXT_texture_integer
582 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
584 return GL_INVALID_ENUM
;
588 return GL_INVALID_ENUM
;
595 * Test if the given image format is a color/RGBA format (i.e., not color
596 * index, depth, stencil, etc).
597 * \param format the image format value (may by an internal texture format)
598 * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
601 _mesa_is_color_format(GLenum format
)
619 case GL_LUMINANCE_ALPHA
:
620 case GL_LUMINANCE4_ALPHA4
:
621 case GL_LUMINANCE6_ALPHA2
:
622 case GL_LUMINANCE8_ALPHA8
:
623 case GL_LUMINANCE12_ALPHA4
:
624 case GL_LUMINANCE12_ALPHA12
:
625 case GL_LUMINANCE16_ALPHA16
:
657 /* float texture formats */
658 case GL_ALPHA16F_ARB
:
659 case GL_ALPHA32F_ARB
:
660 case GL_LUMINANCE16F_ARB
:
661 case GL_LUMINANCE32F_ARB
:
662 case GL_LUMINANCE_ALPHA16F_ARB
:
663 case GL_LUMINANCE_ALPHA32F_ARB
:
664 case GL_INTENSITY16F_ARB
:
665 case GL_INTENSITY32F_ARB
:
674 /* generic integer formats */
675 case GL_RED_INTEGER_EXT
:
676 case GL_GREEN_INTEGER_EXT
:
677 case GL_BLUE_INTEGER_EXT
:
678 case GL_ALPHA_INTEGER_EXT
:
679 case GL_RGB_INTEGER_EXT
:
680 case GL_RGBA_INTEGER_EXT
:
681 case GL_BGR_INTEGER_EXT
:
682 case GL_BGRA_INTEGER_EXT
:
683 case GL_LUMINANCE_INTEGER_EXT
:
684 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
685 /* sized integer formats */
686 case GL_RGBA32UI_EXT
:
688 case GL_ALPHA32UI_EXT
:
689 case GL_INTENSITY32UI_EXT
:
690 case GL_LUMINANCE32UI_EXT
:
691 case GL_LUMINANCE_ALPHA32UI_EXT
:
692 case GL_RGBA16UI_EXT
:
694 case GL_ALPHA16UI_EXT
:
695 case GL_INTENSITY16UI_EXT
:
696 case GL_LUMINANCE16UI_EXT
:
697 case GL_LUMINANCE_ALPHA16UI_EXT
:
700 case GL_ALPHA8UI_EXT
:
701 case GL_INTENSITY8UI_EXT
:
702 case GL_LUMINANCE8UI_EXT
:
703 case GL_LUMINANCE_ALPHA8UI_EXT
:
706 case GL_ALPHA32I_EXT
:
707 case GL_INTENSITY32I_EXT
:
708 case GL_LUMINANCE32I_EXT
:
709 case GL_LUMINANCE_ALPHA32I_EXT
:
712 case GL_ALPHA16I_EXT
:
713 case GL_INTENSITY16I_EXT
:
714 case GL_LUMINANCE16I_EXT
:
715 case GL_LUMINANCE_ALPHA16I_EXT
:
719 case GL_INTENSITY8I_EXT
:
720 case GL_LUMINANCE8I_EXT
:
721 case GL_LUMINANCE_ALPHA8I_EXT
:
722 case GL_R11F_G11F_B10F
:
725 case GL_YCBCR_MESA
: /* not considered to be RGB */
734 * Test if the given image format is a depth component format.
737 _mesa_is_depth_format(GLenum format
)
740 case GL_DEPTH_COMPONENT
:
741 case GL_DEPTH_COMPONENT16
:
742 case GL_DEPTH_COMPONENT24
:
743 case GL_DEPTH_COMPONENT32
:
752 * Test if the given image format is a stencil format.
755 _mesa_is_stencil_format(GLenum format
)
758 case GL_STENCIL_INDEX
:
767 * Test if the given image format is a YCbCr format.
770 _mesa_is_ycbcr_format(GLenum format
)
782 * Test if the given image format is a depth or stencil format.
785 _mesa_is_depth_or_stencil_format(GLenum format
)
788 case GL_DEPTH_COMPONENT
:
789 case GL_DEPTH_COMPONENT16
:
790 case GL_DEPTH_COMPONENT24
:
791 case GL_DEPTH_COMPONENT32
:
792 case GL_STENCIL_INDEX
:
793 case GL_STENCIL_INDEX1_EXT
:
794 case GL_STENCIL_INDEX4_EXT
:
795 case GL_STENCIL_INDEX8_EXT
:
796 case GL_STENCIL_INDEX16_EXT
:
805 * Test if the given format is an integer (non-normalized) format.
808 _mesa_is_integer_format(GLenum format
)
811 /* generic integer formats */
812 case GL_RED_INTEGER_EXT
:
813 case GL_GREEN_INTEGER_EXT
:
814 case GL_BLUE_INTEGER_EXT
:
815 case GL_ALPHA_INTEGER_EXT
:
816 case GL_RGB_INTEGER_EXT
:
817 case GL_RGBA_INTEGER_EXT
:
818 case GL_BGR_INTEGER_EXT
:
819 case GL_BGRA_INTEGER_EXT
:
820 case GL_LUMINANCE_INTEGER_EXT
:
821 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
823 /* specific integer formats */
824 case GL_RGBA32UI_EXT
:
828 case GL_ALPHA32UI_EXT
:
829 case GL_INTENSITY32UI_EXT
:
830 case GL_LUMINANCE32UI_EXT
:
831 case GL_LUMINANCE_ALPHA32UI_EXT
:
832 case GL_RGBA16UI_EXT
:
836 case GL_ALPHA16UI_EXT
:
837 case GL_INTENSITY16UI_EXT
:
838 case GL_LUMINANCE16UI_EXT
:
839 case GL_LUMINANCE_ALPHA16UI_EXT
:
844 case GL_ALPHA8UI_EXT
:
845 case GL_INTENSITY8UI_EXT
:
846 case GL_LUMINANCE8UI_EXT
:
847 case GL_LUMINANCE_ALPHA8UI_EXT
:
852 case GL_ALPHA32I_EXT
:
853 case GL_INTENSITY32I_EXT
:
854 case GL_LUMINANCE32I_EXT
:
855 case GL_LUMINANCE_ALPHA32I_EXT
:
860 case GL_ALPHA16I_EXT
:
861 case GL_INTENSITY16I_EXT
:
862 case GL_LUMINANCE16I_EXT
:
863 case GL_LUMINANCE_ALPHA16I_EXT
:
869 case GL_INTENSITY8I_EXT
:
870 case GL_LUMINANCE8I_EXT
:
871 case GL_LUMINANCE_ALPHA8I_EXT
:
881 * Does the given base texture/renderbuffer format have the channel
885 _mesa_base_format_has_channel(GLenum base_format
, GLenum pname
)
888 case GL_TEXTURE_RED_SIZE
:
889 case GL_TEXTURE_RED_TYPE
:
890 case GL_RENDERBUFFER_RED_SIZE_EXT
:
891 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
892 if (base_format
== GL_RED
||
893 base_format
== GL_RG
||
894 base_format
== GL_RGB
||
895 base_format
== GL_RGBA
) {
899 case GL_TEXTURE_GREEN_SIZE
:
900 case GL_TEXTURE_GREEN_TYPE
:
901 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
902 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
903 if (base_format
== GL_RG
||
904 base_format
== GL_RGB
||
905 base_format
== GL_RGBA
) {
909 case GL_TEXTURE_BLUE_SIZE
:
910 case GL_TEXTURE_BLUE_TYPE
:
911 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
912 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
913 if (base_format
== GL_RGB
||
914 base_format
== GL_RGBA
) {
918 case GL_TEXTURE_ALPHA_SIZE
:
919 case GL_TEXTURE_ALPHA_TYPE
:
920 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
921 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
922 if (base_format
== GL_RGBA
||
923 base_format
== GL_ALPHA
||
924 base_format
== GL_LUMINANCE_ALPHA
) {
928 case GL_TEXTURE_LUMINANCE_SIZE
:
929 case GL_TEXTURE_LUMINANCE_TYPE
:
930 if (base_format
== GL_LUMINANCE
||
931 base_format
== GL_LUMINANCE_ALPHA
) {
935 case GL_TEXTURE_INTENSITY_SIZE
:
936 case GL_TEXTURE_INTENSITY_TYPE
:
937 if (base_format
== GL_INTENSITY
) {
941 case GL_TEXTURE_DEPTH_SIZE
:
942 case GL_TEXTURE_DEPTH_TYPE
:
943 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
944 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
945 if (base_format
== GL_DEPTH_COMPONENT
) {
949 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
950 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
951 if (base_format
== GL_STENCIL_INDEX
) {
956 _mesa_warning(NULL
, "%s: Unexpected channel token 0x%x\n",
957 __FUNCTION__
, pname
);
966 * Return the byte offset of a specific pixel in an image (1D, 2D or 3D).
968 * Pixel unpacking/packing parameters are observed according to \p packing.
970 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
971 * \param packing the pixelstore attributes
972 * \param width the image width
973 * \param height the image height
974 * \param format the pixel format (must be validated beforehand)
975 * \param type the pixel data type (must be validated beforehand)
976 * \param img which image in the volume (0 for 1D or 2D images)
977 * \param row row of pixel in the image (0 for 1D images)
978 * \param column column of pixel in the image
980 * \return offset of pixel.
982 * \sa gl_pixelstore_attrib.
985 _mesa_image_offset( GLuint dimensions
,
986 const struct gl_pixelstore_attrib
*packing
,
987 GLsizei width
, GLsizei height
,
988 GLenum format
, GLenum type
,
989 GLint img
, GLint row
, GLint column
)
991 GLint alignment
; /* 1, 2 or 4 */
992 GLint pixels_per_row
;
993 GLint rows_per_image
;
996 GLint skipimages
; /* for 3-D volume images */
999 ASSERT(dimensions
>= 1 && dimensions
<= 3);
1001 alignment
= packing
->Alignment
;
1002 if (packing
->RowLength
> 0) {
1003 pixels_per_row
= packing
->RowLength
;
1006 pixels_per_row
= width
;
1008 if (packing
->ImageHeight
> 0) {
1009 rows_per_image
= packing
->ImageHeight
;
1012 rows_per_image
= height
;
1015 skippixels
= packing
->SkipPixels
;
1016 /* Note: SKIP_ROWS _is_ used for 1D images */
1017 skiprows
= packing
->SkipRows
;
1018 /* Note: SKIP_IMAGES is only used for 3D images */
1019 skipimages
= (dimensions
== 3) ? packing
->SkipImages
: 0;
1021 if (type
== GL_BITMAP
) {
1023 GLint bytes_per_row
;
1024 GLint bytes_per_image
;
1025 /* components per pixel for color or stencil index: */
1026 const GLint comp_per_pixel
= 1;
1028 /* The pixel type and format should have been error checked earlier */
1029 assert(format
== GL_COLOR_INDEX
|| format
== GL_STENCIL_INDEX
);
1031 bytes_per_row
= alignment
1032 * CEILING( comp_per_pixel
*pixels_per_row
, 8*alignment
);
1034 bytes_per_image
= bytes_per_row
* rows_per_image
;
1036 offset
= (skipimages
+ img
) * bytes_per_image
1037 + (skiprows
+ row
) * bytes_per_row
1038 + (skippixels
+ column
) / 8;
1041 /* Non-BITMAP data */
1042 GLint bytes_per_pixel
, bytes_per_row
, remainder
, bytes_per_image
;
1045 bytes_per_pixel
= _mesa_bytes_per_pixel( format
, type
);
1047 /* The pixel type and format should have been error checked earlier */
1048 assert(bytes_per_pixel
> 0);
1050 bytes_per_row
= pixels_per_row
* bytes_per_pixel
;
1051 remainder
= bytes_per_row
% alignment
;
1053 bytes_per_row
+= (alignment
- remainder
);
1055 ASSERT(bytes_per_row
% alignment
== 0);
1057 bytes_per_image
= bytes_per_row
* rows_per_image
;
1059 if (packing
->Invert
) {
1060 /* set pixel_addr to the last row */
1061 topOfImage
= bytes_per_row
* (height
- 1);
1062 bytes_per_row
= -bytes_per_row
;
1068 /* compute final pixel address */
1069 offset
= (skipimages
+ img
) * bytes_per_image
1071 + (skiprows
+ row
) * bytes_per_row
1072 + (skippixels
+ column
) * bytes_per_pixel
;
1080 * Return the address of a specific pixel in an image (1D, 2D or 3D).
1082 * Pixel unpacking/packing parameters are observed according to \p packing.
1084 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
1085 * \param packing the pixelstore attributes
1086 * \param image starting address of image data
1087 * \param width the image width
1088 * \param height the image height
1089 * \param format the pixel format (must be validated beforehand)
1090 * \param type the pixel data type (must be validated beforehand)
1091 * \param img which image in the volume (0 for 1D or 2D images)
1092 * \param row row of pixel in the image (0 for 1D images)
1093 * \param column column of pixel in the image
1095 * \return address of pixel.
1097 * \sa gl_pixelstore_attrib.
1100 _mesa_image_address( GLuint dimensions
,
1101 const struct gl_pixelstore_attrib
*packing
,
1102 const GLvoid
*image
,
1103 GLsizei width
, GLsizei height
,
1104 GLenum format
, GLenum type
,
1105 GLint img
, GLint row
, GLint column
)
1107 const GLubyte
*addr
= (const GLubyte
*) image
;
1109 addr
+= _mesa_image_offset(dimensions
, packing
, width
, height
,
1110 format
, type
, img
, row
, column
);
1112 return (GLvoid
*) addr
;
1117 _mesa_image_address1d( const struct gl_pixelstore_attrib
*packing
,
1118 const GLvoid
*image
,
1120 GLenum format
, GLenum type
,
1123 return _mesa_image_address(1, packing
, image
, width
, 1,
1124 format
, type
, 0, 0, column
);
1129 _mesa_image_address2d( const struct gl_pixelstore_attrib
*packing
,
1130 const GLvoid
*image
,
1131 GLsizei width
, GLsizei height
,
1132 GLenum format
, GLenum type
,
1133 GLint row
, GLint column
)
1135 return _mesa_image_address(2, packing
, image
, width
, height
,
1136 format
, type
, 0, row
, column
);
1141 _mesa_image_address3d( const struct gl_pixelstore_attrib
*packing
,
1142 const GLvoid
*image
,
1143 GLsizei width
, GLsizei height
,
1144 GLenum format
, GLenum type
,
1145 GLint img
, GLint row
, GLint column
)
1147 return _mesa_image_address(3, packing
, image
, width
, height
,
1148 format
, type
, img
, row
, column
);
1154 * Compute the stride (in bytes) between image rows.
1156 * \param packing the pixelstore attributes
1157 * \param width image width.
1158 * \param format pixel format.
1159 * \param type pixel data type.
1161 * \return the stride in bytes for the given parameters, or -1 if error
1164 _mesa_image_row_stride( const struct gl_pixelstore_attrib
*packing
,
1165 GLint width
, GLenum format
, GLenum type
)
1167 GLint bytesPerRow
, remainder
;
1171 if (type
== GL_BITMAP
) {
1172 if (packing
->RowLength
== 0) {
1173 bytesPerRow
= (width
+ 7) / 8;
1176 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
1180 /* Non-BITMAP data */
1181 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1182 if (bytesPerPixel
<= 0)
1183 return -1; /* error */
1184 if (packing
->RowLength
== 0) {
1185 bytesPerRow
= bytesPerPixel
* width
;
1188 bytesPerRow
= bytesPerPixel
* packing
->RowLength
;
1192 remainder
= bytesPerRow
% packing
->Alignment
;
1193 if (remainder
> 0) {
1194 bytesPerRow
+= (packing
->Alignment
- remainder
);
1197 if (packing
->Invert
) {
1198 /* negate the bytes per row (negative row stride) */
1199 bytesPerRow
= -bytesPerRow
;
1207 * Compute the stride between images in a 3D texture (in bytes) for the given
1208 * pixel packing parameters and image width, format and type.
1211 _mesa_image_image_stride( const struct gl_pixelstore_attrib
*packing
,
1212 GLint width
, GLint height
,
1213 GLenum format
, GLenum type
)
1215 GLint bytesPerRow
, bytesPerImage
, remainder
;
1219 if (type
== GL_BITMAP
) {
1220 if (packing
->RowLength
== 0) {
1221 bytesPerRow
= (width
+ 7) / 8;
1224 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
1228 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1230 if (bytesPerPixel
<= 0)
1231 return -1; /* error */
1232 if (packing
->RowLength
== 0) {
1233 bytesPerRow
= bytesPerPixel
* width
;
1236 bytesPerRow
= bytesPerPixel
* packing
->RowLength
;
1240 remainder
= bytesPerRow
% packing
->Alignment
;
1242 bytesPerRow
+= (packing
->Alignment
- remainder
);
1244 if (packing
->ImageHeight
== 0)
1245 bytesPerImage
= bytesPerRow
* height
;
1247 bytesPerImage
= bytesPerRow
* packing
->ImageHeight
;
1249 return bytesPerImage
;
1255 * "Expand" a bitmap from 1-bit per pixel to 8-bits per pixel.
1256 * This is typically used to convert a bitmap into a GLubyte/pixel texture.
1257 * "On" bits will set texels to \p onValue.
1258 * "Off" bits will not modify texels.
1259 * \param width src bitmap width in pixels
1260 * \param height src bitmap height in pixels
1261 * \param unpack bitmap unpacking state
1262 * \param bitmap the src bitmap data
1263 * \param destBuffer start of dest buffer
1264 * \param destStride row stride in dest buffer
1265 * \param onValue if bit is 1, set destBuffer pixel to this value
1268 _mesa_expand_bitmap(GLsizei width
, GLsizei height
,
1269 const struct gl_pixelstore_attrib
*unpack
,
1270 const GLubyte
*bitmap
,
1271 GLubyte
*destBuffer
, GLint destStride
,
1274 const GLubyte
*srcRow
= (const GLubyte
*)
1275 _mesa_image_address2d(unpack
, bitmap
, width
, height
,
1276 GL_COLOR_INDEX
, GL_BITMAP
, 0, 0);
1277 const GLint srcStride
= _mesa_image_row_stride(unpack
, width
,
1278 GL_COLOR_INDEX
, GL_BITMAP
);
1281 #define SET_PIXEL(COL, ROW) \
1282 destBuffer[(ROW) * destStride + (COL)] = onValue;
1284 for (row
= 0; row
< height
; row
++) {
1285 const GLubyte
*src
= srcRow
;
1287 if (unpack
->LsbFirst
) {
1289 GLubyte mask
= 1U << (unpack
->SkipPixels
& 0x7);
1290 for (col
= 0; col
< width
; col
++) {
1293 SET_PIXEL(col
, row
);
1305 /* get ready for next row */
1311 GLubyte mask
= 128U >> (unpack
->SkipPixels
& 0x7);
1312 for (col
= 0; col
< width
; col
++) {
1315 SET_PIXEL(col
, row
);
1327 /* get ready for next row */
1332 srcRow
+= srcStride
;
1342 * Convert an array of RGBA colors from one datatype to another.
1343 * NOTE: src may equal dst. In that case, we use a temporary buffer.
1346 _mesa_convert_colors(GLenum srcType
, const GLvoid
*src
,
1347 GLenum dstType
, GLvoid
*dst
,
1348 GLuint count
, const GLubyte mask
[])
1351 const GLboolean useTemp
= (src
== dst
);
1353 tempBuffer
= malloc(count
* MAX_PIXEL_BYTES
);
1357 ASSERT(srcType
!= dstType
);
1360 case GL_UNSIGNED_BYTE
:
1361 if (dstType
== GL_UNSIGNED_SHORT
) {
1362 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
1363 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
1365 for (i
= 0; i
< count
; i
++) {
1366 if (!mask
|| mask
[i
]) {
1367 dst2
[i
][RCOMP
] = UBYTE_TO_USHORT(src1
[i
][RCOMP
]);
1368 dst2
[i
][GCOMP
] = UBYTE_TO_USHORT(src1
[i
][GCOMP
]);
1369 dst2
[i
][BCOMP
] = UBYTE_TO_USHORT(src1
[i
][BCOMP
]);
1370 dst2
[i
][ACOMP
] = UBYTE_TO_USHORT(src1
[i
][ACOMP
]);
1374 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
1377 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
1378 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
1380 ASSERT(dstType
== GL_FLOAT
);
1381 for (i
= 0; i
< count
; i
++) {
1382 if (!mask
|| mask
[i
]) {
1383 dst4
[i
][RCOMP
] = UBYTE_TO_FLOAT(src1
[i
][RCOMP
]);
1384 dst4
[i
][GCOMP
] = UBYTE_TO_FLOAT(src1
[i
][GCOMP
]);
1385 dst4
[i
][BCOMP
] = UBYTE_TO_FLOAT(src1
[i
][BCOMP
]);
1386 dst4
[i
][ACOMP
] = UBYTE_TO_FLOAT(src1
[i
][ACOMP
]);
1390 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
1393 case GL_UNSIGNED_SHORT
:
1394 if (dstType
== GL_UNSIGNED_BYTE
) {
1395 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
1396 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
1398 for (i
= 0; i
< count
; i
++) {
1399 if (!mask
|| mask
[i
]) {
1400 dst1
[i
][RCOMP
] = USHORT_TO_UBYTE(src2
[i
][RCOMP
]);
1401 dst1
[i
][GCOMP
] = USHORT_TO_UBYTE(src2
[i
][GCOMP
]);
1402 dst1
[i
][BCOMP
] = USHORT_TO_UBYTE(src2
[i
][BCOMP
]);
1403 dst1
[i
][ACOMP
] = USHORT_TO_UBYTE(src2
[i
][ACOMP
]);
1407 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
1410 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
1411 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
1413 ASSERT(dstType
== GL_FLOAT
);
1414 for (i
= 0; i
< count
; i
++) {
1415 if (!mask
|| mask
[i
]) {
1416 dst4
[i
][RCOMP
] = USHORT_TO_FLOAT(src2
[i
][RCOMP
]);
1417 dst4
[i
][GCOMP
] = USHORT_TO_FLOAT(src2
[i
][GCOMP
]);
1418 dst4
[i
][BCOMP
] = USHORT_TO_FLOAT(src2
[i
][BCOMP
]);
1419 dst4
[i
][ACOMP
] = USHORT_TO_FLOAT(src2
[i
][ACOMP
]);
1423 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
1427 if (dstType
== GL_UNSIGNED_BYTE
) {
1428 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
1429 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
1431 for (i
= 0; i
< count
; i
++) {
1432 if (!mask
|| mask
[i
])
1433 _mesa_unclamped_float_rgba_to_ubyte(dst1
[i
], src4
[i
]);
1436 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
1439 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
1440 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
1442 ASSERT(dstType
== GL_UNSIGNED_SHORT
);
1443 for (i
= 0; i
< count
; i
++) {
1444 if (!mask
|| mask
[i
]) {
1445 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][RCOMP
], src4
[i
][RCOMP
]);
1446 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][GCOMP
], src4
[i
][GCOMP
]);
1447 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][BCOMP
], src4
[i
][BCOMP
]);
1448 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][ACOMP
], src4
[i
][ACOMP
]);
1452 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
1456 _mesa_problem(NULL
, "Invalid datatype in _mesa_convert_colors");
1466 * Perform basic clipping for glDrawPixels. The image's position and size
1467 * and the unpack SkipPixels and SkipRows are adjusted so that the image
1468 * region is entirely within the window and scissor bounds.
1469 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
1470 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
1471 * we'll actually write. Beforehand, *destY-1 is the first drawing row.
1473 * \return GL_TRUE if image is ready for drawing or
1474 * GL_FALSE if image was completely clipped away (draw nothing)
1477 _mesa_clip_drawpixels(const struct gl_context
*ctx
,
1478 GLint
*destX
, GLint
*destY
,
1479 GLsizei
*width
, GLsizei
*height
,
1480 struct gl_pixelstore_attrib
*unpack
)
1482 const struct gl_framebuffer
*buffer
= ctx
->DrawBuffer
;
1484 if (unpack
->RowLength
== 0) {
1485 unpack
->RowLength
= *width
;
1488 ASSERT(ctx
->Pixel
.ZoomX
== 1.0F
);
1489 ASSERT(ctx
->Pixel
.ZoomY
== 1.0F
|| ctx
->Pixel
.ZoomY
== -1.0F
);
1492 if (*destX
< buffer
->_Xmin
) {
1493 unpack
->SkipPixels
+= (buffer
->_Xmin
- *destX
);
1494 *width
-= (buffer
->_Xmin
- *destX
);
1495 *destX
= buffer
->_Xmin
;
1497 /* right clipping */
1498 if (*destX
+ *width
> buffer
->_Xmax
)
1499 *width
-= (*destX
+ *width
- buffer
->_Xmax
);
1504 if (ctx
->Pixel
.ZoomY
== 1.0F
) {
1505 /* bottom clipping */
1506 if (*destY
< buffer
->_Ymin
) {
1507 unpack
->SkipRows
+= (buffer
->_Ymin
- *destY
);
1508 *height
-= (buffer
->_Ymin
- *destY
);
1509 *destY
= buffer
->_Ymin
;
1512 if (*destY
+ *height
> buffer
->_Ymax
)
1513 *height
-= (*destY
+ *height
- buffer
->_Ymax
);
1515 else { /* upside down */
1517 if (*destY
> buffer
->_Ymax
) {
1518 unpack
->SkipRows
+= (*destY
- buffer
->_Ymax
);
1519 *height
-= (*destY
- buffer
->_Ymax
);
1520 *destY
= buffer
->_Ymax
;
1522 /* bottom clipping */
1523 if (*destY
- *height
< buffer
->_Ymin
)
1524 *height
-= (buffer
->_Ymin
- (*destY
- *height
));
1525 /* adjust destY so it's the first row to write to */
1537 * Perform clipping for glReadPixels. The image's window position
1538 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
1539 * so that the image region is entirely within the window bounds.
1540 * Note: this is different from _mesa_clip_drawpixels() in that the
1541 * scissor box is ignored, and we use the bounds of the current readbuffer
1544 * \return GL_TRUE if region to read is in bounds
1545 * GL_FALSE if region is completely out of bounds (nothing to read)
1548 _mesa_clip_readpixels(const struct gl_context
*ctx
,
1549 GLint
*srcX
, GLint
*srcY
,
1550 GLsizei
*width
, GLsizei
*height
,
1551 struct gl_pixelstore_attrib
*pack
)
1553 const struct gl_framebuffer
*buffer
= ctx
->ReadBuffer
;
1555 if (pack
->RowLength
== 0) {
1556 pack
->RowLength
= *width
;
1561 pack
->SkipPixels
+= (0 - *srcX
);
1562 *width
-= (0 - *srcX
);
1565 /* right clipping */
1566 if (*srcX
+ *width
> (GLsizei
) buffer
->Width
)
1567 *width
-= (*srcX
+ *width
- buffer
->Width
);
1572 /* bottom clipping */
1574 pack
->SkipRows
+= (0 - *srcY
);
1575 *height
-= (0 - *srcY
);
1579 if (*srcY
+ *height
> (GLsizei
) buffer
->Height
)
1580 *height
-= (*srcY
+ *height
- buffer
->Height
);
1590 * Do clipping for a glCopyTexSubImage call.
1591 * The framebuffer source region might extend outside the framebuffer
1592 * bounds. Clip the source region against the framebuffer bounds and
1593 * adjust the texture/dest position and size accordingly.
1595 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
1598 _mesa_clip_copytexsubimage(const struct gl_context
*ctx
,
1599 GLint
*destX
, GLint
*destY
,
1600 GLint
*srcX
, GLint
*srcY
,
1601 GLsizei
*width
, GLsizei
*height
)
1603 const struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1604 const GLint srcX0
= *srcX
, srcY0
= *srcY
;
1606 if (_mesa_clip_to_region(0, 0, fb
->Width
, fb
->Height
,
1607 srcX
, srcY
, width
, height
)) {
1608 *destX
= *destX
+ *srcX
- srcX0
;
1609 *destY
= *destY
+ *srcY
- srcY0
;
1621 * Clip the rectangle defined by (x, y, width, height) against the bounds
1622 * specified by [xmin, xmax) and [ymin, ymax).
1623 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
1626 _mesa_clip_to_region(GLint xmin
, GLint ymin
,
1627 GLint xmax
, GLint ymax
,
1629 GLsizei
*width
, GLsizei
*height
)
1633 *width
-= (xmin
- *x
);
1637 /* right clipping */
1638 if (*x
+ *width
> xmax
)
1639 *width
-= (*x
+ *width
- xmax
);
1644 /* bottom (or top) clipping */
1646 *height
-= (ymin
- *y
);
1650 /* top (or bottom) clipping */
1651 if (*y
+ *height
> ymax
)
1652 *height
-= (*y
+ *height
- ymax
);
1662 * Clip dst coords against Xmax (or Ymax).
1665 clip_right_or_top(GLint
*srcX0
, GLint
*srcX1
,
1666 GLint
*dstX0
, GLint
*dstX1
,
1671 if (*dstX1
> maxValue
) {
1672 /* X1 outside right edge */
1673 ASSERT(*dstX0
< maxValue
); /* X0 should be inside right edge */
1674 t
= (GLfloat
) (maxValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
1675 /* chop off [t, 1] part */
1676 ASSERT(t
>= 0.0 && t
<= 1.0);
1678 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
1679 *srcX1
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
1681 else if (*dstX0
> maxValue
) {
1682 /* X0 outside right edge */
1683 ASSERT(*dstX1
< maxValue
); /* X1 should be inside right edge */
1684 t
= (GLfloat
) (maxValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
1685 /* chop off [t, 1] part */
1686 ASSERT(t
>= 0.0 && t
<= 1.0);
1688 bias
= (*srcX0
< *srcX1
) ? -0.5F
: 0.5F
;
1689 *srcX0
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
1695 * Clip dst coords against Xmin (or Ymin).
1698 clip_left_or_bottom(GLint
*srcX0
, GLint
*srcX1
,
1699 GLint
*dstX0
, GLint
*dstX1
,
1704 if (*dstX0
< minValue
) {
1705 /* X0 outside left edge */
1706 ASSERT(*dstX1
> minValue
); /* X1 should be inside left edge */
1707 t
= (GLfloat
) (minValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
1708 /* chop off [0, t] part */
1709 ASSERT(t
>= 0.0 && t
<= 1.0);
1711 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
; /* flipped??? */
1712 *srcX0
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
1714 else if (*dstX1
< minValue
) {
1715 /* X1 outside left edge */
1716 ASSERT(*dstX0
> minValue
); /* X0 should be inside left edge */
1717 t
= (GLfloat
) (minValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
1718 /* chop off [0, t] part */
1719 ASSERT(t
>= 0.0 && t
<= 1.0);
1721 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
1722 *srcX1
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
1728 * Do clipping of blit src/dest rectangles.
1729 * The dest rect is clipped against both the buffer bounds and scissor bounds.
1730 * The src rect is just clipped against the buffer bounds.
1732 * When either the src or dest rect is clipped, the other is also clipped
1735 * Note that X0 need not be less than X1 (same for Y) for either the source
1736 * and dest rects. That makes the clipping a little trickier.
1738 * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped
1741 _mesa_clip_blit(struct gl_context
*ctx
,
1742 GLint
*srcX0
, GLint
*srcY0
, GLint
*srcX1
, GLint
*srcY1
,
1743 GLint
*dstX0
, GLint
*dstY0
, GLint
*dstX1
, GLint
*dstY1
)
1745 const GLint srcXmin
= 0;
1746 const GLint srcXmax
= ctx
->ReadBuffer
->Width
;
1747 const GLint srcYmin
= 0;
1748 const GLint srcYmax
= ctx
->ReadBuffer
->Height
;
1750 /* these include scissor bounds */
1751 const GLint dstXmin
= ctx
->DrawBuffer
->_Xmin
;
1752 const GLint dstXmax
= ctx
->DrawBuffer
->_Xmax
;
1753 const GLint dstYmin
= ctx
->DrawBuffer
->_Ymin
;
1754 const GLint dstYmax
= ctx
->DrawBuffer
->_Ymax
;
1757 printf("PreClipX: src: %d .. %d dst: %d .. %d\n",
1758 *srcX0, *srcX1, *dstX0, *dstX1);
1759 printf("PreClipY: src: %d .. %d dst: %d .. %d\n",
1760 *srcY0, *srcY1, *dstY0, *dstY1);
1763 /* trivial rejection tests */
1764 if (*dstX0
== *dstX1
)
1765 return GL_FALSE
; /* no width */
1766 if (*dstX0
<= dstXmin
&& *dstX1
<= dstXmin
)
1767 return GL_FALSE
; /* totally out (left) of bounds */
1768 if (*dstX0
>= dstXmax
&& *dstX1
>= dstXmax
)
1769 return GL_FALSE
; /* totally out (right) of bounds */
1771 if (*dstY0
== *dstY1
)
1773 if (*dstY0
<= dstYmin
&& *dstY1
<= dstYmin
)
1775 if (*dstY0
>= dstYmax
&& *dstY1
>= dstYmax
)
1778 if (*srcX0
== *srcX1
)
1780 if (*srcX0
<= srcXmin
&& *srcX1
<= srcXmin
)
1782 if (*srcX0
>= srcXmax
&& *srcX1
>= srcXmax
)
1785 if (*srcY0
== *srcY1
)
1787 if (*srcY0
<= srcYmin
&& *srcY1
<= srcYmin
)
1789 if (*srcY0
>= srcYmax
&& *srcY1
>= srcYmax
)
1795 clip_right_or_top(srcX0
, srcX1
, dstX0
, dstX1
, dstXmax
);
1796 clip_right_or_top(srcY0
, srcY1
, dstY0
, dstY1
, dstYmax
);
1797 clip_left_or_bottom(srcX0
, srcX1
, dstX0
, dstX1
, dstXmin
);
1798 clip_left_or_bottom(srcY0
, srcY1
, dstY0
, dstY1
, dstYmin
);
1801 * src clip (just swap src/dst values from above)
1803 clip_right_or_top(dstX0
, dstX1
, srcX0
, srcX1
, srcXmax
);
1804 clip_right_or_top(dstY0
, dstY1
, srcY0
, srcY1
, srcYmax
);
1805 clip_left_or_bottom(dstX0
, dstX1
, srcX0
, srcX1
, srcXmin
);
1806 clip_left_or_bottom(dstY0
, dstY1
, srcY0
, srcY1
, srcYmin
);
1809 printf("PostClipX: src: %d .. %d dst: %d .. %d\n",
1810 *srcX0, *srcX1, *dstX0, *dstX1);
1811 printf("PostClipY: src: %d .. %d dst: %d .. %d\n",
1812 *srcY0, *srcY1, *dstY0, *dstY1);
1815 ASSERT(*dstX0
>= dstXmin
);
1816 ASSERT(*dstX0
<= dstXmax
);
1817 ASSERT(*dstX1
>= dstXmin
);
1818 ASSERT(*dstX1
<= dstXmax
);
1820 ASSERT(*dstY0
>= dstYmin
);
1821 ASSERT(*dstY0
<= dstYmax
);
1822 ASSERT(*dstY1
>= dstYmin
);
1823 ASSERT(*dstY1
<= dstYmax
);
1825 ASSERT(*srcX0
>= srcXmin
);
1826 ASSERT(*srcX0
<= srcXmax
);
1827 ASSERT(*srcX1
>= srcXmin
);
1828 ASSERT(*srcX1
<= srcXmax
);
1830 ASSERT(*srcY0
>= srcYmin
);
1831 ASSERT(*srcY0
<= srcYmax
);
1832 ASSERT(*srcY1
>= srcYmin
);
1833 ASSERT(*srcY1
<= srcYmax
);