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 * Normally, BYTE_TO_FLOAT(0) returns 0.00392 That causes problems when
45 * we later convert the float to a packed integer value (such as for
46 * GL_RGB5_A1) because we'll wind up with a non-zero value.
48 * We redefine the macros here so zero is handled correctly.
51 #define BYTE_TO_FLOAT(B) ((B) == 0 ? 0.0F : ((2.0F * (B) + 1.0F) * (1.0F/255.0F)))
54 #define SHORT_TO_FLOAT(S) ((S) == 0 ? 0.0F : ((2.0F * (S) + 1.0F) * (1.0F/65535.0F)))
58 /** Compute ceiling of integer quotient of A divided by B. */
59 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
63 * \return GL_TRUE if type is packed pixel type, GL_FALSE otherwise.
66 _mesa_type_is_packed(GLenum type)
69 case GL_UNSIGNED_BYTE_3_3_2:
70 case GL_UNSIGNED_BYTE_2_3_3_REV:
71 case MESA_UNSIGNED_BYTE_4_4:
72 case GL_UNSIGNED_SHORT_5_6_5:
73 case GL_UNSIGNED_SHORT_5_6_5_REV:
74 case GL_UNSIGNED_SHORT_4_4_4_4:
75 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
76 case GL_UNSIGNED_SHORT_5_5_5_1:
77 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
78 case GL_UNSIGNED_INT_8_8_8_8:
79 case GL_UNSIGNED_INT_8_8_8_8_REV:
80 case GL_UNSIGNED_INT_10_10_10_2:
81 case GL_UNSIGNED_INT_2_10_10_10_REV:
82 case GL_UNSIGNED_SHORT_8_8_MESA:
83 case GL_UNSIGNED_SHORT_8_8_REV_MESA:
84 case GL_UNSIGNED_INT_24_8_EXT:
85 case GL_UNSIGNED_INT_5_9_9_9_REV:
86 case GL_UNSIGNED_INT_10F_11F_11F_REV:
96 * Flip the order of the 2 bytes in each word in the given array.
99 * \param n number of words.
102 _mesa_swap2( GLushort *p, GLuint n )
105 for (i = 0; i < n; i++) {
106 p[i] = (p[i] >> 8) | ((p[i] << 8) & 0xff00);
113 * Flip the order of the 4 bytes in each word in the given array.
116 _mesa_swap4( GLuint *p, GLuint n )
119 for (i = 0; i < n; i++) {
122 | ((b >> 8) & 0xff00)
123 | ((b << 8) & 0xff0000)
124 | ((b << 24) & 0xff000000);
131 * Get the size of a GL data type.
133 * \param type GL data type.
135 * \return the size, in bytes, of the given data type, 0 if a GL_BITMAP, or -1
136 * if an invalid type enum.
139 _mesa_sizeof_type( GLenum type )
144 case GL_UNSIGNED_BYTE:
145 return sizeof(GLubyte);
147 return sizeof(GLbyte);
148 case GL_UNSIGNED_SHORT:
149 return sizeof(GLushort);
151 return sizeof(GLshort);
152 case GL_UNSIGNED_INT:
153 return sizeof(GLuint);
155 return sizeof(GLint);
157 return sizeof(GLfloat);
159 return sizeof(GLdouble);
160 case GL_HALF_FLOAT_ARB:
161 return sizeof(GLhalfARB);
163 return sizeof(GLfixed);
171 * Same as _mesa_sizeof_type() but also accepting the packed pixel
175 _mesa_sizeof_packed_type( GLenum type )
180 case GL_UNSIGNED_BYTE:
181 return sizeof(GLubyte);
183 return sizeof(GLbyte);
184 case GL_UNSIGNED_SHORT:
185 return sizeof(GLushort);
187 return sizeof(GLshort);
188 case GL_UNSIGNED_INT:
189 return sizeof(GLuint);
191 return sizeof(GLint);
192 case GL_HALF_FLOAT_ARB:
193 return sizeof(GLhalfARB);
195 return sizeof(GLfloat);
196 case GL_UNSIGNED_BYTE_3_3_2:
197 return sizeof(GLubyte);
198 case GL_UNSIGNED_BYTE_2_3_3_REV:
199 return sizeof(GLubyte);
200 case MESA_UNSIGNED_BYTE_4_4:
201 return sizeof(GLubyte);
202 case GL_UNSIGNED_SHORT_5_6_5:
203 return sizeof(GLushort);
204 case GL_UNSIGNED_SHORT_5_6_5_REV:
205 return sizeof(GLushort);
206 case GL_UNSIGNED_SHORT_4_4_4_4:
207 return sizeof(GLushort);
208 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
209 return sizeof(GLushort);
210 case GL_UNSIGNED_SHORT_5_5_5_1:
211 return sizeof(GLushort);
212 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
213 return sizeof(GLushort);
214 case GL_UNSIGNED_INT_8_8_8_8:
215 return sizeof(GLuint);
216 case GL_UNSIGNED_INT_8_8_8_8_REV:
217 return sizeof(GLuint);
218 case GL_UNSIGNED_INT_10_10_10_2:
219 return sizeof(GLuint);
220 case GL_UNSIGNED_INT_2_10_10_10_REV:
221 return sizeof(GLuint);
222 case GL_UNSIGNED_SHORT_8_8_MESA:
223 case GL_UNSIGNED_SHORT_8_8_REV_MESA:
224 return sizeof(GLushort);
225 case GL_UNSIGNED_INT_24_8_EXT:
226 return sizeof(GLuint);
227 case GL_UNSIGNED_INT_5_9_9_9_REV:
228 return sizeof(GLuint);
229 case GL_UNSIGNED_INT_10F_11F_11F_REV:
230 return sizeof(GLuint);
238 * Get the number of components in a pixel format.
240 * \param format pixel format.
242 * \return the number of components in the given format, or -1 if a bad format.
245 _mesa_components_in_format( GLenum format )
249 case GL_COLOR_INDEX1_EXT:
250 case GL_COLOR_INDEX2_EXT:
251 case GL_COLOR_INDEX4_EXT:
252 case GL_COLOR_INDEX8_EXT:
253 case GL_COLOR_INDEX12_EXT:
254 case GL_COLOR_INDEX16_EXT:
255 case GL_STENCIL_INDEX:
256 case GL_DEPTH_COMPONENT:
258 case GL_RED_INTEGER_EXT:
260 case GL_GREEN_INTEGER_EXT:
262 case GL_BLUE_INTEGER_EXT:
264 case GL_ALPHA_INTEGER_EXT:
266 case GL_LUMINANCE_INTEGER_EXT:
269 case GL_LUMINANCE_ALPHA:
270 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
274 case GL_RGB_INTEGER_EXT:
277 case GL_RGBA_INTEGER_EXT:
287 case GL_DEPTH_STENCIL_EXT:
299 * Get the bytes per pixel of pixel format type pair.
301 * \param format pixel format.
302 * \param type pixel type.
304 * \return bytes per pixel, or -1 if a bad format or type was given.
307 _mesa_bytes_per_pixel( GLenum format, GLenum type )
309 GLint comps = _mesa_components_in_format( format );
315 return 0; /* special case */
317 case GL_UNSIGNED_BYTE:
318 return comps * sizeof(GLubyte);
320 case GL_UNSIGNED_SHORT:
321 return comps * sizeof(GLshort);
323 case GL_UNSIGNED_INT:
324 return comps * sizeof(GLint);
326 return comps * sizeof(GLfloat);
327 case GL_HALF_FLOAT_ARB:
328 return comps * sizeof(GLhalfARB);
329 case GL_UNSIGNED_BYTE_3_3_2:
330 case GL_UNSIGNED_BYTE_2_3_3_REV:
331 if (format == GL_RGB || format == GL_BGR ||
332 format == GL_RGB_INTEGER_EXT || format == GL_BGR_INTEGER_EXT)
333 return sizeof(GLubyte);
335 return -1; /* error */
336 case GL_UNSIGNED_SHORT_5_6_5:
337 case GL_UNSIGNED_SHORT_5_6_5_REV:
338 if (format == GL_RGB || format == GL_BGR ||
339 format == GL_RGB_INTEGER_EXT || format == GL_BGR_INTEGER_EXT)
340 return sizeof(GLushort);
342 return -1; /* error */
343 case GL_UNSIGNED_SHORT_4_4_4_4:
344 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
345 case GL_UNSIGNED_SHORT_5_5_5_1:
346 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
347 if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT ||
348 format == GL_RGBA_INTEGER_EXT || format == GL_BGRA_INTEGER_EXT)
349 return sizeof(GLushort);
352 case GL_UNSIGNED_INT_8_8_8_8:
353 case GL_UNSIGNED_INT_8_8_8_8_REV:
354 case GL_UNSIGNED_INT_10_10_10_2:
355 case GL_UNSIGNED_INT_2_10_10_10_REV:
356 if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT ||
357 format == GL_RGBA_INTEGER_EXT || format == GL_BGRA_INTEGER_EXT)
358 return sizeof(GLuint);
361 case GL_UNSIGNED_SHORT_8_8_MESA:
362 case GL_UNSIGNED_SHORT_8_8_REV_MESA:
363 if (format == GL_YCBCR_MESA)
364 return sizeof(GLushort);
367 case GL_UNSIGNED_INT_24_8_EXT:
368 if (format == GL_DEPTH_STENCIL_EXT)
369 return sizeof(GLuint);
372 case GL_UNSIGNED_INT_5_9_9_9_REV:
373 if (format == GL_RGB)
374 return sizeof(GLuint);
377 case GL_UNSIGNED_INT_10F_11F_11F_REV:
378 if (format == GL_RGB)
379 return sizeof(GLuint);
389 * Test for a legal pixel format and type.
391 * \param format pixel format.
392 * \param type pixel type.
394 * \return GL_TRUE if the given pixel format and type are legal, or GL_FALSE
398 _mesa_is_legal_format_and_type(const struct gl_context *ctx,
399 GLenum format, GLenum type)
403 case GL_STENCIL_INDEX:
407 case GL_UNSIGNED_BYTE:
409 case GL_UNSIGNED_SHORT:
411 case GL_UNSIGNED_INT:
414 case GL_HALF_FLOAT_ARB:
415 return ctx->Extensions.ARB_half_float_pixel;
423 #if 0 /* not legal! see table 3.6 of the 1.5 spec */
427 case GL_LUMINANCE_ALPHA:
428 case GL_DEPTH_COMPONENT:
431 case GL_UNSIGNED_BYTE:
433 case GL_UNSIGNED_SHORT:
435 case GL_UNSIGNED_INT:
438 case GL_HALF_FLOAT_ARB:
439 return ctx->Extensions.ARB_half_float_pixel;
444 if (!ctx->Extensions.ARB_texture_rg)
449 case GL_UNSIGNED_BYTE:
451 case GL_UNSIGNED_SHORT:
453 case GL_UNSIGNED_INT:
456 case GL_HALF_FLOAT_ARB:
457 return ctx->Extensions.ARB_half_float_pixel;
464 case GL_UNSIGNED_BYTE:
466 case GL_UNSIGNED_SHORT:
468 case GL_UNSIGNED_INT:
470 case GL_UNSIGNED_BYTE_3_3_2:
471 case GL_UNSIGNED_BYTE_2_3_3_REV:
472 case GL_UNSIGNED_SHORT_5_6_5:
473 case GL_UNSIGNED_SHORT_5_6_5_REV:
475 case GL_HALF_FLOAT_ARB:
476 return ctx->Extensions.ARB_half_float_pixel;
477 case GL_UNSIGNED_INT_5_9_9_9_REV:
478 return ctx->Extensions.EXT_texture_shared_exponent;
479 case GL_UNSIGNED_INT_10F_11F_11F_REV:
480 return ctx->Extensions.EXT_packed_float;
486 /* NOTE: no packed types are supported with BGR. That's
487 * intentional, according to the GL spec.
490 case GL_UNSIGNED_BYTE:
492 case GL_UNSIGNED_SHORT:
494 case GL_UNSIGNED_INT:
497 case GL_HALF_FLOAT_ARB:
498 return ctx->Extensions.ARB_half_float_pixel;
507 case GL_UNSIGNED_BYTE:
509 case GL_UNSIGNED_SHORT:
511 case GL_UNSIGNED_INT:
513 case GL_UNSIGNED_SHORT_4_4_4_4:
514 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
515 case GL_UNSIGNED_SHORT_5_5_5_1:
516 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
517 case GL_UNSIGNED_INT_8_8_8_8:
518 case GL_UNSIGNED_INT_8_8_8_8_REV:
519 case GL_UNSIGNED_INT_10_10_10_2:
520 case GL_UNSIGNED_INT_2_10_10_10_REV:
522 case GL_HALF_FLOAT_ARB:
523 return ctx->Extensions.ARB_half_float_pixel;
528 if (type == GL_UNSIGNED_SHORT_8_8_MESA ||
529 type == GL_UNSIGNED_SHORT_8_8_REV_MESA)
533 case GL_DEPTH_STENCIL_EXT:
534 if (ctx->Extensions.EXT_packed_depth_stencil
535 && type == GL_UNSIGNED_INT_24_8_EXT)
543 case GL_UNSIGNED_BYTE:
545 case GL_UNSIGNED_SHORT:
547 case GL_UNSIGNED_INT:
554 /* integer-valued formats */
555 case GL_RED_INTEGER_EXT:
556 case GL_GREEN_INTEGER_EXT:
557 case GL_BLUE_INTEGER_EXT:
558 case GL_ALPHA_INTEGER_EXT:
561 case GL_UNSIGNED_BYTE:
563 case GL_UNSIGNED_SHORT:
565 case GL_UNSIGNED_INT:
566 return ctx->Extensions.EXT_texture_integer;
571 case GL_RGB_INTEGER_EXT:
574 case GL_UNSIGNED_BYTE:
576 case GL_UNSIGNED_SHORT:
578 case GL_UNSIGNED_INT:
579 case GL_UNSIGNED_BYTE_3_3_2:
580 case GL_UNSIGNED_BYTE_2_3_3_REV:
581 case GL_UNSIGNED_SHORT_5_6_5:
582 case GL_UNSIGNED_SHORT_5_6_5_REV:
583 return ctx->Extensions.EXT_texture_integer;
588 case GL_BGR_INTEGER_EXT:
591 case GL_UNSIGNED_BYTE:
593 case GL_UNSIGNED_SHORT:
595 case GL_UNSIGNED_INT:
596 /* NOTE: no packed formats w/ BGR format */
597 return ctx->Extensions.EXT_texture_integer;
602 case GL_RGBA_INTEGER_EXT:
603 case GL_BGRA_INTEGER_EXT:
606 case GL_UNSIGNED_BYTE:
608 case GL_UNSIGNED_SHORT:
610 case GL_UNSIGNED_INT:
611 case GL_UNSIGNED_SHORT_4_4_4_4:
612 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
613 case GL_UNSIGNED_SHORT_5_5_5_1:
614 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
615 case GL_UNSIGNED_INT_8_8_8_8:
616 case GL_UNSIGNED_INT_8_8_8_8_REV:
617 case GL_UNSIGNED_INT_10_10_10_2:
618 case GL_UNSIGNED_INT_2_10_10_10_REV:
619 return ctx->Extensions.EXT_texture_integer;
624 case GL_LUMINANCE_INTEGER_EXT:
625 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
628 case GL_UNSIGNED_BYTE:
630 case GL_UNSIGNED_SHORT:
632 case GL_UNSIGNED_INT:
633 return ctx->Extensions.EXT_texture_integer;
646 * Test if the given image format is a color/RGBA format (i.e., not color
647 * index, depth, stencil, etc).
648 * \param format the image format value (may by an internal texture format)
649 * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
652 _mesa_is_color_format(GLenum format)
670 case GL_LUMINANCE_ALPHA:
671 case GL_LUMINANCE4_ALPHA4:
672 case GL_LUMINANCE6_ALPHA2:
673 case GL_LUMINANCE8_ALPHA8:
674 case GL_LUMINANCE12_ALPHA4:
675 case GL_LUMINANCE12_ALPHA12:
676 case GL_LUMINANCE16_ALPHA16:
708 /* float texture formats */
709 case GL_ALPHA16F_ARB:
710 case GL_ALPHA32F_ARB:
711 case GL_LUMINANCE16F_ARB:
712 case GL_LUMINANCE32F_ARB:
713 case GL_LUMINANCE_ALPHA16F_ARB:
714 case GL_LUMINANCE_ALPHA32F_ARB:
715 case GL_INTENSITY16F_ARB:
716 case GL_INTENSITY32F_ARB:
725 /* compressed formats */
726 case GL_COMPRESSED_ALPHA:
727 case GL_COMPRESSED_LUMINANCE:
728 case GL_COMPRESSED_LUMINANCE_ALPHA:
729 case GL_COMPRESSED_INTENSITY:
730 case GL_COMPRESSED_RED:
731 case GL_COMPRESSED_RG:
732 case GL_COMPRESSED_RGB:
733 case GL_COMPRESSED_RGBA:
738 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
739 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
740 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
741 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
742 case GL_COMPRESSED_RGB_FXT1_3DFX:
743 case GL_COMPRESSED_RGBA_FXT1_3DFX:
744 #if FEATURE_EXT_texture_sRGB
747 case GL_SRGB_ALPHA_EXT:
748 case GL_SRGB8_ALPHA8_EXT:
749 case GL_SLUMINANCE_ALPHA_EXT:
750 case GL_SLUMINANCE8_ALPHA8_EXT:
751 case GL_SLUMINANCE_EXT:
752 case GL_SLUMINANCE8_EXT:
753 case GL_COMPRESSED_SRGB_EXT:
754 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
755 case GL_COMPRESSED_SRGB_ALPHA_EXT:
756 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
757 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
758 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
759 case GL_COMPRESSED_SLUMINANCE_EXT:
760 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT:
761 #endif /* FEATURE_EXT_texture_sRGB */
762 case GL_COMPRESSED_RED_RGTC1:
763 case GL_COMPRESSED_SIGNED_RED_RGTC1:
764 case GL_COMPRESSED_RG_RGTC2:
765 case GL_COMPRESSED_SIGNED_RG_RGTC2:
766 case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
767 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
768 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
769 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
770 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
771 /* generic integer formats */
772 case GL_RED_INTEGER_EXT:
773 case GL_GREEN_INTEGER_EXT:
774 case GL_BLUE_INTEGER_EXT:
775 case GL_ALPHA_INTEGER_EXT:
776 case GL_RGB_INTEGER_EXT:
777 case GL_RGBA_INTEGER_EXT:
778 case GL_BGR_INTEGER_EXT:
779 case GL_BGRA_INTEGER_EXT:
780 case GL_LUMINANCE_INTEGER_EXT:
781 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
782 /* sized integer formats */
783 case GL_RGBA32UI_EXT:
785 case GL_ALPHA32UI_EXT:
786 case GL_INTENSITY32UI_EXT:
787 case GL_LUMINANCE32UI_EXT:
788 case GL_LUMINANCE_ALPHA32UI_EXT:
789 case GL_RGBA16UI_EXT:
791 case GL_ALPHA16UI_EXT:
792 case GL_INTENSITY16UI_EXT:
793 case GL_LUMINANCE16UI_EXT:
794 case GL_LUMINANCE_ALPHA16UI_EXT:
797 case GL_ALPHA8UI_EXT:
798 case GL_INTENSITY8UI_EXT:
799 case GL_LUMINANCE8UI_EXT:
800 case GL_LUMINANCE_ALPHA8UI_EXT:
803 case GL_ALPHA32I_EXT:
804 case GL_INTENSITY32I_EXT:
805 case GL_LUMINANCE32I_EXT:
806 case GL_LUMINANCE_ALPHA32I_EXT:
809 case GL_ALPHA16I_EXT:
810 case GL_INTENSITY16I_EXT:
811 case GL_LUMINANCE16I_EXT:
812 case GL_LUMINANCE_ALPHA16I_EXT:
816 case GL_INTENSITY8I_EXT:
817 case GL_LUMINANCE8I_EXT:
818 case GL_LUMINANCE_ALPHA8I_EXT:
819 /* signed, normalized texture formats */
831 case GL_RGBA16_SNORM:
833 case GL_ALPHA8_SNORM:
834 case GL_ALPHA16_SNORM:
835 case GL_LUMINANCE_SNORM:
836 case GL_LUMINANCE8_SNORM:
837 case GL_LUMINANCE16_SNORM:
838 case GL_LUMINANCE_ALPHA_SNORM:
839 case GL_LUMINANCE8_ALPHA8_SNORM:
840 case GL_LUMINANCE16_ALPHA16_SNORM:
841 case GL_INTENSITY_SNORM:
842 case GL_INTENSITY8_SNORM:
843 case GL_INTENSITY16_SNORM:
845 case GL_R11F_G11F_B10F:
847 case GL_YCBCR_MESA: /* not considered to be RGB */
856 * Test if the given image format is a color index format.
859 _mesa_is_index_format(GLenum format)
863 case GL_COLOR_INDEX1_EXT:
864 case GL_COLOR_INDEX2_EXT:
865 case GL_COLOR_INDEX4_EXT:
866 case GL_COLOR_INDEX8_EXT:
867 case GL_COLOR_INDEX12_EXT:
868 case GL_COLOR_INDEX16_EXT:
877 * Test if the given image format is a depth component format.
880 _mesa_is_depth_format(GLenum format)
883 case GL_DEPTH_COMPONENT:
884 case GL_DEPTH_COMPONENT16:
885 case GL_DEPTH_COMPONENT24:
886 case GL_DEPTH_COMPONENT32:
895 * Test if the given image format is a stencil format.
898 _mesa_is_stencil_format(GLenum format)
901 case GL_STENCIL_INDEX:
902 case GL_DEPTH_STENCIL:
911 * Test if the given image format is a YCbCr format.
914 _mesa_is_ycbcr_format(GLenum format)
926 * Test if the given image format is a depth+stencil format.
929 _mesa_is_depthstencil_format(GLenum format)
932 case GL_DEPTH24_STENCIL8_EXT:
933 case GL_DEPTH_STENCIL_EXT:
942 * Test if the given image format is a depth or stencil format.
945 _mesa_is_depth_or_stencil_format(GLenum format)
948 case GL_DEPTH_COMPONENT:
949 case GL_DEPTH_COMPONENT16:
950 case GL_DEPTH_COMPONENT24:
951 case GL_DEPTH_COMPONENT32:
952 case GL_STENCIL_INDEX:
953 case GL_STENCIL_INDEX1_EXT:
954 case GL_STENCIL_INDEX4_EXT:
955 case GL_STENCIL_INDEX8_EXT:
956 case GL_STENCIL_INDEX16_EXT:
957 case GL_DEPTH_STENCIL_EXT:
958 case GL_DEPTH24_STENCIL8_EXT:
967 * Test if the given image format is a dudv format.
970 _mesa_is_dudv_format(GLenum format)
983 * Test if the given format is an integer (non-normalized) format.
986 _mesa_is_integer_format(GLenum format)
989 /* generic integer formats */
990 case GL_RED_INTEGER_EXT:
991 case GL_GREEN_INTEGER_EXT:
992 case GL_BLUE_INTEGER_EXT:
993 case GL_ALPHA_INTEGER_EXT:
994 case GL_RGB_INTEGER_EXT:
995 case GL_RGBA_INTEGER_EXT:
996 case GL_BGR_INTEGER_EXT:
997 case GL_BGRA_INTEGER_EXT:
998 case GL_LUMINANCE_INTEGER_EXT:
999 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
1000 /* specific integer formats */
1001 case GL_RGBA32UI_EXT:
1002 case GL_RGB32UI_EXT:
1003 case GL_ALPHA32UI_EXT:
1004 case GL_INTENSITY32UI_EXT:
1005 case GL_LUMINANCE32UI_EXT:
1006 case GL_LUMINANCE_ALPHA32UI_EXT:
1007 case GL_RGBA16UI_EXT:
1008 case GL_RGB16UI_EXT:
1009 case GL_ALPHA16UI_EXT:
1010 case GL_INTENSITY16UI_EXT:
1011 case GL_LUMINANCE16UI_EXT:
1012 case GL_LUMINANCE_ALPHA16UI_EXT:
1013 case GL_RGBA8UI_EXT:
1015 case GL_ALPHA8UI_EXT:
1016 case GL_INTENSITY8UI_EXT:
1017 case GL_LUMINANCE8UI_EXT:
1018 case GL_LUMINANCE_ALPHA8UI_EXT:
1019 case GL_RGBA32I_EXT:
1021 case GL_ALPHA32I_EXT:
1022 case GL_INTENSITY32I_EXT:
1023 case GL_LUMINANCE32I_EXT:
1024 case GL_LUMINANCE_ALPHA32I_EXT:
1025 case GL_RGBA16I_EXT:
1027 case GL_ALPHA16I_EXT:
1028 case GL_INTENSITY16I_EXT:
1029 case GL_LUMINANCE16I_EXT:
1030 case GL_LUMINANCE_ALPHA16I_EXT:
1033 case GL_ALPHA8I_EXT:
1034 case GL_INTENSITY8I_EXT:
1035 case GL_LUMINANCE8I_EXT:
1036 case GL_LUMINANCE_ALPHA8I_EXT:
1045 * Test if an image format is a supported compressed format.
1046 * \param format the internal format token provided by the user.
1047 * \return GL_TRUE if compressed, GL_FALSE if uncompressed
1050 _mesa_is_compressed_format(struct gl_context *ctx, GLenum format)
1053 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
1054 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
1055 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
1056 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
1057 return ctx->Extensions.EXT_texture_compression_s3tc;
1062 return ctx->Extensions.S3_s3tc;
1063 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
1064 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
1065 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
1066 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
1067 return ctx->Extensions.EXT_texture_sRGB
1068 && ctx->Extensions.EXT_texture_compression_s3tc;
1069 case GL_COMPRESSED_RGB_FXT1_3DFX:
1070 case GL_COMPRESSED_RGBA_FXT1_3DFX:
1071 return ctx->Extensions.TDFX_texture_compression_FXT1;
1072 case GL_COMPRESSED_RED_RGTC1:
1073 case GL_COMPRESSED_SIGNED_RED_RGTC1:
1074 case GL_COMPRESSED_RG_RGTC2:
1075 case GL_COMPRESSED_SIGNED_RG_RGTC2:
1076 return ctx->Extensions.ARB_texture_compression_rgtc;
1077 case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
1078 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
1079 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
1080 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
1081 return ctx->Extensions.EXT_texture_compression_latc;
1082 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
1083 return ctx->Extensions.ATI_texture_compression_3dc;
1085 case GL_PALETTE4_RGB8_OES:
1086 case GL_PALETTE4_RGBA8_OES:
1087 case GL_PALETTE4_R5_G6_B5_OES:
1088 case GL_PALETTE4_RGBA4_OES:
1089 case GL_PALETTE4_RGB5_A1_OES:
1090 case GL_PALETTE8_RGB8_OES:
1091 case GL_PALETTE8_RGBA8_OES:
1092 case GL_PALETTE8_R5_G6_B5_OES:
1093 case GL_PALETTE8_RGBA4_OES:
1094 case GL_PALETTE8_RGB5_A1_OES:
1095 return ctx->API == API_OPENGLES;
1104 * Return the address of a specific pixel in an image (1D, 2D or 3D).
1106 * Pixel unpacking/packing parameters are observed according to \p packing.
1108 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
1109 * \param image starting address of image data
1110 * \param width the image width
1111 * \param height theimage height
1112 * \param format the pixel format
1113 * \param type the pixel data type
1114 * \param packing the pixelstore attributes
1115 * \param img which image in the volume (0 for 1D or 2D images)
1116 * \param row row of pixel in the image (0 for 1D images)
1117 * \param column column of pixel in the image
1119 * \return address of pixel on success, or NULL on error.
1121 * \sa gl_pixelstore_attrib.
1124 _mesa_image_address( GLuint dimensions,
1125 const struct gl_pixelstore_attrib *packing,
1126 const GLvoid *image,
1127 GLsizei width, GLsizei height,
1128 GLenum format, GLenum type,
1129 GLint img, GLint row, GLint column )
1131 GLint alignment; /* 1, 2 or 4 */
1132 GLint pixels_per_row;
1133 GLint rows_per_image;
1136 GLint skipimages; /* for 3-D volume images */
1137 GLubyte *pixel_addr;
1139 ASSERT(dimensions >= 1 && dimensions <= 3);
1141 alignment = packing->Alignment;
1142 if (packing->RowLength > 0) {
1143 pixels_per_row = packing->RowLength;
1146 pixels_per_row = width;
1148 if (packing->ImageHeight > 0) {
1149 rows_per_image = packing->ImageHeight;
1152 rows_per_image = height;
1155 skippixels = packing->SkipPixels;
1156 /* Note: SKIP_ROWS _is_ used for 1D images */
1157 skiprows = packing->SkipRows;
1158 /* Note: SKIP_IMAGES is only used for 3D images */
1159 skipimages = (dimensions == 3) ? packing->SkipImages : 0;
1161 if (type == GL_BITMAP) {
1163 GLint comp_per_pixel; /* components per pixel */
1164 GLint bytes_per_comp; /* bytes per component */
1165 GLint bytes_per_row;
1166 GLint bytes_per_image;
1168 /* Compute bytes per component */
1169 bytes_per_comp = _mesa_sizeof_packed_type( type );
1170 if (bytes_per_comp < 0) {
1174 /* Compute number of components per pixel */
1175 comp_per_pixel = _mesa_components_in_format( format );
1176 if (comp_per_pixel < 0) {
1180 bytes_per_row = alignment
1181 * CEILING( comp_per_pixel*pixels_per_row, 8*alignment );
1183 bytes_per_image = bytes_per_row * rows_per_image;
1185 pixel_addr = (GLubyte *) image
1186 + (skipimages + img) * bytes_per_image
1187 + (skiprows + row) * bytes_per_row
1188 + (skippixels + column) / 8;
1191 /* Non-BITMAP data */
1192 GLint bytes_per_pixel, bytes_per_row, remainder, bytes_per_image;
1195 bytes_per_pixel = _mesa_bytes_per_pixel( format, type );
1197 /* The pixel type and format should have been error checked earlier */
1198 assert(bytes_per_pixel > 0);
1200 bytes_per_row = pixels_per_row * bytes_per_pixel;
1201 remainder = bytes_per_row % alignment;
1203 bytes_per_row += (alignment - remainder);
1205 ASSERT(bytes_per_row % alignment == 0);
1207 bytes_per_image = bytes_per_row * rows_per_image;
1209 if (packing->Invert) {
1210 /* set pixel_addr to the last row */
1211 topOfImage = bytes_per_row * (height - 1);
1212 bytes_per_row = -bytes_per_row;
1218 /* compute final pixel address */
1219 pixel_addr = (GLubyte *) image
1220 + (skipimages + img) * bytes_per_image
1222 + (skiprows + row) * bytes_per_row
1223 + (skippixels + column) * bytes_per_pixel;
1226 return (GLvoid *) pixel_addr;
1231 _mesa_image_address1d( const struct gl_pixelstore_attrib *packing,
1232 const GLvoid *image,
1234 GLenum format, GLenum type,
1237 return _mesa_image_address(1, packing, image, width, 1,
1238 format, type, 0, 0, column);
1243 _mesa_image_address2d( const struct gl_pixelstore_attrib *packing,
1244 const GLvoid *image,
1245 GLsizei width, GLsizei height,
1246 GLenum format, GLenum type,
1247 GLint row, GLint column )
1249 return _mesa_image_address(2, packing, image, width, height,
1250 format, type, 0, row, column);
1255 _mesa_image_address3d( const struct gl_pixelstore_attrib *packing,
1256 const GLvoid *image,
1257 GLsizei width, GLsizei height,
1258 GLenum format, GLenum type,
1259 GLint img, GLint row, GLint column )
1261 return _mesa_image_address(3, packing, image, width, height,
1262 format, type, img, row, column);
1268 * Compute the stride (in bytes) between image rows.
1270 * \param packing the pixelstore attributes
1271 * \param width image width.
1272 * \param format pixel format.
1273 * \param type pixel data type.
1275 * \return the stride in bytes for the given parameters, or -1 if error
1278 _mesa_image_row_stride( const struct gl_pixelstore_attrib *packing,
1279 GLint width, GLenum format, GLenum type )
1281 GLint bytesPerRow, remainder;
1285 if (type == GL_BITMAP) {
1286 if (packing->RowLength == 0) {
1287 bytesPerRow = (width + 7) / 8;
1290 bytesPerRow = (packing->RowLength + 7) / 8;
1294 /* Non-BITMAP data */
1295 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
1296 if (bytesPerPixel <= 0)
1297 return -1; /* error */
1298 if (packing->RowLength == 0) {
1299 bytesPerRow = bytesPerPixel * width;
1302 bytesPerRow = bytesPerPixel * packing->RowLength;
1306 remainder = bytesPerRow % packing->Alignment;
1307 if (remainder > 0) {
1308 bytesPerRow += (packing->Alignment - remainder);
1311 if (packing->Invert) {
1312 /* negate the bytes per row (negative row stride) */
1313 bytesPerRow = -bytesPerRow;
1321 * Compute the stride between images in a 3D texture (in bytes) for the given
1322 * pixel packing parameters and image width, format and type.
1325 _mesa_image_image_stride( const struct gl_pixelstore_attrib *packing,
1326 GLint width, GLint height,
1327 GLenum format, GLenum type )
1329 GLint bytesPerRow, bytesPerImage, remainder;
1333 if (type == GL_BITMAP) {
1334 if (packing->RowLength == 0) {
1335 bytesPerRow = (width + 7) / 8;
1338 bytesPerRow = (packing->RowLength + 7) / 8;
1342 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
1344 if (bytesPerPixel <= 0)
1345 return -1; /* error */
1346 if (packing->RowLength == 0) {
1347 bytesPerRow = bytesPerPixel * width;
1350 bytesPerRow = bytesPerPixel * packing->RowLength;
1354 remainder = bytesPerRow % packing->Alignment;
1356 bytesPerRow += (packing->Alignment - remainder);
1358 if (packing->ImageHeight == 0)
1359 bytesPerImage = bytesPerRow * height;
1361 bytesPerImage = bytesPerRow * packing->ImageHeight;
1363 return bytesPerImage;
1369 * "Expand" a bitmap from 1-bit per pixel to 8-bits per pixel.
1370 * This is typically used to convert a bitmap into a GLubyte/pixel texture.
1371 * "On" bits will set texels to \p onValue.
1372 * "Off" bits will not modify texels.
1373 * \param width src bitmap width in pixels
1374 * \param height src bitmap height in pixels
1375 * \param unpack bitmap unpacking state
1376 * \param bitmap the src bitmap data
1377 * \param destBuffer start of dest buffer
1378 * \param destStride row stride in dest buffer
1379 * \param onValue if bit is 1, set destBuffer pixel to this value
1382 _mesa_expand_bitmap(GLsizei width, GLsizei height,
1383 const struct gl_pixelstore_attrib *unpack,
1384 const GLubyte *bitmap,
1385 GLubyte *destBuffer, GLint destStride,
1388 const GLubyte *srcRow = (const GLubyte *)
1389 _mesa_image_address2d(unpack, bitmap, width, height,
1390 GL_COLOR_INDEX, GL_BITMAP, 0, 0);
1391 const GLint srcStride = _mesa_image_row_stride(unpack, width,
1392 GL_COLOR_INDEX, GL_BITMAP);
1395 #define SET_PIXEL(COL, ROW) \
1396 destBuffer[(ROW) * destStride + (COL)] = onValue;
1398 for (row = 0; row < height; row++) {
1399 const GLubyte *src = srcRow;
1401 if (unpack->LsbFirst) {
1403 GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
1404 for (col = 0; col < width; col++) {
1407 SET_PIXEL(col, row);
1419 /* get ready for next row */
1425 GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
1426 for (col = 0; col < width; col++) {
1429 SET_PIXEL(col, row);
1441 /* get ready for next row */
1446 srcRow += srcStride;
1456 * Convert an array of RGBA colors from one datatype to another.
1457 * NOTE: src may equal dst. In that case, we use a temporary buffer.
1460 _mesa_convert_colors(GLenum srcType, const GLvoid *src,
1461 GLenum dstType, GLvoid *dst,
1462 GLuint count, const GLubyte mask[])
1464 GLuint tempBuffer[MAX_WIDTH][4];
1465 const GLboolean useTemp = (src == dst);
1467 ASSERT(srcType != dstType);
1470 case GL_UNSIGNED_BYTE:
1471 if (dstType == GL_UNSIGNED_SHORT) {
1472 const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
1473 GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
1475 for (i = 0; i < count; i++) {
1476 if (!mask || mask[i]) {
1477 dst2[i][RCOMP] = UBYTE_TO_USHORT(src1[i][RCOMP]);
1478 dst2[i][GCOMP] = UBYTE_TO_USHORT(src1[i][GCOMP]);
1479 dst2[i][BCOMP] = UBYTE_TO_USHORT(src1[i][BCOMP]);
1480 dst2[i][ACOMP] = UBYTE_TO_USHORT(src1[i][ACOMP]);
1484 memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
1487 const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
1488 GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
1490 ASSERT(dstType == GL_FLOAT);
1491 for (i = 0; i < count; i++) {
1492 if (!mask || mask[i]) {
1493 dst4[i][RCOMP] = UBYTE_TO_FLOAT(src1[i][RCOMP]);
1494 dst4[i][GCOMP] = UBYTE_TO_FLOAT(src1[i][GCOMP]);
1495 dst4[i][BCOMP] = UBYTE_TO_FLOAT(src1[i][BCOMP]);
1496 dst4[i][ACOMP] = UBYTE_TO_FLOAT(src1[i][ACOMP]);
1500 memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
1503 case GL_UNSIGNED_SHORT:
1504 if (dstType == GL_UNSIGNED_BYTE) {
1505 const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
1506 GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
1508 for (i = 0; i < count; i++) {
1509 if (!mask || mask[i]) {
1510 dst1[i][RCOMP] = USHORT_TO_UBYTE(src2[i][RCOMP]);
1511 dst1[i][GCOMP] = USHORT_TO_UBYTE(src2[i][GCOMP]);
1512 dst1[i][BCOMP] = USHORT_TO_UBYTE(src2[i][BCOMP]);
1513 dst1[i][ACOMP] = USHORT_TO_UBYTE(src2[i][ACOMP]);
1517 memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
1520 const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
1521 GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
1523 ASSERT(dstType == GL_FLOAT);
1524 for (i = 0; i < count; i++) {
1525 if (!mask || mask[i]) {
1526 dst4[i][RCOMP] = USHORT_TO_FLOAT(src2[i][RCOMP]);
1527 dst4[i][GCOMP] = USHORT_TO_FLOAT(src2[i][GCOMP]);
1528 dst4[i][BCOMP] = USHORT_TO_FLOAT(src2[i][BCOMP]);
1529 dst4[i][ACOMP] = USHORT_TO_FLOAT(src2[i][ACOMP]);
1533 memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
1537 if (dstType == GL_UNSIGNED_BYTE) {
1538 const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
1539 GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
1541 for (i = 0; i < count; i++) {
1542 if (!mask || mask[i]) {
1543 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][RCOMP], src4[i][RCOMP]);
1544 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][GCOMP], src4[i][GCOMP]);
1545 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][BCOMP], src4[i][BCOMP]);
1546 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][ACOMP], src4[i][ACOMP]);
1550 memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
1553 const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
1554 GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
1556 ASSERT(dstType == GL_UNSIGNED_SHORT);
1557 for (i = 0; i < count; i++) {
1558 if (!mask || mask[i]) {
1559 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][RCOMP], src4[i][RCOMP]);
1560 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][GCOMP], src4[i][GCOMP]);
1561 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][BCOMP], src4[i][BCOMP]);
1562 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][ACOMP], src4[i][ACOMP]);
1566 memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
1570 _mesa_problem(NULL, "Invalid datatype in _mesa_convert_colors");
1578 * Perform basic clipping for glDrawPixels. The image's position and size
1579 * and the unpack SkipPixels and SkipRows are adjusted so that the image
1580 * region is entirely within the window and scissor bounds.
1581 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
1582 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
1583 * we'll actually write. Beforehand, *destY-1 is the first drawing row.
1585 * \return GL_TRUE if image is ready for drawing or
1586 * GL_FALSE if image was completely clipped away (draw nothing)
1589 _mesa_clip_drawpixels(const struct gl_context *ctx,
1590 GLint *destX, GLint *destY,
1591 GLsizei *width, GLsizei *height,
1592 struct gl_pixelstore_attrib *unpack)
1594 const struct gl_framebuffer *buffer = ctx->DrawBuffer;
1596 if (unpack->RowLength == 0) {
1597 unpack->RowLength = *width;
1600 ASSERT(ctx->Pixel.ZoomX == 1.0F);
1601 ASSERT(ctx->Pixel.ZoomY == 1.0F || ctx->Pixel.ZoomY == -1.0F);
1604 if (*destX < buffer->_Xmin) {
1605 unpack->SkipPixels += (buffer->_Xmin - *destX);
1606 *width -= (buffer->_Xmin - *destX);
1607 *destX = buffer->_Xmin;
1609 /* right clipping */
1610 if (*destX + *width > buffer->_Xmax)
1611 *width -= (*destX + *width - buffer->_Xmax);
1616 if (ctx->Pixel.ZoomY == 1.0F) {
1617 /* bottom clipping */
1618 if (*destY < buffer->_Ymin) {
1619 unpack->SkipRows += (buffer->_Ymin - *destY);
1620 *height -= (buffer->_Ymin - *destY);
1621 *destY = buffer->_Ymin;
1624 if (*destY + *height > buffer->_Ymax)
1625 *height -= (*destY + *height - buffer->_Ymax);
1627 else { /* upside down */
1629 if (*destY > buffer->_Ymax) {
1630 unpack->SkipRows += (*destY - buffer->_Ymax);
1631 *height -= (*destY - buffer->_Ymax);
1632 *destY = buffer->_Ymax;
1634 /* bottom clipping */
1635 if (*destY - *height < buffer->_Ymin)
1636 *height -= (buffer->_Ymin - (*destY - *height));
1637 /* adjust destY so it's the first row to write to */
1649 * Perform clipping for glReadPixels. The image's window position
1650 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
1651 * so that the image region is entirely within the window bounds.
1652 * Note: this is different from _mesa_clip_drawpixels() in that the
1653 * scissor box is ignored, and we use the bounds of the current readbuffer
1656 * \return GL_TRUE if region to read is in bounds
1657 * GL_FALSE if region is completely out of bounds (nothing to read)
1660 _mesa_clip_readpixels(const struct gl_context *ctx,
1661 GLint *srcX, GLint *srcY,
1662 GLsizei *width, GLsizei *height,
1663 struct gl_pixelstore_attrib *pack)
1665 const struct gl_framebuffer *buffer = ctx->ReadBuffer;
1667 if (pack->RowLength == 0) {
1668 pack->RowLength = *width;
1673 pack->SkipPixels += (0 - *srcX);
1674 *width -= (0 - *srcX);
1677 /* right clipping */
1678 if (*srcX + *width > (GLsizei) buffer->Width)
1679 *width -= (*srcX + *width - buffer->Width);
1684 /* bottom clipping */
1686 pack->SkipRows += (0 - *srcY);
1687 *height -= (0 - *srcY);
1691 if (*srcY + *height > (GLsizei) buffer->Height)
1692 *height -= (*srcY + *height - buffer->Height);
1702 * Do clipping for a glCopyTexSubImage call.
1703 * The framebuffer source region might extend outside the framebuffer
1704 * bounds. Clip the source region against the framebuffer bounds and
1705 * adjust the texture/dest position and size accordingly.
1707 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
1710 _mesa_clip_copytexsubimage(const struct gl_context *ctx,
1711 GLint *destX, GLint *destY,
1712 GLint *srcX, GLint *srcY,
1713 GLsizei *width, GLsizei *height)
1715 const struct gl_framebuffer *fb = ctx->ReadBuffer;
1716 const GLint srcX0 = *srcX, srcY0 = *srcY;
1718 if (_mesa_clip_to_region(0, 0, fb->Width, fb->Height,
1719 srcX, srcY, width, height)) {
1720 *destX = *destX + *srcX - srcX0;
1721 *destY = *destY + *srcY - srcY0;
1733 * Clip the rectangle defined by (x, y, width, height) against the bounds
1734 * specified by [xmin, xmax) and [ymin, ymax).
1735 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
1738 _mesa_clip_to_region(GLint xmin, GLint ymin,
1739 GLint xmax, GLint ymax,
1741 GLsizei *width, GLsizei *height )
1745 *width -= (xmin - *x);
1749 /* right clipping */
1750 if (*x + *width > xmax)
1751 *width -= (*x + *width - xmax);
1756 /* bottom (or top) clipping */
1758 *height -= (ymin - *y);
1762 /* top (or bottom) clipping */
1763 if (*y + *height > ymax)
1764 *height -= (*y + *height - ymax);
1774 * Clip dst coords against Xmax (or Ymax).
1777 clip_right_or_top(GLint *srcX0, GLint *srcX1,
1778 GLint *dstX0, GLint *dstX1,
1783 if (*dstX1 > maxValue) {
1784 /* X1 outside right edge */
1785 ASSERT(*dstX0 < maxValue); /* X0 should be inside right edge */
1786 t = (GLfloat) (maxValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0);
1787 /* chop off [t, 1] part */
1788 ASSERT(t >= 0.0 && t <= 1.0);
1790 bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F;
1791 *srcX1 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias);
1793 else if (*dstX0 > maxValue) {
1794 /* X0 outside right edge */
1795 ASSERT(*dstX1 < maxValue); /* X1 should be inside right edge */
1796 t = (GLfloat) (maxValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1);
1797 /* chop off [t, 1] part */
1798 ASSERT(t >= 0.0 && t <= 1.0);
1800 bias = (*srcX0 < *srcX1) ? -0.5F : 0.5F;
1801 *srcX0 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias);
1807 * Clip dst coords against Xmin (or Ymin).
1810 clip_left_or_bottom(GLint *srcX0, GLint *srcX1,
1811 GLint *dstX0, GLint *dstX1,
1816 if (*dstX0 < minValue) {
1817 /* X0 outside left edge */
1818 ASSERT(*dstX1 > minValue); /* X1 should be inside left edge */
1819 t = (GLfloat) (minValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0);
1820 /* chop off [0, t] part */
1821 ASSERT(t >= 0.0 && t <= 1.0);
1823 bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F; /* flipped??? */
1824 *srcX0 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias);
1826 else if (*dstX1 < minValue) {
1827 /* X1 outside left edge */
1828 ASSERT(*dstX0 > minValue); /* X0 should be inside left edge */
1829 t = (GLfloat) (minValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1);
1830 /* chop off [0, t] part */
1831 ASSERT(t >= 0.0 && t <= 1.0);
1833 bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F;
1834 *srcX1 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias);
1840 * Do clipping of blit src/dest rectangles.
1841 * The dest rect is clipped against both the buffer bounds and scissor bounds.
1842 * The src rect is just clipped against the buffer bounds.
1844 * When either the src or dest rect is clipped, the other is also clipped
1847 * Note that X0 need not be less than X1 (same for Y) for either the source
1848 * and dest rects. That makes the clipping a little trickier.
1850 * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped
1853 _mesa_clip_blit(struct gl_context *ctx,
1854 GLint *srcX0, GLint *srcY0, GLint *srcX1, GLint *srcY1,
1855 GLint *dstX0, GLint *dstY0, GLint *dstX1, GLint *dstY1)
1857 const GLint srcXmin = 0;
1858 const GLint srcXmax = ctx->ReadBuffer->Width;
1859 const GLint srcYmin = 0;
1860 const GLint srcYmax = ctx->ReadBuffer->Height;
1862 /* these include scissor bounds */
1863 const GLint dstXmin = ctx->DrawBuffer->_Xmin;
1864 const GLint dstXmax = ctx->DrawBuffer->_Xmax;
1865 const GLint dstYmin = ctx->DrawBuffer->_Ymin;
1866 const GLint dstYmax = ctx->DrawBuffer->_Ymax;
1869 printf("PreClipX: src: %d .. %d dst: %d .. %d\n",
1870 *srcX0, *srcX1, *dstX0, *dstX1);
1871 printf("PreClipY: src: %d .. %d dst: %d .. %d\n",
1872 *srcY0, *srcY1, *dstY0, *dstY1);
1875 /* trivial rejection tests */
1876 if (*dstX0 == *dstX1)
1877 return GL_FALSE; /* no width */
1878 if (*dstX0 <= dstXmin && *dstX1 <= dstXmin)
1879 return GL_FALSE; /* totally out (left) of bounds */
1880 if (*dstX0 >= dstXmax && *dstX1 >= dstXmax)
1881 return GL_FALSE; /* totally out (right) of bounds */
1883 if (*dstY0 == *dstY1)
1885 if (*dstY0 <= dstYmin && *dstY1 <= dstYmin)
1887 if (*dstY0 >= dstYmax && *dstY1 >= dstYmax)
1890 if (*srcX0 == *srcX1)
1892 if (*srcX0 <= srcXmin && *srcX1 <= srcXmin)
1894 if (*srcX0 >= srcXmax && *srcX1 >= srcXmax)
1897 if (*srcY0 == *srcY1)
1899 if (*srcY0 <= srcYmin && *srcY1 <= srcYmin)
1901 if (*srcY0 >= srcYmax && *srcY1 >= srcYmax)
1907 clip_right_or_top(srcX0, srcX1, dstX0, dstX1, dstXmax);
1908 clip_right_or_top(srcY0, srcY1, dstY0, dstY1, dstYmax);
1909 clip_left_or_bottom(srcX0, srcX1, dstX0, dstX1, dstXmin);
1910 clip_left_or_bottom(srcY0, srcY1, dstY0, dstY1, dstYmin);
1913 * src clip (just swap src/dst values from above)
1915 clip_right_or_top(dstX0, dstX1, srcX0, srcX1, srcXmax);
1916 clip_right_or_top(dstY0, dstY1, srcY0, srcY1, srcYmax);
1917 clip_left_or_bottom(dstX0, dstX1, srcX0, srcX1, srcXmin);
1918 clip_left_or_bottom(dstY0, dstY1, srcY0, srcY1, srcYmin);
1921 printf("PostClipX: src: %d .. %d dst: %d .. %d\n",
1922 *srcX0, *srcX1, *dstX0, *dstX1);
1923 printf("PostClipY: src: %d .. %d dst: %d .. %d\n",
1924 *srcY0, *srcY1, *dstY0, *dstY1);
1927 ASSERT(*dstX0 >= dstXmin);
1928 ASSERT(*dstX0 <= dstXmax);
1929 ASSERT(*dstX1 >= dstXmin);
1930 ASSERT(*dstX1 <= dstXmax);
1932 ASSERT(*dstY0 >= dstYmin);
1933 ASSERT(*dstY0 <= dstYmax);
1934 ASSERT(*dstY1 >= dstYmin);
1935 ASSERT(*dstY1 <= dstYmax);
1937 ASSERT(*srcX0 >= srcXmin);
1938 ASSERT(*srcX0 <= srcXmax);
1939 ASSERT(*srcX1 >= srcXmin);
1940 ASSERT(*srcX1 <= srcXmax);
1942 ASSERT(*srcY0 >= srcYmin);
1943 ASSERT(*srcY0 <= srcYmax);
1944 ASSERT(*srcY1 >= srcYmin);
1945 ASSERT(*srcY1 <= srcYmax);