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
;
1091 * Return the address of a specific pixel in an image (1D, 2D or 3D).
1093 * Pixel unpacking/packing parameters are observed according to \p packing.
1095 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
1096 * \param image starting address of image data
1097 * \param width the image width
1098 * \param height theimage height
1099 * \param format the pixel format
1100 * \param type the pixel data type
1101 * \param packing the pixelstore attributes
1102 * \param img which image in the volume (0 for 1D or 2D images)
1103 * \param row row of pixel in the image (0 for 1D images)
1104 * \param column column of pixel in the image
1106 * \return address of pixel on success, or NULL on error.
1108 * \sa gl_pixelstore_attrib.
1111 _mesa_image_address( GLuint dimensions
,
1112 const struct gl_pixelstore_attrib
*packing
,
1113 const GLvoid
*image
,
1114 GLsizei width
, GLsizei height
,
1115 GLenum format
, GLenum type
,
1116 GLint img
, GLint row
, GLint column
)
1118 GLint alignment
; /* 1, 2 or 4 */
1119 GLint pixels_per_row
;
1120 GLint rows_per_image
;
1123 GLint skipimages
; /* for 3-D volume images */
1124 GLubyte
*pixel_addr
;
1126 ASSERT(dimensions
>= 1 && dimensions
<= 3);
1128 alignment
= packing
->Alignment
;
1129 if (packing
->RowLength
> 0) {
1130 pixels_per_row
= packing
->RowLength
;
1133 pixels_per_row
= width
;
1135 if (packing
->ImageHeight
> 0) {
1136 rows_per_image
= packing
->ImageHeight
;
1139 rows_per_image
= height
;
1142 skippixels
= packing
->SkipPixels
;
1143 /* Note: SKIP_ROWS _is_ used for 1D images */
1144 skiprows
= packing
->SkipRows
;
1145 /* Note: SKIP_IMAGES is only used for 3D images */
1146 skipimages
= (dimensions
== 3) ? packing
->SkipImages
: 0;
1148 if (type
== GL_BITMAP
) {
1150 GLint comp_per_pixel
; /* components per pixel */
1151 GLint bytes_per_comp
; /* bytes per component */
1152 GLint bytes_per_row
;
1153 GLint bytes_per_image
;
1155 /* Compute bytes per component */
1156 bytes_per_comp
= _mesa_sizeof_packed_type( type
);
1157 if (bytes_per_comp
< 0) {
1161 /* Compute number of components per pixel */
1162 comp_per_pixel
= _mesa_components_in_format( format
);
1163 if (comp_per_pixel
< 0) {
1167 bytes_per_row
= alignment
1168 * CEILING( comp_per_pixel
*pixels_per_row
, 8*alignment
);
1170 bytes_per_image
= bytes_per_row
* rows_per_image
;
1172 pixel_addr
= (GLubyte
*) image
1173 + (skipimages
+ img
) * bytes_per_image
1174 + (skiprows
+ row
) * bytes_per_row
1175 + (skippixels
+ column
) / 8;
1178 /* Non-BITMAP data */
1179 GLint bytes_per_pixel
, bytes_per_row
, remainder
, bytes_per_image
;
1182 bytes_per_pixel
= _mesa_bytes_per_pixel( format
, type
);
1184 /* The pixel type and format should have been error checked earlier */
1185 assert(bytes_per_pixel
> 0);
1187 bytes_per_row
= pixels_per_row
* bytes_per_pixel
;
1188 remainder
= bytes_per_row
% alignment
;
1190 bytes_per_row
+= (alignment
- remainder
);
1192 ASSERT(bytes_per_row
% alignment
== 0);
1194 bytes_per_image
= bytes_per_row
* rows_per_image
;
1196 if (packing
->Invert
) {
1197 /* set pixel_addr to the last row */
1198 topOfImage
= bytes_per_row
* (height
- 1);
1199 bytes_per_row
= -bytes_per_row
;
1205 /* compute final pixel address */
1206 pixel_addr
= (GLubyte
*) image
1207 + (skipimages
+ img
) * bytes_per_image
1209 + (skiprows
+ row
) * bytes_per_row
1210 + (skippixels
+ column
) * bytes_per_pixel
;
1213 return (GLvoid
*) pixel_addr
;
1218 _mesa_image_address1d( const struct gl_pixelstore_attrib
*packing
,
1219 const GLvoid
*image
,
1221 GLenum format
, GLenum type
,
1224 return _mesa_image_address(1, packing
, image
, width
, 1,
1225 format
, type
, 0, 0, column
);
1230 _mesa_image_address2d( const struct gl_pixelstore_attrib
*packing
,
1231 const GLvoid
*image
,
1232 GLsizei width
, GLsizei height
,
1233 GLenum format
, GLenum type
,
1234 GLint row
, GLint column
)
1236 return _mesa_image_address(2, packing
, image
, width
, height
,
1237 format
, type
, 0, row
, column
);
1242 _mesa_image_address3d( const struct gl_pixelstore_attrib
*packing
,
1243 const GLvoid
*image
,
1244 GLsizei width
, GLsizei height
,
1245 GLenum format
, GLenum type
,
1246 GLint img
, GLint row
, GLint column
)
1248 return _mesa_image_address(3, packing
, image
, width
, height
,
1249 format
, type
, img
, row
, column
);
1255 * Compute the stride (in bytes) between image rows.
1257 * \param packing the pixelstore attributes
1258 * \param width image width.
1259 * \param format pixel format.
1260 * \param type pixel data type.
1262 * \return the stride in bytes for the given parameters, or -1 if error
1265 _mesa_image_row_stride( const struct gl_pixelstore_attrib
*packing
,
1266 GLint width
, GLenum format
, GLenum type
)
1268 GLint bytesPerRow
, remainder
;
1272 if (type
== GL_BITMAP
) {
1273 if (packing
->RowLength
== 0) {
1274 bytesPerRow
= (width
+ 7) / 8;
1277 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
1281 /* Non-BITMAP data */
1282 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1283 if (bytesPerPixel
<= 0)
1284 return -1; /* error */
1285 if (packing
->RowLength
== 0) {
1286 bytesPerRow
= bytesPerPixel
* width
;
1289 bytesPerRow
= bytesPerPixel
* packing
->RowLength
;
1293 remainder
= bytesPerRow
% packing
->Alignment
;
1294 if (remainder
> 0) {
1295 bytesPerRow
+= (packing
->Alignment
- remainder
);
1298 if (packing
->Invert
) {
1299 /* negate the bytes per row (negative row stride) */
1300 bytesPerRow
= -bytesPerRow
;
1308 * Compute the stride between images in a 3D texture (in bytes) for the given
1309 * pixel packing parameters and image width, format and type.
1312 _mesa_image_image_stride( const struct gl_pixelstore_attrib
*packing
,
1313 GLint width
, GLint height
,
1314 GLenum format
, GLenum type
)
1316 GLint bytesPerRow
, bytesPerImage
, remainder
;
1320 if (type
== GL_BITMAP
) {
1321 if (packing
->RowLength
== 0) {
1322 bytesPerRow
= (width
+ 7) / 8;
1325 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
1329 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1331 if (bytesPerPixel
<= 0)
1332 return -1; /* error */
1333 if (packing
->RowLength
== 0) {
1334 bytesPerRow
= bytesPerPixel
* width
;
1337 bytesPerRow
= bytesPerPixel
* packing
->RowLength
;
1341 remainder
= bytesPerRow
% packing
->Alignment
;
1343 bytesPerRow
+= (packing
->Alignment
- remainder
);
1345 if (packing
->ImageHeight
== 0)
1346 bytesPerImage
= bytesPerRow
* height
;
1348 bytesPerImage
= bytesPerRow
* packing
->ImageHeight
;
1350 return bytesPerImage
;
1356 * "Expand" a bitmap from 1-bit per pixel to 8-bits per pixel.
1357 * This is typically used to convert a bitmap into a GLubyte/pixel texture.
1358 * "On" bits will set texels to \p onValue.
1359 * "Off" bits will not modify texels.
1360 * \param width src bitmap width in pixels
1361 * \param height src bitmap height in pixels
1362 * \param unpack bitmap unpacking state
1363 * \param bitmap the src bitmap data
1364 * \param destBuffer start of dest buffer
1365 * \param destStride row stride in dest buffer
1366 * \param onValue if bit is 1, set destBuffer pixel to this value
1369 _mesa_expand_bitmap(GLsizei width
, GLsizei height
,
1370 const struct gl_pixelstore_attrib
*unpack
,
1371 const GLubyte
*bitmap
,
1372 GLubyte
*destBuffer
, GLint destStride
,
1375 const GLubyte
*srcRow
= (const GLubyte
*)
1376 _mesa_image_address2d(unpack
, bitmap
, width
, height
,
1377 GL_COLOR_INDEX
, GL_BITMAP
, 0, 0);
1378 const GLint srcStride
= _mesa_image_row_stride(unpack
, width
,
1379 GL_COLOR_INDEX
, GL_BITMAP
);
1382 #define SET_PIXEL(COL, ROW) \
1383 destBuffer[(ROW) * destStride + (COL)] = onValue;
1385 for (row
= 0; row
< height
; row
++) {
1386 const GLubyte
*src
= srcRow
;
1388 if (unpack
->LsbFirst
) {
1390 GLubyte mask
= 1U << (unpack
->SkipPixels
& 0x7);
1391 for (col
= 0; col
< width
; col
++) {
1394 SET_PIXEL(col
, row
);
1406 /* get ready for next row */
1412 GLubyte mask
= 128U >> (unpack
->SkipPixels
& 0x7);
1413 for (col
= 0; col
< width
; col
++) {
1416 SET_PIXEL(col
, row
);
1428 /* get ready for next row */
1433 srcRow
+= srcStride
;
1443 * Convert an array of RGBA colors from one datatype to another.
1444 * NOTE: src may equal dst. In that case, we use a temporary buffer.
1447 _mesa_convert_colors(GLenum srcType
, const GLvoid
*src
,
1448 GLenum dstType
, GLvoid
*dst
,
1449 GLuint count
, const GLubyte mask
[])
1451 GLuint tempBuffer
[MAX_WIDTH
][4];
1452 const GLboolean useTemp
= (src
== dst
);
1454 ASSERT(srcType
!= dstType
);
1457 case GL_UNSIGNED_BYTE
:
1458 if (dstType
== GL_UNSIGNED_SHORT
) {
1459 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
1460 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
1462 for (i
= 0; i
< count
; i
++) {
1463 if (!mask
|| mask
[i
]) {
1464 dst2
[i
][RCOMP
] = UBYTE_TO_USHORT(src1
[i
][RCOMP
]);
1465 dst2
[i
][GCOMP
] = UBYTE_TO_USHORT(src1
[i
][GCOMP
]);
1466 dst2
[i
][BCOMP
] = UBYTE_TO_USHORT(src1
[i
][BCOMP
]);
1467 dst2
[i
][ACOMP
] = UBYTE_TO_USHORT(src1
[i
][ACOMP
]);
1471 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
1474 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
1475 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
1477 ASSERT(dstType
== GL_FLOAT
);
1478 for (i
= 0; i
< count
; i
++) {
1479 if (!mask
|| mask
[i
]) {
1480 dst4
[i
][RCOMP
] = UBYTE_TO_FLOAT(src1
[i
][RCOMP
]);
1481 dst4
[i
][GCOMP
] = UBYTE_TO_FLOAT(src1
[i
][GCOMP
]);
1482 dst4
[i
][BCOMP
] = UBYTE_TO_FLOAT(src1
[i
][BCOMP
]);
1483 dst4
[i
][ACOMP
] = UBYTE_TO_FLOAT(src1
[i
][ACOMP
]);
1487 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
1490 case GL_UNSIGNED_SHORT
:
1491 if (dstType
== GL_UNSIGNED_BYTE
) {
1492 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
1493 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
1495 for (i
= 0; i
< count
; i
++) {
1496 if (!mask
|| mask
[i
]) {
1497 dst1
[i
][RCOMP
] = USHORT_TO_UBYTE(src2
[i
][RCOMP
]);
1498 dst1
[i
][GCOMP
] = USHORT_TO_UBYTE(src2
[i
][GCOMP
]);
1499 dst1
[i
][BCOMP
] = USHORT_TO_UBYTE(src2
[i
][BCOMP
]);
1500 dst1
[i
][ACOMP
] = USHORT_TO_UBYTE(src2
[i
][ACOMP
]);
1504 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
1507 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
1508 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
1510 ASSERT(dstType
== GL_FLOAT
);
1511 for (i
= 0; i
< count
; i
++) {
1512 if (!mask
|| mask
[i
]) {
1513 dst4
[i
][RCOMP
] = USHORT_TO_FLOAT(src2
[i
][RCOMP
]);
1514 dst4
[i
][GCOMP
] = USHORT_TO_FLOAT(src2
[i
][GCOMP
]);
1515 dst4
[i
][BCOMP
] = USHORT_TO_FLOAT(src2
[i
][BCOMP
]);
1516 dst4
[i
][ACOMP
] = USHORT_TO_FLOAT(src2
[i
][ACOMP
]);
1520 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
1524 if (dstType
== GL_UNSIGNED_BYTE
) {
1525 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
1526 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
1528 for (i
= 0; i
< count
; i
++) {
1529 if (!mask
|| mask
[i
]) {
1530 UNCLAMPED_FLOAT_TO_UBYTE(dst1
[i
][RCOMP
], src4
[i
][RCOMP
]);
1531 UNCLAMPED_FLOAT_TO_UBYTE(dst1
[i
][GCOMP
], src4
[i
][GCOMP
]);
1532 UNCLAMPED_FLOAT_TO_UBYTE(dst1
[i
][BCOMP
], src4
[i
][BCOMP
]);
1533 UNCLAMPED_FLOAT_TO_UBYTE(dst1
[i
][ACOMP
], src4
[i
][ACOMP
]);
1537 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
1540 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
1541 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
1543 ASSERT(dstType
== GL_UNSIGNED_SHORT
);
1544 for (i
= 0; i
< count
; i
++) {
1545 if (!mask
|| mask
[i
]) {
1546 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][RCOMP
], src4
[i
][RCOMP
]);
1547 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][GCOMP
], src4
[i
][GCOMP
]);
1548 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][BCOMP
], src4
[i
][BCOMP
]);
1549 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][ACOMP
], src4
[i
][ACOMP
]);
1553 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
1557 _mesa_problem(NULL
, "Invalid datatype in _mesa_convert_colors");
1565 * Perform basic clipping for glDrawPixels. The image's position and size
1566 * and the unpack SkipPixels and SkipRows are adjusted so that the image
1567 * region is entirely within the window and scissor bounds.
1568 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
1569 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
1570 * we'll actually write. Beforehand, *destY-1 is the first drawing row.
1572 * \return GL_TRUE if image is ready for drawing or
1573 * GL_FALSE if image was completely clipped away (draw nothing)
1576 _mesa_clip_drawpixels(const struct gl_context
*ctx
,
1577 GLint
*destX
, GLint
*destY
,
1578 GLsizei
*width
, GLsizei
*height
,
1579 struct gl_pixelstore_attrib
*unpack
)
1581 const struct gl_framebuffer
*buffer
= ctx
->DrawBuffer
;
1583 if (unpack
->RowLength
== 0) {
1584 unpack
->RowLength
= *width
;
1587 ASSERT(ctx
->Pixel
.ZoomX
== 1.0F
);
1588 ASSERT(ctx
->Pixel
.ZoomY
== 1.0F
|| ctx
->Pixel
.ZoomY
== -1.0F
);
1591 if (*destX
< buffer
->_Xmin
) {
1592 unpack
->SkipPixels
+= (buffer
->_Xmin
- *destX
);
1593 *width
-= (buffer
->_Xmin
- *destX
);
1594 *destX
= buffer
->_Xmin
;
1596 /* right clipping */
1597 if (*destX
+ *width
> buffer
->_Xmax
)
1598 *width
-= (*destX
+ *width
- buffer
->_Xmax
);
1603 if (ctx
->Pixel
.ZoomY
== 1.0F
) {
1604 /* bottom clipping */
1605 if (*destY
< buffer
->_Ymin
) {
1606 unpack
->SkipRows
+= (buffer
->_Ymin
- *destY
);
1607 *height
-= (buffer
->_Ymin
- *destY
);
1608 *destY
= buffer
->_Ymin
;
1611 if (*destY
+ *height
> buffer
->_Ymax
)
1612 *height
-= (*destY
+ *height
- buffer
->_Ymax
);
1614 else { /* upside down */
1616 if (*destY
> buffer
->_Ymax
) {
1617 unpack
->SkipRows
+= (*destY
- buffer
->_Ymax
);
1618 *height
-= (*destY
- buffer
->_Ymax
);
1619 *destY
= buffer
->_Ymax
;
1621 /* bottom clipping */
1622 if (*destY
- *height
< buffer
->_Ymin
)
1623 *height
-= (buffer
->_Ymin
- (*destY
- *height
));
1624 /* adjust destY so it's the first row to write to */
1636 * Perform clipping for glReadPixels. The image's window position
1637 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
1638 * so that the image region is entirely within the window bounds.
1639 * Note: this is different from _mesa_clip_drawpixels() in that the
1640 * scissor box is ignored, and we use the bounds of the current readbuffer
1643 * \return GL_TRUE if region to read is in bounds
1644 * GL_FALSE if region is completely out of bounds (nothing to read)
1647 _mesa_clip_readpixels(const struct gl_context
*ctx
,
1648 GLint
*srcX
, GLint
*srcY
,
1649 GLsizei
*width
, GLsizei
*height
,
1650 struct gl_pixelstore_attrib
*pack
)
1652 const struct gl_framebuffer
*buffer
= ctx
->ReadBuffer
;
1654 if (pack
->RowLength
== 0) {
1655 pack
->RowLength
= *width
;
1660 pack
->SkipPixels
+= (0 - *srcX
);
1661 *width
-= (0 - *srcX
);
1664 /* right clipping */
1665 if (*srcX
+ *width
> (GLsizei
) buffer
->Width
)
1666 *width
-= (*srcX
+ *width
- buffer
->Width
);
1671 /* bottom clipping */
1673 pack
->SkipRows
+= (0 - *srcY
);
1674 *height
-= (0 - *srcY
);
1678 if (*srcY
+ *height
> (GLsizei
) buffer
->Height
)
1679 *height
-= (*srcY
+ *height
- buffer
->Height
);
1689 * Do clipping for a glCopyTexSubImage call.
1690 * The framebuffer source region might extend outside the framebuffer
1691 * bounds. Clip the source region against the framebuffer bounds and
1692 * adjust the texture/dest position and size accordingly.
1694 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
1697 _mesa_clip_copytexsubimage(const struct gl_context
*ctx
,
1698 GLint
*destX
, GLint
*destY
,
1699 GLint
*srcX
, GLint
*srcY
,
1700 GLsizei
*width
, GLsizei
*height
)
1702 const struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1703 const GLint srcX0
= *srcX
, srcY0
= *srcY
;
1705 if (_mesa_clip_to_region(0, 0, fb
->Width
, fb
->Height
,
1706 srcX
, srcY
, width
, height
)) {
1707 *destX
= *destX
+ *srcX
- srcX0
;
1708 *destY
= *destY
+ *srcY
- srcY0
;
1720 * Clip the rectangle defined by (x, y, width, height) against the bounds
1721 * specified by [xmin, xmax) and [ymin, ymax).
1722 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
1725 _mesa_clip_to_region(GLint xmin
, GLint ymin
,
1726 GLint xmax
, GLint ymax
,
1728 GLsizei
*width
, GLsizei
*height
)
1732 *width
-= (xmin
- *x
);
1736 /* right clipping */
1737 if (*x
+ *width
> xmax
)
1738 *width
-= (*x
+ *width
- xmax
);
1743 /* bottom (or top) clipping */
1745 *height
-= (ymin
- *y
);
1749 /* top (or bottom) clipping */
1750 if (*y
+ *height
> ymax
)
1751 *height
-= (*y
+ *height
- ymax
);
1761 * Clip dst coords against Xmax (or Ymax).
1764 clip_right_or_top(GLint
*srcX0
, GLint
*srcX1
,
1765 GLint
*dstX0
, GLint
*dstX1
,
1770 if (*dstX1
> maxValue
) {
1771 /* X1 outside right edge */
1772 ASSERT(*dstX0
< maxValue
); /* X0 should be inside right edge */
1773 t
= (GLfloat
) (maxValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
1774 /* chop off [t, 1] part */
1775 ASSERT(t
>= 0.0 && t
<= 1.0);
1777 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
1778 *srcX1
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
1780 else if (*dstX0
> maxValue
) {
1781 /* X0 outside right edge */
1782 ASSERT(*dstX1
< maxValue
); /* X1 should be inside right edge */
1783 t
= (GLfloat
) (maxValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
1784 /* chop off [t, 1] part */
1785 ASSERT(t
>= 0.0 && t
<= 1.0);
1787 bias
= (*srcX0
< *srcX1
) ? -0.5F
: 0.5F
;
1788 *srcX0
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
1794 * Clip dst coords against Xmin (or Ymin).
1797 clip_left_or_bottom(GLint
*srcX0
, GLint
*srcX1
,
1798 GLint
*dstX0
, GLint
*dstX1
,
1803 if (*dstX0
< minValue
) {
1804 /* X0 outside left edge */
1805 ASSERT(*dstX1
> minValue
); /* X1 should be inside left edge */
1806 t
= (GLfloat
) (minValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
1807 /* chop off [0, t] part */
1808 ASSERT(t
>= 0.0 && t
<= 1.0);
1810 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
; /* flipped??? */
1811 *srcX0
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
1813 else if (*dstX1
< minValue
) {
1814 /* X1 outside left edge */
1815 ASSERT(*dstX0
> minValue
); /* X0 should be inside left edge */
1816 t
= (GLfloat
) (minValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
1817 /* chop off [0, t] part */
1818 ASSERT(t
>= 0.0 && t
<= 1.0);
1820 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
1821 *srcX1
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
1827 * Do clipping of blit src/dest rectangles.
1828 * The dest rect is clipped against both the buffer bounds and scissor bounds.
1829 * The src rect is just clipped against the buffer bounds.
1831 * When either the src or dest rect is clipped, the other is also clipped
1834 * Note that X0 need not be less than X1 (same for Y) for either the source
1835 * and dest rects. That makes the clipping a little trickier.
1837 * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped
1840 _mesa_clip_blit(struct gl_context
*ctx
,
1841 GLint
*srcX0
, GLint
*srcY0
, GLint
*srcX1
, GLint
*srcY1
,
1842 GLint
*dstX0
, GLint
*dstY0
, GLint
*dstX1
, GLint
*dstY1
)
1844 const GLint srcXmin
= 0;
1845 const GLint srcXmax
= ctx
->ReadBuffer
->Width
;
1846 const GLint srcYmin
= 0;
1847 const GLint srcYmax
= ctx
->ReadBuffer
->Height
;
1849 /* these include scissor bounds */
1850 const GLint dstXmin
= ctx
->DrawBuffer
->_Xmin
;
1851 const GLint dstXmax
= ctx
->DrawBuffer
->_Xmax
;
1852 const GLint dstYmin
= ctx
->DrawBuffer
->_Ymin
;
1853 const GLint dstYmax
= ctx
->DrawBuffer
->_Ymax
;
1856 printf("PreClipX: src: %d .. %d dst: %d .. %d\n",
1857 *srcX0, *srcX1, *dstX0, *dstX1);
1858 printf("PreClipY: src: %d .. %d dst: %d .. %d\n",
1859 *srcY0, *srcY1, *dstY0, *dstY1);
1862 /* trivial rejection tests */
1863 if (*dstX0
== *dstX1
)
1864 return GL_FALSE
; /* no width */
1865 if (*dstX0
<= dstXmin
&& *dstX1
<= dstXmin
)
1866 return GL_FALSE
; /* totally out (left) of bounds */
1867 if (*dstX0
>= dstXmax
&& *dstX1
>= dstXmax
)
1868 return GL_FALSE
; /* totally out (right) of bounds */
1870 if (*dstY0
== *dstY1
)
1872 if (*dstY0
<= dstYmin
&& *dstY1
<= dstYmin
)
1874 if (*dstY0
>= dstYmax
&& *dstY1
>= dstYmax
)
1877 if (*srcX0
== *srcX1
)
1879 if (*srcX0
<= srcXmin
&& *srcX1
<= srcXmin
)
1881 if (*srcX0
>= srcXmax
&& *srcX1
>= srcXmax
)
1884 if (*srcY0
== *srcY1
)
1886 if (*srcY0
<= srcYmin
&& *srcY1
<= srcYmin
)
1888 if (*srcY0
>= srcYmax
&& *srcY1
>= srcYmax
)
1894 clip_right_or_top(srcX0
, srcX1
, dstX0
, dstX1
, dstXmax
);
1895 clip_right_or_top(srcY0
, srcY1
, dstY0
, dstY1
, dstYmax
);
1896 clip_left_or_bottom(srcX0
, srcX1
, dstX0
, dstX1
, dstXmin
);
1897 clip_left_or_bottom(srcY0
, srcY1
, dstY0
, dstY1
, dstYmin
);
1900 * src clip (just swap src/dst values from above)
1902 clip_right_or_top(dstX0
, dstX1
, srcX0
, srcX1
, srcXmax
);
1903 clip_right_or_top(dstY0
, dstY1
, srcY0
, srcY1
, srcYmax
);
1904 clip_left_or_bottom(dstX0
, dstX1
, srcX0
, srcX1
, srcXmin
);
1905 clip_left_or_bottom(dstY0
, dstY1
, srcY0
, srcY1
, srcYmin
);
1908 printf("PostClipX: src: %d .. %d dst: %d .. %d\n",
1909 *srcX0, *srcX1, *dstX0, *dstX1);
1910 printf("PostClipY: src: %d .. %d dst: %d .. %d\n",
1911 *srcY0, *srcY1, *dstY0, *dstY1);
1914 ASSERT(*dstX0
>= dstXmin
);
1915 ASSERT(*dstX0
<= dstXmax
);
1916 ASSERT(*dstX1
>= dstXmin
);
1917 ASSERT(*dstX1
<= dstXmax
);
1919 ASSERT(*dstY0
>= dstYmin
);
1920 ASSERT(*dstY0
<= dstYmax
);
1921 ASSERT(*dstY1
>= dstYmin
);
1922 ASSERT(*dstY1
<= dstYmax
);
1924 ASSERT(*srcX0
>= srcXmin
);
1925 ASSERT(*srcX0
<= srcXmax
);
1926 ASSERT(*srcX1
>= srcXmin
);
1927 ASSERT(*srcX1
<= srcXmax
);
1929 ASSERT(*srcY0
>= srcYmin
);
1930 ASSERT(*srcY0
<= srcYmax
);
1931 ASSERT(*srcY1
>= srcYmin
);
1932 ASSERT(*srcY1
<= srcYmax
);