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 (_mesa_is_gles3(ctx
)) {
533 switch (internalFormat
) {
534 case GL_COMPRESSED_RGB8_ETC2
:
535 case GL_COMPRESSED_SRGB8_ETC2
:
537 case GL_COMPRESSED_RGBA8_ETC2_EAC
:
538 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC
:
539 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2
:
540 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2
:
542 case GL_COMPRESSED_R11_EAC
:
543 case GL_COMPRESSED_SIGNED_R11_EAC
:
545 case GL_COMPRESSED_RG11_EAC
:
546 case GL_COMPRESSED_SIGNED_RG11_EAC
:
553 if (ctx
->API
== API_OPENGLES
) {
554 switch (internalFormat
) {
555 case GL_PALETTE4_RGB8_OES
:
556 case GL_PALETTE4_R5_G6_B5_OES
:
557 case GL_PALETTE8_RGB8_OES
:
558 case GL_PALETTE8_R5_G6_B5_OES
:
560 case GL_PALETTE4_RGBA8_OES
:
561 case GL_PALETTE8_RGB5_A1_OES
:
562 case GL_PALETTE4_RGBA4_OES
:
563 case GL_PALETTE4_RGB5_A1_OES
:
564 case GL_PALETTE8_RGBA8_OES
:
565 case GL_PALETTE8_RGBA4_OES
:
572 return -1; /* error */
577 * For cube map faces, return a face index in [0,5].
578 * For other targets return 0;
581 _mesa_tex_target_to_face(GLenum target
)
583 if (_mesa_is_cube_face(target
))
584 return (GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
;
592 * Install gl_texture_image in a gl_texture_object according to the target
593 * and level parameters.
595 * \param tObj texture object.
596 * \param target texture target.
597 * \param level image level.
598 * \param texImage texture image.
601 set_tex_image(struct gl_texture_object
*tObj
,
602 GLenum target
, GLint level
,
603 struct gl_texture_image
*texImage
)
605 const GLuint face
= _mesa_tex_target_to_face(target
);
609 if (target
== GL_TEXTURE_RECTANGLE_NV
|| target
== GL_TEXTURE_EXTERNAL_OES
)
612 tObj
->Image
[face
][level
] = texImage
;
614 /* Set the 'back' pointer */
615 texImage
->TexObject
= tObj
;
616 texImage
->Level
= level
;
617 texImage
->Face
= face
;
622 * Allocate a texture image structure.
624 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
627 * \return a pointer to gl_texture_image struct with all fields initialized to
630 struct gl_texture_image
*
631 _mesa_new_texture_image( struct gl_context
*ctx
)
634 return CALLOC_STRUCT(gl_texture_image
);
639 * Free a gl_texture_image and associated data.
640 * This function is a fallback called via ctx->Driver.DeleteTextureImage().
642 * \param texImage texture image.
644 * Free the texture image structure and the associated image data.
647 _mesa_delete_texture_image(struct gl_context
*ctx
,
648 struct gl_texture_image
*texImage
)
650 /* Free texImage->Data and/or any other driver-specific texture
653 ASSERT(ctx
->Driver
.FreeTextureImageBuffer
);
654 ctx
->Driver
.FreeTextureImageBuffer( ctx
, texImage
);
660 * Test if a target is a proxy target.
662 * \param target texture target.
664 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
667 _mesa_is_proxy_texture(GLenum target
)
670 * NUM_TEXTURE_TARGETS should match number of terms below, except there's no
671 * proxy for GL_TEXTURE_BUFFER and GL_TEXTURE_EXTERNAL_OES.
673 assert(NUM_TEXTURE_TARGETS
== 8 + 2);
675 return (target
== GL_PROXY_TEXTURE_1D
||
676 target
== GL_PROXY_TEXTURE_2D
||
677 target
== GL_PROXY_TEXTURE_3D
||
678 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
679 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
||
680 target
== GL_PROXY_TEXTURE_1D_ARRAY_EXT
||
681 target
== GL_PROXY_TEXTURE_2D_ARRAY_EXT
||
682 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
);
687 * Return the proxy target which corresponds to the given texture target
690 _mesa_get_proxy_target(GLenum target
)
694 case GL_PROXY_TEXTURE_1D
:
695 return GL_PROXY_TEXTURE_1D
;
697 case GL_PROXY_TEXTURE_2D
:
698 return GL_PROXY_TEXTURE_2D
;
700 case GL_PROXY_TEXTURE_3D
:
701 return GL_PROXY_TEXTURE_3D
;
702 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
703 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
704 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
705 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
706 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
707 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
708 case GL_TEXTURE_CUBE_MAP_ARB
:
709 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
710 return GL_PROXY_TEXTURE_CUBE_MAP_ARB
;
711 case GL_TEXTURE_RECTANGLE_NV
:
712 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
713 return GL_PROXY_TEXTURE_RECTANGLE_NV
;
714 case GL_TEXTURE_1D_ARRAY_EXT
:
715 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
716 return GL_PROXY_TEXTURE_1D_ARRAY_EXT
;
717 case GL_TEXTURE_2D_ARRAY_EXT
:
718 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
719 return GL_PROXY_TEXTURE_2D_ARRAY_EXT
;
720 case GL_TEXTURE_CUBE_MAP_ARRAY
:
721 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
722 return GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
;
724 _mesa_problem(NULL
, "unexpected target in _mesa_get_proxy_target()");
731 * Get the texture object that corresponds to the target of the given
732 * texture unit. The target should have already been checked for validity.
734 * \param ctx GL context.
735 * \param texUnit texture unit.
736 * \param target texture target.
738 * \return pointer to the texture object on success, or NULL on failure.
740 struct gl_texture_object
*
741 _mesa_select_tex_object(struct gl_context
*ctx
,
742 const struct gl_texture_unit
*texUnit
,
745 const GLboolean arrayTex
= (ctx
->Extensions
.MESA_texture_array
||
746 ctx
->Extensions
.EXT_texture_array
);
750 return texUnit
->CurrentTex
[TEXTURE_1D_INDEX
];
751 case GL_PROXY_TEXTURE_1D
:
752 return ctx
->Texture
.ProxyTex
[TEXTURE_1D_INDEX
];
754 return texUnit
->CurrentTex
[TEXTURE_2D_INDEX
];
755 case GL_PROXY_TEXTURE_2D
:
756 return ctx
->Texture
.ProxyTex
[TEXTURE_2D_INDEX
];
758 return texUnit
->CurrentTex
[TEXTURE_3D_INDEX
];
759 case GL_PROXY_TEXTURE_3D
:
760 return ctx
->Texture
.ProxyTex
[TEXTURE_3D_INDEX
];
761 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
762 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
763 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
764 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
765 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
766 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
767 case GL_TEXTURE_CUBE_MAP_ARB
:
768 return ctx
->Extensions
.ARB_texture_cube_map
769 ? texUnit
->CurrentTex
[TEXTURE_CUBE_INDEX
] : NULL
;
770 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
771 return ctx
->Extensions
.ARB_texture_cube_map
772 ? ctx
->Texture
.ProxyTex
[TEXTURE_CUBE_INDEX
] : NULL
;
773 case GL_TEXTURE_CUBE_MAP_ARRAY
:
774 return ctx
->Extensions
.ARB_texture_cube_map_array
775 ? texUnit
->CurrentTex
[TEXTURE_CUBE_ARRAY_INDEX
] : NULL
;
776 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
777 return ctx
->Extensions
.ARB_texture_cube_map_array
778 ? ctx
->Texture
.ProxyTex
[TEXTURE_CUBE_ARRAY_INDEX
] : NULL
;
779 case GL_TEXTURE_RECTANGLE_NV
:
780 return ctx
->Extensions
.NV_texture_rectangle
781 ? texUnit
->CurrentTex
[TEXTURE_RECT_INDEX
] : NULL
;
782 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
783 return ctx
->Extensions
.NV_texture_rectangle
784 ? ctx
->Texture
.ProxyTex
[TEXTURE_RECT_INDEX
] : NULL
;
785 case GL_TEXTURE_1D_ARRAY_EXT
:
786 return arrayTex
? texUnit
->CurrentTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
787 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
788 return arrayTex
? ctx
->Texture
.ProxyTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
789 case GL_TEXTURE_2D_ARRAY_EXT
:
790 return arrayTex
? texUnit
->CurrentTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
791 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
792 return arrayTex
? ctx
->Texture
.ProxyTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
793 case GL_TEXTURE_BUFFER
:
794 return ctx
->API
== API_OPENGL_CORE
&&
795 ctx
->Extensions
.ARB_texture_buffer_object
?
796 texUnit
->CurrentTex
[TEXTURE_BUFFER_INDEX
] : NULL
;
797 case GL_TEXTURE_EXTERNAL_OES
:
798 return ctx
->Extensions
.OES_EGL_image_external
799 ? texUnit
->CurrentTex
[TEXTURE_EXTERNAL_INDEX
] : NULL
;
801 _mesa_problem(NULL
, "bad target in _mesa_select_tex_object()");
808 * Return pointer to texture object for given target on current texture unit.
810 struct gl_texture_object
*
811 _mesa_get_current_tex_object(struct gl_context
*ctx
, GLenum target
)
813 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
814 return _mesa_select_tex_object(ctx
, texUnit
, target
);
819 * Get a texture image pointer from a texture object, given a texture
820 * target and mipmap level. The target and level parameters should
821 * have already been error-checked.
823 * \param ctx GL context.
824 * \param texObj texture unit.
825 * \param target texture target.
826 * \param level image level.
828 * \return pointer to the texture image structure, or NULL on failure.
830 struct gl_texture_image
*
831 _mesa_select_tex_image(struct gl_context
*ctx
,
832 const struct gl_texture_object
*texObj
,
833 GLenum target
, GLint level
)
835 const GLuint face
= _mesa_tex_target_to_face(target
);
839 ASSERT(level
< MAX_TEXTURE_LEVELS
);
841 return texObj
->Image
[face
][level
];
846 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
847 * it and install it. Only return NULL if passed a bad parameter or run
850 struct gl_texture_image
*
851 _mesa_get_tex_image(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
852 GLenum target
, GLint level
)
854 struct gl_texture_image
*texImage
;
859 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
861 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
863 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture image allocation");
867 set_tex_image(texObj
, target
, level
, texImage
);
875 * Return pointer to the specified proxy texture image.
876 * Note that proxy textures are per-context, not per-texture unit.
877 * \return pointer to texture image or NULL if invalid target, invalid
878 * level, or out of memory.
880 static struct gl_texture_image
*
881 get_proxy_tex_image(struct gl_context
*ctx
, GLenum target
, GLint level
)
883 struct gl_texture_image
*texImage
;
890 case GL_PROXY_TEXTURE_1D
:
891 if (level
>= ctx
->Const
.MaxTextureLevels
)
893 texIndex
= TEXTURE_1D_INDEX
;
895 case GL_PROXY_TEXTURE_2D
:
896 if (level
>= ctx
->Const
.MaxTextureLevels
)
898 texIndex
= TEXTURE_2D_INDEX
;
900 case GL_PROXY_TEXTURE_3D
:
901 if (level
>= ctx
->Const
.Max3DTextureLevels
)
903 texIndex
= TEXTURE_3D_INDEX
;
905 case GL_PROXY_TEXTURE_CUBE_MAP
:
906 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
908 texIndex
= TEXTURE_CUBE_INDEX
;
910 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
913 texIndex
= TEXTURE_RECT_INDEX
;
915 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
916 if (level
>= ctx
->Const
.MaxTextureLevels
)
918 texIndex
= TEXTURE_1D_ARRAY_INDEX
;
920 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
921 if (level
>= ctx
->Const
.MaxTextureLevels
)
923 texIndex
= TEXTURE_2D_ARRAY_INDEX
;
925 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
926 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
928 texIndex
= TEXTURE_CUBE_ARRAY_INDEX
;
934 texImage
= ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
];
936 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
938 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
941 ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
] = texImage
;
942 /* Set the 'back' pointer */
943 texImage
->TexObject
= ctx
->Texture
.ProxyTex
[texIndex
];
950 * Get the maximum number of allowed mipmap levels.
952 * \param ctx GL context.
953 * \param target texture target.
955 * \return the maximum number of allowed mipmap levels for the given
956 * texture target, or zero if passed a bad target.
961 _mesa_max_texture_levels(struct gl_context
*ctx
, GLenum target
)
965 case GL_PROXY_TEXTURE_1D
:
967 case GL_PROXY_TEXTURE_2D
:
968 return ctx
->Const
.MaxTextureLevels
;
970 case GL_PROXY_TEXTURE_3D
:
971 return ctx
->Const
.Max3DTextureLevels
;
972 case GL_TEXTURE_CUBE_MAP
:
973 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
974 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
975 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
976 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
977 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
978 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
979 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
980 return ctx
->Extensions
.ARB_texture_cube_map
981 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
982 case GL_TEXTURE_RECTANGLE_NV
:
983 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
984 return ctx
->Extensions
.NV_texture_rectangle
? 1 : 0;
985 case GL_TEXTURE_1D_ARRAY_EXT
:
986 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
987 case GL_TEXTURE_2D_ARRAY_EXT
:
988 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
989 return (ctx
->Extensions
.MESA_texture_array
||
990 ctx
->Extensions
.EXT_texture_array
)
991 ? ctx
->Const
.MaxTextureLevels
: 0;
992 case GL_TEXTURE_CUBE_MAP_ARRAY
:
993 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
994 return ctx
->Extensions
.ARB_texture_cube_map_array
995 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
996 case GL_TEXTURE_BUFFER
:
997 return ctx
->API
== API_OPENGL_CORE
&&
998 ctx
->Extensions
.ARB_texture_buffer_object
? 1 : 0;
999 case GL_TEXTURE_EXTERNAL_OES
:
1002 return 0; /* bad target */
1008 * Return number of dimensions per mipmap level for the given texture target.
1011 _mesa_get_texture_dimensions(GLenum target
)
1015 case GL_PROXY_TEXTURE_1D
:
1018 case GL_TEXTURE_RECTANGLE
:
1019 case GL_TEXTURE_CUBE_MAP
:
1020 case GL_PROXY_TEXTURE_2D
:
1021 case GL_PROXY_TEXTURE_RECTANGLE
:
1022 case GL_PROXY_TEXTURE_CUBE_MAP
:
1023 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1024 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1025 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1026 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1027 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1028 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1029 case GL_TEXTURE_1D_ARRAY
:
1030 case GL_PROXY_TEXTURE_1D_ARRAY
:
1031 case GL_TEXTURE_EXTERNAL_OES
:
1034 case GL_PROXY_TEXTURE_3D
:
1035 case GL_TEXTURE_2D_ARRAY
:
1036 case GL_PROXY_TEXTURE_2D_ARRAY
:
1037 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1038 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1040 case GL_TEXTURE_BUFFER
:
1043 _mesa_problem(NULL
, "invalid target 0x%x in get_texture_dimensions()",
1051 * Return the maximum number of mipmap levels for the given target
1052 * and the dimensions.
1053 * The dimensions are expected not to include the border.
1056 _mesa_get_tex_max_num_levels(GLenum target
, GLsizei width
, GLsizei height
,
1063 case GL_TEXTURE_1D_ARRAY
:
1066 case GL_TEXTURE_CUBE_MAP
:
1067 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1068 ASSERT(width
== height
);
1072 case GL_TEXTURE_2D_ARRAY
:
1073 size
= MAX2(width
, height
);
1076 size
= MAX3(width
, height
, depth
);
1078 case GL_TEXTURE_RECTANGLE
:
1079 case GL_TEXTURE_EXTERNAL_OES
:
1086 return _mesa_logbase2(size
) + 1;
1090 #if 000 /* not used anymore */
1092 * glTexImage[123]D can accept a NULL image pointer. In this case we
1093 * create a texture image with unspecified image contents per the OpenGL
1097 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
1099 const GLint components
= _mesa_components_in_format(format
);
1100 const GLint numPixels
= width
* height
* depth
;
1101 GLubyte
*data
= (GLubyte
*) malloc(numPixels
* components
* sizeof(GLubyte
));
1105 * Let's see if anyone finds this. If glTexImage2D() is called with
1106 * a NULL image pointer then load the texture image with something
1107 * interesting instead of leaving it indeterminate.
1110 static const char message
[8][32] = {
1111 " X X XXXXX XXX X ",
1112 " XX XX X X X X X ",
1114 " X X XXXX XXX XXXXX ",
1117 " X X XXXXX XXX X X ",
1121 GLubyte
*imgPtr
= data
;
1123 for (h
= 0; h
< depth
; h
++) {
1124 for (i
= 0; i
< height
; i
++) {
1125 GLint srcRow
= 7 - (i
% 8);
1126 for (j
= 0; j
< width
; j
++) {
1127 GLint srcCol
= j
% 32;
1128 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
1129 for (k
= 0; k
< components
; k
++) {
1145 * Set the size and format-related fields of a gl_texture_image struct
1146 * to zero. This is used when a proxy texture test fails.
1149 clear_teximage_fields(struct gl_texture_image
*img
)
1152 img
->_BaseFormat
= 0;
1153 img
->InternalFormat
= 0;
1162 img
->HeightLog2
= 0;
1164 img
->TexFormat
= MESA_FORMAT_NONE
;
1169 * Initialize basic fields of the gl_texture_image struct.
1171 * \param ctx GL context.
1172 * \param img texture image structure to be initialized.
1173 * \param width image width.
1174 * \param height image height.
1175 * \param depth image depth.
1176 * \param border image border.
1177 * \param internalFormat internal format.
1178 * \param format the actual hardware format (one of MESA_FORMAT_*)
1180 * Fills in the fields of \p img with the given information.
1181 * Note: width, height and depth include the border.
1184 _mesa_init_teximage_fields(struct gl_context
*ctx
,
1185 struct gl_texture_image
*img
,
1186 GLsizei width
, GLsizei height
, GLsizei depth
,
1187 GLint border
, GLenum internalFormat
,
1193 ASSERT(height
>= 0);
1196 target
= img
->TexObject
->Target
;
1197 img
->_BaseFormat
= _mesa_base_tex_format( ctx
, internalFormat
);
1198 ASSERT(img
->_BaseFormat
> 0);
1199 img
->InternalFormat
= internalFormat
;
1200 img
->Border
= border
;
1202 img
->Height
= height
;
1205 img
->Width2
= width
- 2 * border
; /* == 1 << img->WidthLog2; */
1206 img
->WidthLog2
= _mesa_logbase2(img
->Width2
);
1210 case GL_TEXTURE_BUFFER
:
1211 case GL_PROXY_TEXTURE_1D
:
1216 img
->HeightLog2
= 0;
1223 case GL_TEXTURE_1D_ARRAY
:
1224 case GL_PROXY_TEXTURE_1D_ARRAY
:
1225 img
->Height2
= height
; /* no border */
1226 img
->HeightLog2
= 0; /* not used */
1234 case GL_TEXTURE_RECTANGLE
:
1235 case GL_TEXTURE_CUBE_MAP
:
1236 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1237 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1238 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1239 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1240 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1241 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1242 case GL_TEXTURE_EXTERNAL_OES
:
1243 case GL_PROXY_TEXTURE_2D
:
1244 case GL_PROXY_TEXTURE_RECTANGLE
:
1245 case GL_PROXY_TEXTURE_CUBE_MAP
:
1246 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1247 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1254 case GL_TEXTURE_2D_ARRAY
:
1255 case GL_PROXY_TEXTURE_2D_ARRAY
:
1256 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1257 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1258 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1259 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1260 img
->Depth2
= depth
; /* no border */
1261 img
->DepthLog2
= 0; /* not used */
1264 case GL_PROXY_TEXTURE_3D
:
1265 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1266 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1267 img
->Depth2
= depth
- 2 * border
; /* == 1 << img->DepthLog2; */
1268 img
->DepthLog2
= _mesa_logbase2(img
->Depth2
);
1271 _mesa_problem(NULL
, "invalid target 0x%x in _mesa_init_teximage_fields()",
1276 _mesa_get_tex_max_num_levels(target
,
1277 img
->Width2
, img
->Height2
, img
->Depth2
);
1278 img
->TexFormat
= format
;
1283 * Free and clear fields of the gl_texture_image struct.
1285 * \param ctx GL context.
1286 * \param texImage texture image structure to be cleared.
1288 * After the call, \p texImage will have no data associated with it. Its
1289 * fields are cleared so that its parent object will test incomplete.
1292 _mesa_clear_texture_image(struct gl_context
*ctx
,
1293 struct gl_texture_image
*texImage
)
1295 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
1296 clear_teximage_fields(texImage
);
1301 * Check the width, height, depth and border of a texture image are legal.
1302 * Used by all the glTexImage, glCompressedTexImage and glCopyTexImage
1304 * The target and level parameters will have already been validated.
1305 * \return GL_TRUE if size is OK, GL_FALSE otherwise.
1308 _mesa_legal_texture_dimensions(struct gl_context
*ctx
, GLenum target
,
1309 GLint level
, GLint width
, GLint height
,
1310 GLint depth
, GLint border
)
1316 case GL_PROXY_TEXTURE_1D
:
1317 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1); /* level zero size */
1318 maxSize
>>= level
; /* level size */
1319 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1321 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1322 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1328 case GL_PROXY_TEXTURE_2D
:
1329 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1331 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1333 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1335 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1336 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1338 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1344 case GL_PROXY_TEXTURE_3D
:
1345 maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1347 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1349 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1351 if (depth
< 2 * border
|| depth
> 2 * border
+ maxSize
)
1353 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1354 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1356 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1358 if (depth
> 0 && !_mesa_is_pow_two(depth
- 2 * border
))
1363 case GL_TEXTURE_RECTANGLE_NV
:
1364 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1367 maxSize
= ctx
->Const
.MaxTextureRectSize
;
1368 if (width
< 0 || width
> maxSize
)
1370 if (height
< 0 || height
> maxSize
)
1374 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1375 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1376 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1377 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1378 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1379 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1380 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
1381 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1383 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1385 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1387 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1388 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1390 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1395 case GL_TEXTURE_1D_ARRAY_EXT
:
1396 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1397 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1399 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1401 if (height
< 1 || height
> ctx
->Const
.MaxArrayTextureLayers
)
1403 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1404 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1409 case GL_TEXTURE_2D_ARRAY_EXT
:
1410 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1411 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1413 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1415 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1417 if (depth
< 1 || depth
> ctx
->Const
.MaxArrayTextureLayers
)
1419 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1420 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1422 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1427 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1428 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1429 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1430 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1432 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1434 if (depth
< 1 || depth
> ctx
->Const
.MaxArrayTextureLayers
)
1436 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
1438 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1439 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1441 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1446 _mesa_problem(ctx
, "Invalid target in _mesa_legal_texture_dimensions()");
1453 * Do error checking of xoffset, yoffset, zoffset, width, height and depth
1454 * for glTexSubImage, glCopyTexSubImage and glCompressedTexSubImage.
1455 * \param destImage the destination texture image.
1456 * \return GL_TRUE if error found, GL_FALSE otherwise.
1459 error_check_subtexture_dimensions(struct gl_context
*ctx
,
1460 const char *function
, GLuint dims
,
1461 const struct gl_texture_image
*destImage
,
1462 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1463 GLsizei subWidth
, GLsizei subHeight
,
1466 const GLenum target
= destImage
->TexObject
->Target
;
1471 _mesa_error(ctx
, GL_INVALID_VALUE
,
1472 "%s%dD(width=%d)", function
, dims
, subWidth
);
1476 if (dims
> 1 && subHeight
< 0) {
1477 _mesa_error(ctx
, GL_INVALID_VALUE
,
1478 "%s%dD(height=%d)", function
, dims
, subHeight
);
1482 if (dims
> 2 && subDepth
< 0) {
1483 _mesa_error(ctx
, GL_INVALID_VALUE
,
1484 "%s%dD(depth=%d)", function
, dims
, subDepth
);
1488 /* check xoffset and width */
1489 if (xoffset
< -destImage
->Border
) {
1490 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(xoffset)",
1495 if (xoffset
+ subWidth
> destImage
->Width
) {
1496 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(xoffset+width)",
1501 /* check yoffset and height */
1503 GLint yBorder
= (target
== GL_TEXTURE_1D_ARRAY
) ? 0 : destImage
->Border
;
1504 if (yoffset
< -yBorder
) {
1505 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(yoffset)",
1509 if (yoffset
+ subHeight
> destImage
->Height
) {
1510 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(yoffset+height)",
1516 /* check zoffset and depth */
1518 GLint zBorder
= (target
== GL_TEXTURE_2D_ARRAY
) ? 0 : destImage
->Border
;
1519 if (zoffset
< -zBorder
) {
1520 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s3D(zoffset)", function
);
1523 if (zoffset
+ subDepth
> (GLint
) destImage
->Depth
) {
1524 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s3D(zoffset+depth)", function
);
1530 * The OpenGL spec (and GL_ARB_texture_compression) says only whole
1531 * compressed texture images can be updated. But, that restriction may be
1532 * relaxed for particular compressed formats. At this time, all the
1533 * compressed formats supported by Mesa allow sub-textures to be updated
1534 * along compressed block boundaries.
1536 _mesa_get_format_block_size(destImage
->TexFormat
, &bw
, &bh
);
1538 if (bw
!= 1 || bh
!= 1) {
1539 /* offset must be multiple of block size */
1540 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0)) {
1541 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1542 "%s%dD(xoffset = %d, yoffset = %d)",
1543 function
, dims
, xoffset
, yoffset
);
1547 /* size must be multiple of bw by bh or equal to whole texture size */
1548 if ((subWidth
% bw
!= 0) && subWidth
!= destImage
->Width
) {
1549 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1550 "%s%dD(width = %d)", function
, dims
, subWidth
);
1554 if ((subHeight
% bh
!= 0) && subHeight
!= destImage
->Height
) {
1555 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1556 "%s%dD(height = %d)", function
, dims
, subHeight
);
1568 * This is the fallback for Driver.TestProxyTexImage() for doing device-
1569 * specific texture image size checks.
1571 * A hardware driver might override this function if, for example, the
1572 * max 3D texture size is 512x512x64 (i.e. not a cube).
1574 * Note that width, height, depth == 0 is not an error. However, a
1575 * texture with zero width/height/depth will be considered "incomplete"
1576 * and texturing will effectively be disabled.
1578 * \param target any texture target/type
1579 * \param level as passed to glTexImage
1580 * \param format the MESA_FORMAT_x for the tex image
1581 * \param width as passed to glTexImage
1582 * \param height as passed to glTexImage
1583 * \param depth as passed to glTexImage
1584 * \param border as passed to glTexImage
1585 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1588 _mesa_test_proxy_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1590 GLint width
, GLint height
, GLint depth
, GLint border
)
1592 /* We just check if the image size is less than MaxTextureMbytes.
1593 * Some drivers may do more specific checks.
1595 uint64_t bytes
= _mesa_format_image_size64(format
, width
, height
, depth
);
1596 uint64_t mbytes
= bytes
/ (1024 * 1024); /* convert to MB */
1597 mbytes
*= _mesa_num_tex_faces(target
);
1598 return mbytes
<= (uint64_t) ctx
->Const
.MaxTextureMbytes
;
1603 * Return true if the format is only valid for glCompressedTexImage.
1606 compressedteximage_only_format(const struct gl_context
*ctx
, GLenum format
)
1609 case GL_ETC1_RGB8_OES
:
1610 case GL_PALETTE4_RGB8_OES
:
1611 case GL_PALETTE4_RGBA8_OES
:
1612 case GL_PALETTE4_R5_G6_B5_OES
:
1613 case GL_PALETTE4_RGBA4_OES
:
1614 case GL_PALETTE4_RGB5_A1_OES
:
1615 case GL_PALETTE8_RGB8_OES
:
1616 case GL_PALETTE8_RGBA8_OES
:
1617 case GL_PALETTE8_R5_G6_B5_OES
:
1618 case GL_PALETTE8_RGBA4_OES
:
1619 case GL_PALETTE8_RGB5_A1_OES
:
1628 * Helper function to determine whether a target and specific compression
1629 * format are supported.
1632 target_can_be_compressed(const struct gl_context
*ctx
, GLenum target
,
1635 (void) intFormat
; /* not used yet */
1639 case GL_PROXY_TEXTURE_2D
:
1640 return GL_TRUE
; /* true for any compressed format so far */
1641 case GL_PROXY_TEXTURE_CUBE_MAP
:
1642 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1643 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1644 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1645 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1646 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1647 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1648 return ctx
->Extensions
.ARB_texture_cube_map
;
1649 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1650 case GL_TEXTURE_2D_ARRAY_EXT
:
1651 return (ctx
->Extensions
.MESA_texture_array
||
1652 ctx
->Extensions
.EXT_texture_array
);
1653 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1654 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1655 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1663 * Check if the given texture target value is legal for a
1664 * glTexImage1/2/3D call.
1667 legal_teximage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
1673 case GL_PROXY_TEXTURE_1D
:
1674 return _mesa_is_desktop_gl(ctx
);
1682 case GL_PROXY_TEXTURE_2D
:
1683 return _mesa_is_desktop_gl(ctx
);
1684 case GL_PROXY_TEXTURE_CUBE_MAP
:
1685 return _mesa_is_desktop_gl(ctx
)
1686 && ctx
->Extensions
.ARB_texture_cube_map
;
1687 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1688 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1689 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1690 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1691 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1692 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1693 return ctx
->Extensions
.ARB_texture_cube_map
;
1694 case GL_TEXTURE_RECTANGLE_NV
:
1695 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1696 return _mesa_is_desktop_gl(ctx
)
1697 && ctx
->Extensions
.NV_texture_rectangle
;
1698 case GL_TEXTURE_1D_ARRAY_EXT
:
1699 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1700 return _mesa_is_desktop_gl(ctx
)
1701 && (ctx
->Extensions
.MESA_texture_array
||
1702 ctx
->Extensions
.EXT_texture_array
);
1710 case GL_PROXY_TEXTURE_3D
:
1711 return _mesa_is_desktop_gl(ctx
);
1712 case GL_TEXTURE_2D_ARRAY_EXT
:
1713 return (_mesa_is_desktop_gl(ctx
)
1714 && (ctx
->Extensions
.MESA_texture_array
||
1715 ctx
->Extensions
.EXT_texture_array
))
1716 || _mesa_is_gles3(ctx
);
1717 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1718 return _mesa_is_desktop_gl(ctx
)
1719 && (ctx
->Extensions
.MESA_texture_array
||
1720 ctx
->Extensions
.EXT_texture_array
);
1721 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1722 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1723 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1728 _mesa_problem(ctx
, "invalid dims=%u in legal_teximage_target()", dims
);
1735 * Check if the given texture target value is legal for a
1736 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1737 * The difference compared to legal_teximage_target() above is that
1738 * proxy targets are not supported.
1741 legal_texsubimage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
1745 return _mesa_is_desktop_gl(ctx
) && target
== GL_TEXTURE_1D
;
1750 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1751 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1752 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1753 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1754 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1755 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1756 return ctx
->Extensions
.ARB_texture_cube_map
;
1757 case GL_TEXTURE_RECTANGLE_NV
:
1758 return _mesa_is_desktop_gl(ctx
)
1759 && ctx
->Extensions
.NV_texture_rectangle
;
1760 case GL_TEXTURE_1D_ARRAY_EXT
:
1761 return _mesa_is_desktop_gl(ctx
)
1762 && (ctx
->Extensions
.MESA_texture_array
||
1763 ctx
->Extensions
.EXT_texture_array
);
1771 case GL_TEXTURE_2D_ARRAY_EXT
:
1772 return (_mesa_is_desktop_gl(ctx
)
1773 && (ctx
->Extensions
.MESA_texture_array
||
1774 ctx
->Extensions
.EXT_texture_array
))
1775 || _mesa_is_gles3(ctx
);
1776 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1777 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1778 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1783 _mesa_problem(ctx
, "invalid dims=%u in legal_texsubimage_target()",
1791 * Helper function to determine if a texture object is mutable (in terms
1792 * of GL_ARB_texture_storage).
1795 mutable_tex_object(struct gl_context
*ctx
, GLenum target
)
1797 if (ctx
->Extensions
.ARB_texture_storage
) {
1798 struct gl_texture_object
*texObj
=
1799 _mesa_get_current_tex_object(ctx
, target
);
1800 return !texObj
->Immutable
;
1807 * Return expected size of a compressed texture.
1810 compressed_tex_size(GLsizei width
, GLsizei height
, GLsizei depth
,
1813 gl_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
1814 return _mesa_format_image_size(mesaFormat
, width
, height
, depth
);
1819 * Test the glTexImage[123]D() parameters for errors.
1821 * \param ctx GL context.
1822 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1823 * \param target texture target given by the user (already validated).
1824 * \param level image level given by the user.
1825 * \param internalFormat internal format given by the user.
1826 * \param format pixel data format given by the user.
1827 * \param type pixel data type given by the user.
1828 * \param width image width given by the user.
1829 * \param height image height given by the user.
1830 * \param depth image depth given by the user.
1831 * \param border image border given by the user.
1833 * \return GL_TRUE if a error is found, GL_FALSE otherwise
1835 * Verifies each of the parameters against the constants specified in
1836 * __struct gl_contextRec::Const and the supported extensions, and according
1837 * to the OpenGL specification.
1838 * Note that we don't fully error-check the width, height, depth values
1839 * here. That's done in _mesa_legal_texture_dimensions() which is used
1840 * by several other GL entrypoints. Plus, texture dims have a special
1841 * interaction with proxy textures.
1844 texture_error_check( struct gl_context
*ctx
,
1845 GLuint dimensions
, GLenum target
,
1846 GLint level
, GLint internalFormat
,
1847 GLenum format
, GLenum type
,
1848 GLint width
, GLint height
,
1849 GLint depth
, GLint border
)
1851 GLboolean colorFormat
;
1854 /* Even though there are no color-index textures, we still have to support
1855 * uploading color-index data and remapping it to RGB via the
1856 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
1858 const GLboolean indexFormat
= (format
== GL_COLOR_INDEX
);
1860 /* Note: for proxy textures, some error conditions immediately generate
1861 * a GL error in the usual way. But others do not generate a GL error.
1862 * Instead, they cause the width, height, depth, format fields of the
1863 * texture image to be zeroed-out. The GL spec seems to indicate that the
1864 * zero-out behaviour is only used in cases related to memory allocation.
1868 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
1869 _mesa_error(ctx
, GL_INVALID_VALUE
,
1870 "glTexImage%dD(level=%d)", dimensions
, level
);
1875 if (border
< 0 || border
> 1 ||
1876 ((ctx
->API
!= API_OPENGL_COMPAT
||
1877 target
== GL_TEXTURE_RECTANGLE_NV
||
1878 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1879 _mesa_error(ctx
, GL_INVALID_VALUE
,
1880 "glTexImage%dD(border=%d)", dimensions
, border
);
1884 if (width
< 0 || height
< 0 || depth
< 0) {
1885 _mesa_error(ctx
, GL_INVALID_VALUE
,
1886 "glTexImage%dD(width, height or depth < 0)", dimensions
);
1890 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1891 * combinations of format, internalFormat, and type that can be used.
1892 * Formats and types that require additional extensions (e.g., GL_FLOAT
1893 * requires GL_OES_texture_float) are filtered elsewhere.
1896 if (_mesa_is_gles(ctx
)) {
1897 if (_mesa_is_gles3(ctx
)) {
1898 err
= _mesa_es3_error_check_format_and_type(format
, type
,
1901 if (format
!= internalFormat
) {
1902 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1903 "glTexImage%dD(format = %s, internalFormat = %s)",
1905 _mesa_lookup_enum_by_nr(format
),
1906 _mesa_lookup_enum_by_nr(internalFormat
));
1910 err
= _mesa_es_error_check_format_and_type(format
, type
, dimensions
);
1912 if (err
!= GL_NO_ERROR
) {
1913 _mesa_error(ctx
, err
,
1914 "glTexImage%dD(format = %s, type = %s, internalFormat = %s)",
1916 _mesa_lookup_enum_by_nr(format
),
1917 _mesa_lookup_enum_by_nr(type
),
1918 _mesa_lookup_enum_by_nr(internalFormat
));
1923 if ((target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
1924 _mesa_is_cube_face(target
)) && width
!= height
) {
1925 _mesa_error(ctx
, GL_INVALID_VALUE
,
1926 "glTexImage2D(cube width != height)");
1930 if ((target
== GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
||
1931 target
== GL_TEXTURE_CUBE_MAP_ARRAY
) && width
!= height
) {
1932 _mesa_error(ctx
, GL_INVALID_VALUE
,
1933 "glTexImage3D(cube array width != height)");
1937 if ((target
== GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
||
1938 target
== GL_TEXTURE_CUBE_MAP_ARRAY
) && (depth
% 6)) {
1939 _mesa_error(ctx
, GL_INVALID_VALUE
,
1940 "glTexImage3D(cube array depth not multiple of 6)");
1944 /* Check internalFormat */
1945 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1946 _mesa_error(ctx
, GL_INVALID_VALUE
,
1947 "glTexImage%dD(internalFormat=%s)",
1948 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
));
1952 /* Check incoming image format and type */
1953 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1954 if (err
!= GL_NO_ERROR
) {
1955 _mesa_error(ctx
, err
,
1956 "glTexImage%dD(incompatible format = %s, type = %s)",
1957 dimensions
, _mesa_lookup_enum_by_nr(format
),
1958 _mesa_lookup_enum_by_nr(type
));
1962 /* make sure internal format and format basically agree */
1963 colorFormat
= _mesa_is_color_format(format
);
1964 if ((_mesa_is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
) ||
1965 (_mesa_is_depth_format(internalFormat
) != _mesa_is_depth_format(format
)) ||
1966 (_mesa_is_ycbcr_format(internalFormat
) != _mesa_is_ycbcr_format(format
)) ||
1967 (_mesa_is_depthstencil_format(internalFormat
) != _mesa_is_depthstencil_format(format
)) ||
1968 (_mesa_is_dudv_format(internalFormat
) != _mesa_is_dudv_format(format
))) {
1969 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1970 "glTexImage%dD(incompatible internalFormat = %s, format = %s)",
1971 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
),
1972 _mesa_lookup_enum_by_nr(format
));
1976 /* additional checks for ycbcr textures */
1977 if (internalFormat
== GL_YCBCR_MESA
) {
1978 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
1979 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
1980 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
1982 _mesa_snprintf(message
, sizeof(message
),
1983 "glTexImage%dD(format/type YCBCR mismatch)",
1985 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", message
);
1986 return GL_TRUE
; /* error */
1988 if (target
!= GL_TEXTURE_2D
&&
1989 target
!= GL_PROXY_TEXTURE_2D
&&
1990 target
!= GL_TEXTURE_RECTANGLE_NV
&&
1991 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
1992 _mesa_error(ctx
, GL_INVALID_ENUM
,
1993 "glTexImage%dD(bad target for YCbCr texture)",
1999 _mesa_snprintf(message
, sizeof(message
),
2000 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
2001 dimensions
, border
);
2002 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", message
);
2007 /* additional checks for depth textures */
2008 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
2009 || _mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_STENCIL
) {
2010 /* Only 1D, 2D, rect, array and cube textures supported, not 3D
2011 * Cubemaps are only supported for GL version > 3.0 or with EXT_gpu_shader4 */
2012 if (target
!= GL_TEXTURE_1D
&&
2013 target
!= GL_PROXY_TEXTURE_1D
&&
2014 target
!= GL_TEXTURE_2D
&&
2015 target
!= GL_PROXY_TEXTURE_2D
&&
2016 target
!= GL_TEXTURE_1D_ARRAY
&&
2017 target
!= GL_PROXY_TEXTURE_1D_ARRAY
&&
2018 target
!= GL_TEXTURE_2D_ARRAY
&&
2019 target
!= GL_PROXY_TEXTURE_2D_ARRAY
&&
2020 target
!= GL_TEXTURE_RECTANGLE_ARB
&&
2021 target
!= GL_PROXY_TEXTURE_RECTANGLE_ARB
&&
2022 !((_mesa_is_cube_face(target
) || target
== GL_PROXY_TEXTURE_CUBE_MAP
) &&
2023 (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_gpu_shader4
2024 || (ctx
->API
== API_OPENGLES2
&& ctx
->Extensions
.OES_depth_texture_cube_map
))) &&
2025 !((target
== GL_TEXTURE_CUBE_MAP_ARRAY
||
2026 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
) &&
2027 ctx
->Extensions
.ARB_texture_cube_map_array
)) {
2028 _mesa_error(ctx
, GL_INVALID_ENUM
,
2029 "glTexImage%dD(bad target for depth texture)",
2035 /* additional checks for compressed textures */
2036 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
2037 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
2038 _mesa_error(ctx
, GL_INVALID_ENUM
,
2039 "glTexImage%dD(target can't be compressed)", dimensions
);
2042 if (compressedteximage_only_format(ctx
, internalFormat
)) {
2043 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2044 "glTexImage%dD(no compression for format)", dimensions
);
2048 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2049 "glTexImage%dD(border!=0)", dimensions
);
2054 /* additional checks for integer textures */
2055 if ((ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) &&
2056 (_mesa_is_enum_format_integer(format
) !=
2057 _mesa_is_enum_format_integer(internalFormat
))) {
2058 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2059 "glTexImage%dD(integer/non-integer format mismatch)",
2064 if (!mutable_tex_object(ctx
, target
)) {
2065 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2066 "glTexImage%dD(immutable texture)", dimensions
);
2070 /* if we get here, the parameters are OK */
2076 * Error checking for glCompressedTexImage[123]D().
2077 * Note that the width, height and depth values are not fully error checked
2079 * \return GL_TRUE if a error is found, GL_FALSE otherwise
2082 compressed_texture_error_check(struct gl_context
*ctx
, GLint dimensions
,
2083 GLenum target
, GLint level
,
2084 GLenum internalFormat
, GLsizei width
,
2085 GLsizei height
, GLsizei depth
, GLint border
,
2088 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
2090 GLenum error
= GL_NO_ERROR
;
2091 char *reason
= ""; /* no error */
2093 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
2095 error
= GL_INVALID_ENUM
;
2099 /* This will detect any invalid internalFormat value */
2100 if (!_mesa_is_compressed_format(ctx
, internalFormat
)) {
2101 reason
= "internalFormat";
2102 error
= GL_INVALID_ENUM
;
2106 switch (internalFormat
) {
2107 case GL_PALETTE4_RGB8_OES
:
2108 case GL_PALETTE4_RGBA8_OES
:
2109 case GL_PALETTE4_R5_G6_B5_OES
:
2110 case GL_PALETTE4_RGBA4_OES
:
2111 case GL_PALETTE4_RGB5_A1_OES
:
2112 case GL_PALETTE8_RGB8_OES
:
2113 case GL_PALETTE8_RGBA8_OES
:
2114 case GL_PALETTE8_R5_G6_B5_OES
:
2115 case GL_PALETTE8_RGBA4_OES
:
2116 case GL_PALETTE8_RGB5_A1_OES
:
2117 /* check level (note that level should be zero or less!) */
2118 if (level
> 0 || level
< -maxLevels
) {
2120 error
= GL_INVALID_VALUE
;
2124 if (dimensions
!= 2) {
2125 reason
= "compressed paletted textures must be 2D";
2126 error
= GL_INVALID_OPERATION
;
2130 /* Figure out the expected texture size (in bytes). This will be
2131 * checked against the actual size later.
2133 expectedSize
= _mesa_cpal_compressed_size(level
, internalFormat
,
2136 /* This is for the benefit of the TestProxyTexImage below. It expects
2137 * level to be non-negative. OES_compressed_paletted_texture uses a
2138 * weird mechanism where the level specified to glCompressedTexImage2D
2139 * is -(n-1) number of levels in the texture, and the data specifies the
2140 * complete mipmap stack. This is done to ensure the palette is the
2141 * same for all levels.
2148 if (level
< 0 || level
>= maxLevels
) {
2150 error
= GL_INVALID_VALUE
;
2154 /* Figure out the expected texture size (in bytes). This will be
2155 * checked against the actual size later.
2157 expectedSize
= compressed_tex_size(width
, height
, depth
, internalFormat
);
2161 /* This should really never fail */
2162 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
2163 reason
= "internalFormat";
2164 error
= GL_INVALID_ENUM
;
2168 /* No compressed formats support borders at this time */
2170 reason
= "border != 0";
2171 error
= GL_INVALID_VALUE
;
2175 /* For cube map, width must equal height */
2176 if ((target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
2177 _mesa_is_cube_face(target
)) && width
!= height
) {
2178 reason
= "width != height";
2179 error
= GL_INVALID_VALUE
;
2183 /* check image size in bytes */
2184 if (expectedSize
!= imageSize
) {
2185 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
2186 * if <imageSize> is not consistent with the format, dimensions, and
2187 * contents of the specified image.
2189 reason
= "imageSize inconsistant with width/height/format";
2190 error
= GL_INVALID_VALUE
;
2194 if (!mutable_tex_object(ctx
, target
)) {
2195 reason
= "immutable texture";
2196 error
= GL_INVALID_OPERATION
;
2203 _mesa_error(ctx
, error
, "glCompressedTexImage%dD(%s)", dimensions
, reason
);
2210 * Test glTexSubImage[123]D() parameters for errors.
2212 * \param ctx GL context.
2213 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2214 * \param target texture target given by the user (already validated)
2215 * \param level image level given by the user.
2216 * \param xoffset sub-image x offset given by the user.
2217 * \param yoffset sub-image y offset given by the user.
2218 * \param zoffset sub-image z offset given by the user.
2219 * \param format pixel data format given by the user.
2220 * \param type pixel data type given by the user.
2221 * \param width image width given by the user.
2222 * \param height image height given by the user.
2223 * \param depth image depth given by the user.
2225 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2227 * Verifies each of the parameters against the constants specified in
2228 * __struct gl_contextRec::Const and the supported extensions, and according
2229 * to the OpenGL specification.
2232 texsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2233 GLenum target
, GLint level
,
2234 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2235 GLint width
, GLint height
, GLint depth
,
2236 GLenum format
, GLenum type
)
2238 struct gl_texture_object
*texObj
;
2239 struct gl_texture_image
*texImage
;
2242 /* check target (proxies not allowed) */
2243 if (!legal_texsubimage_target(ctx
, dimensions
, target
)) {
2244 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage%uD(target=%s)",
2245 dimensions
, _mesa_lookup_enum_by_nr(target
));
2250 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2251 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage%uD(level=%d)",
2256 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2257 * combinations of format and type that can be used. Formats and types
2258 * that require additional extensions (e.g., GL_FLOAT requires
2259 * GL_OES_texture_float) are filtered elsewhere.
2261 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
2262 err
= _mesa_es_error_check_format_and_type(format
, type
, dimensions
);
2263 if (err
!= GL_NO_ERROR
) {
2264 _mesa_error(ctx
, err
,
2265 "glTexSubImage%dD(format = %s, type = %s)",
2267 _mesa_lookup_enum_by_nr(format
),
2268 _mesa_lookup_enum_by_nr(type
));
2273 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
2274 if (err
!= GL_NO_ERROR
) {
2275 _mesa_error(ctx
, err
,
2276 "glTexSubImage%dD(incompatible format = %s, type = %s)",
2277 dimensions
, _mesa_lookup_enum_by_nr(format
),
2278 _mesa_lookup_enum_by_nr(type
));
2282 /* Get dest texture object / image pointers */
2283 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2285 /* must be out of memory */
2286 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage%dD()", dimensions
);
2290 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2292 /* non-existant texture level */
2293 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2294 "glTexSubImage%dD(invalid texture image)", dimensions
);
2298 if (error_check_subtexture_dimensions(ctx
, "glTexSubImage", dimensions
,
2299 texImage
, xoffset
, yoffset
, 0,
2300 width
, height
, 1)) {
2304 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2305 if (compressedteximage_only_format(ctx
, texImage
->InternalFormat
)) {
2306 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2307 "glTexSubImage%dD(no compression for format)", dimensions
);
2312 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
2313 /* both source and dest must be integer-valued, or neither */
2314 if (_mesa_is_format_integer_color(texImage
->TexFormat
) !=
2315 _mesa_is_enum_format_integer(format
)) {
2316 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2317 "glTexSubImage%dD(integer/non-integer format mismatch)",
2328 * Test glCopyTexImage[12]D() parameters for errors.
2330 * \param ctx GL context.
2331 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2332 * \param target texture target given by the user.
2333 * \param level image level given by the user.
2334 * \param internalFormat internal format given by the user.
2335 * \param width image width given by the user.
2336 * \param height image height given by the user.
2337 * \param border texture border.
2339 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2341 * Verifies each of the parameters against the constants specified in
2342 * __struct gl_contextRec::Const and the supported extensions, and according
2343 * to the OpenGL specification.
2346 copytexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
2347 GLenum target
, GLint level
, GLint internalFormat
,
2348 GLint width
, GLint height
, GLint border
)
2351 GLint rb_base_format
;
2352 struct gl_renderbuffer
*rb
;
2353 GLenum rb_internal_format
;
2356 if (!legal_texsubimage_target(ctx
, dimensions
, target
)) {
2357 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexImage%uD(target=%s)",
2358 dimensions
, _mesa_lookup_enum_by_nr(target
));
2363 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2364 _mesa_error(ctx
, GL_INVALID_VALUE
,
2365 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
2369 /* Check that the source buffer is complete */
2370 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2371 if (ctx
->ReadBuffer
->_Status
== 0) {
2372 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2374 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2375 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2376 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
2380 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2381 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2382 "glCopyTexImage%dD(multisample FBO)",
2389 if (border
< 0 || border
> 1 ||
2390 ((ctx
->API
!= API_OPENGL_COMPAT
||
2391 target
== GL_TEXTURE_RECTANGLE_NV
||
2392 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
2393 _mesa_error(ctx
, GL_INVALID_VALUE
,
2394 "glCopyTexImage%dD(border=%d)", dimensions
, border
);
2398 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, internalFormat
);
2400 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2401 "glCopyTexImage%dD(read buffer)", dimensions
);
2405 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2408 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
2409 switch (internalFormat
) {
2414 case GL_LUMINANCE_ALPHA
:
2417 _mesa_error(ctx
, GL_INVALID_VALUE
,
2418 "glCopyTexImage%dD(internalFormat)", dimensions
);
2423 baseFormat
= _mesa_base_tex_format(ctx
, internalFormat
);
2424 if (baseFormat
< 0) {
2425 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2426 "glCopyTexImage%dD(internalFormat)", dimensions
);
2430 rb_internal_format
= rb
->InternalFormat
;
2431 rb_base_format
= _mesa_base_tex_format(ctx
, rb
->InternalFormat
);
2432 if (_mesa_is_color_format(internalFormat
)) {
2433 if (rb_base_format
< 0) {
2434 _mesa_error(ctx
, GL_INVALID_VALUE
,
2435 "glCopyTexImage%dD(internalFormat)", dimensions
);
2440 if (_mesa_is_gles(ctx
)) {
2442 if (_mesa_base_format_component_count(baseFormat
) >
2443 _mesa_base_format_component_count(rb_base_format
)) {
2446 if (baseFormat
== GL_DEPTH_COMPONENT
||
2447 baseFormat
== GL_DEPTH_STENCIL
||
2448 rb_base_format
== GL_DEPTH_COMPONENT
||
2449 rb_base_format
== GL_DEPTH_STENCIL
||
2450 ((baseFormat
== GL_LUMINANCE_ALPHA
||
2451 baseFormat
== GL_ALPHA
) &&
2452 rb_base_format
!= GL_RGBA
) ||
2453 internalFormat
== GL_RGB9_E5
) {
2456 if (internalFormat
== GL_RGB9_E5
) {
2460 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2461 "glCopyTexImage%dD(internalFormat)", dimensions
);
2466 if ((_mesa_is_desktop_gl(ctx
) &&
2467 ctx
->Extensions
.ARB_framebuffer_object
) ||
2468 _mesa_is_gles3(ctx
)) {
2469 bool rb_is_srgb
= false;
2470 bool dst_is_srgb
= false;
2472 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&&
2473 _mesa_get_format_color_encoding(rb
->Format
) == GL_SRGB
) {
2477 if (_mesa_get_linear_internalformat(internalFormat
) != internalFormat
) {
2481 if (rb_is_srgb
!= dst_is_srgb
) {
2482 /* Page 190 (page 211 of the PDF) in section 8.6 of the OpenGL 4.3
2483 * Core Profile spec says:
2485 * "An INVALID_OPERATION error is generated under any of the
2486 * following conditions:
2490 * - if the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
2491 * for the framebuffer attachment corresponding to the read
2492 * buffer is LINEAR (see section 9.2.3) and internalformat
2493 * is one of the sRGB formats in table 8.23
2494 * - if the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
2495 * for the framebuffer attachment corresponding to the read
2496 * buffer is SRGB and internalformat is not one of the sRGB
2497 * formats. in table 8.23."
2499 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2500 "glCopyTexImage%dD(srgb usage mismatch)", dimensions
);
2505 if (!_mesa_source_buffer_exists(ctx
, baseFormat
)) {
2506 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2507 "glCopyTexImage%dD(missing readbuffer)", dimensions
);
2511 /* From the EXT_texture_integer spec:
2513 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2514 * if the texture internalformat is an integer format and the read color
2515 * buffer is not an integer format, or if the internalformat is not an
2516 * integer format and the read color buffer is an integer format."
2518 if (_mesa_is_color_format(internalFormat
)) {
2519 bool is_int
= _mesa_is_enum_format_integer(internalFormat
);
2520 bool is_rbint
= _mesa_is_enum_format_integer(rb_internal_format
);
2521 if (is_int
|| is_rbint
) {
2522 if (is_int
!= is_rbint
) {
2523 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2524 "glCopyTexImage%dD(integer vs non-integer)", dimensions
);
2526 } else if (_mesa_is_gles(ctx
) &&
2527 _mesa_is_enum_format_unsigned_int(internalFormat
) !=
2528 _mesa_is_enum_format_unsigned_int(rb_internal_format
)) {
2529 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2530 "glCopyTexImage%dD(signed vs unsigned integer)", dimensions
);
2536 if ((target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
||
2537 _mesa_is_cube_face(target
)) && width
!= height
) {
2538 _mesa_error(ctx
, GL_INVALID_VALUE
,
2539 "glTexImage2D(cube width != height)");
2543 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
2544 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
2545 _mesa_error(ctx
, GL_INVALID_ENUM
,
2546 "glCopyTexImage%dD(target)", dimensions
);
2549 if (compressedteximage_only_format(ctx
, internalFormat
)) {
2550 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2551 "glCopyTexImage%dD(no compression for format)", dimensions
);
2555 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2556 "glCopyTexImage%dD(border!=0)", dimensions
);
2561 if (!mutable_tex_object(ctx
, target
)) {
2562 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2563 "glCopyTexImage%dD(immutable texture)", dimensions
);
2567 /* if we get here, the parameters are OK */
2573 * Test glCopyTexSubImage[12]D() parameters for errors.
2574 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2577 copytexsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2578 GLenum target
, GLint level
,
2579 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2580 GLint width
, GLint height
)
2582 struct gl_texture_object
*texObj
;
2583 struct gl_texture_image
*texImage
;
2585 /* Check that the source buffer is complete */
2586 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2587 if (ctx
->ReadBuffer
->_Status
== 0) {
2588 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2590 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2591 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2592 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
2596 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2597 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2598 "glCopyTexSubImage%dD(multisample FBO)",
2604 /* check target (proxies not allowed) */
2605 if (!legal_texsubimage_target(ctx
, dimensions
, target
)) {
2606 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage%uD(target=%s)",
2607 dimensions
, _mesa_lookup_enum_by_nr(target
));
2612 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2613 _mesa_error(ctx
, GL_INVALID_VALUE
,
2614 "glCopyTexSubImage%dD(level=%d)", dimensions
, level
);
2618 /* Get dest texture object / image pointers */
2619 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2621 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%dD()", dimensions
);
2625 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2627 /* destination image does not exist */
2628 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2629 "glCopyTexSubImage%dD(invalid texture image)", dimensions
);
2633 if (error_check_subtexture_dimensions(ctx
, "glCopyTexSubImage",
2634 dimensions
, texImage
,
2635 xoffset
, yoffset
, zoffset
,
2636 width
, height
, 1)) {
2640 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2641 if (compressedteximage_only_format(ctx
, texImage
->InternalFormat
)) {
2642 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2643 "glCopyTexSubImage%dD(no compression for format)", dimensions
);
2648 if (texImage
->InternalFormat
== GL_YCBCR_MESA
) {
2649 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCopyTexSubImage2D");
2653 if (!_mesa_source_buffer_exists(ctx
, texImage
->_BaseFormat
)) {
2654 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2655 "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
2656 dimensions
, texImage
->_BaseFormat
);
2660 /* From the EXT_texture_integer spec:
2662 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2663 * if the texture internalformat is an integer format and the read color
2664 * buffer is not an integer format, or if the internalformat is not an
2665 * integer format and the read color buffer is an integer format."
2667 if (_mesa_is_color_format(texImage
->InternalFormat
)) {
2668 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
2670 if (_mesa_is_format_integer_color(rb
->Format
) !=
2671 _mesa_is_format_integer_color(texImage
->TexFormat
)) {
2672 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2673 "glCopyTexImage%dD(integer vs non-integer)", dimensions
);
2678 /* if we get here, the parameters are OK */
2683 /** Callback info for walking over FBO hash table */
2686 struct gl_context
*ctx
;
2687 struct gl_texture_object
*texObj
;
2693 * Check render to texture callback. Called from _mesa_HashWalk().
2696 check_rtt_cb(GLuint key
, void *data
, void *userData
)
2698 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
2699 const struct cb_info
*info
= (struct cb_info
*) userData
;
2700 struct gl_context
*ctx
= info
->ctx
;
2701 const struct gl_texture_object
*texObj
= info
->texObj
;
2702 const GLuint level
= info
->level
, face
= info
->face
;
2704 /* If this is a user-created FBO */
2705 if (_mesa_is_user_fbo(fb
)) {
2707 /* check if any of the FBO's attachments point to 'texObj' */
2708 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2709 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2710 if (att
->Type
== GL_TEXTURE
&&
2711 att
->Texture
== texObj
&&
2712 att
->TextureLevel
== level
&&
2713 att
->CubeMapFace
== face
) {
2714 ASSERT(_mesa_get_attachment_teximage(att
));
2715 /* Tell driver about the new renderbuffer texture */
2716 ctx
->Driver
.RenderTexture(ctx
, ctx
->DrawBuffer
, att
);
2717 /* Mark fb status as indeterminate to force re-validation */
2726 * When a texture image is specified we have to check if it's bound to
2727 * any framebuffer objects (render to texture) in order to detect changes
2728 * in size or format since that effects FBO completeness.
2729 * Any FBOs rendering into the texture must be re-validated.
2732 _mesa_update_fbo_texture(struct gl_context
*ctx
,
2733 struct gl_texture_object
*texObj
,
2734 GLuint face
, GLuint level
)
2736 /* Only check this texture if it's been marked as RenderToTexture */
2737 if (texObj
->_RenderToTexture
) {
2738 struct cb_info info
;
2740 info
.texObj
= texObj
;
2743 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, check_rtt_cb
, &info
);
2749 * If the texture object's GenerateMipmap flag is set and we've
2750 * changed the texture base level image, regenerate the rest of the
2751 * mipmap levels now.
2754 check_gen_mipmap(struct gl_context
*ctx
, GLenum target
,
2755 struct gl_texture_object
*texObj
, GLint level
)
2757 ASSERT(target
!= GL_TEXTURE_CUBE_MAP
);
2758 if (texObj
->GenerateMipmap
&&
2759 level
== texObj
->BaseLevel
&&
2760 level
< texObj
->MaxLevel
) {
2761 ASSERT(ctx
->Driver
.GenerateMipmap
);
2762 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2767 /** Debug helper: override the user-requested internal format */
2769 override_internal_format(GLenum internalFormat
, GLint width
, GLint height
)
2772 if (internalFormat
== GL_RGBA16F_ARB
||
2773 internalFormat
== GL_RGBA32F_ARB
) {
2774 printf("Convert rgba float tex to int %d x %d\n", width
, height
);
2777 else if (internalFormat
== GL_RGB16F_ARB
||
2778 internalFormat
== GL_RGB32F_ARB
) {
2779 printf("Convert rgb float tex to int %d x %d\n", width
, height
);
2782 else if (internalFormat
== GL_LUMINANCE_ALPHA16F_ARB
||
2783 internalFormat
== GL_LUMINANCE_ALPHA32F_ARB
) {
2784 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2785 return GL_LUMINANCE_ALPHA
;
2787 else if (internalFormat
== GL_LUMINANCE16F_ARB
||
2788 internalFormat
== GL_LUMINANCE32F_ARB
) {
2789 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2790 return GL_LUMINANCE
;
2792 else if (internalFormat
== GL_ALPHA16F_ARB
||
2793 internalFormat
== GL_ALPHA32F_ARB
) {
2794 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2798 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2799 internalFormat = GL_RGBA;
2803 return internalFormat
;
2806 return internalFormat
;
2812 * Choose the actual hardware format for a texture image.
2813 * Try to use the same format as the previous image level when possible.
2814 * Otherwise, ask the driver for the best format.
2815 * It's important to try to choose a consistant format for all levels
2816 * for efficient texture memory layout/allocation. In particular, this
2817 * comes up during automatic mipmap generation.
2820 _mesa_choose_texture_format(struct gl_context
*ctx
,
2821 struct gl_texture_object
*texObj
,
2822 GLenum target
, GLint level
,
2823 GLenum internalFormat
, GLenum format
, GLenum type
)
2827 /* see if we've already chosen a format for the previous level */
2829 struct gl_texture_image
*prevImage
=
2830 _mesa_select_tex_image(ctx
, texObj
, target
, level
- 1);
2831 /* See if the prev level is defined and has an internal format which
2832 * matches the new internal format.
2835 prevImage
->Width
> 0 &&
2836 prevImage
->InternalFormat
== internalFormat
) {
2837 /* use the same format */
2838 ASSERT(prevImage
->TexFormat
!= MESA_FORMAT_NONE
);
2839 return prevImage
->TexFormat
;
2843 /* If the application requested compression to an S3TC format but we don't
2844 * have the DTXn library, force a generic compressed format instead.
2846 if (internalFormat
!= format
&& format
!= GL_NONE
) {
2847 const GLenum before
= internalFormat
;
2849 switch (internalFormat
) {
2850 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
2851 if (!ctx
->Mesa_DXTn
)
2852 internalFormat
= GL_COMPRESSED_RGB
;
2854 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
2855 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
2856 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
2857 if (!ctx
->Mesa_DXTn
)
2858 internalFormat
= GL_COMPRESSED_RGBA
;
2864 if (before
!= internalFormat
) {
2866 "DXT compression requested (%s), "
2867 "but libtxc_dxtn library not installed. Using %s "
2869 _mesa_lookup_enum_by_nr(before
),
2870 _mesa_lookup_enum_by_nr(internalFormat
));
2874 /* choose format from scratch */
2875 f
= ctx
->Driver
.ChooseTextureFormat(ctx
, texObj
->Target
, internalFormat
,
2877 ASSERT(f
!= MESA_FORMAT_NONE
);
2882 * Adjust pixel unpack params and image dimensions to strip off the
2883 * one-pixel texture border.
2885 * Gallium and intel don't support texture borders. They've seldem been used
2886 * and seldom been implemented correctly anyway.
2888 * \param unpackNew returns the new pixel unpack parameters
2891 strip_texture_border(GLenum target
,
2892 GLint
*width
, GLint
*height
, GLint
*depth
,
2893 const struct gl_pixelstore_attrib
*unpack
,
2894 struct gl_pixelstore_attrib
*unpackNew
)
2900 *unpackNew
= *unpack
;
2902 if (unpackNew
->RowLength
== 0)
2903 unpackNew
->RowLength
= *width
;
2905 if (unpackNew
->ImageHeight
== 0)
2906 unpackNew
->ImageHeight
= *height
;
2908 assert(*width
>= 3);
2909 unpackNew
->SkipPixels
++; /* skip the border */
2910 *width
= *width
- 2; /* reduce the width by two border pixels */
2912 /* The min height of a texture with a border is 3 */
2913 if (*height
>= 3 && target
!= GL_TEXTURE_1D_ARRAY
) {
2914 unpackNew
->SkipRows
++; /* skip the border */
2915 *height
= *height
- 2; /* reduce the height by two border pixels */
2918 if (*depth
>= 3 && target
!= GL_TEXTURE_2D_ARRAY
&& target
!= GL_TEXTURE_CUBE_MAP_ARRAY
) {
2919 unpackNew
->SkipImages
++; /* skip the border */
2920 *depth
= *depth
- 2; /* reduce the depth by two border pixels */
2926 * Common code to implement all the glTexImage1D/2D/3D functions
2927 * as well as glCompressedTexImage1D/2D/3D.
2928 * \param compressed only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
2929 * \param format the user's image format (only used if !compressed)
2930 * \param type the user's image type (only used if !compressed)
2931 * \param imageSize only used for glCompressedTexImage1D/2D/3D calls.
2934 teximage(struct gl_context
*ctx
, GLboolean compressed
, GLuint dims
,
2935 GLenum target
, GLint level
, GLint internalFormat
,
2936 GLsizei width
, GLsizei height
, GLsizei depth
,
2937 GLint border
, GLenum format
, GLenum type
,
2938 GLsizei imageSize
, const GLvoid
*pixels
)
2940 const char *func
= compressed
? "glCompressedTexImage" : "glTexImage";
2941 struct gl_pixelstore_attrib unpack_no_border
;
2942 const struct gl_pixelstore_attrib
*unpack
= &ctx
->Unpack
;
2943 struct gl_texture_object
*texObj
;
2944 gl_format texFormat
;
2945 GLboolean dimensionsOK
, sizeOK
;
2947 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2949 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
)) {
2952 "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
2954 _mesa_lookup_enum_by_nr(target
), level
,
2955 _mesa_lookup_enum_by_nr(internalFormat
),
2956 width
, height
, depth
, border
, pixels
);
2959 "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2961 _mesa_lookup_enum_by_nr(target
), level
,
2962 _mesa_lookup_enum_by_nr(internalFormat
),
2963 width
, height
, depth
, border
,
2964 _mesa_lookup_enum_by_nr(format
),
2965 _mesa_lookup_enum_by_nr(type
), pixels
);
2968 internalFormat
= override_internal_format(internalFormat
, width
, height
);
2970 /* target error checking */
2971 if (!legal_teximage_target(ctx
, dims
, target
)) {
2972 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s%uD(target=%s)",
2973 func
, dims
, _mesa_lookup_enum_by_nr(target
));
2977 /* general error checking */
2979 if (compressed_texture_error_check(ctx
, dims
, target
, level
,
2981 width
, height
, depth
,
2986 if (texture_error_check(ctx
, dims
, target
, level
, internalFormat
,
2987 format
, type
, width
, height
, depth
, border
))
2991 /* Here we convert a cpal compressed image into a regular glTexImage2D
2992 * call by decompressing the texture. If we really want to support cpal
2993 * textures in any driver this would have to be changed.
2995 if (ctx
->API
== API_OPENGLES
&& compressed
&& dims
== 2) {
2996 switch (internalFormat
) {
2997 case GL_PALETTE4_RGB8_OES
:
2998 case GL_PALETTE4_RGBA8_OES
:
2999 case GL_PALETTE4_R5_G6_B5_OES
:
3000 case GL_PALETTE4_RGBA4_OES
:
3001 case GL_PALETTE4_RGB5_A1_OES
:
3002 case GL_PALETTE8_RGB8_OES
:
3003 case GL_PALETTE8_RGBA8_OES
:
3004 case GL_PALETTE8_R5_G6_B5_OES
:
3005 case GL_PALETTE8_RGBA4_OES
:
3006 case GL_PALETTE8_RGB5_A1_OES
:
3007 _mesa_cpal_compressed_teximage2d(target
, level
, internalFormat
,
3008 width
, height
, imageSize
, pixels
);
3013 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3016 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3017 internalFormat
, format
, type
);
3018 assert(texFormat
!= MESA_FORMAT_NONE
);
3020 /* check that width, height, depth are legal for the mipmap level */
3021 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, level
, width
,
3022 height
, depth
, border
);
3024 /* check that the texture won't take too much memory, etc */
3025 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, _mesa_get_proxy_target(target
),
3027 width
, height
, depth
, border
);
3029 if (_mesa_is_proxy_texture(target
)) {
3030 /* Proxy texture: just clear or set state depending on error checking */
3031 struct gl_texture_image
*texImage
=
3032 get_proxy_tex_image(ctx
, target
, level
);
3035 return; /* GL_OUT_OF_MEMORY already recorded */
3037 if (dimensionsOK
&& sizeOK
) {
3038 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, depth
,
3039 border
, internalFormat
, texFormat
);
3042 clear_teximage_fields(texImage
);
3046 /* non-proxy target */
3047 const GLuint face
= _mesa_tex_target_to_face(target
);
3048 struct gl_texture_image
*texImage
;
3050 if (!dimensionsOK
) {
3051 _mesa_error(ctx
, GL_INVALID_VALUE
,
3052 "glTexImage%uD(invalid width or height or depth)",
3058 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3059 "glTexImage%uD(image too large)", dims
);
3063 /* Allow a hardware driver to just strip out the border, to provide
3064 * reliable but slightly incorrect hardware rendering instead of
3065 * rarely-tested software fallback rendering.
3067 if (border
&& ctx
->Const
.StripTextureBorder
) {
3068 strip_texture_border(target
, &width
, &height
, &depth
, unpack
,
3071 unpack
= &unpack_no_border
;
3074 if (ctx
->NewState
& _NEW_PIXEL
)
3075 _mesa_update_state(ctx
);
3077 _mesa_lock_texture(ctx
, texObj
);
3079 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3082 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s%uD", func
, dims
);
3085 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3087 _mesa_init_teximage_fields(ctx
, texImage
,
3088 width
, height
, depth
,
3089 border
, internalFormat
, texFormat
);
3091 /* Give the texture to the driver. <pixels> may be null. */
3092 if (width
> 0 && height
> 0 && depth
> 0) {
3094 ctx
->Driver
.CompressedTexImage(ctx
, dims
, texImage
,
3098 ctx
->Driver
.TexImage(ctx
, dims
, texImage
, format
,
3099 type
, pixels
, unpack
);
3103 check_gen_mipmap(ctx
, target
, texObj
, level
);
3105 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3107 _mesa_dirty_texobj(ctx
, texObj
, GL_TRUE
);
3110 _mesa_unlock_texture(ctx
, texObj
);
3117 * Called from the API. Note that width includes the border.
3120 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
3121 GLsizei width
, GLint border
, GLenum format
,
3122 GLenum type
, const GLvoid
*pixels
)
3124 GET_CURRENT_CONTEXT(ctx
);
3125 teximage(ctx
, GL_FALSE
, 1, target
, level
, internalFormat
, width
, 1, 1,
3126 border
, format
, type
, 0, pixels
);
3131 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
3132 GLsizei width
, GLsizei height
, GLint border
,
3133 GLenum format
, GLenum type
,
3134 const GLvoid
*pixels
)
3136 GET_CURRENT_CONTEXT(ctx
);
3137 teximage(ctx
, GL_FALSE
, 2, target
, level
, internalFormat
, width
, height
, 1,
3138 border
, format
, type
, 0, pixels
);
3143 * Called by the API or display list executor.
3144 * Note that width and height include the border.
3147 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
3148 GLsizei width
, GLsizei height
, GLsizei depth
,
3149 GLint border
, GLenum format
, GLenum type
,
3150 const GLvoid
*pixels
)
3152 GET_CURRENT_CONTEXT(ctx
);
3153 teximage(ctx
, GL_FALSE
, 3, target
, level
, internalFormat
,
3154 width
, height
, depth
,
3155 border
, format
, type
, 0, pixels
);
3160 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
3161 GLsizei width
, GLsizei height
, GLsizei depth
,
3162 GLint border
, GLenum format
, GLenum type
,
3163 const GLvoid
*pixels
)
3165 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
3166 depth
, border
, format
, type
, pixels
);
3171 _mesa_EGLImageTargetTexture2DOES (GLenum target
, GLeglImageOES image
)
3173 struct gl_texture_object
*texObj
;
3174 struct gl_texture_image
*texImage
;
3176 GET_CURRENT_CONTEXT(ctx
);
3177 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3181 valid_target
= ctx
->Extensions
.OES_EGL_image
;
3183 case GL_TEXTURE_EXTERNAL_OES
:
3184 valid_target
= ctx
->Extensions
.OES_EGL_image_external
;
3187 valid_target
= false;
3191 if (!valid_target
) {
3192 _mesa_error(ctx
, GL_INVALID_ENUM
,
3193 "glEGLImageTargetTexture2D(target=%d)", target
);
3197 if (ctx
->NewState
& _NEW_PIXEL
)
3198 _mesa_update_state(ctx
);
3200 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3201 _mesa_lock_texture(ctx
, texObj
);
3203 if (texObj
->Immutable
) {
3204 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3205 "glEGLImageTargetTexture2D(texture is immutable)");
3206 _mesa_unlock_texture(ctx
, texObj
);
3210 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
3212 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glEGLImageTargetTexture2D");
3214 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3216 ctx
->Driver
.EGLImageTargetTexture2D(ctx
, target
,
3217 texObj
, texImage
, image
);
3219 _mesa_dirty_texobj(ctx
, texObj
, GL_TRUE
);
3221 _mesa_unlock_texture(ctx
, texObj
);
3228 * Implement all the glTexSubImage1/2/3D() functions.
3231 texsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
3232 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3233 GLsizei width
, GLsizei height
, GLsizei depth
,
3234 GLenum format
, GLenum type
, const GLvoid
*pixels
)
3236 struct gl_texture_object
*texObj
;
3237 struct gl_texture_image
*texImage
;
3239 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3241 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3242 _mesa_debug(ctx
, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3244 _mesa_lookup_enum_by_nr(target
), level
,
3245 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3246 _mesa_lookup_enum_by_nr(format
),
3247 _mesa_lookup_enum_by_nr(type
), pixels
);
3249 /* check target (proxies not allowed) */
3250 if (!legal_texsubimage_target(ctx
, dims
, target
)) {
3251 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage%uD(target=%s)",
3252 dims
, _mesa_lookup_enum_by_nr(target
));
3256 if (ctx
->NewState
& _NEW_PIXEL
)
3257 _mesa_update_state(ctx
);
3259 if (texsubimage_error_check(ctx
, dims
, target
, level
,
3260 xoffset
, yoffset
, zoffset
,
3261 width
, height
, depth
, format
, type
)) {
3262 return; /* error was detected */
3265 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3267 _mesa_lock_texture(ctx
, texObj
);
3269 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3271 if (width
> 0 && height
> 0 && depth
> 0) {
3272 /* If we have a border, offset=-1 is legal. Bias by border width. */
3275 if (target
!= GL_TEXTURE_2D_ARRAY
)
3276 zoffset
+= texImage
->Border
;
3279 if (target
!= GL_TEXTURE_1D_ARRAY
)
3280 yoffset
+= texImage
->Border
;
3283 xoffset
+= texImage
->Border
;
3286 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3287 xoffset
, yoffset
, zoffset
,
3288 width
, height
, depth
,
3289 format
, type
, pixels
, &ctx
->Unpack
);
3291 check_gen_mipmap(ctx
, target
, texObj
, level
);
3293 ctx
->NewState
|= _NEW_TEXTURE
;
3296 _mesa_unlock_texture(ctx
, texObj
);
3301 _mesa_TexSubImage1D( GLenum target
, GLint level
,
3302 GLint xoffset
, GLsizei width
,
3303 GLenum format
, GLenum type
,
3304 const GLvoid
*pixels
)
3306 GET_CURRENT_CONTEXT(ctx
);
3307 texsubimage(ctx
, 1, target
, level
,
3310 format
, type
, pixels
);
3315 _mesa_TexSubImage2D( GLenum target
, GLint level
,
3316 GLint xoffset
, GLint yoffset
,
3317 GLsizei width
, GLsizei height
,
3318 GLenum format
, GLenum type
,
3319 const GLvoid
*pixels
)
3321 GET_CURRENT_CONTEXT(ctx
);
3322 texsubimage(ctx
, 2, target
, level
,
3323 xoffset
, yoffset
, 0,
3325 format
, type
, pixels
);
3331 _mesa_TexSubImage3D( GLenum target
, GLint level
,
3332 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3333 GLsizei width
, GLsizei height
, GLsizei depth
,
3334 GLenum format
, GLenum type
,
3335 const GLvoid
*pixels
)
3337 GET_CURRENT_CONTEXT(ctx
);
3338 texsubimage(ctx
, 3, target
, level
,
3339 xoffset
, yoffset
, zoffset
,
3340 width
, height
, depth
,
3341 format
, type
, pixels
);
3347 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3348 * from. This depends on whether the texture contains color or depth values.
3350 static struct gl_renderbuffer
*
3351 get_copy_tex_image_source(struct gl_context
*ctx
, gl_format texFormat
)
3353 if (_mesa_get_format_bits(texFormat
, GL_DEPTH_BITS
) > 0) {
3354 /* reading from depth/stencil buffer */
3355 return ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
3358 /* copying from color buffer */
3359 return ctx
->ReadBuffer
->_ColorReadBuffer
;
3366 * Implement the glCopyTexImage1/2D() functions.
3369 copyteximage(struct gl_context
*ctx
, GLuint dims
,
3370 GLenum target
, GLint level
, GLenum internalFormat
,
3371 GLint x
, GLint y
, GLsizei width
, GLsizei height
, GLint border
)
3373 struct gl_texture_object
*texObj
;
3374 struct gl_texture_image
*texImage
;
3375 const GLuint face
= _mesa_tex_target_to_face(target
);
3376 gl_format texFormat
;
3378 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3380 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3381 _mesa_debug(ctx
, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3383 _mesa_lookup_enum_by_nr(target
), level
,
3384 _mesa_lookup_enum_by_nr(internalFormat
),
3385 x
, y
, width
, height
, border
);
3387 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3388 _mesa_update_state(ctx
);
3390 if (copytexture_error_check(ctx
, dims
, target
, level
, internalFormat
,
3391 width
, height
, border
))
3394 if (!_mesa_legal_texture_dimensions(ctx
, target
, level
, width
, height
,
3396 _mesa_error(ctx
, GL_INVALID_VALUE
,
3397 "glCopyTexImage%uD(invalid width or height)", dims
);
3401 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3404 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3405 internalFormat
, GL_NONE
, GL_NONE
);
3406 assert(texFormat
!= MESA_FORMAT_NONE
);
3408 if (!ctx
->Driver
.TestProxyTexImage(ctx
, _mesa_get_proxy_target(target
),
3410 width
, height
, 1, border
)) {
3411 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3412 "glCopyTexImage%uD(image too large)", dims
);
3416 if (border
&& ctx
->Const
.StripTextureBorder
) {
3418 width
-= border
* 2;
3421 height
-= border
* 2;
3426 _mesa_lock_texture(ctx
, texObj
);
3428 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3431 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
3434 GLint srcX
= x
, srcY
= y
, dstX
= 0, dstY
= 0, dstZ
= 0;
3436 /* Free old texture image */
3437 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3439 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, 1,
3440 border
, internalFormat
, texFormat
);
3442 /* Allocate texture memory (no pixel data yet) */
3443 ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
);
3445 if (_mesa_clip_copytexsubimage(ctx
, &dstX
, &dstY
, &srcX
, &srcY
,
3447 struct gl_renderbuffer
*srcRb
=
3448 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3450 ctx
->Driver
.CopyTexSubImage(ctx
, dims
, texImage
, dstX
, dstY
, dstZ
,
3451 srcRb
, srcX
, srcY
, width
, height
);
3454 check_gen_mipmap(ctx
, target
, texObj
, level
);
3456 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3458 _mesa_dirty_texobj(ctx
, texObj
, GL_TRUE
);
3461 _mesa_unlock_texture(ctx
, texObj
);
3467 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
3468 GLenum internalFormat
,
3470 GLsizei width
, GLint border
)
3472 GET_CURRENT_CONTEXT(ctx
);
3473 copyteximage(ctx
, 1, target
, level
, internalFormat
, x
, y
, width
, 1, border
);
3479 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
3480 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
3483 GET_CURRENT_CONTEXT(ctx
);
3484 copyteximage(ctx
, 2, target
, level
, internalFormat
,
3485 x
, y
, width
, height
, border
);
3491 * Implementation for glCopyTexSubImage1/2/3D() functions.
3494 copytexsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
3495 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3496 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3498 struct gl_texture_object
*texObj
;
3499 struct gl_texture_image
*texImage
;
3501 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3503 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3504 _mesa_debug(ctx
, "glCopyTexSubImage%uD %s %d %d %d %d %d %d %d %d\n",
3506 _mesa_lookup_enum_by_nr(target
),
3507 level
, xoffset
, yoffset
, zoffset
, x
, y
, width
, height
);
3509 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3510 _mesa_update_state(ctx
);
3512 if (copytexsubimage_error_check(ctx
, dims
, target
, level
,
3513 xoffset
, yoffset
, zoffset
, width
, height
)) {
3517 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3519 _mesa_lock_texture(ctx
, texObj
);
3521 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3523 /* If we have a border, offset=-1 is legal. Bias by border width. */
3526 if (target
!= GL_TEXTURE_2D_ARRAY
)
3527 zoffset
+= texImage
->Border
;
3530 if (target
!= GL_TEXTURE_1D_ARRAY
)
3531 yoffset
+= texImage
->Border
;
3534 xoffset
+= texImage
->Border
;
3537 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3539 struct gl_renderbuffer
*srcRb
=
3540 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3542 ctx
->Driver
.CopyTexSubImage(ctx
, dims
, texImage
,
3543 xoffset
, yoffset
, zoffset
,
3544 srcRb
, x
, y
, width
, height
);
3546 check_gen_mipmap(ctx
, target
, texObj
, level
);
3548 ctx
->NewState
|= _NEW_TEXTURE
;
3551 _mesa_unlock_texture(ctx
, texObj
);
3556 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
3557 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
3559 GET_CURRENT_CONTEXT(ctx
);
3560 copytexsubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, x
, y
, width
, 1);
3566 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
3567 GLint xoffset
, GLint yoffset
,
3568 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3570 GET_CURRENT_CONTEXT(ctx
);
3571 copytexsubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0, x
, y
,
3578 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
3579 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3580 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3582 GET_CURRENT_CONTEXT(ctx
);
3583 copytexsubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
3584 x
, y
, width
, height
);
3590 /**********************************************************************/
3591 /****** Compressed Textures ******/
3592 /**********************************************************************/
3596 * Error checking for glCompressedTexSubImage[123]D().
3597 * \return error code or GL_NO_ERROR.
3600 compressed_subtexture_error_check(struct gl_context
*ctx
, GLint dims
,
3601 GLenum target
, GLint level
,
3602 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3603 GLsizei width
, GLsizei height
, GLsizei depth
,
3604 GLenum format
, GLsizei imageSize
)
3606 struct gl_texture_object
*texObj
;
3607 struct gl_texture_image
*texImage
;
3615 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
3616 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
3617 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
3618 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
3619 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
3620 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
3624 targetOK
= GL_FALSE
;
3629 targetOK
= (target
== GL_TEXTURE_2D_ARRAY
);
3633 /* no 1D compressed textures at this time */
3634 targetOK
= GL_FALSE
;
3639 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage%uD(target)",
3644 /* this will catch any invalid compressed format token */
3645 if (!_mesa_is_compressed_format(ctx
, format
)) {
3646 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage%uD(format)",
3651 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
3652 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexImage%uD(level=%d)",
3657 expectedSize
= compressed_tex_size(width
, height
, depth
, format
);
3658 if (expectedSize
!= imageSize
) {
3659 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexImage%uD(size=%d)",
3664 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3666 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3667 "glCompressedTexSubImage%uD()", dims
);
3671 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3673 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3674 "glCompressedTexSubImage%uD(invalid texture image)", dims
);
3678 if ((GLint
) format
!= texImage
->InternalFormat
) {
3679 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3680 "glCompressedTexSubImage%uD(format=0x%x)", dims
, format
);
3684 if (compressedteximage_only_format(ctx
, format
)) {
3685 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3686 "glCompressedTexSubImage%uD(format=0x%x cannot be updated)"
3691 if (error_check_subtexture_dimensions(ctx
, "glCompressedTexSubImage", dims
,
3692 texImage
, xoffset
, yoffset
, zoffset
,
3693 width
, height
, depth
)) {
3702 _mesa_CompressedTexImage1D(GLenum target
, GLint level
,
3703 GLenum internalFormat
, GLsizei width
,
3704 GLint border
, GLsizei imageSize
,
3707 GET_CURRENT_CONTEXT(ctx
);
3708 teximage(ctx
, GL_TRUE
, 1, target
, level
, internalFormat
,
3709 width
, 1, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
3714 _mesa_CompressedTexImage2D(GLenum target
, GLint level
,
3715 GLenum internalFormat
, GLsizei width
,
3716 GLsizei height
, GLint border
, GLsizei imageSize
,
3719 GET_CURRENT_CONTEXT(ctx
);
3720 teximage(ctx
, GL_TRUE
, 2, target
, level
, internalFormat
,
3721 width
, height
, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
3726 _mesa_CompressedTexImage3D(GLenum target
, GLint level
,
3727 GLenum internalFormat
, GLsizei width
,
3728 GLsizei height
, GLsizei depth
, GLint border
,
3729 GLsizei imageSize
, const GLvoid
*data
)
3731 GET_CURRENT_CONTEXT(ctx
);
3732 teximage(ctx
, GL_TRUE
, 3, target
, level
, internalFormat
,
3733 width
, height
, depth
, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
3738 * Common helper for glCompressedTexSubImage1/2/3D().
3741 compressed_tex_sub_image(GLuint dims
, GLenum target
, GLint level
,
3742 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3743 GLsizei width
, GLsizei height
, GLsizei depth
,
3744 GLenum format
, GLsizei imageSize
, const GLvoid
*data
)
3746 struct gl_texture_object
*texObj
;
3747 struct gl_texture_image
*texImage
;
3748 GET_CURRENT_CONTEXT(ctx
);
3749 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
3751 if (compressed_subtexture_error_check(ctx
, dims
, target
, level
,
3752 xoffset
, yoffset
, zoffset
,
3753 width
, height
, depth
,
3754 format
, imageSize
)) {
3758 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3760 _mesa_lock_texture(ctx
, texObj
);
3762 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3765 if (width
> 0 && height
> 0 && depth
> 0) {
3766 ctx
->Driver
.CompressedTexSubImage(ctx
, dims
, texImage
,
3767 xoffset
, yoffset
, zoffset
,
3768 width
, height
, depth
,
3769 format
, imageSize
, data
);
3771 check_gen_mipmap(ctx
, target
, texObj
, level
);
3773 ctx
->NewState
|= _NEW_TEXTURE
;
3776 _mesa_unlock_texture(ctx
, texObj
);
3781 _mesa_CompressedTexSubImage1D(GLenum target
, GLint level
, GLint xoffset
,
3782 GLsizei width
, GLenum format
,
3783 GLsizei imageSize
, const GLvoid
*data
)
3785 compressed_tex_sub_image(1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
3786 format
, imageSize
, data
);
3791 _mesa_CompressedTexSubImage2D(GLenum target
, GLint level
, GLint xoffset
,
3792 GLint yoffset
, GLsizei width
, GLsizei height
,
3793 GLenum format
, GLsizei imageSize
,
3796 compressed_tex_sub_image(2, target
, level
, xoffset
, yoffset
, 0,
3797 width
, height
, 1, format
, imageSize
, data
);
3802 _mesa_CompressedTexSubImage3D(GLenum target
, GLint level
, GLint xoffset
,
3803 GLint yoffset
, GLint zoffset
, GLsizei width
,
3804 GLsizei height
, GLsizei depth
, GLenum format
,
3805 GLsizei imageSize
, const GLvoid
*data
)
3807 compressed_tex_sub_image(3, target
, level
, xoffset
, yoffset
, zoffset
,
3808 width
, height
, depth
, format
, imageSize
, data
);
3812 get_texbuffer_format(const struct gl_context
*ctx
, GLenum internalFormat
)
3814 switch (internalFormat
) {
3816 return MESA_FORMAT_A8
;
3818 return MESA_FORMAT_A16
;
3819 case GL_ALPHA16F_ARB
:
3820 return MESA_FORMAT_ALPHA_FLOAT16
;
3821 case GL_ALPHA32F_ARB
:
3822 return MESA_FORMAT_ALPHA_FLOAT32
;
3823 case GL_ALPHA8I_EXT
:
3824 return MESA_FORMAT_ALPHA_INT8
;
3825 case GL_ALPHA16I_EXT
:
3826 return MESA_FORMAT_ALPHA_INT16
;
3827 case GL_ALPHA32I_EXT
:
3828 return MESA_FORMAT_ALPHA_INT32
;
3829 case GL_ALPHA8UI_EXT
:
3830 return MESA_FORMAT_ALPHA_UINT8
;
3831 case GL_ALPHA16UI_EXT
:
3832 return MESA_FORMAT_ALPHA_UINT16
;
3833 case GL_ALPHA32UI_EXT
:
3834 return MESA_FORMAT_ALPHA_UINT32
;
3836 return MESA_FORMAT_L8
;
3837 case GL_LUMINANCE16
:
3838 return MESA_FORMAT_L16
;
3839 case GL_LUMINANCE16F_ARB
:
3840 return MESA_FORMAT_LUMINANCE_FLOAT16
;
3841 case GL_LUMINANCE32F_ARB
:
3842 return MESA_FORMAT_LUMINANCE_FLOAT32
;
3843 case GL_LUMINANCE8I_EXT
:
3844 return MESA_FORMAT_LUMINANCE_INT8
;
3845 case GL_LUMINANCE16I_EXT
:
3846 return MESA_FORMAT_LUMINANCE_INT16
;
3847 case GL_LUMINANCE32I_EXT
:
3848 return MESA_FORMAT_LUMINANCE_INT32
;
3849 case GL_LUMINANCE8UI_EXT
:
3850 return MESA_FORMAT_LUMINANCE_UINT8
;
3851 case GL_LUMINANCE16UI_EXT
:
3852 return MESA_FORMAT_LUMINANCE_UINT16
;
3853 case GL_LUMINANCE32UI_EXT
:
3854 return MESA_FORMAT_LUMINANCE_UINT32
;
3855 case GL_LUMINANCE8_ALPHA8
:
3856 return MESA_FORMAT_AL88
;
3857 case GL_LUMINANCE16_ALPHA16
:
3858 return MESA_FORMAT_AL1616
;
3859 case GL_LUMINANCE_ALPHA16F_ARB
:
3860 return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16
;
3861 case GL_LUMINANCE_ALPHA32F_ARB
:
3862 return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32
;
3863 case GL_LUMINANCE_ALPHA8I_EXT
:
3864 return MESA_FORMAT_LUMINANCE_ALPHA_INT8
;
3865 case GL_LUMINANCE_ALPHA16I_EXT
:
3866 return MESA_FORMAT_LUMINANCE_ALPHA_INT8
;
3867 case GL_LUMINANCE_ALPHA32I_EXT
:
3868 return MESA_FORMAT_LUMINANCE_ALPHA_INT16
;
3869 case GL_LUMINANCE_ALPHA8UI_EXT
:
3870 return MESA_FORMAT_LUMINANCE_ALPHA_UINT8
;
3871 case GL_LUMINANCE_ALPHA16UI_EXT
:
3872 return MESA_FORMAT_LUMINANCE_ALPHA_UINT16
;
3873 case GL_LUMINANCE_ALPHA32UI_EXT
:
3874 return MESA_FORMAT_LUMINANCE_ALPHA_UINT32
;
3876 return MESA_FORMAT_I8
;
3877 case GL_INTENSITY16
:
3878 return MESA_FORMAT_I16
;
3879 case GL_INTENSITY16F_ARB
:
3880 return MESA_FORMAT_INTENSITY_FLOAT16
;
3881 case GL_INTENSITY32F_ARB
:
3882 return MESA_FORMAT_INTENSITY_FLOAT32
;
3883 case GL_INTENSITY8I_EXT
:
3884 return MESA_FORMAT_INTENSITY_INT8
;
3885 case GL_INTENSITY16I_EXT
:
3886 return MESA_FORMAT_INTENSITY_INT16
;
3887 case GL_INTENSITY32I_EXT
:
3888 return MESA_FORMAT_INTENSITY_INT32
;
3889 case GL_INTENSITY8UI_EXT
:
3890 return MESA_FORMAT_INTENSITY_UINT8
;
3891 case GL_INTENSITY16UI_EXT
:
3892 return MESA_FORMAT_INTENSITY_UINT16
;
3893 case GL_INTENSITY32UI_EXT
:
3894 return MESA_FORMAT_INTENSITY_UINT32
;
3896 return MESA_FORMAT_RGBA8888_REV
;
3898 return MESA_FORMAT_RGBA_16
;
3899 case GL_RGBA16F_ARB
:
3900 return MESA_FORMAT_RGBA_FLOAT16
;
3901 case GL_RGBA32F_ARB
:
3902 return MESA_FORMAT_RGBA_FLOAT32
;
3904 return MESA_FORMAT_RGBA_INT8
;
3905 case GL_RGBA16I_EXT
:
3906 return MESA_FORMAT_RGBA_INT16
;
3907 case GL_RGBA32I_EXT
:
3908 return MESA_FORMAT_RGBA_INT32
;
3909 case GL_RGBA8UI_EXT
:
3910 return MESA_FORMAT_RGBA_UINT8
;
3911 case GL_RGBA16UI_EXT
:
3912 return MESA_FORMAT_RGBA_UINT16
;
3913 case GL_RGBA32UI_EXT
:
3914 return MESA_FORMAT_RGBA_UINT32
;
3917 return MESA_FORMAT_GR88
;
3919 return MESA_FORMAT_RG1616
;
3921 return MESA_FORMAT_RG_FLOAT16
;
3923 return MESA_FORMAT_RG_FLOAT32
;
3925 return MESA_FORMAT_RG_INT8
;
3927 return MESA_FORMAT_RG_INT16
;
3929 return MESA_FORMAT_RG_INT32
;
3931 return MESA_FORMAT_RG_UINT8
;
3933 return MESA_FORMAT_RG_UINT16
;
3935 return MESA_FORMAT_RG_UINT32
;
3938 return MESA_FORMAT_R8
;
3940 return MESA_FORMAT_R16
;
3942 return MESA_FORMAT_R_FLOAT16
;
3944 return MESA_FORMAT_R_FLOAT32
;
3946 return MESA_FORMAT_R_INT8
;
3948 return MESA_FORMAT_R_INT16
;
3950 return MESA_FORMAT_R_INT32
;
3952 return MESA_FORMAT_R_UINT8
;
3954 return MESA_FORMAT_R_UINT16
;
3956 return MESA_FORMAT_R_UINT32
;
3959 return MESA_FORMAT_RGB_FLOAT32
;
3961 return MESA_FORMAT_RGB_UINT32
;
3963 return MESA_FORMAT_RGB_INT32
;
3966 return MESA_FORMAT_NONE
;
3971 validate_texbuffer_format(const struct gl_context
*ctx
, GLenum internalFormat
)
3973 gl_format format
= get_texbuffer_format(ctx
, internalFormat
);
3976 if (format
== MESA_FORMAT_NONE
)
3977 return MESA_FORMAT_NONE
;
3979 datatype
= _mesa_get_format_datatype(format
);
3980 if (datatype
== GL_FLOAT
&& !ctx
->Extensions
.ARB_texture_float
)
3981 return MESA_FORMAT_NONE
;
3983 if (datatype
== GL_HALF_FLOAT
&& !ctx
->Extensions
.ARB_half_float_pixel
)
3984 return MESA_FORMAT_NONE
;
3986 /* The GL_ARB_texture_rg and GL_ARB_texture_buffer_object specs don't make
3987 * any mention of R/RG formats, but they appear in the GL 3.1 core
3990 if (ctx
->Version
<= 30) {
3991 GLenum base_format
= _mesa_get_format_base_format(format
);
3993 if (base_format
== GL_R
|| base_format
== GL_RG
)
3994 return MESA_FORMAT_NONE
;
3997 if (!ctx
->Extensions
.ARB_texture_buffer_object_rgb32
) {
3998 GLenum base_format
= _mesa_get_format_base_format(format
);
3999 if (base_format
== GL_RGB
)
4000 return MESA_FORMAT_NONE
;
4006 /** GL_ARB_texture_buffer_object */
4008 _mesa_TexBuffer(GLenum target
, GLenum internalFormat
, GLuint buffer
)
4010 struct gl_texture_object
*texObj
;
4011 struct gl_buffer_object
*bufObj
;
4014 GET_CURRENT_CONTEXT(ctx
);
4015 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
4017 if (!(ctx
->API
== API_OPENGL_CORE
&&
4018 ctx
->Extensions
.ARB_texture_buffer_object
)) {
4019 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBuffer");
4023 if (target
!= GL_TEXTURE_BUFFER_ARB
) {
4024 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexBuffer(target)");
4028 format
= validate_texbuffer_format(ctx
, internalFormat
);
4029 if (format
== MESA_FORMAT_NONE
) {
4030 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexBuffer(internalFormat 0x%x)",
4035 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4036 if (buffer
&& !bufObj
) {
4037 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBuffer(buffer %u)", buffer
);
4041 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4043 _mesa_lock_texture(ctx
, texObj
);
4045 _mesa_reference_buffer_object(ctx
, &texObj
->BufferObject
, bufObj
);
4046 texObj
->BufferObjectFormat
= internalFormat
;
4047 texObj
->_BufferObjectFormat
= format
;
4049 _mesa_unlock_texture(ctx
, texObj
);