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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
29 * Texture image-related functions.
34 #include "bufferobj.h"
38 #include "framebuffer.h"
43 #include "multisample.h"
45 #include "texcompress.h"
46 #include "texcompress_cpal.h"
50 #include "texstorage.h"
51 #include "textureview.h"
53 #include "glformats.h"
57 * State changes which we care about for glCopyTex[Sub]Image() calls.
58 * In particular, we care about pixel transfer state and buffer state
59 * (such as glReadBuffer to make sure we read from the right renderbuffer).
61 #define NEW_COPY_TEX_STATE (_NEW_BUFFERS | _NEW_PIXEL)
66 * Return the simple base format for a given internal texture format.
67 * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
69 * \param ctx GL context.
70 * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
72 * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
73 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
75 * This is the format which is used during texture application (i.e. the
76 * texture format and env mode determine the arithmetic used.
79 _mesa_base_tex_format( struct gl_context
*ctx
, GLint internalFormat
)
81 switch (internalFormat
) {
87 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_ALPHA
: -1;
94 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_LUMINANCE
: -1;
96 case GL_LUMINANCE_ALPHA
:
97 case GL_LUMINANCE4_ALPHA4
:
98 case GL_LUMINANCE6_ALPHA2
:
99 case GL_LUMINANCE8_ALPHA8
:
100 case GL_LUMINANCE12_ALPHA4
:
101 case GL_LUMINANCE12_ALPHA12
:
102 case GL_LUMINANCE16_ALPHA16
:
103 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_LUMINANCE_ALPHA
: -1;
109 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_INTENSITY
: -1;
111 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_RGB
: -1;
122 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_RGBA
: -1;
136 /* GL_BGRA can be an internal format *only* in OpenGL ES (1.x or 2.0).
138 if (_mesa_is_gles(ctx
)) {
139 switch (internalFormat
) {
147 if (ctx
->Extensions
.ARB_ES2_compatibility
) {
148 switch (internalFormat
) {
156 if (ctx
->Extensions
.ARB_depth_texture
) {
157 switch (internalFormat
) {
158 case GL_DEPTH_COMPONENT
:
159 case GL_DEPTH_COMPONENT16
:
160 case GL_DEPTH_COMPONENT24
:
161 case GL_DEPTH_COMPONENT32
:
162 return GL_DEPTH_COMPONENT
;
168 switch (internalFormat
) {
169 case GL_COMPRESSED_ALPHA
:
171 case GL_COMPRESSED_LUMINANCE
:
173 case GL_COMPRESSED_LUMINANCE_ALPHA
:
174 return GL_LUMINANCE_ALPHA
;
175 case GL_COMPRESSED_INTENSITY
:
177 case GL_COMPRESSED_RGB
:
179 case GL_COMPRESSED_RGBA
:
185 if (ctx
->Extensions
.TDFX_texture_compression_FXT1
) {
186 switch (internalFormat
) {
187 case GL_COMPRESSED_RGB_FXT1_3DFX
:
189 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
196 /* Assume that the ANGLE flag will always be set if the EXT flag is set.
198 if (ctx
->Extensions
.ANGLE_texture_compression_dxt
) {
199 switch (internalFormat
) {
200 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
202 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
203 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
204 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
211 if (_mesa_is_desktop_gl(ctx
)
212 && ctx
->Extensions
.ANGLE_texture_compression_dxt
) {
213 switch (internalFormat
) {
225 if (ctx
->Extensions
.MESA_ycbcr_texture
) {
226 if (internalFormat
== GL_YCBCR_MESA
)
227 return GL_YCBCR_MESA
;
230 if (ctx
->Extensions
.ARB_texture_float
) {
231 switch (internalFormat
) {
232 case GL_ALPHA16F_ARB
:
233 case GL_ALPHA32F_ARB
:
241 case GL_INTENSITY16F_ARB
:
242 case GL_INTENSITY32F_ARB
:
244 case GL_LUMINANCE16F_ARB
:
245 case GL_LUMINANCE32F_ARB
:
247 case GL_LUMINANCE_ALPHA16F_ARB
:
248 case GL_LUMINANCE_ALPHA32F_ARB
:
249 return GL_LUMINANCE_ALPHA
;
255 if (ctx
->Extensions
.ATI_envmap_bumpmap
) {
256 switch (internalFormat
) {
265 if (ctx
->Extensions
.EXT_texture_snorm
) {
266 switch (internalFormat
) {
281 case GL_RGBA16_SNORM
:
284 case GL_ALPHA8_SNORM
:
285 case GL_ALPHA16_SNORM
:
287 case GL_LUMINANCE_SNORM
:
288 case GL_LUMINANCE8_SNORM
:
289 case GL_LUMINANCE16_SNORM
:
291 case GL_LUMINANCE_ALPHA_SNORM
:
292 case GL_LUMINANCE8_ALPHA8_SNORM
:
293 case GL_LUMINANCE16_ALPHA16_SNORM
:
294 return GL_LUMINANCE_ALPHA
;
295 case GL_INTENSITY_SNORM
:
296 case GL_INTENSITY8_SNORM
:
297 case GL_INTENSITY16_SNORM
:
304 switch (internalFormat
) {
305 case GL_DEPTH_STENCIL
:
306 case GL_DEPTH24_STENCIL8
:
307 return GL_DEPTH_STENCIL
;
312 if (ctx
->Extensions
.EXT_texture_sRGB
) {
313 switch (internalFormat
) {
316 case GL_COMPRESSED_SRGB_EXT
:
318 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
319 return ctx
->Extensions
.EXT_texture_compression_s3tc
? GL_RGB
: -1;
320 case GL_SRGB_ALPHA_EXT
:
321 case GL_SRGB8_ALPHA8_EXT
:
322 case GL_COMPRESSED_SRGB_ALPHA_EXT
:
324 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
325 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
326 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
327 return ctx
->Extensions
.EXT_texture_compression_s3tc
? GL_RGBA
: -1;
328 case GL_SLUMINANCE_ALPHA_EXT
:
329 case GL_SLUMINANCE8_ALPHA8_EXT
:
330 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT
:
331 return GL_LUMINANCE_ALPHA
;
332 case GL_SLUMINANCE_EXT
:
333 case GL_SLUMINANCE8_EXT
:
334 case GL_COMPRESSED_SLUMINANCE_EXT
:
341 if (ctx
->Version
>= 30 ||
342 ctx
->Extensions
.EXT_texture_integer
) {
343 switch (internalFormat
) {
345 case GL_RGBA16UI_EXT
:
346 case GL_RGBA32UI_EXT
:
362 if (ctx
->Extensions
.EXT_texture_integer
) {
363 switch (internalFormat
) {
364 case GL_ALPHA8UI_EXT
:
365 case GL_ALPHA16UI_EXT
:
366 case GL_ALPHA32UI_EXT
:
368 case GL_ALPHA16I_EXT
:
369 case GL_ALPHA32I_EXT
:
371 case GL_INTENSITY8UI_EXT
:
372 case GL_INTENSITY16UI_EXT
:
373 case GL_INTENSITY32UI_EXT
:
374 case GL_INTENSITY8I_EXT
:
375 case GL_INTENSITY16I_EXT
:
376 case GL_INTENSITY32I_EXT
:
378 case GL_LUMINANCE8UI_EXT
:
379 case GL_LUMINANCE16UI_EXT
:
380 case GL_LUMINANCE32UI_EXT
:
381 case GL_LUMINANCE8I_EXT
:
382 case GL_LUMINANCE16I_EXT
:
383 case GL_LUMINANCE32I_EXT
:
385 case GL_LUMINANCE_ALPHA8UI_EXT
:
386 case GL_LUMINANCE_ALPHA16UI_EXT
:
387 case GL_LUMINANCE_ALPHA32UI_EXT
:
388 case GL_LUMINANCE_ALPHA8I_EXT
:
389 case GL_LUMINANCE_ALPHA16I_EXT
:
390 case GL_LUMINANCE_ALPHA32I_EXT
:
391 return GL_LUMINANCE_ALPHA
;
397 if (ctx
->Extensions
.ARB_texture_rg
) {
398 switch (internalFormat
) {
401 if (!ctx
->Extensions
.ARB_texture_float
)
410 if (ctx
->Version
< 30 && !ctx
->Extensions
.EXT_texture_integer
)
416 case GL_COMPRESSED_RED
:
421 if (!ctx
->Extensions
.ARB_texture_float
)
430 if (ctx
->Version
< 30 && !ctx
->Extensions
.EXT_texture_integer
)
436 case GL_COMPRESSED_RG
:
443 if (ctx
->Extensions
.EXT_texture_shared_exponent
) {
444 switch (internalFormat
) {
452 if (ctx
->Extensions
.EXT_packed_float
) {
453 switch (internalFormat
) {
454 case GL_R11F_G11F_B10F_EXT
:
461 if (ctx
->Extensions
.ARB_depth_buffer_float
) {
462 switch (internalFormat
) {
463 case GL_DEPTH_COMPONENT32F
:
464 return GL_DEPTH_COMPONENT
;
465 case GL_DEPTH32F_STENCIL8
:
466 return GL_DEPTH_STENCIL
;
472 if (ctx
->Extensions
.ARB_texture_compression_rgtc
) {
473 switch (internalFormat
) {
474 case GL_COMPRESSED_RED_RGTC1
:
475 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
477 case GL_COMPRESSED_RG_RGTC2
:
478 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
485 if (ctx
->Extensions
.EXT_texture_compression_latc
) {
486 switch (internalFormat
) {
487 case GL_COMPRESSED_LUMINANCE_LATC1_EXT
:
488 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT
:
490 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT
:
491 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT
:
492 return GL_LUMINANCE_ALPHA
;
498 if (ctx
->Extensions
.ATI_texture_compression_3dc
) {
499 switch (internalFormat
) {
500 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI
:
501 return GL_LUMINANCE_ALPHA
;
507 if (ctx
->Extensions
.OES_compressed_ETC1_RGB8_texture
) {
508 switch (internalFormat
) {
509 case GL_ETC1_RGB8_OES
:
516 if (_mesa_is_gles3(ctx
) || ctx
->Extensions
.ARB_ES3_compatibility
) {
517 switch (internalFormat
) {
518 case GL_COMPRESSED_RGB8_ETC2
:
519 case GL_COMPRESSED_SRGB8_ETC2
:
521 case GL_COMPRESSED_RGBA8_ETC2_EAC
:
522 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC
:
523 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2
:
524 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2
:
526 case GL_COMPRESSED_R11_EAC
:
527 case GL_COMPRESSED_SIGNED_R11_EAC
:
529 case GL_COMPRESSED_RG11_EAC
:
530 case GL_COMPRESSED_SIGNED_RG11_EAC
:
537 if (ctx
->API
== API_OPENGLES
) {
538 switch (internalFormat
) {
539 case GL_PALETTE4_RGB8_OES
:
540 case GL_PALETTE4_R5_G6_B5_OES
:
541 case GL_PALETTE8_RGB8_OES
:
542 case GL_PALETTE8_R5_G6_B5_OES
:
544 case GL_PALETTE4_RGBA8_OES
:
545 case GL_PALETTE8_RGB5_A1_OES
:
546 case GL_PALETTE4_RGBA4_OES
:
547 case GL_PALETTE4_RGB5_A1_OES
:
548 case GL_PALETTE8_RGBA8_OES
:
549 case GL_PALETTE8_RGBA4_OES
:
556 return -1; /* error */
561 * For cube map faces, return a face index in [0,5].
562 * For other targets return 0;
565 _mesa_tex_target_to_face(GLenum target
)
567 if (_mesa_is_cube_face(target
))
568 return (GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
;
576 * Install gl_texture_image in a gl_texture_object according to the target
577 * and level parameters.
579 * \param tObj texture object.
580 * \param target texture target.
581 * \param level image level.
582 * \param texImage texture image.
585 set_tex_image(struct gl_texture_object
*tObj
,
586 GLenum target
, GLint level
,
587 struct gl_texture_image
*texImage
)
589 const GLuint face
= _mesa_tex_target_to_face(target
);
593 if (target
== GL_TEXTURE_RECTANGLE_NV
|| target
== GL_TEXTURE_EXTERNAL_OES
)
596 tObj
->Image
[face
][level
] = texImage
;
598 /* Set the 'back' pointer */
599 texImage
->TexObject
= tObj
;
600 texImage
->Level
= level
;
601 texImage
->Face
= face
;
606 * Allocate a texture image structure.
608 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
611 * \return a pointer to gl_texture_image struct with all fields initialized to
614 struct gl_texture_image
*
615 _mesa_new_texture_image( struct gl_context
*ctx
)
618 return CALLOC_STRUCT(gl_texture_image
);
623 * Free a gl_texture_image and associated data.
624 * This function is a fallback called via ctx->Driver.DeleteTextureImage().
626 * \param texImage texture image.
628 * Free the texture image structure and the associated image data.
631 _mesa_delete_texture_image(struct gl_context
*ctx
,
632 struct gl_texture_image
*texImage
)
634 /* Free texImage->Data and/or any other driver-specific texture
637 ASSERT(ctx
->Driver
.FreeTextureImageBuffer
);
638 ctx
->Driver
.FreeTextureImageBuffer( ctx
, texImage
);
644 * Test if a target is a proxy target.
646 * \param target texture target.
648 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
651 _mesa_is_proxy_texture(GLenum target
)
654 static const GLenum targets
[] = {
658 GL_PROXY_TEXTURE_CUBE_MAP
,
659 GL_PROXY_TEXTURE_RECTANGLE
,
660 GL_PROXY_TEXTURE_1D_ARRAY
,
661 GL_PROXY_TEXTURE_2D_ARRAY
,
662 GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
,
663 GL_PROXY_TEXTURE_2D_MULTISAMPLE
,
664 GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
667 * NUM_TEXTURE_TARGETS should match number of terms above, except there's no
668 * proxy for GL_TEXTURE_BUFFER and GL_TEXTURE_EXTERNAL_OES.
670 STATIC_ASSERT(NUM_TEXTURE_TARGETS
== Elements(targets
) + 2);
672 for (i
= 0; i
< Elements(targets
); ++i
)
673 if (target
== targets
[i
])
680 * Return the proxy target which corresponds to the given texture target
683 proxy_target(GLenum target
)
687 case GL_PROXY_TEXTURE_1D
:
688 return GL_PROXY_TEXTURE_1D
;
690 case GL_PROXY_TEXTURE_2D
:
691 return GL_PROXY_TEXTURE_2D
;
693 case GL_PROXY_TEXTURE_3D
:
694 return GL_PROXY_TEXTURE_3D
;
695 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
696 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
697 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
698 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
699 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
700 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
701 case GL_TEXTURE_CUBE_MAP_ARB
:
702 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
703 return GL_PROXY_TEXTURE_CUBE_MAP_ARB
;
704 case GL_TEXTURE_RECTANGLE_NV
:
705 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
706 return GL_PROXY_TEXTURE_RECTANGLE_NV
;
707 case GL_TEXTURE_1D_ARRAY_EXT
:
708 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
709 return GL_PROXY_TEXTURE_1D_ARRAY_EXT
;
710 case GL_TEXTURE_2D_ARRAY_EXT
:
711 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
712 return GL_PROXY_TEXTURE_2D_ARRAY_EXT
;
713 case GL_TEXTURE_CUBE_MAP_ARRAY
:
714 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
715 return GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
;
716 case GL_TEXTURE_2D_MULTISAMPLE
:
717 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
718 return GL_PROXY_TEXTURE_2D_MULTISAMPLE
;
719 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
720 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
721 return GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
;
723 _mesa_problem(NULL
, "unexpected target in proxy_target()");
730 * Return a pointer to the current texture object for the given target
731 * on the current texture unit.
732 * Note: all <target> error checking should have been done by this point.
734 struct gl_texture_object
*
735 _mesa_get_current_tex_object(struct gl_context
*ctx
, GLenum target
)
737 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
738 const GLboolean arrayTex
= ctx
->Extensions
.EXT_texture_array
;
742 return texUnit
->CurrentTex
[TEXTURE_1D_INDEX
];
743 case GL_PROXY_TEXTURE_1D
:
744 return ctx
->Texture
.ProxyTex
[TEXTURE_1D_INDEX
];
746 return texUnit
->CurrentTex
[TEXTURE_2D_INDEX
];
747 case GL_PROXY_TEXTURE_2D
:
748 return ctx
->Texture
.ProxyTex
[TEXTURE_2D_INDEX
];
750 return texUnit
->CurrentTex
[TEXTURE_3D_INDEX
];
751 case GL_PROXY_TEXTURE_3D
:
752 return ctx
->Texture
.ProxyTex
[TEXTURE_3D_INDEX
];
753 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
754 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
755 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
756 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
757 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
758 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
759 case GL_TEXTURE_CUBE_MAP_ARB
:
760 return ctx
->Extensions
.ARB_texture_cube_map
761 ? texUnit
->CurrentTex
[TEXTURE_CUBE_INDEX
] : NULL
;
762 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
763 return ctx
->Extensions
.ARB_texture_cube_map
764 ? ctx
->Texture
.ProxyTex
[TEXTURE_CUBE_INDEX
] : NULL
;
765 case GL_TEXTURE_CUBE_MAP_ARRAY
:
766 return ctx
->Extensions
.ARB_texture_cube_map_array
767 ? texUnit
->CurrentTex
[TEXTURE_CUBE_ARRAY_INDEX
] : NULL
;
768 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
769 return ctx
->Extensions
.ARB_texture_cube_map_array
770 ? ctx
->Texture
.ProxyTex
[TEXTURE_CUBE_ARRAY_INDEX
] : NULL
;
771 case GL_TEXTURE_RECTANGLE_NV
:
772 return ctx
->Extensions
.NV_texture_rectangle
773 ? texUnit
->CurrentTex
[TEXTURE_RECT_INDEX
] : NULL
;
774 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
775 return ctx
->Extensions
.NV_texture_rectangle
776 ? ctx
->Texture
.ProxyTex
[TEXTURE_RECT_INDEX
] : NULL
;
777 case GL_TEXTURE_1D_ARRAY_EXT
:
778 return arrayTex
? texUnit
->CurrentTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
779 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
780 return arrayTex
? ctx
->Texture
.ProxyTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
781 case GL_TEXTURE_2D_ARRAY_EXT
:
782 return arrayTex
? texUnit
->CurrentTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
783 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
784 return arrayTex
? ctx
->Texture
.ProxyTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
785 case GL_TEXTURE_BUFFER
:
786 return ctx
->API
== API_OPENGL_CORE
&&
787 ctx
->Extensions
.ARB_texture_buffer_object
?
788 texUnit
->CurrentTex
[TEXTURE_BUFFER_INDEX
] : NULL
;
789 case GL_TEXTURE_EXTERNAL_OES
:
790 return _mesa_is_gles(ctx
) && ctx
->Extensions
.OES_EGL_image_external
791 ? texUnit
->CurrentTex
[TEXTURE_EXTERNAL_INDEX
] : NULL
;
792 case GL_TEXTURE_2D_MULTISAMPLE
:
793 return ctx
->Extensions
.ARB_texture_multisample
794 ? texUnit
->CurrentTex
[TEXTURE_2D_MULTISAMPLE_INDEX
] : NULL
;
795 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
796 return ctx
->Extensions
.ARB_texture_multisample
797 ? ctx
->Texture
.ProxyTex
[TEXTURE_2D_MULTISAMPLE_INDEX
] : NULL
;
798 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
799 return ctx
->Extensions
.ARB_texture_multisample
800 ? texUnit
->CurrentTex
[TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
] : NULL
;
801 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
802 return ctx
->Extensions
.ARB_texture_multisample
803 ? ctx
->Texture
.ProxyTex
[TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
] : NULL
;
805 _mesa_problem(NULL
, "bad target in _mesa_get_current_tex_object()");
813 * Get a texture image pointer from a texture object, given a texture
814 * target and mipmap level. The target and level parameters should
815 * have already been error-checked.
817 * \param ctx GL context.
818 * \param texObj texture unit.
819 * \param target texture target.
820 * \param level image level.
822 * \return pointer to the texture image structure, or NULL on failure.
824 struct gl_texture_image
*
825 _mesa_select_tex_image(struct gl_context
*ctx
,
826 const struct gl_texture_object
*texObj
,
827 GLenum target
, GLint level
)
829 const GLuint face
= _mesa_tex_target_to_face(target
);
833 ASSERT(level
< MAX_TEXTURE_LEVELS
);
835 return texObj
->Image
[face
][level
];
840 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
841 * it and install it. Only return NULL if passed a bad parameter or run
844 struct gl_texture_image
*
845 _mesa_get_tex_image(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
846 GLenum target
, GLint level
)
848 struct gl_texture_image
*texImage
;
853 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
855 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
857 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture image allocation");
861 set_tex_image(texObj
, target
, level
, texImage
);
869 * Return pointer to the specified proxy texture image.
870 * Note that proxy textures are per-context, not per-texture unit.
871 * \return pointer to texture image or NULL if invalid target, invalid
872 * level, or out of memory.
874 static struct gl_texture_image
*
875 get_proxy_tex_image(struct gl_context
*ctx
, GLenum target
, GLint level
)
877 struct gl_texture_image
*texImage
;
884 case GL_PROXY_TEXTURE_1D
:
885 if (level
>= ctx
->Const
.MaxTextureLevels
)
887 texIndex
= TEXTURE_1D_INDEX
;
889 case GL_PROXY_TEXTURE_2D
:
890 if (level
>= ctx
->Const
.MaxTextureLevels
)
892 texIndex
= TEXTURE_2D_INDEX
;
894 case GL_PROXY_TEXTURE_3D
:
895 if (level
>= ctx
->Const
.Max3DTextureLevels
)
897 texIndex
= TEXTURE_3D_INDEX
;
899 case GL_PROXY_TEXTURE_CUBE_MAP
:
900 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
902 texIndex
= TEXTURE_CUBE_INDEX
;
904 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
907 texIndex
= TEXTURE_RECT_INDEX
;
909 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
910 if (level
>= ctx
->Const
.MaxTextureLevels
)
912 texIndex
= TEXTURE_1D_ARRAY_INDEX
;
914 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
915 if (level
>= ctx
->Const
.MaxTextureLevels
)
917 texIndex
= TEXTURE_2D_ARRAY_INDEX
;
919 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
920 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
922 texIndex
= TEXTURE_CUBE_ARRAY_INDEX
;
924 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
927 texIndex
= TEXTURE_2D_MULTISAMPLE_INDEX
;
929 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
932 texIndex
= TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
;
938 texImage
= ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
];
940 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
942 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
945 ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
] = texImage
;
946 /* Set the 'back' pointer */
947 texImage
->TexObject
= ctx
->Texture
.ProxyTex
[texIndex
];
954 * Get the maximum number of allowed mipmap levels.
956 * \param ctx GL context.
957 * \param target texture target.
959 * \return the maximum number of allowed mipmap levels for the given
960 * texture target, or zero if passed a bad target.
965 _mesa_max_texture_levels(struct gl_context
*ctx
, GLenum target
)
969 case GL_PROXY_TEXTURE_1D
:
971 case GL_PROXY_TEXTURE_2D
:
972 return ctx
->Const
.MaxTextureLevels
;
974 case GL_PROXY_TEXTURE_3D
:
975 return ctx
->Const
.Max3DTextureLevels
;
976 case GL_TEXTURE_CUBE_MAP
:
977 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
978 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
979 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
980 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
981 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
982 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
983 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
984 return ctx
->Extensions
.ARB_texture_cube_map
985 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
986 case GL_TEXTURE_RECTANGLE_NV
:
987 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
988 return ctx
->Extensions
.NV_texture_rectangle
? 1 : 0;
989 case GL_TEXTURE_1D_ARRAY_EXT
:
990 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
991 case GL_TEXTURE_2D_ARRAY_EXT
:
992 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
993 return ctx
->Extensions
.EXT_texture_array
994 ? ctx
->Const
.MaxTextureLevels
: 0;
995 case GL_TEXTURE_CUBE_MAP_ARRAY
:
996 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
997 return ctx
->Extensions
.ARB_texture_cube_map_array
998 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
999 case GL_TEXTURE_BUFFER
:
1000 return ctx
->API
== API_OPENGL_CORE
&&
1001 ctx
->Extensions
.ARB_texture_buffer_object
? 1 : 0;
1002 case GL_TEXTURE_2D_MULTISAMPLE
:
1003 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
1004 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1005 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1006 return _mesa_is_desktop_gl(ctx
)
1007 && ctx
->Extensions
.ARB_texture_multisample
1009 case GL_TEXTURE_EXTERNAL_OES
:
1012 return 0; /* bad target */
1018 * Return number of dimensions per mipmap level for the given texture target.
1021 _mesa_get_texture_dimensions(GLenum target
)
1025 case GL_PROXY_TEXTURE_1D
:
1028 case GL_TEXTURE_RECTANGLE
:
1029 case GL_TEXTURE_CUBE_MAP
:
1030 case GL_PROXY_TEXTURE_2D
:
1031 case GL_PROXY_TEXTURE_RECTANGLE
:
1032 case GL_PROXY_TEXTURE_CUBE_MAP
:
1033 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1034 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1035 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1036 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1037 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1038 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1039 case GL_TEXTURE_1D_ARRAY
:
1040 case GL_PROXY_TEXTURE_1D_ARRAY
:
1041 case GL_TEXTURE_EXTERNAL_OES
:
1042 case GL_TEXTURE_2D_MULTISAMPLE
:
1043 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
1046 case GL_PROXY_TEXTURE_3D
:
1047 case GL_TEXTURE_2D_ARRAY
:
1048 case GL_PROXY_TEXTURE_2D_ARRAY
:
1049 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1050 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1051 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1052 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1054 case GL_TEXTURE_BUFFER
:
1057 _mesa_problem(NULL
, "invalid target 0x%x in get_texture_dimensions()",
1065 * Check if a texture target can have more than one layer.
1068 _mesa_tex_target_is_layered(GLenum target
)
1072 case GL_PROXY_TEXTURE_1D
:
1074 case GL_PROXY_TEXTURE_2D
:
1075 case GL_TEXTURE_RECTANGLE
:
1076 case GL_PROXY_TEXTURE_RECTANGLE
:
1077 case GL_TEXTURE_2D_MULTISAMPLE
:
1078 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
1079 case GL_TEXTURE_BUFFER
:
1080 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1081 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1082 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1083 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1084 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1085 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1086 case GL_TEXTURE_EXTERNAL_OES
:
1090 case GL_PROXY_TEXTURE_3D
:
1091 case GL_TEXTURE_CUBE_MAP
:
1092 case GL_PROXY_TEXTURE_CUBE_MAP
:
1093 case GL_TEXTURE_1D_ARRAY
:
1094 case GL_PROXY_TEXTURE_1D_ARRAY
:
1095 case GL_TEXTURE_2D_ARRAY
:
1096 case GL_PROXY_TEXTURE_2D_ARRAY
:
1097 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1098 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1099 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1100 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1104 assert(!"Invalid texture target.");
1111 * Return the number of layers present in the given level of an array,
1112 * cubemap or 3D texture. If the texture is not layered return zero.
1115 _mesa_get_texture_layers(const struct gl_texture_object
*texObj
, GLint level
)
1117 assert(level
>= 0 && level
< MAX_TEXTURE_LEVELS
);
1119 switch (texObj
->Target
) {
1122 case GL_TEXTURE_RECTANGLE
:
1123 case GL_TEXTURE_2D_MULTISAMPLE
:
1124 case GL_TEXTURE_BUFFER
:
1125 case GL_TEXTURE_EXTERNAL_OES
:
1128 case GL_TEXTURE_CUBE_MAP
:
1131 case GL_TEXTURE_1D_ARRAY
: {
1132 struct gl_texture_image
*img
= texObj
->Image
[0][level
];
1133 return img
? img
->Height
: 0;
1137 case GL_TEXTURE_2D_ARRAY
:
1138 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1139 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
: {
1140 struct gl_texture_image
*img
= texObj
->Image
[0][level
];
1141 return img
? img
->Depth
: 0;
1145 assert(!"Invalid texture target.");
1152 * Return the maximum number of mipmap levels for the given target
1153 * and the dimensions.
1154 * The dimensions are expected not to include the border.
1157 _mesa_get_tex_max_num_levels(GLenum target
, GLsizei width
, GLsizei height
,
1164 case GL_TEXTURE_1D_ARRAY
:
1165 case GL_PROXY_TEXTURE_1D
:
1166 case GL_PROXY_TEXTURE_1D_ARRAY
:
1169 case GL_TEXTURE_CUBE_MAP
:
1170 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1171 case GL_PROXY_TEXTURE_CUBE_MAP
:
1172 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1176 case GL_TEXTURE_2D_ARRAY
:
1177 case GL_PROXY_TEXTURE_2D
:
1178 case GL_PROXY_TEXTURE_2D_ARRAY
:
1179 size
= MAX2(width
, height
);
1182 case GL_PROXY_TEXTURE_3D
:
1183 size
= MAX3(width
, height
, depth
);
1185 case GL_TEXTURE_RECTANGLE
:
1186 case GL_TEXTURE_EXTERNAL_OES
:
1187 case GL_TEXTURE_2D_MULTISAMPLE
:
1188 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1189 case GL_PROXY_TEXTURE_RECTANGLE
:
1190 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
1191 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1198 return _mesa_logbase2(size
) + 1;
1202 #if 000 /* not used anymore */
1204 * glTexImage[123]D can accept a NULL image pointer. In this case we
1205 * create a texture image with unspecified image contents per the OpenGL
1209 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
1211 const GLint components
= _mesa_components_in_format(format
);
1212 const GLint numPixels
= width
* height
* depth
;
1213 GLubyte
*data
= (GLubyte
*) malloc(numPixels
* components
* sizeof(GLubyte
));
1217 * Let's see if anyone finds this. If glTexImage2D() is called with
1218 * a NULL image pointer then load the texture image with something
1219 * interesting instead of leaving it indeterminate.
1222 static const char message
[8][32] = {
1223 " X X XXXXX XXX X ",
1224 " XX XX X X X X X ",
1226 " X X XXXX XXX XXXXX ",
1229 " X X XXXXX XXX X X ",
1233 GLubyte
*imgPtr
= data
;
1235 for (h
= 0; h
< depth
; h
++) {
1236 for (i
= 0; i
< height
; i
++) {
1237 GLint srcRow
= 7 - (i
% 8);
1238 for (j
= 0; j
< width
; j
++) {
1239 GLint srcCol
= j
% 32;
1240 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
1241 for (k
= 0; k
< components
; k
++) {
1257 * Set the size and format-related fields of a gl_texture_image struct
1258 * to zero. This is used when a proxy texture test fails.
1261 clear_teximage_fields(struct gl_texture_image
*img
)
1264 img
->_BaseFormat
= 0;
1265 img
->InternalFormat
= 0;
1274 img
->HeightLog2
= 0;
1276 img
->TexFormat
= MESA_FORMAT_NONE
;
1277 img
->NumSamples
= 0;
1278 img
->FixedSampleLocations
= GL_TRUE
;
1283 * Initialize basic fields of the gl_texture_image struct.
1285 * \param ctx GL context.
1286 * \param img texture image structure to be initialized.
1287 * \param width image width.
1288 * \param height image height.
1289 * \param depth image depth.
1290 * \param border image border.
1291 * \param internalFormat internal format.
1292 * \param format the actual hardware format (one of MESA_FORMAT_*)
1294 * Fills in the fields of \p img with the given information.
1295 * Note: width, height and depth include the border.
1298 _mesa_init_teximage_fields(struct gl_context
*ctx
,
1299 struct gl_texture_image
*img
,
1300 GLsizei width
, GLsizei height
, GLsizei depth
,
1301 GLint border
, GLenum internalFormat
,
1307 ASSERT(height
>= 0);
1310 target
= img
->TexObject
->Target
;
1311 img
->_BaseFormat
= _mesa_base_tex_format( ctx
, internalFormat
);
1312 ASSERT(img
->_BaseFormat
> 0);
1313 img
->InternalFormat
= internalFormat
;
1314 img
->Border
= border
;
1316 img
->Height
= height
;
1319 img
->Width2
= width
- 2 * border
; /* == 1 << img->WidthLog2; */
1320 img
->WidthLog2
= _mesa_logbase2(img
->Width2
);
1322 img
->NumSamples
= 0;
1323 img
->FixedSampleLocations
= GL_TRUE
;
1327 case GL_TEXTURE_BUFFER
:
1328 case GL_PROXY_TEXTURE_1D
:
1333 img
->HeightLog2
= 0;
1340 case GL_TEXTURE_1D_ARRAY
:
1341 case GL_PROXY_TEXTURE_1D_ARRAY
:
1342 img
->Height2
= height
; /* no border */
1343 img
->HeightLog2
= 0; /* not used */
1351 case GL_TEXTURE_RECTANGLE
:
1352 case GL_TEXTURE_CUBE_MAP
:
1353 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1354 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1355 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1356 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1357 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1358 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1359 case GL_TEXTURE_EXTERNAL_OES
:
1360 case GL_PROXY_TEXTURE_2D
:
1361 case GL_PROXY_TEXTURE_RECTANGLE
:
1362 case GL_PROXY_TEXTURE_CUBE_MAP
:
1363 case GL_TEXTURE_2D_MULTISAMPLE
:
1364 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
1365 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1366 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1373 case GL_TEXTURE_2D_ARRAY
:
1374 case GL_PROXY_TEXTURE_2D_ARRAY
:
1375 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1376 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1377 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1378 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1379 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1380 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1381 img
->Depth2
= depth
; /* no border */
1382 img
->DepthLog2
= 0; /* not used */
1385 case GL_PROXY_TEXTURE_3D
:
1386 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1387 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1388 img
->Depth2
= depth
- 2 * border
; /* == 1 << img->DepthLog2; */
1389 img
->DepthLog2
= _mesa_logbase2(img
->Depth2
);
1392 _mesa_problem(NULL
, "invalid target 0x%x in _mesa_init_teximage_fields()",
1397 _mesa_get_tex_max_num_levels(target
,
1398 img
->Width2
, img
->Height2
, img
->Depth2
);
1399 img
->TexFormat
= format
;
1404 * Free and clear fields of the gl_texture_image struct.
1406 * \param ctx GL context.
1407 * \param texImage texture image structure to be cleared.
1409 * After the call, \p texImage will have no data associated with it. Its
1410 * fields are cleared so that its parent object will test incomplete.
1413 _mesa_clear_texture_image(struct gl_context
*ctx
,
1414 struct gl_texture_image
*texImage
)
1416 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
1417 clear_teximage_fields(texImage
);
1422 * Check the width, height, depth and border of a texture image are legal.
1423 * Used by all the glTexImage, glCompressedTexImage and glCopyTexImage
1425 * The target and level parameters will have already been validated.
1426 * \return GL_TRUE if size is OK, GL_FALSE otherwise.
1429 _mesa_legal_texture_dimensions(struct gl_context
*ctx
, GLenum target
,
1430 GLint level
, GLint width
, GLint height
,
1431 GLint depth
, GLint border
)
1437 case GL_PROXY_TEXTURE_1D
:
1438 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1); /* level zero size */
1439 maxSize
>>= level
; /* level size */
1440 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1442 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1443 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1449 case GL_PROXY_TEXTURE_2D
:
1450 case GL_TEXTURE_2D_MULTISAMPLE
:
1451 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
1452 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1454 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1456 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1458 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1459 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1461 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1467 case GL_PROXY_TEXTURE_3D
:
1468 maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1470 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1472 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1474 if (depth
< 2 * border
|| depth
> 2 * border
+ maxSize
)
1476 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1477 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1479 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1481 if (depth
> 0 && !_mesa_is_pow_two(depth
- 2 * border
))
1486 case GL_TEXTURE_RECTANGLE_NV
:
1487 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1490 maxSize
= ctx
->Const
.MaxTextureRectSize
;
1491 if (width
< 0 || width
> maxSize
)
1493 if (height
< 0 || height
> maxSize
)
1497 case GL_TEXTURE_CUBE_MAP
:
1498 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1499 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1500 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1501 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1502 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1503 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1504 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
1505 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1507 if (width
!= height
)
1509 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1511 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1513 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1514 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1516 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1521 case GL_TEXTURE_1D_ARRAY_EXT
:
1522 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1523 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1525 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1527 if (height
< 1 || height
> ctx
->Const
.MaxArrayTextureLayers
)
1529 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1530 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1535 case GL_TEXTURE_2D_ARRAY_EXT
:
1536 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1537 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1538 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1539 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1541 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1543 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1545 if (depth
< 0 || depth
> ctx
->Const
.MaxArrayTextureLayers
)
1547 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1548 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1550 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1555 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1556 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1557 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1558 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1560 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1562 if (depth
< 0 || depth
> ctx
->Const
.MaxArrayTextureLayers
|| depth
% 6)
1564 if (width
!= height
)
1566 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
1568 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1569 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1571 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1576 _mesa_problem(ctx
, "Invalid target in _mesa_legal_texture_dimensions()");
1583 * Do error checking of xoffset, yoffset, zoffset, width, height and depth
1584 * for glTexSubImage, glCopyTexSubImage and glCompressedTexSubImage.
1585 * \param destImage the destination texture image.
1586 * \return GL_TRUE if error found, GL_FALSE otherwise.
1589 error_check_subtexture_dimensions(struct gl_context
*ctx
,
1590 const char *function
, GLuint dims
,
1591 const struct gl_texture_image
*destImage
,
1592 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1593 GLsizei subWidth
, GLsizei subHeight
,
1596 const GLenum target
= destImage
->TexObject
->Target
;
1601 _mesa_error(ctx
, GL_INVALID_VALUE
,
1602 "%s%dD(width=%d)", function
, dims
, subWidth
);
1606 if (dims
> 1 && subHeight
< 0) {
1607 _mesa_error(ctx
, GL_INVALID_VALUE
,
1608 "%s%dD(height=%d)", function
, dims
, subHeight
);
1612 if (dims
> 2 && subDepth
< 0) {
1613 _mesa_error(ctx
, GL_INVALID_VALUE
,
1614 "%s%dD(depth=%d)", function
, dims
, subDepth
);
1618 /* check xoffset and width */
1619 if (xoffset
< - (GLint
) destImage
->Border
) {
1620 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(xoffset)",
1625 if (xoffset
+ subWidth
> (GLint
) destImage
->Width
) {
1626 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(xoffset+width)",
1631 /* check yoffset and height */
1633 GLint yBorder
= (target
== GL_TEXTURE_1D_ARRAY
) ? 0 : destImage
->Border
;
1634 if (yoffset
< -yBorder
) {
1635 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(yoffset)",
1639 if (yoffset
+ subHeight
> (GLint
) destImage
->Height
) {
1640 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(yoffset+height)",
1646 /* check zoffset and depth */
1648 GLint zBorder
= (target
== GL_TEXTURE_2D_ARRAY
) ? 0 : destImage
->Border
;
1649 if (zoffset
< -zBorder
) {
1650 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s3D(zoffset)", function
);
1653 if (zoffset
+ subDepth
> (GLint
) destImage
->Depth
) {
1654 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s3D(zoffset+depth)", function
);
1660 * The OpenGL spec (and GL_ARB_texture_compression) says only whole
1661 * compressed texture images can be updated. But, that restriction may be
1662 * relaxed for particular compressed formats. At this time, all the
1663 * compressed formats supported by Mesa allow sub-textures to be updated
1664 * along compressed block boundaries.
1666 _mesa_get_format_block_size(destImage
->TexFormat
, &bw
, &bh
);
1668 if (bw
!= 1 || bh
!= 1) {
1669 /* offset must be multiple of block size */
1670 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0)) {
1671 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1672 "%s%dD(xoffset = %d, yoffset = %d)",
1673 function
, dims
, xoffset
, yoffset
);
1677 /* The size must be a multiple of bw x bh, or we must be using a
1678 * offset+size that exactly hits the edge of the image. This
1679 * is important for small mipmap levels (1x1, 2x1, etc) and for
1682 if ((subWidth
% bw
!= 0) &&
1683 (xoffset
+ subWidth
!= (GLint
) destImage
->Width
)) {
1684 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1685 "%s%dD(width = %d)", function
, dims
, subWidth
);
1689 if ((subHeight
% bh
!= 0) &&
1690 (yoffset
+ subHeight
!= (GLint
) destImage
->Height
)) {
1691 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1692 "%s%dD(height = %d)", function
, dims
, subHeight
);
1704 * This is the fallback for Driver.TestProxyTexImage() for doing device-
1705 * specific texture image size checks.
1707 * A hardware driver might override this function if, for example, the
1708 * max 3D texture size is 512x512x64 (i.e. not a cube).
1710 * Note that width, height, depth == 0 is not an error. However, a
1711 * texture with zero width/height/depth will be considered "incomplete"
1712 * and texturing will effectively be disabled.
1714 * \param target any texture target/type
1715 * \param level as passed to glTexImage
1716 * \param format the MESA_FORMAT_x for the tex image
1717 * \param width as passed to glTexImage
1718 * \param height as passed to glTexImage
1719 * \param depth as passed to glTexImage
1720 * \param border as passed to glTexImage
1721 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1724 _mesa_test_proxy_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1726 GLint width
, GLint height
, GLint depth
, GLint border
)
1728 /* We just check if the image size is less than MaxTextureMbytes.
1729 * Some drivers may do more specific checks.
1731 uint64_t bytes
= _mesa_format_image_size64(format
, width
, height
, depth
);
1732 uint64_t mbytes
= bytes
/ (1024 * 1024); /* convert to MB */
1733 mbytes
*= _mesa_num_tex_faces(target
);
1734 return mbytes
<= (uint64_t) ctx
->Const
.MaxTextureMbytes
;
1739 * Return true if the format is only valid for glCompressedTexImage.
1742 compressedteximage_only_format(const struct gl_context
*ctx
, GLenum format
)
1745 case GL_ETC1_RGB8_OES
:
1746 case GL_PALETTE4_RGB8_OES
:
1747 case GL_PALETTE4_RGBA8_OES
:
1748 case GL_PALETTE4_R5_G6_B5_OES
:
1749 case GL_PALETTE4_RGBA4_OES
:
1750 case GL_PALETTE4_RGB5_A1_OES
:
1751 case GL_PALETTE8_RGB8_OES
:
1752 case GL_PALETTE8_RGBA8_OES
:
1753 case GL_PALETTE8_R5_G6_B5_OES
:
1754 case GL_PALETTE8_RGBA4_OES
:
1755 case GL_PALETTE8_RGB5_A1_OES
:
1764 * Helper function to determine whether a target and specific compression
1765 * format are supported.
1768 target_can_be_compressed(const struct gl_context
*ctx
, GLenum target
,
1771 (void) intFormat
; /* not used yet */
1775 case GL_PROXY_TEXTURE_2D
:
1776 return GL_TRUE
; /* true for any compressed format so far */
1777 case GL_PROXY_TEXTURE_CUBE_MAP
:
1778 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1779 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1780 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1781 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1782 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1783 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1784 return ctx
->Extensions
.ARB_texture_cube_map
;
1785 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1786 case GL_TEXTURE_2D_ARRAY_EXT
:
1787 return ctx
->Extensions
.EXT_texture_array
;
1788 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1789 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1790 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1798 * Check if the given texture target value is legal for a
1799 * glTexImage1/2/3D call.
1802 legal_teximage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
1808 case GL_PROXY_TEXTURE_1D
:
1809 return _mesa_is_desktop_gl(ctx
);
1817 case GL_PROXY_TEXTURE_2D
:
1818 return _mesa_is_desktop_gl(ctx
);
1819 case GL_PROXY_TEXTURE_CUBE_MAP
:
1820 return _mesa_is_desktop_gl(ctx
)
1821 && ctx
->Extensions
.ARB_texture_cube_map
;
1822 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1823 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1824 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1825 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1826 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1827 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1828 return ctx
->Extensions
.ARB_texture_cube_map
;
1829 case GL_TEXTURE_RECTANGLE_NV
:
1830 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1831 return _mesa_is_desktop_gl(ctx
)
1832 && ctx
->Extensions
.NV_texture_rectangle
;
1833 case GL_TEXTURE_1D_ARRAY_EXT
:
1834 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1835 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1843 case GL_PROXY_TEXTURE_3D
:
1844 return _mesa_is_desktop_gl(ctx
);
1845 case GL_TEXTURE_2D_ARRAY_EXT
:
1846 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1847 || _mesa_is_gles3(ctx
);
1848 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1849 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1850 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1851 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1852 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1857 _mesa_problem(ctx
, "invalid dims=%u in legal_teximage_target()", dims
);
1864 * Check if the given texture target value is legal for a
1865 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1866 * The difference compared to legal_teximage_target() above is that
1867 * proxy targets are not supported.
1870 legal_texsubimage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
1874 return _mesa_is_desktop_gl(ctx
) && target
== GL_TEXTURE_1D
;
1879 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1880 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1881 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1882 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1883 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1884 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1885 return ctx
->Extensions
.ARB_texture_cube_map
;
1886 case GL_TEXTURE_RECTANGLE_NV
:
1887 return _mesa_is_desktop_gl(ctx
)
1888 && ctx
->Extensions
.NV_texture_rectangle
;
1889 case GL_TEXTURE_1D_ARRAY_EXT
:
1890 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1898 case GL_TEXTURE_2D_ARRAY_EXT
:
1899 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1900 || _mesa_is_gles3(ctx
);
1901 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1902 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1903 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1908 _mesa_problem(ctx
, "invalid dims=%u in legal_texsubimage_target()",
1916 * Helper function to determine if a texture object is mutable (in terms
1917 * of GL_ARB_texture_storage).
1920 mutable_tex_object(struct gl_context
*ctx
, GLenum target
)
1922 struct gl_texture_object
*texObj
= _mesa_get_current_tex_object(ctx
, target
);
1923 return !texObj
->Immutable
;
1928 * Return expected size of a compressed texture.
1931 compressed_tex_size(GLsizei width
, GLsizei height
, GLsizei depth
,
1934 mesa_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
1935 return _mesa_format_image_size(mesaFormat
, width
, height
, depth
);
1939 * Verify that a texture format is valid with a particular target
1941 * In particular, textures with base format of \c GL_DEPTH_COMPONENT or
1942 * \c GL_DEPTH_STENCIL are only valid with certain, context dependent texture
1945 * \param ctx GL context
1946 * \param target Texture target
1947 * \param internalFormat Internal format of the texture image
1948 * \param dimensions Dimensionality at the caller. This is \b not used
1949 * in the validation. It is only used when logging
1951 * \param caller Base name of the calling function (e.g.,
1952 * "glTexImage" or "glTexStorage").
1954 * \returns true if the combination is legal, false otherwise.
1957 _mesa_legal_texture_base_format_for_target(struct gl_context
*ctx
,
1958 GLenum target
, GLenum internalFormat
,
1959 unsigned dimensions
,
1962 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
1963 || _mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_STENCIL
) {
1964 /* Section 3.8.3 (Texture Image Specification) of the OpenGL 3.3 Core
1965 * Profile spec says:
1967 * "Textures with a base internal format of DEPTH_COMPONENT or
1968 * DEPTH_STENCIL are supported by texture image specification
1969 * commands only if target is TEXTURE_1D, TEXTURE_2D,
1970 * TEXTURE_1D_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_RECTANGLE,
1971 * TEXTURE_CUBE_MAP, PROXY_TEXTURE_1D, PROXY_TEXTURE_2D,
1972 * PROXY_TEXTURE_1D_ARRAY, PROXY_TEXTURE_2D_ARRAY,
1973 * PROXY_TEXTURE_RECTANGLE, or PROXY_TEXTURE_CUBE_MAP. Using these
1974 * formats in conjunction with any other target will result in an
1975 * INVALID_OPERATION error."
1977 * Cubemaps are only supported with desktop OpenGL version >= 3.0,
1978 * EXT_gpu_shader4, or, on OpenGL ES 2.0+, OES_depth_texture_cube_map.
1980 if (target
!= GL_TEXTURE_1D
&&
1981 target
!= GL_PROXY_TEXTURE_1D
&&
1982 target
!= GL_TEXTURE_2D
&&
1983 target
!= GL_PROXY_TEXTURE_2D
&&
1984 target
!= GL_TEXTURE_1D_ARRAY
&&
1985 target
!= GL_PROXY_TEXTURE_1D_ARRAY
&&
1986 target
!= GL_TEXTURE_2D_ARRAY
&&
1987 target
!= GL_PROXY_TEXTURE_2D_ARRAY
&&
1988 target
!= GL_TEXTURE_RECTANGLE_ARB
&&
1989 target
!= GL_PROXY_TEXTURE_RECTANGLE_ARB
&&
1990 !((_mesa_is_cube_face(target
) ||
1991 target
== GL_TEXTURE_CUBE_MAP
||
1992 target
== GL_PROXY_TEXTURE_CUBE_MAP
) &&
1993 (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_gpu_shader4
1994 || (ctx
->API
== API_OPENGLES2
&& ctx
->Extensions
.OES_depth_texture_cube_map
))) &&
1995 !((target
== GL_TEXTURE_CUBE_MAP_ARRAY
||
1996 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
) &&
1997 ctx
->Extensions
.ARB_texture_cube_map_array
)) {
1998 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1999 "%s%dD(bad target for depth texture)",
2000 caller
, dimensions
);
2009 * Test the glTexImage[123]D() parameters for errors.
2011 * \param ctx GL context.
2012 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2013 * \param target texture target given by the user (already validated).
2014 * \param level image level given by the user.
2015 * \param internalFormat internal format given by the user.
2016 * \param format pixel data format given by the user.
2017 * \param type pixel data type given by the user.
2018 * \param width image width given by the user.
2019 * \param height image height given by the user.
2020 * \param depth image depth given by the user.
2021 * \param border image border given by the user.
2023 * \return GL_TRUE if a error is found, GL_FALSE otherwise
2025 * Verifies each of the parameters against the constants specified in
2026 * __struct gl_contextRec::Const and the supported extensions, and according
2027 * to the OpenGL specification.
2028 * Note that we don't fully error-check the width, height, depth values
2029 * here. That's done in _mesa_legal_texture_dimensions() which is used
2030 * by several other GL entrypoints. Plus, texture dims have a special
2031 * interaction with proxy textures.
2034 texture_error_check( struct gl_context
*ctx
,
2035 GLuint dimensions
, GLenum target
,
2036 GLint level
, GLint internalFormat
,
2037 GLenum format
, GLenum type
,
2038 GLint width
, GLint height
,
2039 GLint depth
, GLint border
)
2041 GLboolean colorFormat
;
2044 /* Even though there are no color-index textures, we still have to support
2045 * uploading color-index data and remapping it to RGB via the
2046 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
2048 const GLboolean indexFormat
= (format
== GL_COLOR_INDEX
);
2050 /* Note: for proxy textures, some error conditions immediately generate
2051 * a GL error in the usual way. But others do not generate a GL error.
2052 * Instead, they cause the width, height, depth, format fields of the
2053 * texture image to be zeroed-out. The GL spec seems to indicate that the
2054 * zero-out behaviour is only used in cases related to memory allocation.
2058 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2059 _mesa_error(ctx
, GL_INVALID_VALUE
,
2060 "glTexImage%dD(level=%d)", dimensions
, level
);
2065 if (border
< 0 || border
> 1 ||
2066 ((ctx
->API
!= API_OPENGL_COMPAT
||
2067 target
== GL_TEXTURE_RECTANGLE_NV
||
2068 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
2069 _mesa_error(ctx
, GL_INVALID_VALUE
,
2070 "glTexImage%dD(border=%d)", dimensions
, border
);
2074 if (width
< 0 || height
< 0 || depth
< 0) {
2075 _mesa_error(ctx
, GL_INVALID_VALUE
,
2076 "glTexImage%dD(width, height or depth < 0)", dimensions
);
2080 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2081 * combinations of format, internalFormat, and type that can be used.
2082 * Formats and types that require additional extensions (e.g., GL_FLOAT
2083 * requires GL_OES_texture_float) are filtered elsewhere.
2086 if (_mesa_is_gles(ctx
)) {
2087 if (_mesa_is_gles3(ctx
)) {
2088 err
= _mesa_es3_error_check_format_and_type(format
, type
,
2091 if (format
!= internalFormat
) {
2092 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2093 "glTexImage%dD(format = %s, internalFormat = %s)",
2095 _mesa_lookup_enum_by_nr(format
),
2096 _mesa_lookup_enum_by_nr(internalFormat
));
2100 err
= _mesa_es_error_check_format_and_type(format
, type
, dimensions
);
2102 if (err
!= GL_NO_ERROR
) {
2103 _mesa_error(ctx
, err
,
2104 "glTexImage%dD(format = %s, type = %s, internalFormat = %s)",
2106 _mesa_lookup_enum_by_nr(format
),
2107 _mesa_lookup_enum_by_nr(type
),
2108 _mesa_lookup_enum_by_nr(internalFormat
));
2113 /* Check internalFormat */
2114 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
2115 _mesa_error(ctx
, GL_INVALID_VALUE
,
2116 "glTexImage%dD(internalFormat=%s)",
2117 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
));
2121 /* Check incoming image format and type */
2122 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
2123 if (err
!= GL_NO_ERROR
) {
2124 _mesa_error(ctx
, err
,
2125 "glTexImage%dD(incompatible format = %s, type = %s)",
2126 dimensions
, _mesa_lookup_enum_by_nr(format
),
2127 _mesa_lookup_enum_by_nr(type
));
2131 /* make sure internal format and format basically agree */
2132 colorFormat
= _mesa_is_color_format(format
);
2133 if ((_mesa_is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
) ||
2134 (_mesa_is_depth_format(internalFormat
) != _mesa_is_depth_format(format
)) ||
2135 (_mesa_is_ycbcr_format(internalFormat
) != _mesa_is_ycbcr_format(format
)) ||
2136 (_mesa_is_depthstencil_format(internalFormat
) != _mesa_is_depthstencil_format(format
)) ||
2137 (_mesa_is_dudv_format(internalFormat
) != _mesa_is_dudv_format(format
))) {
2138 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2139 "glTexImage%dD(incompatible internalFormat = %s, format = %s)",
2140 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
),
2141 _mesa_lookup_enum_by_nr(format
));
2145 /* additional checks for ycbcr textures */
2146 if (internalFormat
== GL_YCBCR_MESA
) {
2147 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
2148 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
2149 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
2151 _mesa_snprintf(message
, sizeof(message
),
2152 "glTexImage%dD(format/type YCBCR mismatch)",
2154 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", message
);
2155 return GL_TRUE
; /* error */
2157 if (target
!= GL_TEXTURE_2D
&&
2158 target
!= GL_PROXY_TEXTURE_2D
&&
2159 target
!= GL_TEXTURE_RECTANGLE_NV
&&
2160 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
2161 _mesa_error(ctx
, GL_INVALID_ENUM
,
2162 "glTexImage%dD(bad target for YCbCr texture)",
2168 _mesa_snprintf(message
, sizeof(message
),
2169 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
2170 dimensions
, border
);
2171 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", message
);
2176 /* additional checks for depth textures */
2177 if (!_mesa_legal_texture_base_format_for_target(ctx
, target
, internalFormat
,
2178 dimensions
, "glTexImage"))
2181 /* additional checks for compressed textures */
2182 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
2183 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
2184 _mesa_error(ctx
, GL_INVALID_ENUM
,
2185 "glTexImage%dD(target can't be compressed)", dimensions
);
2188 if (compressedteximage_only_format(ctx
, internalFormat
)) {
2189 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2190 "glTexImage%dD(no compression for format)", dimensions
);
2194 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2195 "glTexImage%dD(border!=0)", dimensions
);
2200 /* additional checks for integer textures */
2201 if ((ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) &&
2202 (_mesa_is_enum_format_integer(format
) !=
2203 _mesa_is_enum_format_integer(internalFormat
))) {
2204 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2205 "glTexImage%dD(integer/non-integer format mismatch)",
2210 if (!mutable_tex_object(ctx
, target
)) {
2211 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2212 "glTexImage%dD(immutable texture)", dimensions
);
2216 /* if we get here, the parameters are OK */
2222 * Error checking for glCompressedTexImage[123]D().
2223 * Note that the width, height and depth values are not fully error checked
2225 * \return GL_TRUE if a error is found, GL_FALSE otherwise
2228 compressed_texture_error_check(struct gl_context
*ctx
, GLint dimensions
,
2229 GLenum target
, GLint level
,
2230 GLenum internalFormat
, GLsizei width
,
2231 GLsizei height
, GLsizei depth
, GLint border
,
2234 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
2236 GLenum error
= GL_NO_ERROR
;
2237 char *reason
= ""; /* no error */
2239 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
2241 error
= GL_INVALID_ENUM
;
2245 /* This will detect any invalid internalFormat value */
2246 if (!_mesa_is_compressed_format(ctx
, internalFormat
)) {
2247 _mesa_error(ctx
, GL_INVALID_ENUM
,
2248 "glCompressedTexImage%dD(internalFormat=%s)",
2249 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
));
2253 switch (internalFormat
) {
2254 case GL_PALETTE4_RGB8_OES
:
2255 case GL_PALETTE4_RGBA8_OES
:
2256 case GL_PALETTE4_R5_G6_B5_OES
:
2257 case GL_PALETTE4_RGBA4_OES
:
2258 case GL_PALETTE4_RGB5_A1_OES
:
2259 case GL_PALETTE8_RGB8_OES
:
2260 case GL_PALETTE8_RGBA8_OES
:
2261 case GL_PALETTE8_R5_G6_B5_OES
:
2262 case GL_PALETTE8_RGBA4_OES
:
2263 case GL_PALETTE8_RGB5_A1_OES
:
2264 /* check level (note that level should be zero or less!) */
2265 if (level
> 0 || level
< -maxLevels
) {
2267 error
= GL_INVALID_VALUE
;
2271 if (dimensions
!= 2) {
2272 reason
= "compressed paletted textures must be 2D";
2273 error
= GL_INVALID_OPERATION
;
2277 /* Figure out the expected texture size (in bytes). This will be
2278 * checked against the actual size later.
2280 expectedSize
= _mesa_cpal_compressed_size(level
, internalFormat
,
2283 /* This is for the benefit of the TestProxyTexImage below. It expects
2284 * level to be non-negative. OES_compressed_paletted_texture uses a
2285 * weird mechanism where the level specified to glCompressedTexImage2D
2286 * is -(n-1) number of levels in the texture, and the data specifies the
2287 * complete mipmap stack. This is done to ensure the palette is the
2288 * same for all levels.
2295 if (level
< 0 || level
>= maxLevels
) {
2297 error
= GL_INVALID_VALUE
;
2301 /* Figure out the expected texture size (in bytes). This will be
2302 * checked against the actual size later.
2304 expectedSize
= compressed_tex_size(width
, height
, depth
, internalFormat
);
2308 /* This should really never fail */
2309 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
2310 reason
= "internalFormat";
2311 error
= GL_INVALID_ENUM
;
2315 /* No compressed formats support borders at this time */
2317 reason
= "border != 0";
2318 error
= GL_INVALID_VALUE
;
2322 /* check image size in bytes */
2323 if (expectedSize
!= imageSize
) {
2324 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
2325 * if <imageSize> is not consistent with the format, dimensions, and
2326 * contents of the specified image.
2328 reason
= "imageSize inconsistant with width/height/format";
2329 error
= GL_INVALID_VALUE
;
2333 if (!mutable_tex_object(ctx
, target
)) {
2334 reason
= "immutable texture";
2335 error
= GL_INVALID_OPERATION
;
2342 /* Note: not all error paths exit through here. */
2343 _mesa_error(ctx
, error
, "glCompressedTexImage%dD(%s)", dimensions
, reason
);
2350 * Test glTexSubImage[123]D() parameters for errors.
2352 * \param ctx GL context.
2353 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2354 * \param target texture target given by the user (already validated)
2355 * \param level image level given by the user.
2356 * \param xoffset sub-image x offset given by the user.
2357 * \param yoffset sub-image y offset given by the user.
2358 * \param zoffset sub-image z offset given by the user.
2359 * \param format pixel data format given by the user.
2360 * \param type pixel data type given by the user.
2361 * \param width image width given by the user.
2362 * \param height image height given by the user.
2363 * \param depth image depth given by the user.
2365 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2367 * Verifies each of the parameters against the constants specified in
2368 * __struct gl_contextRec::Const and the supported extensions, and according
2369 * to the OpenGL specification.
2372 texsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2373 GLenum target
, GLint level
,
2374 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2375 GLint width
, GLint height
, GLint depth
,
2376 GLenum format
, GLenum type
)
2378 struct gl_texture_object
*texObj
;
2379 struct gl_texture_image
*texImage
;
2382 /* check target (proxies not allowed) */
2383 if (!legal_texsubimage_target(ctx
, dimensions
, target
)) {
2384 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage%uD(target=%s)",
2385 dimensions
, _mesa_lookup_enum_by_nr(target
));
2390 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2391 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage%uD(level=%d)",
2396 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2397 * combinations of format and type that can be used. Formats and types
2398 * that require additional extensions (e.g., GL_FLOAT requires
2399 * GL_OES_texture_float) are filtered elsewhere.
2401 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
2402 err
= _mesa_es_error_check_format_and_type(format
, type
, dimensions
);
2403 if (err
!= GL_NO_ERROR
) {
2404 _mesa_error(ctx
, err
,
2405 "glTexSubImage%dD(format = %s, type = %s)",
2407 _mesa_lookup_enum_by_nr(format
),
2408 _mesa_lookup_enum_by_nr(type
));
2413 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
2414 if (err
!= GL_NO_ERROR
) {
2415 _mesa_error(ctx
, err
,
2416 "glTexSubImage%dD(incompatible format = %s, type = %s)",
2417 dimensions
, _mesa_lookup_enum_by_nr(format
),
2418 _mesa_lookup_enum_by_nr(type
));
2422 /* Get dest texture object / image pointers */
2423 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2425 /* must be out of memory */
2426 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage%dD()", dimensions
);
2430 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2432 /* non-existant texture level */
2433 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2434 "glTexSubImage%dD(invalid texture image)", dimensions
);
2438 if (error_check_subtexture_dimensions(ctx
, "glTexSubImage", dimensions
,
2439 texImage
, xoffset
, yoffset
, 0,
2440 width
, height
, 1)) {
2444 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2445 if (compressedteximage_only_format(ctx
, texImage
->InternalFormat
)) {
2446 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2447 "glTexSubImage%dD(no compression for format)", dimensions
);
2452 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
2453 /* both source and dest must be integer-valued, or neither */
2454 if (_mesa_is_format_integer_color(texImage
->TexFormat
) !=
2455 _mesa_is_enum_format_integer(format
)) {
2456 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2457 "glTexSubImage%dD(integer/non-integer format mismatch)",
2468 * Test glCopyTexImage[12]D() parameters for errors.
2470 * \param ctx GL context.
2471 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2472 * \param target texture target given by the user.
2473 * \param level image level given by the user.
2474 * \param internalFormat internal format given by the user.
2475 * \param width image width given by the user.
2476 * \param height image height given by the user.
2477 * \param border texture border.
2479 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2481 * Verifies each of the parameters against the constants specified in
2482 * __struct gl_contextRec::Const and the supported extensions, and according
2483 * to the OpenGL specification.
2486 copytexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
2487 GLenum target
, GLint level
, GLint internalFormat
,
2488 GLint width
, GLint height
, GLint border
)
2491 GLint rb_base_format
;
2492 struct gl_renderbuffer
*rb
;
2493 GLenum rb_internal_format
;
2496 if (!legal_texsubimage_target(ctx
, dimensions
, target
)) {
2497 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexImage%uD(target=%s)",
2498 dimensions
, _mesa_lookup_enum_by_nr(target
));
2503 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2504 _mesa_error(ctx
, GL_INVALID_VALUE
,
2505 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
2509 /* Check that the source buffer is complete */
2510 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2511 if (ctx
->ReadBuffer
->_Status
== 0) {
2512 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2514 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2515 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2516 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
2520 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2521 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2522 "glCopyTexImage%dD(multisample FBO)",
2529 if (border
< 0 || border
> 1 ||
2530 ((ctx
->API
!= API_OPENGL_COMPAT
||
2531 target
== GL_TEXTURE_RECTANGLE_NV
||
2532 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
2533 _mesa_error(ctx
, GL_INVALID_VALUE
,
2534 "glCopyTexImage%dD(border=%d)", dimensions
, border
);
2538 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, internalFormat
);
2540 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2541 "glCopyTexImage%dD(read buffer)", dimensions
);
2545 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2548 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
2549 switch (internalFormat
) {
2554 case GL_LUMINANCE_ALPHA
:
2557 _mesa_error(ctx
, GL_INVALID_VALUE
,
2558 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2559 _mesa_lookup_enum_by_nr(internalFormat
));
2564 baseFormat
= _mesa_base_tex_format(ctx
, internalFormat
);
2565 if (baseFormat
< 0) {
2566 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2567 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2568 _mesa_lookup_enum_by_nr(internalFormat
));
2572 rb_internal_format
= rb
->InternalFormat
;
2573 rb_base_format
= _mesa_base_tex_format(ctx
, rb
->InternalFormat
);
2574 if (_mesa_is_color_format(internalFormat
)) {
2575 if (rb_base_format
< 0) {
2576 _mesa_error(ctx
, GL_INVALID_VALUE
,
2577 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2578 _mesa_lookup_enum_by_nr(internalFormat
));
2583 if (_mesa_is_gles(ctx
)) {
2585 if (_mesa_base_format_component_count(baseFormat
) >
2586 _mesa_base_format_component_count(rb_base_format
)) {
2589 if (baseFormat
== GL_DEPTH_COMPONENT
||
2590 baseFormat
== GL_DEPTH_STENCIL
||
2591 rb_base_format
== GL_DEPTH_COMPONENT
||
2592 rb_base_format
== GL_DEPTH_STENCIL
||
2593 ((baseFormat
== GL_LUMINANCE_ALPHA
||
2594 baseFormat
== GL_ALPHA
) &&
2595 rb_base_format
!= GL_RGBA
) ||
2596 internalFormat
== GL_RGB9_E5
) {
2599 if (internalFormat
== GL_RGB9_E5
) {
2603 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2604 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2605 _mesa_lookup_enum_by_nr(internalFormat
));
2610 if (_mesa_is_gles3(ctx
)) {
2611 bool rb_is_srgb
= false;
2612 bool dst_is_srgb
= false;
2614 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&&
2615 _mesa_get_format_color_encoding(rb
->Format
) == GL_SRGB
) {
2619 if (_mesa_get_linear_internalformat(internalFormat
) != internalFormat
) {
2623 if (rb_is_srgb
!= dst_is_srgb
) {
2624 /* Page 137 (page 149 of the PDF) in section 3.8.5 of the
2625 * OpenGLES 3.0.0 spec says:
2627 * "The error INVALID_OPERATION is also generated if the
2628 * value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the
2629 * framebuffer attachment corresponding to the read buffer
2630 * is LINEAR (see section 6.1.13) and internalformat is
2631 * one of the sRGB formats described in section 3.8.16, or
2632 * if the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING is
2633 * SRGB and internalformat is not one of the sRGB formats."
2635 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2636 "glCopyTexImage%dD(srgb usage mismatch)", dimensions
);
2641 if (!_mesa_source_buffer_exists(ctx
, baseFormat
)) {
2642 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2643 "glCopyTexImage%dD(missing readbuffer)", dimensions
);
2647 /* From the EXT_texture_integer spec:
2649 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2650 * if the texture internalformat is an integer format and the read color
2651 * buffer is not an integer format, or if the internalformat is not an
2652 * integer format and the read color buffer is an integer format."
2654 if (_mesa_is_color_format(internalFormat
)) {
2655 bool is_int
= _mesa_is_enum_format_integer(internalFormat
);
2656 bool is_rbint
= _mesa_is_enum_format_integer(rb_internal_format
);
2657 if (is_int
|| is_rbint
) {
2658 if (is_int
!= is_rbint
) {
2659 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2660 "glCopyTexImage%dD(integer vs non-integer)", dimensions
);
2662 } else if (_mesa_is_gles(ctx
) &&
2663 _mesa_is_enum_format_unsigned_int(internalFormat
) !=
2664 _mesa_is_enum_format_unsigned_int(rb_internal_format
)) {
2665 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2666 "glCopyTexImage%dD(signed vs unsigned integer)", dimensions
);
2672 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
2673 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
2674 _mesa_error(ctx
, GL_INVALID_ENUM
,
2675 "glCopyTexImage%dD(target)", dimensions
);
2678 if (compressedteximage_only_format(ctx
, internalFormat
)) {
2679 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2680 "glCopyTexImage%dD(no compression for format)", dimensions
);
2684 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2685 "glCopyTexImage%dD(border!=0)", dimensions
);
2690 if (!mutable_tex_object(ctx
, target
)) {
2691 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2692 "glCopyTexImage%dD(immutable texture)", dimensions
);
2696 /* if we get here, the parameters are OK */
2702 * Test glCopyTexSubImage[12]D() parameters for errors.
2703 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2706 copytexsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2707 GLenum target
, GLint level
,
2708 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2709 GLint width
, GLint height
)
2711 struct gl_texture_object
*texObj
;
2712 struct gl_texture_image
*texImage
;
2714 /* Check that the source buffer is complete */
2715 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2716 if (ctx
->ReadBuffer
->_Status
== 0) {
2717 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2719 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2720 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2721 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
2725 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2726 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2727 "glCopyTexSubImage%dD(multisample FBO)",
2733 /* check target (proxies not allowed) */
2734 if (!legal_texsubimage_target(ctx
, dimensions
, target
)) {
2735 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage%uD(target=%s)",
2736 dimensions
, _mesa_lookup_enum_by_nr(target
));
2741 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2742 _mesa_error(ctx
, GL_INVALID_VALUE
,
2743 "glCopyTexSubImage%dD(level=%d)", dimensions
, level
);
2747 /* Get dest texture object / image pointers */
2748 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2750 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%dD()", dimensions
);
2754 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2756 /* destination image does not exist */
2757 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2758 "glCopyTexSubImage%dD(invalid texture image)", dimensions
);
2762 if (error_check_subtexture_dimensions(ctx
, "glCopyTexSubImage",
2763 dimensions
, texImage
,
2764 xoffset
, yoffset
, zoffset
,
2765 width
, height
, 1)) {
2769 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2770 if (compressedteximage_only_format(ctx
, texImage
->InternalFormat
)) {
2771 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2772 "glCopyTexSubImage%dD(no compression for format)", dimensions
);
2777 if (texImage
->InternalFormat
== GL_YCBCR_MESA
) {
2778 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCopyTexSubImage2D");
2782 if (!_mesa_source_buffer_exists(ctx
, texImage
->_BaseFormat
)) {
2783 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2784 "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
2785 dimensions
, texImage
->_BaseFormat
);
2789 /* From the EXT_texture_integer spec:
2791 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2792 * if the texture internalformat is an integer format and the read color
2793 * buffer is not an integer format, or if the internalformat is not an
2794 * integer format and the read color buffer is an integer format."
2796 if (_mesa_is_color_format(texImage
->InternalFormat
)) {
2797 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
2799 if (_mesa_is_format_integer_color(rb
->Format
) !=
2800 _mesa_is_format_integer_color(texImage
->TexFormat
)) {
2801 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2802 "glCopyTexImage%dD(integer vs non-integer)", dimensions
);
2807 /* if we get here, the parameters are OK */
2812 /** Callback info for walking over FBO hash table */
2815 struct gl_context
*ctx
;
2816 struct gl_texture_object
*texObj
;
2822 * Check render to texture callback. Called from _mesa_HashWalk().
2825 check_rtt_cb(GLuint key
, void *data
, void *userData
)
2827 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
2828 const struct cb_info
*info
= (struct cb_info
*) userData
;
2829 struct gl_context
*ctx
= info
->ctx
;
2830 const struct gl_texture_object
*texObj
= info
->texObj
;
2831 const GLuint level
= info
->level
, face
= info
->face
;
2833 /* If this is a user-created FBO */
2834 if (_mesa_is_user_fbo(fb
)) {
2836 /* check if any of the FBO's attachments point to 'texObj' */
2837 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2838 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2839 if (att
->Type
== GL_TEXTURE
&&
2840 att
->Texture
== texObj
&&
2841 att
->TextureLevel
== level
&&
2842 att
->CubeMapFace
== face
) {
2843 _mesa_update_texture_renderbuffer(ctx
, ctx
->DrawBuffer
, att
);
2844 ASSERT(att
->Renderbuffer
->TexImage
);
2845 /* Mark fb status as indeterminate to force re-validation */
2854 * When a texture image is specified we have to check if it's bound to
2855 * any framebuffer objects (render to texture) in order to detect changes
2856 * in size or format since that effects FBO completeness.
2857 * Any FBOs rendering into the texture must be re-validated.
2860 _mesa_update_fbo_texture(struct gl_context
*ctx
,
2861 struct gl_texture_object
*texObj
,
2862 GLuint face
, GLuint level
)
2864 /* Only check this texture if it's been marked as RenderToTexture */
2865 if (texObj
->_RenderToTexture
) {
2866 struct cb_info info
;
2868 info
.texObj
= texObj
;
2871 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, check_rtt_cb
, &info
);
2877 * If the texture object's GenerateMipmap flag is set and we've
2878 * changed the texture base level image, regenerate the rest of the
2879 * mipmap levels now.
2882 check_gen_mipmap(struct gl_context
*ctx
, GLenum target
,
2883 struct gl_texture_object
*texObj
, GLint level
)
2885 ASSERT(target
!= GL_TEXTURE_CUBE_MAP
);
2886 if (texObj
->GenerateMipmap
&&
2887 level
== texObj
->BaseLevel
&&
2888 level
< texObj
->MaxLevel
) {
2889 ASSERT(ctx
->Driver
.GenerateMipmap
);
2890 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2895 /** Debug helper: override the user-requested internal format */
2897 override_internal_format(GLenum internalFormat
, GLint width
, GLint height
)
2900 if (internalFormat
== GL_RGBA16F_ARB
||
2901 internalFormat
== GL_RGBA32F_ARB
) {
2902 printf("Convert rgba float tex to int %d x %d\n", width
, height
);
2905 else if (internalFormat
== GL_RGB16F_ARB
||
2906 internalFormat
== GL_RGB32F_ARB
) {
2907 printf("Convert rgb float tex to int %d x %d\n", width
, height
);
2910 else if (internalFormat
== GL_LUMINANCE_ALPHA16F_ARB
||
2911 internalFormat
== GL_LUMINANCE_ALPHA32F_ARB
) {
2912 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2913 return GL_LUMINANCE_ALPHA
;
2915 else if (internalFormat
== GL_LUMINANCE16F_ARB
||
2916 internalFormat
== GL_LUMINANCE32F_ARB
) {
2917 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2918 return GL_LUMINANCE
;
2920 else if (internalFormat
== GL_ALPHA16F_ARB
||
2921 internalFormat
== GL_ALPHA32F_ARB
) {
2922 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2926 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2927 internalFormat = GL_RGBA;
2931 return internalFormat
;
2934 return internalFormat
;
2940 * Choose the actual hardware format for a texture image.
2941 * Try to use the same format as the previous image level when possible.
2942 * Otherwise, ask the driver for the best format.
2943 * It's important to try to choose a consistant format for all levels
2944 * for efficient texture memory layout/allocation. In particular, this
2945 * comes up during automatic mipmap generation.
2948 _mesa_choose_texture_format(struct gl_context
*ctx
,
2949 struct gl_texture_object
*texObj
,
2950 GLenum target
, GLint level
,
2951 GLenum internalFormat
, GLenum format
, GLenum type
)
2955 /* see if we've already chosen a format for the previous level */
2957 struct gl_texture_image
*prevImage
=
2958 _mesa_select_tex_image(ctx
, texObj
, target
, level
- 1);
2959 /* See if the prev level is defined and has an internal format which
2960 * matches the new internal format.
2963 prevImage
->Width
> 0 &&
2964 prevImage
->InternalFormat
== internalFormat
) {
2965 /* use the same format */
2966 ASSERT(prevImage
->TexFormat
!= MESA_FORMAT_NONE
);
2967 return prevImage
->TexFormat
;
2971 /* If the application requested compression to an S3TC format but we don't
2972 * have the DXTn library, force a generic compressed format instead.
2974 if (internalFormat
!= format
&& format
!= GL_NONE
) {
2975 const GLenum before
= internalFormat
;
2977 switch (internalFormat
) {
2978 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
2979 if (!ctx
->Mesa_DXTn
)
2980 internalFormat
= GL_COMPRESSED_RGB
;
2982 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
2983 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
2984 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
2985 if (!ctx
->Mesa_DXTn
)
2986 internalFormat
= GL_COMPRESSED_RGBA
;
2992 if (before
!= internalFormat
) {
2994 "DXT compression requested (%s), "
2995 "but libtxc_dxtn library not installed. Using %s "
2997 _mesa_lookup_enum_by_nr(before
),
2998 _mesa_lookup_enum_by_nr(internalFormat
));
3002 /* choose format from scratch */
3003 f
= ctx
->Driver
.ChooseTextureFormat(ctx
, texObj
->Target
, internalFormat
,
3005 ASSERT(f
!= MESA_FORMAT_NONE
);
3011 * Adjust pixel unpack params and image dimensions to strip off the
3012 * one-pixel texture border.
3014 * Gallium and intel don't support texture borders. They've seldem been used
3015 * and seldom been implemented correctly anyway.
3017 * \param unpackNew returns the new pixel unpack parameters
3020 strip_texture_border(GLenum target
,
3021 GLint
*width
, GLint
*height
, GLint
*depth
,
3022 const struct gl_pixelstore_attrib
*unpack
,
3023 struct gl_pixelstore_attrib
*unpackNew
)
3029 *unpackNew
= *unpack
;
3031 if (unpackNew
->RowLength
== 0)
3032 unpackNew
->RowLength
= *width
;
3034 if (unpackNew
->ImageHeight
== 0)
3035 unpackNew
->ImageHeight
= *height
;
3037 assert(*width
>= 3);
3038 unpackNew
->SkipPixels
++; /* skip the border */
3039 *width
= *width
- 2; /* reduce the width by two border pixels */
3041 /* The min height of a texture with a border is 3 */
3042 if (*height
>= 3 && target
!= GL_TEXTURE_1D_ARRAY
) {
3043 unpackNew
->SkipRows
++; /* skip the border */
3044 *height
= *height
- 2; /* reduce the height by two border pixels */
3048 target
!= GL_TEXTURE_2D_ARRAY
&&
3049 target
!= GL_TEXTURE_CUBE_MAP_ARRAY
) {
3050 unpackNew
->SkipImages
++; /* skip the border */
3051 *depth
= *depth
- 2; /* reduce the depth by two border pixels */
3057 * Common code to implement all the glTexImage1D/2D/3D functions
3058 * as well as glCompressedTexImage1D/2D/3D.
3059 * \param compressed only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
3060 * \param format the user's image format (only used if !compressed)
3061 * \param type the user's image type (only used if !compressed)
3062 * \param imageSize only used for glCompressedTexImage1D/2D/3D calls.
3065 teximage(struct gl_context
*ctx
, GLboolean compressed
, GLuint dims
,
3066 GLenum target
, GLint level
, GLint internalFormat
,
3067 GLsizei width
, GLsizei height
, GLsizei depth
,
3068 GLint border
, GLenum format
, GLenum type
,
3069 GLsizei imageSize
, const GLvoid
*pixels
)
3071 const char *func
= compressed
? "glCompressedTexImage" : "glTexImage";
3072 struct gl_pixelstore_attrib unpack_no_border
;
3073 const struct gl_pixelstore_attrib
*unpack
= &ctx
->Unpack
;
3074 struct gl_texture_object
*texObj
;
3075 mesa_format texFormat
;
3076 GLboolean dimensionsOK
, sizeOK
;
3078 FLUSH_VERTICES(ctx
, 0);
3080 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
)) {
3083 "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
3085 _mesa_lookup_enum_by_nr(target
), level
,
3086 _mesa_lookup_enum_by_nr(internalFormat
),
3087 width
, height
, depth
, border
, pixels
);
3090 "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
3092 _mesa_lookup_enum_by_nr(target
), level
,
3093 _mesa_lookup_enum_by_nr(internalFormat
),
3094 width
, height
, depth
, border
,
3095 _mesa_lookup_enum_by_nr(format
),
3096 _mesa_lookup_enum_by_nr(type
), pixels
);
3099 internalFormat
= override_internal_format(internalFormat
, width
, height
);
3101 /* target error checking */
3102 if (!legal_teximage_target(ctx
, dims
, target
)) {
3103 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s%uD(target=%s)",
3104 func
, dims
, _mesa_lookup_enum_by_nr(target
));
3108 /* general error checking */
3110 if (compressed_texture_error_check(ctx
, dims
, target
, level
,
3112 width
, height
, depth
,
3117 if (texture_error_check(ctx
, dims
, target
, level
, internalFormat
,
3118 format
, type
, width
, height
, depth
, border
))
3122 /* Here we convert a cpal compressed image into a regular glTexImage2D
3123 * call by decompressing the texture. If we really want to support cpal
3124 * textures in any driver this would have to be changed.
3126 if (ctx
->API
== API_OPENGLES
&& compressed
&& dims
== 2) {
3127 switch (internalFormat
) {
3128 case GL_PALETTE4_RGB8_OES
:
3129 case GL_PALETTE4_RGBA8_OES
:
3130 case GL_PALETTE4_R5_G6_B5_OES
:
3131 case GL_PALETTE4_RGBA4_OES
:
3132 case GL_PALETTE4_RGB5_A1_OES
:
3133 case GL_PALETTE8_RGB8_OES
:
3134 case GL_PALETTE8_RGBA8_OES
:
3135 case GL_PALETTE8_R5_G6_B5_OES
:
3136 case GL_PALETTE8_RGBA4_OES
:
3137 case GL_PALETTE8_RGB5_A1_OES
:
3138 _mesa_cpal_compressed_teximage2d(target
, level
, internalFormat
,
3139 width
, height
, imageSize
, pixels
);
3144 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3148 /* For glCompressedTexImage() the driver has no choice about the
3149 * texture format since we'll never transcode the user's compressed
3150 * image data. The internalFormat was error checked earlier.
3152 texFormat
= _mesa_glenum_to_compressed_format(internalFormat
);
3155 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3156 internalFormat
, format
, type
);
3159 assert(texFormat
!= MESA_FORMAT_NONE
);
3161 /* check that width, height, depth are legal for the mipmap level */
3162 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, level
, width
,
3163 height
, depth
, border
);
3165 /* check that the texture won't take too much memory, etc */
3166 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target(target
),
3168 width
, height
, depth
, border
);
3170 if (_mesa_is_proxy_texture(target
)) {
3171 /* Proxy texture: just clear or set state depending on error checking */
3172 struct gl_texture_image
*texImage
=
3173 get_proxy_tex_image(ctx
, target
, level
);
3176 return; /* GL_OUT_OF_MEMORY already recorded */
3178 if (dimensionsOK
&& sizeOK
) {
3179 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, depth
,
3180 border
, internalFormat
, texFormat
);
3183 clear_teximage_fields(texImage
);
3187 /* non-proxy target */
3188 const GLuint face
= _mesa_tex_target_to_face(target
);
3189 struct gl_texture_image
*texImage
;
3191 if (!dimensionsOK
) {
3192 _mesa_error(ctx
, GL_INVALID_VALUE
,
3193 "glTexImage%uD(invalid width or height or depth)",
3199 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3200 "glTexImage%uD(image too large)", dims
);
3204 /* Allow a hardware driver to just strip out the border, to provide
3205 * reliable but slightly incorrect hardware rendering instead of
3206 * rarely-tested software fallback rendering.
3208 if (border
&& ctx
->Const
.StripTextureBorder
) {
3209 strip_texture_border(target
, &width
, &height
, &depth
, unpack
,
3212 unpack
= &unpack_no_border
;
3215 if (ctx
->NewState
& _NEW_PIXEL
)
3216 _mesa_update_state(ctx
);
3218 _mesa_lock_texture(ctx
, texObj
);
3220 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3223 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s%uD", func
, dims
);
3226 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3228 _mesa_init_teximage_fields(ctx
, texImage
,
3229 width
, height
, depth
,
3230 border
, internalFormat
, texFormat
);
3232 /* Give the texture to the driver. <pixels> may be null. */
3233 if (width
> 0 && height
> 0 && depth
> 0) {
3235 ctx
->Driver
.CompressedTexImage(ctx
, dims
, texImage
,
3239 ctx
->Driver
.TexImage(ctx
, dims
, texImage
, format
,
3240 type
, pixels
, unpack
);
3244 check_gen_mipmap(ctx
, target
, texObj
, level
);
3246 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3248 _mesa_dirty_texobj(ctx
, texObj
);
3251 _mesa_unlock_texture(ctx
, texObj
);
3258 * Called from the API. Note that width includes the border.
3261 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
3262 GLsizei width
, GLint border
, GLenum format
,
3263 GLenum type
, const GLvoid
*pixels
)
3265 GET_CURRENT_CONTEXT(ctx
);
3266 teximage(ctx
, GL_FALSE
, 1, target
, level
, internalFormat
, width
, 1, 1,
3267 border
, format
, type
, 0, pixels
);
3272 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
3273 GLsizei width
, GLsizei height
, GLint border
,
3274 GLenum format
, GLenum type
,
3275 const GLvoid
*pixels
)
3277 GET_CURRENT_CONTEXT(ctx
);
3278 teximage(ctx
, GL_FALSE
, 2, target
, level
, internalFormat
, width
, height
, 1,
3279 border
, format
, type
, 0, pixels
);
3284 * Called by the API or display list executor.
3285 * Note that width and height include the border.
3288 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
3289 GLsizei width
, GLsizei height
, GLsizei depth
,
3290 GLint border
, GLenum format
, GLenum type
,
3291 const GLvoid
*pixels
)
3293 GET_CURRENT_CONTEXT(ctx
);
3294 teximage(ctx
, GL_FALSE
, 3, target
, level
, internalFormat
,
3295 width
, height
, depth
,
3296 border
, format
, type
, 0, pixels
);
3301 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
3302 GLsizei width
, GLsizei height
, GLsizei depth
,
3303 GLint border
, GLenum format
, GLenum type
,
3304 const GLvoid
*pixels
)
3306 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
3307 depth
, border
, format
, type
, pixels
);
3312 _mesa_EGLImageTargetTexture2DOES (GLenum target
, GLeglImageOES image
)
3314 struct gl_texture_object
*texObj
;
3315 struct gl_texture_image
*texImage
;
3317 GET_CURRENT_CONTEXT(ctx
);
3318 FLUSH_VERTICES(ctx
, 0);
3322 valid_target
= ctx
->Extensions
.OES_EGL_image
;
3324 case GL_TEXTURE_EXTERNAL_OES
:
3326 _mesa_is_gles(ctx
) ? ctx
->Extensions
.OES_EGL_image_external
: false;
3329 valid_target
= false;
3333 if (!valid_target
) {
3334 _mesa_error(ctx
, GL_INVALID_ENUM
,
3335 "glEGLImageTargetTexture2D(target=%d)", target
);
3340 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3341 "glEGLImageTargetTexture2D(image=%p)", image
);
3345 if (ctx
->NewState
& _NEW_PIXEL
)
3346 _mesa_update_state(ctx
);
3348 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3349 _mesa_lock_texture(ctx
, texObj
);
3351 if (texObj
->Immutable
) {
3352 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3353 "glEGLImageTargetTexture2D(texture is immutable)");
3354 _mesa_unlock_texture(ctx
, texObj
);
3358 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
3360 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glEGLImageTargetTexture2D");
3362 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3364 ctx
->Driver
.EGLImageTargetTexture2D(ctx
, target
,
3365 texObj
, texImage
, image
);
3367 _mesa_dirty_texobj(ctx
, texObj
);
3369 _mesa_unlock_texture(ctx
, texObj
);
3376 * Implement all the glTexSubImage1/2/3D() functions.
3379 texsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
3380 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3381 GLsizei width
, GLsizei height
, GLsizei depth
,
3382 GLenum format
, GLenum type
, const GLvoid
*pixels
)
3384 struct gl_texture_object
*texObj
;
3385 struct gl_texture_image
*texImage
;
3387 FLUSH_VERTICES(ctx
, 0);
3389 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3390 _mesa_debug(ctx
, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3392 _mesa_lookup_enum_by_nr(target
), level
,
3393 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3394 _mesa_lookup_enum_by_nr(format
),
3395 _mesa_lookup_enum_by_nr(type
), pixels
);
3397 /* check target (proxies not allowed) */
3398 if (!legal_texsubimage_target(ctx
, dims
, target
)) {
3399 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage%uD(target=%s)",
3400 dims
, _mesa_lookup_enum_by_nr(target
));
3404 if (ctx
->NewState
& _NEW_PIXEL
)
3405 _mesa_update_state(ctx
);
3407 if (texsubimage_error_check(ctx
, dims
, target
, level
,
3408 xoffset
, yoffset
, zoffset
,
3409 width
, height
, depth
, format
, type
)) {
3410 return; /* error was detected */
3413 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3415 _mesa_lock_texture(ctx
, texObj
);
3417 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3419 if (width
> 0 && height
> 0 && depth
> 0) {
3420 /* If we have a border, offset=-1 is legal. Bias by border width. */
3423 if (target
!= GL_TEXTURE_2D_ARRAY
)
3424 zoffset
+= texImage
->Border
;
3427 if (target
!= GL_TEXTURE_1D_ARRAY
)
3428 yoffset
+= texImage
->Border
;
3431 xoffset
+= texImage
->Border
;
3434 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3435 xoffset
, yoffset
, zoffset
,
3436 width
, height
, depth
,
3437 format
, type
, pixels
, &ctx
->Unpack
);
3439 check_gen_mipmap(ctx
, target
, texObj
, level
);
3441 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3442 * the texel data, not the texture format, size, etc.
3446 _mesa_unlock_texture(ctx
, texObj
);
3451 _mesa_TexSubImage1D( GLenum target
, GLint level
,
3452 GLint xoffset
, GLsizei width
,
3453 GLenum format
, GLenum type
,
3454 const GLvoid
*pixels
)
3456 GET_CURRENT_CONTEXT(ctx
);
3457 texsubimage(ctx
, 1, target
, level
,
3460 format
, type
, pixels
);
3465 _mesa_TexSubImage2D( GLenum target
, GLint level
,
3466 GLint xoffset
, GLint yoffset
,
3467 GLsizei width
, GLsizei height
,
3468 GLenum format
, GLenum type
,
3469 const GLvoid
*pixels
)
3471 GET_CURRENT_CONTEXT(ctx
);
3472 texsubimage(ctx
, 2, target
, level
,
3473 xoffset
, yoffset
, 0,
3475 format
, type
, pixels
);
3481 _mesa_TexSubImage3D( GLenum target
, GLint level
,
3482 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3483 GLsizei width
, GLsizei height
, GLsizei depth
,
3484 GLenum format
, GLenum type
,
3485 const GLvoid
*pixels
)
3487 GET_CURRENT_CONTEXT(ctx
);
3488 texsubimage(ctx
, 3, target
, level
,
3489 xoffset
, yoffset
, zoffset
,
3490 width
, height
, depth
,
3491 format
, type
, pixels
);
3497 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3498 * from. This depends on whether the texture contains color or depth values.
3500 static struct gl_renderbuffer
*
3501 get_copy_tex_image_source(struct gl_context
*ctx
, mesa_format texFormat
)
3503 if (_mesa_get_format_bits(texFormat
, GL_DEPTH_BITS
) > 0) {
3504 /* reading from depth/stencil buffer */
3505 return ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
3508 /* copying from color buffer */
3509 return ctx
->ReadBuffer
->_ColorReadBuffer
;
3514 copytexsubimage_by_slice(struct gl_context
*ctx
,
3515 struct gl_texture_image
*texImage
,
3517 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3518 struct gl_renderbuffer
*rb
,
3520 GLsizei width
, GLsizei height
)
3522 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
3525 /* For 1D arrays, we copy each scanline of the source rectangle into the
3528 assert(zoffset
== 0);
3530 for (slice
= 0; slice
< height
; slice
++) {
3531 assert(yoffset
+ slice
< texImage
->Height
);
3532 ctx
->Driver
.CopyTexSubImage(ctx
, 2, texImage
,
3533 xoffset
, 0, yoffset
+ slice
,
3534 rb
, x
, y
+ slice
, width
, 1);
3537 ctx
->Driver
.CopyTexSubImage(ctx
, dims
, texImage
,
3538 xoffset
, yoffset
, zoffset
,
3539 rb
, x
, y
, width
, height
);
3545 * Implement the glCopyTexImage1/2D() functions.
3548 copyteximage(struct gl_context
*ctx
, GLuint dims
,
3549 GLenum target
, GLint level
, GLenum internalFormat
,
3550 GLint x
, GLint y
, GLsizei width
, GLsizei height
, GLint border
)
3552 struct gl_texture_object
*texObj
;
3553 struct gl_texture_image
*texImage
;
3554 const GLuint face
= _mesa_tex_target_to_face(target
);
3555 mesa_format texFormat
;
3557 FLUSH_VERTICES(ctx
, 0);
3559 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3560 _mesa_debug(ctx
, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3562 _mesa_lookup_enum_by_nr(target
), level
,
3563 _mesa_lookup_enum_by_nr(internalFormat
),
3564 x
, y
, width
, height
, border
);
3566 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3567 _mesa_update_state(ctx
);
3569 if (copytexture_error_check(ctx
, dims
, target
, level
, internalFormat
,
3570 width
, height
, border
))
3573 if (!_mesa_legal_texture_dimensions(ctx
, target
, level
, width
, height
,
3575 _mesa_error(ctx
, GL_INVALID_VALUE
,
3576 "glCopyTexImage%uD(invalid width or height)", dims
);
3580 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3583 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3584 internalFormat
, GL_NONE
, GL_NONE
);
3585 assert(texFormat
!= MESA_FORMAT_NONE
);
3587 if (!ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target(target
),
3589 width
, height
, 1, border
)) {
3590 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3591 "glCopyTexImage%uD(image too large)", dims
);
3595 if (border
&& ctx
->Const
.StripTextureBorder
) {
3597 width
-= border
* 2;
3600 height
-= border
* 2;
3605 _mesa_lock_texture(ctx
, texObj
);
3607 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3610 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
3613 GLint srcX
= x
, srcY
= y
, dstX
= 0, dstY
= 0, dstZ
= 0;
3615 /* Free old texture image */
3616 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3618 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, 1,
3619 border
, internalFormat
, texFormat
);
3621 if (width
&& height
) {
3622 /* Allocate texture memory (no pixel data yet) */
3623 ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
);
3625 if (_mesa_clip_copytexsubimage(ctx
, &dstX
, &dstY
, &srcX
, &srcY
,
3627 struct gl_renderbuffer
*srcRb
=
3628 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3630 copytexsubimage_by_slice(ctx
, texImage
, dims
,
3632 srcRb
, srcX
, srcY
, width
, height
);
3635 check_gen_mipmap(ctx
, target
, texObj
, level
);
3638 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3640 _mesa_dirty_texobj(ctx
, texObj
);
3643 _mesa_unlock_texture(ctx
, texObj
);
3649 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
3650 GLenum internalFormat
,
3652 GLsizei width
, GLint border
)
3654 GET_CURRENT_CONTEXT(ctx
);
3655 copyteximage(ctx
, 1, target
, level
, internalFormat
, x
, y
, width
, 1, border
);
3661 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
3662 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
3665 GET_CURRENT_CONTEXT(ctx
);
3666 copyteximage(ctx
, 2, target
, level
, internalFormat
,
3667 x
, y
, width
, height
, border
);
3673 * Implementation for glCopyTexSubImage1/2/3D() functions.
3676 copytexsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
3677 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3678 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3680 struct gl_texture_object
*texObj
;
3681 struct gl_texture_image
*texImage
;
3683 FLUSH_VERTICES(ctx
, 0);
3685 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3686 _mesa_debug(ctx
, "glCopyTexSubImage%uD %s %d %d %d %d %d %d %d %d\n",
3688 _mesa_lookup_enum_by_nr(target
),
3689 level
, xoffset
, yoffset
, zoffset
, x
, y
, width
, height
);
3691 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3692 _mesa_update_state(ctx
);
3694 if (copytexsubimage_error_check(ctx
, dims
, target
, level
,
3695 xoffset
, yoffset
, zoffset
, width
, height
)) {
3699 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3701 _mesa_lock_texture(ctx
, texObj
);
3703 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3705 /* If we have a border, offset=-1 is legal. Bias by border width. */
3708 if (target
!= GL_TEXTURE_2D_ARRAY
)
3709 zoffset
+= texImage
->Border
;
3712 if (target
!= GL_TEXTURE_1D_ARRAY
)
3713 yoffset
+= texImage
->Border
;
3716 xoffset
+= texImage
->Border
;
3719 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3721 struct gl_renderbuffer
*srcRb
=
3722 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3724 copytexsubimage_by_slice(ctx
, texImage
, dims
,
3725 xoffset
, yoffset
, zoffset
,
3726 srcRb
, x
, y
, width
, height
);
3728 check_gen_mipmap(ctx
, target
, texObj
, level
);
3730 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3731 * the texel data, not the texture format, size, etc.
3735 _mesa_unlock_texture(ctx
, texObj
);
3740 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
3741 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
3743 GET_CURRENT_CONTEXT(ctx
);
3744 copytexsubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, x
, y
, width
, 1);
3750 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
3751 GLint xoffset
, GLint yoffset
,
3752 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3754 GET_CURRENT_CONTEXT(ctx
);
3755 copytexsubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0, x
, y
,
3762 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
3763 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3764 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3766 GET_CURRENT_CONTEXT(ctx
);
3767 copytexsubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
3768 x
, y
, width
, height
);
3774 /**********************************************************************/
3775 /****** Compressed Textures ******/
3776 /**********************************************************************/
3780 * Error checking for glCompressedTexSubImage[123]D().
3781 * \return error code or GL_NO_ERROR.
3784 compressed_subtexture_error_check(struct gl_context
*ctx
, GLint dims
,
3785 GLenum target
, GLint level
,
3786 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3787 GLsizei width
, GLsizei height
, GLsizei depth
,
3788 GLenum format
, GLsizei imageSize
)
3790 struct gl_texture_object
*texObj
;
3791 struct gl_texture_image
*texImage
;
3799 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
3800 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
3801 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
3802 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
3803 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
3804 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
3808 targetOK
= GL_FALSE
;
3813 targetOK
= (target
== GL_TEXTURE_2D_ARRAY
);
3817 /* no 1D compressed textures at this time */
3818 targetOK
= GL_FALSE
;
3823 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage%uD(target)",
3828 /* this will catch any invalid compressed format token */
3829 if (!_mesa_is_compressed_format(ctx
, format
)) {
3830 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage%uD(format)",
3835 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
3836 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexImage%uD(level=%d)",
3841 expectedSize
= compressed_tex_size(width
, height
, depth
, format
);
3842 if (expectedSize
!= imageSize
) {
3843 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexImage%uD(size=%d)",
3848 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3850 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3851 "glCompressedTexSubImage%uD()", dims
);
3855 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3857 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3858 "glCompressedTexSubImage%uD(invalid texture image)", dims
);
3862 if ((GLint
) format
!= texImage
->InternalFormat
) {
3863 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3864 "glCompressedTexSubImage%uD(format=0x%x)", dims
, format
);
3868 if (compressedteximage_only_format(ctx
, format
)) {
3869 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3870 "glCompressedTexSubImage%uD(format=0x%x cannot be updated)"
3875 if (error_check_subtexture_dimensions(ctx
, "glCompressedTexSubImage", dims
,
3876 texImage
, xoffset
, yoffset
, zoffset
,
3877 width
, height
, depth
)) {
3886 _mesa_CompressedTexImage1D(GLenum target
, GLint level
,
3887 GLenum internalFormat
, GLsizei width
,
3888 GLint border
, GLsizei imageSize
,
3891 GET_CURRENT_CONTEXT(ctx
);
3892 teximage(ctx
, GL_TRUE
, 1, target
, level
, internalFormat
,
3893 width
, 1, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
3898 _mesa_CompressedTexImage2D(GLenum target
, GLint level
,
3899 GLenum internalFormat
, GLsizei width
,
3900 GLsizei height
, GLint border
, GLsizei imageSize
,
3903 GET_CURRENT_CONTEXT(ctx
);
3904 teximage(ctx
, GL_TRUE
, 2, target
, level
, internalFormat
,
3905 width
, height
, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
3910 _mesa_CompressedTexImage3D(GLenum target
, GLint level
,
3911 GLenum internalFormat
, GLsizei width
,
3912 GLsizei height
, GLsizei depth
, GLint border
,
3913 GLsizei imageSize
, const GLvoid
*data
)
3915 GET_CURRENT_CONTEXT(ctx
);
3916 teximage(ctx
, GL_TRUE
, 3, target
, level
, internalFormat
,
3917 width
, height
, depth
, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
3922 * Common helper for glCompressedTexSubImage1/2/3D().
3925 compressed_tex_sub_image(GLuint dims
, GLenum target
, GLint level
,
3926 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3927 GLsizei width
, GLsizei height
, GLsizei depth
,
3928 GLenum format
, GLsizei imageSize
, const GLvoid
*data
)
3930 struct gl_texture_object
*texObj
;
3931 struct gl_texture_image
*texImage
;
3932 GET_CURRENT_CONTEXT(ctx
);
3933 FLUSH_VERTICES(ctx
, 0);
3935 if (compressed_subtexture_error_check(ctx
, dims
, target
, level
,
3936 xoffset
, yoffset
, zoffset
,
3937 width
, height
, depth
,
3938 format
, imageSize
)) {
3942 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3944 _mesa_lock_texture(ctx
, texObj
);
3946 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3949 if (width
> 0 && height
> 0 && depth
> 0) {
3950 ctx
->Driver
.CompressedTexSubImage(ctx
, dims
, texImage
,
3951 xoffset
, yoffset
, zoffset
,
3952 width
, height
, depth
,
3953 format
, imageSize
, data
);
3955 check_gen_mipmap(ctx
, target
, texObj
, level
);
3957 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3958 * the texel data, not the texture format, size, etc.
3962 _mesa_unlock_texture(ctx
, texObj
);
3967 _mesa_CompressedTexSubImage1D(GLenum target
, GLint level
, GLint xoffset
,
3968 GLsizei width
, GLenum format
,
3969 GLsizei imageSize
, const GLvoid
*data
)
3971 compressed_tex_sub_image(1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
3972 format
, imageSize
, data
);
3977 _mesa_CompressedTexSubImage2D(GLenum target
, GLint level
, GLint xoffset
,
3978 GLint yoffset
, GLsizei width
, GLsizei height
,
3979 GLenum format
, GLsizei imageSize
,
3982 compressed_tex_sub_image(2, target
, level
, xoffset
, yoffset
, 0,
3983 width
, height
, 1, format
, imageSize
, data
);
3988 _mesa_CompressedTexSubImage3D(GLenum target
, GLint level
, GLint xoffset
,
3989 GLint yoffset
, GLint zoffset
, GLsizei width
,
3990 GLsizei height
, GLsizei depth
, GLenum format
,
3991 GLsizei imageSize
, const GLvoid
*data
)
3993 compressed_tex_sub_image(3, target
, level
, xoffset
, yoffset
, zoffset
,
3994 width
, height
, depth
, format
, imageSize
, data
);
3998 get_texbuffer_format(const struct gl_context
*ctx
, GLenum internalFormat
)
4000 if (ctx
->API
!= API_OPENGL_CORE
) {
4001 switch (internalFormat
) {
4003 return MESA_FORMAT_A_UNORM8
;
4005 return MESA_FORMAT_A_UNORM16
;
4006 case GL_ALPHA16F_ARB
:
4007 return MESA_FORMAT_A_FLOAT16
;
4008 case GL_ALPHA32F_ARB
:
4009 return MESA_FORMAT_A_FLOAT32
;
4010 case GL_ALPHA8I_EXT
:
4011 return MESA_FORMAT_A_SINT8
;
4012 case GL_ALPHA16I_EXT
:
4013 return MESA_FORMAT_A_SINT16
;
4014 case GL_ALPHA32I_EXT
:
4015 return MESA_FORMAT_A_SINT32
;
4016 case GL_ALPHA8UI_EXT
:
4017 return MESA_FORMAT_A_UINT8
;
4018 case GL_ALPHA16UI_EXT
:
4019 return MESA_FORMAT_A_UINT16
;
4020 case GL_ALPHA32UI_EXT
:
4021 return MESA_FORMAT_A_UINT32
;
4023 return MESA_FORMAT_L_UNORM8
;
4024 case GL_LUMINANCE16
:
4025 return MESA_FORMAT_L_UNORM16
;
4026 case GL_LUMINANCE16F_ARB
:
4027 return MESA_FORMAT_L_FLOAT16
;
4028 case GL_LUMINANCE32F_ARB
:
4029 return MESA_FORMAT_L_FLOAT32
;
4030 case GL_LUMINANCE8I_EXT
:
4031 return MESA_FORMAT_L_SINT8
;
4032 case GL_LUMINANCE16I_EXT
:
4033 return MESA_FORMAT_L_SINT16
;
4034 case GL_LUMINANCE32I_EXT
:
4035 return MESA_FORMAT_L_SINT32
;
4036 case GL_LUMINANCE8UI_EXT
:
4037 return MESA_FORMAT_L_UINT8
;
4038 case GL_LUMINANCE16UI_EXT
:
4039 return MESA_FORMAT_L_UINT16
;
4040 case GL_LUMINANCE32UI_EXT
:
4041 return MESA_FORMAT_L_UINT32
;
4042 case GL_LUMINANCE8_ALPHA8
:
4043 return MESA_FORMAT_L8A8_UNORM
;
4044 case GL_LUMINANCE16_ALPHA16
:
4045 return MESA_FORMAT_L16A16_UNORM
;
4046 case GL_LUMINANCE_ALPHA16F_ARB
:
4047 return MESA_FORMAT_LA_FLOAT16
;
4048 case GL_LUMINANCE_ALPHA32F_ARB
:
4049 return MESA_FORMAT_LA_FLOAT32
;
4050 case GL_LUMINANCE_ALPHA8I_EXT
:
4051 return MESA_FORMAT_LA_SINT8
;
4052 case GL_LUMINANCE_ALPHA16I_EXT
:
4053 return MESA_FORMAT_LA_SINT8
;
4054 case GL_LUMINANCE_ALPHA32I_EXT
:
4055 return MESA_FORMAT_LA_SINT16
;
4056 case GL_LUMINANCE_ALPHA8UI_EXT
:
4057 return MESA_FORMAT_LA_UINT8
;
4058 case GL_LUMINANCE_ALPHA16UI_EXT
:
4059 return MESA_FORMAT_LA_UINT16
;
4060 case GL_LUMINANCE_ALPHA32UI_EXT
:
4061 return MESA_FORMAT_LA_UINT32
;
4063 return MESA_FORMAT_I_UNORM8
;
4064 case GL_INTENSITY16
:
4065 return MESA_FORMAT_I_UNORM16
;
4066 case GL_INTENSITY16F_ARB
:
4067 return MESA_FORMAT_I_FLOAT16
;
4068 case GL_INTENSITY32F_ARB
:
4069 return MESA_FORMAT_I_FLOAT32
;
4070 case GL_INTENSITY8I_EXT
:
4071 return MESA_FORMAT_I_SINT8
;
4072 case GL_INTENSITY16I_EXT
:
4073 return MESA_FORMAT_I_SINT16
;
4074 case GL_INTENSITY32I_EXT
:
4075 return MESA_FORMAT_I_SINT32
;
4076 case GL_INTENSITY8UI_EXT
:
4077 return MESA_FORMAT_I_UINT8
;
4078 case GL_INTENSITY16UI_EXT
:
4079 return MESA_FORMAT_I_UINT16
;
4080 case GL_INTENSITY32UI_EXT
:
4081 return MESA_FORMAT_I_UINT32
;
4087 if (ctx
->API
== API_OPENGL_CORE
&&
4088 ctx
->Extensions
.ARB_texture_buffer_object_rgb32
) {
4089 switch (internalFormat
) {
4091 return MESA_FORMAT_RGB_FLOAT32
;
4093 return MESA_FORMAT_RGB_UINT32
;
4095 return MESA_FORMAT_RGB_SINT32
;
4101 switch (internalFormat
) {
4103 return MESA_FORMAT_R8G8B8A8_UNORM
;
4105 return MESA_FORMAT_RGBA_UNORM16
;
4106 case GL_RGBA16F_ARB
:
4107 return MESA_FORMAT_RGBA_FLOAT16
;
4108 case GL_RGBA32F_ARB
:
4109 return MESA_FORMAT_RGBA_FLOAT32
;
4111 return MESA_FORMAT_RGBA_SINT8
;
4112 case GL_RGBA16I_EXT
:
4113 return MESA_FORMAT_RGBA_SINT16
;
4114 case GL_RGBA32I_EXT
:
4115 return MESA_FORMAT_RGBA_SINT32
;
4116 case GL_RGBA8UI_EXT
:
4117 return MESA_FORMAT_RGBA_UINT8
;
4118 case GL_RGBA16UI_EXT
:
4119 return MESA_FORMAT_RGBA_UINT16
;
4120 case GL_RGBA32UI_EXT
:
4121 return MESA_FORMAT_RGBA_UINT32
;
4124 return MESA_FORMAT_R8G8_UNORM
;
4126 return MESA_FORMAT_R16G16_UNORM
;
4128 return MESA_FORMAT_RG_FLOAT16
;
4130 return MESA_FORMAT_RG_FLOAT32
;
4132 return MESA_FORMAT_RG_SINT8
;
4134 return MESA_FORMAT_RG_SINT16
;
4136 return MESA_FORMAT_RG_SINT32
;
4138 return MESA_FORMAT_RG_UINT8
;
4140 return MESA_FORMAT_RG_UINT16
;
4142 return MESA_FORMAT_RG_UINT32
;
4145 return MESA_FORMAT_R_UNORM8
;
4147 return MESA_FORMAT_R_UNORM16
;
4149 return MESA_FORMAT_R_FLOAT16
;
4151 return MESA_FORMAT_R_FLOAT32
;
4153 return MESA_FORMAT_R_SINT8
;
4155 return MESA_FORMAT_R_SINT16
;
4157 return MESA_FORMAT_R_SINT32
;
4159 return MESA_FORMAT_R_UINT8
;
4161 return MESA_FORMAT_R_UINT16
;
4163 return MESA_FORMAT_R_UINT32
;
4166 return MESA_FORMAT_NONE
;
4172 _mesa_validate_texbuffer_format(const struct gl_context
*ctx
,
4173 GLenum internalFormat
)
4175 mesa_format format
= get_texbuffer_format(ctx
, internalFormat
);
4178 if (format
== MESA_FORMAT_NONE
)
4179 return MESA_FORMAT_NONE
;
4181 datatype
= _mesa_get_format_datatype(format
);
4183 /* The GL_ARB_texture_buffer_object spec says:
4185 * "If ARB_texture_float is not supported, references to the
4186 * floating-point internal formats provided by that extension should be
4187 * removed, and such formats may not be passed to TexBufferARB."
4189 * As a result, GL_HALF_FLOAT internal format depends on both
4190 * GL_ARB_texture_float and GL_ARB_half_float_pixel.
4192 if ((datatype
== GL_FLOAT
|| datatype
== GL_HALF_FLOAT
) &&
4193 !ctx
->Extensions
.ARB_texture_float
)
4194 return MESA_FORMAT_NONE
;
4196 if (!ctx
->Extensions
.ARB_texture_rg
) {
4197 GLenum base_format
= _mesa_get_format_base_format(format
);
4198 if (base_format
== GL_R
|| base_format
== GL_RG
)
4199 return MESA_FORMAT_NONE
;
4202 if (!ctx
->Extensions
.ARB_texture_buffer_object_rgb32
) {
4203 GLenum base_format
= _mesa_get_format_base_format(format
);
4204 if (base_format
== GL_RGB
)
4205 return MESA_FORMAT_NONE
;
4212 texbufferrange(struct gl_context
*ctx
, GLenum target
, GLenum internalFormat
,
4213 struct gl_buffer_object
*bufObj
,
4214 GLintptr offset
, GLsizeiptr size
)
4216 struct gl_texture_object
*texObj
;
4219 FLUSH_VERTICES(ctx
, 0);
4221 if (target
!= GL_TEXTURE_BUFFER_ARB
) {
4222 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexBuffer(target)");
4226 format
= _mesa_validate_texbuffer_format(ctx
, internalFormat
);
4227 if (format
== MESA_FORMAT_NONE
) {
4228 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexBuffer(internalFormat 0x%x)",
4233 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4235 _mesa_lock_texture(ctx
, texObj
);
4237 _mesa_reference_buffer_object(ctx
, &texObj
->BufferObject
, bufObj
);
4238 texObj
->BufferObjectFormat
= internalFormat
;
4239 texObj
->_BufferObjectFormat
= format
;
4240 texObj
->BufferOffset
= offset
;
4241 texObj
->BufferSize
= size
;
4243 _mesa_unlock_texture(ctx
, texObj
);
4247 /** GL_ARB_texture_buffer_object */
4249 _mesa_TexBuffer(GLenum target
, GLenum internalFormat
, GLuint buffer
)
4251 struct gl_buffer_object
*bufObj
;
4253 GET_CURRENT_CONTEXT(ctx
);
4255 /* NOTE: ARB_texture_buffer_object has interactions with
4256 * the compatibility profile that are not implemented.
4258 if (!(ctx
->API
== API_OPENGL_CORE
&&
4259 ctx
->Extensions
.ARB_texture_buffer_object
)) {
4260 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBuffer");
4264 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4265 if (!bufObj
&& buffer
) {
4266 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBuffer(buffer %u)", buffer
);
4270 texbufferrange(ctx
, target
, internalFormat
, bufObj
, 0, buffer
? -1 : 0);
4274 /** GL_ARB_texture_buffer_range */
4276 _mesa_TexBufferRange(GLenum target
, GLenum internalFormat
, GLuint buffer
,
4277 GLintptr offset
, GLsizeiptr size
)
4279 struct gl_buffer_object
*bufObj
;
4281 GET_CURRENT_CONTEXT(ctx
);
4283 if (!(ctx
->API
== API_OPENGL_CORE
&&
4284 ctx
->Extensions
.ARB_texture_buffer_range
)) {
4285 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBufferRange");
4289 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4293 (offset
+ size
) > bufObj
->Size
) {
4294 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexBufferRange");
4297 if (offset
% ctx
->Const
.TextureBufferOffsetAlignment
) {
4298 _mesa_error(ctx
, GL_INVALID_VALUE
,
4299 "glTexBufferRange(invalid offset alignment)");
4302 } else if (buffer
) {
4303 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBufferRange(buffer %u)",
4311 texbufferrange(ctx
, target
, internalFormat
, bufObj
, offset
, size
);
4316 is_renderable_texture_format(struct gl_context
*ctx
, GLenum internalformat
)
4318 /* Everything that is allowed for renderbuffers,
4319 * except for a base format of GL_STENCIL_INDEX.
4321 GLenum baseFormat
= _mesa_base_fbo_format(ctx
, internalformat
);
4322 return baseFormat
!= 0 && baseFormat
!= GL_STENCIL_INDEX
;
4326 /** GL_ARB_texture_multisample */
4328 check_multisample_target(GLuint dims
, GLenum target
)
4331 case GL_TEXTURE_2D_MULTISAMPLE
:
4332 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
4335 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
4336 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
4346 teximagemultisample(GLuint dims
, GLenum target
, GLsizei samples
,
4347 GLint internalformat
, GLsizei width
, GLsizei height
,
4348 GLsizei depth
, GLboolean fixedsamplelocations
,
4349 GLboolean immutable
, const char *func
)
4351 struct gl_texture_object
*texObj
;
4352 struct gl_texture_image
*texImage
;
4353 GLboolean sizeOK
, dimensionsOK
;
4354 mesa_format texFormat
;
4355 GLenum sample_count_error
;
4357 GET_CURRENT_CONTEXT(ctx
);
4359 if (!(ctx
->Extensions
.ARB_texture_multisample
4360 && _mesa_is_desktop_gl(ctx
))) {
4361 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unsupported)", func
);
4365 if (!check_multisample_target(dims
, target
)) {
4366 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
4370 /* check that the specified internalformat is color/depth/stencil-renderable;
4371 * refer GL3.1 spec 4.4.4
4374 if (immutable
&& !_mesa_is_legal_tex_storage_format(ctx
, internalformat
)) {
4375 _mesa_error(ctx
, GL_INVALID_ENUM
,
4376 "%s(internalformat=%s not legal for immutable-format)",
4377 func
, _mesa_lookup_enum_by_nr(internalformat
));
4381 if (!is_renderable_texture_format(ctx
, internalformat
)) {
4382 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4383 "%s(internalformat=%s)",
4384 func
, _mesa_lookup_enum_by_nr(internalformat
));
4388 sample_count_error
= _mesa_check_sample_count(ctx
, target
,
4389 internalformat
, samples
);
4390 if (sample_count_error
!= GL_NO_ERROR
) {
4391 _mesa_error(ctx
, sample_count_error
, "%s(samples)", func
);
4395 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4397 if (immutable
&& (!texObj
|| (texObj
->Name
== 0))) {
4398 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4399 "%s(texture object 0)",
4404 texImage
= _mesa_get_tex_image(ctx
, texObj
, 0, 0);
4406 if (texImage
== NULL
) {
4407 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", func
);
4411 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, 0,
4412 internalformat
, GL_NONE
, GL_NONE
);
4413 assert(texFormat
!= MESA_FORMAT_NONE
);
4415 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, 0,
4416 width
, height
, depth
, 0);
4418 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, target
, 0, texFormat
,
4419 width
, height
, depth
, 0);
4421 if (_mesa_is_proxy_texture(target
)) {
4422 if (dimensionsOK
&& sizeOK
) {
4423 _mesa_init_teximage_fields(ctx
, texImage
,
4424 width
, height
, depth
, 0, internalformat
, texFormat
);
4425 texImage
->NumSamples
= samples
;
4426 texImage
->FixedSampleLocations
= fixedsamplelocations
;
4429 /* clear all image fields */
4430 _mesa_init_teximage_fields(ctx
, texImage
,
4431 0, 0, 0, 0, GL_NONE
, MESA_FORMAT_NONE
);
4435 if (!dimensionsOK
) {
4436 _mesa_error(ctx
, GL_INVALID_VALUE
,
4437 "%s(invalid width or height)", func
);
4442 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
4443 "%s(texture too large)", func
);
4447 /* Check if texObj->Immutable is set */
4448 if (texObj
->Immutable
) {
4449 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
4453 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
4455 _mesa_init_teximage_fields(ctx
, texImage
,
4456 width
, height
, depth
, 0, internalformat
, texFormat
);
4458 texImage
->NumSamples
= samples
;
4459 texImage
->FixedSampleLocations
= fixedsamplelocations
;
4461 if (width
> 0 && height
> 0 && depth
> 0) {
4462 if (!ctx
->Driver
.AllocTextureStorage(ctx
, texObj
, 1,
4463 width
, height
, depth
)) {
4464 /* tidy up the texture image state. strictly speaking,
4465 * we're allowed to just leave this in whatever state we
4466 * like, but being tidy is good.
4468 _mesa_init_teximage_fields(ctx
, texImage
,
4469 0, 0, 0, 0, GL_NONE
, MESA_FORMAT_NONE
);
4473 texObj
->Immutable
= immutable
;
4476 _mesa_set_texture_view_state(ctx
, texObj
, target
, 1);
4479 _mesa_update_fbo_texture(ctx
, texObj
, 0, 0);
4485 _mesa_TexImage2DMultisample(GLenum target
, GLsizei samples
,
4486 GLenum internalformat
, GLsizei width
,
4487 GLsizei height
, GLboolean fixedsamplelocations
)
4489 teximagemultisample(2, target
, samples
, internalformat
,
4490 width
, height
, 1, fixedsamplelocations
, GL_FALSE
,
4491 "glTexImage2DMultisample");
4496 _mesa_TexImage3DMultisample(GLenum target
, GLsizei samples
,
4497 GLenum internalformat
, GLsizei width
,
4498 GLsizei height
, GLsizei depth
,
4499 GLboolean fixedsamplelocations
)
4501 teximagemultisample(3, target
, samples
, internalformat
,
4502 width
, height
, depth
, fixedsamplelocations
, GL_FALSE
,
4503 "glTexImage3DMultisample");
4508 _mesa_TexStorage2DMultisample(GLenum target
, GLsizei samples
,
4509 GLenum internalformat
, GLsizei width
,
4510 GLsizei height
, GLboolean fixedsamplelocations
)
4512 teximagemultisample(2, target
, samples
, internalformat
,
4513 width
, height
, 1, fixedsamplelocations
, GL_TRUE
,
4514 "glTexStorage2DMultisample");
4519 _mesa_TexStorage3DMultisample(GLenum target
, GLsizei samples
,
4520 GLenum internalformat
, GLsizei width
,
4521 GLsizei height
, GLsizei depth
,
4522 GLboolean fixedsamplelocations
)
4524 teximagemultisample(3, target
, samples
, internalformat
,
4525 width
, height
, depth
, fixedsamplelocations
, GL_TRUE
,
4526 "glTexStorage3DMultisample");