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"
58 * State changes which we care about for glCopyTex[Sub]Image() calls.
59 * In particular, we care about pixel transfer state and buffer state
60 * (such as glReadBuffer to make sure we read from the right renderbuffer).
62 #define NEW_COPY_TEX_STATE (_NEW_BUFFERS | _NEW_PIXEL)
67 * Return the simple base format for a given internal texture format.
68 * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
70 * \param ctx GL context.
71 * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
73 * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
74 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
76 * This is the format which is used during texture application (i.e. the
77 * texture format and env mode determine the arithmetic used.
80 _mesa_base_tex_format( struct gl_context
*ctx
, GLint internalFormat
)
82 switch (internalFormat
) {
88 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_ALPHA
: -1;
95 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_LUMINANCE
: -1;
97 case GL_LUMINANCE_ALPHA
:
98 case GL_LUMINANCE4_ALPHA4
:
99 case GL_LUMINANCE6_ALPHA2
:
100 case GL_LUMINANCE8_ALPHA8
:
101 case GL_LUMINANCE12_ALPHA4
:
102 case GL_LUMINANCE12_ALPHA12
:
103 case GL_LUMINANCE16_ALPHA16
:
104 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_LUMINANCE_ALPHA
: -1;
110 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_INTENSITY
: -1;
112 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_RGB
: -1;
123 return (ctx
->API
!= API_OPENGL_CORE
) ? GL_RGBA
: -1;
137 /* GL_BGRA can be an internal format *only* in OpenGL ES (1.x or 2.0).
139 if (_mesa_is_gles(ctx
)) {
140 switch (internalFormat
) {
148 if (ctx
->Extensions
.ARB_ES2_compatibility
) {
149 switch (internalFormat
) {
157 if (ctx
->Extensions
.ARB_depth_texture
) {
158 switch (internalFormat
) {
159 case GL_DEPTH_COMPONENT
:
160 case GL_DEPTH_COMPONENT16
:
161 case GL_DEPTH_COMPONENT24
:
162 case GL_DEPTH_COMPONENT32
:
163 return GL_DEPTH_COMPONENT
;
164 case GL_DEPTH_STENCIL
:
165 case GL_DEPTH24_STENCIL8
:
166 return GL_DEPTH_STENCIL
;
172 switch (internalFormat
) {
173 case GL_COMPRESSED_ALPHA
:
175 case GL_COMPRESSED_LUMINANCE
:
177 case GL_COMPRESSED_LUMINANCE_ALPHA
:
178 return GL_LUMINANCE_ALPHA
;
179 case GL_COMPRESSED_INTENSITY
:
181 case GL_COMPRESSED_RGB
:
183 case GL_COMPRESSED_RGBA
:
189 if (ctx
->Extensions
.TDFX_texture_compression_FXT1
) {
190 switch (internalFormat
) {
191 case GL_COMPRESSED_RGB_FXT1_3DFX
:
193 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
200 /* Assume that the ANGLE flag will always be set if the EXT flag is set.
202 if (ctx
->Extensions
.ANGLE_texture_compression_dxt
) {
203 switch (internalFormat
) {
204 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
206 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
207 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
208 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
215 if (_mesa_is_desktop_gl(ctx
)
216 && ctx
->Extensions
.ANGLE_texture_compression_dxt
) {
217 switch (internalFormat
) {
229 if (ctx
->Extensions
.MESA_ycbcr_texture
) {
230 if (internalFormat
== GL_YCBCR_MESA
)
231 return GL_YCBCR_MESA
;
234 if (ctx
->Extensions
.ARB_texture_float
) {
235 switch (internalFormat
) {
236 case GL_ALPHA16F_ARB
:
237 case GL_ALPHA32F_ARB
:
245 case GL_INTENSITY16F_ARB
:
246 case GL_INTENSITY32F_ARB
:
248 case GL_LUMINANCE16F_ARB
:
249 case GL_LUMINANCE32F_ARB
:
251 case GL_LUMINANCE_ALPHA16F_ARB
:
252 case GL_LUMINANCE_ALPHA32F_ARB
:
253 return GL_LUMINANCE_ALPHA
;
259 if (ctx
->Extensions
.EXT_texture_snorm
) {
260 switch (internalFormat
) {
275 case GL_RGBA16_SNORM
:
278 case GL_ALPHA8_SNORM
:
279 case GL_ALPHA16_SNORM
:
281 case GL_LUMINANCE_SNORM
:
282 case GL_LUMINANCE8_SNORM
:
283 case GL_LUMINANCE16_SNORM
:
285 case GL_LUMINANCE_ALPHA_SNORM
:
286 case GL_LUMINANCE8_ALPHA8_SNORM
:
287 case GL_LUMINANCE16_ALPHA16_SNORM
:
288 return GL_LUMINANCE_ALPHA
;
289 case GL_INTENSITY_SNORM
:
290 case GL_INTENSITY8_SNORM
:
291 case GL_INTENSITY16_SNORM
:
298 if (ctx
->Extensions
.EXT_texture_sRGB
) {
299 switch (internalFormat
) {
302 case GL_COMPRESSED_SRGB_EXT
:
304 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
305 return ctx
->Extensions
.EXT_texture_compression_s3tc
? GL_RGB
: -1;
306 case GL_SRGB_ALPHA_EXT
:
307 case GL_SRGB8_ALPHA8_EXT
:
308 case GL_COMPRESSED_SRGB_ALPHA_EXT
:
310 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
311 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
312 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
313 return ctx
->Extensions
.EXT_texture_compression_s3tc
? GL_RGBA
: -1;
314 case GL_SLUMINANCE_ALPHA_EXT
:
315 case GL_SLUMINANCE8_ALPHA8_EXT
:
316 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT
:
317 return GL_LUMINANCE_ALPHA
;
318 case GL_SLUMINANCE_EXT
:
319 case GL_SLUMINANCE8_EXT
:
320 case GL_COMPRESSED_SLUMINANCE_EXT
:
327 if (ctx
->Version
>= 30 ||
328 ctx
->Extensions
.EXT_texture_integer
) {
329 switch (internalFormat
) {
331 case GL_RGBA16UI_EXT
:
332 case GL_RGBA32UI_EXT
:
348 if (ctx
->Extensions
.EXT_texture_integer
) {
349 switch (internalFormat
) {
350 case GL_ALPHA8UI_EXT
:
351 case GL_ALPHA16UI_EXT
:
352 case GL_ALPHA32UI_EXT
:
354 case GL_ALPHA16I_EXT
:
355 case GL_ALPHA32I_EXT
:
357 case GL_INTENSITY8UI_EXT
:
358 case GL_INTENSITY16UI_EXT
:
359 case GL_INTENSITY32UI_EXT
:
360 case GL_INTENSITY8I_EXT
:
361 case GL_INTENSITY16I_EXT
:
362 case GL_INTENSITY32I_EXT
:
364 case GL_LUMINANCE8UI_EXT
:
365 case GL_LUMINANCE16UI_EXT
:
366 case GL_LUMINANCE32UI_EXT
:
367 case GL_LUMINANCE8I_EXT
:
368 case GL_LUMINANCE16I_EXT
:
369 case GL_LUMINANCE32I_EXT
:
371 case GL_LUMINANCE_ALPHA8UI_EXT
:
372 case GL_LUMINANCE_ALPHA16UI_EXT
:
373 case GL_LUMINANCE_ALPHA32UI_EXT
:
374 case GL_LUMINANCE_ALPHA8I_EXT
:
375 case GL_LUMINANCE_ALPHA16I_EXT
:
376 case GL_LUMINANCE_ALPHA32I_EXT
:
377 return GL_LUMINANCE_ALPHA
;
383 if (ctx
->Extensions
.ARB_texture_rg
) {
384 switch (internalFormat
) {
387 if (!ctx
->Extensions
.ARB_texture_float
)
396 if (ctx
->Version
< 30 && !ctx
->Extensions
.EXT_texture_integer
)
402 case GL_COMPRESSED_RED
:
407 if (!ctx
->Extensions
.ARB_texture_float
)
416 if (ctx
->Version
< 30 && !ctx
->Extensions
.EXT_texture_integer
)
422 case GL_COMPRESSED_RG
:
429 if (ctx
->Extensions
.EXT_texture_shared_exponent
) {
430 switch (internalFormat
) {
438 if (ctx
->Extensions
.EXT_packed_float
) {
439 switch (internalFormat
) {
440 case GL_R11F_G11F_B10F_EXT
:
447 if (ctx
->Extensions
.ARB_depth_buffer_float
) {
448 switch (internalFormat
) {
449 case GL_DEPTH_COMPONENT32F
:
450 return GL_DEPTH_COMPONENT
;
451 case GL_DEPTH32F_STENCIL8
:
452 return GL_DEPTH_STENCIL
;
458 if (ctx
->Extensions
.ARB_texture_compression_rgtc
) {
459 switch (internalFormat
) {
460 case GL_COMPRESSED_RED_RGTC1
:
461 case GL_COMPRESSED_SIGNED_RED_RGTC1
:
463 case GL_COMPRESSED_RG_RGTC2
:
464 case GL_COMPRESSED_SIGNED_RG_RGTC2
:
471 if (ctx
->Extensions
.EXT_texture_compression_latc
) {
472 switch (internalFormat
) {
473 case GL_COMPRESSED_LUMINANCE_LATC1_EXT
:
474 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT
:
476 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT
:
477 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT
:
478 return GL_LUMINANCE_ALPHA
;
484 if (ctx
->Extensions
.ATI_texture_compression_3dc
) {
485 switch (internalFormat
) {
486 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI
:
487 return GL_LUMINANCE_ALPHA
;
493 if (ctx
->Extensions
.OES_compressed_ETC1_RGB8_texture
) {
494 switch (internalFormat
) {
495 case GL_ETC1_RGB8_OES
:
502 if (_mesa_is_gles3(ctx
) || ctx
->Extensions
.ARB_ES3_compatibility
) {
503 switch (internalFormat
) {
504 case GL_COMPRESSED_RGB8_ETC2
:
505 case GL_COMPRESSED_SRGB8_ETC2
:
507 case GL_COMPRESSED_RGBA8_ETC2_EAC
:
508 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC
:
509 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2
:
510 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2
:
512 case GL_COMPRESSED_R11_EAC
:
513 case GL_COMPRESSED_SIGNED_R11_EAC
:
515 case GL_COMPRESSED_RG11_EAC
:
516 case GL_COMPRESSED_SIGNED_RG11_EAC
:
523 if (ctx
->API
== API_OPENGLES
) {
524 switch (internalFormat
) {
525 case GL_PALETTE4_RGB8_OES
:
526 case GL_PALETTE4_R5_G6_B5_OES
:
527 case GL_PALETTE8_RGB8_OES
:
528 case GL_PALETTE8_R5_G6_B5_OES
:
530 case GL_PALETTE4_RGBA8_OES
:
531 case GL_PALETTE8_RGB5_A1_OES
:
532 case GL_PALETTE4_RGBA4_OES
:
533 case GL_PALETTE4_RGB5_A1_OES
:
534 case GL_PALETTE8_RGBA8_OES
:
535 case GL_PALETTE8_RGBA4_OES
:
542 return -1; /* error */
547 * For cube map faces, return a face index in [0,5].
548 * For other targets return 0;
551 _mesa_tex_target_to_face(GLenum target
)
553 if (_mesa_is_cube_face(target
))
554 return (GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
;
562 * Install gl_texture_image in a gl_texture_object according to the target
563 * and level parameters.
565 * \param tObj texture object.
566 * \param target texture target.
567 * \param level image level.
568 * \param texImage texture image.
571 set_tex_image(struct gl_texture_object
*tObj
,
572 GLenum target
, GLint level
,
573 struct gl_texture_image
*texImage
)
575 const GLuint face
= _mesa_tex_target_to_face(target
);
579 if (target
== GL_TEXTURE_RECTANGLE_NV
|| target
== GL_TEXTURE_EXTERNAL_OES
)
582 tObj
->Image
[face
][level
] = texImage
;
584 /* Set the 'back' pointer */
585 texImage
->TexObject
= tObj
;
586 texImage
->Level
= level
;
587 texImage
->Face
= face
;
592 * Allocate a texture image structure.
594 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
597 * \return a pointer to gl_texture_image struct with all fields initialized to
600 struct gl_texture_image
*
601 _mesa_new_texture_image( struct gl_context
*ctx
)
604 return CALLOC_STRUCT(gl_texture_image
);
609 * Free a gl_texture_image and associated data.
610 * This function is a fallback called via ctx->Driver.DeleteTextureImage().
612 * \param texImage texture image.
614 * Free the texture image structure and the associated image data.
617 _mesa_delete_texture_image(struct gl_context
*ctx
,
618 struct gl_texture_image
*texImage
)
620 /* Free texImage->Data and/or any other driver-specific texture
623 ASSERT(ctx
->Driver
.FreeTextureImageBuffer
);
624 ctx
->Driver
.FreeTextureImageBuffer( ctx
, texImage
);
630 * Test if a target is a proxy target.
632 * \param target texture target.
634 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
637 _mesa_is_proxy_texture(GLenum target
)
640 static const GLenum targets
[] = {
644 GL_PROXY_TEXTURE_CUBE_MAP
,
645 GL_PROXY_TEXTURE_RECTANGLE
,
646 GL_PROXY_TEXTURE_1D_ARRAY
,
647 GL_PROXY_TEXTURE_2D_ARRAY
,
648 GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
,
649 GL_PROXY_TEXTURE_2D_MULTISAMPLE
,
650 GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
653 * NUM_TEXTURE_TARGETS should match number of terms above, except there's no
654 * proxy for GL_TEXTURE_BUFFER and GL_TEXTURE_EXTERNAL_OES.
656 STATIC_ASSERT(NUM_TEXTURE_TARGETS
== Elements(targets
) + 2);
658 for (i
= 0; i
< Elements(targets
); ++i
)
659 if (target
== targets
[i
])
666 * Return the proxy target which corresponds to the given texture target
669 proxy_target(GLenum target
)
673 case GL_PROXY_TEXTURE_1D
:
674 return GL_PROXY_TEXTURE_1D
;
676 case GL_PROXY_TEXTURE_2D
:
677 return GL_PROXY_TEXTURE_2D
;
679 case GL_PROXY_TEXTURE_3D
:
680 return GL_PROXY_TEXTURE_3D
;
681 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
682 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
683 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
684 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
685 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
686 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
687 case GL_TEXTURE_CUBE_MAP_ARB
:
688 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
689 return GL_PROXY_TEXTURE_CUBE_MAP_ARB
;
690 case GL_TEXTURE_RECTANGLE_NV
:
691 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
692 return GL_PROXY_TEXTURE_RECTANGLE_NV
;
693 case GL_TEXTURE_1D_ARRAY_EXT
:
694 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
695 return GL_PROXY_TEXTURE_1D_ARRAY_EXT
;
696 case GL_TEXTURE_2D_ARRAY_EXT
:
697 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
698 return GL_PROXY_TEXTURE_2D_ARRAY_EXT
;
699 case GL_TEXTURE_CUBE_MAP_ARRAY
:
700 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
701 return GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
;
702 case GL_TEXTURE_2D_MULTISAMPLE
:
703 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
704 return GL_PROXY_TEXTURE_2D_MULTISAMPLE
;
705 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
706 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
707 return GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
;
709 _mesa_problem(NULL
, "unexpected target in proxy_target()");
716 * Return a pointer to the current texture object for the given target
717 * on the current texture unit.
718 * Note: all <target> error checking should have been done by this point.
720 struct gl_texture_object
*
721 _mesa_get_current_tex_object(struct gl_context
*ctx
, GLenum target
)
723 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
724 const GLboolean arrayTex
= ctx
->Extensions
.EXT_texture_array
;
728 return texUnit
->CurrentTex
[TEXTURE_1D_INDEX
];
729 case GL_PROXY_TEXTURE_1D
:
730 return ctx
->Texture
.ProxyTex
[TEXTURE_1D_INDEX
];
732 return texUnit
->CurrentTex
[TEXTURE_2D_INDEX
];
733 case GL_PROXY_TEXTURE_2D
:
734 return ctx
->Texture
.ProxyTex
[TEXTURE_2D_INDEX
];
736 return texUnit
->CurrentTex
[TEXTURE_3D_INDEX
];
737 case GL_PROXY_TEXTURE_3D
:
738 return ctx
->Texture
.ProxyTex
[TEXTURE_3D_INDEX
];
739 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
740 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
741 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
742 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
743 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
744 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
745 case GL_TEXTURE_CUBE_MAP_ARB
:
746 return ctx
->Extensions
.ARB_texture_cube_map
747 ? texUnit
->CurrentTex
[TEXTURE_CUBE_INDEX
] : NULL
;
748 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
749 return ctx
->Extensions
.ARB_texture_cube_map
750 ? ctx
->Texture
.ProxyTex
[TEXTURE_CUBE_INDEX
] : NULL
;
751 case GL_TEXTURE_CUBE_MAP_ARRAY
:
752 return ctx
->Extensions
.ARB_texture_cube_map_array
753 ? texUnit
->CurrentTex
[TEXTURE_CUBE_ARRAY_INDEX
] : NULL
;
754 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
755 return ctx
->Extensions
.ARB_texture_cube_map_array
756 ? ctx
->Texture
.ProxyTex
[TEXTURE_CUBE_ARRAY_INDEX
] : NULL
;
757 case GL_TEXTURE_RECTANGLE_NV
:
758 return ctx
->Extensions
.NV_texture_rectangle
759 ? texUnit
->CurrentTex
[TEXTURE_RECT_INDEX
] : NULL
;
760 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
761 return ctx
->Extensions
.NV_texture_rectangle
762 ? ctx
->Texture
.ProxyTex
[TEXTURE_RECT_INDEX
] : NULL
;
763 case GL_TEXTURE_1D_ARRAY_EXT
:
764 return arrayTex
? texUnit
->CurrentTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
765 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
766 return arrayTex
? ctx
->Texture
.ProxyTex
[TEXTURE_1D_ARRAY_INDEX
] : NULL
;
767 case GL_TEXTURE_2D_ARRAY_EXT
:
768 return arrayTex
? texUnit
->CurrentTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
769 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
770 return arrayTex
? ctx
->Texture
.ProxyTex
[TEXTURE_2D_ARRAY_INDEX
] : NULL
;
771 case GL_TEXTURE_BUFFER
:
772 return ctx
->API
== API_OPENGL_CORE
&&
773 ctx
->Extensions
.ARB_texture_buffer_object
?
774 texUnit
->CurrentTex
[TEXTURE_BUFFER_INDEX
] : NULL
;
775 case GL_TEXTURE_EXTERNAL_OES
:
776 return _mesa_is_gles(ctx
) && ctx
->Extensions
.OES_EGL_image_external
777 ? texUnit
->CurrentTex
[TEXTURE_EXTERNAL_INDEX
] : NULL
;
778 case GL_TEXTURE_2D_MULTISAMPLE
:
779 return ctx
->Extensions
.ARB_texture_multisample
780 ? texUnit
->CurrentTex
[TEXTURE_2D_MULTISAMPLE_INDEX
] : NULL
;
781 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
782 return ctx
->Extensions
.ARB_texture_multisample
783 ? ctx
->Texture
.ProxyTex
[TEXTURE_2D_MULTISAMPLE_INDEX
] : NULL
;
784 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
785 return ctx
->Extensions
.ARB_texture_multisample
786 ? texUnit
->CurrentTex
[TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
] : NULL
;
787 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
788 return ctx
->Extensions
.ARB_texture_multisample
789 ? ctx
->Texture
.ProxyTex
[TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
] : NULL
;
791 _mesa_problem(NULL
, "bad target in _mesa_get_current_tex_object()");
799 * Get a texture image pointer from a texture object, given a texture
800 * target and mipmap level. The target and level parameters should
801 * have already been error-checked.
803 * \param ctx GL context.
804 * \param texObj texture unit.
805 * \param target texture target.
806 * \param level image level.
808 * \return pointer to the texture image structure, or NULL on failure.
810 struct gl_texture_image
*
811 _mesa_select_tex_image(struct gl_context
*ctx
,
812 const struct gl_texture_object
*texObj
,
813 GLenum target
, GLint level
)
815 const GLuint face
= _mesa_tex_target_to_face(target
);
819 ASSERT(level
< MAX_TEXTURE_LEVELS
);
821 return texObj
->Image
[face
][level
];
826 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
827 * it and install it. Only return NULL if passed a bad parameter or run
830 struct gl_texture_image
*
831 _mesa_get_tex_image(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
832 GLenum target
, GLint level
)
834 struct gl_texture_image
*texImage
;
839 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
841 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
843 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture image allocation");
847 set_tex_image(texObj
, target
, level
, texImage
);
855 * Return pointer to the specified proxy texture image.
856 * Note that proxy textures are per-context, not per-texture unit.
857 * \return pointer to texture image or NULL if invalid target, invalid
858 * level, or out of memory.
860 static struct gl_texture_image
*
861 get_proxy_tex_image(struct gl_context
*ctx
, GLenum target
, GLint level
)
863 struct gl_texture_image
*texImage
;
870 case GL_PROXY_TEXTURE_1D
:
871 if (level
>= ctx
->Const
.MaxTextureLevels
)
873 texIndex
= TEXTURE_1D_INDEX
;
875 case GL_PROXY_TEXTURE_2D
:
876 if (level
>= ctx
->Const
.MaxTextureLevels
)
878 texIndex
= TEXTURE_2D_INDEX
;
880 case GL_PROXY_TEXTURE_3D
:
881 if (level
>= ctx
->Const
.Max3DTextureLevels
)
883 texIndex
= TEXTURE_3D_INDEX
;
885 case GL_PROXY_TEXTURE_CUBE_MAP
:
886 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
888 texIndex
= TEXTURE_CUBE_INDEX
;
890 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
893 texIndex
= TEXTURE_RECT_INDEX
;
895 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
896 if (level
>= ctx
->Const
.MaxTextureLevels
)
898 texIndex
= TEXTURE_1D_ARRAY_INDEX
;
900 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
901 if (level
>= ctx
->Const
.MaxTextureLevels
)
903 texIndex
= TEXTURE_2D_ARRAY_INDEX
;
905 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
906 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
908 texIndex
= TEXTURE_CUBE_ARRAY_INDEX
;
910 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
913 texIndex
= TEXTURE_2D_MULTISAMPLE_INDEX
;
915 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
918 texIndex
= TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
;
924 texImage
= ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
];
926 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
928 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
931 ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
] = texImage
;
932 /* Set the 'back' pointer */
933 texImage
->TexObject
= ctx
->Texture
.ProxyTex
[texIndex
];
940 * Get the maximum number of allowed mipmap levels.
942 * \param ctx GL context.
943 * \param target texture target.
945 * \return the maximum number of allowed mipmap levels for the given
946 * texture target, or zero if passed a bad target.
951 _mesa_max_texture_levels(struct gl_context
*ctx
, GLenum target
)
955 case GL_PROXY_TEXTURE_1D
:
957 case GL_PROXY_TEXTURE_2D
:
958 return ctx
->Const
.MaxTextureLevels
;
960 case GL_PROXY_TEXTURE_3D
:
961 return ctx
->Const
.Max3DTextureLevels
;
962 case GL_TEXTURE_CUBE_MAP
:
963 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
964 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
965 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
966 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
967 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
968 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
969 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
970 return ctx
->Extensions
.ARB_texture_cube_map
971 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
972 case GL_TEXTURE_RECTANGLE_NV
:
973 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
974 return ctx
->Extensions
.NV_texture_rectangle
? 1 : 0;
975 case GL_TEXTURE_1D_ARRAY_EXT
:
976 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
977 case GL_TEXTURE_2D_ARRAY_EXT
:
978 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
979 return ctx
->Extensions
.EXT_texture_array
980 ? ctx
->Const
.MaxTextureLevels
: 0;
981 case GL_TEXTURE_CUBE_MAP_ARRAY
:
982 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
983 return ctx
->Extensions
.ARB_texture_cube_map_array
984 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
985 case GL_TEXTURE_BUFFER
:
986 return ctx
->API
== API_OPENGL_CORE
&&
987 ctx
->Extensions
.ARB_texture_buffer_object
? 1 : 0;
988 case GL_TEXTURE_2D_MULTISAMPLE
:
989 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
990 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
991 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
992 return _mesa_is_desktop_gl(ctx
)
993 && ctx
->Extensions
.ARB_texture_multisample
995 case GL_TEXTURE_EXTERNAL_OES
:
998 return 0; /* bad target */
1004 * Return number of dimensions per mipmap level for the given texture target.
1007 _mesa_get_texture_dimensions(GLenum target
)
1011 case GL_PROXY_TEXTURE_1D
:
1014 case GL_TEXTURE_RECTANGLE
:
1015 case GL_TEXTURE_CUBE_MAP
:
1016 case GL_PROXY_TEXTURE_2D
:
1017 case GL_PROXY_TEXTURE_RECTANGLE
:
1018 case GL_PROXY_TEXTURE_CUBE_MAP
:
1019 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1020 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1021 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1022 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1023 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1024 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1025 case GL_TEXTURE_1D_ARRAY
:
1026 case GL_PROXY_TEXTURE_1D_ARRAY
:
1027 case GL_TEXTURE_EXTERNAL_OES
:
1028 case GL_TEXTURE_2D_MULTISAMPLE
:
1029 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
1032 case GL_PROXY_TEXTURE_3D
:
1033 case GL_TEXTURE_2D_ARRAY
:
1034 case GL_PROXY_TEXTURE_2D_ARRAY
:
1035 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1036 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1037 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1038 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1040 case GL_TEXTURE_BUFFER
:
1043 _mesa_problem(NULL
, "invalid target 0x%x in get_texture_dimensions()",
1051 * Check if a texture target can have more than one layer.
1054 _mesa_tex_target_is_layered(GLenum target
)
1058 case GL_PROXY_TEXTURE_1D
:
1060 case GL_PROXY_TEXTURE_2D
:
1061 case GL_TEXTURE_RECTANGLE
:
1062 case GL_PROXY_TEXTURE_RECTANGLE
:
1063 case GL_TEXTURE_2D_MULTISAMPLE
:
1064 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
1065 case GL_TEXTURE_BUFFER
:
1066 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1067 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1068 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1069 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1070 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1071 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1072 case GL_TEXTURE_EXTERNAL_OES
:
1076 case GL_PROXY_TEXTURE_3D
:
1077 case GL_TEXTURE_CUBE_MAP
:
1078 case GL_PROXY_TEXTURE_CUBE_MAP
:
1079 case GL_TEXTURE_1D_ARRAY
:
1080 case GL_PROXY_TEXTURE_1D_ARRAY
:
1081 case GL_TEXTURE_2D_ARRAY
:
1082 case GL_PROXY_TEXTURE_2D_ARRAY
:
1083 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1084 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1085 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1086 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1090 assert(!"Invalid texture target.");
1097 * Return the number of layers present in the given level of an array,
1098 * cubemap or 3D texture. If the texture is not layered return zero.
1101 _mesa_get_texture_layers(const struct gl_texture_object
*texObj
, GLint level
)
1103 assert(level
>= 0 && level
< MAX_TEXTURE_LEVELS
);
1105 switch (texObj
->Target
) {
1108 case GL_TEXTURE_RECTANGLE
:
1109 case GL_TEXTURE_2D_MULTISAMPLE
:
1110 case GL_TEXTURE_BUFFER
:
1111 case GL_TEXTURE_EXTERNAL_OES
:
1114 case GL_TEXTURE_CUBE_MAP
:
1117 case GL_TEXTURE_1D_ARRAY
: {
1118 struct gl_texture_image
*img
= texObj
->Image
[0][level
];
1119 return img
? img
->Height
: 0;
1123 case GL_TEXTURE_2D_ARRAY
:
1124 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1125 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
: {
1126 struct gl_texture_image
*img
= texObj
->Image
[0][level
];
1127 return img
? img
->Depth
: 0;
1131 assert(!"Invalid texture target.");
1138 * Return the maximum number of mipmap levels for the given target
1139 * and the dimensions.
1140 * The dimensions are expected not to include the border.
1143 _mesa_get_tex_max_num_levels(GLenum target
, GLsizei width
, GLsizei height
,
1150 case GL_TEXTURE_1D_ARRAY
:
1151 case GL_PROXY_TEXTURE_1D
:
1152 case GL_PROXY_TEXTURE_1D_ARRAY
:
1155 case GL_TEXTURE_CUBE_MAP
:
1156 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1157 case GL_PROXY_TEXTURE_CUBE_MAP
:
1158 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1162 case GL_TEXTURE_2D_ARRAY
:
1163 case GL_PROXY_TEXTURE_2D
:
1164 case GL_PROXY_TEXTURE_2D_ARRAY
:
1165 size
= MAX2(width
, height
);
1168 case GL_PROXY_TEXTURE_3D
:
1169 size
= MAX3(width
, height
, depth
);
1171 case GL_TEXTURE_RECTANGLE
:
1172 case GL_TEXTURE_EXTERNAL_OES
:
1173 case GL_TEXTURE_2D_MULTISAMPLE
:
1174 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1175 case GL_PROXY_TEXTURE_RECTANGLE
:
1176 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
1177 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1184 return _mesa_logbase2(size
) + 1;
1188 #if 000 /* not used anymore */
1190 * glTexImage[123]D can accept a NULL image pointer. In this case we
1191 * create a texture image with unspecified image contents per the OpenGL
1195 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
1197 const GLint components
= _mesa_components_in_format(format
);
1198 const GLint numPixels
= width
* height
* depth
;
1199 GLubyte
*data
= (GLubyte
*) malloc(numPixels
* components
* sizeof(GLubyte
));
1203 * Let's see if anyone finds this. If glTexImage2D() is called with
1204 * a NULL image pointer then load the texture image with something
1205 * interesting instead of leaving it indeterminate.
1208 static const char message
[8][32] = {
1209 " X X XXXXX XXX X ",
1210 " XX XX X X X X X ",
1212 " X X XXXX XXX XXXXX ",
1215 " X X XXXXX XXX X X ",
1219 GLubyte
*imgPtr
= data
;
1221 for (h
= 0; h
< depth
; h
++) {
1222 for (i
= 0; i
< height
; i
++) {
1223 GLint srcRow
= 7 - (i
% 8);
1224 for (j
= 0; j
< width
; j
++) {
1225 GLint srcCol
= j
% 32;
1226 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
1227 for (k
= 0; k
< components
; k
++) {
1243 * Set the size and format-related fields of a gl_texture_image struct
1244 * to zero. This is used when a proxy texture test fails.
1247 clear_teximage_fields(struct gl_texture_image
*img
)
1250 img
->_BaseFormat
= 0;
1251 img
->InternalFormat
= 0;
1260 img
->HeightLog2
= 0;
1262 img
->TexFormat
= MESA_FORMAT_NONE
;
1263 img
->NumSamples
= 0;
1264 img
->FixedSampleLocations
= GL_TRUE
;
1269 * Initialize basic fields of the gl_texture_image struct.
1271 * \param ctx GL context.
1272 * \param img texture image structure to be initialized.
1273 * \param width image width.
1274 * \param height image height.
1275 * \param depth image depth.
1276 * \param border image border.
1277 * \param internalFormat internal format.
1278 * \param format the actual hardware format (one of MESA_FORMAT_*)
1279 * \param numSamples number of samples per texel, or zero for non-MS.
1280 * \param fixedSampleLocations are sample locations fixed?
1282 * Fills in the fields of \p img with the given information.
1283 * Note: width, height and depth include the border.
1286 init_teximage_fields_ms(struct gl_context
*ctx
,
1287 struct gl_texture_image
*img
,
1288 GLsizei width
, GLsizei height
, GLsizei depth
,
1289 GLint border
, GLenum internalFormat
,
1291 GLuint numSamples
, GLboolean fixedSampleLocations
)
1296 ASSERT(height
>= 0);
1299 target
= img
->TexObject
->Target
;
1300 img
->_BaseFormat
= _mesa_base_tex_format( ctx
, internalFormat
);
1301 ASSERT(img
->_BaseFormat
> 0);
1302 img
->InternalFormat
= internalFormat
;
1303 img
->Border
= border
;
1305 img
->Height
= height
;
1308 img
->Width2
= width
- 2 * border
; /* == 1 << img->WidthLog2; */
1309 img
->WidthLog2
= _mesa_logbase2(img
->Width2
);
1311 img
->NumSamples
= 0;
1312 img
->FixedSampleLocations
= GL_TRUE
;
1316 case GL_TEXTURE_BUFFER
:
1317 case GL_PROXY_TEXTURE_1D
:
1322 img
->HeightLog2
= 0;
1329 case GL_TEXTURE_1D_ARRAY
:
1330 case GL_PROXY_TEXTURE_1D_ARRAY
:
1331 img
->Height2
= height
; /* no border */
1332 img
->HeightLog2
= 0; /* not used */
1340 case GL_TEXTURE_RECTANGLE
:
1341 case GL_TEXTURE_CUBE_MAP
:
1342 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1343 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1344 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1345 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1346 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1347 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1348 case GL_TEXTURE_EXTERNAL_OES
:
1349 case GL_PROXY_TEXTURE_2D
:
1350 case GL_PROXY_TEXTURE_RECTANGLE
:
1351 case GL_PROXY_TEXTURE_CUBE_MAP
:
1352 case GL_TEXTURE_2D_MULTISAMPLE
:
1353 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
1354 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1355 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1362 case GL_TEXTURE_2D_ARRAY
:
1363 case GL_PROXY_TEXTURE_2D_ARRAY
:
1364 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1365 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1366 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1367 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1368 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1369 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1370 img
->Depth2
= depth
; /* no border */
1371 img
->DepthLog2
= 0; /* not used */
1374 case GL_PROXY_TEXTURE_3D
:
1375 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
1376 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
1377 img
->Depth2
= depth
- 2 * border
; /* == 1 << img->DepthLog2; */
1378 img
->DepthLog2
= _mesa_logbase2(img
->Depth2
);
1381 _mesa_problem(NULL
, "invalid target 0x%x in _mesa_init_teximage_fields()",
1386 _mesa_get_tex_max_num_levels(target
,
1387 img
->Width2
, img
->Height2
, img
->Depth2
);
1388 img
->TexFormat
= format
;
1389 img
->NumSamples
= numSamples
;
1390 img
->FixedSampleLocations
= fixedSampleLocations
;
1395 _mesa_init_teximage_fields(struct gl_context
*ctx
,
1396 struct gl_texture_image
*img
,
1397 GLsizei width
, GLsizei height
, GLsizei depth
,
1398 GLint border
, GLenum internalFormat
,
1401 init_teximage_fields_ms(ctx
, img
, width
, height
, depth
, border
,
1402 internalFormat
, format
, 0, GL_TRUE
);
1407 * Free and clear fields of the gl_texture_image struct.
1409 * \param ctx GL context.
1410 * \param texImage texture image structure to be cleared.
1412 * After the call, \p texImage will have no data associated with it. Its
1413 * fields are cleared so that its parent object will test incomplete.
1416 _mesa_clear_texture_image(struct gl_context
*ctx
,
1417 struct gl_texture_image
*texImage
)
1419 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
1420 clear_teximage_fields(texImage
);
1425 * Check the width, height, depth and border of a texture image are legal.
1426 * Used by all the glTexImage, glCompressedTexImage and glCopyTexImage
1428 * The target and level parameters will have already been validated.
1429 * \return GL_TRUE if size is OK, GL_FALSE otherwise.
1432 _mesa_legal_texture_dimensions(struct gl_context
*ctx
, GLenum target
,
1433 GLint level
, GLint width
, GLint height
,
1434 GLint depth
, GLint border
)
1440 case GL_PROXY_TEXTURE_1D
:
1441 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1); /* level zero size */
1442 maxSize
>>= level
; /* level size */
1443 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1445 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1446 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1452 case GL_PROXY_TEXTURE_2D
:
1453 case GL_TEXTURE_2D_MULTISAMPLE
:
1454 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
1455 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1457 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1459 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1461 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1462 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1464 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1470 case GL_PROXY_TEXTURE_3D
:
1471 maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
1473 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1475 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1477 if (depth
< 2 * border
|| depth
> 2 * border
+ maxSize
)
1479 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1480 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1482 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1484 if (depth
> 0 && !_mesa_is_pow_two(depth
- 2 * border
))
1489 case GL_TEXTURE_RECTANGLE_NV
:
1490 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1493 maxSize
= ctx
->Const
.MaxTextureRectSize
;
1494 if (width
< 0 || width
> maxSize
)
1496 if (height
< 0 || height
> maxSize
)
1500 case GL_TEXTURE_CUBE_MAP
:
1501 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1502 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1503 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1504 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1505 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1506 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1507 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
1508 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1510 if (width
!= height
)
1512 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1514 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1516 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1517 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1519 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1524 case GL_TEXTURE_1D_ARRAY_EXT
:
1525 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1526 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1528 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1530 if (height
< 1 || height
> ctx
->Const
.MaxArrayTextureLayers
)
1532 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1533 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1538 case GL_TEXTURE_2D_ARRAY_EXT
:
1539 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1540 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1541 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1542 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1544 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1546 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1548 if (depth
< 0 || depth
> ctx
->Const
.MaxArrayTextureLayers
)
1550 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1551 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1553 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1558 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1559 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1560 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1561 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1563 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1565 if (depth
< 0 || depth
> ctx
->Const
.MaxArrayTextureLayers
|| depth
% 6)
1567 if (width
!= height
)
1569 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
1571 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1572 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1574 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1579 _mesa_problem(ctx
, "Invalid target in _mesa_legal_texture_dimensions()");
1586 * Do error checking of xoffset, yoffset, zoffset, width, height and depth
1587 * for glTexSubImage, glCopyTexSubImage and glCompressedTexSubImage.
1588 * \param destImage the destination texture image.
1589 * \return GL_TRUE if error found, GL_FALSE otherwise.
1592 error_check_subtexture_dimensions(struct gl_context
*ctx
,
1593 const char *function
, GLuint dims
,
1594 const struct gl_texture_image
*destImage
,
1595 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1596 GLsizei subWidth
, GLsizei subHeight
,
1599 const GLenum target
= destImage
->TexObject
->Target
;
1604 _mesa_error(ctx
, GL_INVALID_VALUE
,
1605 "%s%dD(width=%d)", function
, dims
, subWidth
);
1609 if (dims
> 1 && subHeight
< 0) {
1610 _mesa_error(ctx
, GL_INVALID_VALUE
,
1611 "%s%dD(height=%d)", function
, dims
, subHeight
);
1615 if (dims
> 2 && subDepth
< 0) {
1616 _mesa_error(ctx
, GL_INVALID_VALUE
,
1617 "%s%dD(depth=%d)", function
, dims
, subDepth
);
1621 /* check xoffset and width */
1622 if (xoffset
< - (GLint
) destImage
->Border
) {
1623 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(xoffset)",
1628 if (xoffset
+ subWidth
> (GLint
) destImage
->Width
) {
1629 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(xoffset+width)",
1634 /* check yoffset and height */
1636 GLint yBorder
= (target
== GL_TEXTURE_1D_ARRAY
) ? 0 : destImage
->Border
;
1637 if (yoffset
< -yBorder
) {
1638 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(yoffset)",
1642 if (yoffset
+ subHeight
> (GLint
) destImage
->Height
) {
1643 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s%dD(yoffset+height)",
1649 /* check zoffset and depth */
1651 GLint zBorder
= (target
== GL_TEXTURE_2D_ARRAY
||
1652 target
== GL_TEXTURE_CUBE_MAP_ARRAY
) ?
1653 0 : destImage
->Border
;
1655 if (zoffset
< -zBorder
) {
1656 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s3D(zoffset)", function
);
1659 if (zoffset
+ subDepth
> (GLint
) destImage
->Depth
) {
1660 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s3D(zoffset+depth)", function
);
1666 * The OpenGL spec (and GL_ARB_texture_compression) says only whole
1667 * compressed texture images can be updated. But, that restriction may be
1668 * relaxed for particular compressed formats. At this time, all the
1669 * compressed formats supported by Mesa allow sub-textures to be updated
1670 * along compressed block boundaries.
1672 _mesa_get_format_block_size(destImage
->TexFormat
, &bw
, &bh
);
1674 if (bw
!= 1 || bh
!= 1) {
1675 /* offset must be multiple of block size */
1676 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0)) {
1677 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1678 "%s%dD(xoffset = %d, yoffset = %d)",
1679 function
, dims
, xoffset
, yoffset
);
1683 /* The size must be a multiple of bw x bh, or we must be using a
1684 * offset+size that exactly hits the edge of the image. This
1685 * is important for small mipmap levels (1x1, 2x1, etc) and for
1688 if ((subWidth
% bw
!= 0) &&
1689 (xoffset
+ subWidth
!= (GLint
) destImage
->Width
)) {
1690 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1691 "%s%dD(width = %d)", function
, dims
, subWidth
);
1695 if ((subHeight
% bh
!= 0) &&
1696 (yoffset
+ subHeight
!= (GLint
) destImage
->Height
)) {
1697 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1698 "%s%dD(height = %d)", function
, dims
, subHeight
);
1710 * This is the fallback for Driver.TestProxyTexImage() for doing device-
1711 * specific texture image size checks.
1713 * A hardware driver might override this function if, for example, the
1714 * max 3D texture size is 512x512x64 (i.e. not a cube).
1716 * Note that width, height, depth == 0 is not an error. However, a
1717 * texture with zero width/height/depth will be considered "incomplete"
1718 * and texturing will effectively be disabled.
1720 * \param target any texture target/type
1721 * \param level as passed to glTexImage
1722 * \param format the MESA_FORMAT_x for the tex image
1723 * \param width as passed to glTexImage
1724 * \param height as passed to glTexImage
1725 * \param depth as passed to glTexImage
1726 * \param border as passed to glTexImage
1727 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1730 _mesa_test_proxy_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1732 GLint width
, GLint height
, GLint depth
, GLint border
)
1734 /* We just check if the image size is less than MaxTextureMbytes.
1735 * Some drivers may do more specific checks.
1737 uint64_t bytes
= _mesa_format_image_size64(format
, width
, height
, depth
);
1738 uint64_t mbytes
= bytes
/ (1024 * 1024); /* convert to MB */
1739 mbytes
*= _mesa_num_tex_faces(target
);
1740 return mbytes
<= (uint64_t) ctx
->Const
.MaxTextureMbytes
;
1745 * Return true if the format is only valid for glCompressedTexImage.
1748 compressedteximage_only_format(const struct gl_context
*ctx
, GLenum format
)
1751 case GL_ETC1_RGB8_OES
:
1752 case GL_PALETTE4_RGB8_OES
:
1753 case GL_PALETTE4_RGBA8_OES
:
1754 case GL_PALETTE4_R5_G6_B5_OES
:
1755 case GL_PALETTE4_RGBA4_OES
:
1756 case GL_PALETTE4_RGB5_A1_OES
:
1757 case GL_PALETTE8_RGB8_OES
:
1758 case GL_PALETTE8_RGBA8_OES
:
1759 case GL_PALETTE8_R5_G6_B5_OES
:
1760 case GL_PALETTE8_RGBA4_OES
:
1761 case GL_PALETTE8_RGB5_A1_OES
:
1770 * Helper function to determine whether a target and specific compression
1771 * format are supported.
1774 target_can_be_compressed(const struct gl_context
*ctx
, GLenum target
,
1777 (void) intFormat
; /* not used yet */
1781 case GL_PROXY_TEXTURE_2D
:
1782 return GL_TRUE
; /* true for any compressed format so far */
1783 case GL_PROXY_TEXTURE_CUBE_MAP
:
1784 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1785 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1786 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1787 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1788 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1789 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1790 return ctx
->Extensions
.ARB_texture_cube_map
;
1791 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1792 case GL_TEXTURE_2D_ARRAY_EXT
:
1793 return ctx
->Extensions
.EXT_texture_array
;
1794 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1795 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1796 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1804 * Check if the given texture target value is legal for a
1805 * glTexImage1/2/3D call.
1808 legal_teximage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
1814 case GL_PROXY_TEXTURE_1D
:
1815 return _mesa_is_desktop_gl(ctx
);
1823 case GL_PROXY_TEXTURE_2D
:
1824 return _mesa_is_desktop_gl(ctx
);
1825 case GL_PROXY_TEXTURE_CUBE_MAP
:
1826 return _mesa_is_desktop_gl(ctx
)
1827 && ctx
->Extensions
.ARB_texture_cube_map
;
1828 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1829 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1830 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1831 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1832 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1833 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1834 return ctx
->Extensions
.ARB_texture_cube_map
;
1835 case GL_TEXTURE_RECTANGLE_NV
:
1836 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1837 return _mesa_is_desktop_gl(ctx
)
1838 && ctx
->Extensions
.NV_texture_rectangle
;
1839 case GL_TEXTURE_1D_ARRAY_EXT
:
1840 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1841 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1849 case GL_PROXY_TEXTURE_3D
:
1850 return _mesa_is_desktop_gl(ctx
);
1851 case GL_TEXTURE_2D_ARRAY_EXT
:
1852 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1853 || _mesa_is_gles3(ctx
);
1854 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1855 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1856 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1857 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1858 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1863 _mesa_problem(ctx
, "invalid dims=%u in legal_teximage_target()", dims
);
1870 * Check if the given texture target value is legal for a
1871 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1872 * The difference compared to legal_teximage_target() above is that
1873 * proxy targets are not supported.
1876 legal_texsubimage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
1880 return _mesa_is_desktop_gl(ctx
) && target
== GL_TEXTURE_1D
;
1885 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1886 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1887 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1888 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1889 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1890 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1891 return ctx
->Extensions
.ARB_texture_cube_map
;
1892 case GL_TEXTURE_RECTANGLE_NV
:
1893 return _mesa_is_desktop_gl(ctx
)
1894 && ctx
->Extensions
.NV_texture_rectangle
;
1895 case GL_TEXTURE_1D_ARRAY_EXT
:
1896 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1904 case GL_TEXTURE_2D_ARRAY_EXT
:
1905 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1906 || _mesa_is_gles3(ctx
);
1907 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1908 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1909 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1914 _mesa_problem(ctx
, "invalid dims=%u in legal_texsubimage_target()",
1922 * Helper function to determine if a texture object is mutable (in terms
1923 * of GL_ARB_texture_storage).
1926 mutable_tex_object(struct gl_context
*ctx
, GLenum target
)
1928 struct gl_texture_object
*texObj
= _mesa_get_current_tex_object(ctx
, target
);
1929 return !texObj
->Immutable
;
1934 * Return expected size of a compressed texture.
1937 compressed_tex_size(GLsizei width
, GLsizei height
, GLsizei depth
,
1940 mesa_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
1941 return _mesa_format_image_size(mesaFormat
, width
, height
, depth
);
1945 * Verify that a texture format is valid with a particular target
1947 * In particular, textures with base format of \c GL_DEPTH_COMPONENT or
1948 * \c GL_DEPTH_STENCIL are only valid with certain, context dependent texture
1951 * \param ctx GL context
1952 * \param target Texture target
1953 * \param internalFormat Internal format of the texture image
1954 * \param dimensions Dimensionality at the caller. This is \b not used
1955 * in the validation. It is only used when logging
1957 * \param caller Base name of the calling function (e.g.,
1958 * "glTexImage" or "glTexStorage").
1960 * \returns true if the combination is legal, false otherwise.
1963 _mesa_legal_texture_base_format_for_target(struct gl_context
*ctx
,
1964 GLenum target
, GLenum internalFormat
,
1965 unsigned dimensions
,
1968 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
1969 || _mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_STENCIL
) {
1970 /* Section 3.8.3 (Texture Image Specification) of the OpenGL 3.3 Core
1971 * Profile spec says:
1973 * "Textures with a base internal format of DEPTH_COMPONENT or
1974 * DEPTH_STENCIL are supported by texture image specification
1975 * commands only if target is TEXTURE_1D, TEXTURE_2D,
1976 * TEXTURE_1D_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_RECTANGLE,
1977 * TEXTURE_CUBE_MAP, PROXY_TEXTURE_1D, PROXY_TEXTURE_2D,
1978 * PROXY_TEXTURE_1D_ARRAY, PROXY_TEXTURE_2D_ARRAY,
1979 * PROXY_TEXTURE_RECTANGLE, or PROXY_TEXTURE_CUBE_MAP. Using these
1980 * formats in conjunction with any other target will result in an
1981 * INVALID_OPERATION error."
1983 * Cubemaps are only supported with desktop OpenGL version >= 3.0,
1984 * EXT_gpu_shader4, or, on OpenGL ES 2.0+, OES_depth_texture_cube_map.
1986 if (target
!= GL_TEXTURE_1D
&&
1987 target
!= GL_PROXY_TEXTURE_1D
&&
1988 target
!= GL_TEXTURE_2D
&&
1989 target
!= GL_PROXY_TEXTURE_2D
&&
1990 target
!= GL_TEXTURE_1D_ARRAY
&&
1991 target
!= GL_PROXY_TEXTURE_1D_ARRAY
&&
1992 target
!= GL_TEXTURE_2D_ARRAY
&&
1993 target
!= GL_PROXY_TEXTURE_2D_ARRAY
&&
1994 target
!= GL_TEXTURE_RECTANGLE_ARB
&&
1995 target
!= GL_PROXY_TEXTURE_RECTANGLE_ARB
&&
1996 !((_mesa_is_cube_face(target
) ||
1997 target
== GL_TEXTURE_CUBE_MAP
||
1998 target
== GL_PROXY_TEXTURE_CUBE_MAP
) &&
1999 (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_gpu_shader4
2000 || (ctx
->API
== API_OPENGLES2
&& ctx
->Extensions
.OES_depth_texture_cube_map
))) &&
2001 !((target
== GL_TEXTURE_CUBE_MAP_ARRAY
||
2002 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
) &&
2003 ctx
->Extensions
.ARB_texture_cube_map_array
)) {
2004 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2005 "%s%dD(bad target for depth texture)",
2006 caller
, dimensions
);
2015 texture_formats_agree(GLenum internalFormat
,
2018 GLboolean colorFormat
;
2019 GLboolean is_format_depth_or_depthstencil
;
2020 GLboolean is_internalFormat_depth_or_depthstencil
;
2022 /* Even though there are no color-index textures, we still have to support
2023 * uploading color-index data and remapping it to RGB via the
2024 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
2026 const GLboolean indexFormat
= (format
== GL_COLOR_INDEX
);
2028 is_internalFormat_depth_or_depthstencil
=
2029 _mesa_is_depth_format(internalFormat
) ||
2030 _mesa_is_depthstencil_format(internalFormat
);
2032 is_format_depth_or_depthstencil
=
2033 _mesa_is_depth_format(format
) ||
2034 _mesa_is_depthstencil_format(format
);
2036 colorFormat
= _mesa_is_color_format(format
);
2038 if (_mesa_is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
)
2041 if (is_internalFormat_depth_or_depthstencil
!=
2042 is_format_depth_or_depthstencil
)
2045 if (_mesa_is_ycbcr_format(internalFormat
) != _mesa_is_ycbcr_format(format
))
2052 * Test the glTexImage[123]D() parameters for errors.
2054 * \param ctx GL context.
2055 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2056 * \param target texture target given by the user (already validated).
2057 * \param level image level given by the user.
2058 * \param internalFormat internal format given by the user.
2059 * \param format pixel data format given by the user.
2060 * \param type pixel data type given by the user.
2061 * \param width image width given by the user.
2062 * \param height image height given by the user.
2063 * \param depth image depth given by the user.
2064 * \param border image border given by the user.
2066 * \return GL_TRUE if a error is found, GL_FALSE otherwise
2068 * Verifies each of the parameters against the constants specified in
2069 * __struct gl_contextRec::Const and the supported extensions, and according
2070 * to the OpenGL specification.
2071 * Note that we don't fully error-check the width, height, depth values
2072 * here. That's done in _mesa_legal_texture_dimensions() which is used
2073 * by several other GL entrypoints. Plus, texture dims have a special
2074 * interaction with proxy textures.
2077 texture_error_check( struct gl_context
*ctx
,
2078 GLuint dimensions
, GLenum target
,
2079 GLint level
, GLint internalFormat
,
2080 GLenum format
, GLenum type
,
2081 GLint width
, GLint height
,
2082 GLint depth
, GLint border
)
2086 /* Note: for proxy textures, some error conditions immediately generate
2087 * a GL error in the usual way. But others do not generate a GL error.
2088 * Instead, they cause the width, height, depth, format fields of the
2089 * texture image to be zeroed-out. The GL spec seems to indicate that the
2090 * zero-out behaviour is only used in cases related to memory allocation.
2094 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2095 _mesa_error(ctx
, GL_INVALID_VALUE
,
2096 "glTexImage%dD(level=%d)", dimensions
, level
);
2101 if (border
< 0 || border
> 1 ||
2102 ((ctx
->API
!= API_OPENGL_COMPAT
||
2103 target
== GL_TEXTURE_RECTANGLE_NV
||
2104 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
2105 _mesa_error(ctx
, GL_INVALID_VALUE
,
2106 "glTexImage%dD(border=%d)", dimensions
, border
);
2110 if (width
< 0 || height
< 0 || depth
< 0) {
2111 _mesa_error(ctx
, GL_INVALID_VALUE
,
2112 "glTexImage%dD(width, height or depth < 0)", dimensions
);
2116 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2117 * combinations of format, internalFormat, and type that can be used.
2118 * Formats and types that require additional extensions (e.g., GL_FLOAT
2119 * requires GL_OES_texture_float) are filtered elsewhere.
2122 if (_mesa_is_gles(ctx
)) {
2123 if (_mesa_is_gles3(ctx
)) {
2124 err
= _mesa_es3_error_check_format_and_type(format
, type
,
2127 if (format
!= internalFormat
) {
2128 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2129 "glTexImage%dD(format = %s, internalFormat = %s)",
2131 _mesa_lookup_enum_by_nr(format
),
2132 _mesa_lookup_enum_by_nr(internalFormat
));
2136 err
= _mesa_es_error_check_format_and_type(format
, type
, dimensions
);
2138 if (err
!= GL_NO_ERROR
) {
2139 _mesa_error(ctx
, err
,
2140 "glTexImage%dD(format = %s, type = %s, internalFormat = %s)",
2142 _mesa_lookup_enum_by_nr(format
),
2143 _mesa_lookup_enum_by_nr(type
),
2144 _mesa_lookup_enum_by_nr(internalFormat
));
2149 /* Check internalFormat */
2150 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
2151 _mesa_error(ctx
, GL_INVALID_VALUE
,
2152 "glTexImage%dD(internalFormat=%s)",
2153 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
));
2157 /* Check incoming image format and type */
2158 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
2159 if (err
!= GL_NO_ERROR
) {
2160 _mesa_error(ctx
, err
,
2161 "glTexImage%dD(incompatible format = %s, type = %s)",
2162 dimensions
, _mesa_lookup_enum_by_nr(format
),
2163 _mesa_lookup_enum_by_nr(type
));
2167 /* make sure internal format and format basically agree */
2168 if (!texture_formats_agree(internalFormat
, format
)) {
2169 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2170 "glTexImage%dD(incompatible internalFormat = %s, format = %s)",
2171 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
),
2172 _mesa_lookup_enum_by_nr(format
));
2176 /* additional checks for ycbcr textures */
2177 if (internalFormat
== GL_YCBCR_MESA
) {
2178 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
2179 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
2180 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
2182 _mesa_snprintf(message
, sizeof(message
),
2183 "glTexImage%dD(format/type YCBCR mismatch)",
2185 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", message
);
2186 return GL_TRUE
; /* error */
2188 if (target
!= GL_TEXTURE_2D
&&
2189 target
!= GL_PROXY_TEXTURE_2D
&&
2190 target
!= GL_TEXTURE_RECTANGLE_NV
&&
2191 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
2192 _mesa_error(ctx
, GL_INVALID_ENUM
,
2193 "glTexImage%dD(bad target for YCbCr texture)",
2199 _mesa_snprintf(message
, sizeof(message
),
2200 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
2201 dimensions
, border
);
2202 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", message
);
2207 /* additional checks for depth textures */
2208 if (!_mesa_legal_texture_base_format_for_target(ctx
, target
, internalFormat
,
2209 dimensions
, "glTexImage"))
2212 /* additional checks for compressed textures */
2213 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
2214 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
2215 _mesa_error(ctx
, GL_INVALID_ENUM
,
2216 "glTexImage%dD(target can't be compressed)", dimensions
);
2219 if (compressedteximage_only_format(ctx
, internalFormat
)) {
2220 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2221 "glTexImage%dD(no compression for format)", dimensions
);
2225 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2226 "glTexImage%dD(border!=0)", dimensions
);
2231 /* additional checks for integer textures */
2232 if ((ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) &&
2233 (_mesa_is_enum_format_integer(format
) !=
2234 _mesa_is_enum_format_integer(internalFormat
))) {
2235 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2236 "glTexImage%dD(integer/non-integer format mismatch)",
2241 if (!mutable_tex_object(ctx
, target
)) {
2242 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2243 "glTexImage%dD(immutable texture)", dimensions
);
2247 /* if we get here, the parameters are OK */
2253 _mesa_compressed_texture_pixel_storage_error_check(struct gl_context
*ctx
,
2255 struct gl_pixelstore_attrib
*packing
,
2258 if (!_mesa_is_desktop_gl(ctx
) || !packing
->CompressedBlockSize
)
2261 if (packing
->CompressedBlockWidth
&& packing
->SkipPixels
% packing
->CompressedBlockWidth
) {
2262 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2263 "%s(skip-pixels %% block-width)", caller
);
2267 if (dimensions
> 1 && packing
->CompressedBlockHeight
&& packing
->SkipRows
% packing
->CompressedBlockHeight
) {
2268 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2269 "%s(skip-rows %% block-height)", caller
);
2273 if (dimensions
> 2 && packing
->CompressedBlockDepth
&& packing
->SkipImages
% packing
->CompressedBlockDepth
) {
2274 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2275 "%s(skip-images %% block-depth)", caller
);
2283 * Error checking for glCompressedTexImage[123]D().
2284 * Note that the width, height and depth values are not fully error checked
2286 * \return GL_TRUE if a error is found, GL_FALSE otherwise
2289 compressed_texture_error_check(struct gl_context
*ctx
, GLint dimensions
,
2290 GLenum target
, GLint level
,
2291 GLenum internalFormat
, GLsizei width
,
2292 GLsizei height
, GLsizei depth
, GLint border
,
2295 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
2297 GLenum error
= GL_NO_ERROR
;
2298 char *reason
= ""; /* no error */
2300 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
2302 error
= GL_INVALID_ENUM
;
2306 /* This will detect any invalid internalFormat value */
2307 if (!_mesa_is_compressed_format(ctx
, internalFormat
)) {
2308 _mesa_error(ctx
, GL_INVALID_ENUM
,
2309 "glCompressedTexImage%dD(internalFormat=%s)",
2310 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
));
2314 switch (internalFormat
) {
2315 case GL_PALETTE4_RGB8_OES
:
2316 case GL_PALETTE4_RGBA8_OES
:
2317 case GL_PALETTE4_R5_G6_B5_OES
:
2318 case GL_PALETTE4_RGBA4_OES
:
2319 case GL_PALETTE4_RGB5_A1_OES
:
2320 case GL_PALETTE8_RGB8_OES
:
2321 case GL_PALETTE8_RGBA8_OES
:
2322 case GL_PALETTE8_R5_G6_B5_OES
:
2323 case GL_PALETTE8_RGBA4_OES
:
2324 case GL_PALETTE8_RGB5_A1_OES
:
2325 /* check level (note that level should be zero or less!) */
2326 if (level
> 0 || level
< -maxLevels
) {
2328 error
= GL_INVALID_VALUE
;
2332 if (dimensions
!= 2) {
2333 reason
= "compressed paletted textures must be 2D";
2334 error
= GL_INVALID_OPERATION
;
2338 /* Figure out the expected texture size (in bytes). This will be
2339 * checked against the actual size later.
2341 expectedSize
= _mesa_cpal_compressed_size(level
, internalFormat
,
2344 /* This is for the benefit of the TestProxyTexImage below. It expects
2345 * level to be non-negative. OES_compressed_paletted_texture uses a
2346 * weird mechanism where the level specified to glCompressedTexImage2D
2347 * is -(n-1) number of levels in the texture, and the data specifies the
2348 * complete mipmap stack. This is done to ensure the palette is the
2349 * same for all levels.
2356 if (level
< 0 || level
>= maxLevels
) {
2358 error
= GL_INVALID_VALUE
;
2362 /* Figure out the expected texture size (in bytes). This will be
2363 * checked against the actual size later.
2365 expectedSize
= compressed_tex_size(width
, height
, depth
, internalFormat
);
2369 /* This should really never fail */
2370 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
2371 reason
= "internalFormat";
2372 error
= GL_INVALID_ENUM
;
2376 /* No compressed formats support borders at this time */
2378 reason
= "border != 0";
2379 error
= GL_INVALID_VALUE
;
2383 /* Check for invalid pixel storage modes */
2384 if (!_mesa_compressed_texture_pixel_storage_error_check(ctx
, dimensions
,
2386 "glCompressedTexImage")) {
2390 /* check image size in bytes */
2391 if (expectedSize
!= imageSize
) {
2392 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
2393 * if <imageSize> is not consistent with the format, dimensions, and
2394 * contents of the specified image.
2396 reason
= "imageSize inconsistant with width/height/format";
2397 error
= GL_INVALID_VALUE
;
2401 if (!mutable_tex_object(ctx
, target
)) {
2402 reason
= "immutable texture";
2403 error
= GL_INVALID_OPERATION
;
2410 /* Note: not all error paths exit through here. */
2411 _mesa_error(ctx
, error
, "glCompressedTexImage%dD(%s)", dimensions
, reason
);
2418 * Test glTexSubImage[123]D() parameters for errors.
2420 * \param ctx GL context.
2421 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2422 * \param target texture target given by the user (already validated)
2423 * \param level image level given by the user.
2424 * \param xoffset sub-image x offset given by the user.
2425 * \param yoffset sub-image y offset given by the user.
2426 * \param zoffset sub-image z offset given by the user.
2427 * \param format pixel data format given by the user.
2428 * \param type pixel data type given by the user.
2429 * \param width image width given by the user.
2430 * \param height image height given by the user.
2431 * \param depth image depth given by the user.
2433 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2435 * Verifies each of the parameters against the constants specified in
2436 * __struct gl_contextRec::Const and the supported extensions, and according
2437 * to the OpenGL specification.
2440 texsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2441 GLenum target
, GLint level
,
2442 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2443 GLint width
, GLint height
, GLint depth
,
2444 GLenum format
, GLenum type
)
2446 struct gl_texture_object
*texObj
;
2447 struct gl_texture_image
*texImage
;
2450 /* check target (proxies not allowed) */
2451 if (!legal_texsubimage_target(ctx
, dimensions
, target
)) {
2452 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage%uD(target=%s)",
2453 dimensions
, _mesa_lookup_enum_by_nr(target
));
2458 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2459 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage%uD(level=%d)",
2464 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2465 * combinations of format and type that can be used. Formats and types
2466 * that require additional extensions (e.g., GL_FLOAT requires
2467 * GL_OES_texture_float) are filtered elsewhere.
2469 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
2470 err
= _mesa_es_error_check_format_and_type(format
, type
, dimensions
);
2471 if (err
!= GL_NO_ERROR
) {
2472 _mesa_error(ctx
, err
,
2473 "glTexSubImage%dD(format = %s, type = %s)",
2475 _mesa_lookup_enum_by_nr(format
),
2476 _mesa_lookup_enum_by_nr(type
));
2481 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
2482 if (err
!= GL_NO_ERROR
) {
2483 _mesa_error(ctx
, err
,
2484 "glTexSubImage%dD(incompatible format = %s, type = %s)",
2485 dimensions
, _mesa_lookup_enum_by_nr(format
),
2486 _mesa_lookup_enum_by_nr(type
));
2490 /* Get dest texture object / image pointers */
2491 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2493 /* must be out of memory */
2494 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage%dD()", dimensions
);
2498 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2500 /* non-existant texture level */
2501 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2502 "glTexSubImage%dD(invalid texture image)", dimensions
);
2506 if (error_check_subtexture_dimensions(ctx
, "glTexSubImage", dimensions
,
2507 texImage
, xoffset
, yoffset
, 0,
2508 width
, height
, 1)) {
2512 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2513 if (compressedteximage_only_format(ctx
, texImage
->InternalFormat
)) {
2514 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2515 "glTexSubImage%dD(no compression for format)", dimensions
);
2520 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
2521 /* both source and dest must be integer-valued, or neither */
2522 if (_mesa_is_format_integer_color(texImage
->TexFormat
) !=
2523 _mesa_is_enum_format_integer(format
)) {
2524 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2525 "glTexSubImage%dD(integer/non-integer format mismatch)",
2536 * Test glCopyTexImage[12]D() parameters for errors.
2538 * \param ctx GL context.
2539 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2540 * \param target texture target given by the user.
2541 * \param level image level given by the user.
2542 * \param internalFormat internal format given by the user.
2543 * \param width image width given by the user.
2544 * \param height image height given by the user.
2545 * \param border texture border.
2547 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2549 * Verifies each of the parameters against the constants specified in
2550 * __struct gl_contextRec::Const and the supported extensions, and according
2551 * to the OpenGL specification.
2554 copytexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
2555 GLenum target
, GLint level
, GLint internalFormat
,
2556 GLint width
, GLint height
, GLint border
)
2559 GLint rb_base_format
;
2560 struct gl_renderbuffer
*rb
;
2561 GLenum rb_internal_format
;
2564 if (!legal_texsubimage_target(ctx
, dimensions
, target
)) {
2565 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexImage%uD(target=%s)",
2566 dimensions
, _mesa_lookup_enum_by_nr(target
));
2571 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2572 _mesa_error(ctx
, GL_INVALID_VALUE
,
2573 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
2577 /* Check that the source buffer is complete */
2578 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2579 if (ctx
->ReadBuffer
->_Status
== 0) {
2580 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2582 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2583 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2584 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
2588 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2589 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2590 "glCopyTexImage%dD(multisample FBO)",
2597 if (border
< 0 || border
> 1 ||
2598 ((ctx
->API
!= API_OPENGL_COMPAT
||
2599 target
== GL_TEXTURE_RECTANGLE_NV
||
2600 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
2601 _mesa_error(ctx
, GL_INVALID_VALUE
,
2602 "glCopyTexImage%dD(border=%d)", dimensions
, border
);
2606 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, internalFormat
);
2608 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2609 "glCopyTexImage%dD(read buffer)", dimensions
);
2613 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2616 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
2617 switch (internalFormat
) {
2622 case GL_LUMINANCE_ALPHA
:
2625 _mesa_error(ctx
, GL_INVALID_VALUE
,
2626 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2627 _mesa_lookup_enum_by_nr(internalFormat
));
2632 baseFormat
= _mesa_base_tex_format(ctx
, internalFormat
);
2633 if (baseFormat
< 0) {
2634 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2635 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2636 _mesa_lookup_enum_by_nr(internalFormat
));
2640 rb_internal_format
= rb
->InternalFormat
;
2641 rb_base_format
= _mesa_base_tex_format(ctx
, rb
->InternalFormat
);
2642 if (_mesa_is_color_format(internalFormat
)) {
2643 if (rb_base_format
< 0) {
2644 _mesa_error(ctx
, GL_INVALID_VALUE
,
2645 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2646 _mesa_lookup_enum_by_nr(internalFormat
));
2651 if (_mesa_is_gles(ctx
)) {
2653 if (_mesa_base_format_component_count(baseFormat
) >
2654 _mesa_base_format_component_count(rb_base_format
)) {
2657 if (baseFormat
== GL_DEPTH_COMPONENT
||
2658 baseFormat
== GL_DEPTH_STENCIL
||
2659 rb_base_format
== GL_DEPTH_COMPONENT
||
2660 rb_base_format
== GL_DEPTH_STENCIL
||
2661 ((baseFormat
== GL_LUMINANCE_ALPHA
||
2662 baseFormat
== GL_ALPHA
) &&
2663 rb_base_format
!= GL_RGBA
) ||
2664 internalFormat
== GL_RGB9_E5
) {
2667 if (internalFormat
== GL_RGB9_E5
) {
2671 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2672 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2673 _mesa_lookup_enum_by_nr(internalFormat
));
2678 if (_mesa_is_gles3(ctx
)) {
2679 bool rb_is_srgb
= false;
2680 bool dst_is_srgb
= false;
2682 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&&
2683 _mesa_get_format_color_encoding(rb
->Format
) == GL_SRGB
) {
2687 if (_mesa_get_linear_internalformat(internalFormat
) != internalFormat
) {
2691 if (rb_is_srgb
!= dst_is_srgb
) {
2692 /* Page 137 (page 149 of the PDF) in section 3.8.5 of the
2693 * OpenGLES 3.0.0 spec says:
2695 * "The error INVALID_OPERATION is also generated if the
2696 * value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the
2697 * framebuffer attachment corresponding to the read buffer
2698 * is LINEAR (see section 6.1.13) and internalformat is
2699 * one of the sRGB formats described in section 3.8.16, or
2700 * if the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING is
2701 * SRGB and internalformat is not one of the sRGB formats."
2703 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2704 "glCopyTexImage%dD(srgb usage mismatch)", dimensions
);
2709 if (!_mesa_source_buffer_exists(ctx
, baseFormat
)) {
2710 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2711 "glCopyTexImage%dD(missing readbuffer)", dimensions
);
2715 /* From the EXT_texture_integer spec:
2717 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2718 * if the texture internalformat is an integer format and the read color
2719 * buffer is not an integer format, or if the internalformat is not an
2720 * integer format and the read color buffer is an integer format."
2722 if (_mesa_is_color_format(internalFormat
)) {
2723 bool is_int
= _mesa_is_enum_format_integer(internalFormat
);
2724 bool is_rbint
= _mesa_is_enum_format_integer(rb_internal_format
);
2725 if (is_int
|| is_rbint
) {
2726 if (is_int
!= is_rbint
) {
2727 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2728 "glCopyTexImage%dD(integer vs non-integer)", dimensions
);
2730 } else if (_mesa_is_gles(ctx
) &&
2731 _mesa_is_enum_format_unsigned_int(internalFormat
) !=
2732 _mesa_is_enum_format_unsigned_int(rb_internal_format
)) {
2733 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2734 "glCopyTexImage%dD(signed vs unsigned integer)", dimensions
);
2740 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
2741 if (!target_can_be_compressed(ctx
, target
, internalFormat
)) {
2742 _mesa_error(ctx
, GL_INVALID_ENUM
,
2743 "glCopyTexImage%dD(target)", dimensions
);
2746 if (compressedteximage_only_format(ctx
, internalFormat
)) {
2747 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2748 "glCopyTexImage%dD(no compression for format)", dimensions
);
2752 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2753 "glCopyTexImage%dD(border!=0)", dimensions
);
2758 if (!mutable_tex_object(ctx
, target
)) {
2759 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2760 "glCopyTexImage%dD(immutable texture)", dimensions
);
2764 /* if we get here, the parameters are OK */
2770 * Test glCopyTexSubImage[12]D() parameters for errors.
2771 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2774 copytexsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2775 GLenum target
, GLint level
,
2776 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2777 GLint width
, GLint height
)
2779 struct gl_texture_object
*texObj
;
2780 struct gl_texture_image
*texImage
;
2782 /* Check that the source buffer is complete */
2783 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2784 if (ctx
->ReadBuffer
->_Status
== 0) {
2785 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2787 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2788 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2789 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
2793 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2794 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2795 "glCopyTexSubImage%dD(multisample FBO)",
2801 /* check target (proxies not allowed) */
2802 if (!legal_texsubimage_target(ctx
, dimensions
, target
)) {
2803 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage%uD(target=%s)",
2804 dimensions
, _mesa_lookup_enum_by_nr(target
));
2809 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2810 _mesa_error(ctx
, GL_INVALID_VALUE
,
2811 "glCopyTexSubImage%dD(level=%d)", dimensions
, level
);
2815 /* Get dest texture object / image pointers */
2816 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2818 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage%dD()", dimensions
);
2822 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2824 /* destination image does not exist */
2825 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2826 "glCopyTexSubImage%dD(invalid texture image)", dimensions
);
2830 if (error_check_subtexture_dimensions(ctx
, "glCopyTexSubImage",
2831 dimensions
, texImage
,
2832 xoffset
, yoffset
, zoffset
,
2833 width
, height
, 1)) {
2837 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2838 if (compressedteximage_only_format(ctx
, texImage
->InternalFormat
)) {
2839 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2840 "glCopyTexSubImage%dD(no compression for format)", dimensions
);
2845 if (texImage
->InternalFormat
== GL_YCBCR_MESA
) {
2846 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCopyTexSubImage2D");
2850 if (!_mesa_source_buffer_exists(ctx
, texImage
->_BaseFormat
)) {
2851 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2852 "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
2853 dimensions
, texImage
->_BaseFormat
);
2857 /* From the EXT_texture_integer spec:
2859 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2860 * if the texture internalformat is an integer format and the read color
2861 * buffer is not an integer format, or if the internalformat is not an
2862 * integer format and the read color buffer is an integer format."
2864 if (_mesa_is_color_format(texImage
->InternalFormat
)) {
2865 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
2867 if (_mesa_is_format_integer_color(rb
->Format
) !=
2868 _mesa_is_format_integer_color(texImage
->TexFormat
)) {
2869 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2870 "glCopyTexImage%dD(integer vs non-integer)", dimensions
);
2875 /* if we get here, the parameters are OK */
2880 /** Callback info for walking over FBO hash table */
2883 struct gl_context
*ctx
;
2884 struct gl_texture_object
*texObj
;
2890 * Check render to texture callback. Called from _mesa_HashWalk().
2893 check_rtt_cb(GLuint key
, void *data
, void *userData
)
2895 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
2896 const struct cb_info
*info
= (struct cb_info
*) userData
;
2897 struct gl_context
*ctx
= info
->ctx
;
2898 const struct gl_texture_object
*texObj
= info
->texObj
;
2899 const GLuint level
= info
->level
, face
= info
->face
;
2901 /* If this is a user-created FBO */
2902 if (_mesa_is_user_fbo(fb
)) {
2904 /* check if any of the FBO's attachments point to 'texObj' */
2905 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2906 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2907 if (att
->Type
== GL_TEXTURE
&&
2908 att
->Texture
== texObj
&&
2909 att
->TextureLevel
== level
&&
2910 att
->CubeMapFace
== face
) {
2911 _mesa_update_texture_renderbuffer(ctx
, ctx
->DrawBuffer
, att
);
2912 ASSERT(att
->Renderbuffer
->TexImage
);
2913 /* Mark fb status as indeterminate to force re-validation */
2922 * When a texture image is specified we have to check if it's bound to
2923 * any framebuffer objects (render to texture) in order to detect changes
2924 * in size or format since that effects FBO completeness.
2925 * Any FBOs rendering into the texture must be re-validated.
2928 _mesa_update_fbo_texture(struct gl_context
*ctx
,
2929 struct gl_texture_object
*texObj
,
2930 GLuint face
, GLuint level
)
2932 /* Only check this texture if it's been marked as RenderToTexture */
2933 if (texObj
->_RenderToTexture
) {
2934 struct cb_info info
;
2936 info
.texObj
= texObj
;
2939 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, check_rtt_cb
, &info
);
2945 * If the texture object's GenerateMipmap flag is set and we've
2946 * changed the texture base level image, regenerate the rest of the
2947 * mipmap levels now.
2950 check_gen_mipmap(struct gl_context
*ctx
, GLenum target
,
2951 struct gl_texture_object
*texObj
, GLint level
)
2953 ASSERT(target
!= GL_TEXTURE_CUBE_MAP
);
2954 if (texObj
->GenerateMipmap
&&
2955 level
== texObj
->BaseLevel
&&
2956 level
< texObj
->MaxLevel
) {
2957 ASSERT(ctx
->Driver
.GenerateMipmap
);
2958 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2963 /** Debug helper: override the user-requested internal format */
2965 override_internal_format(GLenum internalFormat
, GLint width
, GLint height
)
2968 if (internalFormat
== GL_RGBA16F_ARB
||
2969 internalFormat
== GL_RGBA32F_ARB
) {
2970 printf("Convert rgba float tex to int %d x %d\n", width
, height
);
2973 else if (internalFormat
== GL_RGB16F_ARB
||
2974 internalFormat
== GL_RGB32F_ARB
) {
2975 printf("Convert rgb float tex to int %d x %d\n", width
, height
);
2978 else if (internalFormat
== GL_LUMINANCE_ALPHA16F_ARB
||
2979 internalFormat
== GL_LUMINANCE_ALPHA32F_ARB
) {
2980 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2981 return GL_LUMINANCE_ALPHA
;
2983 else if (internalFormat
== GL_LUMINANCE16F_ARB
||
2984 internalFormat
== GL_LUMINANCE32F_ARB
) {
2985 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2986 return GL_LUMINANCE
;
2988 else if (internalFormat
== GL_ALPHA16F_ARB
||
2989 internalFormat
== GL_ALPHA32F_ARB
) {
2990 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2994 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2995 internalFormat = GL_RGBA;
2999 return internalFormat
;
3002 return internalFormat
;
3008 * Choose the actual hardware format for a texture image.
3009 * Try to use the same format as the previous image level when possible.
3010 * Otherwise, ask the driver for the best format.
3011 * It's important to try to choose a consistant format for all levels
3012 * for efficient texture memory layout/allocation. In particular, this
3013 * comes up during automatic mipmap generation.
3016 _mesa_choose_texture_format(struct gl_context
*ctx
,
3017 struct gl_texture_object
*texObj
,
3018 GLenum target
, GLint level
,
3019 GLenum internalFormat
, GLenum format
, GLenum type
)
3023 /* see if we've already chosen a format for the previous level */
3025 struct gl_texture_image
*prevImage
=
3026 _mesa_select_tex_image(ctx
, texObj
, target
, level
- 1);
3027 /* See if the prev level is defined and has an internal format which
3028 * matches the new internal format.
3031 prevImage
->Width
> 0 &&
3032 prevImage
->InternalFormat
== internalFormat
) {
3033 /* use the same format */
3034 ASSERT(prevImage
->TexFormat
!= MESA_FORMAT_NONE
);
3035 return prevImage
->TexFormat
;
3039 /* If the application requested compression to an S3TC format but we don't
3040 * have the DXTn library, force a generic compressed format instead.
3042 if (internalFormat
!= format
&& format
!= GL_NONE
) {
3043 const GLenum before
= internalFormat
;
3045 switch (internalFormat
) {
3046 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
3047 if (!ctx
->Mesa_DXTn
)
3048 internalFormat
= GL_COMPRESSED_RGB
;
3050 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
3051 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
3052 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
3053 if (!ctx
->Mesa_DXTn
)
3054 internalFormat
= GL_COMPRESSED_RGBA
;
3060 if (before
!= internalFormat
) {
3062 "DXT compression requested (%s), "
3063 "but libtxc_dxtn library not installed. Using %s "
3065 _mesa_lookup_enum_by_nr(before
),
3066 _mesa_lookup_enum_by_nr(internalFormat
));
3070 /* choose format from scratch */
3071 f
= ctx
->Driver
.ChooseTextureFormat(ctx
, target
, internalFormat
,
3073 ASSERT(f
!= MESA_FORMAT_NONE
);
3079 * Adjust pixel unpack params and image dimensions to strip off the
3080 * one-pixel texture border.
3082 * Gallium and intel don't support texture borders. They've seldem been used
3083 * and seldom been implemented correctly anyway.
3085 * \param unpackNew returns the new pixel unpack parameters
3088 strip_texture_border(GLenum target
,
3089 GLint
*width
, GLint
*height
, GLint
*depth
,
3090 const struct gl_pixelstore_attrib
*unpack
,
3091 struct gl_pixelstore_attrib
*unpackNew
)
3097 *unpackNew
= *unpack
;
3099 if (unpackNew
->RowLength
== 0)
3100 unpackNew
->RowLength
= *width
;
3102 if (unpackNew
->ImageHeight
== 0)
3103 unpackNew
->ImageHeight
= *height
;
3105 assert(*width
>= 3);
3106 unpackNew
->SkipPixels
++; /* skip the border */
3107 *width
= *width
- 2; /* reduce the width by two border pixels */
3109 /* The min height of a texture with a border is 3 */
3110 if (*height
>= 3 && target
!= GL_TEXTURE_1D_ARRAY
) {
3111 unpackNew
->SkipRows
++; /* skip the border */
3112 *height
= *height
- 2; /* reduce the height by two border pixels */
3116 target
!= GL_TEXTURE_2D_ARRAY
&&
3117 target
!= GL_TEXTURE_CUBE_MAP_ARRAY
) {
3118 unpackNew
->SkipImages
++; /* skip the border */
3119 *depth
= *depth
- 2; /* reduce the depth by two border pixels */
3125 * Common code to implement all the glTexImage1D/2D/3D functions
3126 * as well as glCompressedTexImage1D/2D/3D.
3127 * \param compressed only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
3128 * \param format the user's image format (only used if !compressed)
3129 * \param type the user's image type (only used if !compressed)
3130 * \param imageSize only used for glCompressedTexImage1D/2D/3D calls.
3133 teximage(struct gl_context
*ctx
, GLboolean compressed
, GLuint dims
,
3134 GLenum target
, GLint level
, GLint internalFormat
,
3135 GLsizei width
, GLsizei height
, GLsizei depth
,
3136 GLint border
, GLenum format
, GLenum type
,
3137 GLsizei imageSize
, const GLvoid
*pixels
)
3139 const char *func
= compressed
? "glCompressedTexImage" : "glTexImage";
3140 struct gl_pixelstore_attrib unpack_no_border
;
3141 const struct gl_pixelstore_attrib
*unpack
= &ctx
->Unpack
;
3142 struct gl_texture_object
*texObj
;
3143 mesa_format texFormat
;
3144 GLboolean dimensionsOK
, sizeOK
;
3146 FLUSH_VERTICES(ctx
, 0);
3148 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
)) {
3151 "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
3153 _mesa_lookup_enum_by_nr(target
), level
,
3154 _mesa_lookup_enum_by_nr(internalFormat
),
3155 width
, height
, depth
, border
, pixels
);
3158 "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
3160 _mesa_lookup_enum_by_nr(target
), level
,
3161 _mesa_lookup_enum_by_nr(internalFormat
),
3162 width
, height
, depth
, border
,
3163 _mesa_lookup_enum_by_nr(format
),
3164 _mesa_lookup_enum_by_nr(type
), pixels
);
3167 internalFormat
= override_internal_format(internalFormat
, width
, height
);
3169 /* target error checking */
3170 if (!legal_teximage_target(ctx
, dims
, target
)) {
3171 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s%uD(target=%s)",
3172 func
, dims
, _mesa_lookup_enum_by_nr(target
));
3176 /* general error checking */
3178 if (compressed_texture_error_check(ctx
, dims
, target
, level
,
3180 width
, height
, depth
,
3185 if (texture_error_check(ctx
, dims
, target
, level
, internalFormat
,
3186 format
, type
, width
, height
, depth
, border
))
3190 /* Here we convert a cpal compressed image into a regular glTexImage2D
3191 * call by decompressing the texture. If we really want to support cpal
3192 * textures in any driver this would have to be changed.
3194 if (ctx
->API
== API_OPENGLES
&& compressed
&& dims
== 2) {
3195 switch (internalFormat
) {
3196 case GL_PALETTE4_RGB8_OES
:
3197 case GL_PALETTE4_RGBA8_OES
:
3198 case GL_PALETTE4_R5_G6_B5_OES
:
3199 case GL_PALETTE4_RGBA4_OES
:
3200 case GL_PALETTE4_RGB5_A1_OES
:
3201 case GL_PALETTE8_RGB8_OES
:
3202 case GL_PALETTE8_RGBA8_OES
:
3203 case GL_PALETTE8_R5_G6_B5_OES
:
3204 case GL_PALETTE8_RGBA4_OES
:
3205 case GL_PALETTE8_RGB5_A1_OES
:
3206 _mesa_cpal_compressed_teximage2d(target
, level
, internalFormat
,
3207 width
, height
, imageSize
, pixels
);
3212 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3216 /* For glCompressedTexImage() the driver has no choice about the
3217 * texture format since we'll never transcode the user's compressed
3218 * image data. The internalFormat was error checked earlier.
3220 texFormat
= _mesa_glenum_to_compressed_format(internalFormat
);
3223 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3224 internalFormat
, format
, type
);
3227 assert(texFormat
!= MESA_FORMAT_NONE
);
3229 /* check that width, height, depth are legal for the mipmap level */
3230 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, level
, width
,
3231 height
, depth
, border
);
3233 /* check that the texture won't take too much memory, etc */
3234 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target(target
),
3236 width
, height
, depth
, border
);
3238 if (_mesa_is_proxy_texture(target
)) {
3239 /* Proxy texture: just clear or set state depending on error checking */
3240 struct gl_texture_image
*texImage
=
3241 get_proxy_tex_image(ctx
, target
, level
);
3244 return; /* GL_OUT_OF_MEMORY already recorded */
3246 if (dimensionsOK
&& sizeOK
) {
3247 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, depth
,
3248 border
, internalFormat
, texFormat
);
3251 clear_teximage_fields(texImage
);
3255 /* non-proxy target */
3256 const GLuint face
= _mesa_tex_target_to_face(target
);
3257 struct gl_texture_image
*texImage
;
3259 if (!dimensionsOK
) {
3260 _mesa_error(ctx
, GL_INVALID_VALUE
,
3261 "glTexImage%uD(invalid width or height or depth)",
3267 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3268 "glTexImage%uD(image too large)", dims
);
3272 /* Allow a hardware driver to just strip out the border, to provide
3273 * reliable but slightly incorrect hardware rendering instead of
3274 * rarely-tested software fallback rendering.
3276 if (border
&& ctx
->Const
.StripTextureBorder
) {
3277 strip_texture_border(target
, &width
, &height
, &depth
, unpack
,
3280 unpack
= &unpack_no_border
;
3283 if (ctx
->NewState
& _NEW_PIXEL
)
3284 _mesa_update_state(ctx
);
3286 _mesa_lock_texture(ctx
, texObj
);
3288 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3291 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s%uD", func
, dims
);
3294 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3296 _mesa_init_teximage_fields(ctx
, texImage
,
3297 width
, height
, depth
,
3298 border
, internalFormat
, texFormat
);
3300 /* Give the texture to the driver. <pixels> may be null. */
3301 if (width
> 0 && height
> 0 && depth
> 0) {
3303 ctx
->Driver
.CompressedTexImage(ctx
, dims
, texImage
,
3307 ctx
->Driver
.TexImage(ctx
, dims
, texImage
, format
,
3308 type
, pixels
, unpack
);
3312 check_gen_mipmap(ctx
, target
, texObj
, level
);
3314 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3316 _mesa_dirty_texobj(ctx
, texObj
);
3319 _mesa_unlock_texture(ctx
, texObj
);
3326 * Called from the API. Note that width includes the border.
3329 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
3330 GLsizei width
, GLint border
, GLenum format
,
3331 GLenum type
, const GLvoid
*pixels
)
3333 GET_CURRENT_CONTEXT(ctx
);
3334 teximage(ctx
, GL_FALSE
, 1, target
, level
, internalFormat
, width
, 1, 1,
3335 border
, format
, type
, 0, pixels
);
3340 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
3341 GLsizei width
, GLsizei height
, GLint border
,
3342 GLenum format
, GLenum type
,
3343 const GLvoid
*pixels
)
3345 GET_CURRENT_CONTEXT(ctx
);
3346 teximage(ctx
, GL_FALSE
, 2, target
, level
, internalFormat
, width
, height
, 1,
3347 border
, format
, type
, 0, pixels
);
3352 * Called by the API or display list executor.
3353 * Note that width and height include the border.
3356 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
3357 GLsizei width
, GLsizei height
, GLsizei depth
,
3358 GLint border
, GLenum format
, GLenum type
,
3359 const GLvoid
*pixels
)
3361 GET_CURRENT_CONTEXT(ctx
);
3362 teximage(ctx
, GL_FALSE
, 3, target
, level
, internalFormat
,
3363 width
, height
, depth
,
3364 border
, format
, type
, 0, pixels
);
3369 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
3370 GLsizei width
, GLsizei height
, GLsizei depth
,
3371 GLint border
, GLenum format
, GLenum type
,
3372 const GLvoid
*pixels
)
3374 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
3375 depth
, border
, format
, type
, pixels
);
3380 _mesa_EGLImageTargetTexture2DOES (GLenum target
, GLeglImageOES image
)
3382 struct gl_texture_object
*texObj
;
3383 struct gl_texture_image
*texImage
;
3385 GET_CURRENT_CONTEXT(ctx
);
3386 FLUSH_VERTICES(ctx
, 0);
3390 valid_target
= ctx
->Extensions
.OES_EGL_image
;
3392 case GL_TEXTURE_EXTERNAL_OES
:
3394 _mesa_is_gles(ctx
) ? ctx
->Extensions
.OES_EGL_image_external
: false;
3397 valid_target
= false;
3401 if (!valid_target
) {
3402 _mesa_error(ctx
, GL_INVALID_ENUM
,
3403 "glEGLImageTargetTexture2D(target=%d)", target
);
3408 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3409 "glEGLImageTargetTexture2D(image=%p)", image
);
3413 if (ctx
->NewState
& _NEW_PIXEL
)
3414 _mesa_update_state(ctx
);
3416 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3417 _mesa_lock_texture(ctx
, texObj
);
3419 if (texObj
->Immutable
) {
3420 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3421 "glEGLImageTargetTexture2D(texture is immutable)");
3422 _mesa_unlock_texture(ctx
, texObj
);
3426 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
3428 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glEGLImageTargetTexture2D");
3430 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3432 ctx
->Driver
.EGLImageTargetTexture2D(ctx
, target
,
3433 texObj
, texImage
, image
);
3435 _mesa_dirty_texobj(ctx
, texObj
);
3437 _mesa_unlock_texture(ctx
, texObj
);
3444 * Implement all the glTexSubImage1/2/3D() functions.
3447 texsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
3448 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3449 GLsizei width
, GLsizei height
, GLsizei depth
,
3450 GLenum format
, GLenum type
, const GLvoid
*pixels
)
3452 struct gl_texture_object
*texObj
;
3453 struct gl_texture_image
*texImage
;
3455 FLUSH_VERTICES(ctx
, 0);
3457 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3458 _mesa_debug(ctx
, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3460 _mesa_lookup_enum_by_nr(target
), level
,
3461 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3462 _mesa_lookup_enum_by_nr(format
),
3463 _mesa_lookup_enum_by_nr(type
), pixels
);
3465 /* check target (proxies not allowed) */
3466 if (!legal_texsubimage_target(ctx
, dims
, target
)) {
3467 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage%uD(target=%s)",
3468 dims
, _mesa_lookup_enum_by_nr(target
));
3472 if (ctx
->NewState
& _NEW_PIXEL
)
3473 _mesa_update_state(ctx
);
3475 if (texsubimage_error_check(ctx
, dims
, target
, level
,
3476 xoffset
, yoffset
, zoffset
,
3477 width
, height
, depth
, format
, type
)) {
3478 return; /* error was detected */
3481 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3483 _mesa_lock_texture(ctx
, texObj
);
3485 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3487 if (width
> 0 && height
> 0 && depth
> 0) {
3488 /* If we have a border, offset=-1 is legal. Bias by border width. */
3491 if (target
!= GL_TEXTURE_2D_ARRAY
)
3492 zoffset
+= texImage
->Border
;
3495 if (target
!= GL_TEXTURE_1D_ARRAY
)
3496 yoffset
+= texImage
->Border
;
3499 xoffset
+= texImage
->Border
;
3502 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3503 xoffset
, yoffset
, zoffset
,
3504 width
, height
, depth
,
3505 format
, type
, pixels
, &ctx
->Unpack
);
3507 check_gen_mipmap(ctx
, target
, texObj
, level
);
3509 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3510 * the texel data, not the texture format, size, etc.
3514 _mesa_unlock_texture(ctx
, texObj
);
3519 _mesa_TexSubImage1D( GLenum target
, GLint level
,
3520 GLint xoffset
, GLsizei width
,
3521 GLenum format
, GLenum type
,
3522 const GLvoid
*pixels
)
3524 GET_CURRENT_CONTEXT(ctx
);
3525 texsubimage(ctx
, 1, target
, level
,
3528 format
, type
, pixels
);
3533 _mesa_TexSubImage2D( GLenum target
, GLint level
,
3534 GLint xoffset
, GLint yoffset
,
3535 GLsizei width
, GLsizei height
,
3536 GLenum format
, GLenum type
,
3537 const GLvoid
*pixels
)
3539 GET_CURRENT_CONTEXT(ctx
);
3540 texsubimage(ctx
, 2, target
, level
,
3541 xoffset
, yoffset
, 0,
3543 format
, type
, pixels
);
3549 _mesa_TexSubImage3D( GLenum target
, GLint level
,
3550 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3551 GLsizei width
, GLsizei height
, GLsizei depth
,
3552 GLenum format
, GLenum type
,
3553 const GLvoid
*pixels
)
3555 GET_CURRENT_CONTEXT(ctx
);
3556 texsubimage(ctx
, 3, target
, level
,
3557 xoffset
, yoffset
, zoffset
,
3558 width
, height
, depth
,
3559 format
, type
, pixels
);
3565 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3566 * from. This depends on whether the texture contains color or depth values.
3568 static struct gl_renderbuffer
*
3569 get_copy_tex_image_source(struct gl_context
*ctx
, mesa_format texFormat
)
3571 if (_mesa_get_format_bits(texFormat
, GL_DEPTH_BITS
) > 0) {
3572 /* reading from depth/stencil buffer */
3573 return ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
3576 /* copying from color buffer */
3577 return ctx
->ReadBuffer
->_ColorReadBuffer
;
3582 copytexsubimage_by_slice(struct gl_context
*ctx
,
3583 struct gl_texture_image
*texImage
,
3585 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3586 struct gl_renderbuffer
*rb
,
3588 GLsizei width
, GLsizei height
)
3590 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
3593 /* For 1D arrays, we copy each scanline of the source rectangle into the
3596 assert(zoffset
== 0);
3598 for (slice
= 0; slice
< height
; slice
++) {
3599 assert(yoffset
+ slice
< texImage
->Height
);
3600 ctx
->Driver
.CopyTexSubImage(ctx
, 2, texImage
,
3601 xoffset
, 0, yoffset
+ slice
,
3602 rb
, x
, y
+ slice
, width
, 1);
3605 ctx
->Driver
.CopyTexSubImage(ctx
, dims
, texImage
,
3606 xoffset
, yoffset
, zoffset
,
3607 rb
, x
, y
, width
, height
);
3613 * Implement the glCopyTexImage1/2D() functions.
3616 copyteximage(struct gl_context
*ctx
, GLuint dims
,
3617 GLenum target
, GLint level
, GLenum internalFormat
,
3618 GLint x
, GLint y
, GLsizei width
, GLsizei height
, GLint border
)
3620 struct gl_texture_object
*texObj
;
3621 struct gl_texture_image
*texImage
;
3622 const GLuint face
= _mesa_tex_target_to_face(target
);
3623 mesa_format texFormat
;
3625 FLUSH_VERTICES(ctx
, 0);
3627 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3628 _mesa_debug(ctx
, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3630 _mesa_lookup_enum_by_nr(target
), level
,
3631 _mesa_lookup_enum_by_nr(internalFormat
),
3632 x
, y
, width
, height
, border
);
3634 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3635 _mesa_update_state(ctx
);
3637 if (copytexture_error_check(ctx
, dims
, target
, level
, internalFormat
,
3638 width
, height
, border
))
3641 if (!_mesa_legal_texture_dimensions(ctx
, target
, level
, width
, height
,
3643 _mesa_error(ctx
, GL_INVALID_VALUE
,
3644 "glCopyTexImage%uD(invalid width or height)", dims
);
3648 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3651 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3652 internalFormat
, GL_NONE
, GL_NONE
);
3653 assert(texFormat
!= MESA_FORMAT_NONE
);
3655 if (!ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target(target
),
3657 width
, height
, 1, border
)) {
3658 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3659 "glCopyTexImage%uD(image too large)", dims
);
3663 if (border
&& ctx
->Const
.StripTextureBorder
) {
3665 width
-= border
* 2;
3668 height
-= border
* 2;
3673 _mesa_lock_texture(ctx
, texObj
);
3675 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3678 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
3681 GLint srcX
= x
, srcY
= y
, dstX
= 0, dstY
= 0, dstZ
= 0;
3683 /* Free old texture image */
3684 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3686 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, 1,
3687 border
, internalFormat
, texFormat
);
3689 if (width
&& height
) {
3690 /* Allocate texture memory (no pixel data yet) */
3691 ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
);
3693 if (_mesa_clip_copytexsubimage(ctx
, &dstX
, &dstY
, &srcX
, &srcY
,
3695 struct gl_renderbuffer
*srcRb
=
3696 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3698 copytexsubimage_by_slice(ctx
, texImage
, dims
,
3700 srcRb
, srcX
, srcY
, width
, height
);
3703 check_gen_mipmap(ctx
, target
, texObj
, level
);
3706 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3708 _mesa_dirty_texobj(ctx
, texObj
);
3711 _mesa_unlock_texture(ctx
, texObj
);
3717 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
3718 GLenum internalFormat
,
3720 GLsizei width
, GLint border
)
3722 GET_CURRENT_CONTEXT(ctx
);
3723 copyteximage(ctx
, 1, target
, level
, internalFormat
, x
, y
, width
, 1, border
);
3729 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
3730 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
3733 GET_CURRENT_CONTEXT(ctx
);
3734 copyteximage(ctx
, 2, target
, level
, internalFormat
,
3735 x
, y
, width
, height
, border
);
3741 * Implementation for glCopyTexSubImage1/2/3D() functions.
3744 copytexsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
3745 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3746 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3748 struct gl_texture_object
*texObj
;
3749 struct gl_texture_image
*texImage
;
3751 FLUSH_VERTICES(ctx
, 0);
3753 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3754 _mesa_debug(ctx
, "glCopyTexSubImage%uD %s %d %d %d %d %d %d %d %d\n",
3756 _mesa_lookup_enum_by_nr(target
),
3757 level
, xoffset
, yoffset
, zoffset
, x
, y
, width
, height
);
3759 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3760 _mesa_update_state(ctx
);
3762 if (copytexsubimage_error_check(ctx
, dims
, target
, level
,
3763 xoffset
, yoffset
, zoffset
, width
, height
)) {
3767 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3769 _mesa_lock_texture(ctx
, texObj
);
3771 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3773 /* If we have a border, offset=-1 is legal. Bias by border width. */
3776 if (target
!= GL_TEXTURE_2D_ARRAY
)
3777 zoffset
+= texImage
->Border
;
3780 if (target
!= GL_TEXTURE_1D_ARRAY
)
3781 yoffset
+= texImage
->Border
;
3784 xoffset
+= texImage
->Border
;
3787 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3789 struct gl_renderbuffer
*srcRb
=
3790 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3792 copytexsubimage_by_slice(ctx
, texImage
, dims
,
3793 xoffset
, yoffset
, zoffset
,
3794 srcRb
, x
, y
, width
, height
);
3796 check_gen_mipmap(ctx
, target
, texObj
, level
);
3798 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3799 * the texel data, not the texture format, size, etc.
3803 _mesa_unlock_texture(ctx
, texObj
);
3808 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
3809 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
3811 GET_CURRENT_CONTEXT(ctx
);
3812 copytexsubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, x
, y
, width
, 1);
3818 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
3819 GLint xoffset
, GLint yoffset
,
3820 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3822 GET_CURRENT_CONTEXT(ctx
);
3823 copytexsubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0, x
, y
,
3830 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
3831 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3832 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3834 GET_CURRENT_CONTEXT(ctx
);
3835 copytexsubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
3836 x
, y
, width
, height
);
3840 check_clear_tex_image(struct gl_context
*ctx
,
3841 const char *function
,
3842 struct gl_texture_image
*texImage
,
3843 GLenum format
, GLenum type
,
3845 GLubyte
*clearValue
)
3847 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3848 static const GLubyte zeroData
[MAX_PIXEL_BYTES
];
3849 GLenum internalFormat
= texImage
->InternalFormat
;
3852 if (texObj
->Target
== GL_TEXTURE_BUFFER
) {
3853 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3854 "%s(buffer texture)", function
);
3858 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
3859 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3860 "%s(compressed texture)", function
);
3864 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
3865 if (err
!= GL_NO_ERROR
) {
3866 _mesa_error(ctx
, err
,
3867 "%s(incompatible format = %s, type = %s)",
3869 _mesa_lookup_enum_by_nr(format
),
3870 _mesa_lookup_enum_by_nr(type
));
3874 /* make sure internal format and format basically agree */
3875 if (!texture_formats_agree(internalFormat
, format
)) {
3876 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3877 "%s(incompatible internalFormat = %s, format = %s)",
3879 _mesa_lookup_enum_by_nr(internalFormat
),
3880 _mesa_lookup_enum_by_nr(format
));
3884 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
3885 /* both source and dest must be integer-valued, or neither */
3886 if (_mesa_is_format_integer_color(texImage
->TexFormat
) !=
3887 _mesa_is_enum_format_integer(format
)) {
3888 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3889 "%s(integer/non-integer format mismatch)",
3895 if (!_mesa_texstore(ctx
,
3897 texImage
->_BaseFormat
,
3898 texImage
->TexFormat
,
3899 0, /* dstRowStride */
3901 1, 1, 1, /* srcWidth/Height/Depth */
3903 data
? data
: zeroData
,
3904 &ctx
->DefaultPacking
)) {
3905 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid format)", function
);
3912 static struct gl_texture_object
*
3913 get_tex_obj_for_clear(struct gl_context
*ctx
,
3914 const char *function
,
3917 struct gl_texture_object
*texObj
;
3920 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(zero texture)", function
);
3924 texObj
= _mesa_lookup_texture(ctx
, texture
);
3926 if (texObj
== NULL
) {
3927 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", function
);
3931 if (texObj
->Target
== 0) {
3932 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unbound tex)", function
);
3940 get_tex_images_for_clear(struct gl_context
*ctx
,
3941 const char *function
,
3942 struct gl_texture_object
*texObj
,
3944 struct gl_texture_image
**texImages
)
3949 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
3950 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid level)", function
);
3954 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
3955 for (i
= 0; i
< MAX_FACES
; i
++) {
3956 target
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ i
;
3958 texImages
[i
] = _mesa_select_tex_image(ctx
, texObj
, target
, level
);
3959 if (texImages
[i
] == NULL
) {
3960 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3961 "%s(invalid level)", function
);
3969 texImages
[0] = _mesa_select_tex_image(ctx
, texObj
, texObj
->Target
, level
);
3971 if (texImages
[0] == NULL
) {
3972 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid level)", function
);
3980 _mesa_ClearTexSubImage( GLuint texture
, GLint level
,
3981 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3982 GLsizei width
, GLsizei height
, GLsizei depth
,
3983 GLenum format
, GLenum type
, const void *data
)
3985 GET_CURRENT_CONTEXT(ctx
);
3986 struct gl_texture_object
*texObj
;
3987 struct gl_texture_image
*texImages
[MAX_FACES
];
3988 GLubyte clearValue
[MAX_FACES
][MAX_PIXEL_BYTES
];
3990 int minDepth
, maxDepth
;
3992 texObj
= get_tex_obj_for_clear(ctx
, "glClearTexSubImage", texture
);
3997 _mesa_lock_texture(ctx
, texObj
);
3999 numImages
= get_tex_images_for_clear(ctx
, "glClearTexSubImage",
4000 texObj
, level
, texImages
);
4004 if (numImages
== 1) {
4005 minDepth
= -(int) texImages
[0]->Border
;
4006 maxDepth
= texImages
[0]->Depth
;
4009 maxDepth
= numImages
;
4012 if (xoffset
< -(GLint
) texImages
[0]->Border
||
4013 yoffset
< -(GLint
) texImages
[0]->Border
||
4014 zoffset
< minDepth
||
4018 xoffset
+ width
> texImages
[0]->Width
||
4019 yoffset
+ height
> texImages
[0]->Height
||
4020 zoffset
+ depth
> maxDepth
) {
4021 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4022 "glClearSubTexImage(invalid dimensions)");
4026 if (numImages
== 1) {
4027 if (check_clear_tex_image(ctx
, "glClearTexSubImage",
4029 format
, type
, data
, clearValue
[0])) {
4030 ctx
->Driver
.ClearTexSubImage(ctx
,
4032 xoffset
, yoffset
, zoffset
,
4033 width
, height
, depth
,
4034 data
? clearValue
[0] : NULL
);
4037 for (i
= zoffset
; i
< zoffset
+ depth
; i
++) {
4038 if (!check_clear_tex_image(ctx
, "glClearTexSubImage",
4040 format
, type
, data
, clearValue
[i
]))
4043 for (i
= zoffset
; i
< zoffset
+ depth
; i
++) {
4044 ctx
->Driver
.ClearTexSubImage(ctx
,
4046 xoffset
, yoffset
, 0,
4048 data
? clearValue
[i
] : NULL
);
4053 _mesa_unlock_texture(ctx
, texObj
);
4057 _mesa_ClearTexImage( GLuint texture
, GLint level
,
4058 GLenum format
, GLenum type
, const void *data
)
4060 GET_CURRENT_CONTEXT(ctx
);
4061 struct gl_texture_object
*texObj
;
4062 struct gl_texture_image
*texImages
[MAX_FACES
];
4063 GLubyte clearValue
[MAX_FACES
][MAX_PIXEL_BYTES
];
4066 texObj
= get_tex_obj_for_clear(ctx
, "glClearTexImage", texture
);
4071 _mesa_lock_texture(ctx
, texObj
);
4073 numImages
= get_tex_images_for_clear(ctx
, "glClearTexImage",
4074 texObj
, level
, texImages
);
4076 for (i
= 0; i
< numImages
; i
++) {
4077 if (!check_clear_tex_image(ctx
, "glClearTexImage",
4084 for (i
= 0; i
< numImages
; i
++) {
4085 ctx
->Driver
.ClearTexSubImage(ctx
, texImages
[i
],
4086 -(GLint
) texImages
[i
]->Border
, /* xoffset */
4087 -(GLint
) texImages
[i
]->Border
, /* yoffset */
4088 -(GLint
) texImages
[i
]->Border
, /* zoffset */
4089 texImages
[i
]->Width
,
4090 texImages
[i
]->Height
,
4091 texImages
[i
]->Depth
,
4092 data
? clearValue
[i
] : NULL
);
4096 _mesa_unlock_texture(ctx
, texObj
);
4102 /**********************************************************************/
4103 /****** Compressed Textures ******/
4104 /**********************************************************************/
4108 * Error checking for glCompressedTexSubImage[123]D().
4109 * \return error code or GL_NO_ERROR.
4112 compressed_subtexture_error_check(struct gl_context
*ctx
, GLint dims
,
4113 GLenum target
, GLint level
,
4114 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4115 GLsizei width
, GLsizei height
, GLsizei depth
,
4116 GLenum format
, GLsizei imageSize
)
4118 struct gl_texture_object
*texObj
;
4119 struct gl_texture_image
*texImage
;
4127 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
4128 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
4129 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
4130 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
4131 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
4132 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
4136 targetOK
= GL_FALSE
;
4141 targetOK
= (target
== GL_TEXTURE_2D_ARRAY
);
4145 /* no 1D compressed textures at this time */
4146 targetOK
= GL_FALSE
;
4151 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexSubImage%uD(target)",
4156 /* this will catch any invalid compressed format token */
4157 if (!_mesa_is_compressed_format(ctx
, format
)) {
4158 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCompressedTexImage%uD(format)",
4163 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
4164 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexSubImage%uD(level=%d)",
4169 /* Check for invalid pixel storage modes */
4170 if (!_mesa_compressed_texture_pixel_storage_error_check(ctx
, dims
,
4172 "glCompressedTexSubImage")) {
4177 expectedSize
= compressed_tex_size(width
, height
, depth
, format
);
4178 if (expectedSize
!= imageSize
) {
4179 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCompressedTexSubImage%uD(size=%d)",
4184 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4186 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
4187 "glCompressedTexSubImage%uD()", dims
);
4191 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
4193 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4194 "glCompressedTexSubImage%uD(invalid texture image)", dims
);
4198 if ((GLint
) format
!= texImage
->InternalFormat
) {
4199 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4200 "glCompressedTexSubImage%uD(format=0x%x)", dims
, format
);
4204 if (compressedteximage_only_format(ctx
, format
)) {
4205 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4206 "glCompressedTexSubImage%uD(format=0x%x cannot be updated)"
4211 if (error_check_subtexture_dimensions(ctx
, "glCompressedTexSubImage", dims
,
4212 texImage
, xoffset
, yoffset
, zoffset
,
4213 width
, height
, depth
)) {
4222 _mesa_CompressedTexImage1D(GLenum target
, GLint level
,
4223 GLenum internalFormat
, GLsizei width
,
4224 GLint border
, GLsizei imageSize
,
4227 GET_CURRENT_CONTEXT(ctx
);
4228 teximage(ctx
, GL_TRUE
, 1, target
, level
, internalFormat
,
4229 width
, 1, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4234 _mesa_CompressedTexImage2D(GLenum target
, GLint level
,
4235 GLenum internalFormat
, GLsizei width
,
4236 GLsizei height
, GLint border
, GLsizei imageSize
,
4239 GET_CURRENT_CONTEXT(ctx
);
4240 teximage(ctx
, GL_TRUE
, 2, target
, level
, internalFormat
,
4241 width
, height
, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4246 _mesa_CompressedTexImage3D(GLenum target
, GLint level
,
4247 GLenum internalFormat
, GLsizei width
,
4248 GLsizei height
, GLsizei depth
, GLint border
,
4249 GLsizei imageSize
, const GLvoid
*data
)
4251 GET_CURRENT_CONTEXT(ctx
);
4252 teximage(ctx
, GL_TRUE
, 3, target
, level
, internalFormat
,
4253 width
, height
, depth
, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4258 * Common helper for glCompressedTexSubImage1/2/3D().
4261 compressed_tex_sub_image(GLuint dims
, GLenum target
, GLint level
,
4262 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4263 GLsizei width
, GLsizei height
, GLsizei depth
,
4264 GLenum format
, GLsizei imageSize
, const GLvoid
*data
)
4266 struct gl_texture_object
*texObj
;
4267 struct gl_texture_image
*texImage
;
4268 GET_CURRENT_CONTEXT(ctx
);
4269 FLUSH_VERTICES(ctx
, 0);
4271 if (compressed_subtexture_error_check(ctx
, dims
, target
, level
,
4272 xoffset
, yoffset
, zoffset
,
4273 width
, height
, depth
,
4274 format
, imageSize
)) {
4278 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4280 _mesa_lock_texture(ctx
, texObj
);
4282 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
4285 if (width
> 0 && height
> 0 && depth
> 0) {
4286 ctx
->Driver
.CompressedTexSubImage(ctx
, dims
, texImage
,
4287 xoffset
, yoffset
, zoffset
,
4288 width
, height
, depth
,
4289 format
, imageSize
, data
);
4291 check_gen_mipmap(ctx
, target
, texObj
, level
);
4293 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
4294 * the texel data, not the texture format, size, etc.
4298 _mesa_unlock_texture(ctx
, texObj
);
4303 _mesa_CompressedTexSubImage1D(GLenum target
, GLint level
, GLint xoffset
,
4304 GLsizei width
, GLenum format
,
4305 GLsizei imageSize
, const GLvoid
*data
)
4307 compressed_tex_sub_image(1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
4308 format
, imageSize
, data
);
4313 _mesa_CompressedTexSubImage2D(GLenum target
, GLint level
, GLint xoffset
,
4314 GLint yoffset
, GLsizei width
, GLsizei height
,
4315 GLenum format
, GLsizei imageSize
,
4318 compressed_tex_sub_image(2, target
, level
, xoffset
, yoffset
, 0,
4319 width
, height
, 1, format
, imageSize
, data
);
4324 _mesa_CompressedTexSubImage3D(GLenum target
, GLint level
, GLint xoffset
,
4325 GLint yoffset
, GLint zoffset
, GLsizei width
,
4326 GLsizei height
, GLsizei depth
, GLenum format
,
4327 GLsizei imageSize
, const GLvoid
*data
)
4329 compressed_tex_sub_image(3, target
, level
, xoffset
, yoffset
, zoffset
,
4330 width
, height
, depth
, format
, imageSize
, data
);
4334 get_texbuffer_format(const struct gl_context
*ctx
, GLenum internalFormat
)
4336 if (ctx
->API
!= API_OPENGL_CORE
) {
4337 switch (internalFormat
) {
4339 return MESA_FORMAT_A_UNORM8
;
4341 return MESA_FORMAT_A_UNORM16
;
4342 case GL_ALPHA16F_ARB
:
4343 return MESA_FORMAT_A_FLOAT16
;
4344 case GL_ALPHA32F_ARB
:
4345 return MESA_FORMAT_A_FLOAT32
;
4346 case GL_ALPHA8I_EXT
:
4347 return MESA_FORMAT_A_SINT8
;
4348 case GL_ALPHA16I_EXT
:
4349 return MESA_FORMAT_A_SINT16
;
4350 case GL_ALPHA32I_EXT
:
4351 return MESA_FORMAT_A_SINT32
;
4352 case GL_ALPHA8UI_EXT
:
4353 return MESA_FORMAT_A_UINT8
;
4354 case GL_ALPHA16UI_EXT
:
4355 return MESA_FORMAT_A_UINT16
;
4356 case GL_ALPHA32UI_EXT
:
4357 return MESA_FORMAT_A_UINT32
;
4359 return MESA_FORMAT_L_UNORM8
;
4360 case GL_LUMINANCE16
:
4361 return MESA_FORMAT_L_UNORM16
;
4362 case GL_LUMINANCE16F_ARB
:
4363 return MESA_FORMAT_L_FLOAT16
;
4364 case GL_LUMINANCE32F_ARB
:
4365 return MESA_FORMAT_L_FLOAT32
;
4366 case GL_LUMINANCE8I_EXT
:
4367 return MESA_FORMAT_L_SINT8
;
4368 case GL_LUMINANCE16I_EXT
:
4369 return MESA_FORMAT_L_SINT16
;
4370 case GL_LUMINANCE32I_EXT
:
4371 return MESA_FORMAT_L_SINT32
;
4372 case GL_LUMINANCE8UI_EXT
:
4373 return MESA_FORMAT_L_UINT8
;
4374 case GL_LUMINANCE16UI_EXT
:
4375 return MESA_FORMAT_L_UINT16
;
4376 case GL_LUMINANCE32UI_EXT
:
4377 return MESA_FORMAT_L_UINT32
;
4378 case GL_LUMINANCE8_ALPHA8
:
4379 return MESA_FORMAT_L8A8_UNORM
;
4380 case GL_LUMINANCE16_ALPHA16
:
4381 return MESA_FORMAT_L16A16_UNORM
;
4382 case GL_LUMINANCE_ALPHA16F_ARB
:
4383 return MESA_FORMAT_LA_FLOAT16
;
4384 case GL_LUMINANCE_ALPHA32F_ARB
:
4385 return MESA_FORMAT_LA_FLOAT32
;
4386 case GL_LUMINANCE_ALPHA8I_EXT
:
4387 return MESA_FORMAT_LA_SINT8
;
4388 case GL_LUMINANCE_ALPHA16I_EXT
:
4389 return MESA_FORMAT_LA_SINT8
;
4390 case GL_LUMINANCE_ALPHA32I_EXT
:
4391 return MESA_FORMAT_LA_SINT16
;
4392 case GL_LUMINANCE_ALPHA8UI_EXT
:
4393 return MESA_FORMAT_LA_UINT8
;
4394 case GL_LUMINANCE_ALPHA16UI_EXT
:
4395 return MESA_FORMAT_LA_UINT16
;
4396 case GL_LUMINANCE_ALPHA32UI_EXT
:
4397 return MESA_FORMAT_LA_UINT32
;
4399 return MESA_FORMAT_I_UNORM8
;
4400 case GL_INTENSITY16
:
4401 return MESA_FORMAT_I_UNORM16
;
4402 case GL_INTENSITY16F_ARB
:
4403 return MESA_FORMAT_I_FLOAT16
;
4404 case GL_INTENSITY32F_ARB
:
4405 return MESA_FORMAT_I_FLOAT32
;
4406 case GL_INTENSITY8I_EXT
:
4407 return MESA_FORMAT_I_SINT8
;
4408 case GL_INTENSITY16I_EXT
:
4409 return MESA_FORMAT_I_SINT16
;
4410 case GL_INTENSITY32I_EXT
:
4411 return MESA_FORMAT_I_SINT32
;
4412 case GL_INTENSITY8UI_EXT
:
4413 return MESA_FORMAT_I_UINT8
;
4414 case GL_INTENSITY16UI_EXT
:
4415 return MESA_FORMAT_I_UINT16
;
4416 case GL_INTENSITY32UI_EXT
:
4417 return MESA_FORMAT_I_UINT32
;
4423 if (ctx
->API
== API_OPENGL_CORE
&&
4424 ctx
->Extensions
.ARB_texture_buffer_object_rgb32
) {
4425 switch (internalFormat
) {
4427 return MESA_FORMAT_RGB_FLOAT32
;
4429 return MESA_FORMAT_RGB_UINT32
;
4431 return MESA_FORMAT_RGB_SINT32
;
4437 switch (internalFormat
) {
4439 return MESA_FORMAT_R8G8B8A8_UNORM
;
4441 return MESA_FORMAT_RGBA_UNORM16
;
4442 case GL_RGBA16F_ARB
:
4443 return MESA_FORMAT_RGBA_FLOAT16
;
4444 case GL_RGBA32F_ARB
:
4445 return MESA_FORMAT_RGBA_FLOAT32
;
4447 return MESA_FORMAT_RGBA_SINT8
;
4448 case GL_RGBA16I_EXT
:
4449 return MESA_FORMAT_RGBA_SINT16
;
4450 case GL_RGBA32I_EXT
:
4451 return MESA_FORMAT_RGBA_SINT32
;
4452 case GL_RGBA8UI_EXT
:
4453 return MESA_FORMAT_RGBA_UINT8
;
4454 case GL_RGBA16UI_EXT
:
4455 return MESA_FORMAT_RGBA_UINT16
;
4456 case GL_RGBA32UI_EXT
:
4457 return MESA_FORMAT_RGBA_UINT32
;
4460 return MESA_FORMAT_R8G8_UNORM
;
4462 return MESA_FORMAT_R16G16_UNORM
;
4464 return MESA_FORMAT_RG_FLOAT16
;
4466 return MESA_FORMAT_RG_FLOAT32
;
4468 return MESA_FORMAT_RG_SINT8
;
4470 return MESA_FORMAT_RG_SINT16
;
4472 return MESA_FORMAT_RG_SINT32
;
4474 return MESA_FORMAT_RG_UINT8
;
4476 return MESA_FORMAT_RG_UINT16
;
4478 return MESA_FORMAT_RG_UINT32
;
4481 return MESA_FORMAT_R_UNORM8
;
4483 return MESA_FORMAT_R_UNORM16
;
4485 return MESA_FORMAT_R_FLOAT16
;
4487 return MESA_FORMAT_R_FLOAT32
;
4489 return MESA_FORMAT_R_SINT8
;
4491 return MESA_FORMAT_R_SINT16
;
4493 return MESA_FORMAT_R_SINT32
;
4495 return MESA_FORMAT_R_UINT8
;
4497 return MESA_FORMAT_R_UINT16
;
4499 return MESA_FORMAT_R_UINT32
;
4502 return MESA_FORMAT_NONE
;
4508 _mesa_validate_texbuffer_format(const struct gl_context
*ctx
,
4509 GLenum internalFormat
)
4511 mesa_format format
= get_texbuffer_format(ctx
, internalFormat
);
4514 if (format
== MESA_FORMAT_NONE
)
4515 return MESA_FORMAT_NONE
;
4517 datatype
= _mesa_get_format_datatype(format
);
4519 /* The GL_ARB_texture_buffer_object spec says:
4521 * "If ARB_texture_float is not supported, references to the
4522 * floating-point internal formats provided by that extension should be
4523 * removed, and such formats may not be passed to TexBufferARB."
4525 * As a result, GL_HALF_FLOAT internal format depends on both
4526 * GL_ARB_texture_float and GL_ARB_half_float_pixel.
4528 if ((datatype
== GL_FLOAT
|| datatype
== GL_HALF_FLOAT
) &&
4529 !ctx
->Extensions
.ARB_texture_float
)
4530 return MESA_FORMAT_NONE
;
4532 if (!ctx
->Extensions
.ARB_texture_rg
) {
4533 GLenum base_format
= _mesa_get_format_base_format(format
);
4534 if (base_format
== GL_R
|| base_format
== GL_RG
)
4535 return MESA_FORMAT_NONE
;
4538 if (!ctx
->Extensions
.ARB_texture_buffer_object_rgb32
) {
4539 GLenum base_format
= _mesa_get_format_base_format(format
);
4540 if (base_format
== GL_RGB
)
4541 return MESA_FORMAT_NONE
;
4548 texbufferrange(struct gl_context
*ctx
, GLenum target
, GLenum internalFormat
,
4549 struct gl_buffer_object
*bufObj
,
4550 GLintptr offset
, GLsizeiptr size
)
4552 struct gl_texture_object
*texObj
;
4555 FLUSH_VERTICES(ctx
, 0);
4557 if (target
!= GL_TEXTURE_BUFFER_ARB
) {
4558 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexBuffer(target)");
4562 format
= _mesa_validate_texbuffer_format(ctx
, internalFormat
);
4563 if (format
== MESA_FORMAT_NONE
) {
4564 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexBuffer(internalFormat 0x%x)",
4569 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4571 _mesa_lock_texture(ctx
, texObj
);
4573 _mesa_reference_buffer_object(ctx
, &texObj
->BufferObject
, bufObj
);
4574 texObj
->BufferObjectFormat
= internalFormat
;
4575 texObj
->_BufferObjectFormat
= format
;
4576 texObj
->BufferOffset
= offset
;
4577 texObj
->BufferSize
= size
;
4579 _mesa_unlock_texture(ctx
, texObj
);
4583 /** GL_ARB_texture_buffer_object */
4585 _mesa_TexBuffer(GLenum target
, GLenum internalFormat
, GLuint buffer
)
4587 struct gl_buffer_object
*bufObj
;
4589 GET_CURRENT_CONTEXT(ctx
);
4591 /* NOTE: ARB_texture_buffer_object has interactions with
4592 * the compatibility profile that are not implemented.
4594 if (!(ctx
->API
== API_OPENGL_CORE
&&
4595 ctx
->Extensions
.ARB_texture_buffer_object
)) {
4596 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBuffer");
4600 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4601 if (!bufObj
&& buffer
) {
4602 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBuffer(buffer %u)", buffer
);
4606 texbufferrange(ctx
, target
, internalFormat
, bufObj
, 0, buffer
? -1 : 0);
4610 /** GL_ARB_texture_buffer_range */
4612 _mesa_TexBufferRange(GLenum target
, GLenum internalFormat
, GLuint buffer
,
4613 GLintptr offset
, GLsizeiptr size
)
4615 struct gl_buffer_object
*bufObj
;
4617 GET_CURRENT_CONTEXT(ctx
);
4619 if (!(ctx
->API
== API_OPENGL_CORE
&&
4620 ctx
->Extensions
.ARB_texture_buffer_range
)) {
4621 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBufferRange");
4625 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4629 (offset
+ size
) > bufObj
->Size
) {
4630 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexBufferRange");
4633 if (offset
% ctx
->Const
.TextureBufferOffsetAlignment
) {
4634 _mesa_error(ctx
, GL_INVALID_VALUE
,
4635 "glTexBufferRange(invalid offset alignment)");
4638 } else if (buffer
) {
4639 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexBufferRange(buffer %u)",
4647 texbufferrange(ctx
, target
, internalFormat
, bufObj
, offset
, size
);
4652 is_renderable_texture_format(struct gl_context
*ctx
, GLenum internalformat
)
4654 /* Everything that is allowed for renderbuffers,
4655 * except for a base format of GL_STENCIL_INDEX.
4657 GLenum baseFormat
= _mesa_base_fbo_format(ctx
, internalformat
);
4658 return baseFormat
!= 0 && baseFormat
!= GL_STENCIL_INDEX
;
4662 /** GL_ARB_texture_multisample */
4664 check_multisample_target(GLuint dims
, GLenum target
)
4667 case GL_TEXTURE_2D_MULTISAMPLE
:
4668 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
4671 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
4672 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
4682 teximagemultisample(GLuint dims
, GLenum target
, GLsizei samples
,
4683 GLint internalformat
, GLsizei width
, GLsizei height
,
4684 GLsizei depth
, GLboolean fixedsamplelocations
,
4685 GLboolean immutable
, const char *func
)
4687 struct gl_texture_object
*texObj
;
4688 struct gl_texture_image
*texImage
;
4689 GLboolean sizeOK
, dimensionsOK
, samplesOK
;
4690 mesa_format texFormat
;
4691 GLenum sample_count_error
;
4693 GET_CURRENT_CONTEXT(ctx
);
4695 if (!(ctx
->Extensions
.ARB_texture_multisample
4696 && _mesa_is_desktop_gl(ctx
))) {
4697 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unsupported)", func
);
4701 if (!check_multisample_target(dims
, target
)) {
4702 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
4706 /* check that the specified internalformat is color/depth/stencil-renderable;
4707 * refer GL3.1 spec 4.4.4
4710 if (immutable
&& !_mesa_is_legal_tex_storage_format(ctx
, internalformat
)) {
4711 _mesa_error(ctx
, GL_INVALID_ENUM
,
4712 "%s(internalformat=%s not legal for immutable-format)",
4713 func
, _mesa_lookup_enum_by_nr(internalformat
));
4717 if (!is_renderable_texture_format(ctx
, internalformat
)) {
4718 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4719 "%s(internalformat=%s)",
4720 func
, _mesa_lookup_enum_by_nr(internalformat
));
4724 sample_count_error
= _mesa_check_sample_count(ctx
, target
,
4725 internalformat
, samples
);
4726 samplesOK
= sample_count_error
== GL_NO_ERROR
;
4728 /* Page 254 of OpenGL 4.4 spec says:
4729 * "Proxy arrays for two-dimensional multisample and two-dimensional
4730 * multisample array textures are operated on in the same way when
4731 * TexImage2DMultisample is called with target specified as
4732 * PROXY_TEXTURE_2D_MULTISAMPLE, or TexImage3DMultisample is called
4733 * with target specified as PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY.
4734 * However, if samples is not supported, then no error is generated.
4736 if (!samplesOK
&& !_mesa_is_proxy_texture(target
)) {
4737 _mesa_error(ctx
, sample_count_error
, "%s(samples)", func
);
4741 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4743 if (immutable
&& (!texObj
|| (texObj
->Name
== 0))) {
4744 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4745 "%s(texture object 0)",
4750 texImage
= _mesa_get_tex_image(ctx
, texObj
, 0, 0);
4752 if (texImage
== NULL
) {
4753 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", func
);
4757 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, 0,
4758 internalformat
, GL_NONE
, GL_NONE
);
4759 assert(texFormat
!= MESA_FORMAT_NONE
);
4761 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, 0,
4762 width
, height
, depth
, 0);
4764 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, target
, 0, texFormat
,
4765 width
, height
, depth
, 0);
4767 if (_mesa_is_proxy_texture(target
)) {
4768 if (samplesOK
&& dimensionsOK
&& sizeOK
) {
4769 init_teximage_fields_ms(ctx
, texImage
, width
, height
, depth
, 0,
4770 internalformat
, texFormat
,
4771 samples
, fixedsamplelocations
);
4774 /* clear all image fields */
4775 clear_teximage_fields(texImage
);
4779 if (!dimensionsOK
) {
4780 _mesa_error(ctx
, GL_INVALID_VALUE
,
4781 "%s(invalid width or height)", func
);
4786 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
4787 "%s(texture too large)", func
);
4791 /* Check if texObj->Immutable is set */
4792 if (texObj
->Immutable
) {
4793 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
4797 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
4799 init_teximage_fields_ms(ctx
, texImage
, width
, height
, depth
, 0,
4800 internalformat
, texFormat
,
4801 samples
, fixedsamplelocations
);
4803 if (width
> 0 && height
> 0 && depth
> 0) {
4804 if (!ctx
->Driver
.AllocTextureStorage(ctx
, texObj
, 1,
4805 width
, height
, depth
)) {
4806 /* tidy up the texture image state. strictly speaking,
4807 * we're allowed to just leave this in whatever state we
4808 * like, but being tidy is good.
4810 _mesa_init_teximage_fields(ctx
, texImage
,
4811 0, 0, 0, 0, GL_NONE
, MESA_FORMAT_NONE
);
4815 texObj
->Immutable
= immutable
;
4818 _mesa_set_texture_view_state(ctx
, texObj
, target
, 1);
4821 _mesa_update_fbo_texture(ctx
, texObj
, 0, 0);
4827 _mesa_TexImage2DMultisample(GLenum target
, GLsizei samples
,
4828 GLenum internalformat
, GLsizei width
,
4829 GLsizei height
, GLboolean fixedsamplelocations
)
4831 teximagemultisample(2, target
, samples
, internalformat
,
4832 width
, height
, 1, fixedsamplelocations
, GL_FALSE
,
4833 "glTexImage2DMultisample");
4838 _mesa_TexImage3DMultisample(GLenum target
, GLsizei samples
,
4839 GLenum internalformat
, GLsizei width
,
4840 GLsizei height
, GLsizei depth
,
4841 GLboolean fixedsamplelocations
)
4843 teximagemultisample(3, target
, samples
, internalformat
,
4844 width
, height
, depth
, fixedsamplelocations
, GL_FALSE
,
4845 "glTexImage3DMultisample");
4850 _mesa_TexStorage2DMultisample(GLenum target
, GLsizei samples
,
4851 GLenum internalformat
, GLsizei width
,
4852 GLsizei height
, GLboolean fixedsamplelocations
)
4854 teximagemultisample(2, target
, samples
, internalformat
,
4855 width
, height
, 1, fixedsamplelocations
, GL_TRUE
,
4856 "glTexStorage2DMultisample");
4860 _mesa_TexStorage3DMultisample(GLenum target
, GLsizei samples
,
4861 GLenum internalformat
, GLsizei width
,
4862 GLsizei height
, GLsizei depth
,
4863 GLboolean fixedsamplelocations
)
4865 teximagemultisample(3, target
, samples
, internalformat
,
4866 width
, height
, depth
, fixedsamplelocations
, GL_TRUE
,
4867 "glTexStorage3DMultisample");