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.
34 * \return GL_TRUE if type is packed pixel type, GL_FALSE otherwise.
37 _mesa_type_is_packed(GLenum type
)
40 case GL_UNSIGNED_BYTE_3_3_2
:
41 case GL_UNSIGNED_BYTE_2_3_3_REV
:
42 case MESA_UNSIGNED_BYTE_4_4
:
43 case GL_UNSIGNED_SHORT_5_6_5
:
44 case GL_UNSIGNED_SHORT_5_6_5_REV
:
45 case GL_UNSIGNED_SHORT_4_4_4_4
:
46 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
47 case GL_UNSIGNED_SHORT_5_5_5_1
:
48 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
49 case GL_UNSIGNED_INT_8_8_8_8
:
50 case GL_UNSIGNED_INT_8_8_8_8_REV
:
51 case GL_UNSIGNED_SHORT_8_8_MESA
:
52 case GL_UNSIGNED_SHORT_8_8_REV_MESA
:
53 case GL_UNSIGNED_INT_5_9_9_9_REV
:
54 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
64 * Flip the order of the 2 bytes in each word in the given array.
67 * \param n number of words.
70 _mesa_swap2( GLushort
*p
, GLuint n
)
73 for (i
= 0; i
< n
; i
++) {
74 p
[i
] = (p
[i
] >> 8) | ((p
[i
] << 8) & 0xff00);
81 * Flip the order of the 4 bytes in each word in the given array.
84 _mesa_swap4( GLuint
*p
, GLuint n
)
87 for (i
= 0; i
< n
; i
++) {
91 | ((b
<< 8) & 0xff0000)
92 | ((b
<< 24) & 0xff000000);
99 * Get the size of a GL data type.
101 * \param type GL data type.
103 * \return the size, in bytes, of the given data type, 0 if a GL_BITMAP, or -1
104 * if an invalid type enum.
107 _mesa_sizeof_type( GLenum type
)
112 case GL_UNSIGNED_BYTE
:
113 return sizeof(GLubyte
);
115 return sizeof(GLbyte
);
116 case GL_UNSIGNED_SHORT
:
117 return sizeof(GLushort
);
119 return sizeof(GLshort
);
120 case GL_UNSIGNED_INT
:
121 return sizeof(GLuint
);
123 return sizeof(GLint
);
125 return sizeof(GLfloat
);
127 return sizeof(GLdouble
);
128 case GL_HALF_FLOAT_ARB
:
129 return sizeof(GLhalfARB
);
131 return sizeof(GLfixed
);
139 * Same as _mesa_sizeof_type() but also accepting the packed pixel
143 _mesa_sizeof_packed_type( GLenum type
)
148 case GL_UNSIGNED_BYTE
:
149 return sizeof(GLubyte
);
151 return sizeof(GLbyte
);
152 case GL_UNSIGNED_SHORT
:
153 return sizeof(GLushort
);
155 return sizeof(GLshort
);
156 case GL_UNSIGNED_INT
:
157 return sizeof(GLuint
);
159 return sizeof(GLint
);
160 case GL_HALF_FLOAT_ARB
:
161 return sizeof(GLhalfARB
);
163 return sizeof(GLfloat
);
164 case GL_UNSIGNED_BYTE_3_3_2
:
165 case GL_UNSIGNED_BYTE_2_3_3_REV
:
166 case MESA_UNSIGNED_BYTE_4_4
:
167 return sizeof(GLubyte
);
168 case GL_UNSIGNED_SHORT_5_6_5
:
169 case GL_UNSIGNED_SHORT_5_6_5_REV
:
170 case GL_UNSIGNED_SHORT_4_4_4_4
:
171 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
172 case GL_UNSIGNED_SHORT_5_5_5_1
:
173 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
174 case GL_UNSIGNED_SHORT_8_8_MESA
:
175 case GL_UNSIGNED_SHORT_8_8_REV_MESA
:
176 return sizeof(GLushort
);
177 case GL_UNSIGNED_INT_8_8_8_8
:
178 case GL_UNSIGNED_INT_8_8_8_8_REV
:
179 case GL_UNSIGNED_INT_5_9_9_9_REV
:
180 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
181 return sizeof(GLuint
);
189 * Get the number of components in a pixel format.
191 * \param format pixel format.
193 * \return the number of components in the given format, or -1 if a bad format.
196 _mesa_components_in_format( GLenum format
)
200 case GL_STENCIL_INDEX
:
201 case GL_DEPTH_COMPONENT
:
203 case GL_RED_INTEGER_EXT
:
205 case GL_GREEN_INTEGER_EXT
:
207 case GL_BLUE_INTEGER_EXT
:
209 case GL_ALPHA_INTEGER_EXT
:
211 case GL_LUMINANCE_INTEGER_EXT
:
215 case GL_LUMINANCE_ALPHA
:
216 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
224 case GL_RGB_INTEGER_EXT
:
225 case GL_BGR_INTEGER_EXT
:
231 case GL_RGBA_INTEGER_EXT
:
232 case GL_BGRA_INTEGER_EXT
:
242 * Get the bytes per pixel of pixel format type pair.
244 * \param format pixel format.
245 * \param type pixel type.
247 * \return bytes per pixel, or -1 if a bad format or type was given.
250 _mesa_bytes_per_pixel( GLenum format
, GLenum type
)
252 GLint comps
= _mesa_components_in_format( format
);
258 return 0; /* special case */
260 case GL_UNSIGNED_BYTE
:
261 return comps
* sizeof(GLubyte
);
263 case GL_UNSIGNED_SHORT
:
264 return comps
* sizeof(GLshort
);
266 case GL_UNSIGNED_INT
:
267 return comps
* sizeof(GLint
);
269 return comps
* sizeof(GLfloat
);
270 case GL_HALF_FLOAT_ARB
:
271 return comps
* sizeof(GLhalfARB
);
272 case GL_UNSIGNED_BYTE_3_3_2
:
273 case GL_UNSIGNED_BYTE_2_3_3_REV
:
274 if (format
== GL_RGB
|| format
== GL_BGR
||
275 format
== GL_RGB_INTEGER_EXT
|| format
== GL_BGR_INTEGER_EXT
)
276 return sizeof(GLubyte
);
278 return -1; /* error */
279 case GL_UNSIGNED_SHORT_5_6_5
:
280 case GL_UNSIGNED_SHORT_5_6_5_REV
:
281 if (format
== GL_RGB
|| format
== GL_BGR
||
282 format
== GL_RGB_INTEGER_EXT
|| format
== GL_BGR_INTEGER_EXT
)
283 return sizeof(GLushort
);
285 return -1; /* error */
286 case GL_UNSIGNED_SHORT_4_4_4_4
:
287 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
288 case GL_UNSIGNED_SHORT_5_5_5_1
:
289 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
290 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
||
291 format
== GL_RGBA_INTEGER_EXT
|| format
== GL_BGRA_INTEGER_EXT
)
292 return sizeof(GLushort
);
295 case GL_UNSIGNED_INT_8_8_8_8
:
296 case GL_UNSIGNED_INT_8_8_8_8_REV
:
297 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
||
298 format
== GL_RGBA_INTEGER_EXT
|| format
== GL_BGRA_INTEGER_EXT
)
299 return sizeof(GLuint
);
302 case GL_UNSIGNED_SHORT_8_8_MESA
:
303 case GL_UNSIGNED_SHORT_8_8_REV_MESA
:
304 if (format
== GL_YCBCR_MESA
)
305 return sizeof(GLushort
);
308 case GL_UNSIGNED_INT_5_9_9_9_REV
:
309 if (format
== GL_RGB
)
310 return sizeof(GLuint
);
313 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
314 if (format
== GL_RGB
)
315 return sizeof(GLuint
);
325 * Do error checking of format/type combinations for glReadPixels,
326 * glDrawPixels and glTex[Sub]Image. Note that depending on the format
327 * and type values, we may either generate GL_INVALID_OPERATION or
330 * \param format pixel format.
331 * \param type pixel type.
333 * \return GL_INVALID_ENUM, GL_INVALID_OPERATION or GL_NO_ERROR
336 _mesa_error_check_format_and_type(const struct gl_context
*ctx
,
337 GLenum format
, GLenum type
)
339 /* special type-based checks (see glReadPixels, glDrawPixels error lists) */
342 if (format
!= GL_COLOR_INDEX
&& format
!= GL_STENCIL_INDEX
) {
343 return GL_INVALID_ENUM
;
347 case GL_UNSIGNED_BYTE_3_3_2
:
348 case GL_UNSIGNED_BYTE_2_3_3_REV
:
349 case GL_UNSIGNED_SHORT_5_6_5
:
350 case GL_UNSIGNED_SHORT_5_6_5_REV
:
351 if (format
== GL_RGB
) {
354 return GL_INVALID_OPERATION
;
356 case GL_UNSIGNED_SHORT_4_4_4_4
:
357 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
358 case GL_UNSIGNED_SHORT_5_5_5_1
:
359 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
360 case GL_UNSIGNED_INT_8_8_8_8
:
361 case GL_UNSIGNED_INT_8_8_8_8_REV
:
362 if (format
== GL_RGBA
||
364 format
== GL_ABGR_EXT
) {
367 return GL_INVALID_OPERATION
;
373 /* now, for each format, check the type for compatibility */
376 case GL_STENCIL_INDEX
:
380 case GL_UNSIGNED_BYTE
:
382 case GL_UNSIGNED_SHORT
:
384 case GL_UNSIGNED_INT
:
388 return ctx
->Extensions
.ARB_half_float_pixel
389 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
391 return GL_INVALID_ENUM
;
398 #if 0 /* not legal! see table 3.6 of the 1.5 spec */
402 case GL_LUMINANCE_ALPHA
:
403 case GL_DEPTH_COMPONENT
:
406 case GL_UNSIGNED_BYTE
:
408 case GL_UNSIGNED_SHORT
:
410 case GL_UNSIGNED_INT
:
414 return ctx
->Extensions
.ARB_half_float_pixel
415 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
417 return GL_INVALID_ENUM
;
423 case GL_UNSIGNED_BYTE
:
425 case GL_UNSIGNED_SHORT
:
427 case GL_UNSIGNED_INT
:
429 case GL_UNSIGNED_BYTE_3_3_2
:
430 case GL_UNSIGNED_BYTE_2_3_3_REV
:
431 case GL_UNSIGNED_SHORT_5_6_5
:
432 case GL_UNSIGNED_SHORT_5_6_5_REV
:
435 return ctx
->Extensions
.ARB_half_float_pixel
436 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
438 return GL_INVALID_ENUM
;
443 /* NOTE: no packed types are supported with BGR. That's
444 * intentional, according to the GL spec.
447 case GL_UNSIGNED_BYTE
:
449 case GL_UNSIGNED_SHORT
:
451 case GL_UNSIGNED_INT
:
455 return ctx
->Extensions
.ARB_half_float_pixel
456 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
458 return GL_INVALID_ENUM
;
466 case GL_UNSIGNED_BYTE
:
468 case GL_UNSIGNED_SHORT
:
470 case GL_UNSIGNED_INT
:
472 case GL_UNSIGNED_SHORT_4_4_4_4
:
473 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
474 case GL_UNSIGNED_SHORT_5_5_5_1
:
475 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
476 case GL_UNSIGNED_INT_8_8_8_8
:
477 case GL_UNSIGNED_INT_8_8_8_8_REV
:
480 return ctx
->Extensions
.ARB_half_float_pixel
481 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
483 return GL_INVALID_ENUM
;
487 if (!ctx
->Extensions
.MESA_ycbcr_texture
)
488 return GL_INVALID_ENUM
;
489 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
||
490 type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
)
493 return GL_INVALID_OPERATION
;
495 /* integer-valued formats */
496 case GL_RED_INTEGER_EXT
:
497 case GL_GREEN_INTEGER_EXT
:
498 case GL_BLUE_INTEGER_EXT
:
499 case GL_ALPHA_INTEGER_EXT
:
503 case GL_UNSIGNED_BYTE
:
505 case GL_UNSIGNED_SHORT
:
507 case GL_UNSIGNED_INT
:
508 return (ctx
->VersionMajor
>= 3 ||
509 ctx
->Extensions
.EXT_texture_integer
)
510 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
512 return GL_INVALID_ENUM
;
515 case GL_RGB_INTEGER_EXT
:
518 case GL_UNSIGNED_BYTE
:
520 case GL_UNSIGNED_SHORT
:
522 case GL_UNSIGNED_INT
:
523 return (ctx
->VersionMajor
>= 3 ||
524 ctx
->Extensions
.EXT_texture_integer
)
525 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
527 return GL_INVALID_ENUM
;
530 case GL_BGR_INTEGER_EXT
:
533 case GL_UNSIGNED_BYTE
:
535 case GL_UNSIGNED_SHORT
:
537 case GL_UNSIGNED_INT
:
538 /* NOTE: no packed formats w/ BGR format */
539 return (ctx
->VersionMajor
>= 3 ||
540 ctx
->Extensions
.EXT_texture_integer
)
541 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
543 return GL_INVALID_ENUM
;
546 case GL_RGBA_INTEGER_EXT
:
547 case GL_BGRA_INTEGER_EXT
:
550 case GL_UNSIGNED_BYTE
:
552 case GL_UNSIGNED_SHORT
:
554 case GL_UNSIGNED_INT
:
555 return (ctx
->VersionMajor
>= 3 ||
556 ctx
->Extensions
.EXT_texture_integer
)
557 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
559 return GL_INVALID_ENUM
;
562 case GL_LUMINANCE_INTEGER_EXT
:
563 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
566 case GL_UNSIGNED_BYTE
:
568 case GL_UNSIGNED_SHORT
:
570 case GL_UNSIGNED_INT
:
571 return ctx
->Extensions
.EXT_texture_integer
572 ? GL_NO_ERROR
: GL_INVALID_ENUM
;
574 return GL_INVALID_ENUM
;
578 return GL_INVALID_ENUM
;
585 * Test if the given image format is a color/RGBA format (i.e., not color
586 * index, depth, stencil, etc).
587 * \param format the image format value (may by an internal texture format)
588 * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
591 _mesa_is_color_format(GLenum format
)
609 case GL_LUMINANCE_ALPHA
:
610 case GL_LUMINANCE4_ALPHA4
:
611 case GL_LUMINANCE6_ALPHA2
:
612 case GL_LUMINANCE8_ALPHA8
:
613 case GL_LUMINANCE12_ALPHA4
:
614 case GL_LUMINANCE12_ALPHA12
:
615 case GL_LUMINANCE16_ALPHA16
:
647 /* float texture formats */
648 case GL_ALPHA16F_ARB
:
649 case GL_ALPHA32F_ARB
:
650 case GL_LUMINANCE16F_ARB
:
651 case GL_LUMINANCE32F_ARB
:
652 case GL_LUMINANCE_ALPHA16F_ARB
:
653 case GL_LUMINANCE_ALPHA32F_ARB
:
654 case GL_INTENSITY16F_ARB
:
655 case GL_INTENSITY32F_ARB
:
664 /* generic integer formats */
665 case GL_RED_INTEGER_EXT
:
666 case GL_GREEN_INTEGER_EXT
:
667 case GL_BLUE_INTEGER_EXT
:
668 case GL_ALPHA_INTEGER_EXT
:
669 case GL_RGB_INTEGER_EXT
:
670 case GL_RGBA_INTEGER_EXT
:
671 case GL_BGR_INTEGER_EXT
:
672 case GL_BGRA_INTEGER_EXT
:
673 case GL_LUMINANCE_INTEGER_EXT
:
674 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
675 /* sized integer formats */
676 case GL_RGBA32UI_EXT
:
678 case GL_ALPHA32UI_EXT
:
679 case GL_INTENSITY32UI_EXT
:
680 case GL_LUMINANCE32UI_EXT
:
681 case GL_LUMINANCE_ALPHA32UI_EXT
:
682 case GL_RGBA16UI_EXT
:
684 case GL_ALPHA16UI_EXT
:
685 case GL_INTENSITY16UI_EXT
:
686 case GL_LUMINANCE16UI_EXT
:
687 case GL_LUMINANCE_ALPHA16UI_EXT
:
690 case GL_ALPHA8UI_EXT
:
691 case GL_INTENSITY8UI_EXT
:
692 case GL_LUMINANCE8UI_EXT
:
693 case GL_LUMINANCE_ALPHA8UI_EXT
:
696 case GL_ALPHA32I_EXT
:
697 case GL_INTENSITY32I_EXT
:
698 case GL_LUMINANCE32I_EXT
:
699 case GL_LUMINANCE_ALPHA32I_EXT
:
702 case GL_ALPHA16I_EXT
:
703 case GL_INTENSITY16I_EXT
:
704 case GL_LUMINANCE16I_EXT
:
705 case GL_LUMINANCE_ALPHA16I_EXT
:
709 case GL_INTENSITY8I_EXT
:
710 case GL_LUMINANCE8I_EXT
:
711 case GL_LUMINANCE_ALPHA8I_EXT
:
712 case GL_R11F_G11F_B10F
:
715 case GL_YCBCR_MESA
: /* not considered to be RGB */
724 * Test if the given image format is a depth component format.
727 _mesa_is_depth_format(GLenum format
)
730 case GL_DEPTH_COMPONENT
:
731 case GL_DEPTH_COMPONENT16
:
732 case GL_DEPTH_COMPONENT24
:
733 case GL_DEPTH_COMPONENT32
:
742 * Test if the given image format is a stencil format.
745 _mesa_is_stencil_format(GLenum format
)
748 case GL_STENCIL_INDEX
:
757 * Test if the given image format is a YCbCr format.
760 _mesa_is_ycbcr_format(GLenum format
)
772 * Test if the given image format is a depth or stencil format.
775 _mesa_is_depth_or_stencil_format(GLenum format
)
778 case GL_DEPTH_COMPONENT
:
779 case GL_DEPTH_COMPONENT16
:
780 case GL_DEPTH_COMPONENT24
:
781 case GL_DEPTH_COMPONENT32
:
782 case GL_STENCIL_INDEX
:
783 case GL_STENCIL_INDEX1_EXT
:
784 case GL_STENCIL_INDEX4_EXT
:
785 case GL_STENCIL_INDEX8_EXT
:
786 case GL_STENCIL_INDEX16_EXT
:
795 * Test if the given format is an integer (non-normalized) format.
798 _mesa_is_integer_format(GLenum format
)
801 /* generic integer formats */
802 case GL_RED_INTEGER_EXT
:
803 case GL_GREEN_INTEGER_EXT
:
804 case GL_BLUE_INTEGER_EXT
:
805 case GL_ALPHA_INTEGER_EXT
:
806 case GL_RGB_INTEGER_EXT
:
807 case GL_RGBA_INTEGER_EXT
:
808 case GL_BGR_INTEGER_EXT
:
809 case GL_BGRA_INTEGER_EXT
:
810 case GL_LUMINANCE_INTEGER_EXT
:
811 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
813 /* specific integer formats */
814 case GL_RGBA32UI_EXT
:
818 case GL_ALPHA32UI_EXT
:
819 case GL_INTENSITY32UI_EXT
:
820 case GL_LUMINANCE32UI_EXT
:
821 case GL_LUMINANCE_ALPHA32UI_EXT
:
822 case GL_RGBA16UI_EXT
:
826 case GL_ALPHA16UI_EXT
:
827 case GL_INTENSITY16UI_EXT
:
828 case GL_LUMINANCE16UI_EXT
:
829 case GL_LUMINANCE_ALPHA16UI_EXT
:
834 case GL_ALPHA8UI_EXT
:
835 case GL_INTENSITY8UI_EXT
:
836 case GL_LUMINANCE8UI_EXT
:
837 case GL_LUMINANCE_ALPHA8UI_EXT
:
842 case GL_ALPHA32I_EXT
:
843 case GL_INTENSITY32I_EXT
:
844 case GL_LUMINANCE32I_EXT
:
845 case GL_LUMINANCE_ALPHA32I_EXT
:
850 case GL_ALPHA16I_EXT
:
851 case GL_INTENSITY16I_EXT
:
852 case GL_LUMINANCE16I_EXT
:
853 case GL_LUMINANCE_ALPHA16I_EXT
:
859 case GL_INTENSITY8I_EXT
:
860 case GL_LUMINANCE8I_EXT
:
861 case GL_LUMINANCE_ALPHA8I_EXT
:
871 * Does the given base texture/renderbuffer format have the channel
875 _mesa_base_format_has_channel(GLenum base_format
, GLenum pname
)
878 case GL_TEXTURE_RED_SIZE
:
879 case GL_TEXTURE_RED_TYPE
:
880 case GL_RENDERBUFFER_RED_SIZE_EXT
:
881 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE
:
882 if (base_format
== GL_RED
||
883 base_format
== GL_RG
||
884 base_format
== GL_RGB
||
885 base_format
== GL_RGBA
) {
889 case GL_TEXTURE_GREEN_SIZE
:
890 case GL_TEXTURE_GREEN_TYPE
:
891 case GL_RENDERBUFFER_GREEN_SIZE_EXT
:
892 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
:
893 if (base_format
== GL_RG
||
894 base_format
== GL_RGB
||
895 base_format
== GL_RGBA
) {
899 case GL_TEXTURE_BLUE_SIZE
:
900 case GL_TEXTURE_BLUE_TYPE
:
901 case GL_RENDERBUFFER_BLUE_SIZE_EXT
:
902 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
:
903 if (base_format
== GL_RGB
||
904 base_format
== GL_RGBA
) {
908 case GL_TEXTURE_ALPHA_SIZE
:
909 case GL_TEXTURE_ALPHA_TYPE
:
910 case GL_RENDERBUFFER_ALPHA_SIZE_EXT
:
911 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
:
912 if (base_format
== GL_RGBA
||
913 base_format
== GL_ALPHA
||
914 base_format
== GL_LUMINANCE_ALPHA
) {
918 case GL_TEXTURE_LUMINANCE_SIZE
:
919 case GL_TEXTURE_LUMINANCE_TYPE
:
920 if (base_format
== GL_LUMINANCE
||
921 base_format
== GL_LUMINANCE_ALPHA
) {
925 case GL_TEXTURE_INTENSITY_SIZE
:
926 case GL_TEXTURE_INTENSITY_TYPE
:
927 if (base_format
== GL_INTENSITY
) {
931 case GL_TEXTURE_DEPTH_SIZE
:
932 case GL_TEXTURE_DEPTH_TYPE
:
933 case GL_RENDERBUFFER_DEPTH_SIZE_EXT
:
934 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
:
935 if (base_format
== GL_DEPTH_COMPONENT
) {
939 case GL_RENDERBUFFER_STENCIL_SIZE_EXT
:
940 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE
:
941 if (base_format
== GL_STENCIL_INDEX
) {
946 _mesa_warning(NULL
, "%s: Unexpected channel token 0x%x\n",
947 __FUNCTION__
, pname
);
956 * Return the byte offset of a specific pixel in an image (1D, 2D or 3D).
958 * Pixel unpacking/packing parameters are observed according to \p packing.
960 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
961 * \param packing the pixelstore attributes
962 * \param width the image width
963 * \param height the image height
964 * \param format the pixel format (must be validated beforehand)
965 * \param type the pixel data type (must be validated beforehand)
966 * \param img which image in the volume (0 for 1D or 2D images)
967 * \param row row of pixel in the image (0 for 1D images)
968 * \param column column of pixel in the image
970 * \return offset of pixel.
972 * \sa gl_pixelstore_attrib.
975 _mesa_image_offset( GLuint dimensions
,
976 const struct gl_pixelstore_attrib
*packing
,
977 GLsizei width
, GLsizei height
,
978 GLenum format
, GLenum type
,
979 GLint img
, GLint row
, GLint column
)
981 GLint alignment
; /* 1, 2 or 4 */
982 GLint pixels_per_row
;
983 GLint rows_per_image
;
986 GLint skipimages
; /* for 3-D volume images */
989 ASSERT(dimensions
>= 1 && dimensions
<= 3);
991 alignment
= packing
->Alignment
;
992 if (packing
->RowLength
> 0) {
993 pixels_per_row
= packing
->RowLength
;
996 pixels_per_row
= width
;
998 if (packing
->ImageHeight
> 0) {
999 rows_per_image
= packing
->ImageHeight
;
1002 rows_per_image
= height
;
1005 skippixels
= packing
->SkipPixels
;
1006 /* Note: SKIP_ROWS _is_ used for 1D images */
1007 skiprows
= packing
->SkipRows
;
1008 /* Note: SKIP_IMAGES is only used for 3D images */
1009 skipimages
= (dimensions
== 3) ? packing
->SkipImages
: 0;
1011 if (type
== GL_BITMAP
) {
1013 GLint bytes_per_row
;
1014 GLint bytes_per_image
;
1015 /* components per pixel for color or stencil index: */
1016 const GLint comp_per_pixel
= 1;
1018 /* The pixel type and format should have been error checked earlier */
1019 assert(format
== GL_COLOR_INDEX
|| format
== GL_STENCIL_INDEX
);
1021 bytes_per_row
= alignment
1022 * CEILING( comp_per_pixel
*pixels_per_row
, 8*alignment
);
1024 bytes_per_image
= bytes_per_row
* rows_per_image
;
1026 offset
= (skipimages
+ img
) * bytes_per_image
1027 + (skiprows
+ row
) * bytes_per_row
1028 + (skippixels
+ column
) / 8;
1031 /* Non-BITMAP data */
1032 GLint bytes_per_pixel
, bytes_per_row
, remainder
, bytes_per_image
;
1035 bytes_per_pixel
= _mesa_bytes_per_pixel( format
, type
);
1037 /* The pixel type and format should have been error checked earlier */
1038 assert(bytes_per_pixel
> 0);
1040 bytes_per_row
= pixels_per_row
* bytes_per_pixel
;
1041 remainder
= bytes_per_row
% alignment
;
1043 bytes_per_row
+= (alignment
- remainder
);
1045 ASSERT(bytes_per_row
% alignment
== 0);
1047 bytes_per_image
= bytes_per_row
* rows_per_image
;
1049 if (packing
->Invert
) {
1050 /* set pixel_addr to the last row */
1051 topOfImage
= bytes_per_row
* (height
- 1);
1052 bytes_per_row
= -bytes_per_row
;
1058 /* compute final pixel address */
1059 offset
= (skipimages
+ img
) * bytes_per_image
1061 + (skiprows
+ row
) * bytes_per_row
1062 + (skippixels
+ column
) * bytes_per_pixel
;
1070 * Return the address of a specific pixel in an image (1D, 2D or 3D).
1072 * Pixel unpacking/packing parameters are observed according to \p packing.
1074 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
1075 * \param packing the pixelstore attributes
1076 * \param image starting address of image data
1077 * \param width the image width
1078 * \param height the image height
1079 * \param format the pixel format (must be validated beforehand)
1080 * \param type the pixel data type (must be validated beforehand)
1081 * \param img which image in the volume (0 for 1D or 2D images)
1082 * \param row row of pixel in the image (0 for 1D images)
1083 * \param column column of pixel in the image
1085 * \return address of pixel.
1087 * \sa gl_pixelstore_attrib.
1090 _mesa_image_address( GLuint dimensions
,
1091 const struct gl_pixelstore_attrib
*packing
,
1092 const GLvoid
*image
,
1093 GLsizei width
, GLsizei height
,
1094 GLenum format
, GLenum type
,
1095 GLint img
, GLint row
, GLint column
)
1097 const GLubyte
*addr
= (const GLubyte
*) image
;
1099 addr
+= _mesa_image_offset(dimensions
, packing
, width
, height
,
1100 format
, type
, img
, row
, column
);
1102 return (GLvoid
*) addr
;
1107 _mesa_image_address1d( const struct gl_pixelstore_attrib
*packing
,
1108 const GLvoid
*image
,
1110 GLenum format
, GLenum type
,
1113 return _mesa_image_address(1, packing
, image
, width
, 1,
1114 format
, type
, 0, 0, column
);
1119 _mesa_image_address2d( const struct gl_pixelstore_attrib
*packing
,
1120 const GLvoid
*image
,
1121 GLsizei width
, GLsizei height
,
1122 GLenum format
, GLenum type
,
1123 GLint row
, GLint column
)
1125 return _mesa_image_address(2, packing
, image
, width
, height
,
1126 format
, type
, 0, row
, column
);
1131 _mesa_image_address3d( const struct gl_pixelstore_attrib
*packing
,
1132 const GLvoid
*image
,
1133 GLsizei width
, GLsizei height
,
1134 GLenum format
, GLenum type
,
1135 GLint img
, GLint row
, GLint column
)
1137 return _mesa_image_address(3, packing
, image
, width
, height
,
1138 format
, type
, img
, row
, column
);
1144 * Compute the stride (in bytes) between image rows.
1146 * \param packing the pixelstore attributes
1147 * \param width image width.
1148 * \param format pixel format.
1149 * \param type pixel data type.
1151 * \return the stride in bytes for the given parameters, or -1 if error
1154 _mesa_image_row_stride( const struct gl_pixelstore_attrib
*packing
,
1155 GLint width
, GLenum format
, GLenum type
)
1157 GLint bytesPerRow
, remainder
;
1161 if (type
== GL_BITMAP
) {
1162 if (packing
->RowLength
== 0) {
1163 bytesPerRow
= (width
+ 7) / 8;
1166 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
1170 /* Non-BITMAP data */
1171 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1172 if (bytesPerPixel
<= 0)
1173 return -1; /* error */
1174 if (packing
->RowLength
== 0) {
1175 bytesPerRow
= bytesPerPixel
* width
;
1178 bytesPerRow
= bytesPerPixel
* packing
->RowLength
;
1182 remainder
= bytesPerRow
% packing
->Alignment
;
1183 if (remainder
> 0) {
1184 bytesPerRow
+= (packing
->Alignment
- remainder
);
1187 if (packing
->Invert
) {
1188 /* negate the bytes per row (negative row stride) */
1189 bytesPerRow
= -bytesPerRow
;
1197 * Compute the stride between images in a 3D texture (in bytes) for the given
1198 * pixel packing parameters and image width, format and type.
1201 _mesa_image_image_stride( const struct gl_pixelstore_attrib
*packing
,
1202 GLint width
, GLint height
,
1203 GLenum format
, GLenum type
)
1205 GLint bytesPerRow
, bytesPerImage
, remainder
;
1209 if (type
== GL_BITMAP
) {
1210 if (packing
->RowLength
== 0) {
1211 bytesPerRow
= (width
+ 7) / 8;
1214 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
1218 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1220 if (bytesPerPixel
<= 0)
1221 return -1; /* error */
1222 if (packing
->RowLength
== 0) {
1223 bytesPerRow
= bytesPerPixel
* width
;
1226 bytesPerRow
= bytesPerPixel
* packing
->RowLength
;
1230 remainder
= bytesPerRow
% packing
->Alignment
;
1232 bytesPerRow
+= (packing
->Alignment
- remainder
);
1234 if (packing
->ImageHeight
== 0)
1235 bytesPerImage
= bytesPerRow
* height
;
1237 bytesPerImage
= bytesPerRow
* packing
->ImageHeight
;
1239 return bytesPerImage
;
1245 * "Expand" a bitmap from 1-bit per pixel to 8-bits per pixel.
1246 * This is typically used to convert a bitmap into a GLubyte/pixel texture.
1247 * "On" bits will set texels to \p onValue.
1248 * "Off" bits will not modify texels.
1249 * \param width src bitmap width in pixels
1250 * \param height src bitmap height in pixels
1251 * \param unpack bitmap unpacking state
1252 * \param bitmap the src bitmap data
1253 * \param destBuffer start of dest buffer
1254 * \param destStride row stride in dest buffer
1255 * \param onValue if bit is 1, set destBuffer pixel to this value
1258 _mesa_expand_bitmap(GLsizei width
, GLsizei height
,
1259 const struct gl_pixelstore_attrib
*unpack
,
1260 const GLubyte
*bitmap
,
1261 GLubyte
*destBuffer
, GLint destStride
,
1264 const GLubyte
*srcRow
= (const GLubyte
*)
1265 _mesa_image_address2d(unpack
, bitmap
, width
, height
,
1266 GL_COLOR_INDEX
, GL_BITMAP
, 0, 0);
1267 const GLint srcStride
= _mesa_image_row_stride(unpack
, width
,
1268 GL_COLOR_INDEX
, GL_BITMAP
);
1271 #define SET_PIXEL(COL, ROW) \
1272 destBuffer[(ROW) * destStride + (COL)] = onValue;
1274 for (row
= 0; row
< height
; row
++) {
1275 const GLubyte
*src
= srcRow
;
1277 if (unpack
->LsbFirst
) {
1279 GLubyte mask
= 1U << (unpack
->SkipPixels
& 0x7);
1280 for (col
= 0; col
< width
; col
++) {
1283 SET_PIXEL(col
, row
);
1295 /* get ready for next row */
1301 GLubyte mask
= 128U >> (unpack
->SkipPixels
& 0x7);
1302 for (col
= 0; col
< width
; col
++) {
1305 SET_PIXEL(col
, row
);
1317 /* get ready for next row */
1322 srcRow
+= srcStride
;
1332 * Convert an array of RGBA colors from one datatype to another.
1333 * NOTE: src may equal dst. In that case, we use a temporary buffer.
1336 _mesa_convert_colors(GLenum srcType
, const GLvoid
*src
,
1337 GLenum dstType
, GLvoid
*dst
,
1338 GLuint count
, const GLubyte mask
[])
1341 const GLboolean useTemp
= (src
== dst
);
1343 tempBuffer
= malloc(count
* MAX_PIXEL_BYTES
);
1347 ASSERT(srcType
!= dstType
);
1350 case GL_UNSIGNED_BYTE
:
1351 if (dstType
== GL_UNSIGNED_SHORT
) {
1352 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
1353 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
1355 for (i
= 0; i
< count
; i
++) {
1356 if (!mask
|| mask
[i
]) {
1357 dst2
[i
][RCOMP
] = UBYTE_TO_USHORT(src1
[i
][RCOMP
]);
1358 dst2
[i
][GCOMP
] = UBYTE_TO_USHORT(src1
[i
][GCOMP
]);
1359 dst2
[i
][BCOMP
] = UBYTE_TO_USHORT(src1
[i
][BCOMP
]);
1360 dst2
[i
][ACOMP
] = UBYTE_TO_USHORT(src1
[i
][ACOMP
]);
1364 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
1367 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
1368 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
1370 ASSERT(dstType
== GL_FLOAT
);
1371 for (i
= 0; i
< count
; i
++) {
1372 if (!mask
|| mask
[i
]) {
1373 dst4
[i
][RCOMP
] = UBYTE_TO_FLOAT(src1
[i
][RCOMP
]);
1374 dst4
[i
][GCOMP
] = UBYTE_TO_FLOAT(src1
[i
][GCOMP
]);
1375 dst4
[i
][BCOMP
] = UBYTE_TO_FLOAT(src1
[i
][BCOMP
]);
1376 dst4
[i
][ACOMP
] = UBYTE_TO_FLOAT(src1
[i
][ACOMP
]);
1380 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
1383 case GL_UNSIGNED_SHORT
:
1384 if (dstType
== GL_UNSIGNED_BYTE
) {
1385 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
1386 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
1388 for (i
= 0; i
< count
; i
++) {
1389 if (!mask
|| mask
[i
]) {
1390 dst1
[i
][RCOMP
] = USHORT_TO_UBYTE(src2
[i
][RCOMP
]);
1391 dst1
[i
][GCOMP
] = USHORT_TO_UBYTE(src2
[i
][GCOMP
]);
1392 dst1
[i
][BCOMP
] = USHORT_TO_UBYTE(src2
[i
][BCOMP
]);
1393 dst1
[i
][ACOMP
] = USHORT_TO_UBYTE(src2
[i
][ACOMP
]);
1397 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
1400 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
1401 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
1403 ASSERT(dstType
== GL_FLOAT
);
1404 for (i
= 0; i
< count
; i
++) {
1405 if (!mask
|| mask
[i
]) {
1406 dst4
[i
][RCOMP
] = USHORT_TO_FLOAT(src2
[i
][RCOMP
]);
1407 dst4
[i
][GCOMP
] = USHORT_TO_FLOAT(src2
[i
][GCOMP
]);
1408 dst4
[i
][BCOMP
] = USHORT_TO_FLOAT(src2
[i
][BCOMP
]);
1409 dst4
[i
][ACOMP
] = USHORT_TO_FLOAT(src2
[i
][ACOMP
]);
1413 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
1417 if (dstType
== GL_UNSIGNED_BYTE
) {
1418 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
1419 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
1421 for (i
= 0; i
< count
; i
++) {
1422 if (!mask
|| mask
[i
])
1423 _mesa_unclamped_float_rgba_to_ubyte(dst1
[i
], src4
[i
]);
1426 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
1429 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
1430 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
1432 ASSERT(dstType
== GL_UNSIGNED_SHORT
);
1433 for (i
= 0; i
< count
; i
++) {
1434 if (!mask
|| mask
[i
]) {
1435 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][RCOMP
], src4
[i
][RCOMP
]);
1436 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][GCOMP
], src4
[i
][GCOMP
]);
1437 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][BCOMP
], src4
[i
][BCOMP
]);
1438 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][ACOMP
], src4
[i
][ACOMP
]);
1442 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
1446 _mesa_problem(NULL
, "Invalid datatype in _mesa_convert_colors");
1456 * Perform basic clipping for glDrawPixels. The image's position and size
1457 * and the unpack SkipPixels and SkipRows are adjusted so that the image
1458 * region is entirely within the window and scissor bounds.
1459 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
1460 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
1461 * we'll actually write. Beforehand, *destY-1 is the first drawing row.
1463 * \return GL_TRUE if image is ready for drawing or
1464 * GL_FALSE if image was completely clipped away (draw nothing)
1467 _mesa_clip_drawpixels(const struct gl_context
*ctx
,
1468 GLint
*destX
, GLint
*destY
,
1469 GLsizei
*width
, GLsizei
*height
,
1470 struct gl_pixelstore_attrib
*unpack
)
1472 const struct gl_framebuffer
*buffer
= ctx
->DrawBuffer
;
1474 if (unpack
->RowLength
== 0) {
1475 unpack
->RowLength
= *width
;
1478 ASSERT(ctx
->Pixel
.ZoomX
== 1.0F
);
1479 ASSERT(ctx
->Pixel
.ZoomY
== 1.0F
|| ctx
->Pixel
.ZoomY
== -1.0F
);
1482 if (*destX
< buffer
->_Xmin
) {
1483 unpack
->SkipPixels
+= (buffer
->_Xmin
- *destX
);
1484 *width
-= (buffer
->_Xmin
- *destX
);
1485 *destX
= buffer
->_Xmin
;
1487 /* right clipping */
1488 if (*destX
+ *width
> buffer
->_Xmax
)
1489 *width
-= (*destX
+ *width
- buffer
->_Xmax
);
1494 if (ctx
->Pixel
.ZoomY
== 1.0F
) {
1495 /* bottom clipping */
1496 if (*destY
< buffer
->_Ymin
) {
1497 unpack
->SkipRows
+= (buffer
->_Ymin
- *destY
);
1498 *height
-= (buffer
->_Ymin
- *destY
);
1499 *destY
= buffer
->_Ymin
;
1502 if (*destY
+ *height
> buffer
->_Ymax
)
1503 *height
-= (*destY
+ *height
- buffer
->_Ymax
);
1505 else { /* upside down */
1507 if (*destY
> buffer
->_Ymax
) {
1508 unpack
->SkipRows
+= (*destY
- buffer
->_Ymax
);
1509 *height
-= (*destY
- buffer
->_Ymax
);
1510 *destY
= buffer
->_Ymax
;
1512 /* bottom clipping */
1513 if (*destY
- *height
< buffer
->_Ymin
)
1514 *height
-= (buffer
->_Ymin
- (*destY
- *height
));
1515 /* adjust destY so it's the first row to write to */
1527 * Perform clipping for glReadPixels. The image's window position
1528 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
1529 * so that the image region is entirely within the window bounds.
1530 * Note: this is different from _mesa_clip_drawpixels() in that the
1531 * scissor box is ignored, and we use the bounds of the current readbuffer
1534 * \return GL_TRUE if region to read is in bounds
1535 * GL_FALSE if region is completely out of bounds (nothing to read)
1538 _mesa_clip_readpixels(const struct gl_context
*ctx
,
1539 GLint
*srcX
, GLint
*srcY
,
1540 GLsizei
*width
, GLsizei
*height
,
1541 struct gl_pixelstore_attrib
*pack
)
1543 const struct gl_framebuffer
*buffer
= ctx
->ReadBuffer
;
1545 if (pack
->RowLength
== 0) {
1546 pack
->RowLength
= *width
;
1551 pack
->SkipPixels
+= (0 - *srcX
);
1552 *width
-= (0 - *srcX
);
1555 /* right clipping */
1556 if (*srcX
+ *width
> (GLsizei
) buffer
->Width
)
1557 *width
-= (*srcX
+ *width
- buffer
->Width
);
1562 /* bottom clipping */
1564 pack
->SkipRows
+= (0 - *srcY
);
1565 *height
-= (0 - *srcY
);
1569 if (*srcY
+ *height
> (GLsizei
) buffer
->Height
)
1570 *height
-= (*srcY
+ *height
- buffer
->Height
);
1580 * Do clipping for a glCopyTexSubImage call.
1581 * The framebuffer source region might extend outside the framebuffer
1582 * bounds. Clip the source region against the framebuffer bounds and
1583 * adjust the texture/dest position and size accordingly.
1585 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
1588 _mesa_clip_copytexsubimage(const struct gl_context
*ctx
,
1589 GLint
*destX
, GLint
*destY
,
1590 GLint
*srcX
, GLint
*srcY
,
1591 GLsizei
*width
, GLsizei
*height
)
1593 const struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1594 const GLint srcX0
= *srcX
, srcY0
= *srcY
;
1596 if (_mesa_clip_to_region(0, 0, fb
->Width
, fb
->Height
,
1597 srcX
, srcY
, width
, height
)) {
1598 *destX
= *destX
+ *srcX
- srcX0
;
1599 *destY
= *destY
+ *srcY
- srcY0
;
1611 * Clip the rectangle defined by (x, y, width, height) against the bounds
1612 * specified by [xmin, xmax) and [ymin, ymax).
1613 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
1616 _mesa_clip_to_region(GLint xmin
, GLint ymin
,
1617 GLint xmax
, GLint ymax
,
1619 GLsizei
*width
, GLsizei
*height
)
1623 *width
-= (xmin
- *x
);
1627 /* right clipping */
1628 if (*x
+ *width
> xmax
)
1629 *width
-= (*x
+ *width
- xmax
);
1634 /* bottom (or top) clipping */
1636 *height
-= (ymin
- *y
);
1640 /* top (or bottom) clipping */
1641 if (*y
+ *height
> ymax
)
1642 *height
-= (*y
+ *height
- ymax
);
1652 * Clip dst coords against Xmax (or Ymax).
1655 clip_right_or_top(GLint
*srcX0
, GLint
*srcX1
,
1656 GLint
*dstX0
, GLint
*dstX1
,
1661 if (*dstX1
> maxValue
) {
1662 /* X1 outside right edge */
1663 ASSERT(*dstX0
< maxValue
); /* X0 should be inside right edge */
1664 t
= (GLfloat
) (maxValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
1665 /* chop off [t, 1] part */
1666 ASSERT(t
>= 0.0 && t
<= 1.0);
1668 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
1669 *srcX1
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
1671 else if (*dstX0
> maxValue
) {
1672 /* X0 outside right edge */
1673 ASSERT(*dstX1
< maxValue
); /* X1 should be inside right edge */
1674 t
= (GLfloat
) (maxValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
1675 /* chop off [t, 1] part */
1676 ASSERT(t
>= 0.0 && t
<= 1.0);
1678 bias
= (*srcX0
< *srcX1
) ? -0.5F
: 0.5F
;
1679 *srcX0
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
1685 * Clip dst coords against Xmin (or Ymin).
1688 clip_left_or_bottom(GLint
*srcX0
, GLint
*srcX1
,
1689 GLint
*dstX0
, GLint
*dstX1
,
1694 if (*dstX0
< minValue
) {
1695 /* X0 outside left edge */
1696 ASSERT(*dstX1
> minValue
); /* X1 should be inside left edge */
1697 t
= (GLfloat
) (minValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
1698 /* chop off [0, t] part */
1699 ASSERT(t
>= 0.0 && t
<= 1.0);
1701 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
; /* flipped??? */
1702 *srcX0
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
1704 else if (*dstX1
< minValue
) {
1705 /* X1 outside left edge */
1706 ASSERT(*dstX0
> minValue
); /* X0 should be inside left edge */
1707 t
= (GLfloat
) (minValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
1708 /* chop off [0, t] part */
1709 ASSERT(t
>= 0.0 && t
<= 1.0);
1711 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
1712 *srcX1
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
1718 * Do clipping of blit src/dest rectangles.
1719 * The dest rect is clipped against both the buffer bounds and scissor bounds.
1720 * The src rect is just clipped against the buffer bounds.
1722 * When either the src or dest rect is clipped, the other is also clipped
1725 * Note that X0 need not be less than X1 (same for Y) for either the source
1726 * and dest rects. That makes the clipping a little trickier.
1728 * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped
1731 _mesa_clip_blit(struct gl_context
*ctx
,
1732 GLint
*srcX0
, GLint
*srcY0
, GLint
*srcX1
, GLint
*srcY1
,
1733 GLint
*dstX0
, GLint
*dstY0
, GLint
*dstX1
, GLint
*dstY1
)
1735 const GLint srcXmin
= 0;
1736 const GLint srcXmax
= ctx
->ReadBuffer
->Width
;
1737 const GLint srcYmin
= 0;
1738 const GLint srcYmax
= ctx
->ReadBuffer
->Height
;
1740 /* these include scissor bounds */
1741 const GLint dstXmin
= ctx
->DrawBuffer
->_Xmin
;
1742 const GLint dstXmax
= ctx
->DrawBuffer
->_Xmax
;
1743 const GLint dstYmin
= ctx
->DrawBuffer
->_Ymin
;
1744 const GLint dstYmax
= ctx
->DrawBuffer
->_Ymax
;
1747 printf("PreClipX: src: %d .. %d dst: %d .. %d\n",
1748 *srcX0, *srcX1, *dstX0, *dstX1);
1749 printf("PreClipY: src: %d .. %d dst: %d .. %d\n",
1750 *srcY0, *srcY1, *dstY0, *dstY1);
1753 /* trivial rejection tests */
1754 if (*dstX0
== *dstX1
)
1755 return GL_FALSE
; /* no width */
1756 if (*dstX0
<= dstXmin
&& *dstX1
<= dstXmin
)
1757 return GL_FALSE
; /* totally out (left) of bounds */
1758 if (*dstX0
>= dstXmax
&& *dstX1
>= dstXmax
)
1759 return GL_FALSE
; /* totally out (right) of bounds */
1761 if (*dstY0
== *dstY1
)
1763 if (*dstY0
<= dstYmin
&& *dstY1
<= dstYmin
)
1765 if (*dstY0
>= dstYmax
&& *dstY1
>= dstYmax
)
1768 if (*srcX0
== *srcX1
)
1770 if (*srcX0
<= srcXmin
&& *srcX1
<= srcXmin
)
1772 if (*srcX0
>= srcXmax
&& *srcX1
>= srcXmax
)
1775 if (*srcY0
== *srcY1
)
1777 if (*srcY0
<= srcYmin
&& *srcY1
<= srcYmin
)
1779 if (*srcY0
>= srcYmax
&& *srcY1
>= srcYmax
)
1785 clip_right_or_top(srcX0
, srcX1
, dstX0
, dstX1
, dstXmax
);
1786 clip_right_or_top(srcY0
, srcY1
, dstY0
, dstY1
, dstYmax
);
1787 clip_left_or_bottom(srcX0
, srcX1
, dstX0
, dstX1
, dstXmin
);
1788 clip_left_or_bottom(srcY0
, srcY1
, dstY0
, dstY1
, dstYmin
);
1791 * src clip (just swap src/dst values from above)
1793 clip_right_or_top(dstX0
, dstX1
, srcX0
, srcX1
, srcXmax
);
1794 clip_right_or_top(dstY0
, dstY1
, srcY0
, srcY1
, srcYmax
);
1795 clip_left_or_bottom(dstX0
, dstX1
, srcX0
, srcX1
, srcXmin
);
1796 clip_left_or_bottom(dstY0
, dstY1
, srcY0
, srcY1
, srcYmin
);
1799 printf("PostClipX: src: %d .. %d dst: %d .. %d\n",
1800 *srcX0, *srcX1, *dstX0, *dstX1);
1801 printf("PostClipY: src: %d .. %d dst: %d .. %d\n",
1802 *srcY0, *srcY1, *dstY0, *dstY1);
1805 ASSERT(*dstX0
>= dstXmin
);
1806 ASSERT(*dstX0
<= dstXmax
);
1807 ASSERT(*dstX1
>= dstXmin
);
1808 ASSERT(*dstX1
<= dstXmax
);
1810 ASSERT(*dstY0
>= dstYmin
);
1811 ASSERT(*dstY0
<= dstYmax
);
1812 ASSERT(*dstY1
>= dstYmin
);
1813 ASSERT(*dstY1
<= dstYmax
);
1815 ASSERT(*srcX0
>= srcXmin
);
1816 ASSERT(*srcX0
<= srcXmax
);
1817 ASSERT(*srcX1
>= srcXmin
);
1818 ASSERT(*srcX1
<= srcXmax
);
1820 ASSERT(*srcY0
>= srcYmin
);
1821 ASSERT(*srcY0
<= srcYmax
);
1822 ASSERT(*srcY1
>= srcYmin
);
1823 ASSERT(*srcY1
<= srcYmax
);