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.
43 * Normally, BYTE_TO_FLOAT(0) returns 0.00392 That causes problems when
44 * we later convert the float to a packed integer value (such as for
45 * GL_RGB5_A1) because we'll wind up with a non-zero value.
47 * We redefine the macros here so zero is handled correctly.
50 #define BYTE_TO_FLOAT(B) ((B) == 0 ? 0.0F : ((2.0F * (B) + 1.0F) * (1.0F/255.0F)))
53 #define SHORT_TO_FLOAT(S) ((S) == 0 ? 0.0F : ((2.0F * (S) + 1.0F) * (1.0F/65535.0F)))
57 /** Compute ceiling of integer quotient of A divided by B. */
58 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
62 * \return GL_TRUE if type is packed pixel type, GL_FALSE otherwise.
65 _mesa_type_is_packed(GLenum type
)
68 case GL_UNSIGNED_BYTE_3_3_2
:
69 case GL_UNSIGNED_BYTE_2_3_3_REV
:
70 case GL_UNSIGNED_SHORT_5_6_5
:
71 case GL_UNSIGNED_SHORT_5_6_5_REV
:
72 case GL_UNSIGNED_SHORT_4_4_4_4
:
73 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
74 case GL_UNSIGNED_SHORT_5_5_5_1
:
75 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
76 case GL_UNSIGNED_INT_8_8_8_8
:
77 case GL_UNSIGNED_INT_8_8_8_8_REV
:
78 case GL_UNSIGNED_INT_10_10_10_2
:
79 case GL_UNSIGNED_INT_2_10_10_10_REV
:
80 case GL_UNSIGNED_SHORT_8_8_MESA
:
81 case GL_UNSIGNED_SHORT_8_8_REV_MESA
:
82 case GL_UNSIGNED_INT_24_8_EXT
:
92 * Flip the order of the 2 bytes in each word in the given array.
95 * \param n number of words.
98 _mesa_swap2( GLushort
*p
, GLuint n
)
101 for (i
= 0; i
< n
; i
++) {
102 p
[i
] = (p
[i
] >> 8) | ((p
[i
] << 8) & 0xff00);
109 * Flip the order of the 4 bytes in each word in the given array.
112 _mesa_swap4( GLuint
*p
, GLuint n
)
115 for (i
= 0; i
< n
; i
++) {
118 | ((b
>> 8) & 0xff00)
119 | ((b
<< 8) & 0xff0000)
120 | ((b
<< 24) & 0xff000000);
127 * Get the size of a GL data type.
129 * \param type GL data type.
131 * \return the size, in bytes, of the given data type, 0 if a GL_BITMAP, or -1
132 * if an invalid type enum.
135 _mesa_sizeof_type( GLenum type
)
140 case GL_UNSIGNED_BYTE
:
141 return sizeof(GLubyte
);
143 return sizeof(GLbyte
);
144 case GL_UNSIGNED_SHORT
:
145 return sizeof(GLushort
);
147 return sizeof(GLshort
);
148 case GL_UNSIGNED_INT
:
149 return sizeof(GLuint
);
151 return sizeof(GLint
);
153 return sizeof(GLfloat
);
155 return sizeof(GLdouble
);
156 case GL_HALF_FLOAT_ARB
:
157 return sizeof(GLhalfARB
);
159 return sizeof(GLfixed
);
167 * Same as _mesa_sizeof_type() but also accepting the packed pixel
171 _mesa_sizeof_packed_type( GLenum type
)
176 case GL_UNSIGNED_BYTE
:
177 return sizeof(GLubyte
);
179 return sizeof(GLbyte
);
180 case GL_UNSIGNED_SHORT
:
181 return sizeof(GLushort
);
183 return sizeof(GLshort
);
184 case GL_UNSIGNED_INT
:
185 return sizeof(GLuint
);
187 return sizeof(GLint
);
188 case GL_HALF_FLOAT_ARB
:
189 return sizeof(GLhalfARB
);
191 return sizeof(GLfloat
);
192 case GL_UNSIGNED_BYTE_3_3_2
:
193 return sizeof(GLubyte
);
194 case GL_UNSIGNED_BYTE_2_3_3_REV
:
195 return sizeof(GLubyte
);
196 case GL_UNSIGNED_SHORT_5_6_5
:
197 return sizeof(GLushort
);
198 case GL_UNSIGNED_SHORT_5_6_5_REV
:
199 return sizeof(GLushort
);
200 case GL_UNSIGNED_SHORT_4_4_4_4
:
201 return sizeof(GLushort
);
202 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
203 return sizeof(GLushort
);
204 case GL_UNSIGNED_SHORT_5_5_5_1
:
205 return sizeof(GLushort
);
206 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
207 return sizeof(GLushort
);
208 case GL_UNSIGNED_INT_8_8_8_8
:
209 return sizeof(GLuint
);
210 case GL_UNSIGNED_INT_8_8_8_8_REV
:
211 return sizeof(GLuint
);
212 case GL_UNSIGNED_INT_10_10_10_2
:
213 return sizeof(GLuint
);
214 case GL_UNSIGNED_INT_2_10_10_10_REV
:
215 return sizeof(GLuint
);
216 case GL_UNSIGNED_SHORT_8_8_MESA
:
217 case GL_UNSIGNED_SHORT_8_8_REV_MESA
:
218 return sizeof(GLushort
);
219 case GL_UNSIGNED_INT_24_8_EXT
:
220 return sizeof(GLuint
);
228 * Get the number of components in a pixel format.
230 * \param format pixel format.
232 * \return the number of components in the given format, or -1 if a bad format.
235 _mesa_components_in_format( GLenum format
)
239 case GL_COLOR_INDEX1_EXT
:
240 case GL_COLOR_INDEX2_EXT
:
241 case GL_COLOR_INDEX4_EXT
:
242 case GL_COLOR_INDEX8_EXT
:
243 case GL_COLOR_INDEX12_EXT
:
244 case GL_COLOR_INDEX16_EXT
:
245 case GL_STENCIL_INDEX
:
246 case GL_DEPTH_COMPONENT
:
248 case GL_RED_INTEGER_EXT
:
250 case GL_GREEN_INTEGER_EXT
:
252 case GL_BLUE_INTEGER_EXT
:
254 case GL_ALPHA_INTEGER_EXT
:
256 case GL_LUMINANCE_INTEGER_EXT
:
259 case GL_LUMINANCE_ALPHA
:
260 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
264 case GL_RGB_INTEGER_EXT
:
267 case GL_RGBA_INTEGER_EXT
:
277 case GL_DEPTH_STENCIL_EXT
:
289 * Get the bytes per pixel of pixel format type pair.
291 * \param format pixel format.
292 * \param type pixel type.
294 * \return bytes per pixel, or -1 if a bad format or type was given.
297 _mesa_bytes_per_pixel( GLenum format
, GLenum type
)
299 GLint comps
= _mesa_components_in_format( format
);
305 return 0; /* special case */
307 case GL_UNSIGNED_BYTE
:
308 return comps
* sizeof(GLubyte
);
310 case GL_UNSIGNED_SHORT
:
311 return comps
* sizeof(GLshort
);
313 case GL_UNSIGNED_INT
:
314 return comps
* sizeof(GLint
);
316 return comps
* sizeof(GLfloat
);
317 case GL_HALF_FLOAT_ARB
:
318 return comps
* sizeof(GLhalfARB
);
319 case GL_UNSIGNED_BYTE_3_3_2
:
320 case GL_UNSIGNED_BYTE_2_3_3_REV
:
321 if (format
== GL_RGB
|| format
== GL_BGR
||
322 format
== GL_RGB_INTEGER_EXT
|| format
== GL_BGR_INTEGER_EXT
)
323 return sizeof(GLubyte
);
325 return -1; /* error */
326 case GL_UNSIGNED_SHORT_5_6_5
:
327 case GL_UNSIGNED_SHORT_5_6_5_REV
:
328 if (format
== GL_RGB
|| format
== GL_BGR
||
329 format
== GL_RGB_INTEGER_EXT
|| format
== GL_BGR_INTEGER_EXT
)
330 return sizeof(GLushort
);
332 return -1; /* error */
333 case GL_UNSIGNED_SHORT_4_4_4_4
:
334 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
335 case GL_UNSIGNED_SHORT_5_5_5_1
:
336 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
337 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
||
338 format
== GL_RGBA_INTEGER_EXT
|| format
== GL_BGRA_INTEGER_EXT
)
339 return sizeof(GLushort
);
342 case GL_UNSIGNED_INT_8_8_8_8
:
343 case GL_UNSIGNED_INT_8_8_8_8_REV
:
344 case GL_UNSIGNED_INT_10_10_10_2
:
345 case GL_UNSIGNED_INT_2_10_10_10_REV
:
346 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
||
347 format
== GL_RGBA_INTEGER_EXT
|| format
== GL_BGRA_INTEGER_EXT
)
348 return sizeof(GLuint
);
351 case GL_UNSIGNED_SHORT_8_8_MESA
:
352 case GL_UNSIGNED_SHORT_8_8_REV_MESA
:
353 if (format
== GL_YCBCR_MESA
)
354 return sizeof(GLushort
);
357 case GL_UNSIGNED_INT_24_8_EXT
:
358 if (format
== GL_DEPTH_STENCIL_EXT
)
359 return sizeof(GLuint
);
369 * Test for a legal pixel format and type.
371 * \param format pixel format.
372 * \param type pixel type.
374 * \return GL_TRUE if the given pixel format and type are legal, or GL_FALSE
378 _mesa_is_legal_format_and_type(const struct gl_context
*ctx
,
379 GLenum format
, GLenum type
)
383 case GL_STENCIL_INDEX
:
387 case GL_UNSIGNED_BYTE
:
389 case GL_UNSIGNED_SHORT
:
391 case GL_UNSIGNED_INT
:
394 case GL_HALF_FLOAT_ARB
:
395 return ctx
->Extensions
.ARB_half_float_pixel
;
403 #if 0 /* not legal! see table 3.6 of the 1.5 spec */
407 case GL_LUMINANCE_ALPHA
:
408 case GL_DEPTH_COMPONENT
:
411 case GL_UNSIGNED_BYTE
:
413 case GL_UNSIGNED_SHORT
:
415 case GL_UNSIGNED_INT
:
418 case GL_HALF_FLOAT_ARB
:
419 return ctx
->Extensions
.ARB_half_float_pixel
;
424 if (!ctx
->Extensions
.ARB_texture_rg
)
429 case GL_UNSIGNED_BYTE
:
431 case GL_UNSIGNED_SHORT
:
433 case GL_UNSIGNED_INT
:
436 case GL_HALF_FLOAT_ARB
:
437 return ctx
->Extensions
.ARB_half_float_pixel
;
444 case GL_UNSIGNED_BYTE
:
446 case GL_UNSIGNED_SHORT
:
448 case GL_UNSIGNED_INT
:
450 case GL_UNSIGNED_BYTE_3_3_2
:
451 case GL_UNSIGNED_BYTE_2_3_3_REV
:
452 case GL_UNSIGNED_SHORT_5_6_5
:
453 case GL_UNSIGNED_SHORT_5_6_5_REV
:
455 case GL_HALF_FLOAT_ARB
:
456 return ctx
->Extensions
.ARB_half_float_pixel
;
462 /* NOTE: no packed types are supported with BGR. That's
463 * intentional, according to the GL spec.
466 case GL_UNSIGNED_BYTE
:
468 case GL_UNSIGNED_SHORT
:
470 case GL_UNSIGNED_INT
:
473 case GL_HALF_FLOAT_ARB
:
474 return ctx
->Extensions
.ARB_half_float_pixel
;
483 case GL_UNSIGNED_BYTE
:
485 case GL_UNSIGNED_SHORT
:
487 case GL_UNSIGNED_INT
:
489 case GL_UNSIGNED_SHORT_4_4_4_4
:
490 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
491 case GL_UNSIGNED_SHORT_5_5_5_1
:
492 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
493 case GL_UNSIGNED_INT_8_8_8_8
:
494 case GL_UNSIGNED_INT_8_8_8_8_REV
:
495 case GL_UNSIGNED_INT_10_10_10_2
:
496 case GL_UNSIGNED_INT_2_10_10_10_REV
:
498 case GL_HALF_FLOAT_ARB
:
499 return ctx
->Extensions
.ARB_half_float_pixel
;
504 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
||
505 type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
)
509 case GL_DEPTH_STENCIL_EXT
:
510 if (ctx
->Extensions
.EXT_packed_depth_stencil
511 && type
== GL_UNSIGNED_INT_24_8_EXT
)
519 case GL_UNSIGNED_BYTE
:
521 case GL_UNSIGNED_SHORT
:
523 case GL_UNSIGNED_INT
:
530 /* integer-valued formats */
531 case GL_RED_INTEGER_EXT
:
532 case GL_GREEN_INTEGER_EXT
:
533 case GL_BLUE_INTEGER_EXT
:
534 case GL_ALPHA_INTEGER_EXT
:
537 case GL_UNSIGNED_BYTE
:
539 case GL_UNSIGNED_SHORT
:
541 case GL_UNSIGNED_INT
:
542 return ctx
->Extensions
.EXT_texture_integer
;
547 case GL_RGB_INTEGER_EXT
:
550 case GL_UNSIGNED_BYTE
:
552 case GL_UNSIGNED_SHORT
:
554 case GL_UNSIGNED_INT
:
555 case GL_UNSIGNED_BYTE_3_3_2
:
556 case GL_UNSIGNED_BYTE_2_3_3_REV
:
557 case GL_UNSIGNED_SHORT_5_6_5
:
558 case GL_UNSIGNED_SHORT_5_6_5_REV
:
559 return ctx
->Extensions
.EXT_texture_integer
;
564 case GL_BGR_INTEGER_EXT
:
567 case GL_UNSIGNED_BYTE
:
569 case GL_UNSIGNED_SHORT
:
571 case GL_UNSIGNED_INT
:
572 /* NOTE: no packed formats w/ BGR format */
573 return ctx
->Extensions
.EXT_texture_integer
;
578 case GL_RGBA_INTEGER_EXT
:
579 case GL_BGRA_INTEGER_EXT
:
582 case GL_UNSIGNED_BYTE
:
584 case GL_UNSIGNED_SHORT
:
586 case GL_UNSIGNED_INT
:
587 case GL_UNSIGNED_SHORT_4_4_4_4
:
588 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
589 case GL_UNSIGNED_SHORT_5_5_5_1
:
590 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
591 case GL_UNSIGNED_INT_8_8_8_8
:
592 case GL_UNSIGNED_INT_8_8_8_8_REV
:
593 case GL_UNSIGNED_INT_10_10_10_2
:
594 case GL_UNSIGNED_INT_2_10_10_10_REV
:
595 return ctx
->Extensions
.EXT_texture_integer
;
600 case GL_LUMINANCE_INTEGER_EXT
:
601 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
604 case GL_UNSIGNED_BYTE
:
606 case GL_UNSIGNED_SHORT
:
608 case GL_UNSIGNED_INT
:
609 return ctx
->Extensions
.EXT_texture_integer
;
622 * Test if the given image format is a color/RGBA format (i.e., not color
623 * index, depth, stencil, etc).
624 * \param format the image format value (may by an internal texture format)
625 * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
628 _mesa_is_color_format(GLenum format
)
646 case GL_LUMINANCE_ALPHA
:
647 case GL_LUMINANCE4_ALPHA4
:
648 case GL_LUMINANCE6_ALPHA2
:
649 case GL_LUMINANCE8_ALPHA8
:
650 case GL_LUMINANCE12_ALPHA4
:
651 case GL_LUMINANCE12_ALPHA12
:
652 case GL_LUMINANCE16_ALPHA16
:
684 /* float texture formats */
685 case GL_ALPHA16F_ARB
:
686 case GL_ALPHA32F_ARB
:
687 case GL_LUMINANCE16F_ARB
:
688 case GL_LUMINANCE32F_ARB
:
689 case GL_LUMINANCE_ALPHA16F_ARB
:
690 case GL_LUMINANCE_ALPHA32F_ARB
:
691 case GL_INTENSITY16F_ARB
:
692 case GL_INTENSITY32F_ARB
:
701 /* compressed formats */
702 case GL_COMPRESSED_ALPHA
:
703 case GL_COMPRESSED_LUMINANCE
:
704 case GL_COMPRESSED_LUMINANCE_ALPHA
:
705 case GL_COMPRESSED_INTENSITY
:
706 case GL_COMPRESSED_RED
:
707 case GL_COMPRESSED_RG
:
708 case GL_COMPRESSED_RGB
:
709 case GL_COMPRESSED_RGBA
:
714 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
715 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
716 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
717 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
718 case GL_COMPRESSED_RGB_FXT1_3DFX
:
719 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
720 #if FEATURE_EXT_texture_sRGB
723 case GL_SRGB_ALPHA_EXT
:
724 case GL_SRGB8_ALPHA8_EXT
:
725 case GL_SLUMINANCE_ALPHA_EXT
:
726 case GL_SLUMINANCE8_ALPHA8_EXT
:
727 case GL_SLUMINANCE_EXT
:
728 case GL_SLUMINANCE8_EXT
:
729 case GL_COMPRESSED_SRGB_EXT
:
730 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
731 case GL_COMPRESSED_SRGB_ALPHA_EXT
:
732 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
733 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
734 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
735 case GL_COMPRESSED_SLUMINANCE_EXT
:
736 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT
:
737 #endif /* FEATURE_EXT_texture_sRGB */
738 case GL_COMPRESSED_RED_RGTC1
:
739 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
740 case GL_COMPRESSED_RG_RGTC2
:
741 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
742 /* signed, normalized texture formats */
745 /* generic integer formats */
746 case GL_RED_INTEGER_EXT
:
747 case GL_GREEN_INTEGER_EXT
:
748 case GL_BLUE_INTEGER_EXT
:
749 case GL_ALPHA_INTEGER_EXT
:
750 case GL_RGB_INTEGER_EXT
:
751 case GL_RGBA_INTEGER_EXT
:
752 case GL_BGR_INTEGER_EXT
:
753 case GL_BGRA_INTEGER_EXT
:
754 case GL_LUMINANCE_INTEGER_EXT
:
755 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
756 /* sized integer formats */
757 case GL_RGBA32UI_EXT
:
759 case GL_ALPHA32UI_EXT
:
760 case GL_INTENSITY32UI_EXT
:
761 case GL_LUMINANCE32UI_EXT
:
762 case GL_LUMINANCE_ALPHA32UI_EXT
:
763 case GL_RGBA16UI_EXT
:
765 case GL_ALPHA16UI_EXT
:
766 case GL_INTENSITY16UI_EXT
:
767 case GL_LUMINANCE16UI_EXT
:
768 case GL_LUMINANCE_ALPHA16UI_EXT
:
771 case GL_ALPHA8UI_EXT
:
772 case GL_INTENSITY8UI_EXT
:
773 case GL_LUMINANCE8UI_EXT
:
774 case GL_LUMINANCE_ALPHA8UI_EXT
:
777 case GL_ALPHA32I_EXT
:
778 case GL_INTENSITY32I_EXT
:
779 case GL_LUMINANCE32I_EXT
:
780 case GL_LUMINANCE_ALPHA32I_EXT
:
783 case GL_ALPHA16I_EXT
:
784 case GL_INTENSITY16I_EXT
:
785 case GL_LUMINANCE16I_EXT
:
786 case GL_LUMINANCE_ALPHA16I_EXT
:
790 case GL_INTENSITY8I_EXT
:
791 case GL_LUMINANCE8I_EXT
:
792 case GL_LUMINANCE_ALPHA8I_EXT
:
794 case GL_YCBCR_MESA
: /* not considered to be RGB */
803 * Test if the given image format is a color index format.
806 _mesa_is_index_format(GLenum format
)
810 case GL_COLOR_INDEX1_EXT
:
811 case GL_COLOR_INDEX2_EXT
:
812 case GL_COLOR_INDEX4_EXT
:
813 case GL_COLOR_INDEX8_EXT
:
814 case GL_COLOR_INDEX12_EXT
:
815 case GL_COLOR_INDEX16_EXT
:
824 * Test if the given image format is a depth component format.
827 _mesa_is_depth_format(GLenum format
)
830 case GL_DEPTH_COMPONENT
:
831 case GL_DEPTH_COMPONENT16
:
832 case GL_DEPTH_COMPONENT24
:
833 case GL_DEPTH_COMPONENT32
:
842 * Test if the given image format is a stencil format.
845 _mesa_is_stencil_format(GLenum format
)
848 case GL_STENCIL_INDEX
:
849 case GL_DEPTH_STENCIL
:
858 * Test if the given image format is a YCbCr format.
861 _mesa_is_ycbcr_format(GLenum format
)
873 * Test if the given image format is a depth+stencil format.
876 _mesa_is_depthstencil_format(GLenum format
)
879 case GL_DEPTH24_STENCIL8_EXT
:
880 case GL_DEPTH_STENCIL_EXT
:
889 * Test if the given image format is a depth or stencil format.
892 _mesa_is_depth_or_stencil_format(GLenum format
)
895 case GL_DEPTH_COMPONENT
:
896 case GL_DEPTH_COMPONENT16
:
897 case GL_DEPTH_COMPONENT24
:
898 case GL_DEPTH_COMPONENT32
:
899 case GL_STENCIL_INDEX
:
900 case GL_STENCIL_INDEX1_EXT
:
901 case GL_STENCIL_INDEX4_EXT
:
902 case GL_STENCIL_INDEX8_EXT
:
903 case GL_STENCIL_INDEX16_EXT
:
904 case GL_DEPTH_STENCIL_EXT
:
905 case GL_DEPTH24_STENCIL8_EXT
:
914 * Test if the given image format is a dudv format.
917 _mesa_is_dudv_format(GLenum format
)
930 * Test if the given format is an integer (non-normalized) format.
933 _mesa_is_integer_format(GLenum format
)
936 /* generic integer formats */
937 case GL_RED_INTEGER_EXT
:
938 case GL_GREEN_INTEGER_EXT
:
939 case GL_BLUE_INTEGER_EXT
:
940 case GL_ALPHA_INTEGER_EXT
:
941 case GL_RGB_INTEGER_EXT
:
942 case GL_RGBA_INTEGER_EXT
:
943 case GL_BGR_INTEGER_EXT
:
944 case GL_BGRA_INTEGER_EXT
:
945 case GL_LUMINANCE_INTEGER_EXT
:
946 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
947 /* specific integer formats */
948 case GL_RGBA32UI_EXT
:
950 case GL_ALPHA32UI_EXT
:
951 case GL_INTENSITY32UI_EXT
:
952 case GL_LUMINANCE32UI_EXT
:
953 case GL_LUMINANCE_ALPHA32UI_EXT
:
954 case GL_RGBA16UI_EXT
:
956 case GL_ALPHA16UI_EXT
:
957 case GL_INTENSITY16UI_EXT
:
958 case GL_LUMINANCE16UI_EXT
:
959 case GL_LUMINANCE_ALPHA16UI_EXT
:
962 case GL_ALPHA8UI_EXT
:
963 case GL_INTENSITY8UI_EXT
:
964 case GL_LUMINANCE8UI_EXT
:
965 case GL_LUMINANCE_ALPHA8UI_EXT
:
968 case GL_ALPHA32I_EXT
:
969 case GL_INTENSITY32I_EXT
:
970 case GL_LUMINANCE32I_EXT
:
971 case GL_LUMINANCE_ALPHA32I_EXT
:
974 case GL_ALPHA16I_EXT
:
975 case GL_INTENSITY16I_EXT
:
976 case GL_LUMINANCE16I_EXT
:
977 case GL_LUMINANCE_ALPHA16I_EXT
:
981 case GL_INTENSITY8I_EXT
:
982 case GL_LUMINANCE8I_EXT
:
983 case GL_LUMINANCE_ALPHA8I_EXT
:
992 * Test if an image format is a supported compressed format.
993 * \param format the internal format token provided by the user.
994 * \return GL_TRUE if compressed, GL_FALSE if uncompressed
997 _mesa_is_compressed_format(struct gl_context
*ctx
, GLenum format
)
1000 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
1001 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
1002 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
1003 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
1004 return ctx
->Extensions
.EXT_texture_compression_s3tc
;
1009 return ctx
->Extensions
.S3_s3tc
;
1010 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
1011 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
1012 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
1013 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
1014 return ctx
->Extensions
.EXT_texture_sRGB
1015 && ctx
->Extensions
.EXT_texture_compression_s3tc
;
1016 case GL_COMPRESSED_RGB_FXT1_3DFX
:
1017 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
1018 return ctx
->Extensions
.TDFX_texture_compression_FXT1
;
1019 case GL_COMPRESSED_RED_RGTC1
:
1020 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
1021 case GL_COMPRESSED_RG_RGTC2
:
1022 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
1023 return ctx
->Extensions
.ARB_texture_compression_rgtc
;
1031 * Return the address of a specific pixel in an image (1D, 2D or 3D).
1033 * Pixel unpacking/packing parameters are observed according to \p packing.
1035 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
1036 * \param image starting address of image data
1037 * \param width the image width
1038 * \param height theimage height
1039 * \param format the pixel format
1040 * \param type the pixel data type
1041 * \param packing the pixelstore attributes
1042 * \param img which image in the volume (0 for 1D or 2D images)
1043 * \param row row of pixel in the image (0 for 1D images)
1044 * \param column column of pixel in the image
1046 * \return address of pixel on success, or NULL on error.
1048 * \sa gl_pixelstore_attrib.
1051 _mesa_image_address( GLuint dimensions
,
1052 const struct gl_pixelstore_attrib
*packing
,
1053 const GLvoid
*image
,
1054 GLsizei width
, GLsizei height
,
1055 GLenum format
, GLenum type
,
1056 GLint img
, GLint row
, GLint column
)
1058 GLint alignment
; /* 1, 2 or 4 */
1059 GLint pixels_per_row
;
1060 GLint rows_per_image
;
1063 GLint skipimages
; /* for 3-D volume images */
1064 GLubyte
*pixel_addr
;
1066 ASSERT(dimensions
>= 1 && dimensions
<= 3);
1068 alignment
= packing
->Alignment
;
1069 if (packing
->RowLength
> 0) {
1070 pixels_per_row
= packing
->RowLength
;
1073 pixels_per_row
= width
;
1075 if (packing
->ImageHeight
> 0) {
1076 rows_per_image
= packing
->ImageHeight
;
1079 rows_per_image
= height
;
1082 skippixels
= packing
->SkipPixels
;
1083 /* Note: SKIP_ROWS _is_ used for 1D images */
1084 skiprows
= packing
->SkipRows
;
1085 /* Note: SKIP_IMAGES is only used for 3D images */
1086 skipimages
= (dimensions
== 3) ? packing
->SkipImages
: 0;
1088 if (type
== GL_BITMAP
) {
1090 GLint comp_per_pixel
; /* components per pixel */
1091 GLint bytes_per_comp
; /* bytes per component */
1092 GLint bytes_per_row
;
1093 GLint bytes_per_image
;
1095 /* Compute bytes per component */
1096 bytes_per_comp
= _mesa_sizeof_packed_type( type
);
1097 if (bytes_per_comp
< 0) {
1101 /* Compute number of components per pixel */
1102 comp_per_pixel
= _mesa_components_in_format( format
);
1103 if (comp_per_pixel
< 0) {
1107 bytes_per_row
= alignment
1108 * CEILING( comp_per_pixel
*pixels_per_row
, 8*alignment
);
1110 bytes_per_image
= bytes_per_row
* rows_per_image
;
1112 pixel_addr
= (GLubyte
*) image
1113 + (skipimages
+ img
) * bytes_per_image
1114 + (skiprows
+ row
) * bytes_per_row
1115 + (skippixels
+ column
) / 8;
1118 /* Non-BITMAP data */
1119 GLint bytes_per_pixel
, bytes_per_row
, remainder
, bytes_per_image
;
1122 bytes_per_pixel
= _mesa_bytes_per_pixel( format
, type
);
1124 /* The pixel type and format should have been error checked earlier */
1125 assert(bytes_per_pixel
> 0);
1127 bytes_per_row
= pixels_per_row
* bytes_per_pixel
;
1128 remainder
= bytes_per_row
% alignment
;
1130 bytes_per_row
+= (alignment
- remainder
);
1132 ASSERT(bytes_per_row
% alignment
== 0);
1134 bytes_per_image
= bytes_per_row
* rows_per_image
;
1136 if (packing
->Invert
) {
1137 /* set pixel_addr to the last row */
1138 topOfImage
= bytes_per_row
* (height
- 1);
1139 bytes_per_row
= -bytes_per_row
;
1145 /* compute final pixel address */
1146 pixel_addr
= (GLubyte
*) image
1147 + (skipimages
+ img
) * bytes_per_image
1149 + (skiprows
+ row
) * bytes_per_row
1150 + (skippixels
+ column
) * bytes_per_pixel
;
1153 return (GLvoid
*) pixel_addr
;
1158 _mesa_image_address1d( const struct gl_pixelstore_attrib
*packing
,
1159 const GLvoid
*image
,
1161 GLenum format
, GLenum type
,
1164 return _mesa_image_address(1, packing
, image
, width
, 1,
1165 format
, type
, 0, 0, column
);
1170 _mesa_image_address2d( const struct gl_pixelstore_attrib
*packing
,
1171 const GLvoid
*image
,
1172 GLsizei width
, GLsizei height
,
1173 GLenum format
, GLenum type
,
1174 GLint row
, GLint column
)
1176 return _mesa_image_address(2, packing
, image
, width
, height
,
1177 format
, type
, 0, row
, column
);
1182 _mesa_image_address3d( const struct gl_pixelstore_attrib
*packing
,
1183 const GLvoid
*image
,
1184 GLsizei width
, GLsizei height
,
1185 GLenum format
, GLenum type
,
1186 GLint img
, GLint row
, GLint column
)
1188 return _mesa_image_address(3, packing
, image
, width
, height
,
1189 format
, type
, img
, row
, column
);
1195 * Compute the stride (in bytes) between image rows.
1197 * \param packing the pixelstore attributes
1198 * \param width image width.
1199 * \param format pixel format.
1200 * \param type pixel data type.
1202 * \return the stride in bytes for the given parameters, or -1 if error
1205 _mesa_image_row_stride( const struct gl_pixelstore_attrib
*packing
,
1206 GLint width
, GLenum format
, GLenum type
)
1208 GLint bytesPerRow
, remainder
;
1212 if (type
== GL_BITMAP
) {
1213 if (packing
->RowLength
== 0) {
1214 bytesPerRow
= (width
+ 7) / 8;
1217 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
1221 /* Non-BITMAP data */
1222 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1223 if (bytesPerPixel
<= 0)
1224 return -1; /* error */
1225 if (packing
->RowLength
== 0) {
1226 bytesPerRow
= bytesPerPixel
* width
;
1229 bytesPerRow
= bytesPerPixel
* packing
->RowLength
;
1233 remainder
= bytesPerRow
% packing
->Alignment
;
1234 if (remainder
> 0) {
1235 bytesPerRow
+= (packing
->Alignment
- remainder
);
1238 if (packing
->Invert
) {
1239 /* negate the bytes per row (negative row stride) */
1240 bytesPerRow
= -bytesPerRow
;
1248 * Compute the stride between images in a 3D texture (in bytes) for the given
1249 * pixel packing parameters and image width, format and type.
1252 _mesa_image_image_stride( const struct gl_pixelstore_attrib
*packing
,
1253 GLint width
, GLint height
,
1254 GLenum format
, GLenum type
)
1256 GLint bytesPerRow
, bytesPerImage
, remainder
;
1260 if (type
== GL_BITMAP
) {
1261 if (packing
->RowLength
== 0) {
1262 bytesPerRow
= (width
+ 7) / 8;
1265 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
1269 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1271 if (bytesPerPixel
<= 0)
1272 return -1; /* error */
1273 if (packing
->RowLength
== 0) {
1274 bytesPerRow
= bytesPerPixel
* width
;
1277 bytesPerRow
= bytesPerPixel
* packing
->RowLength
;
1281 remainder
= bytesPerRow
% packing
->Alignment
;
1283 bytesPerRow
+= (packing
->Alignment
- remainder
);
1285 if (packing
->ImageHeight
== 0)
1286 bytesPerImage
= bytesPerRow
* height
;
1288 bytesPerImage
= bytesPerRow
* packing
->ImageHeight
;
1290 return bytesPerImage
;
1296 * "Expand" a bitmap from 1-bit per pixel to 8-bits per pixel.
1297 * This is typically used to convert a bitmap into a GLubyte/pixel texture.
1298 * "On" bits will set texels to \p onValue.
1299 * "Off" bits will not modify texels.
1300 * \param width src bitmap width in pixels
1301 * \param height src bitmap height in pixels
1302 * \param unpack bitmap unpacking state
1303 * \param bitmap the src bitmap data
1304 * \param destBuffer start of dest buffer
1305 * \param destStride row stride in dest buffer
1306 * \param onValue if bit is 1, set destBuffer pixel to this value
1309 _mesa_expand_bitmap(GLsizei width
, GLsizei height
,
1310 const struct gl_pixelstore_attrib
*unpack
,
1311 const GLubyte
*bitmap
,
1312 GLubyte
*destBuffer
, GLint destStride
,
1315 const GLubyte
*srcRow
= (const GLubyte
*)
1316 _mesa_image_address2d(unpack
, bitmap
, width
, height
,
1317 GL_COLOR_INDEX
, GL_BITMAP
, 0, 0);
1318 const GLint srcStride
= _mesa_image_row_stride(unpack
, width
,
1319 GL_COLOR_INDEX
, GL_BITMAP
);
1322 #define SET_PIXEL(COL, ROW) \
1323 destBuffer[(ROW) * destStride + (COL)] = onValue;
1325 for (row
= 0; row
< height
; row
++) {
1326 const GLubyte
*src
= srcRow
;
1328 if (unpack
->LsbFirst
) {
1330 GLubyte mask
= 1U << (unpack
->SkipPixels
& 0x7);
1331 for (col
= 0; col
< width
; col
++) {
1334 SET_PIXEL(col
, row
);
1346 /* get ready for next row */
1352 GLubyte mask
= 128U >> (unpack
->SkipPixels
& 0x7);
1353 for (col
= 0; col
< width
; col
++) {
1356 SET_PIXEL(col
, row
);
1368 /* get ready for next row */
1373 srcRow
+= srcStride
;
1383 * Convert an array of RGBA colors from one datatype to another.
1384 * NOTE: src may equal dst. In that case, we use a temporary buffer.
1387 _mesa_convert_colors(GLenum srcType
, const GLvoid
*src
,
1388 GLenum dstType
, GLvoid
*dst
,
1389 GLuint count
, const GLubyte mask
[])
1391 GLuint tempBuffer
[MAX_WIDTH
][4];
1392 const GLboolean useTemp
= (src
== dst
);
1394 ASSERT(srcType
!= dstType
);
1397 case GL_UNSIGNED_BYTE
:
1398 if (dstType
== GL_UNSIGNED_SHORT
) {
1399 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
1400 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
1402 for (i
= 0; i
< count
; i
++) {
1403 if (!mask
|| mask
[i
]) {
1404 dst2
[i
][RCOMP
] = UBYTE_TO_USHORT(src1
[i
][RCOMP
]);
1405 dst2
[i
][GCOMP
] = UBYTE_TO_USHORT(src1
[i
][GCOMP
]);
1406 dst2
[i
][BCOMP
] = UBYTE_TO_USHORT(src1
[i
][BCOMP
]);
1407 dst2
[i
][ACOMP
] = UBYTE_TO_USHORT(src1
[i
][ACOMP
]);
1411 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
1414 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
1415 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
1417 ASSERT(dstType
== GL_FLOAT
);
1418 for (i
= 0; i
< count
; i
++) {
1419 if (!mask
|| mask
[i
]) {
1420 dst4
[i
][RCOMP
] = UBYTE_TO_FLOAT(src1
[i
][RCOMP
]);
1421 dst4
[i
][GCOMP
] = UBYTE_TO_FLOAT(src1
[i
][GCOMP
]);
1422 dst4
[i
][BCOMP
] = UBYTE_TO_FLOAT(src1
[i
][BCOMP
]);
1423 dst4
[i
][ACOMP
] = UBYTE_TO_FLOAT(src1
[i
][ACOMP
]);
1427 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
1430 case GL_UNSIGNED_SHORT
:
1431 if (dstType
== GL_UNSIGNED_BYTE
) {
1432 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
1433 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
1435 for (i
= 0; i
< count
; i
++) {
1436 if (!mask
|| mask
[i
]) {
1437 dst1
[i
][RCOMP
] = USHORT_TO_UBYTE(src2
[i
][RCOMP
]);
1438 dst1
[i
][GCOMP
] = USHORT_TO_UBYTE(src2
[i
][GCOMP
]);
1439 dst1
[i
][BCOMP
] = USHORT_TO_UBYTE(src2
[i
][BCOMP
]);
1440 dst1
[i
][ACOMP
] = USHORT_TO_UBYTE(src2
[i
][ACOMP
]);
1444 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
1447 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
1448 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
1450 ASSERT(dstType
== GL_FLOAT
);
1451 for (i
= 0; i
< count
; i
++) {
1452 if (!mask
|| mask
[i
]) {
1453 dst4
[i
][RCOMP
] = USHORT_TO_FLOAT(src2
[i
][RCOMP
]);
1454 dst4
[i
][GCOMP
] = USHORT_TO_FLOAT(src2
[i
][GCOMP
]);
1455 dst4
[i
][BCOMP
] = USHORT_TO_FLOAT(src2
[i
][BCOMP
]);
1456 dst4
[i
][ACOMP
] = USHORT_TO_FLOAT(src2
[i
][ACOMP
]);
1460 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
1464 if (dstType
== GL_UNSIGNED_BYTE
) {
1465 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
1466 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
1468 for (i
= 0; i
< count
; i
++) {
1469 if (!mask
|| mask
[i
]) {
1470 UNCLAMPED_FLOAT_TO_UBYTE(dst1
[i
][RCOMP
], src4
[i
][RCOMP
]);
1471 UNCLAMPED_FLOAT_TO_UBYTE(dst1
[i
][GCOMP
], src4
[i
][GCOMP
]);
1472 UNCLAMPED_FLOAT_TO_UBYTE(dst1
[i
][BCOMP
], src4
[i
][BCOMP
]);
1473 UNCLAMPED_FLOAT_TO_UBYTE(dst1
[i
][ACOMP
], src4
[i
][ACOMP
]);
1477 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
1480 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
1481 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
1483 ASSERT(dstType
== GL_UNSIGNED_SHORT
);
1484 for (i
= 0; i
< count
; i
++) {
1485 if (!mask
|| mask
[i
]) {
1486 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][RCOMP
], src4
[i
][RCOMP
]);
1487 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][GCOMP
], src4
[i
][GCOMP
]);
1488 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][BCOMP
], src4
[i
][BCOMP
]);
1489 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][ACOMP
], src4
[i
][ACOMP
]);
1493 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
1497 _mesa_problem(NULL
, "Invalid datatype in _mesa_convert_colors");
1505 * Perform basic clipping for glDrawPixels. The image's position and size
1506 * and the unpack SkipPixels and SkipRows are adjusted so that the image
1507 * region is entirely within the window and scissor bounds.
1508 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
1509 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
1510 * we'll actually write. Beforehand, *destY-1 is the first drawing row.
1512 * \return GL_TRUE if image is ready for drawing or
1513 * GL_FALSE if image was completely clipped away (draw nothing)
1516 _mesa_clip_drawpixels(const struct gl_context
*ctx
,
1517 GLint
*destX
, GLint
*destY
,
1518 GLsizei
*width
, GLsizei
*height
,
1519 struct gl_pixelstore_attrib
*unpack
)
1521 const struct gl_framebuffer
*buffer
= ctx
->DrawBuffer
;
1523 if (unpack
->RowLength
== 0) {
1524 unpack
->RowLength
= *width
;
1527 ASSERT(ctx
->Pixel
.ZoomX
== 1.0F
);
1528 ASSERT(ctx
->Pixel
.ZoomY
== 1.0F
|| ctx
->Pixel
.ZoomY
== -1.0F
);
1531 if (*destX
< buffer
->_Xmin
) {
1532 unpack
->SkipPixels
+= (buffer
->_Xmin
- *destX
);
1533 *width
-= (buffer
->_Xmin
- *destX
);
1534 *destX
= buffer
->_Xmin
;
1536 /* right clipping */
1537 if (*destX
+ *width
> buffer
->_Xmax
)
1538 *width
-= (*destX
+ *width
- buffer
->_Xmax
);
1543 if (ctx
->Pixel
.ZoomY
== 1.0F
) {
1544 /* bottom clipping */
1545 if (*destY
< buffer
->_Ymin
) {
1546 unpack
->SkipRows
+= (buffer
->_Ymin
- *destY
);
1547 *height
-= (buffer
->_Ymin
- *destY
);
1548 *destY
= buffer
->_Ymin
;
1551 if (*destY
+ *height
> buffer
->_Ymax
)
1552 *height
-= (*destY
+ *height
- buffer
->_Ymax
);
1554 else { /* upside down */
1556 if (*destY
> buffer
->_Ymax
) {
1557 unpack
->SkipRows
+= (*destY
- buffer
->_Ymax
);
1558 *height
-= (*destY
- buffer
->_Ymax
);
1559 *destY
= buffer
->_Ymax
;
1561 /* bottom clipping */
1562 if (*destY
- *height
< buffer
->_Ymin
)
1563 *height
-= (buffer
->_Ymin
- (*destY
- *height
));
1564 /* adjust destY so it's the first row to write to */
1576 * Perform clipping for glReadPixels. The image's window position
1577 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
1578 * so that the image region is entirely within the window bounds.
1579 * Note: this is different from _mesa_clip_drawpixels() in that the
1580 * scissor box is ignored, and we use the bounds of the current readbuffer
1583 * \return GL_TRUE if image is ready for drawing or
1584 * GL_FALSE if image was completely clipped away (draw nothing)
1587 _mesa_clip_readpixels(const struct gl_context
*ctx
,
1588 GLint
*srcX
, GLint
*srcY
,
1589 GLsizei
*width
, GLsizei
*height
,
1590 struct gl_pixelstore_attrib
*pack
)
1592 const struct gl_framebuffer
*buffer
= ctx
->ReadBuffer
;
1594 if (pack
->RowLength
== 0) {
1595 pack
->RowLength
= *width
;
1600 pack
->SkipPixels
+= (0 - *srcX
);
1601 *width
-= (0 - *srcX
);
1604 /* right clipping */
1605 if (*srcX
+ *width
> (GLsizei
) buffer
->Width
)
1606 *width
-= (*srcX
+ *width
- buffer
->Width
);
1611 /* bottom clipping */
1613 pack
->SkipRows
+= (0 - *srcY
);
1614 *height
-= (0 - *srcY
);
1618 if (*srcY
+ *height
> (GLsizei
) buffer
->Height
)
1619 *height
-= (*srcY
+ *height
- buffer
->Height
);
1629 * Do clipping for a glCopyTexSubImage call.
1630 * The framebuffer source region might extend outside the framebuffer
1631 * bounds. Clip the source region against the framebuffer bounds and
1632 * adjust the texture/dest position and size accordingly.
1634 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
1637 _mesa_clip_copytexsubimage(const struct gl_context
*ctx
,
1638 GLint
*destX
, GLint
*destY
,
1639 GLint
*srcX
, GLint
*srcY
,
1640 GLsizei
*width
, GLsizei
*height
)
1642 const struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1643 const GLint srcX0
= *srcX
, srcY0
= *srcY
;
1645 if (_mesa_clip_to_region(0, 0, fb
->Width
, fb
->Height
,
1646 srcX
, srcY
, width
, height
)) {
1647 *destX
= *destX
+ *srcX
- srcX0
;
1648 *destY
= *destY
+ *srcY
- srcY0
;
1660 * Clip the rectangle defined by (x, y, width, height) against the bounds
1661 * specified by [xmin, xmax) and [ymin, ymax).
1662 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
1665 _mesa_clip_to_region(GLint xmin
, GLint ymin
,
1666 GLint xmax
, GLint ymax
,
1668 GLsizei
*width
, GLsizei
*height
)
1672 *width
-= (xmin
- *x
);
1676 /* right clipping */
1677 if (*x
+ *width
> xmax
)
1678 *width
-= (*x
+ *width
- xmax
);
1683 /* bottom (or top) clipping */
1685 *height
-= (ymin
- *y
);
1689 /* top (or bottom) clipping */
1690 if (*y
+ *height
> ymax
)
1691 *height
-= (*y
+ *height
- ymax
);
1701 * Clip dst coords against Xmax (or Ymax).
1704 clip_right_or_top(GLint
*srcX0
, GLint
*srcX1
,
1705 GLint
*dstX0
, GLint
*dstX1
,
1710 if (*dstX1
> maxValue
) {
1711 /* X1 outside right edge */
1712 ASSERT(*dstX0
< maxValue
); /* X0 should be inside right edge */
1713 t
= (GLfloat
) (maxValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
1714 /* chop off [t, 1] part */
1715 ASSERT(t
>= 0.0 && t
<= 1.0);
1717 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
1718 *srcX1
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
1720 else if (*dstX0
> maxValue
) {
1721 /* X0 outside right edge */
1722 ASSERT(*dstX1
< maxValue
); /* X1 should be inside right edge */
1723 t
= (GLfloat
) (maxValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
1724 /* chop off [t, 1] part */
1725 ASSERT(t
>= 0.0 && t
<= 1.0);
1727 bias
= (*srcX0
< *srcX1
) ? -0.5F
: 0.5F
;
1728 *srcX0
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
1734 * Clip dst coords against Xmin (or Ymin).
1737 clip_left_or_bottom(GLint
*srcX0
, GLint
*srcX1
,
1738 GLint
*dstX0
, GLint
*dstX1
,
1743 if (*dstX0
< minValue
) {
1744 /* X0 outside left edge */
1745 ASSERT(*dstX1
> minValue
); /* X1 should be inside left edge */
1746 t
= (GLfloat
) (minValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
1747 /* chop off [0, t] part */
1748 ASSERT(t
>= 0.0 && t
<= 1.0);
1750 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
; /* flipped??? */
1751 *srcX0
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
1753 else if (*dstX1
< minValue
) {
1754 /* X1 outside left edge */
1755 ASSERT(*dstX0
> minValue
); /* X0 should be inside left edge */
1756 t
= (GLfloat
) (minValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
1757 /* chop off [0, t] part */
1758 ASSERT(t
>= 0.0 && t
<= 1.0);
1760 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
1761 *srcX1
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
1767 * Do clipping of blit src/dest rectangles.
1768 * The dest rect is clipped against both the buffer bounds and scissor bounds.
1769 * The src rect is just clipped against the buffer bounds.
1771 * When either the src or dest rect is clipped, the other is also clipped
1774 * Note that X0 need not be less than X1 (same for Y) for either the source
1775 * and dest rects. That makes the clipping a little trickier.
1777 * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped
1780 _mesa_clip_blit(struct gl_context
*ctx
,
1781 GLint
*srcX0
, GLint
*srcY0
, GLint
*srcX1
, GLint
*srcY1
,
1782 GLint
*dstX0
, GLint
*dstY0
, GLint
*dstX1
, GLint
*dstY1
)
1784 const GLint srcXmin
= 0;
1785 const GLint srcXmax
= ctx
->ReadBuffer
->Width
;
1786 const GLint srcYmin
= 0;
1787 const GLint srcYmax
= ctx
->ReadBuffer
->Height
;
1789 /* these include scissor bounds */
1790 const GLint dstXmin
= ctx
->DrawBuffer
->_Xmin
;
1791 const GLint dstXmax
= ctx
->DrawBuffer
->_Xmax
;
1792 const GLint dstYmin
= ctx
->DrawBuffer
->_Ymin
;
1793 const GLint dstYmax
= ctx
->DrawBuffer
->_Ymax
;
1796 printf("PreClipX: src: %d .. %d dst: %d .. %d\n",
1797 *srcX0, *srcX1, *dstX0, *dstX1);
1798 printf("PreClipY: src: %d .. %d dst: %d .. %d\n",
1799 *srcY0, *srcY1, *dstY0, *dstY1);
1802 /* trivial rejection tests */
1803 if (*dstX0
== *dstX1
)
1804 return GL_FALSE
; /* no width */
1805 if (*dstX0
<= dstXmin
&& *dstX1
<= dstXmin
)
1806 return GL_FALSE
; /* totally out (left) of bounds */
1807 if (*dstX0
>= dstXmax
&& *dstX1
>= dstXmax
)
1808 return GL_FALSE
; /* totally out (right) of bounds */
1810 if (*dstY0
== *dstY1
)
1812 if (*dstY0
<= dstYmin
&& *dstY1
<= dstYmin
)
1814 if (*dstY0
>= dstYmax
&& *dstY1
>= dstYmax
)
1817 if (*srcX0
== *srcX1
)
1819 if (*srcX0
<= srcXmin
&& *srcX1
<= srcXmin
)
1821 if (*srcX0
>= srcXmax
&& *srcX1
>= srcXmax
)
1824 if (*srcY0
== *srcY1
)
1826 if (*srcY0
<= srcYmin
&& *srcY1
<= srcYmin
)
1828 if (*srcY0
>= srcYmax
&& *srcY1
>= srcYmax
)
1834 clip_right_or_top(srcX0
, srcX1
, dstX0
, dstX1
, dstXmax
);
1835 clip_right_or_top(srcY0
, srcY1
, dstY0
, dstY1
, dstYmax
);
1836 clip_left_or_bottom(srcX0
, srcX1
, dstX0
, dstX1
, dstXmin
);
1837 clip_left_or_bottom(srcY0
, srcY1
, dstY0
, dstY1
, dstYmin
);
1840 * src clip (just swap src/dst values from above)
1842 clip_right_or_top(dstX0
, dstX1
, srcX0
, srcX1
, srcXmax
);
1843 clip_right_or_top(dstY0
, dstY1
, srcY0
, srcY1
, srcYmax
);
1844 clip_left_or_bottom(dstX0
, dstX1
, srcX0
, srcX1
, srcXmin
);
1845 clip_left_or_bottom(dstY0
, dstY1
, srcY0
, srcY1
, srcYmin
);
1848 printf("PostClipX: src: %d .. %d dst: %d .. %d\n",
1849 *srcX0, *srcX1, *dstX0, *dstX1);
1850 printf("PostClipY: src: %d .. %d dst: %d .. %d\n",
1851 *srcY0, *srcY1, *dstY0, *dstY1);
1854 ASSERT(*dstX0
>= dstXmin
);
1855 ASSERT(*dstX0
<= dstXmax
);
1856 ASSERT(*dstX1
>= dstXmin
);
1857 ASSERT(*dstX1
<= dstXmax
);
1859 ASSERT(*dstY0
>= dstYmin
);
1860 ASSERT(*dstY0
<= dstYmax
);
1861 ASSERT(*dstY1
>= dstYmin
);
1862 ASSERT(*dstY1
<= dstYmax
);
1864 ASSERT(*srcX0
>= srcXmin
);
1865 ASSERT(*srcX0
<= srcXmax
);
1866 ASSERT(*srcX1
>= srcXmin
);
1867 ASSERT(*srcX1
<= srcXmax
);
1869 ASSERT(*srcY0
>= srcYmin
);
1870 ASSERT(*srcY0
<= srcYmax
);
1871 ASSERT(*srcY1
>= srcYmin
);
1872 ASSERT(*srcY1
<= srcYmax
);