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.
42 * Normally, BYTE_TO_FLOAT(0) returns 0.00392 That causes problems when
43 * we later convert the float to a packed integer value (such as for
44 * GL_RGB5_A1) because we'll wind up with a non-zero value.
46 * We redefine the macros here so zero is handled correctly.
49 #define BYTE_TO_FLOAT(B) ((B) == 0 ? 0.0F : ((2.0F * (B) + 1.0F) * (1.0F/255.0F)))
52 #define SHORT_TO_FLOAT(S) ((S) == 0 ? 0.0F : ((2.0F * (S) + 1.0F) * (1.0F/65535.0F)))
56 /** Compute ceiling of integer quotient of A divided by B. */
57 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
61 * \return GL_TRUE if type is packed pixel type, GL_FALSE otherwise.
64 _mesa_type_is_packed(GLenum type
)
67 case GL_UNSIGNED_BYTE_3_3_2
:
68 case GL_UNSIGNED_BYTE_2_3_3_REV
:
69 case GL_UNSIGNED_SHORT_5_6_5
:
70 case GL_UNSIGNED_SHORT_5_6_5_REV
:
71 case GL_UNSIGNED_SHORT_4_4_4_4
:
72 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
73 case GL_UNSIGNED_SHORT_5_5_5_1
:
74 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
75 case GL_UNSIGNED_INT_8_8_8_8
:
76 case GL_UNSIGNED_INT_8_8_8_8_REV
:
77 case GL_UNSIGNED_INT_10_10_10_2
:
78 case GL_UNSIGNED_INT_2_10_10_10_REV
:
79 case GL_UNSIGNED_SHORT_8_8_MESA
:
80 case GL_UNSIGNED_SHORT_8_8_REV_MESA
:
81 case GL_UNSIGNED_INT_24_8_EXT
:
91 * Flip the order of the 2 bytes in each word in the given array.
94 * \param n number of words.
97 _mesa_swap2( GLushort
*p
, GLuint n
)
100 for (i
= 0; i
< n
; i
++) {
101 p
[i
] = (p
[i
] >> 8) | ((p
[i
] << 8) & 0xff00);
108 * Flip the order of the 4 bytes in each word in the given array.
111 _mesa_swap4( GLuint
*p
, GLuint n
)
114 for (i
= 0; i
< n
; i
++) {
117 | ((b
>> 8) & 0xff00)
118 | ((b
<< 8) & 0xff0000)
119 | ((b
<< 24) & 0xff000000);
126 * Get the size of a GL data type.
128 * \param type GL data type.
130 * \return the size, in bytes, of the given data type, 0 if a GL_BITMAP, or -1
131 * if an invalid type enum.
134 _mesa_sizeof_type( GLenum type
)
139 case GL_UNSIGNED_BYTE
:
140 return sizeof(GLubyte
);
142 return sizeof(GLbyte
);
143 case GL_UNSIGNED_SHORT
:
144 return sizeof(GLushort
);
146 return sizeof(GLshort
);
147 case GL_UNSIGNED_INT
:
148 return sizeof(GLuint
);
150 return sizeof(GLint
);
152 return sizeof(GLfloat
);
154 return sizeof(GLdouble
);
155 case GL_HALF_FLOAT_ARB
:
156 return sizeof(GLhalfARB
);
164 * Same as _mesa_sizeof_type() but also accepting the packed pixel
168 _mesa_sizeof_packed_type( GLenum type
)
173 case GL_UNSIGNED_BYTE
:
174 return sizeof(GLubyte
);
176 return sizeof(GLbyte
);
177 case GL_UNSIGNED_SHORT
:
178 return sizeof(GLushort
);
180 return sizeof(GLshort
);
181 case GL_UNSIGNED_INT
:
182 return sizeof(GLuint
);
184 return sizeof(GLint
);
185 case GL_HALF_FLOAT_ARB
:
186 return sizeof(GLhalfARB
);
188 return sizeof(GLfloat
);
189 case GL_UNSIGNED_BYTE_3_3_2
:
190 return sizeof(GLubyte
);
191 case GL_UNSIGNED_BYTE_2_3_3_REV
:
192 return sizeof(GLubyte
);
193 case GL_UNSIGNED_SHORT_5_6_5
:
194 return sizeof(GLushort
);
195 case GL_UNSIGNED_SHORT_5_6_5_REV
:
196 return sizeof(GLushort
);
197 case GL_UNSIGNED_SHORT_4_4_4_4
:
198 return sizeof(GLushort
);
199 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
200 return sizeof(GLushort
);
201 case GL_UNSIGNED_SHORT_5_5_5_1
:
202 return sizeof(GLushort
);
203 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
204 return sizeof(GLushort
);
205 case GL_UNSIGNED_INT_8_8_8_8
:
206 return sizeof(GLuint
);
207 case GL_UNSIGNED_INT_8_8_8_8_REV
:
208 return sizeof(GLuint
);
209 case GL_UNSIGNED_INT_10_10_10_2
:
210 return sizeof(GLuint
);
211 case GL_UNSIGNED_INT_2_10_10_10_REV
:
212 return sizeof(GLuint
);
213 case GL_UNSIGNED_SHORT_8_8_MESA
:
214 case GL_UNSIGNED_SHORT_8_8_REV_MESA
:
215 return sizeof(GLushort
);
216 case GL_UNSIGNED_INT_24_8_EXT
:
217 return sizeof(GLuint
);
225 * Get the number of components in a pixel format.
227 * \param format pixel format.
229 * \return the number of components in the given format, or -1 if a bad format.
232 _mesa_components_in_format( GLenum format
)
236 case GL_COLOR_INDEX1_EXT
:
237 case GL_COLOR_INDEX2_EXT
:
238 case GL_COLOR_INDEX4_EXT
:
239 case GL_COLOR_INDEX8_EXT
:
240 case GL_COLOR_INDEX12_EXT
:
241 case GL_COLOR_INDEX16_EXT
:
242 case GL_STENCIL_INDEX
:
243 case GL_DEPTH_COMPONENT
:
245 case GL_RED_INTEGER_EXT
:
247 case GL_GREEN_INTEGER_EXT
:
249 case GL_BLUE_INTEGER_EXT
:
251 case GL_ALPHA_INTEGER_EXT
:
253 case GL_LUMINANCE_INTEGER_EXT
:
256 case GL_LUMINANCE_ALPHA
:
257 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
261 case GL_RGB_INTEGER_EXT
:
264 case GL_RGBA_INTEGER_EXT
:
274 case GL_DEPTH_STENCIL_EXT
:
286 * Get the bytes per pixel of pixel format type pair.
288 * \param format pixel format.
289 * \param type pixel type.
291 * \return bytes per pixel, or -1 if a bad format or type was given.
294 _mesa_bytes_per_pixel( GLenum format
, GLenum type
)
296 GLint comps
= _mesa_components_in_format( format
);
302 return 0; /* special case */
304 case GL_UNSIGNED_BYTE
:
305 return comps
* sizeof(GLubyte
);
307 case GL_UNSIGNED_SHORT
:
308 return comps
* sizeof(GLshort
);
310 case GL_UNSIGNED_INT
:
311 return comps
* sizeof(GLint
);
313 return comps
* sizeof(GLfloat
);
314 case GL_HALF_FLOAT_ARB
:
315 return comps
* sizeof(GLhalfARB
);
316 case GL_UNSIGNED_BYTE_3_3_2
:
317 case GL_UNSIGNED_BYTE_2_3_3_REV
:
318 if (format
== GL_RGB
|| format
== GL_BGR
||
319 format
== GL_RGB_INTEGER_EXT
|| format
== GL_BGR_INTEGER_EXT
)
320 return sizeof(GLubyte
);
322 return -1; /* error */
323 case GL_UNSIGNED_SHORT_5_6_5
:
324 case GL_UNSIGNED_SHORT_5_6_5_REV
:
325 if (format
== GL_RGB
|| format
== GL_BGR
||
326 format
== GL_RGB_INTEGER_EXT
|| format
== GL_BGR_INTEGER_EXT
)
327 return sizeof(GLushort
);
329 return -1; /* error */
330 case GL_UNSIGNED_SHORT_4_4_4_4
:
331 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
332 case GL_UNSIGNED_SHORT_5_5_5_1
:
333 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
334 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
||
335 format
== GL_RGBA_INTEGER_EXT
|| format
== GL_BGRA_INTEGER_EXT
)
336 return sizeof(GLushort
);
339 case GL_UNSIGNED_INT_8_8_8_8
:
340 case GL_UNSIGNED_INT_8_8_8_8_REV
:
341 case GL_UNSIGNED_INT_10_10_10_2
:
342 case GL_UNSIGNED_INT_2_10_10_10_REV
:
343 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
||
344 format
== GL_RGBA_INTEGER_EXT
|| format
== GL_BGRA_INTEGER_EXT
)
345 return sizeof(GLuint
);
348 case GL_UNSIGNED_SHORT_8_8_MESA
:
349 case GL_UNSIGNED_SHORT_8_8_REV_MESA
:
350 if (format
== GL_YCBCR_MESA
)
351 return sizeof(GLushort
);
354 case GL_UNSIGNED_INT_24_8_EXT
:
355 if (format
== GL_DEPTH_STENCIL_EXT
)
356 return sizeof(GLuint
);
366 * Test for a legal pixel format and type.
368 * \param format pixel format.
369 * \param type pixel type.
371 * \return GL_TRUE if the given pixel format and type are legal, or GL_FALSE
375 _mesa_is_legal_format_and_type(const struct gl_context
*ctx
,
376 GLenum format
, GLenum type
)
380 case GL_STENCIL_INDEX
:
384 case GL_UNSIGNED_BYTE
:
386 case GL_UNSIGNED_SHORT
:
388 case GL_UNSIGNED_INT
:
391 case GL_HALF_FLOAT_ARB
:
392 return ctx
->Extensions
.ARB_half_float_pixel
;
400 #if 0 /* not legal! see table 3.6 of the 1.5 spec */
404 case GL_LUMINANCE_ALPHA
:
405 case GL_DEPTH_COMPONENT
:
408 case GL_UNSIGNED_BYTE
:
410 case GL_UNSIGNED_SHORT
:
412 case GL_UNSIGNED_INT
:
415 case GL_HALF_FLOAT_ARB
:
416 return ctx
->Extensions
.ARB_half_float_pixel
;
421 if (!ctx
->Extensions
.ARB_texture_rg
)
426 case GL_UNSIGNED_BYTE
:
428 case GL_UNSIGNED_SHORT
:
430 case GL_UNSIGNED_INT
:
433 case GL_HALF_FLOAT_ARB
:
434 return ctx
->Extensions
.ARB_half_float_pixel
;
441 case GL_UNSIGNED_BYTE
:
443 case GL_UNSIGNED_SHORT
:
445 case GL_UNSIGNED_INT
:
447 case GL_UNSIGNED_BYTE_3_3_2
:
448 case GL_UNSIGNED_BYTE_2_3_3_REV
:
449 case GL_UNSIGNED_SHORT_5_6_5
:
450 case GL_UNSIGNED_SHORT_5_6_5_REV
:
452 case GL_HALF_FLOAT_ARB
:
453 return ctx
->Extensions
.ARB_half_float_pixel
;
459 /* NOTE: no packed types are supported with BGR. That's
460 * intentional, according to the GL spec.
463 case GL_UNSIGNED_BYTE
:
465 case GL_UNSIGNED_SHORT
:
467 case GL_UNSIGNED_INT
:
470 case GL_HALF_FLOAT_ARB
:
471 return ctx
->Extensions
.ARB_half_float_pixel
;
480 case GL_UNSIGNED_BYTE
:
482 case GL_UNSIGNED_SHORT
:
484 case GL_UNSIGNED_INT
:
486 case GL_UNSIGNED_SHORT_4_4_4_4
:
487 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
488 case GL_UNSIGNED_SHORT_5_5_5_1
:
489 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
490 case GL_UNSIGNED_INT_8_8_8_8
:
491 case GL_UNSIGNED_INT_8_8_8_8_REV
:
492 case GL_UNSIGNED_INT_10_10_10_2
:
493 case GL_UNSIGNED_INT_2_10_10_10_REV
:
495 case GL_HALF_FLOAT_ARB
:
496 return ctx
->Extensions
.ARB_half_float_pixel
;
501 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
||
502 type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
)
506 case GL_DEPTH_STENCIL_EXT
:
507 if (ctx
->Extensions
.EXT_packed_depth_stencil
508 && type
== GL_UNSIGNED_INT_24_8_EXT
)
516 case GL_UNSIGNED_BYTE
:
518 case GL_UNSIGNED_SHORT
:
520 case GL_UNSIGNED_INT
:
527 /* integer-valued formats */
528 case GL_RED_INTEGER_EXT
:
529 case GL_GREEN_INTEGER_EXT
:
530 case GL_BLUE_INTEGER_EXT
:
531 case GL_ALPHA_INTEGER_EXT
:
534 case GL_UNSIGNED_BYTE
:
536 case GL_UNSIGNED_SHORT
:
538 case GL_UNSIGNED_INT
:
539 return ctx
->Extensions
.EXT_texture_integer
;
544 case GL_RGB_INTEGER_EXT
:
547 case GL_UNSIGNED_BYTE
:
549 case GL_UNSIGNED_SHORT
:
551 case GL_UNSIGNED_INT
:
552 case GL_UNSIGNED_BYTE_3_3_2
:
553 case GL_UNSIGNED_BYTE_2_3_3_REV
:
554 case GL_UNSIGNED_SHORT_5_6_5
:
555 case GL_UNSIGNED_SHORT_5_6_5_REV
:
556 return ctx
->Extensions
.EXT_texture_integer
;
561 case GL_BGR_INTEGER_EXT
:
564 case GL_UNSIGNED_BYTE
:
566 case GL_UNSIGNED_SHORT
:
568 case GL_UNSIGNED_INT
:
569 /* NOTE: no packed formats w/ BGR format */
570 return ctx
->Extensions
.EXT_texture_integer
;
575 case GL_RGBA_INTEGER_EXT
:
576 case GL_BGRA_INTEGER_EXT
:
579 case GL_UNSIGNED_BYTE
:
581 case GL_UNSIGNED_SHORT
:
583 case GL_UNSIGNED_INT
:
584 case GL_UNSIGNED_SHORT_4_4_4_4
:
585 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
586 case GL_UNSIGNED_SHORT_5_5_5_1
:
587 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
588 case GL_UNSIGNED_INT_8_8_8_8
:
589 case GL_UNSIGNED_INT_8_8_8_8_REV
:
590 case GL_UNSIGNED_INT_10_10_10_2
:
591 case GL_UNSIGNED_INT_2_10_10_10_REV
:
592 return ctx
->Extensions
.EXT_texture_integer
;
597 case GL_LUMINANCE_INTEGER_EXT
:
598 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
601 case GL_UNSIGNED_BYTE
:
603 case GL_UNSIGNED_SHORT
:
605 case GL_UNSIGNED_INT
:
606 return ctx
->Extensions
.EXT_texture_integer
;
619 * Test if the given image format is a color/RGBA format (i.e., not color
620 * index, depth, stencil, etc).
621 * \param format the image format value (may by an internal texture format)
622 * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
625 _mesa_is_color_format(GLenum format
)
643 case GL_LUMINANCE_ALPHA
:
644 case GL_LUMINANCE4_ALPHA4
:
645 case GL_LUMINANCE6_ALPHA2
:
646 case GL_LUMINANCE8_ALPHA8
:
647 case GL_LUMINANCE12_ALPHA4
:
648 case GL_LUMINANCE12_ALPHA12
:
649 case GL_LUMINANCE16_ALPHA16
:
681 /* float texture formats */
682 case GL_ALPHA16F_ARB
:
683 case GL_ALPHA32F_ARB
:
684 case GL_LUMINANCE16F_ARB
:
685 case GL_LUMINANCE32F_ARB
:
686 case GL_LUMINANCE_ALPHA16F_ARB
:
687 case GL_LUMINANCE_ALPHA32F_ARB
:
688 case GL_INTENSITY16F_ARB
:
689 case GL_INTENSITY32F_ARB
:
698 /* compressed formats */
699 case GL_COMPRESSED_ALPHA
:
700 case GL_COMPRESSED_LUMINANCE
:
701 case GL_COMPRESSED_LUMINANCE_ALPHA
:
702 case GL_COMPRESSED_INTENSITY
:
703 case GL_COMPRESSED_RED
:
704 case GL_COMPRESSED_RG
:
705 case GL_COMPRESSED_RGB
:
706 case GL_COMPRESSED_RGBA
:
711 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
712 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
713 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
714 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
715 case GL_COMPRESSED_RGB_FXT1_3DFX
:
716 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
717 #if FEATURE_EXT_texture_sRGB
720 case GL_SRGB_ALPHA_EXT
:
721 case GL_SRGB8_ALPHA8_EXT
:
722 case GL_SLUMINANCE_ALPHA_EXT
:
723 case GL_SLUMINANCE8_ALPHA8_EXT
:
724 case GL_SLUMINANCE_EXT
:
725 case GL_SLUMINANCE8_EXT
:
726 case GL_COMPRESSED_SRGB_EXT
:
727 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
728 case GL_COMPRESSED_SRGB_ALPHA_EXT
:
729 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
730 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
731 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
732 case GL_COMPRESSED_SLUMINANCE_EXT
:
733 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT
:
734 #endif /* FEATURE_EXT_texture_sRGB */
735 case GL_COMPRESSED_RED_RGTC1
:
736 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
737 case GL_COMPRESSED_RG_RGTC2
:
738 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
739 /* signed, normalized texture formats */
742 /* generic integer formats */
743 case GL_RED_INTEGER_EXT
:
744 case GL_GREEN_INTEGER_EXT
:
745 case GL_BLUE_INTEGER_EXT
:
746 case GL_ALPHA_INTEGER_EXT
:
747 case GL_RGB_INTEGER_EXT
:
748 case GL_RGBA_INTEGER_EXT
:
749 case GL_BGR_INTEGER_EXT
:
750 case GL_BGRA_INTEGER_EXT
:
751 case GL_LUMINANCE_INTEGER_EXT
:
752 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
753 /* sized integer formats */
754 case GL_RGBA32UI_EXT
:
756 case GL_ALPHA32UI_EXT
:
757 case GL_INTENSITY32UI_EXT
:
758 case GL_LUMINANCE32UI_EXT
:
759 case GL_LUMINANCE_ALPHA32UI_EXT
:
760 case GL_RGBA16UI_EXT
:
762 case GL_ALPHA16UI_EXT
:
763 case GL_INTENSITY16UI_EXT
:
764 case GL_LUMINANCE16UI_EXT
:
765 case GL_LUMINANCE_ALPHA16UI_EXT
:
768 case GL_ALPHA8UI_EXT
:
769 case GL_INTENSITY8UI_EXT
:
770 case GL_LUMINANCE8UI_EXT
:
771 case GL_LUMINANCE_ALPHA8UI_EXT
:
774 case GL_ALPHA32I_EXT
:
775 case GL_INTENSITY32I_EXT
:
776 case GL_LUMINANCE32I_EXT
:
777 case GL_LUMINANCE_ALPHA32I_EXT
:
780 case GL_ALPHA16I_EXT
:
781 case GL_INTENSITY16I_EXT
:
782 case GL_LUMINANCE16I_EXT
:
783 case GL_LUMINANCE_ALPHA16I_EXT
:
787 case GL_INTENSITY8I_EXT
:
788 case GL_LUMINANCE8I_EXT
:
789 case GL_LUMINANCE_ALPHA8I_EXT
:
791 case GL_YCBCR_MESA
: /* not considered to be RGB */
800 * Test if the given image format is a color index format.
803 _mesa_is_index_format(GLenum format
)
807 case GL_COLOR_INDEX1_EXT
:
808 case GL_COLOR_INDEX2_EXT
:
809 case GL_COLOR_INDEX4_EXT
:
810 case GL_COLOR_INDEX8_EXT
:
811 case GL_COLOR_INDEX12_EXT
:
812 case GL_COLOR_INDEX16_EXT
:
821 * Test if the given image format is a depth component format.
824 _mesa_is_depth_format(GLenum format
)
827 case GL_DEPTH_COMPONENT
:
828 case GL_DEPTH_COMPONENT16
:
829 case GL_DEPTH_COMPONENT24
:
830 case GL_DEPTH_COMPONENT32
:
839 * Test if the given image format is a stencil format.
842 _mesa_is_stencil_format(GLenum format
)
845 case GL_STENCIL_INDEX
:
846 case GL_DEPTH_STENCIL
:
855 * Test if the given image format is a YCbCr format.
858 _mesa_is_ycbcr_format(GLenum format
)
870 * Test if the given image format is a depth+stencil format.
873 _mesa_is_depthstencil_format(GLenum format
)
876 case GL_DEPTH24_STENCIL8_EXT
:
877 case GL_DEPTH_STENCIL_EXT
:
886 * Test if the given image format is a depth or stencil format.
889 _mesa_is_depth_or_stencil_format(GLenum format
)
892 case GL_DEPTH_COMPONENT
:
893 case GL_DEPTH_COMPONENT16
:
894 case GL_DEPTH_COMPONENT24
:
895 case GL_DEPTH_COMPONENT32
:
896 case GL_STENCIL_INDEX
:
897 case GL_STENCIL_INDEX1_EXT
:
898 case GL_STENCIL_INDEX4_EXT
:
899 case GL_STENCIL_INDEX8_EXT
:
900 case GL_STENCIL_INDEX16_EXT
:
901 case GL_DEPTH_STENCIL_EXT
:
902 case GL_DEPTH24_STENCIL8_EXT
:
911 * Test if the given image format is a dudv format.
914 _mesa_is_dudv_format(GLenum format
)
927 * Test if the given format is an integer (non-normalized) format.
930 _mesa_is_integer_format(GLenum format
)
933 /* generic integer formats */
934 case GL_RED_INTEGER_EXT
:
935 case GL_GREEN_INTEGER_EXT
:
936 case GL_BLUE_INTEGER_EXT
:
937 case GL_ALPHA_INTEGER_EXT
:
938 case GL_RGB_INTEGER_EXT
:
939 case GL_RGBA_INTEGER_EXT
:
940 case GL_BGR_INTEGER_EXT
:
941 case GL_BGRA_INTEGER_EXT
:
942 case GL_LUMINANCE_INTEGER_EXT
:
943 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
944 /* specific integer formats */
945 case GL_RGBA32UI_EXT
:
947 case GL_ALPHA32UI_EXT
:
948 case GL_INTENSITY32UI_EXT
:
949 case GL_LUMINANCE32UI_EXT
:
950 case GL_LUMINANCE_ALPHA32UI_EXT
:
951 case GL_RGBA16UI_EXT
:
953 case GL_ALPHA16UI_EXT
:
954 case GL_INTENSITY16UI_EXT
:
955 case GL_LUMINANCE16UI_EXT
:
956 case GL_LUMINANCE_ALPHA16UI_EXT
:
959 case GL_ALPHA8UI_EXT
:
960 case GL_INTENSITY8UI_EXT
:
961 case GL_LUMINANCE8UI_EXT
:
962 case GL_LUMINANCE_ALPHA8UI_EXT
:
965 case GL_ALPHA32I_EXT
:
966 case GL_INTENSITY32I_EXT
:
967 case GL_LUMINANCE32I_EXT
:
968 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
:
978 case GL_INTENSITY8I_EXT
:
979 case GL_LUMINANCE8I_EXT
:
980 case GL_LUMINANCE_ALPHA8I_EXT
:
989 * Test if an image format is a supported compressed format.
990 * \param format the internal format token provided by the user.
991 * \return GL_TRUE if compressed, GL_FALSE if uncompressed
994 _mesa_is_compressed_format(struct gl_context
*ctx
, GLenum format
)
997 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
998 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
999 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
1000 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
1001 return ctx
->Extensions
.EXT_texture_compression_s3tc
;
1006 return ctx
->Extensions
.S3_s3tc
;
1007 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
1008 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
1009 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
1010 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
1011 return ctx
->Extensions
.EXT_texture_sRGB
1012 && ctx
->Extensions
.EXT_texture_compression_s3tc
;
1013 case GL_COMPRESSED_RGB_FXT1_3DFX
:
1014 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
1015 return ctx
->Extensions
.TDFX_texture_compression_FXT1
;
1016 case GL_COMPRESSED_RED_RGTC1
:
1017 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
1018 case GL_COMPRESSED_RG_RGTC2
:
1019 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
1020 return ctx
->Extensions
.ARB_texture_compression_rgtc
;
1028 * Return the address of a specific pixel in an image (1D, 2D or 3D).
1030 * Pixel unpacking/packing parameters are observed according to \p packing.
1032 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
1033 * \param image starting address of image data
1034 * \param width the image width
1035 * \param height theimage height
1036 * \param format the pixel format
1037 * \param type the pixel data type
1038 * \param packing the pixelstore attributes
1039 * \param img which image in the volume (0 for 1D or 2D images)
1040 * \param row row of pixel in the image (0 for 1D images)
1041 * \param column column of pixel in the image
1043 * \return address of pixel on success, or NULL on error.
1045 * \sa gl_pixelstore_attrib.
1048 _mesa_image_address( GLuint dimensions
,
1049 const struct gl_pixelstore_attrib
*packing
,
1050 const GLvoid
*image
,
1051 GLsizei width
, GLsizei height
,
1052 GLenum format
, GLenum type
,
1053 GLint img
, GLint row
, GLint column
)
1055 GLint alignment
; /* 1, 2 or 4 */
1056 GLint pixels_per_row
;
1057 GLint rows_per_image
;
1060 GLint skipimages
; /* for 3-D volume images */
1061 GLubyte
*pixel_addr
;
1063 ASSERT(dimensions
>= 1 && dimensions
<= 3);
1065 alignment
= packing
->Alignment
;
1066 if (packing
->RowLength
> 0) {
1067 pixels_per_row
= packing
->RowLength
;
1070 pixels_per_row
= width
;
1072 if (packing
->ImageHeight
> 0) {
1073 rows_per_image
= packing
->ImageHeight
;
1076 rows_per_image
= height
;
1079 skippixels
= packing
->SkipPixels
;
1080 /* Note: SKIP_ROWS _is_ used for 1D images */
1081 skiprows
= packing
->SkipRows
;
1082 /* Note: SKIP_IMAGES is only used for 3D images */
1083 skipimages
= (dimensions
== 3) ? packing
->SkipImages
: 0;
1085 if (type
== GL_BITMAP
) {
1087 GLint comp_per_pixel
; /* components per pixel */
1088 GLint bytes_per_comp
; /* bytes per component */
1089 GLint bytes_per_row
;
1090 GLint bytes_per_image
;
1092 /* Compute bytes per component */
1093 bytes_per_comp
= _mesa_sizeof_packed_type( type
);
1094 if (bytes_per_comp
< 0) {
1098 /* Compute number of components per pixel */
1099 comp_per_pixel
= _mesa_components_in_format( format
);
1100 if (comp_per_pixel
< 0) {
1104 bytes_per_row
= alignment
1105 * CEILING( comp_per_pixel
*pixels_per_row
, 8*alignment
);
1107 bytes_per_image
= bytes_per_row
* rows_per_image
;
1109 pixel_addr
= (GLubyte
*) image
1110 + (skipimages
+ img
) * bytes_per_image
1111 + (skiprows
+ row
) * bytes_per_row
1112 + (skippixels
+ column
) / 8;
1115 /* Non-BITMAP data */
1116 GLint bytes_per_pixel
, bytes_per_row
, remainder
, bytes_per_image
;
1119 bytes_per_pixel
= _mesa_bytes_per_pixel( format
, type
);
1121 /* The pixel type and format should have been error checked earlier */
1122 assert(bytes_per_pixel
> 0);
1124 bytes_per_row
= pixels_per_row
* bytes_per_pixel
;
1125 remainder
= bytes_per_row
% alignment
;
1127 bytes_per_row
+= (alignment
- remainder
);
1129 ASSERT(bytes_per_row
% alignment
== 0);
1131 bytes_per_image
= bytes_per_row
* rows_per_image
;
1133 if (packing
->Invert
) {
1134 /* set pixel_addr to the last row */
1135 topOfImage
= bytes_per_row
* (height
- 1);
1136 bytes_per_row
= -bytes_per_row
;
1142 /* compute final pixel address */
1143 pixel_addr
= (GLubyte
*) image
1144 + (skipimages
+ img
) * bytes_per_image
1146 + (skiprows
+ row
) * bytes_per_row
1147 + (skippixels
+ column
) * bytes_per_pixel
;
1150 return (GLvoid
*) pixel_addr
;
1155 _mesa_image_address1d( const struct gl_pixelstore_attrib
*packing
,
1156 const GLvoid
*image
,
1158 GLenum format
, GLenum type
,
1161 return _mesa_image_address(1, packing
, image
, width
, 1,
1162 format
, type
, 0, 0, column
);
1167 _mesa_image_address2d( const struct gl_pixelstore_attrib
*packing
,
1168 const GLvoid
*image
,
1169 GLsizei width
, GLsizei height
,
1170 GLenum format
, GLenum type
,
1171 GLint row
, GLint column
)
1173 return _mesa_image_address(2, packing
, image
, width
, height
,
1174 format
, type
, 0, row
, column
);
1179 _mesa_image_address3d( const struct gl_pixelstore_attrib
*packing
,
1180 const GLvoid
*image
,
1181 GLsizei width
, GLsizei height
,
1182 GLenum format
, GLenum type
,
1183 GLint img
, GLint row
, GLint column
)
1185 return _mesa_image_address(3, packing
, image
, width
, height
,
1186 format
, type
, img
, row
, column
);
1192 * Compute the stride (in bytes) between image rows.
1194 * \param packing the pixelstore attributes
1195 * \param width image width.
1196 * \param format pixel format.
1197 * \param type pixel data type.
1199 * \return the stride in bytes for the given parameters, or -1 if error
1202 _mesa_image_row_stride( const struct gl_pixelstore_attrib
*packing
,
1203 GLint width
, GLenum format
, GLenum type
)
1205 GLint bytesPerRow
, remainder
;
1209 if (type
== GL_BITMAP
) {
1210 if (packing
->RowLength
== 0) {
1211 bytesPerRow
= (width
+ 7) / 8;
1214 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
1218 /* Non-BITMAP data */
1219 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
;
1231 if (remainder
> 0) {
1232 bytesPerRow
+= (packing
->Alignment
- remainder
);
1235 if (packing
->Invert
) {
1236 /* negate the bytes per row (negative row stride) */
1237 bytesPerRow
= -bytesPerRow
;
1245 * Compute the stride between images in a 3D texture (in bytes) for the given
1246 * pixel packing parameters and image width, format and type.
1249 _mesa_image_image_stride( const struct gl_pixelstore_attrib
*packing
,
1250 GLint width
, GLint height
,
1251 GLenum format
, GLenum type
)
1253 GLint bytesPerRow
, bytesPerImage
, remainder
;
1257 if (type
== GL_BITMAP
) {
1258 if (packing
->RowLength
== 0) {
1259 bytesPerRow
= (width
+ 7) / 8;
1262 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
1266 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1268 if (bytesPerPixel
<= 0)
1269 return -1; /* error */
1270 if (packing
->RowLength
== 0) {
1271 bytesPerRow
= bytesPerPixel
* width
;
1274 bytesPerRow
= bytesPerPixel
* packing
->RowLength
;
1278 remainder
= bytesPerRow
% packing
->Alignment
;
1280 bytesPerRow
+= (packing
->Alignment
- remainder
);
1282 if (packing
->ImageHeight
== 0)
1283 bytesPerImage
= bytesPerRow
* height
;
1285 bytesPerImage
= bytesPerRow
* packing
->ImageHeight
;
1287 return bytesPerImage
;
1293 * "Expand" a bitmap from 1-bit per pixel to 8-bits per pixel.
1294 * This is typically used to convert a bitmap into a GLubyte/pixel texture.
1295 * "On" bits will set texels to \p onValue.
1296 * "Off" bits will not modify texels.
1297 * \param width src bitmap width in pixels
1298 * \param height src bitmap height in pixels
1299 * \param unpack bitmap unpacking state
1300 * \param bitmap the src bitmap data
1301 * \param destBuffer start of dest buffer
1302 * \param destStride row stride in dest buffer
1303 * \param onValue if bit is 1, set destBuffer pixel to this value
1306 _mesa_expand_bitmap(GLsizei width
, GLsizei height
,
1307 const struct gl_pixelstore_attrib
*unpack
,
1308 const GLubyte
*bitmap
,
1309 GLubyte
*destBuffer
, GLint destStride
,
1312 const GLubyte
*srcRow
= (const GLubyte
*)
1313 _mesa_image_address2d(unpack
, bitmap
, width
, height
,
1314 GL_COLOR_INDEX
, GL_BITMAP
, 0, 0);
1315 const GLint srcStride
= _mesa_image_row_stride(unpack
, width
,
1316 GL_COLOR_INDEX
, GL_BITMAP
);
1319 #define SET_PIXEL(COL, ROW) \
1320 destBuffer[(ROW) * destStride + (COL)] = onValue;
1322 for (row
= 0; row
< height
; row
++) {
1323 const GLubyte
*src
= srcRow
;
1325 if (unpack
->LsbFirst
) {
1327 GLubyte mask
= 1U << (unpack
->SkipPixels
& 0x7);
1328 for (col
= 0; col
< width
; col
++) {
1331 SET_PIXEL(col
, row
);
1343 /* get ready for next row */
1349 GLubyte mask
= 128U >> (unpack
->SkipPixels
& 0x7);
1350 for (col
= 0; col
< width
; col
++) {
1353 SET_PIXEL(col
, row
);
1365 /* get ready for next row */
1370 srcRow
+= srcStride
;
1380 * Convert an array of RGBA colors from one datatype to another.
1381 * NOTE: src may equal dst. In that case, we use a temporary buffer.
1384 _mesa_convert_colors(GLenum srcType
, const GLvoid
*src
,
1385 GLenum dstType
, GLvoid
*dst
,
1386 GLuint count
, const GLubyte mask
[])
1388 GLuint tempBuffer
[MAX_WIDTH
][4];
1389 const GLboolean useTemp
= (src
== dst
);
1391 ASSERT(srcType
!= dstType
);
1394 case GL_UNSIGNED_BYTE
:
1395 if (dstType
== GL_UNSIGNED_SHORT
) {
1396 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
1397 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
1399 for (i
= 0; i
< count
; i
++) {
1400 if (!mask
|| mask
[i
]) {
1401 dst2
[i
][RCOMP
] = UBYTE_TO_USHORT(src1
[i
][RCOMP
]);
1402 dst2
[i
][GCOMP
] = UBYTE_TO_USHORT(src1
[i
][GCOMP
]);
1403 dst2
[i
][BCOMP
] = UBYTE_TO_USHORT(src1
[i
][BCOMP
]);
1404 dst2
[i
][ACOMP
] = UBYTE_TO_USHORT(src1
[i
][ACOMP
]);
1408 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
1411 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
1412 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
1414 ASSERT(dstType
== GL_FLOAT
);
1415 for (i
= 0; i
< count
; i
++) {
1416 if (!mask
|| mask
[i
]) {
1417 dst4
[i
][RCOMP
] = UBYTE_TO_FLOAT(src1
[i
][RCOMP
]);
1418 dst4
[i
][GCOMP
] = UBYTE_TO_FLOAT(src1
[i
][GCOMP
]);
1419 dst4
[i
][BCOMP
] = UBYTE_TO_FLOAT(src1
[i
][BCOMP
]);
1420 dst4
[i
][ACOMP
] = UBYTE_TO_FLOAT(src1
[i
][ACOMP
]);
1424 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
1427 case GL_UNSIGNED_SHORT
:
1428 if (dstType
== GL_UNSIGNED_BYTE
) {
1429 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
1430 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
1432 for (i
= 0; i
< count
; i
++) {
1433 if (!mask
|| mask
[i
]) {
1434 dst1
[i
][RCOMP
] = USHORT_TO_UBYTE(src2
[i
][RCOMP
]);
1435 dst1
[i
][GCOMP
] = USHORT_TO_UBYTE(src2
[i
][GCOMP
]);
1436 dst1
[i
][BCOMP
] = USHORT_TO_UBYTE(src2
[i
][BCOMP
]);
1437 dst1
[i
][ACOMP
] = USHORT_TO_UBYTE(src2
[i
][ACOMP
]);
1441 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
1444 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
1445 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
1447 ASSERT(dstType
== GL_FLOAT
);
1448 for (i
= 0; i
< count
; i
++) {
1449 if (!mask
|| mask
[i
]) {
1450 dst4
[i
][RCOMP
] = USHORT_TO_FLOAT(src2
[i
][RCOMP
]);
1451 dst4
[i
][GCOMP
] = USHORT_TO_FLOAT(src2
[i
][GCOMP
]);
1452 dst4
[i
][BCOMP
] = USHORT_TO_FLOAT(src2
[i
][BCOMP
]);
1453 dst4
[i
][ACOMP
] = USHORT_TO_FLOAT(src2
[i
][ACOMP
]);
1457 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
1461 if (dstType
== GL_UNSIGNED_BYTE
) {
1462 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
1463 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
1465 for (i
= 0; i
< count
; i
++) {
1466 if (!mask
|| mask
[i
]) {
1467 UNCLAMPED_FLOAT_TO_UBYTE(dst1
[i
][RCOMP
], src4
[i
][RCOMP
]);
1468 UNCLAMPED_FLOAT_TO_UBYTE(dst1
[i
][GCOMP
], src4
[i
][GCOMP
]);
1469 UNCLAMPED_FLOAT_TO_UBYTE(dst1
[i
][BCOMP
], src4
[i
][BCOMP
]);
1470 UNCLAMPED_FLOAT_TO_UBYTE(dst1
[i
][ACOMP
], src4
[i
][ACOMP
]);
1474 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
1477 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
1478 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
1480 ASSERT(dstType
== GL_UNSIGNED_SHORT
);
1481 for (i
= 0; i
< count
; i
++) {
1482 if (!mask
|| mask
[i
]) {
1483 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][RCOMP
], src4
[i
][RCOMP
]);
1484 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][GCOMP
], src4
[i
][GCOMP
]);
1485 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][BCOMP
], src4
[i
][BCOMP
]);
1486 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][ACOMP
], src4
[i
][ACOMP
]);
1490 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
1494 _mesa_problem(NULL
, "Invalid datatype in _mesa_convert_colors");
1502 * Perform basic clipping for glDrawPixels. The image's position and size
1503 * and the unpack SkipPixels and SkipRows are adjusted so that the image
1504 * region is entirely within the window and scissor bounds.
1505 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
1506 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
1507 * we'll actually write. Beforehand, *destY-1 is the first drawing row.
1509 * \return GL_TRUE if image is ready for drawing or
1510 * GL_FALSE if image was completely clipped away (draw nothing)
1513 _mesa_clip_drawpixels(const struct gl_context
*ctx
,
1514 GLint
*destX
, GLint
*destY
,
1515 GLsizei
*width
, GLsizei
*height
,
1516 struct gl_pixelstore_attrib
*unpack
)
1518 const struct gl_framebuffer
*buffer
= ctx
->DrawBuffer
;
1520 if (unpack
->RowLength
== 0) {
1521 unpack
->RowLength
= *width
;
1524 ASSERT(ctx
->Pixel
.ZoomX
== 1.0F
);
1525 ASSERT(ctx
->Pixel
.ZoomY
== 1.0F
|| ctx
->Pixel
.ZoomY
== -1.0F
);
1528 if (*destX
< buffer
->_Xmin
) {
1529 unpack
->SkipPixels
+= (buffer
->_Xmin
- *destX
);
1530 *width
-= (buffer
->_Xmin
- *destX
);
1531 *destX
= buffer
->_Xmin
;
1533 /* right clipping */
1534 if (*destX
+ *width
> buffer
->_Xmax
)
1535 *width
-= (*destX
+ *width
- buffer
->_Xmax
);
1540 if (ctx
->Pixel
.ZoomY
== 1.0F
) {
1541 /* bottom clipping */
1542 if (*destY
< buffer
->_Ymin
) {
1543 unpack
->SkipRows
+= (buffer
->_Ymin
- *destY
);
1544 *height
-= (buffer
->_Ymin
- *destY
);
1545 *destY
= buffer
->_Ymin
;
1548 if (*destY
+ *height
> buffer
->_Ymax
)
1549 *height
-= (*destY
+ *height
- buffer
->_Ymax
);
1551 else { /* upside down */
1553 if (*destY
> buffer
->_Ymax
) {
1554 unpack
->SkipRows
+= (*destY
- buffer
->_Ymax
);
1555 *height
-= (*destY
- buffer
->_Ymax
);
1556 *destY
= buffer
->_Ymax
;
1558 /* bottom clipping */
1559 if (*destY
- *height
< buffer
->_Ymin
)
1560 *height
-= (buffer
->_Ymin
- (*destY
- *height
));
1561 /* adjust destY so it's the first row to write to */
1573 * Perform clipping for glReadPixels. The image's window position
1574 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
1575 * so that the image region is entirely within the window bounds.
1576 * Note: this is different from _mesa_clip_drawpixels() in that the
1577 * scissor box is ignored, and we use the bounds of the current readbuffer
1580 * \return GL_TRUE if image is ready for drawing or
1581 * GL_FALSE if image was completely clipped away (draw nothing)
1584 _mesa_clip_readpixels(const struct gl_context
*ctx
,
1585 GLint
*srcX
, GLint
*srcY
,
1586 GLsizei
*width
, GLsizei
*height
,
1587 struct gl_pixelstore_attrib
*pack
)
1589 const struct gl_framebuffer
*buffer
= ctx
->ReadBuffer
;
1591 if (pack
->RowLength
== 0) {
1592 pack
->RowLength
= *width
;
1597 pack
->SkipPixels
+= (0 - *srcX
);
1598 *width
-= (0 - *srcX
);
1601 /* right clipping */
1602 if (*srcX
+ *width
> (GLsizei
) buffer
->Width
)
1603 *width
-= (*srcX
+ *width
- buffer
->Width
);
1608 /* bottom clipping */
1610 pack
->SkipRows
+= (0 - *srcY
);
1611 *height
-= (0 - *srcY
);
1615 if (*srcY
+ *height
> (GLsizei
) buffer
->Height
)
1616 *height
-= (*srcY
+ *height
- buffer
->Height
);
1626 * Do clipping for a glCopyTexSubImage call.
1627 * The framebuffer source region might extend outside the framebuffer
1628 * bounds. Clip the source region against the framebuffer bounds and
1629 * adjust the texture/dest position and size accordingly.
1631 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
1634 _mesa_clip_copytexsubimage(const struct gl_context
*ctx
,
1635 GLint
*destX
, GLint
*destY
,
1636 GLint
*srcX
, GLint
*srcY
,
1637 GLsizei
*width
, GLsizei
*height
)
1639 const struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1640 const GLint srcX0
= *srcX
, srcY0
= *srcY
;
1642 if (_mesa_clip_to_region(0, 0, fb
->Width
, fb
->Height
,
1643 srcX
, srcY
, width
, height
)) {
1644 *destX
= *destX
+ *srcX
- srcX0
;
1645 *destY
= *destY
+ *srcY
- srcY0
;
1657 * Clip the rectangle defined by (x, y, width, height) against the bounds
1658 * specified by [xmin, xmax) and [ymin, ymax).
1659 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
1662 _mesa_clip_to_region(GLint xmin
, GLint ymin
,
1663 GLint xmax
, GLint ymax
,
1665 GLsizei
*width
, GLsizei
*height
)
1669 *width
-= (xmin
- *x
);
1673 /* right clipping */
1674 if (*x
+ *width
> xmax
)
1675 *width
-= (*x
+ *width
- xmax
);
1680 /* bottom (or top) clipping */
1682 *height
-= (ymin
- *y
);
1686 /* top (or bottom) clipping */
1687 if (*y
+ *height
> ymax
)
1688 *height
-= (*y
+ *height
- ymax
);
1698 * Clip dst coords against Xmax (or Ymax).
1701 clip_right_or_top(GLint
*srcX0
, GLint
*srcX1
,
1702 GLint
*dstX0
, GLint
*dstX1
,
1707 if (*dstX1
> maxValue
) {
1708 /* X1 outside right edge */
1709 ASSERT(*dstX0
< maxValue
); /* X0 should be inside right edge */
1710 t
= (GLfloat
) (maxValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
1711 /* chop off [t, 1] part */
1712 ASSERT(t
>= 0.0 && t
<= 1.0);
1714 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
1715 *srcX1
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
1717 else if (*dstX0
> maxValue
) {
1718 /* X0 outside right edge */
1719 ASSERT(*dstX1
< maxValue
); /* X1 should be inside right edge */
1720 t
= (GLfloat
) (maxValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
1721 /* chop off [t, 1] part */
1722 ASSERT(t
>= 0.0 && t
<= 1.0);
1724 bias
= (*srcX0
< *srcX1
) ? -0.5F
: 0.5F
;
1725 *srcX0
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
1731 * Clip dst coords against Xmin (or Ymin).
1734 clip_left_or_bottom(GLint
*srcX0
, GLint
*srcX1
,
1735 GLint
*dstX0
, GLint
*dstX1
,
1740 if (*dstX0
< minValue
) {
1741 /* X0 outside left edge */
1742 ASSERT(*dstX1
> minValue
); /* X1 should be inside left edge */
1743 t
= (GLfloat
) (minValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
1744 /* chop off [0, t] part */
1745 ASSERT(t
>= 0.0 && t
<= 1.0);
1747 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
; /* flipped??? */
1748 *srcX0
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
1750 else if (*dstX1
< minValue
) {
1751 /* X1 outside left edge */
1752 ASSERT(*dstX0
> minValue
); /* X0 should be inside left edge */
1753 t
= (GLfloat
) (minValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
1754 /* chop off [0, t] part */
1755 ASSERT(t
>= 0.0 && t
<= 1.0);
1757 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
1758 *srcX1
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
1764 * Do clipping of blit src/dest rectangles.
1765 * The dest rect is clipped against both the buffer bounds and scissor bounds.
1766 * The src rect is just clipped against the buffer bounds.
1768 * When either the src or dest rect is clipped, the other is also clipped
1771 * Note that X0 need not be less than X1 (same for Y) for either the source
1772 * and dest rects. That makes the clipping a little trickier.
1774 * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped
1777 _mesa_clip_blit(struct gl_context
*ctx
,
1778 GLint
*srcX0
, GLint
*srcY0
, GLint
*srcX1
, GLint
*srcY1
,
1779 GLint
*dstX0
, GLint
*dstY0
, GLint
*dstX1
, GLint
*dstY1
)
1781 const GLint srcXmin
= 0;
1782 const GLint srcXmax
= ctx
->ReadBuffer
->Width
;
1783 const GLint srcYmin
= 0;
1784 const GLint srcYmax
= ctx
->ReadBuffer
->Height
;
1786 /* these include scissor bounds */
1787 const GLint dstXmin
= ctx
->DrawBuffer
->_Xmin
;
1788 const GLint dstXmax
= ctx
->DrawBuffer
->_Xmax
;
1789 const GLint dstYmin
= ctx
->DrawBuffer
->_Ymin
;
1790 const GLint dstYmax
= ctx
->DrawBuffer
->_Ymax
;
1793 printf("PreClipX: src: %d .. %d dst: %d .. %d\n",
1794 *srcX0, *srcX1, *dstX0, *dstX1);
1795 printf("PreClipY: src: %d .. %d dst: %d .. %d\n",
1796 *srcY0, *srcY1, *dstY0, *dstY1);
1799 /* trivial rejection tests */
1800 if (*dstX0
== *dstX1
)
1801 return GL_FALSE
; /* no width */
1802 if (*dstX0
<= dstXmin
&& *dstX1
<= dstXmin
)
1803 return GL_FALSE
; /* totally out (left) of bounds */
1804 if (*dstX0
>= dstXmax
&& *dstX1
>= dstXmax
)
1805 return GL_FALSE
; /* totally out (right) of bounds */
1807 if (*dstY0
== *dstY1
)
1809 if (*dstY0
<= dstYmin
&& *dstY1
<= dstYmin
)
1811 if (*dstY0
>= dstYmax
&& *dstY1
>= dstYmax
)
1814 if (*srcX0
== *srcX1
)
1816 if (*srcX0
<= srcXmin
&& *srcX1
<= srcXmin
)
1818 if (*srcX0
>= srcXmax
&& *srcX1
>= srcXmax
)
1821 if (*srcY0
== *srcY1
)
1823 if (*srcY0
<= srcYmin
&& *srcY1
<= srcYmin
)
1825 if (*srcY0
>= srcYmax
&& *srcY1
>= srcYmax
)
1831 clip_right_or_top(srcX0
, srcX1
, dstX0
, dstX1
, dstXmax
);
1832 clip_right_or_top(srcY0
, srcY1
, dstY0
, dstY1
, dstYmax
);
1833 clip_left_or_bottom(srcX0
, srcX1
, dstX0
, dstX1
, dstXmin
);
1834 clip_left_or_bottom(srcY0
, srcY1
, dstY0
, dstY1
, dstYmin
);
1837 * src clip (just swap src/dst values from above)
1839 clip_right_or_top(dstX0
, dstX1
, srcX0
, srcX1
, srcXmax
);
1840 clip_right_or_top(dstY0
, dstY1
, srcY0
, srcY1
, srcYmax
);
1841 clip_left_or_bottom(dstX0
, dstX1
, srcX0
, srcX1
, srcXmin
);
1842 clip_left_or_bottom(dstY0
, dstY1
, srcY0
, srcY1
, srcYmin
);
1845 printf("PostClipX: src: %d .. %d dst: %d .. %d\n",
1846 *srcX0, *srcX1, *dstX0, *dstX1);
1847 printf("PostClipY: src: %d .. %d dst: %d .. %d\n",
1848 *srcY0, *srcY1, *dstY0, *dstY1);
1851 ASSERT(*dstX0
>= dstXmin
);
1852 ASSERT(*dstX0
<= dstXmax
);
1853 ASSERT(*dstX1
>= dstXmin
);
1854 ASSERT(*dstX1
<= dstXmax
);
1856 ASSERT(*dstY0
>= dstYmin
);
1857 ASSERT(*dstY0
<= dstYmax
);
1858 ASSERT(*dstY1
>= dstYmin
);
1859 ASSERT(*dstY1
<= dstYmax
);
1861 ASSERT(*srcX0
>= srcXmin
);
1862 ASSERT(*srcX0
<= srcXmax
);
1863 ASSERT(*srcX1
>= srcXmin
);
1864 ASSERT(*srcX1
<= srcXmax
);
1866 ASSERT(*srcY0
>= srcYmin
);
1867 ASSERT(*srcY0
<= srcYmax
);
1868 ASSERT(*srcY1
>= srcYmin
);
1869 ASSERT(*srcY1
<= srcYmax
);