2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Texture image-related functions.
33 #include "bufferobj.h"
37 #include "framebuffer.h"
42 #include "mfeatures.h"
44 #include "texcompress.h"
45 #include "texcompress_cpal.h"
50 #include "glformats.h"
54 * State changes which we care about for glCopyTex[Sub]Image() calls.
55 * In particular, we care about pixel transfer state and buffer state
56 * (such as glReadBuffer to make sure we read from the right renderbuffer).
58 #define NEW_COPY_TEX_STATE (_NEW_BUFFERS | _NEW_PIXEL)
63 * Return the simple base format for a given internal texture format.
64 * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
66 * \param ctx GL context.
67 * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
69 * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
70 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
72 * This is the format which is used during texture application (i.e. the
73 * texture format and env mode determine the arithmetic used.
76 _mesa_base_tex_format( struct gl_context
*ctx
, GLint internalFormat
)
78 switch (internalFormat
) {
84 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_ALPHA
: -1;
91 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_LUMINANCE
: -1;
93 case GL_LUMINANCE_ALPHA
:
94 case GL_LUMINANCE4_ALPHA4
:
95 case GL_LUMINANCE6_ALPHA2
:
96 case GL_LUMINANCE8_ALPHA8
:
97 case GL_LUMINANCE12_ALPHA4
:
98 case GL_LUMINANCE12_ALPHA12
:
99 case GL_LUMINANCE16_ALPHA16
:
100 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_LUMINANCE_ALPHA
: -1;
106 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_INTENSITY
: -1;
108 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_RGB
: -1;
119 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_RGBA
: -1;
133 /* GL_BGRA can be an internal format *only* in OpenGL ES (1.x or 2.0).
135 if (_mesa_is_gles(ctx
)) {
136 switch (internalFormat
) {
144 if (ctx
->Extensions
.ARB_ES2_compatibility
) {
145 switch (internalFormat
) {
153 if (ctx
->Extensions
.ARB_depth_texture
) {
154 switch (internalFormat
) {
155 case GL_DEPTH_COMPONENT
:
156 case GL_DEPTH_COMPONENT16
:
157 case GL_DEPTH_COMPONENT24
:
158 case GL_DEPTH_COMPONENT32
:
159 return GL_DEPTH_COMPONENT
;
165 switch (internalFormat
) {
166 case GL_COMPRESSED_ALPHA
:
168 case GL_COMPRESSED_LUMINANCE
:
170 case GL_COMPRESSED_LUMINANCE_ALPHA
:
171 return GL_LUMINANCE_ALPHA
;
172 case GL_COMPRESSED_INTENSITY
:
174 case GL_COMPRESSED_RGB
:
176 case GL_COMPRESSED_RGBA
:
182 if (ctx
->Extensions
.TDFX_texture_compression_FXT1
) {
183 switch (internalFormat
) {
184 case GL_COMPRESSED_RGB_FXT1_3DFX
:
186 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
193 if (ctx
->Extensions
.EXT_texture_compression_s3tc
) {
194 switch (internalFormat
) {
195 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
197 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
198 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
199 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
206 /* GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE && GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE */
207 if (ctx
->API
== API_OPENGLES2
&&
208 ctx
->Extensions
.ANGLE_texture_compression_dxt
) {
209 switch (internalFormat
) {
210 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
211 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
218 if (ctx
->Extensions
.S3_s3tc
) {
219 switch (internalFormat
) {
231 if (ctx
->Extensions
.MESA_ycbcr_texture
) {
232 if (internalFormat
== GL_YCBCR_MESA
)
233 return GL_YCBCR_MESA
;
236 if (ctx
->Extensions
.ARB_texture_float
) {
237 switch (internalFormat
) {
238 case GL_ALPHA16F_ARB
:
239 case GL_ALPHA32F_ARB
:
247 case GL_INTENSITY16F_ARB
:
248 case GL_INTENSITY32F_ARB
:
250 case GL_LUMINANCE16F_ARB
:
251 case GL_LUMINANCE32F_ARB
:
253 case GL_LUMINANCE_ALPHA16F_ARB
:
254 case GL_LUMINANCE_ALPHA32F_ARB
:
255 return GL_LUMINANCE_ALPHA
;
261 if (ctx
->Extensions
.ATI_envmap_bumpmap
) {
262 switch (internalFormat
) {
271 if (ctx
->Extensions
.EXT_texture_snorm
) {
272 switch (internalFormat
) {
287 case GL_RGBA16_SNORM
:
290 case GL_ALPHA8_SNORM
:
291 case GL_ALPHA16_SNORM
:
293 case GL_LUMINANCE_SNORM
:
294 case GL_LUMINANCE8_SNORM
:
295 case GL_LUMINANCE16_SNORM
:
297 case GL_LUMINANCE_ALPHA_SNORM
:
298 case GL_LUMINANCE8_ALPHA8_SNORM
:
299 case GL_LUMINANCE16_ALPHA16_SNORM
:
300 return GL_LUMINANCE_ALPHA
;
301 case GL_INTENSITY_SNORM
:
302 case GL_INTENSITY8_SNORM
:
303 case GL_INTENSITY16_SNORM
:
310 if (ctx
->Extensions
.EXT_packed_depth_stencil
) {
311 switch (internalFormat
) {
312 case GL_DEPTH_STENCIL_EXT
:
313 case GL_DEPTH24_STENCIL8_EXT
:
314 return GL_DEPTH_STENCIL_EXT
;
320 if (ctx
->Extensions
.EXT_texture_sRGB
) {
321 switch (internalFormat
) {
324 case GL_COMPRESSED_SRGB_EXT
:
325 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
327 case GL_SRGB_ALPHA_EXT
:
328 case GL_SRGB8_ALPHA8_EXT
:
329 case GL_COMPRESSED_SRGB_ALPHA_EXT
:
330 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
331 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
332 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
334 case GL_SLUMINANCE_ALPHA_EXT
:
335 case GL_SLUMINANCE8_ALPHA8_EXT
:
336 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT
:
337 return GL_LUMINANCE_ALPHA
;
338 case GL_SLUMINANCE_EXT
:
339 case GL_SLUMINANCE8_EXT
:
340 case GL_COMPRESSED_SLUMINANCE_EXT
:
347 if (ctx
->Version
>= 30 ||
348 ctx
->Extensions
.EXT_texture_integer
) {
349 switch (internalFormat
) {
351 case GL_RGBA16UI_EXT
:
352 case GL_RGBA32UI_EXT
:
368 if (ctx
->Extensions
.EXT_texture_integer
) {
369 switch (internalFormat
) {
370 case GL_ALPHA8UI_EXT
:
371 case GL_ALPHA16UI_EXT
:
372 case GL_ALPHA32UI_EXT
:
374 case GL_ALPHA16I_EXT
:
375 case GL_ALPHA32I_EXT
:
377 case GL_INTENSITY8UI_EXT
:
378 case GL_INTENSITY16UI_EXT
:
379 case GL_INTENSITY32UI_EXT
:
380 case GL_INTENSITY8I_EXT
:
381 case GL_INTENSITY16I_EXT
:
382 case GL_INTENSITY32I_EXT
:
384 case GL_LUMINANCE8UI_EXT
:
385 case GL_LUMINANCE16UI_EXT
:
386 case GL_LUMINANCE32UI_EXT
:
387 case GL_LUMINANCE8I_EXT
:
388 case GL_LUMINANCE16I_EXT
:
389 case GL_LUMINANCE32I_EXT
:
391 case GL_LUMINANCE_ALPHA8UI_EXT
:
392 case GL_LUMINANCE_ALPHA16UI_EXT
:
393 case GL_LUMINANCE_ALPHA32UI_EXT
:
394 case GL_LUMINANCE_ALPHA8I_EXT
:
395 case GL_LUMINANCE_ALPHA16I_EXT
:
396 case GL_LUMINANCE_ALPHA32I_EXT
:
397 return GL_LUMINANCE_ALPHA
;
403 if (ctx
->Extensions
.ARB_texture_rg
) {
404 switch (internalFormat
) {
406 /* R16F depends on both ARB_half_float_pixel and ARB_texture_float.
408 if (!ctx
->Extensions
.ARB_half_float_pixel
)
412 if (!ctx
->Extensions
.ARB_texture_float
)
421 if (ctx
->Version
< 30 && !ctx
->Extensions
.EXT_texture_integer
)
427 case GL_COMPRESSED_RED
:
431 /* RG16F depends on both ARB_half_float_pixel and ARB_texture_float.
433 if (!ctx
->Extensions
.ARB_half_float_pixel
)
437 if (!ctx
->Extensions
.ARB_texture_float
)
446 if (ctx
->Version
< 30 && !ctx
->Extensions
.EXT_texture_integer
)
452 case GL_COMPRESSED_RG
:
459 if (ctx
->Extensions
.EXT_texture_shared_exponent
) {
460 switch (internalFormat
) {
468 if (ctx
->Extensions
.EXT_packed_float
) {
469 switch (internalFormat
) {
470 case GL_R11F_G11F_B10F_EXT
:
477 if (ctx
->Extensions
.ARB_depth_buffer_float
) {
478 switch (internalFormat
) {
479 case GL_DEPTH_COMPONENT32F
:
480 return GL_DEPTH_COMPONENT
;
481 case GL_DEPTH32F_STENCIL8
:
482 return GL_DEPTH_STENCIL
;
488 if (ctx
->Extensions
.ARB_texture_compression_rgtc
) {
489 switch (internalFormat
) {
490 case GL_COMPRESSED_RED_RGTC1
:
491 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
493 case GL_COMPRESSED_RG_RGTC2
:
494 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
501 if (ctx
->Extensions
.EXT_texture_compression_latc
) {
502 switch (internalFormat
) {
503 case GL_COMPRESSED_LUMINANCE_LATC1_EXT
:
504 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT
:
506 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT
:
507 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT
:
508 return GL_LUMINANCE_ALPHA
;
514 if (ctx
->Extensions
.ATI_texture_compression_3dc
) {
515 switch (internalFormat
) {
516 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI
:
517 return GL_LUMINANCE_ALPHA
;
523 if (ctx
->Extensions
.OES_compressed_ETC1_RGB8_texture
) {
524 switch (internalFormat
) {
525 case GL_ETC1_RGB8_OES
:
532 if (ctx
->API
== API_OPENGLES
) {
533 switch (internalFormat
) {
534 case GL_PALETTE4_RGB8_OES
:
535 case GL_PALETTE4_R5_G6_B5_OES
:
536 case GL_PALETTE8_RGB8_OES
:
537 case GL_PALETTE8_R5_G6_B5_OES
:
539 case GL_PALETTE4_RGBA8_OES
:
540 case GL_PALETTE8_RGB5_A1_OES
:
541 case GL_PALETTE4_RGBA4_OES
:
542 case GL_PALETTE4_RGB5_A1_OES
:
543 case GL_PALETTE8_RGBA8_OES
:
544 case GL_PALETTE8_RGBA4_OES
:
551 return -1; /* error */
556 * For cube map faces, return a face index in [0,5].
557 * For other targets return 0;
560 _mesa_tex_target_to_face(GLenum target
)
562 if (_mesa_is_cube_face(target
))
563 return (GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
;
571 * Install gl_texture_image in a gl_texture_object according to the target
572 * and level parameters.
574 * \param tObj texture object.
575 * \param target texture target.
576 * \param level image level.
577 * \param texImage texture image.
580 set_tex_image(struct gl_texture_object
*tObj
,
581 GLenum target
, GLint level
,
582 struct gl_texture_image
*texImage
)
584 const GLuint face
= _mesa_tex_target_to_face(target
);
588 if (target
== GL_TEXTURE_RECTANGLE_NV
|| target
== GL_TEXTURE_EXTERNAL_OES
)
591 tObj
->Image
[face
][level
] = texImage
;
593 /* Set the 'back' pointer */
594 texImage
->TexObject
= tObj
;
595 texImage
->Level
= level
;
596 texImage
->Face
= face
;
601 * Allocate a texture image structure.
603 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
606 * \return a pointer to gl_texture_image struct with all fields initialized to
609 struct gl_texture_image
*
610 _mesa_new_texture_image( struct gl_context
*ctx
)
613 return CALLOC_STRUCT(gl_texture_image
);
618 * Free a gl_texture_image and associated data.
619 * This function is a fallback called via ctx->Driver.DeleteTextureImage().
621 * \param texImage texture image.
623 * Free the texture image structure and the associated image data.
626 _mesa_delete_texture_image(struct gl_context
*ctx
,
627 struct gl_texture_image
*texImage
)
629 /* Free texImage->Data and/or any other driver-specific texture
632 ASSERT(ctx
->Driver
.FreeTextureImageBuffer
);
633 ctx
->Driver
.FreeTextureImageBuffer( ctx
, texImage
);
639 * Test if a target is a proxy target.
641 * \param target texture target.
643 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
646 _mesa_is_proxy_texture(GLenum target
)
649 * NUM_TEXTURE_TARGETS should match number of terms below, except there's no
650 * proxy for GL_TEXTURE_BUFFER and GL_TEXTURE_EXTERNAL_OES.
652 assert(NUM_TEXTURE_TARGETS
== 8 + 2);
654 return (target
== GL_PROXY_TEXTURE_1D
||
655 target
== GL_PROXY_TEXTURE_2D
||
656 target
== GL_PROXY_TEXTURE_3D
||
657 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
658 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
||
659 target
== GL_PROXY_TEXTURE_1D_ARRAY_EXT
||
660 target
== GL_PROXY_TEXTURE_2D_ARRAY_EXT
||
661 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
);
666 * Return the proxy target which corresponds to the given texture target
669 _mesa_get_proxy_target(GLenum target
)
673 case GL_PROXY_TEXTURE_1D
:
674 return GL_PROXY_TEXTURE_1D
;
676 case GL_PROXY_TEXTURE_2D
:
677 return GL_PROXY_TEXTURE_2D
;
679 case GL_PROXY_TEXTURE_3D
:
680 return GL_PROXY_TEXTURE_3D
;
681 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
682 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
683 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
684 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
685 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
686 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
687 case GL_TEXTURE_CUBE_MAP_ARB
:
688 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
689 return GL_PROXY_TEXTURE_CUBE_MAP_ARB
;
690 case GL_TEXTURE_RECTANGLE_NV
:
691 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
692 return GL_PROXY_TEXTURE_RECTANGLE_NV
;
693 case GL_TEXTURE_1D_ARRAY_EXT
:
694 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
695 return GL_PROXY_TEXTURE_1D_ARRAY_EXT
;
696 case GL_TEXTURE_2D_ARRAY_EXT
:
697 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
698 return GL_PROXY_TEXTURE_2D_ARRAY_EXT
;
699 case GL_TEXTURE_CUBE_MAP_ARRAY
:
700 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
701 return GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
;
703 _mesa_problem(NULL
, "unexpected target in _mesa_get_proxy_target()");
710 * Get the texture object that corresponds to the target of the given
711 * texture unit. The target should have already been checked for validity.
713 * \param ctx GL context.
714 * \param texUnit texture unit.
715 * \param target texture target.
717 * \return pointer to the texture object on success, or NULL on failure.
719 struct gl_texture_object
*
720 _mesa_select_tex_object(struct gl_context
*ctx
,
721 const struct gl_texture_unit
*texUnit
,
724 const GLboolean arrayTex
= (ctx
->Extensions
.MESA_texture_array
||
725 ctx
->Extensions
.EXT_texture_array
);
729 return texUnit
->CurrentTex
[TEXTURE_1D_INDEX
];
730 case GL_PROXY_TEXTURE_1D
:
731 return ctx
->Texture
.ProxyTex
[TEXTURE_1D_INDEX
];
733 return texUnit
->CurrentTex
[TEXTURE_2D_INDEX
];
734 case GL_PROXY_TEXTURE_2D
:
735 return ctx
->Texture
.ProxyTex
[TEXTURE_2D_INDEX
];
737 return texUnit
->CurrentTex
[TEXTURE_3D_INDEX
];
738 case GL_PROXY_TEXTURE_3D
:
739 return ctx
->Texture
.ProxyTex
[TEXTURE_3D_INDEX
];
740 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
741 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
742 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
743 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
744 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
745 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
746 case GL_TEXTURE_CUBE_MAP_ARB
:
747 return ctx
->Extensions
.ARB_texture_cube_map
748 ? texUnit
->CurrentTex
[TEXTURE_CUBE_INDEX
] : NULL
;
749 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
750 return ctx
->Extensions
.ARB_texture_cube_map
751 ? ctx
->Texture
.ProxyTex
[TEXTURE_CUBE_INDEX
] : NULL
;
752 case GL_TEXTURE_CUBE_MAP_ARRAY
:
753 return ctx
->Extensions
.ARB_texture_cube_map_array
754 ? texUnit
->CurrentTex
[TEXTURE_CUBE_ARRAY_INDEX
] : NULL
;
755 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
756 return ctx
->Extensions
.ARB_texture_cube_map_array
757 ? ctx
->Texture
.ProxyTex
[TEXTURE_CUBE_ARRAY_INDEX
] : NULL
;
758 case GL_TEXTURE_RECTANGLE_NV
:
759 return ctx
->Extensions
.NV_texture_rectangle
760 ? texUnit
->CurrentTex
[TEXTURE_RECT_INDEX
] : NULL
;
761 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
762 return ctx
->Extensions
.NV_texture_rectangle
763 ? ctx
->Texture
.ProxyTex
[TEXTURE_RECT_INDEX
] : NULL
;
764 case GL_TEXTURE_1D_ARRAY_EXT
:
765 return arrayTex
? texUnit
->CurrentTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
766 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
767 return arrayTex
? ctx
->Texture
.ProxyTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
768 case GL_TEXTURE_2D_ARRAY_EXT
:
769 return arrayTex
? texUnit
->CurrentTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
770 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
771 return arrayTex
? ctx
->Texture
.ProxyTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
772 case GL_TEXTURE_BUFFER
:
773 return _mesa_is_desktop_gl(ctx
)
774 && ctx
->Extensions
.ARB_texture_buffer_object
775 ? texUnit
->CurrentTex
[TEXTURE_BUFFER_INDEX
] : NULL
;
776 case GL_TEXTURE_EXTERNAL_OES
:
777 return ctx
->Extensions
.OES_EGL_image_external
778 ? texUnit
->CurrentTex
[TEXTURE_EXTERNAL_INDEX
] : NULL
;
780 _mesa_problem(NULL
, "bad target in _mesa_select_tex_object()");
787 * Return pointer to texture object for given target on current texture unit.
789 struct gl_texture_object
*
790 _mesa_get_current_tex_object(struct gl_context
*ctx
, GLenum target
)
792 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
793 return _mesa_select_tex_object(ctx
, texUnit
, target
);
798 * Get a texture image pointer from a texture object, given a texture
799 * target and mipmap level. The target and level parameters should
800 * have already been error-checked.
802 * \param ctx GL context.
803 * \param texObj texture unit.
804 * \param target texture target.
805 * \param level image level.
807 * \return pointer to the texture image structure, or NULL on failure.
809 struct gl_texture_image
*
810 _mesa_select_tex_image(struct gl_context
*ctx
,
811 const struct gl_texture_object
*texObj
,
812 GLenum target
, GLint level
)
814 const GLuint face
= _mesa_tex_target_to_face(target
);
818 ASSERT(level
< MAX_TEXTURE_LEVELS
);
820 return texObj
->Image
[face
][level
];
825 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
826 * it and install it. Only return NULL if passed a bad parameter or run
829 struct gl_texture_image
*
830 _mesa_get_tex_image(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
831 GLenum target
, GLint level
)
833 struct gl_texture_image
*texImage
;
838 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
840 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
842 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture image allocation");
846 set_tex_image(texObj
, target
, level
, texImage
);
854 * Return pointer to the specified proxy texture image.
855 * Note that proxy textures are per-context, not per-texture unit.
856 * \return pointer to texture image or NULL if invalid target, invalid
857 * level, or out of memory.
859 static struct gl_texture_image
*
860 get_proxy_tex_image(struct gl_context
*ctx
, GLenum target
, GLint level
)
862 struct gl_texture_image
*texImage
;
869 case GL_PROXY_TEXTURE_1D
:
870 if (level
>= ctx
->Const
.MaxTextureLevels
)
872 texIndex
= TEXTURE_1D_INDEX
;
874 case GL_PROXY_TEXTURE_2D
:
875 if (level
>= ctx
->Const
.MaxTextureLevels
)
877 texIndex
= TEXTURE_2D_INDEX
;
879 case GL_PROXY_TEXTURE_3D
:
880 if (level
>= ctx
->Const
.Max3DTextureLevels
)
882 texIndex
= TEXTURE_3D_INDEX
;
884 case GL_PROXY_TEXTURE_CUBE_MAP
:
885 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
887 texIndex
= TEXTURE_CUBE_INDEX
;
889 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
892 texIndex
= TEXTURE_RECT_INDEX
;
894 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
895 if (level
>= ctx
->Const
.MaxTextureLevels
)
897 texIndex
= TEXTURE_1D_ARRAY_INDEX
;
899 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
900 if (level
>= ctx
->Const
.MaxTextureLevels
)
902 texIndex
= TEXTURE_2D_ARRAY_INDEX
;
904 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
905 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
907 texIndex
= TEXTURE_CUBE_ARRAY_INDEX
;
913 texImage
= ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
];
915 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
917 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
920 ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
] = texImage
;
921 /* Set the 'back' pointer */
922 texImage
->TexObject
= ctx
->Texture
.ProxyTex
[texIndex
];
929 * Get the maximum number of allowed mipmap levels.
931 * \param ctx GL context.
932 * \param target texture target.
934 * \return the maximum number of allowed mipmap levels for the given
935 * texture target, or zero if passed a bad target.
940 _mesa_max_texture_levels(struct gl_context
*ctx
, GLenum target
)
944 case GL_PROXY_TEXTURE_1D
:
946 case GL_PROXY_TEXTURE_2D
:
947 return ctx
->Const
.MaxTextureLevels
;
949 case GL_PROXY_TEXTURE_3D
:
950 return ctx
->Const
.Max3DTextureLevels
;
951 case GL_TEXTURE_CUBE_MAP
:
952 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
953 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
954 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
955 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
956 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
957 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
958 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
959 return ctx
->Extensions
.ARB_texture_cube_map
960 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
961 case GL_TEXTURE_RECTANGLE_NV
:
962 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
963 return ctx
->Extensions
.NV_texture_rectangle
? 1 : 0;
964 case GL_TEXTURE_1D_ARRAY_EXT
:
965 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
966 case GL_TEXTURE_2D_ARRAY_EXT
:
967 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
968 return (ctx
->Extensions
.MESA_texture_array
||
969 ctx
->Extensions
.EXT_texture_array
)
970 ? ctx
->Const
.MaxTextureLevels
: 0;
971 case GL_TEXTURE_CUBE_MAP_ARRAY
:
972 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
973 return ctx
->Extensions
.ARB_texture_cube_map_array
974 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
975 case GL_TEXTURE_BUFFER
:
976 return _mesa_is_desktop_gl(ctx
)
977 && ctx
->Extensions
.ARB_texture_buffer_object
979 case GL_TEXTURE_EXTERNAL_OES
:
982 return 0; /* bad target */
988 * Return number of dimensions per mipmap level for the given texture target.
991 _mesa_get_texture_dimensions(GLenum target
)
995 case GL_PROXY_TEXTURE_1D
:
998 case GL_TEXTURE_RECTANGLE
:
999 case GL_TEXTURE_CUBE_MAP
:
1000 case GL_PROXY_TEXTURE_2D
:
1001 case GL_PROXY_TEXTURE_RECTANGLE
:
1002 case GL_PROXY_TEXTURE_CUBE_MAP
:
1003 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1004 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1005 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1006 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1007 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1008 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1009 case GL_TEXTURE_1D_ARRAY
:
1010 case GL_PROXY_TEXTURE_1D_ARRAY
:
1011 case GL_TEXTURE_EXTERNAL_OES
:
1014 case GL_PROXY_TEXTURE_3D
:
1015 case GL_TEXTURE_2D_ARRAY
:
1016 case GL_PROXY_TEXTURE_2D_ARRAY
:
1017 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1018 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1020 case GL_TEXTURE_BUFFER
:
1023 _mesa_problem(NULL
, "invalid target 0x%x in get_texture_dimensions()",
1031 * Return the maximum number of mipmap levels for the given target
1032 * and the dimensions.
1033 * The dimensions are expected not to include the border.
1036 _mesa_get_tex_max_num_levels(GLenum target
, GLsizei width
, GLsizei height
,
1043 case GL_TEXTURE_1D_ARRAY
:
1046 case GL_TEXTURE_CUBE_MAP
:
1047 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1048 ASSERT(width
== height
);
1052 case GL_TEXTURE_2D_ARRAY
:
1053 size
= MAX2(width
, height
);
1056 size
= MAX3(width
, height
, depth
);
1058 case GL_TEXTURE_RECTANGLE
:
1059 case GL_TEXTURE_EXTERNAL_OES
:
1066 return _mesa_logbase2(size
) + 1;
1070 #if 000 /* not used anymore */
1072 * glTexImage[123]D can accept a NULL image pointer. In this case we
1073 * create a texture image with unspecified image contents per the OpenGL
1077 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
1079 const GLint components
= _mesa_components_in_format(format
);
1080 const GLint numPixels
= width
* height
* depth
;
1081 GLubyte
*data
= (GLubyte
*) malloc(numPixels
* components
* sizeof(GLubyte
));
1085 * Let's see if anyone finds this. If glTexImage2D() is called with
1086 * a NULL image pointer then load the texture image with something
1087 * interesting instead of leaving it indeterminate.
1090 static const char message
[8][32] = {
1091 " X X XXXXX XXX X ",
1092 " XX XX X X X X X ",
1094 " X X XXXX XXX XXXXX ",
1097 " X X XXXXX XXX X X ",
1101 GLubyte
*imgPtr
= data
;
1103 for (h
= 0; h
< depth
; h
++) {
1104 for (i
= 0; i
< height
; i
++) {
1105 GLint srcRow
= 7 - (i
% 8);
1106 for (j
= 0; j
< width
; j
++) {
1107 GLint srcCol
= j
% 32;
1108 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
1109 for (k
= 0; k
< components
; k
++) {
1125 * Set the size and format-related fields of a gl_texture_image struct
1126 * to zero. This is used when a proxy texture test fails.
1129 clear_teximage_fields(struct gl_texture_image
*img
)
1132 img
->_BaseFormat
= 0;
1133 img
->InternalFormat
= 0;
1142 img
->HeightLog2
= 0;
1144 img
->TexFormat
= MESA_FORMAT_NONE
;
1149 * Initialize basic fields of the gl_texture_image struct.
1151 * \param ctx GL context.
1152 * \param img texture image structure to be initialized.
1153 * \param width image width.
1154 * \param height image height.
1155 * \param depth image depth.
1156 * \param border image border.
1157 * \param internalFormat internal format.
1158 * \param format the actual hardware format (one of MESA_FORMAT_*)
1160 * Fills in the fields of \p img with the given information.
1161 * Note: width, height and depth include the border.
1164 _mesa_init_teximage_fields(struct gl_context
*ctx
,
1165 struct gl_texture_image
*img
,
1166 GLsizei width
, GLsizei height
, GLsizei depth
,
1167 GLint border
, GLenum internalFormat
,
1173 ASSERT(height
>= 0);
1176 target
= img
->TexObject
->Target
;
1177 img
->_BaseFormat
= _mesa_base_tex_format( ctx
, internalFormat
);
1178 ASSERT(img
->_BaseFormat
> 0);
1179 img
->InternalFormat
= internalFormat
;
1180 img
->Border
= border
;
1182 img
->Height
= height
;
1185 img
->Width2
= width
- 2 * border
; /* == 1 << img->WidthLog2; */
1186 img
->WidthLog2
= _mesa_logbase2(img
->Width2
);
1190 case GL_TEXTURE_BUFFER
:
1191 case GL_PROXY_TEXTURE_1D
:
1196 img
->HeightLog2
= 0;
1203 case GL_TEXTURE_1D_ARRAY
:
1204 case GL_PROXY_TEXTURE_1D_ARRAY
:
1205 img
->Height2
= height
; /* no border */
1206 img
->HeightLog2
= 0; /* not used */
1214 case GL_TEXTURE_RECTANGLE
:
1215 case GL_TEXTURE_CUBE_MAP
:
1216 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1217 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1218 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1219 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1220 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1221 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1222 case GL_TEXTURE_EXTERNAL_OES
:
1223 case GL_PROXY_TEXTURE_2D
:
1224 case GL_PROXY_TEXTURE_RECTANGLE
:
1225 case GL_PROXY_TEXTURE_CUBE_MAP
:
1226 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1227 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1234 case GL_TEXTURE_2D_ARRAY
:
1235 case GL_PROXY_TEXTURE_2D_ARRAY
:
1236 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1237 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1238 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1239 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1240 img
->Depth2
= depth
; /* no border */
1241 img
->DepthLog2
= 0; /* not used */
1244 case GL_PROXY_TEXTURE_3D
:
1245 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1246 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1247 img
->Depth2
= depth
- 2 * border
; /* == 1 << img->DepthLog2; */
1248 img
->DepthLog2
= _mesa_logbase2(img
->Depth2
);
1251 _mesa_problem(NULL
, "invalid target 0x%x in _mesa_init_teximage_fields()",
1256 _mesa_get_tex_max_num_levels(target
,
1257 img
->Width2
, img
->Height2
, img
->Depth2
);
1258 img
->TexFormat
= format
;
1263 * Free and clear fields of the gl_texture_image struct.
1265 * \param ctx GL context.
1266 * \param texImage texture image structure to be cleared.
1268 * After the call, \p texImage will have no data associated with it. Its
1269 * fields are cleared so that its parent object will test incomplete.
1272 _mesa_clear_texture_image(struct gl_context
*ctx
,
1273 struct gl_texture_image
*texImage
)
1275 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
1276 clear_teximage_fields(texImage
);
1281 * Check the width, height, depth and border of a texture image are legal.
1282 * Used by all the glTexImage, glCompressedTexImage and glCopyTexImage
1284 * The target and level parameters will have already been validated.
1285 * \return GL_TRUE if size is OK, GL_FALSE otherwise.
1288 _mesa_legal_texture_dimensions(struct gl_context
*ctx
, GLenum target
,
1289 GLint level
, GLint width
, GLint height
,
1290 GLint depth
, GLint border
)
1296 case GL_PROXY_TEXTURE_1D
:
1297 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1); /* level zero size */
1298 maxSize
>>= level
; /* level size */
1299 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1301 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1302 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1308 case GL_PROXY_TEXTURE_2D
:
1309 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1311 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1313 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1315 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1316 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1318 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1324 case GL_PROXY_TEXTURE_3D
:
1325 maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1327 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1329 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1331 if (depth
< 2 * border
|| depth
> 2 * border
+ maxSize
)
1333 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1334 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1336 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1338 if (depth
> 0 && !_mesa_is_pow_two(depth
- 2 * border
))
1343 case GL_TEXTURE_RECTANGLE_NV
:
1344 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1347 maxSize
= ctx
->Const
.MaxTextureRectSize
;
1348 if (width
< 0 || width
> maxSize
)
1350 if (height
< 0 || height
> maxSize
)
1354 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1355 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1356 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1357 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1358 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1359 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1360 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
1361 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1363 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1365 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1367 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1368 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1370 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1375 case GL_TEXTURE_1D_ARRAY_EXT
:
1376 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1377 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1379 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1381 if (height
< 1 || height
> ctx
->Const
.MaxArrayTextureLayers
)
1383 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1384 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1389 case GL_TEXTURE_2D_ARRAY_EXT
:
1390 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1391 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1393 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1395 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1397 if (depth
< 1 || depth
> ctx
->Const
.MaxArrayTextureLayers
)
1399 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1400 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1402 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1407 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1408 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1409 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1410 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1412 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1414 if (depth
< 1 || depth
> ctx
->Const
.MaxArrayTextureLayers
)
1416 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
1418 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1419 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1421 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1426 _mesa_problem(ctx
, "Invalid target in _mesa_legal_texture_dimensions()");
1433 * Do error checking of xoffset, yoffset, zoffset, width, height and depth
1434 * for glTexSubImage, glCopyTexSubImage and glCompressedTexSubImage.
1435 * \param destImage the destination texture image.
1436 * \return GL_TRUE if error found, GL_FALSE otherwise.
1439 error_check_subtexture_dimensions(struct gl_context
*ctx
,
1440 const char *function
, GLuint dims
,
1441 const struct gl_texture_image
*destImage
,
1442 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1443 GLsizei subWidth
, GLsizei subHeight
,
1446 const GLenum target
= destImage
->TexObject
->Target
;
1451 _mesa_error(ctx
, GL_INVALID_VALUE
,
1452 "%s%dD(width=%d)", function
, dims
, subWidth
);
1456 if (dims
> 1 && subHeight
< 0) {
1457 _mesa_error(ctx
, GL_INVALID_VALUE
,
1458 "%s%dD(height=%d)", function
, dims
, subHeight
);
1462 if (dims
> 2 && subDepth
< 0) {
1463 _mesa_error(ctx
, GL_INVALID_VALUE
,
1464 "%s%dD(depth=%d)", function
, dims
, subDepth
);
1468 /* check xoffset and width */
1469 if (xoffset
< -destImage
->Border
) {
1470 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(xoffset)",
1475 if (xoffset
+ subWidth
> destImage
->Width
) {
1476 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(xoffset+width)",
1481 /* check yoffset and height */
1483 GLint yBorder
= (target
== GL_TEXTURE_1D_ARRAY
) ? 0 : destImage
->Border
;
1484 if (yoffset
< -yBorder
) {
1485 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(yoffset)",
1489 if (yoffset
+ subHeight
> destImage
->Height
) {
1490 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(yoffset+height)",
1496 /* check zoffset and depth */
1498 GLint zBorder
= (target
== GL_TEXTURE_2D_ARRAY
) ? 0 : destImage
->Border
;
1499 if (zoffset
< -zBorder
) {
1500 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s3D(zoffset)", function
);
1503 if (zoffset
+ subDepth
> (GLint
) destImage
->Depth
) {
1504 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s3D(zoffset+depth)", function
);
1510 * The OpenGL spec (and GL_ARB_texture_compression) says only whole
1511 * compressed texture images can be updated. But, that restriction may be
1512 * relaxed for particular compressed formats. At this time, all the
1513 * compressed formats supported by Mesa allow sub-textures to be updated
1514 * along compressed block boundaries.
1516 _mesa_get_format_block_size(destImage
->TexFormat
, &bw
, &bh
);
1518 if (bw
!= 1 || bh
!= 1) {
1519 /* offset must be multiple of block size */
1520 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0)) {
1521 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1522 "%s%dD(xoffset = %d, yoffset = %d)",
1523 function
, dims
, xoffset
, yoffset
);
1527 /* size must be multiple of bw by bh or equal to whole texture size */
1528 if ((subWidth
% bw
!= 0) && subWidth
!= destImage
->Width
) {
1529 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1530 "%s%dD(width = %d)", function
, dims
, subWidth
);
1534 if ((subHeight
% bh
!= 0) && subHeight
!= destImage
->Height
) {
1535 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1536 "%s%dD(height = %d)", function
, dims
, subHeight
);
1548 * This is the fallback for Driver.TestProxyTexImage() for doing device-
1549 * specific texture image size checks.
1551 * A hardware driver might override this function if, for example, the
1552 * max 3D texture size is 512x512x64 (i.e. not a cube).
1554 * Note that width, height, depth == 0 is not an error. However, a
1555 * texture with zero width/height/depth will be considered "incomplete"
1556 * and texturing will effectively be disabled.
1558 * \param target any texture target/type
1559 * \param level as passed to glTexImage
1560 * \param format the MESA_FORMAT_x for the tex image
1561 * \param width as passed to glTexImage
1562 * \param height as passed to glTexImage
1563 * \param depth as passed to glTexImage
1564 * \param border as passed to glTexImage
1565 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1568 _mesa_test_proxy_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1570 GLint width
, GLint height
, GLint depth
, GLint border
)
1572 /* We just check if the image size is less than MaxTextureMbytes.
1573 * Some drivers may do more specific checks.
1575 uint64_t bytes
= _mesa_format_image_size64(format
, width
, height
, depth
);
1576 uint64_t mbytes
= bytes
/ (1024 * 1024); /* convert to MB */
1577 mbytes
*= _mesa_num_tex_faces(target
);
1578 return mbytes
<= (uint64_t) ctx
->Const
.MaxTextureMbytes
;
1583 * Return true if the format is only valid for glCompressedTexImage.
1586 compressedteximage_only_format(const struct gl_context
*ctx
, GLenum format
)
1589 case GL_ETC1_RGB8_OES
:
1590 case GL_PALETTE4_RGB8_OES
:
1591 case GL_PALETTE4_RGBA8_OES
:
1592 case GL_PALETTE4_R5_G6_B5_OES
:
1593 case GL_PALETTE4_RGBA4_OES
:
1594 case GL_PALETTE4_RGB5_A1_OES
:
1595 case GL_PALETTE8_RGB8_OES
:
1596 case GL_PALETTE8_RGBA8_OES
:
1597 case GL_PALETTE8_R5_G6_B5_OES
:
1598 case GL_PALETTE8_RGBA4_OES
:
1599 case GL_PALETTE8_RGB5_A1_OES
:
1608 * Helper function to determine whether a target and specific compression
1609 * format are supported.
1612 target_can_be_compressed(const struct gl_context
*ctx
, GLenum target
,
1615 (void) intFormat
; /* not used yet */
1619 case GL_PROXY_TEXTURE_2D
:
1620 return GL_TRUE
; /* true for any compressed format so far */
1621 case GL_PROXY_TEXTURE_CUBE_MAP
:
1622 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1623 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1624 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1625 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1626 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1627 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1628 return ctx
->Extensions
.ARB_texture_cube_map
;
1629 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1630 case GL_TEXTURE_2D_ARRAY_EXT
:
1631 return (ctx
->Extensions
.MESA_texture_array
||
1632 ctx
->Extensions
.EXT_texture_array
);
1633 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1634 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1635 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1643 * Check if the given texture target value is legal for a
1644 * glTexImage1/2/3D call.
1647 legal_teximage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
1653 case GL_PROXY_TEXTURE_1D
:
1654 return _mesa_is_desktop_gl(ctx
);
1662 case GL_PROXY_TEXTURE_2D
:
1663 return _mesa_is_desktop_gl(ctx
);
1664 case GL_PROXY_TEXTURE_CUBE_MAP
:
1665 return _mesa_is_desktop_gl(ctx
)
1666 && ctx
->Extensions
.ARB_texture_cube_map
;
1667 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1668 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1669 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1670 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1671 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1672 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1673 return ctx
->Extensions
.ARB_texture_cube_map
;
1674 case GL_TEXTURE_RECTANGLE_NV
:
1675 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1676 return _mesa_is_desktop_gl(ctx
)
1677 && ctx
->Extensions
.NV_texture_rectangle
;
1678 case GL_TEXTURE_1D_ARRAY_EXT
:
1679 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1680 return _mesa_is_desktop_gl(ctx
)
1681 && (ctx
->Extensions
.MESA_texture_array
||
1682 ctx
->Extensions
.EXT_texture_array
);
1690 case GL_PROXY_TEXTURE_3D
:
1691 return _mesa_is_desktop_gl(ctx
);
1692 case GL_TEXTURE_2D_ARRAY_EXT
:
1693 return (_mesa_is_desktop_gl(ctx
)
1694 && (ctx
->Extensions
.MESA_texture_array
||
1695 ctx
->Extensions
.EXT_texture_array
))
1696 || _mesa_is_gles3(ctx
);
1697 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1698 return _mesa_is_desktop_gl(ctx
)
1699 && (ctx
->Extensions
.MESA_texture_array
||
1700 ctx
->Extensions
.EXT_texture_array
);
1701 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1702 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1703 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1708 _mesa_problem(ctx
, "invalid dims=%u in legal_teximage_target()", dims
);
1715 * Check if the given texture target value is legal for a
1716 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1717 * The difference compared to legal_teximage_target() above is that
1718 * proxy targets are not supported.
1721 legal_texsubimage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
1725 return _mesa_is_desktop_gl(ctx
) && target
== GL_TEXTURE_1D
;
1730 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1731 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1732 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1733 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1734 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1735 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1736 return ctx
->Extensions
.ARB_texture_cube_map
;
1737 case GL_TEXTURE_RECTANGLE_NV
:
1738 return _mesa_is_desktop_gl(ctx
)
1739 && ctx
->Extensions
.NV_texture_rectangle
;
1740 case GL_TEXTURE_1D_ARRAY_EXT
:
1741 return _mesa_is_desktop_gl(ctx
)
1742 && (ctx
->Extensions
.MESA_texture_array
||
1743 ctx
->Extensions
.EXT_texture_array
);
1751 case GL_TEXTURE_2D_ARRAY_EXT
:
1752 return (_mesa_is_desktop_gl(ctx
)
1753 && (ctx
->Extensions
.MESA_texture_array
||
1754 ctx
->Extensions
.EXT_texture_array
))
1755 || _mesa_is_gles3(ctx
);
1756 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1757 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1758 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1763 _mesa_problem(ctx
, "invalid dims=%u in legal_texsubimage_target()",
1771 * Helper function to determine if a texture object is mutable (in terms
1772 * of GL_ARB_texture_storage).
1775 mutable_tex_object(struct gl_context
*ctx
, GLenum target
)
1777 if (ctx
->Extensions
.ARB_texture_storage
) {
1778 struct gl_texture_object
*texObj
=
1779 _mesa_get_current_tex_object(ctx
, target
);
1780 return !texObj
->Immutable
;
1787 * Return expected size of a compressed texture.
1790 compressed_tex_size(GLsizei width
, GLsizei height
, GLsizei depth
,
1793 gl_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
1794 return _mesa_format_image_size(mesaFormat
, width
, height
, depth
);
1799 * Test the glTexImage[123]D() parameters for errors.
1801 * \param ctx GL context.
1802 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1803 * \param target texture target given by the user (already validated).
1804 * \param level image level given by the user.
1805 * \param internalFormat internal format given by the user.
1806 * \param format pixel data format given by the user.
1807 * \param type pixel data type given by the user.
1808 * \param width image width given by the user.
1809 * \param height image height given by the user.
1810 * \param depth image depth given by the user.
1811 * \param border image border given by the user.
1813 * \return GL_TRUE if a error is found, GL_FALSE otherwise
1815 * Verifies each of the parameters against the constants specified in
1816 * __struct gl_contextRec::Const and the supported extensions, and according
1817 * to the OpenGL specification.
1818 * Note that we don't fully error-check the width, height, depth values
1819 * here. That's done in _mesa_legal_texture_dimensions() which is used
1820 * by several other GL entrypoints. Plus, texture dims have a special
1821 * interaction with proxy textures.
1824 texture_error_check( struct gl_context
*ctx
,
1825 GLuint dimensions
, GLenum target
,
1826 GLint level
, GLint internalFormat
,
1827 GLenum format
, GLenum type
,
1828 GLint width
, GLint height
,
1829 GLint depth
, GLint border
)
1831 GLboolean colorFormat
;
1834 /* Even though there are no color-index textures, we still have to support
1835 * uploading color-index data and remapping it to RGB via the
1836 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
1838 const GLboolean indexFormat
= (format
== GL_COLOR_INDEX
);
1840 /* Note: for proxy textures, some error conditions immediately generate
1841 * a GL error in the usual way. But others do not generate a GL error.
1842 * Instead, they cause the width, height, depth, format fields of the
1843 * texture image to be zeroed-out. The GL spec seems to indicate that the
1844 * zero-out behaviour is only used in cases related to memory allocation.
1848 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
1849 _mesa_error(ctx
, GL_INVALID_VALUE
,
1850 "glTexImage%dD(level=%d)", dimensions
, level
);
1855 if (border
< 0 || border
> 1 ||
1856 ((ctx
->API
!= API_OPENGL
||
1857 target
== GL_TEXTURE_RECTANGLE_NV
||
1858 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1859 _mesa_error(ctx
, GL_INVALID_VALUE
,
1860 "glTexImage%dD(border=%d)", dimensions
, border
);
1864 if (width
< 0 || height
< 0 || depth
< 0) {
1865 _mesa_error(ctx
, GL_INVALID_VALUE
,
1866 "glTexImage%dD(width, height or depth < 0)", dimensions
);
1870 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1871 * combinations of format, internalFormat, and type that can be used.
1872 * Formats and types that require additional extensions (e.g., GL_FLOAT
1873 * requires GL_OES_texture_float) are filtered elsewhere.
1876 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
1877 if (format
!= internalFormat
) {
1878 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1879 "glTexImage%dD(format = %s, internalFormat = %s)",
1881 _mesa_lookup_enum_by_nr(format
),
1882 _mesa_lookup_enum_by_nr(internalFormat
));
1886 err
= _mesa_es_error_check_format_and_type(format
, type
, dimensions
);
1887 if (err
!= GL_NO_ERROR
) {
1888 _mesa_error(ctx
, err
,
1889 "glTexImage%dD(format = %s, type = %s)",
1891 _mesa_lookup_enum_by_nr(format
),
1892 _mesa_lookup_enum_by_nr(type
));
1897 if ((target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
1898 _mesa_is_cube_face(target
)) && width
!= height
) {
1899 _mesa_error(ctx
, GL_INVALID_VALUE
,
1900 "glTexImage2D(cube width != height)");
1904 if ((target
== GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
||
1905 target
== GL_TEXTURE_CUBE_MAP_ARRAY
) && width
!= height
) {
1906 _mesa_error(ctx
, GL_INVALID_VALUE
,
1907 "glTexImage3D(cube array width != height)");
1911 if ((target
== GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
||
1912 target
== GL_TEXTURE_CUBE_MAP_ARRAY
) && (depth
% 6)) {
1913 _mesa_error(ctx
, GL_INVALID_VALUE
,
1914 "glTexImage3D(cube array depth not multiple of 6)");
1918 /* Check internalFormat */
1919 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1920 _mesa_error(ctx
, GL_INVALID_VALUE
,
1921 "glTexImage%dD(internalFormat=%s)",
1922 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
));
1926 /* Check incoming image format and type */
1927 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1928 if (err
!= GL_NO_ERROR
) {
1929 _mesa_error(ctx
, err
,
1930 "glTexImage%dD(incompatible format 0x%x, type 0x%x)",
1931 dimensions
, format
, type
);
1935 /* make sure internal format and format basically agree */
1936 colorFormat
= _mesa_is_color_format(format
);
1937 if ((_mesa_is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
) ||
1938 (_mesa_is_depth_format(internalFormat
) != _mesa_is_depth_format(format
)) ||
1939 (_mesa_is_ycbcr_format(internalFormat
) != _mesa_is_ycbcr_format(format
)) ||
1940 (_mesa_is_depthstencil_format(internalFormat
) != _mesa_is_depthstencil_format(format
)) ||
1941 (_mesa_is_dudv_format(internalFormat
) != _mesa_is_dudv_format(format
))) {
1942 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1943 "glTexImage%dD(incompatible internalFormat 0x%x, format 0x%x)",
1944 dimensions
, internalFormat
, format
);
1948 /* additional checks for ycbcr textures */
1949 if (internalFormat
== GL_YCBCR_MESA
) {
1950 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
1951 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
1952 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
1954 _mesa_snprintf(message
, sizeof(message
),
1955 "glTexImage%dD(format/type YCBCR mismatch)",
1957 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", message
);
1958 return GL_TRUE
; /* error */
1960 if (target
!= GL_TEXTURE_2D
&&
1961 target
!= GL_PROXY_TEXTURE_2D
&&
1962 target
!= GL_TEXTURE_RECTANGLE_NV
&&
1963 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
1964 _mesa_error(ctx
, GL_INVALID_ENUM
,
1965 "glTexImage%dD(bad target for YCbCr texture)",
1971 _mesa_snprintf(message
, sizeof(message
),
1972 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1973 dimensions
, border
);
1974 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", message
);
1979 /* additional checks for depth textures */
1980 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
) {
1981 /* Only 1D, 2D, rect, array and cube textures supported, not 3D
1982 * Cubemaps are only supported for GL version > 3.0 or with EXT_gpu_shader4 */
1983 if (target
!= GL_TEXTURE_1D
&&
1984 target
!= GL_PROXY_TEXTURE_1D
&&
1985 target
!= GL_TEXTURE_2D
&&
1986 target
!= GL_PROXY_TEXTURE_2D
&&
1987 target
!= GL_TEXTURE_1D_ARRAY
&&
1988 target
!= GL_PROXY_TEXTURE_1D_ARRAY
&&
1989 target
!= GL_TEXTURE_2D_ARRAY
&&
1990 target
!= GL_PROXY_TEXTURE_2D_ARRAY
&&
1991 target
!= GL_TEXTURE_RECTANGLE_ARB
&&
1992 target
!= GL_PROXY_TEXTURE_RECTANGLE_ARB
&&
1993 !((_mesa_is_cube_face(target
) || target
== GL_PROXY_TEXTURE_CUBE_MAP
) &&
1994 (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_gpu_shader4
)) &&
1995 !((target
== GL_TEXTURE_CUBE_MAP_ARRAY
||
1996 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
) &&
1997 ctx
->Extensions
.ARB_texture_cube_map_array
)) {
1998 _mesa_error(ctx
, GL_INVALID_ENUM
,
1999 "glTexImage%dD(bad target for depth texture)",
2005 /* additional checks for compressed textures */
2006 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
2007 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
2008 _mesa_error(ctx
, GL_INVALID_ENUM
,
2009 "glTexImage%dD(target can't be compressed)", dimensions
);
2012 if (compressedteximage_only_format(ctx
, internalFormat
)) {
2013 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2014 "glTexImage%dD(no compression for format)", dimensions
);
2018 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2019 "glTexImage%dD(border!=0)", dimensions
);
2024 /* additional checks for integer textures */
2025 if ((ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) &&
2026 (_mesa_is_enum_format_integer(format
) !=
2027 _mesa_is_enum_format_integer(internalFormat
))) {
2028 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2029 "glTexImage%dD(integer/non-integer format mismatch)",
2034 if (!mutable_tex_object(ctx
, target
)) {
2035 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2036 "glTexImage%dD(immutable texture)", dimensions
);
2040 /* if we get here, the parameters are OK */
2046 * Error checking for glCompressedTexImage[123]D().
2047 * Note that the width, height and depth values are not fully error checked
2049 * \return GL_TRUE if a error is found, GL_FALSE otherwise
2052 compressed_texture_error_check(struct gl_context
*ctx
, GLint dimensions
,
2053 GLenum target
, GLint level
,
2054 GLenum internalFormat
, GLsizei width
,
2055 GLsizei height
, GLsizei depth
, GLint border
,
2058 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
2060 GLenum error
= GL_NO_ERROR
;
2061 char *reason
= ""; /* no error */
2063 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
2065 error
= GL_INVALID_ENUM
;
2069 /* This will detect any invalid internalFormat value */
2070 if (!_mesa_is_compressed_format(ctx
, internalFormat
)) {
2071 reason
= "internalFormat";
2072 error
= GL_INVALID_ENUM
;
2076 switch (internalFormat
) {
2077 case GL_PALETTE4_RGB8_OES
:
2078 case GL_PALETTE4_RGBA8_OES
:
2079 case GL_PALETTE4_R5_G6_B5_OES
:
2080 case GL_PALETTE4_RGBA4_OES
:
2081 case GL_PALETTE4_RGB5_A1_OES
:
2082 case GL_PALETTE8_RGB8_OES
:
2083 case GL_PALETTE8_RGBA8_OES
:
2084 case GL_PALETTE8_R5_G6_B5_OES
:
2085 case GL_PALETTE8_RGBA4_OES
:
2086 case GL_PALETTE8_RGB5_A1_OES
:
2087 /* check level (note that level should be zero or less!) */
2088 if (level
> 0 || level
< -maxLevels
) {
2090 error
= GL_INVALID_VALUE
;
2094 if (dimensions
!= 2) {
2095 reason
= "compressed paletted textures must be 2D";
2096 error
= GL_INVALID_OPERATION
;
2100 /* Figure out the expected texture size (in bytes). This will be
2101 * checked against the actual size later.
2103 expectedSize
= _mesa_cpal_compressed_size(level
, internalFormat
,
2106 /* This is for the benefit of the TestProxyTexImage below. It expects
2107 * level to be non-negative. OES_compressed_paletted_texture uses a
2108 * weird mechanism where the level specified to glCompressedTexImage2D
2109 * is -(n-1) number of levels in the texture, and the data specifies the
2110 * complete mipmap stack. This is done to ensure the palette is the
2111 * same for all levels.
2118 if (level
< 0 || level
>= maxLevels
) {
2120 error
= GL_INVALID_VALUE
;
2124 /* Figure out the expected texture size (in bytes). This will be
2125 * checked against the actual size later.
2127 expectedSize
= compressed_tex_size(width
, height
, depth
, internalFormat
);
2131 /* This should really never fail */
2132 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
2133 reason
= "internalFormat";
2134 error
= GL_INVALID_ENUM
;
2138 /* No compressed formats support borders at this time */
2140 reason
= "border != 0";
2141 error
= GL_INVALID_VALUE
;
2145 /* For cube map, width must equal height */
2146 if ((target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
2147 _mesa_is_cube_face(target
)) && width
!= height
) {
2148 reason
= "width != height";
2149 error
= GL_INVALID_VALUE
;
2153 /* check image size in bytes */
2154 if (expectedSize
!= imageSize
) {
2155 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
2156 * if <imageSize> is not consistent with the format, dimensions, and
2157 * contents of the specified image.
2159 reason
= "imageSize inconsistant with width/height/format";
2160 error
= GL_INVALID_VALUE
;
2164 if (!mutable_tex_object(ctx
, target
)) {
2165 reason
= "immutable texture";
2166 error
= GL_INVALID_OPERATION
;
2173 _mesa_error(ctx
, error
, "glCompressedTexImage%dD(%s)", dimensions
, reason
);
2180 * Test glTexSubImage[123]D() parameters for errors.
2182 * \param ctx GL context.
2183 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2184 * \param target texture target given by the user (already validated)
2185 * \param level image level given by the user.
2186 * \param xoffset sub-image x offset given by the user.
2187 * \param yoffset sub-image y offset given by the user.
2188 * \param zoffset sub-image z offset given by the user.
2189 * \param format pixel data format given by the user.
2190 * \param type pixel data type given by the user.
2191 * \param width image width given by the user.
2192 * \param height image height given by the user.
2193 * \param depth image depth given by the user.
2195 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2197 * Verifies each of the parameters against the constants specified in
2198 * __struct gl_contextRec::Const and the supported extensions, and according
2199 * to the OpenGL specification.
2202 texsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2203 GLenum target
, GLint level
,
2204 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2205 GLint width
, GLint height
, GLint depth
,
2206 GLenum format
, GLenum type
)
2208 struct gl_texture_object
*texObj
;
2209 struct gl_texture_image
*texImage
;
2212 /* check target (proxies not allowed) */
2213 if (!legal_texsubimage_target(ctx
, dimensions
, target
)) {
2214 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage%uD(target=%s)",
2215 dimensions
, _mesa_lookup_enum_by_nr(target
));
2220 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2221 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage%uD(level=%d)",
2226 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2227 * combinations of format and type that can be used. Formats and types
2228 * that require additional extensions (e.g., GL_FLOAT requires
2229 * GL_OES_texture_float) are filtered elsewhere.
2231 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
2232 err
= _mesa_es_error_check_format_and_type(format
, type
, dimensions
);
2233 if (err
!= GL_NO_ERROR
) {
2234 _mesa_error(ctx
, err
,
2235 "glTexSubImage%dD(format = %s, type = %s)",
2237 _mesa_lookup_enum_by_nr(format
),
2238 _mesa_lookup_enum_by_nr(type
));
2243 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
2244 if (err
!= GL_NO_ERROR
) {
2245 _mesa_error(ctx
, err
,
2246 "glTexSubImage%dD(incompatible format 0x%x, type 0x%x)",
2247 dimensions
, format
, type
);
2251 /* Get dest texture object / image pointers */
2252 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2254 /* must be out of memory */
2255 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage%dD()", dimensions
);
2259 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2261 /* non-existant texture level */
2262 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2263 "glTexSubImage%dD(invalid texture image)", dimensions
);
2267 if (error_check_subtexture_dimensions(ctx
, "glTexSubImage", dimensions
,
2268 texImage
, xoffset
, yoffset
, 0,
2269 width
, height
, 1)) {
2273 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2274 if (compressedteximage_only_format(ctx
, texImage
->InternalFormat
)) {
2275 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2276 "glTexSubImage%dD(no compression for format)", dimensions
);
2281 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
2282 /* both source and dest must be integer-valued, or neither */
2283 if (_mesa_is_format_integer_color(texImage
->TexFormat
) !=
2284 _mesa_is_enum_format_integer(format
)) {
2285 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2286 "glTexSubImage%dD(integer/non-integer format mismatch)",
2297 * Test glCopyTexImage[12]D() parameters for errors.
2299 * \param ctx GL context.
2300 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2301 * \param target texture target given by the user.
2302 * \param level image level given by the user.
2303 * \param internalFormat internal format given by the user.
2304 * \param width image width given by the user.
2305 * \param height image height given by the user.
2306 * \param border texture border.
2308 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2310 * Verifies each of the parameters against the constants specified in
2311 * __struct gl_contextRec::Const and the supported extensions, and according
2312 * to the OpenGL specification.
2315 copytexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
2316 GLenum target
, GLint level
, GLint internalFormat
,
2317 GLint width
, GLint height
, GLint border
)
2322 if (!legal_texsubimage_target(ctx
, dimensions
, target
)) {
2323 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexImage%uD(target=%s)",
2324 dimensions
, _mesa_lookup_enum_by_nr(target
));
2329 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2330 _mesa_error(ctx
, GL_INVALID_VALUE
,
2331 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
2335 /* Check that the source buffer is complete */
2336 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2337 if (ctx
->ReadBuffer
->_Status
== 0) {
2338 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2340 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2341 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2342 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
2346 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2347 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2348 "glCopyTexImage%dD(multisample FBO)",
2355 if (border
< 0 || border
> 1 ||
2356 ((ctx
->API
!= API_OPENGL
||
2357 target
== GL_TEXTURE_RECTANGLE_NV
||
2358 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
2359 _mesa_error(ctx
, GL_INVALID_VALUE
,
2360 "glCopyTexImage%dD(border=%d)", dimensions
, border
);
2364 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2367 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
2368 switch (internalFormat
) {
2373 case GL_LUMINANCE_ALPHA
:
2376 _mesa_error(ctx
, GL_INVALID_VALUE
,
2377 "glCopyTexImage%dD(internalFormat)", dimensions
);
2382 baseFormat
= _mesa_base_tex_format(ctx
, internalFormat
);
2383 if (baseFormat
< 0) {
2384 _mesa_error(ctx
, GL_INVALID_VALUE
,
2385 "glCopyTexImage%dD(internalFormat)", dimensions
);
2389 if (!_mesa_source_buffer_exists(ctx
, baseFormat
)) {
2390 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2391 "glCopyTexImage%dD(missing readbuffer)", dimensions
);
2395 /* From the EXT_texture_integer spec:
2397 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2398 * if the texture internalformat is an integer format and the read color
2399 * buffer is not an integer format, or if the internalformat is not an
2400 * integer format and the read color buffer is an integer format."
2402 if (_mesa_is_color_format(internalFormat
)) {
2403 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
2405 if (_mesa_is_enum_format_integer(rb
->InternalFormat
) !=
2406 _mesa_is_enum_format_integer(internalFormat
)) {
2407 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2408 "glCopyTexImage%dD(integer vs non-integer)", dimensions
);
2413 if ((target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
2414 _mesa_is_cube_face(target
)) && width
!= height
) {
2415 _mesa_error(ctx
, GL_INVALID_VALUE
,
2416 "glTexImage2D(cube width != height)");
2420 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
2421 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
2422 _mesa_error(ctx
, GL_INVALID_ENUM
,
2423 "glCopyTexImage%dD(target)", dimensions
);
2426 if (compressedteximage_only_format(ctx
, internalFormat
)) {
2427 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2428 "glCopyTexImage%dD(no compression for format)", dimensions
);
2432 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2433 "glCopyTexImage%dD(border!=0)", dimensions
);
2438 if (!mutable_tex_object(ctx
, target
)) {
2439 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2440 "glCopyTexImage%dD(immutable texture)", dimensions
);
2444 /* if we get here, the parameters are OK */
2450 * Test glCopyTexSubImage[12]D() parameters for errors.
2451 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2454 copytexsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2455 GLenum target
, GLint level
,
2456 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2457 GLint width
, GLint height
)
2459 struct gl_texture_object
*texObj
;
2460 struct gl_texture_image
*texImage
;
2462 /* Check that the source buffer is complete */
2463 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2464 if (ctx
->ReadBuffer
->_Status
== 0) {
2465 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2467 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2468 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2469 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
2473 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2474 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2475 "glCopyTexSubImage%dD(multisample FBO)",
2481 /* check target (proxies not allowed) */
2482 if (!legal_texsubimage_target(ctx
, dimensions
, target
)) {
2483 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage%uD(target=%s)",
2484 dimensions
, _mesa_lookup_enum_by_nr(target
));
2489 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2490 _mesa_error(ctx
, GL_INVALID_VALUE
,
2491 "glCopyTexSubImage%dD(level=%d)", dimensions
, level
);
2495 /* Get dest texture object / image pointers */
2496 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2498 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%dD()", dimensions
);
2502 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2504 /* destination image does not exist */
2505 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2506 "glCopyTexSubImage%dD(invalid texture image)", dimensions
);
2510 if (error_check_subtexture_dimensions(ctx
, "glCopyTexSubImage",
2511 dimensions
, texImage
,
2512 xoffset
, yoffset
, zoffset
,
2513 width
, height
, 1)) {
2517 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2518 if (compressedteximage_only_format(ctx
, texImage
->InternalFormat
)) {
2519 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2520 "glCopyTexSubImage%dD(no compression for format)", dimensions
);
2525 if (texImage
->InternalFormat
== GL_YCBCR_MESA
) {
2526 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCopyTexSubImage2D");
2530 if (!_mesa_source_buffer_exists(ctx
, texImage
->_BaseFormat
)) {
2531 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2532 "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
2533 dimensions
, texImage
->_BaseFormat
);
2537 /* From the EXT_texture_integer spec:
2539 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2540 * if the texture internalformat is an integer format and the read color
2541 * buffer is not an integer format, or if the internalformat is not an
2542 * integer format and the read color buffer is an integer format."
2544 if (_mesa_is_color_format(texImage
->InternalFormat
)) {
2545 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
2547 if (_mesa_is_format_integer_color(rb
->Format
) !=
2548 _mesa_is_format_integer_color(texImage
->TexFormat
)) {
2549 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2550 "glCopyTexImage%dD(integer vs non-integer)", dimensions
);
2555 /* if we get here, the parameters are OK */
2560 /** Callback info for walking over FBO hash table */
2563 struct gl_context
*ctx
;
2564 struct gl_texture_object
*texObj
;
2570 * Check render to texture callback. Called from _mesa_HashWalk().
2573 check_rtt_cb(GLuint key
, void *data
, void *userData
)
2575 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
2576 const struct cb_info
*info
= (struct cb_info
*) userData
;
2577 struct gl_context
*ctx
= info
->ctx
;
2578 const struct gl_texture_object
*texObj
= info
->texObj
;
2579 const GLuint level
= info
->level
, face
= info
->face
;
2581 /* If this is a user-created FBO */
2582 if (_mesa_is_user_fbo(fb
)) {
2584 /* check if any of the FBO's attachments point to 'texObj' */
2585 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2586 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2587 if (att
->Type
== GL_TEXTURE
&&
2588 att
->Texture
== texObj
&&
2589 att
->TextureLevel
== level
&&
2590 att
->CubeMapFace
== face
) {
2591 ASSERT(_mesa_get_attachment_teximage(att
));
2592 /* Tell driver about the new renderbuffer texture */
2593 ctx
->Driver
.RenderTexture(ctx
, ctx
->DrawBuffer
, att
);
2594 /* Mark fb status as indeterminate to force re-validation */
2603 * When a texture image is specified we have to check if it's bound to
2604 * any framebuffer objects (render to texture) in order to detect changes
2605 * in size or format since that effects FBO completeness.
2606 * Any FBOs rendering into the texture must be re-validated.
2609 _mesa_update_fbo_texture(struct gl_context
*ctx
,
2610 struct gl_texture_object
*texObj
,
2611 GLuint face
, GLuint level
)
2613 /* Only check this texture if it's been marked as RenderToTexture */
2614 if (texObj
->_RenderToTexture
) {
2615 struct cb_info info
;
2617 info
.texObj
= texObj
;
2620 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, check_rtt_cb
, &info
);
2626 * If the texture object's GenerateMipmap flag is set and we've
2627 * changed the texture base level image, regenerate the rest of the
2628 * mipmap levels now.
2631 check_gen_mipmap(struct gl_context
*ctx
, GLenum target
,
2632 struct gl_texture_object
*texObj
, GLint level
)
2634 ASSERT(target
!= GL_TEXTURE_CUBE_MAP
);
2635 if (texObj
->GenerateMipmap
&&
2636 level
== texObj
->BaseLevel
&&
2637 level
< texObj
->MaxLevel
) {
2638 ASSERT(ctx
->Driver
.GenerateMipmap
);
2639 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2644 /** Debug helper: override the user-requested internal format */
2646 override_internal_format(GLenum internalFormat
, GLint width
, GLint height
)
2649 if (internalFormat
== GL_RGBA16F_ARB
||
2650 internalFormat
== GL_RGBA32F_ARB
) {
2651 printf("Convert rgba float tex to int %d x %d\n", width
, height
);
2654 else if (internalFormat
== GL_RGB16F_ARB
||
2655 internalFormat
== GL_RGB32F_ARB
) {
2656 printf("Convert rgb float tex to int %d x %d\n", width
, height
);
2659 else if (internalFormat
== GL_LUMINANCE_ALPHA16F_ARB
||
2660 internalFormat
== GL_LUMINANCE_ALPHA32F_ARB
) {
2661 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2662 return GL_LUMINANCE_ALPHA
;
2664 else if (internalFormat
== GL_LUMINANCE16F_ARB
||
2665 internalFormat
== GL_LUMINANCE32F_ARB
) {
2666 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2667 return GL_LUMINANCE
;
2669 else if (internalFormat
== GL_ALPHA16F_ARB
||
2670 internalFormat
== GL_ALPHA32F_ARB
) {
2671 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2675 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2676 internalFormat = GL_RGBA;
2680 return internalFormat
;
2683 return internalFormat
;
2689 * Choose the actual hardware format for a texture image.
2690 * Try to use the same format as the previous image level when possible.
2691 * Otherwise, ask the driver for the best format.
2692 * It's important to try to choose a consistant format for all levels
2693 * for efficient texture memory layout/allocation. In particular, this
2694 * comes up during automatic mipmap generation.
2697 _mesa_choose_texture_format(struct gl_context
*ctx
,
2698 struct gl_texture_object
*texObj
,
2699 GLenum target
, GLint level
,
2700 GLenum internalFormat
, GLenum format
, GLenum type
)
2704 /* see if we've already chosen a format for the previous level */
2706 struct gl_texture_image
*prevImage
=
2707 _mesa_select_tex_image(ctx
, texObj
, target
, level
- 1);
2708 /* See if the prev level is defined and has an internal format which
2709 * matches the new internal format.
2712 prevImage
->Width
> 0 &&
2713 prevImage
->InternalFormat
== internalFormat
) {
2714 /* use the same format */
2715 ASSERT(prevImage
->TexFormat
!= MESA_FORMAT_NONE
);
2716 return prevImage
->TexFormat
;
2720 /* If the application requested compression to an S3TC format but we don't
2721 * have the DTXn library, force a generic compressed format instead.
2723 if (internalFormat
!= format
&& format
!= GL_NONE
) {
2724 const GLenum before
= internalFormat
;
2726 switch (internalFormat
) {
2727 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
2728 if (!ctx
->Mesa_DXTn
)
2729 internalFormat
= GL_COMPRESSED_RGB
;
2731 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
2732 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
2733 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
2734 if (!ctx
->Mesa_DXTn
)
2735 internalFormat
= GL_COMPRESSED_RGBA
;
2741 if (before
!= internalFormat
) {
2743 "DXT compression requested (%s), "
2744 "but libtxc_dxtn library not installed. Using %s "
2746 _mesa_lookup_enum_by_nr(before
),
2747 _mesa_lookup_enum_by_nr(internalFormat
));
2751 /* choose format from scratch */
2752 f
= ctx
->Driver
.ChooseTextureFormat(ctx
, texObj
->Target
, internalFormat
,
2754 ASSERT(f
!= MESA_FORMAT_NONE
);
2759 * Adjust pixel unpack params and image dimensions to strip off the
2760 * one-pixel texture border.
2762 * Gallium and intel don't support texture borders. They've seldem been used
2763 * and seldom been implemented correctly anyway.
2765 * \param unpackNew returns the new pixel unpack parameters
2768 strip_texture_border(GLenum target
,
2769 GLint
*width
, GLint
*height
, GLint
*depth
,
2770 const struct gl_pixelstore_attrib
*unpack
,
2771 struct gl_pixelstore_attrib
*unpackNew
)
2777 *unpackNew
= *unpack
;
2779 if (unpackNew
->RowLength
== 0)
2780 unpackNew
->RowLength
= *width
;
2782 if (unpackNew
->ImageHeight
== 0)
2783 unpackNew
->ImageHeight
= *height
;
2785 assert(*width
>= 3);
2786 unpackNew
->SkipPixels
++; /* skip the border */
2787 *width
= *width
- 2; /* reduce the width by two border pixels */
2789 /* The min height of a texture with a border is 3 */
2790 if (*height
>= 3 && target
!= GL_TEXTURE_1D_ARRAY
) {
2791 unpackNew
->SkipRows
++; /* skip the border */
2792 *height
= *height
- 2; /* reduce the height by two border pixels */
2795 if (*depth
>= 3 && target
!= GL_TEXTURE_2D_ARRAY
&& target
!= GL_TEXTURE_CUBE_MAP_ARRAY
) {
2796 unpackNew
->SkipImages
++; /* skip the border */
2797 *depth
= *depth
- 2; /* reduce the depth by two border pixels */
2803 * Common code to implement all the glTexImage1D/2D/3D functions
2804 * as well as glCompressedTexImage1D/2D/3D.
2805 * \param compressed only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
2806 * \param format the user's image format (only used if !compressed)
2807 * \param type the user's image type (only used if !compressed)
2808 * \param imageSize only used for glCompressedTexImage1D/2D/3D calls.
2811 teximage(struct gl_context
*ctx
, GLboolean compressed
, GLuint dims
,
2812 GLenum target
, GLint level
, GLint internalFormat
,
2813 GLsizei width
, GLsizei height
, GLsizei depth
,
2814 GLint border
, GLenum format
, GLenum type
,
2815 GLsizei imageSize
, const GLvoid
*pixels
)
2817 const char *func
= compressed
? "glCompressedTexImage" : "glTexImage";
2818 struct gl_pixelstore_attrib unpack_no_border
;
2819 const struct gl_pixelstore_attrib
*unpack
= &ctx
->Unpack
;
2820 struct gl_texture_object
*texObj
;
2821 gl_format texFormat
;
2822 GLboolean dimensionsOK
, sizeOK
;
2824 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2826 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
)) {
2829 "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
2831 _mesa_lookup_enum_by_nr(target
), level
,
2832 _mesa_lookup_enum_by_nr(internalFormat
),
2833 width
, height
, depth
, border
, pixels
);
2836 "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2838 _mesa_lookup_enum_by_nr(target
), level
,
2839 _mesa_lookup_enum_by_nr(internalFormat
),
2840 width
, height
, depth
, border
,
2841 _mesa_lookup_enum_by_nr(format
),
2842 _mesa_lookup_enum_by_nr(type
), pixels
);
2845 internalFormat
= override_internal_format(internalFormat
, width
, height
);
2847 /* target error checking */
2848 if (!legal_teximage_target(ctx
, dims
, target
)) {
2849 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s%uD(target=%s)",
2850 func
, dims
, _mesa_lookup_enum_by_nr(target
));
2854 /* general error checking */
2856 if (compressed_texture_error_check(ctx
, dims
, target
, level
,
2858 width
, height
, depth
,
2863 if (texture_error_check(ctx
, dims
, target
, level
, internalFormat
,
2864 format
, type
, width
, height
, depth
, border
))
2868 /* Here we convert a cpal compressed image into a regular glTexImage2D
2869 * call by decompressing the texture. If we really want to support cpal
2870 * textures in any driver this would have to be changed.
2872 if (ctx
->API
== API_OPENGLES
&& compressed
&& dims
== 2) {
2873 switch (internalFormat
) {
2874 case GL_PALETTE4_RGB8_OES
:
2875 case GL_PALETTE4_RGBA8_OES
:
2876 case GL_PALETTE4_R5_G6_B5_OES
:
2877 case GL_PALETTE4_RGBA4_OES
:
2878 case GL_PALETTE4_RGB5_A1_OES
:
2879 case GL_PALETTE8_RGB8_OES
:
2880 case GL_PALETTE8_RGBA8_OES
:
2881 case GL_PALETTE8_R5_G6_B5_OES
:
2882 case GL_PALETTE8_RGBA4_OES
:
2883 case GL_PALETTE8_RGB5_A1_OES
:
2884 _mesa_cpal_compressed_teximage2d(target
, level
, internalFormat
,
2885 width
, height
, imageSize
, pixels
);
2890 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2893 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
2894 internalFormat
, format
, type
);
2895 assert(texFormat
!= MESA_FORMAT_NONE
);
2897 /* check that width, height, depth are legal for the mipmap level */
2898 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, level
, width
,
2899 height
, depth
, border
);
2901 /* check that the texture won't take too much memory, etc */
2902 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, _mesa_get_proxy_target(target
),
2904 width
, height
, depth
, border
);
2906 if (_mesa_is_proxy_texture(target
)) {
2907 /* Proxy texture: just clear or set state depending on error checking */
2908 struct gl_texture_image
*texImage
=
2909 get_proxy_tex_image(ctx
, target
, level
);
2912 return; /* GL_OUT_OF_MEMORY already recorded */
2914 if (dimensionsOK
&& sizeOK
) {
2915 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, depth
,
2916 border
, internalFormat
, texFormat
);
2919 clear_teximage_fields(texImage
);
2923 /* non-proxy target */
2924 const GLuint face
= _mesa_tex_target_to_face(target
);
2925 struct gl_texture_image
*texImage
;
2927 if (!dimensionsOK
) {
2928 _mesa_error(ctx
, GL_INVALID_VALUE
,
2929 "glTexImage%uD(invalid width or height or depth)",
2935 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
2936 "glTexImage%uD(image too large)", dims
);
2940 /* Allow a hardware driver to just strip out the border, to provide
2941 * reliable but slightly incorrect hardware rendering instead of
2942 * rarely-tested software fallback rendering.
2944 if (border
&& ctx
->Const
.StripTextureBorder
) {
2945 strip_texture_border(target
, &width
, &height
, &depth
, unpack
,
2948 unpack
= &unpack_no_border
;
2951 if (ctx
->NewState
& _NEW_PIXEL
)
2952 _mesa_update_state(ctx
);
2954 _mesa_lock_texture(ctx
, texObj
);
2956 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2959 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s%uD", func
, dims
);
2962 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
2964 _mesa_init_teximage_fields(ctx
, texImage
,
2965 width
, height
, depth
,
2966 border
, internalFormat
, texFormat
);
2968 /* Give the texture to the driver. <pixels> may be null. */
2969 if (width
> 0 && height
> 0 && depth
> 0) {
2971 ctx
->Driver
.CompressedTexImage(ctx
, dims
, texImage
,
2975 ctx
->Driver
.TexImage(ctx
, dims
, texImage
, format
,
2976 type
, pixels
, unpack
);
2980 check_gen_mipmap(ctx
, target
, texObj
, level
);
2982 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
2984 _mesa_dirty_texobj(ctx
, texObj
, GL_TRUE
);
2987 _mesa_unlock_texture(ctx
, texObj
);
2994 * Called from the API. Note that width includes the border.
2997 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
2998 GLsizei width
, GLint border
, GLenum format
,
2999 GLenum type
, const GLvoid
*pixels
)
3001 GET_CURRENT_CONTEXT(ctx
);
3002 teximage(ctx
, GL_FALSE
, 1, target
, level
, internalFormat
, width
, 1, 1,
3003 border
, format
, type
, 0, pixels
);
3008 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
3009 GLsizei width
, GLsizei height
, GLint border
,
3010 GLenum format
, GLenum type
,
3011 const GLvoid
*pixels
)
3013 GET_CURRENT_CONTEXT(ctx
);
3014 teximage(ctx
, GL_FALSE
, 2, target
, level
, internalFormat
, width
, height
, 1,
3015 border
, format
, type
, 0, pixels
);
3020 * Called by the API or display list executor.
3021 * Note that width and height include the border.
3024 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
3025 GLsizei width
, GLsizei height
, GLsizei depth
,
3026 GLint border
, GLenum format
, GLenum type
,
3027 const GLvoid
*pixels
)
3029 GET_CURRENT_CONTEXT(ctx
);
3030 teximage(ctx
, GL_FALSE
, 3, target
, level
, internalFormat
,
3031 width
, height
, depth
,
3032 border
, format
, type
, 0, pixels
);
3037 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
3038 GLsizei width
, GLsizei height
, GLsizei depth
,
3039 GLint border
, GLenum format
, GLenum type
,
3040 const GLvoid
*pixels
)
3042 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
3043 depth
, border
, format
, type
, pixels
);
3048 _mesa_EGLImageTargetTexture2DOES (GLenum target
, GLeglImageOES image
)
3050 struct gl_texture_object
*texObj
;
3051 struct gl_texture_image
*texImage
;
3053 GET_CURRENT_CONTEXT(ctx
);
3054 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3058 valid_target
= ctx
->Extensions
.OES_EGL_image
;
3060 case GL_TEXTURE_EXTERNAL_OES
:
3061 valid_target
= ctx
->Extensions
.OES_EGL_image_external
;
3064 valid_target
= false;
3068 if (!valid_target
) {
3069 _mesa_error(ctx
, GL_INVALID_ENUM
,
3070 "glEGLImageTargetTexture2D(target=%d)", target
);
3074 if (ctx
->NewState
& _NEW_PIXEL
)
3075 _mesa_update_state(ctx
);
3077 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3078 _mesa_lock_texture(ctx
, texObj
);
3080 if (texObj
->Immutable
) {
3081 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3082 "glEGLImageTargetTexture2D(texture is immutable)");
3083 _mesa_unlock_texture(ctx
, texObj
);
3087 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
3089 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glEGLImageTargetTexture2D");
3091 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3093 ctx
->Driver
.EGLImageTargetTexture2D(ctx
, target
,
3094 texObj
, texImage
, image
);
3096 _mesa_dirty_texobj(ctx
, texObj
, GL_TRUE
);
3098 _mesa_unlock_texture(ctx
, texObj
);
3105 * Implement all the glTexSubImage1/2/3D() functions.
3108 texsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
3109 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3110 GLsizei width
, GLsizei height
, GLsizei depth
,
3111 GLenum format
, GLenum type
, const GLvoid
*pixels
)
3113 struct gl_texture_object
*texObj
;
3114 struct gl_texture_image
*texImage
;
3116 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3118 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3119 _mesa_debug(ctx
, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3121 _mesa_lookup_enum_by_nr(target
), level
,
3122 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3123 _mesa_lookup_enum_by_nr(format
),
3124 _mesa_lookup_enum_by_nr(type
), pixels
);
3126 /* check target (proxies not allowed) */
3127 if (!legal_texsubimage_target(ctx
, dims
, target
)) {
3128 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage%uD(target=%s)",
3129 dims
, _mesa_lookup_enum_by_nr(target
));
3133 if (ctx
->NewState
& _NEW_PIXEL
)
3134 _mesa_update_state(ctx
);
3136 if (texsubimage_error_check(ctx
, dims
, target
, level
,
3137 xoffset
, yoffset
, zoffset
,
3138 width
, height
, depth
, format
, type
)) {
3139 return; /* error was detected */
3142 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3144 _mesa_lock_texture(ctx
, texObj
);
3146 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3148 if (width
> 0 && height
> 0 && depth
> 0) {
3149 /* If we have a border, offset=-1 is legal. Bias by border width. */
3152 if (target
!= GL_TEXTURE_2D_ARRAY
)
3153 zoffset
+= texImage
->Border
;
3156 if (target
!= GL_TEXTURE_1D_ARRAY
)
3157 yoffset
+= texImage
->Border
;
3160 xoffset
+= texImage
->Border
;
3163 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3164 xoffset
, yoffset
, zoffset
,
3165 width
, height
, depth
,
3166 format
, type
, pixels
, &ctx
->Unpack
);
3168 check_gen_mipmap(ctx
, target
, texObj
, level
);
3170 ctx
->NewState
|= _NEW_TEXTURE
;
3173 _mesa_unlock_texture(ctx
, texObj
);
3178 _mesa_TexSubImage1D( GLenum target
, GLint level
,
3179 GLint xoffset
, GLsizei width
,
3180 GLenum format
, GLenum type
,
3181 const GLvoid
*pixels
)
3183 GET_CURRENT_CONTEXT(ctx
);
3184 texsubimage(ctx
, 1, target
, level
,
3187 format
, type
, pixels
);
3192 _mesa_TexSubImage2D( GLenum target
, GLint level
,
3193 GLint xoffset
, GLint yoffset
,
3194 GLsizei width
, GLsizei height
,
3195 GLenum format
, GLenum type
,
3196 const GLvoid
*pixels
)
3198 GET_CURRENT_CONTEXT(ctx
);
3199 texsubimage(ctx
, 2, target
, level
,
3200 xoffset
, yoffset
, 0,
3202 format
, type
, pixels
);
3208 _mesa_TexSubImage3D( GLenum target
, GLint level
,
3209 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3210 GLsizei width
, GLsizei height
, GLsizei depth
,
3211 GLenum format
, GLenum type
,
3212 const GLvoid
*pixels
)
3214 GET_CURRENT_CONTEXT(ctx
);
3215 texsubimage(ctx
, 3, target
, level
,
3216 xoffset
, yoffset
, zoffset
,
3217 width
, height
, depth
,
3218 format
, type
, pixels
);
3224 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3225 * from. This depends on whether the texture contains color or depth values.
3227 static struct gl_renderbuffer
*
3228 get_copy_tex_image_source(struct gl_context
*ctx
, gl_format texFormat
)
3230 if (_mesa_get_format_bits(texFormat
, GL_DEPTH_BITS
) > 0) {
3231 /* reading from depth/stencil buffer */
3232 return ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
3235 /* copying from color buffer */
3236 return ctx
->ReadBuffer
->_ColorReadBuffer
;
3243 * Implement the glCopyTexImage1/2D() functions.
3246 copyteximage(struct gl_context
*ctx
, GLuint dims
,
3247 GLenum target
, GLint level
, GLenum internalFormat
,
3248 GLint x
, GLint y
, GLsizei width
, GLsizei height
, GLint border
)
3250 struct gl_texture_object
*texObj
;
3251 struct gl_texture_image
*texImage
;
3252 const GLuint face
= _mesa_tex_target_to_face(target
);
3253 gl_format texFormat
;
3255 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3257 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3258 _mesa_debug(ctx
, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3260 _mesa_lookup_enum_by_nr(target
), level
,
3261 _mesa_lookup_enum_by_nr(internalFormat
),
3262 x
, y
, width
, height
, border
);
3264 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3265 _mesa_update_state(ctx
);
3267 if (copytexture_error_check(ctx
, dims
, target
, level
, internalFormat
,
3268 width
, height
, border
))
3271 if (!_mesa_legal_texture_dimensions(ctx
, target
, level
, width
, height
,
3273 _mesa_error(ctx
, GL_INVALID_VALUE
,
3274 "glCopyTexImage%uD(invalid width or height)", dims
);
3278 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3281 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3282 internalFormat
, GL_NONE
, GL_NONE
);
3283 assert(texFormat
!= MESA_FORMAT_NONE
);
3285 if (!ctx
->Driver
.TestProxyTexImage(ctx
, _mesa_get_proxy_target(target
),
3287 width
, height
, 1, border
)) {
3288 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3289 "glCopyTexImage%uD(image too large)", dims
);
3293 if (border
&& ctx
->Const
.StripTextureBorder
) {
3295 width
-= border
* 2;
3298 height
-= border
* 2;
3303 _mesa_lock_texture(ctx
, texObj
);
3305 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3308 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
3311 GLint srcX
= x
, srcY
= y
, dstX
= 0, dstY
= 0, dstZ
= 0;
3313 /* Free old texture image */
3314 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3316 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, 1,
3317 border
, internalFormat
, texFormat
);
3319 /* Allocate texture memory (no pixel data yet) */
3320 ctx
->Driver
.TexImage(ctx
, dims
, texImage
,
3322 NULL
, &ctx
->Unpack
);
3324 if (_mesa_clip_copytexsubimage(ctx
, &dstX
, &dstY
, &srcX
, &srcY
,
3326 struct gl_renderbuffer
*srcRb
=
3327 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3329 ctx
->Driver
.CopyTexSubImage(ctx
, dims
, texImage
, dstX
, dstY
, dstZ
,
3330 srcRb
, srcX
, srcY
, width
, height
);
3333 check_gen_mipmap(ctx
, target
, texObj
, level
);
3335 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3337 _mesa_dirty_texobj(ctx
, texObj
, GL_TRUE
);
3340 _mesa_unlock_texture(ctx
, texObj
);
3346 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
3347 GLenum internalFormat
,
3349 GLsizei width
, GLint border
)
3351 GET_CURRENT_CONTEXT(ctx
);
3352 copyteximage(ctx
, 1, target
, level
, internalFormat
, x
, y
, width
, 1, border
);
3358 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
3359 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
3362 GET_CURRENT_CONTEXT(ctx
);
3363 copyteximage(ctx
, 2, target
, level
, internalFormat
,
3364 x
, y
, width
, height
, border
);
3370 * Implementation for glCopyTexSubImage1/2/3D() functions.
3373 copytexsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
3374 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3375 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3377 struct gl_texture_object
*texObj
;
3378 struct gl_texture_image
*texImage
;
3380 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3382 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3383 _mesa_debug(ctx
, "glCopyTexSubImage%uD %s %d %d %d %d %d %d %d %d\n",
3385 _mesa_lookup_enum_by_nr(target
),
3386 level
, xoffset
, yoffset
, zoffset
, x
, y
, width
, height
);
3388 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3389 _mesa_update_state(ctx
);
3391 if (copytexsubimage_error_check(ctx
, dims
, target
, level
,
3392 xoffset
, yoffset
, zoffset
, width
, height
)) {
3396 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3398 _mesa_lock_texture(ctx
, texObj
);
3400 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3402 /* If we have a border, offset=-1 is legal. Bias by border width. */
3405 if (target
!= GL_TEXTURE_2D_ARRAY
)
3406 zoffset
+= texImage
->Border
;
3409 if (target
!= GL_TEXTURE_1D_ARRAY
)
3410 yoffset
+= texImage
->Border
;
3413 xoffset
+= texImage
->Border
;
3416 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3418 struct gl_renderbuffer
*srcRb
=
3419 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3421 ctx
->Driver
.CopyTexSubImage(ctx
, dims
, texImage
,
3422 xoffset
, yoffset
, zoffset
,
3423 srcRb
, x
, y
, width
, height
);
3425 check_gen_mipmap(ctx
, target
, texObj
, level
);
3427 ctx
->NewState
|= _NEW_TEXTURE
;
3430 _mesa_unlock_texture(ctx
, texObj
);
3435 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
3436 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
3438 GET_CURRENT_CONTEXT(ctx
);
3439 copytexsubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, x
, y
, width
, 1);
3445 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
3446 GLint xoffset
, GLint yoffset
,
3447 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3449 GET_CURRENT_CONTEXT(ctx
);
3450 copytexsubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0, x
, y
,
3457 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
3458 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3459 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3461 GET_CURRENT_CONTEXT(ctx
);
3462 copytexsubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
3463 x
, y
, width
, height
);
3469 /**********************************************************************/
3470 /****** Compressed Textures ******/
3471 /**********************************************************************/
3475 * Error checking for glCompressedTexSubImage[123]D().
3476 * \return error code or GL_NO_ERROR.
3479 compressed_subtexture_error_check(struct gl_context
*ctx
, GLint dims
,
3480 GLenum target
, GLint level
,
3481 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3482 GLsizei width
, GLsizei height
, GLsizei depth
,
3483 GLenum format
, GLsizei imageSize
)
3485 struct gl_texture_object
*texObj
;
3486 struct gl_texture_image
*texImage
;
3493 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
3494 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
3495 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
3496 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
3497 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
3498 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
3502 targetOK
= GL_FALSE
;
3506 assert(dims
== 1 || dims
== 3);
3507 /* no 1D or 3D compressed textures at this time */
3508 targetOK
= GL_FALSE
;
3512 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage%uD(target)",
3517 /* this will catch any invalid compressed format token */
3518 if (!_mesa_is_compressed_format(ctx
, format
)) {
3519 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage%uD(format)",
3524 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
3525 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexImage%uD(level=%d)",
3530 expectedSize
= compressed_tex_size(width
, height
, depth
, format
);
3531 if (expectedSize
!= imageSize
) {
3532 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexImage%uD(size=%d)",
3537 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3539 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3540 "glCompressedTexSubImage%uD()", dims
);
3544 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3546 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3547 "glCompressedTexSubImage%uD(invalid texture image)", dims
);
3551 if ((GLint
) format
!= texImage
->InternalFormat
) {
3552 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3553 "glCompressedTexSubImage%uD(format=0x%x)", dims
, format
);
3557 if (compressedteximage_only_format(ctx
, format
)) {
3558 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3559 "glCompressedTexSubImage%uD(format=0x%x cannot be updated)"
3564 if (error_check_subtexture_dimensions(ctx
, "glCompressedTexSubImage", dims
,
3565 texImage
, xoffset
, yoffset
, zoffset
,
3566 width
, height
, depth
)) {
3575 _mesa_CompressedTexImage1D(GLenum target
, GLint level
,
3576 GLenum internalFormat
, GLsizei width
,
3577 GLint border
, GLsizei imageSize
,
3580 GET_CURRENT_CONTEXT(ctx
);
3581 teximage(ctx
, GL_TRUE
, 1, target
, level
, internalFormat
,
3582 width
, 1, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
3587 _mesa_CompressedTexImage2D(GLenum target
, GLint level
,
3588 GLenum internalFormat
, GLsizei width
,
3589 GLsizei height
, GLint border
, GLsizei imageSize
,
3592 GET_CURRENT_CONTEXT(ctx
);
3593 teximage(ctx
, GL_TRUE
, 2, target
, level
, internalFormat
,
3594 width
, height
, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
3599 _mesa_CompressedTexImage3D(GLenum target
, GLint level
,
3600 GLenum internalFormat
, GLsizei width
,
3601 GLsizei height
, GLsizei depth
, GLint border
,
3602 GLsizei imageSize
, const GLvoid
*data
)
3604 GET_CURRENT_CONTEXT(ctx
);
3605 teximage(ctx
, GL_TRUE
, 3, target
, level
, internalFormat
,
3606 width
, height
, depth
, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
3611 * Common helper for glCompressedTexSubImage1/2/3D().
3614 compressed_tex_sub_image(GLuint dims
, GLenum target
, GLint level
,
3615 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3616 GLsizei width
, GLsizei height
, GLsizei depth
,
3617 GLenum format
, GLsizei imageSize
, const GLvoid
*data
)
3619 struct gl_texture_object
*texObj
;
3620 struct gl_texture_image
*texImage
;
3621 GET_CURRENT_CONTEXT(ctx
);
3622 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3624 if (compressed_subtexture_error_check(ctx
, dims
, target
, level
,
3625 xoffset
, yoffset
, zoffset
,
3626 width
, height
, depth
,
3627 format
, imageSize
)) {
3631 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3633 _mesa_lock_texture(ctx
, texObj
);
3635 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3638 if (width
> 0 && height
> 0 && depth
> 0) {
3639 ctx
->Driver
.CompressedTexSubImage(ctx
, dims
, texImage
,
3640 xoffset
, yoffset
, zoffset
,
3641 width
, height
, depth
,
3642 format
, imageSize
, data
);
3644 check_gen_mipmap(ctx
, target
, texObj
, level
);
3646 ctx
->NewState
|= _NEW_TEXTURE
;
3649 _mesa_unlock_texture(ctx
, texObj
);
3654 _mesa_CompressedTexSubImage1D(GLenum target
, GLint level
, GLint xoffset
,
3655 GLsizei width
, GLenum format
,
3656 GLsizei imageSize
, const GLvoid
*data
)
3658 compressed_tex_sub_image(1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
3659 format
, imageSize
, data
);
3664 _mesa_CompressedTexSubImage2D(GLenum target
, GLint level
, GLint xoffset
,
3665 GLint yoffset
, GLsizei width
, GLsizei height
,
3666 GLenum format
, GLsizei imageSize
,
3669 compressed_tex_sub_image(2, target
, level
, xoffset
, yoffset
, 0,
3670 width
, height
, 1, format
, imageSize
, data
);
3675 _mesa_CompressedTexSubImage3D(GLenum target
, GLint level
, GLint xoffset
,
3676 GLint yoffset
, GLint zoffset
, GLsizei width
,
3677 GLsizei height
, GLsizei depth
, GLenum format
,
3678 GLsizei imageSize
, const GLvoid
*data
)
3680 compressed_tex_sub_image(3, target
, level
, xoffset
, yoffset
, zoffset
,
3681 width
, height
, depth
, format
, imageSize
, data
);
3685 get_texbuffer_format(const struct gl_context
*ctx
, GLenum internalFormat
)
3687 switch (internalFormat
) {
3689 return MESA_FORMAT_A8
;
3691 return MESA_FORMAT_A16
;
3692 case GL_ALPHA16F_ARB
:
3693 return MESA_FORMAT_ALPHA_FLOAT16
;
3694 case GL_ALPHA32F_ARB
:
3695 return MESA_FORMAT_ALPHA_FLOAT32
;
3696 case GL_ALPHA8I_EXT
:
3697 return MESA_FORMAT_ALPHA_INT8
;
3698 case GL_ALPHA16I_EXT
:
3699 return MESA_FORMAT_ALPHA_INT16
;
3700 case GL_ALPHA32I_EXT
:
3701 return MESA_FORMAT_ALPHA_INT32
;
3702 case GL_ALPHA8UI_EXT
:
3703 return MESA_FORMAT_ALPHA_UINT8
;
3704 case GL_ALPHA16UI_EXT
:
3705 return MESA_FORMAT_ALPHA_UINT16
;
3706 case GL_ALPHA32UI_EXT
:
3707 return MESA_FORMAT_ALPHA_UINT32
;
3709 return MESA_FORMAT_L8
;
3710 case GL_LUMINANCE16
:
3711 return MESA_FORMAT_L16
;
3712 case GL_LUMINANCE16F_ARB
:
3713 return MESA_FORMAT_LUMINANCE_FLOAT16
;
3714 case GL_LUMINANCE32F_ARB
:
3715 return MESA_FORMAT_LUMINANCE_FLOAT32
;
3716 case GL_LUMINANCE8I_EXT
:
3717 return MESA_FORMAT_LUMINANCE_INT8
;
3718 case GL_LUMINANCE16I_EXT
:
3719 return MESA_FORMAT_LUMINANCE_INT16
;
3720 case GL_LUMINANCE32I_EXT
:
3721 return MESA_FORMAT_LUMINANCE_INT32
;
3722 case GL_LUMINANCE8UI_EXT
:
3723 return MESA_FORMAT_LUMINANCE_UINT8
;
3724 case GL_LUMINANCE16UI_EXT
:
3725 return MESA_FORMAT_LUMINANCE_UINT16
;
3726 case GL_LUMINANCE32UI_EXT
:
3727 return MESA_FORMAT_LUMINANCE_UINT32
;
3728 case GL_LUMINANCE8_ALPHA8
:
3729 return MESA_FORMAT_AL88
;
3730 case GL_LUMINANCE16_ALPHA16
:
3731 return MESA_FORMAT_AL1616
;
3732 case GL_LUMINANCE_ALPHA16F_ARB
:
3733 return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16
;
3734 case GL_LUMINANCE_ALPHA32F_ARB
:
3735 return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32
;
3736 case GL_LUMINANCE_ALPHA8I_EXT
:
3737 return MESA_FORMAT_LUMINANCE_ALPHA_INT8
;
3738 case GL_LUMINANCE_ALPHA16I_EXT
:
3739 return MESA_FORMAT_LUMINANCE_ALPHA_INT8
;
3740 case GL_LUMINANCE_ALPHA32I_EXT
:
3741 return MESA_FORMAT_LUMINANCE_ALPHA_INT16
;
3742 case GL_LUMINANCE_ALPHA8UI_EXT
:
3743 return MESA_FORMAT_LUMINANCE_ALPHA_UINT8
;
3744 case GL_LUMINANCE_ALPHA16UI_EXT
:
3745 return MESA_FORMAT_LUMINANCE_ALPHA_UINT16
;
3746 case GL_LUMINANCE_ALPHA32UI_EXT
:
3747 return MESA_FORMAT_LUMINANCE_ALPHA_UINT32
;
3749 return MESA_FORMAT_I8
;
3750 case GL_INTENSITY16
:
3751 return MESA_FORMAT_I16
;
3752 case GL_INTENSITY16F_ARB
:
3753 return MESA_FORMAT_INTENSITY_FLOAT16
;
3754 case GL_INTENSITY32F_ARB
:
3755 return MESA_FORMAT_INTENSITY_FLOAT32
;
3756 case GL_INTENSITY8I_EXT
:
3757 return MESA_FORMAT_INTENSITY_INT8
;
3758 case GL_INTENSITY16I_EXT
:
3759 return MESA_FORMAT_INTENSITY_INT16
;
3760 case GL_INTENSITY32I_EXT
:
3761 return MESA_FORMAT_INTENSITY_INT32
;
3762 case GL_INTENSITY8UI_EXT
:
3763 return MESA_FORMAT_INTENSITY_UINT8
;
3764 case GL_INTENSITY16UI_EXT
:
3765 return MESA_FORMAT_INTENSITY_UINT16
;
3766 case GL_INTENSITY32UI_EXT
:
3767 return MESA_FORMAT_INTENSITY_UINT32
;
3769 return MESA_FORMAT_RGBA8888_REV
;
3771 return MESA_FORMAT_RGBA_16
;
3772 case GL_RGBA16F_ARB
:
3773 return MESA_FORMAT_RGBA_FLOAT16
;
3774 case GL_RGBA32F_ARB
:
3775 return MESA_FORMAT_RGBA_FLOAT32
;
3777 return MESA_FORMAT_RGBA_INT8
;
3778 case GL_RGBA16I_EXT
:
3779 return MESA_FORMAT_RGBA_INT16
;
3780 case GL_RGBA32I_EXT
:
3781 return MESA_FORMAT_RGBA_INT32
;
3782 case GL_RGBA8UI_EXT
:
3783 return MESA_FORMAT_RGBA_UINT8
;
3784 case GL_RGBA16UI_EXT
:
3785 return MESA_FORMAT_RGBA_UINT16
;
3786 case GL_RGBA32UI_EXT
:
3787 return MESA_FORMAT_RGBA_UINT32
;
3790 return MESA_FORMAT_GR88
;
3792 return MESA_FORMAT_RG1616
;
3794 return MESA_FORMAT_RG_FLOAT16
;
3796 return MESA_FORMAT_RG_FLOAT32
;
3798 return MESA_FORMAT_RG_INT8
;
3800 return MESA_FORMAT_RG_INT16
;
3802 return MESA_FORMAT_RG_INT32
;
3804 return MESA_FORMAT_RG_UINT8
;
3806 return MESA_FORMAT_RG_UINT16
;
3808 return MESA_FORMAT_RG_UINT32
;
3811 return MESA_FORMAT_R8
;
3813 return MESA_FORMAT_R16
;
3815 return MESA_FORMAT_R_FLOAT16
;
3817 return MESA_FORMAT_R_FLOAT32
;
3819 return MESA_FORMAT_R_INT8
;
3821 return MESA_FORMAT_R_INT16
;
3823 return MESA_FORMAT_R_INT32
;
3825 return MESA_FORMAT_R_UINT8
;
3827 return MESA_FORMAT_R_UINT16
;
3829 return MESA_FORMAT_R_UINT32
;
3832 return MESA_FORMAT_NONE
;
3837 validate_texbuffer_format(const struct gl_context
*ctx
, GLenum internalFormat
)
3839 gl_format format
= get_texbuffer_format(ctx
, internalFormat
);
3842 if (format
== MESA_FORMAT_NONE
)
3843 return MESA_FORMAT_NONE
;
3845 datatype
= _mesa_get_format_datatype(format
);
3846 if (datatype
== GL_FLOAT
&& !ctx
->Extensions
.ARB_texture_float
)
3847 return MESA_FORMAT_NONE
;
3849 if (datatype
== GL_HALF_FLOAT
&& !ctx
->Extensions
.ARB_half_float_pixel
)
3850 return MESA_FORMAT_NONE
;
3852 /* The GL_ARB_texture_rg and GL_ARB_texture_buffer_object specs don't make
3853 * any mention of R/RG formats, but they appear in the GL 3.1 core
3856 if (ctx
->Version
<= 30) {
3857 GLenum base_format
= _mesa_get_format_base_format(format
);
3859 if (base_format
== GL_R
|| base_format
== GL_RG
)
3860 return MESA_FORMAT_NONE
;
3866 /** GL_ARB_texture_buffer_object */
3868 _mesa_TexBuffer(GLenum target
, GLenum internalFormat
, GLuint buffer
)
3870 struct gl_texture_object
*texObj
;
3871 struct gl_buffer_object
*bufObj
;
3874 GET_CURRENT_CONTEXT(ctx
);
3875 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3877 if (!(ctx
->Extensions
.ARB_texture_buffer_object
3878 && _mesa_is_desktop_gl(ctx
))) {
3879 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBuffer");
3883 if (target
!= GL_TEXTURE_BUFFER_ARB
) {
3884 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexBuffer(target)");
3888 format
= validate_texbuffer_format(ctx
, internalFormat
);
3889 if (format
== MESA_FORMAT_NONE
) {
3890 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexBuffer(internalFormat 0x%x)",
3895 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3896 if (buffer
&& !bufObj
) {
3897 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBuffer(buffer %u)", buffer
);
3901 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3903 _mesa_lock_texture(ctx
, texObj
);
3905 _mesa_reference_buffer_object(ctx
, &texObj
->BufferObject
, bufObj
);
3906 texObj
->BufferObjectFormat
= internalFormat
;
3907 texObj
->_BufferObjectFormat
= format
;
3909 _mesa_unlock_texture(ctx
, texObj
);