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"
44 #include "pixelstore.h"
46 #include "texcompress.h"
47 #include "texcompress_cpal.h"
51 #include "texstorage.h"
52 #include "textureview.h"
54 #include "glformats.h"
60 * State changes which we care about for glCopyTex[Sub]Image() calls.
61 * In particular, we care about pixel transfer state and buffer state
62 * (such as glReadBuffer to make sure we read from the right renderbuffer).
64 #define NEW_COPY_TEX_STATE (_NEW_BUFFERS | _NEW_PIXEL)
67 * Returns a corresponding internal floating point format for a given base
68 * format as specifed by OES_texture_float. In case of GL_FLOAT, the internal
69 * format needs to be a 32 bit component and in case of GL_HALF_FLOAT_OES it
70 * needs to be a 16 bit component.
72 * For example, given base format GL_RGBA, type GL_Float return GL_RGBA32F_ARB.
75 adjust_for_oes_float_texture(GLenum format
, GLenum type
)
85 return GL_ALPHA32F_ARB
;
87 return GL_LUMINANCE32F_ARB
;
88 case GL_LUMINANCE_ALPHA
:
89 return GL_LUMINANCE_ALPHA32F_ARB
;
95 case GL_HALF_FLOAT_OES
:
102 return GL_ALPHA16F_ARB
;
104 return GL_LUMINANCE16F_ARB
;
105 case GL_LUMINANCE_ALPHA
:
106 return GL_LUMINANCE_ALPHA16F_ARB
;
120 * For cube map faces, return a face index in [0,5].
121 * For other targets return 0;
124 _mesa_tex_target_to_face(GLenum target
)
126 if (_mesa_is_cube_face(target
))
127 return (GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
;
135 * Install gl_texture_image in a gl_texture_object according to the target
136 * and level parameters.
138 * \param tObj texture object.
139 * \param target texture target.
140 * \param level image level.
141 * \param texImage texture image.
144 set_tex_image(struct gl_texture_object
*tObj
,
145 GLenum target
, GLint level
,
146 struct gl_texture_image
*texImage
)
148 const GLuint face
= _mesa_tex_target_to_face(target
);
152 if (target
== GL_TEXTURE_RECTANGLE_NV
|| target
== GL_TEXTURE_EXTERNAL_OES
)
155 tObj
->Image
[face
][level
] = texImage
;
157 /* Set the 'back' pointer */
158 texImage
->TexObject
= tObj
;
159 texImage
->Level
= level
;
160 texImage
->Face
= face
;
165 * Allocate a texture image structure.
167 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
170 * \return a pointer to gl_texture_image struct with all fields initialized to
173 struct gl_texture_image
*
174 _mesa_new_texture_image( struct gl_context
*ctx
)
177 return CALLOC_STRUCT(gl_texture_image
);
182 * Free a gl_texture_image and associated data.
183 * This function is a fallback called via ctx->Driver.DeleteTextureImage().
185 * \param texImage texture image.
187 * Free the texture image structure and the associated image data.
190 _mesa_delete_texture_image(struct gl_context
*ctx
,
191 struct gl_texture_image
*texImage
)
193 /* Free texImage->Data and/or any other driver-specific texture
196 assert(ctx
->Driver
.FreeTextureImageBuffer
);
197 ctx
->Driver
.FreeTextureImageBuffer( ctx
, texImage
);
203 * Test if a target is a proxy target.
205 * \param target texture target.
207 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
210 _mesa_is_proxy_texture(GLenum target
)
213 static const GLenum targets
[] = {
217 GL_PROXY_TEXTURE_CUBE_MAP
,
218 GL_PROXY_TEXTURE_RECTANGLE
,
219 GL_PROXY_TEXTURE_1D_ARRAY
,
220 GL_PROXY_TEXTURE_2D_ARRAY
,
221 GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
,
222 GL_PROXY_TEXTURE_2D_MULTISAMPLE
,
223 GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
226 * NUM_TEXTURE_TARGETS should match number of terms above, except there's no
227 * proxy for GL_TEXTURE_BUFFER and GL_TEXTURE_EXTERNAL_OES.
229 STATIC_ASSERT(NUM_TEXTURE_TARGETS
== ARRAY_SIZE(targets
) + 2);
231 for (i
= 0; i
< ARRAY_SIZE(targets
); ++i
)
232 if (target
== targets
[i
])
239 * Test if a target is an array target.
241 * \param target texture target.
243 * \return true if the target is an array target, false otherwise.
246 _mesa_is_array_texture(GLenum target
)
249 case GL_TEXTURE_1D_ARRAY
:
250 case GL_TEXTURE_2D_ARRAY
:
251 case GL_TEXTURE_CUBE_MAP_ARRAY
:
252 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
261 * Return the proxy target which corresponds to the given texture target
264 proxy_target(GLenum target
)
268 case GL_PROXY_TEXTURE_1D
:
269 return GL_PROXY_TEXTURE_1D
;
271 case GL_PROXY_TEXTURE_2D
:
272 return GL_PROXY_TEXTURE_2D
;
274 case GL_PROXY_TEXTURE_3D
:
275 return GL_PROXY_TEXTURE_3D
;
276 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
277 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
278 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
279 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
280 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
281 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
282 case GL_TEXTURE_CUBE_MAP_ARB
:
283 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
284 return GL_PROXY_TEXTURE_CUBE_MAP_ARB
;
285 case GL_TEXTURE_RECTANGLE_NV
:
286 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
287 return GL_PROXY_TEXTURE_RECTANGLE_NV
;
288 case GL_TEXTURE_1D_ARRAY_EXT
:
289 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
290 return GL_PROXY_TEXTURE_1D_ARRAY_EXT
;
291 case GL_TEXTURE_2D_ARRAY_EXT
:
292 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
293 return GL_PROXY_TEXTURE_2D_ARRAY_EXT
;
294 case GL_TEXTURE_CUBE_MAP_ARRAY
:
295 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
296 return GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
;
297 case GL_TEXTURE_2D_MULTISAMPLE
:
298 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
299 return GL_PROXY_TEXTURE_2D_MULTISAMPLE
;
300 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
301 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
302 return GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
;
304 _mesa_problem(NULL
, "unexpected target in proxy_target()");
313 * Get a texture image pointer from a texture object, given a texture
314 * target and mipmap level. The target and level parameters should
315 * have already been error-checked.
317 * \param texObj texture unit.
318 * \param target texture target.
319 * \param level image level.
321 * \return pointer to the texture image structure, or NULL on failure.
323 struct gl_texture_image
*
324 _mesa_select_tex_image(const struct gl_texture_object
*texObj
,
325 GLenum target
, GLint level
)
327 const GLuint face
= _mesa_tex_target_to_face(target
);
331 assert(level
< MAX_TEXTURE_LEVELS
);
333 return texObj
->Image
[face
][level
];
338 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
339 * it and install it. Only return NULL if passed a bad parameter or run
342 struct gl_texture_image
*
343 _mesa_get_tex_image(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
344 GLenum target
, GLint level
)
346 struct gl_texture_image
*texImage
;
351 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
353 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
355 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture image allocation");
359 set_tex_image(texObj
, target
, level
, texImage
);
367 * Return pointer to the specified proxy texture image.
368 * Note that proxy textures are per-context, not per-texture unit.
369 * \return pointer to texture image or NULL if invalid target, invalid
370 * level, or out of memory.
372 static struct gl_texture_image
*
373 get_proxy_tex_image(struct gl_context
*ctx
, GLenum target
, GLint level
)
375 struct gl_texture_image
*texImage
;
382 case GL_PROXY_TEXTURE_1D
:
383 if (level
>= ctx
->Const
.MaxTextureLevels
)
385 texIndex
= TEXTURE_1D_INDEX
;
387 case GL_PROXY_TEXTURE_2D
:
388 if (level
>= ctx
->Const
.MaxTextureLevels
)
390 texIndex
= TEXTURE_2D_INDEX
;
392 case GL_PROXY_TEXTURE_3D
:
393 if (level
>= ctx
->Const
.Max3DTextureLevels
)
395 texIndex
= TEXTURE_3D_INDEX
;
397 case GL_PROXY_TEXTURE_CUBE_MAP
:
398 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
400 texIndex
= TEXTURE_CUBE_INDEX
;
402 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
405 texIndex
= TEXTURE_RECT_INDEX
;
407 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
408 if (level
>= ctx
->Const
.MaxTextureLevels
)
410 texIndex
= TEXTURE_1D_ARRAY_INDEX
;
412 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
413 if (level
>= ctx
->Const
.MaxTextureLevels
)
415 texIndex
= TEXTURE_2D_ARRAY_INDEX
;
417 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
418 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
420 texIndex
= TEXTURE_CUBE_ARRAY_INDEX
;
422 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
425 texIndex
= TEXTURE_2D_MULTISAMPLE_INDEX
;
427 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
430 texIndex
= TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
;
436 texImage
= ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
];
438 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
440 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
443 ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
] = texImage
;
444 /* Set the 'back' pointer */
445 texImage
->TexObject
= ctx
->Texture
.ProxyTex
[texIndex
];
452 * Get the maximum number of allowed mipmap levels.
454 * \param ctx GL context.
455 * \param target texture target.
457 * \return the maximum number of allowed mipmap levels for the given
458 * texture target, or zero if passed a bad target.
463 _mesa_max_texture_levels(struct gl_context
*ctx
, GLenum target
)
467 case GL_PROXY_TEXTURE_1D
:
469 case GL_PROXY_TEXTURE_2D
:
470 return ctx
->Const
.MaxTextureLevels
;
472 case GL_PROXY_TEXTURE_3D
:
473 return ctx
->Const
.Max3DTextureLevels
;
474 case GL_TEXTURE_CUBE_MAP
:
475 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
476 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
477 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
478 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
479 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
480 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
481 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
482 return ctx
->Extensions
.ARB_texture_cube_map
483 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
484 case GL_TEXTURE_RECTANGLE_NV
:
485 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
486 return ctx
->Extensions
.NV_texture_rectangle
? 1 : 0;
487 case GL_TEXTURE_1D_ARRAY_EXT
:
488 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
489 case GL_TEXTURE_2D_ARRAY_EXT
:
490 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
491 return ctx
->Extensions
.EXT_texture_array
492 ? ctx
->Const
.MaxTextureLevels
: 0;
493 case GL_TEXTURE_CUBE_MAP_ARRAY
:
494 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
495 return ctx
->Extensions
.ARB_texture_cube_map_array
496 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
497 case GL_TEXTURE_BUFFER
:
498 return ctx
->API
== API_OPENGL_CORE
&&
499 ctx
->Extensions
.ARB_texture_buffer_object
? 1 : 0;
500 case GL_TEXTURE_2D_MULTISAMPLE
:
501 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
502 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
503 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
504 return (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles31(ctx
))
505 && ctx
->Extensions
.ARB_texture_multisample
507 case GL_TEXTURE_EXTERNAL_OES
:
510 return 0; /* bad target */
516 * Return number of dimensions per mipmap level for the given texture target.
519 _mesa_get_texture_dimensions(GLenum target
)
523 case GL_PROXY_TEXTURE_1D
:
526 case GL_TEXTURE_RECTANGLE
:
527 case GL_TEXTURE_CUBE_MAP
:
528 case GL_PROXY_TEXTURE_2D
:
529 case GL_PROXY_TEXTURE_RECTANGLE
:
530 case GL_PROXY_TEXTURE_CUBE_MAP
:
531 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
532 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
533 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
534 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
535 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
536 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
537 case GL_TEXTURE_1D_ARRAY
:
538 case GL_PROXY_TEXTURE_1D_ARRAY
:
539 case GL_TEXTURE_EXTERNAL_OES
:
540 case GL_TEXTURE_2D_MULTISAMPLE
:
541 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
544 case GL_PROXY_TEXTURE_3D
:
545 case GL_TEXTURE_2D_ARRAY
:
546 case GL_PROXY_TEXTURE_2D_ARRAY
:
547 case GL_TEXTURE_CUBE_MAP_ARRAY
:
548 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
549 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
550 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
552 case GL_TEXTURE_BUFFER
:
555 _mesa_problem(NULL
, "invalid target 0x%x in get_texture_dimensions()",
563 * Check if a texture target can have more than one layer.
566 _mesa_tex_target_is_layered(GLenum target
)
570 case GL_PROXY_TEXTURE_1D
:
572 case GL_PROXY_TEXTURE_2D
:
573 case GL_TEXTURE_RECTANGLE
:
574 case GL_PROXY_TEXTURE_RECTANGLE
:
575 case GL_TEXTURE_2D_MULTISAMPLE
:
576 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
577 case GL_TEXTURE_BUFFER
:
578 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
579 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
580 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
581 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
582 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
583 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
584 case GL_TEXTURE_EXTERNAL_OES
:
588 case GL_PROXY_TEXTURE_3D
:
589 case GL_TEXTURE_CUBE_MAP
:
590 case GL_PROXY_TEXTURE_CUBE_MAP
:
591 case GL_TEXTURE_1D_ARRAY
:
592 case GL_PROXY_TEXTURE_1D_ARRAY
:
593 case GL_TEXTURE_2D_ARRAY
:
594 case GL_PROXY_TEXTURE_2D_ARRAY
:
595 case GL_TEXTURE_CUBE_MAP_ARRAY
:
596 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
597 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
598 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
602 assert(!"Invalid texture target.");
609 * Return the number of layers present in the given level of an array,
610 * cubemap or 3D texture. If the texture is not layered return zero.
613 _mesa_get_texture_layers(const struct gl_texture_object
*texObj
, GLint level
)
615 assert(level
>= 0 && level
< MAX_TEXTURE_LEVELS
);
617 switch (texObj
->Target
) {
620 case GL_TEXTURE_RECTANGLE
:
621 case GL_TEXTURE_2D_MULTISAMPLE
:
622 case GL_TEXTURE_BUFFER
:
623 case GL_TEXTURE_EXTERNAL_OES
:
626 case GL_TEXTURE_CUBE_MAP
:
629 case GL_TEXTURE_1D_ARRAY
: {
630 struct gl_texture_image
*img
= texObj
->Image
[0][level
];
631 return img
? img
->Height
: 0;
635 case GL_TEXTURE_2D_ARRAY
:
636 case GL_TEXTURE_CUBE_MAP_ARRAY
:
637 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
: {
638 struct gl_texture_image
*img
= texObj
->Image
[0][level
];
639 return img
? img
->Depth
: 0;
643 assert(!"Invalid texture target.");
650 * Return the maximum number of mipmap levels for the given target
651 * and the dimensions.
652 * The dimensions are expected not to include the border.
655 _mesa_get_tex_max_num_levels(GLenum target
, GLsizei width
, GLsizei height
,
662 case GL_TEXTURE_1D_ARRAY
:
663 case GL_PROXY_TEXTURE_1D
:
664 case GL_PROXY_TEXTURE_1D_ARRAY
:
667 case GL_TEXTURE_CUBE_MAP
:
668 case GL_TEXTURE_CUBE_MAP_ARRAY
:
669 case GL_PROXY_TEXTURE_CUBE_MAP
:
670 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
674 case GL_TEXTURE_2D_ARRAY
:
675 case GL_PROXY_TEXTURE_2D
:
676 case GL_PROXY_TEXTURE_2D_ARRAY
:
677 size
= MAX2(width
, height
);
680 case GL_PROXY_TEXTURE_3D
:
681 size
= MAX3(width
, height
, depth
);
683 case GL_TEXTURE_RECTANGLE
:
684 case GL_TEXTURE_EXTERNAL_OES
:
685 case GL_TEXTURE_2D_MULTISAMPLE
:
686 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
687 case GL_PROXY_TEXTURE_RECTANGLE
:
688 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
689 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
696 return _mesa_logbase2(size
) + 1;
700 #if 000 /* not used anymore */
702 * glTexImage[123]D can accept a NULL image pointer. In this case we
703 * create a texture image with unspecified image contents per the OpenGL
707 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
709 const GLint components
= _mesa_components_in_format(format
);
710 const GLint numPixels
= width
* height
* depth
;
711 GLubyte
*data
= (GLubyte
*) malloc(numPixels
* components
* sizeof(GLubyte
));
715 * Let's see if anyone finds this. If glTexImage2D() is called with
716 * a NULL image pointer then load the texture image with something
717 * interesting instead of leaving it indeterminate.
720 static const char message
[8][32] = {
724 " X X XXXX XXX XXXXX ",
727 " X X XXXXX XXX X X ",
731 GLubyte
*imgPtr
= data
;
733 for (h
= 0; h
< depth
; h
++) {
734 for (i
= 0; i
< height
; i
++) {
735 GLint srcRow
= 7 - (i
% 8);
736 for (j
= 0; j
< width
; j
++) {
737 GLint srcCol
= j
% 32;
738 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
739 for (k
= 0; k
< components
; k
++) {
755 * Set the size and format-related fields of a gl_texture_image struct
756 * to zero. This is used when a proxy texture test fails.
759 clear_teximage_fields(struct gl_texture_image
*img
)
762 img
->_BaseFormat
= 0;
763 img
->InternalFormat
= 0;
774 img
->TexFormat
= MESA_FORMAT_NONE
;
776 img
->FixedSampleLocations
= GL_TRUE
;
781 * Initialize basic fields of the gl_texture_image struct.
783 * \param ctx GL context.
784 * \param img texture image structure to be initialized.
785 * \param width image width.
786 * \param height image height.
787 * \param depth image depth.
788 * \param border image border.
789 * \param internalFormat internal format.
790 * \param format the actual hardware format (one of MESA_FORMAT_*)
791 * \param numSamples number of samples per texel, or zero for non-MS.
792 * \param fixedSampleLocations are sample locations fixed?
794 * Fills in the fields of \p img with the given information.
795 * Note: width, height and depth include the border.
798 init_teximage_fields_ms(struct gl_context
*ctx
,
799 struct gl_texture_image
*img
,
800 GLsizei width
, GLsizei height
, GLsizei depth
,
801 GLint border
, GLenum internalFormat
,
803 GLuint numSamples
, GLboolean fixedSampleLocations
)
811 target
= img
->TexObject
->Target
;
812 img
->_BaseFormat
= _mesa_base_tex_format( ctx
, internalFormat
);
813 assert(img
->_BaseFormat
!= -1);
814 img
->InternalFormat
= internalFormat
;
815 img
->Border
= border
;
817 img
->Height
= height
;
820 img
->Width2
= width
- 2 * border
; /* == 1 << img->WidthLog2; */
821 img
->WidthLog2
= _mesa_logbase2(img
->Width2
);
824 img
->FixedSampleLocations
= GL_TRUE
;
828 case GL_TEXTURE_BUFFER
:
829 case GL_PROXY_TEXTURE_1D
:
841 case GL_TEXTURE_1D_ARRAY
:
842 case GL_PROXY_TEXTURE_1D_ARRAY
:
843 img
->Height2
= height
; /* no border */
844 img
->HeightLog2
= 0; /* not used */
852 case GL_TEXTURE_RECTANGLE
:
853 case GL_TEXTURE_CUBE_MAP
:
854 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
855 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
856 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
857 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
858 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
859 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
860 case GL_TEXTURE_EXTERNAL_OES
:
861 case GL_PROXY_TEXTURE_2D
:
862 case GL_PROXY_TEXTURE_RECTANGLE
:
863 case GL_PROXY_TEXTURE_CUBE_MAP
:
864 case GL_TEXTURE_2D_MULTISAMPLE
:
865 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
866 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
867 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
874 case GL_TEXTURE_2D_ARRAY
:
875 case GL_PROXY_TEXTURE_2D_ARRAY
:
876 case GL_TEXTURE_CUBE_MAP_ARRAY
:
877 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
878 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
879 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
880 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
881 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
882 img
->Depth2
= depth
; /* no border */
883 img
->DepthLog2
= 0; /* not used */
886 case GL_PROXY_TEXTURE_3D
:
887 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
888 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
889 img
->Depth2
= depth
- 2 * border
; /* == 1 << img->DepthLog2; */
890 img
->DepthLog2
= _mesa_logbase2(img
->Depth2
);
893 _mesa_problem(NULL
, "invalid target 0x%x in _mesa_init_teximage_fields()",
898 _mesa_get_tex_max_num_levels(target
,
899 img
->Width2
, img
->Height2
, img
->Depth2
);
900 img
->TexFormat
= format
;
901 img
->NumSamples
= numSamples
;
902 img
->FixedSampleLocations
= fixedSampleLocations
;
907 _mesa_init_teximage_fields(struct gl_context
*ctx
,
908 struct gl_texture_image
*img
,
909 GLsizei width
, GLsizei height
, GLsizei depth
,
910 GLint border
, GLenum internalFormat
,
913 init_teximage_fields_ms(ctx
, img
, width
, height
, depth
, border
,
914 internalFormat
, format
, 0, GL_TRUE
);
919 * Free and clear fields of the gl_texture_image struct.
921 * \param ctx GL context.
922 * \param texImage texture image structure to be cleared.
924 * After the call, \p texImage will have no data associated with it. Its
925 * fields are cleared so that its parent object will test incomplete.
928 _mesa_clear_texture_image(struct gl_context
*ctx
,
929 struct gl_texture_image
*texImage
)
931 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
932 clear_teximage_fields(texImage
);
937 * Check the width, height, depth and border of a texture image are legal.
938 * Used by all the glTexImage, glCompressedTexImage and glCopyTexImage
940 * The target and level parameters will have already been validated.
941 * \return GL_TRUE if size is OK, GL_FALSE otherwise.
944 _mesa_legal_texture_dimensions(struct gl_context
*ctx
, GLenum target
,
945 GLint level
, GLint width
, GLint height
,
946 GLint depth
, GLint border
)
952 case GL_PROXY_TEXTURE_1D
:
953 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1); /* level zero size */
954 maxSize
>>= level
; /* level size */
955 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
957 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
958 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
964 case GL_PROXY_TEXTURE_2D
:
965 case GL_TEXTURE_2D_MULTISAMPLE
:
966 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
967 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
969 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
971 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
973 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
974 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
976 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
982 case GL_PROXY_TEXTURE_3D
:
983 maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
985 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
987 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
989 if (depth
< 2 * border
|| depth
> 2 * border
+ maxSize
)
991 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
992 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
994 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
996 if (depth
> 0 && !_mesa_is_pow_two(depth
- 2 * border
))
1001 case GL_TEXTURE_RECTANGLE_NV
:
1002 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1005 maxSize
= ctx
->Const
.MaxTextureRectSize
;
1006 if (width
< 0 || width
> maxSize
)
1008 if (height
< 0 || height
> maxSize
)
1012 case GL_TEXTURE_CUBE_MAP
:
1013 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1014 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1015 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1016 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1017 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1018 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1019 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
1020 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1022 if (width
!= height
)
1024 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1026 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1028 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1029 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1031 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1036 case GL_TEXTURE_1D_ARRAY_EXT
:
1037 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1038 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1040 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1042 if (height
< 0 || height
> ctx
->Const
.MaxArrayTextureLayers
)
1044 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1045 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1050 case GL_TEXTURE_2D_ARRAY_EXT
:
1051 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1052 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1053 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1054 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1056 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1058 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1060 if (depth
< 0 || depth
> ctx
->Const
.MaxArrayTextureLayers
)
1062 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1063 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1065 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1070 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1071 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1072 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1073 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1075 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1077 if (depth
< 0 || depth
> ctx
->Const
.MaxArrayTextureLayers
|| depth
% 6)
1079 if (width
!= height
)
1081 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
1083 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1084 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1086 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1091 _mesa_problem(ctx
, "Invalid target in _mesa_legal_texture_dimensions()");
1098 * Do error checking of xoffset, yoffset, zoffset, width, height and depth
1099 * for glTexSubImage, glCopyTexSubImage and glCompressedTexSubImage.
1100 * \param destImage the destination texture image.
1101 * \return GL_TRUE if error found, GL_FALSE otherwise.
1104 error_check_subtexture_dimensions(struct gl_context
*ctx
, GLuint dims
,
1105 const struct gl_texture_image
*destImage
,
1106 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1107 GLsizei subWidth
, GLsizei subHeight
,
1108 GLsizei subDepth
, const char *func
)
1110 const GLenum target
= destImage
->TexObject
->Target
;
1115 _mesa_error(ctx
, GL_INVALID_VALUE
,
1116 "%s(width=%d)", func
, subWidth
);
1120 if (dims
> 1 && subHeight
< 0) {
1121 _mesa_error(ctx
, GL_INVALID_VALUE
,
1122 "%s(height=%d)", func
, subHeight
);
1126 if (dims
> 2 && subDepth
< 0) {
1127 _mesa_error(ctx
, GL_INVALID_VALUE
,
1128 "%s(depth=%d)", func
, subDepth
);
1132 /* check xoffset and width */
1133 if (xoffset
< - (GLint
) destImage
->Border
) {
1134 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(xoffset)", func
);
1138 if (xoffset
+ subWidth
> (GLint
) destImage
->Width
) {
1139 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(xoffset+width)", func
);
1143 /* check yoffset and height */
1145 GLint yBorder
= (target
== GL_TEXTURE_1D_ARRAY
) ? 0 : destImage
->Border
;
1146 if (yoffset
< -yBorder
) {
1147 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(yoffset)", func
);
1150 if (yoffset
+ subHeight
> (GLint
) destImage
->Height
) {
1151 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(yoffset+height)", func
);
1156 /* check zoffset and depth */
1159 GLint zBorder
= (target
== GL_TEXTURE_2D_ARRAY
||
1160 target
== GL_TEXTURE_CUBE_MAP_ARRAY
) ?
1161 0 : destImage
->Border
;
1163 if (zoffset
< -zBorder
) {
1164 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(zoffset)", func
);
1168 depth
= (GLint
) destImage
->Depth
;
1169 if (target
== GL_TEXTURE_CUBE_MAP
)
1171 if (zoffset
+ subDepth
> depth
) {
1172 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(zoffset+depth)", func
);
1178 * The OpenGL spec (and GL_ARB_texture_compression) says only whole
1179 * compressed texture images can be updated. But, that restriction may be
1180 * relaxed for particular compressed formats. At this time, all the
1181 * compressed formats supported by Mesa allow sub-textures to be updated
1182 * along compressed block boundaries.
1184 _mesa_get_format_block_size(destImage
->TexFormat
, &bw
, &bh
);
1186 if (bw
!= 1 || bh
!= 1) {
1187 /* offset must be multiple of block size */
1188 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0)) {
1189 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1190 "%s(xoffset = %d, yoffset = %d)",
1191 func
, xoffset
, yoffset
);
1195 /* The size must be a multiple of bw x bh, or we must be using a
1196 * offset+size that exactly hits the edge of the image. This
1197 * is important for small mipmap levels (1x1, 2x1, etc) and for
1200 if ((subWidth
% bw
!= 0) &&
1201 (xoffset
+ subWidth
!= (GLint
) destImage
->Width
)) {
1202 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1203 "%s(width = %d)", func
, subWidth
);
1207 if ((subHeight
% bh
!= 0) &&
1208 (yoffset
+ subHeight
!= (GLint
) destImage
->Height
)) {
1209 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1210 "%s(height = %d)", func
, subHeight
);
1222 * This is the fallback for Driver.TestProxyTexImage() for doing device-
1223 * specific texture image size checks.
1225 * A hardware driver might override this function if, for example, the
1226 * max 3D texture size is 512x512x64 (i.e. not a cube).
1228 * Note that width, height, depth == 0 is not an error. However, a
1229 * texture with zero width/height/depth will be considered "incomplete"
1230 * and texturing will effectively be disabled.
1232 * \param target any texture target/type
1233 * \param level as passed to glTexImage
1234 * \param format the MESA_FORMAT_x for the tex image
1235 * \param width as passed to glTexImage
1236 * \param height as passed to glTexImage
1237 * \param depth as passed to glTexImage
1238 * \param border as passed to glTexImage
1239 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1242 _mesa_test_proxy_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1244 GLint width
, GLint height
, GLint depth
, GLint border
)
1246 /* We just check if the image size is less than MaxTextureMbytes.
1247 * Some drivers may do more specific checks.
1249 uint64_t bytes
= _mesa_format_image_size64(format
, width
, height
, depth
);
1250 uint64_t mbytes
= bytes
/ (1024 * 1024); /* convert to MB */
1251 mbytes
*= _mesa_num_tex_faces(target
);
1252 return mbytes
<= (uint64_t) ctx
->Const
.MaxTextureMbytes
;
1257 * Return true if the format is only valid for glCompressedTexImage.
1260 compressedteximage_only_format(const struct gl_context
*ctx
, GLenum format
)
1263 case GL_ETC1_RGB8_OES
:
1264 case GL_PALETTE4_RGB8_OES
:
1265 case GL_PALETTE4_RGBA8_OES
:
1266 case GL_PALETTE4_R5_G6_B5_OES
:
1267 case GL_PALETTE4_RGBA4_OES
:
1268 case GL_PALETTE4_RGB5_A1_OES
:
1269 case GL_PALETTE8_RGB8_OES
:
1270 case GL_PALETTE8_RGBA8_OES
:
1271 case GL_PALETTE8_R5_G6_B5_OES
:
1272 case GL_PALETTE8_RGBA4_OES
:
1273 case GL_PALETTE8_RGB5_A1_OES
:
1281 * Return true if the format doesn't support online compression.
1284 _mesa_format_no_online_compression(const struct gl_context
*ctx
, GLenum format
)
1286 return _mesa_is_astc_format(format
) ||
1287 compressedteximage_only_format(ctx
, format
);
1290 /* Writes to an GL error pointer if non-null and returns whether or not the
1291 * error is GL_NO_ERROR */
1293 write_error(GLenum
*err_ptr
, GLenum error
)
1298 return error
== GL_NO_ERROR
;
1302 * Helper function to determine whether a target and specific compression
1303 * format are supported. The error parameter returns GL_NO_ERROR if the
1304 * target can be compressed. Otherwise it returns either GL_INVALID_OPERATION
1305 * or GL_INVALID_ENUM, whichever is more appropriate.
1308 _mesa_target_can_be_compressed(const struct gl_context
*ctx
, GLenum target
,
1309 GLenum intFormat
, GLenum
*error
)
1311 GLboolean target_can_be_compresed
= GL_FALSE
;
1312 mesa_format format
= _mesa_glenum_to_compressed_format(intFormat
);
1313 enum mesa_format_layout layout
= _mesa_get_format_layout(format
);
1317 case GL_PROXY_TEXTURE_2D
:
1318 target_can_be_compresed
= GL_TRUE
; /* true for any compressed format so far */
1320 case GL_PROXY_TEXTURE_CUBE_MAP
:
1321 case GL_TEXTURE_CUBE_MAP
:
1322 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1323 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1324 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1325 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1326 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1327 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1328 target_can_be_compresed
= ctx
->Extensions
.ARB_texture_cube_map
;
1330 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1331 case GL_TEXTURE_2D_ARRAY_EXT
:
1332 target_can_be_compresed
= ctx
->Extensions
.EXT_texture_array
;
1334 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1335 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1336 /* From the KHR_texture_compression_astc_hdr spec:
1338 * Add a second new column "3D Tex." which is empty for all non-ASTC
1339 * formats. If only the LDR profile is supported by the
1340 * implementation, this column is also empty for all ASTC formats. If
1341 * both the LDR and HDR profiles are supported only, this column is
1342 * checked for all ASTC formats.
1344 * Add a third new column "Cube Map Array Tex." which is empty for all
1345 * non-ASTC formats, and checked for all ASTC formats.
1349 * 'An INVALID_OPERATION error is generated by CompressedTexImage3D
1350 * if <internalformat> is TEXTURE_CUBE_MAP_ARRAY and the
1351 * "Cube Map Array" column of table 8.19 is *not* checked, or if
1352 * <internalformat> is TEXTURE_3D and the "3D Tex." column of table
1353 * 8.19 is *not* checked'
1355 * The instances of <internalformat> above should say <target>.
1357 * ETC2/EAC formats are the only alternative in GLES and thus such errors
1358 * have already been handled by normal ETC2/EAC behavior.
1361 /* From section 3.8.6, page 146 of OpenGL ES 3.0 spec:
1363 * "The ETC2/EAC texture compression algorithm supports only
1364 * two-dimensional images. If internalformat is an ETC2/EAC format,
1365 * glCompressedTexImage3D will generate an INVALID_OPERATION error if
1366 * target is not TEXTURE_2D_ARRAY."
1368 * This should also be applicable for glTexStorage3D(). Other available
1369 * targets for these functions are: TEXTURE_3D and TEXTURE_CUBE_MAP_ARRAY.
1371 if (layout
== MESA_FORMAT_LAYOUT_ETC2
&& _mesa_is_gles3(ctx
))
1372 return write_error(error
, GL_INVALID_OPERATION
);
1373 target_can_be_compresed
= ctx
->Extensions
.ARB_texture_cube_map_array
;
1377 case MESA_FORMAT_LAYOUT_ETC2
:
1378 /* See ETC2/EAC comment in case GL_TEXTURE_CUBE_MAP_ARRAY. */
1379 if (_mesa_is_gles3(ctx
))
1380 return write_error(error
, GL_INVALID_OPERATION
);
1382 case MESA_FORMAT_LAYOUT_BPTC
:
1383 target_can_be_compresed
= ctx
->Extensions
.ARB_texture_compression_bptc
;
1385 case MESA_FORMAT_LAYOUT_ASTC
:
1386 target_can_be_compresed
=
1387 ctx
->Extensions
.KHR_texture_compression_astc_hdr
;
1389 /* Throw an INVALID_OPERATION error if the target is TEXTURE_3D and
1390 * and the hdr extension is not supported.
1391 * See comment in switch case GL_TEXTURE_CUBE_MAP_ARRAY for more info.
1393 if (!target_can_be_compresed
)
1394 return write_error(error
, GL_INVALID_OPERATION
);
1402 return write_error(error
,
1403 target_can_be_compresed
? GL_NO_ERROR
: GL_INVALID_ENUM
);
1408 * Check if the given texture target value is legal for a
1409 * glTexImage1/2/3D call.
1412 legal_teximage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
1418 case GL_PROXY_TEXTURE_1D
:
1419 return _mesa_is_desktop_gl(ctx
);
1427 case GL_PROXY_TEXTURE_2D
:
1428 return _mesa_is_desktop_gl(ctx
);
1429 case GL_PROXY_TEXTURE_CUBE_MAP
:
1430 return _mesa_is_desktop_gl(ctx
)
1431 && ctx
->Extensions
.ARB_texture_cube_map
;
1432 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1433 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1434 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1435 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1436 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1437 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1438 return ctx
->Extensions
.ARB_texture_cube_map
;
1439 case GL_TEXTURE_RECTANGLE_NV
:
1440 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1441 return _mesa_is_desktop_gl(ctx
)
1442 && ctx
->Extensions
.NV_texture_rectangle
;
1443 case GL_TEXTURE_1D_ARRAY_EXT
:
1444 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1445 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1453 case GL_PROXY_TEXTURE_3D
:
1454 return _mesa_is_desktop_gl(ctx
);
1455 case GL_TEXTURE_2D_ARRAY_EXT
:
1456 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1457 || _mesa_is_gles3(ctx
);
1458 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1459 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1460 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1461 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1462 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1467 _mesa_problem(ctx
, "invalid dims=%u in legal_teximage_target()", dims
);
1474 * Check if the given texture target value is legal for a
1475 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1476 * The difference compared to legal_teximage_target() above is that
1477 * proxy targets are not supported.
1480 legal_texsubimage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
,
1485 return _mesa_is_desktop_gl(ctx
) && target
== GL_TEXTURE_1D
;
1490 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1491 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1492 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1493 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1494 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1495 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1496 return ctx
->Extensions
.ARB_texture_cube_map
;
1497 case GL_TEXTURE_RECTANGLE_NV
:
1498 return _mesa_is_desktop_gl(ctx
)
1499 && ctx
->Extensions
.NV_texture_rectangle
;
1500 case GL_TEXTURE_1D_ARRAY_EXT
:
1501 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1509 case GL_TEXTURE_2D_ARRAY_EXT
:
1510 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1511 || _mesa_is_gles3(ctx
);
1512 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1513 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1514 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1516 /* Table 8.15 of the OpenGL 4.5 core profile spec
1517 * (20141030) says that TEXTURE_CUBE_MAP is valid for TextureSubImage3D
1518 * and CopyTextureSubImage3D.
1520 case GL_TEXTURE_CUBE_MAP
:
1526 _mesa_problem(ctx
, "invalid dims=%u in legal_texsubimage_target()",
1534 * Helper function to determine if a texture object is mutable (in terms
1535 * of GL_ARB_texture_storage).
1538 mutable_tex_object(struct gl_context
*ctx
, GLenum target
)
1540 struct gl_texture_object
*texObj
= _mesa_get_current_tex_object(ctx
, target
);
1544 return !texObj
->Immutable
;
1549 * Return expected size of a compressed texture.
1552 compressed_tex_size(GLsizei width
, GLsizei height
, GLsizei depth
,
1555 mesa_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
1556 return _mesa_format_image_size(mesaFormat
, width
, height
, depth
);
1560 * Verify that a texture format is valid with a particular target
1562 * In particular, textures with base format of \c GL_DEPTH_COMPONENT or
1563 * \c GL_DEPTH_STENCIL are only valid with certain, context dependent texture
1566 * \param ctx GL context
1567 * \param target Texture target
1568 * \param internalFormat Internal format of the texture image
1569 * \param dimensions Dimensionality at the caller. This is \b not used
1570 * in the validation. It is only used when logging
1572 * \param caller Base name of the calling function (e.g.,
1573 * "glTexImage" or "glTexStorage").
1575 * \returns true if the combination is legal, false otherwise.
1578 _mesa_legal_texture_base_format_for_target(struct gl_context
*ctx
,
1579 GLenum target
, GLenum internalFormat
,
1580 unsigned dimensions
,
1583 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
1584 || _mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_STENCIL
1585 || _mesa_base_tex_format(ctx
, internalFormat
) == GL_STENCIL_INDEX
) {
1586 /* Section 3.8.3 (Texture Image Specification) of the OpenGL 3.3 Core
1587 * Profile spec says:
1589 * "Textures with a base internal format of DEPTH_COMPONENT or
1590 * DEPTH_STENCIL are supported by texture image specification
1591 * commands only if target is TEXTURE_1D, TEXTURE_2D,
1592 * TEXTURE_1D_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_RECTANGLE,
1593 * TEXTURE_CUBE_MAP, PROXY_TEXTURE_1D, PROXY_TEXTURE_2D,
1594 * PROXY_TEXTURE_1D_ARRAY, PROXY_TEXTURE_2D_ARRAY,
1595 * PROXY_TEXTURE_RECTANGLE, or PROXY_TEXTURE_CUBE_MAP. Using these
1596 * formats in conjunction with any other target will result in an
1597 * INVALID_OPERATION error."
1599 * Cubemaps are only supported with desktop OpenGL version >= 3.0,
1600 * EXT_gpu_shader4, or, on OpenGL ES 2.0+, OES_depth_texture_cube_map.
1602 if (target
!= GL_TEXTURE_1D
&&
1603 target
!= GL_PROXY_TEXTURE_1D
&&
1604 target
!= GL_TEXTURE_2D
&&
1605 target
!= GL_PROXY_TEXTURE_2D
&&
1606 target
!= GL_TEXTURE_1D_ARRAY
&&
1607 target
!= GL_PROXY_TEXTURE_1D_ARRAY
&&
1608 target
!= GL_TEXTURE_2D_ARRAY
&&
1609 target
!= GL_PROXY_TEXTURE_2D_ARRAY
&&
1610 target
!= GL_TEXTURE_RECTANGLE_ARB
&&
1611 target
!= GL_PROXY_TEXTURE_RECTANGLE_ARB
&&
1612 !((_mesa_is_cube_face(target
) ||
1613 target
== GL_TEXTURE_CUBE_MAP
||
1614 target
== GL_PROXY_TEXTURE_CUBE_MAP
) &&
1615 (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_gpu_shader4
1616 || (ctx
->API
== API_OPENGLES2
&& ctx
->Extensions
.OES_depth_texture_cube_map
))) &&
1617 !((target
== GL_TEXTURE_CUBE_MAP_ARRAY
||
1618 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
) &&
1619 ctx
->Extensions
.ARB_texture_cube_map_array
)) {
1620 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1621 "%s%dD(bad target for depth texture)",
1622 caller
, dimensions
);
1631 texture_formats_agree(GLenum internalFormat
,
1634 GLboolean colorFormat
;
1635 GLboolean is_format_depth_or_depthstencil
;
1636 GLboolean is_internalFormat_depth_or_depthstencil
;
1638 /* Even though there are no color-index textures, we still have to support
1639 * uploading color-index data and remapping it to RGB via the
1640 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
1642 const GLboolean indexFormat
= (format
== GL_COLOR_INDEX
);
1644 is_internalFormat_depth_or_depthstencil
=
1645 _mesa_is_depth_format(internalFormat
) ||
1646 _mesa_is_depthstencil_format(internalFormat
);
1648 is_format_depth_or_depthstencil
=
1649 _mesa_is_depth_format(format
) ||
1650 _mesa_is_depthstencil_format(format
);
1652 colorFormat
= _mesa_is_color_format(format
);
1654 if (_mesa_is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
)
1657 if (is_internalFormat_depth_or_depthstencil
!=
1658 is_format_depth_or_depthstencil
)
1661 if (_mesa_is_ycbcr_format(internalFormat
) != _mesa_is_ycbcr_format(format
))
1668 * Test the combination of format, type and internal format arguments of
1669 * different texture operations on GLES.
1671 * \param ctx GL context.
1672 * \param format pixel data format given by the user.
1673 * \param type pixel data type given by the user.
1674 * \param internalFormat internal format given by the user.
1675 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1676 * \param callerName name of the caller function to print in the error message
1678 * \return true if a error is found, false otherwise
1680 * Currently, it is used by texture_error_check() and texsubimage_error_check().
1683 texture_format_error_check_gles(struct gl_context
*ctx
, GLenum format
,
1684 GLenum type
, GLenum internalFormat
,
1685 GLuint dimensions
, const char *callerName
)
1689 if (_mesa_is_gles3(ctx
)) {
1690 err
= _mesa_es3_error_check_format_and_type(ctx
, format
, type
,
1692 if (err
!= GL_NO_ERROR
) {
1693 _mesa_error(ctx
, err
,
1694 "%s(format = %s, type = %s, internalformat = %s)",
1695 callerName
, _mesa_enum_to_string(format
),
1696 _mesa_enum_to_string(type
),
1697 _mesa_enum_to_string(internalFormat
));
1702 err
= _mesa_es_error_check_format_and_type(ctx
, format
, type
, dimensions
);
1703 if (err
!= GL_NO_ERROR
) {
1704 _mesa_error(ctx
, err
, "%s(format = %s, type = %s)",
1705 callerName
, _mesa_enum_to_string(format
),
1706 _mesa_enum_to_string(type
));
1715 * Test the glTexImage[123]D() parameters for errors.
1717 * \param ctx GL context.
1718 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1719 * \param target texture target given by the user (already validated).
1720 * \param level image level given by the user.
1721 * \param internalFormat internal format given by the user.
1722 * \param format pixel data format given by the user.
1723 * \param type pixel data type given by the user.
1724 * \param width image width given by the user.
1725 * \param height image height given by the user.
1726 * \param depth image depth given by the user.
1727 * \param border image border given by the user.
1729 * \return GL_TRUE if a error is found, GL_FALSE otherwise
1731 * Verifies each of the parameters against the constants specified in
1732 * __struct gl_contextRec::Const and the supported extensions, and according
1733 * to the OpenGL specification.
1734 * Note that we don't fully error-check the width, height, depth values
1735 * here. That's done in _mesa_legal_texture_dimensions() which is used
1736 * by several other GL entrypoints. Plus, texture dims have a special
1737 * interaction with proxy textures.
1740 texture_error_check( struct gl_context
*ctx
,
1741 GLuint dimensions
, GLenum target
,
1742 GLint level
, GLint internalFormat
,
1743 GLenum format
, GLenum type
,
1744 GLint width
, GLint height
,
1745 GLint depth
, GLint border
,
1746 const GLvoid
*pixels
)
1750 /* Note: for proxy textures, some error conditions immediately generate
1751 * a GL error in the usual way. But others do not generate a GL error.
1752 * Instead, they cause the width, height, depth, format fields of the
1753 * texture image to be zeroed-out. The GL spec seems to indicate that the
1754 * zero-out behaviour is only used in cases related to memory allocation.
1758 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
1759 _mesa_error(ctx
, GL_INVALID_VALUE
,
1760 "glTexImage%dD(level=%d)", dimensions
, level
);
1765 if (border
< 0 || border
> 1 ||
1766 ((ctx
->API
!= API_OPENGL_COMPAT
||
1767 target
== GL_TEXTURE_RECTANGLE_NV
||
1768 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1769 _mesa_error(ctx
, GL_INVALID_VALUE
,
1770 "glTexImage%dD(border=%d)", dimensions
, border
);
1774 if (width
< 0 || height
< 0 || depth
< 0) {
1775 _mesa_error(ctx
, GL_INVALID_VALUE
,
1776 "glTexImage%dD(width, height or depth < 0)", dimensions
);
1780 /* Check incoming image format and type */
1781 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1782 if (err
!= GL_NO_ERROR
) {
1783 /* Prior to OpenGL-ES 2.0, an INVALID_VALUE is expected instead of
1784 * INVALID_ENUM. From page 73 OpenGL ES 1.1 spec:
1786 * "Specifying a value for internalformat that is not one of the
1787 * above (acceptable) values generates the error INVALID VALUE."
1789 if (err
== GL_INVALID_ENUM
&& _mesa_is_gles(ctx
) && ctx
->Version
< 20)
1790 err
= GL_INVALID_VALUE
;
1792 _mesa_error(ctx
, err
,
1793 "glTexImage%dD(incompatible format = %s, type = %s)",
1794 dimensions
, _mesa_enum_to_string(format
),
1795 _mesa_enum_to_string(type
));
1799 /* Check internalFormat */
1800 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1801 _mesa_error(ctx
, GL_INVALID_VALUE
,
1802 "glTexImage%dD(internalFormat=%s)",
1803 dimensions
, _mesa_enum_to_string(internalFormat
));
1807 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1808 * combinations of format, internalFormat, and type that can be used.
1809 * Formats and types that require additional extensions (e.g., GL_FLOAT
1810 * requires GL_OES_texture_float) are filtered elsewhere.
1812 if (_mesa_is_gles(ctx
) &&
1813 texture_format_error_check_gles(ctx
, format
, type
, internalFormat
,
1814 dimensions
, "glTexImage%dD")) {
1818 /* validate the bound PBO, if any */
1819 if (!_mesa_validate_pbo_source(ctx
, dimensions
, &ctx
->Unpack
,
1820 width
, height
, depth
, format
, type
,
1821 INT_MAX
, pixels
, "glTexImage")) {
1825 /* make sure internal format and format basically agree */
1826 if (!texture_formats_agree(internalFormat
, format
)) {
1827 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1828 "glTexImage%dD(incompatible internalFormat = %s, format = %s)",
1829 dimensions
, _mesa_enum_to_string(internalFormat
),
1830 _mesa_enum_to_string(format
));
1834 /* additional checks for ycbcr textures */
1835 if (internalFormat
== GL_YCBCR_MESA
) {
1836 assert(ctx
->Extensions
.MESA_ycbcr_texture
);
1837 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
1838 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
1840 _mesa_snprintf(message
, sizeof(message
),
1841 "glTexImage%dD(format/type YCBCR mismatch)",
1843 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", message
);
1844 return GL_TRUE
; /* error */
1846 if (target
!= GL_TEXTURE_2D
&&
1847 target
!= GL_PROXY_TEXTURE_2D
&&
1848 target
!= GL_TEXTURE_RECTANGLE_NV
&&
1849 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
1850 _mesa_error(ctx
, GL_INVALID_ENUM
,
1851 "glTexImage%dD(bad target for YCbCr texture)",
1857 _mesa_snprintf(message
, sizeof(message
),
1858 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1859 dimensions
, border
);
1860 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", message
);
1865 /* additional checks for depth textures */
1866 if (!_mesa_legal_texture_base_format_for_target(ctx
, target
, internalFormat
,
1867 dimensions
, "glTexImage"))
1870 /* additional checks for compressed textures */
1871 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
1873 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
, &err
)) {
1874 _mesa_error(ctx
, err
,
1875 "glTexImage%dD(target can't be compressed)", dimensions
);
1878 if (_mesa_format_no_online_compression(ctx
, internalFormat
)) {
1879 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1880 "glTexImage%dD(no compression for format)", dimensions
);
1884 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1885 "glTexImage%dD(border!=0)", dimensions
);
1890 /* additional checks for integer textures */
1891 if ((ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) &&
1892 (_mesa_is_enum_format_integer(format
) !=
1893 _mesa_is_enum_format_integer(internalFormat
))) {
1894 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1895 "glTexImage%dD(integer/non-integer format mismatch)",
1900 if (!mutable_tex_object(ctx
, target
)) {
1901 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1902 "glTexImage%dD(immutable texture)", dimensions
);
1906 /* if we get here, the parameters are OK */
1912 * Error checking for glCompressedTexImage[123]D().
1913 * Note that the width, height and depth values are not fully error checked
1915 * \return GL_TRUE if a error is found, GL_FALSE otherwise
1918 compressed_texture_error_check(struct gl_context
*ctx
, GLint dimensions
,
1919 GLenum target
, GLint level
,
1920 GLenum internalFormat
, GLsizei width
,
1921 GLsizei height
, GLsizei depth
, GLint border
,
1922 GLsizei imageSize
, const GLvoid
*data
)
1924 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1926 GLenum error
= GL_NO_ERROR
;
1927 char *reason
= ""; /* no error */
1929 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
, &error
)) {
1934 /* This will detect any invalid internalFormat value */
1935 if (!_mesa_is_compressed_format(ctx
, internalFormat
)) {
1936 _mesa_error(ctx
, GL_INVALID_ENUM
,
1937 "glCompressedTexImage%dD(internalFormat=%s)",
1938 dimensions
, _mesa_enum_to_string(internalFormat
));
1942 /* validate the bound PBO, if any */
1943 if (!_mesa_validate_pbo_source_compressed(ctx
, dimensions
, &ctx
->Unpack
,
1945 "glCompressedTexImage")) {
1949 switch (internalFormat
) {
1950 case GL_PALETTE4_RGB8_OES
:
1951 case GL_PALETTE4_RGBA8_OES
:
1952 case GL_PALETTE4_R5_G6_B5_OES
:
1953 case GL_PALETTE4_RGBA4_OES
:
1954 case GL_PALETTE4_RGB5_A1_OES
:
1955 case GL_PALETTE8_RGB8_OES
:
1956 case GL_PALETTE8_RGBA8_OES
:
1957 case GL_PALETTE8_R5_G6_B5_OES
:
1958 case GL_PALETTE8_RGBA4_OES
:
1959 case GL_PALETTE8_RGB5_A1_OES
:
1960 /* check level (note that level should be zero or less!) */
1961 if (level
> 0 || level
< -maxLevels
) {
1963 error
= GL_INVALID_VALUE
;
1967 if (dimensions
!= 2) {
1968 reason
= "compressed paletted textures must be 2D";
1969 error
= GL_INVALID_OPERATION
;
1973 /* Figure out the expected texture size (in bytes). This will be
1974 * checked against the actual size later.
1976 expectedSize
= _mesa_cpal_compressed_size(level
, internalFormat
,
1979 /* This is for the benefit of the TestProxyTexImage below. It expects
1980 * level to be non-negative. OES_compressed_paletted_texture uses a
1981 * weird mechanism where the level specified to glCompressedTexImage2D
1982 * is -(n-1) number of levels in the texture, and the data specifies the
1983 * complete mipmap stack. This is done to ensure the palette is the
1984 * same for all levels.
1991 if (level
< 0 || level
>= maxLevels
) {
1993 error
= GL_INVALID_VALUE
;
1997 /* Figure out the expected texture size (in bytes). This will be
1998 * checked against the actual size later.
2000 expectedSize
= compressed_tex_size(width
, height
, depth
, internalFormat
);
2004 /* This should really never fail */
2005 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
2006 reason
= "internalFormat";
2007 error
= GL_INVALID_ENUM
;
2011 /* No compressed formats support borders at this time */
2013 reason
= "border != 0";
2014 error
= GL_INVALID_VALUE
;
2018 /* Check for invalid pixel storage modes */
2019 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dimensions
,
2021 "glCompressedTexImage")) {
2025 /* check image size in bytes */
2026 if (expectedSize
!= imageSize
) {
2027 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
2028 * if <imageSize> is not consistent with the format, dimensions, and
2029 * contents of the specified image.
2031 reason
= "imageSize inconsistent with width/height/format";
2032 error
= GL_INVALID_VALUE
;
2036 if (!mutable_tex_object(ctx
, target
)) {
2037 reason
= "immutable texture";
2038 error
= GL_INVALID_OPERATION
;
2045 /* Note: not all error paths exit through here. */
2046 _mesa_error(ctx
, error
, "glCompressedTexImage%dD(%s)",
2047 dimensions
, reason
);
2054 * Test glTexSubImage[123]D() parameters for errors.
2056 * \param ctx GL context.
2057 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2058 * \param target texture target given by the user (already validated)
2059 * \param level image level given by the user.
2060 * \param xoffset sub-image x offset given by the user.
2061 * \param yoffset sub-image y offset given by the user.
2062 * \param zoffset sub-image z offset given by the user.
2063 * \param format pixel data format given by the user.
2064 * \param type pixel data type given by the user.
2065 * \param width image width given by the user.
2066 * \param height image height given by the user.
2067 * \param depth image depth given by the user.
2069 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2071 * Verifies each of the parameters against the constants specified in
2072 * __struct gl_contextRec::Const and the supported extensions, and according
2073 * to the OpenGL specification.
2076 texsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2077 struct gl_texture_object
*texObj
,
2078 GLenum target
, GLint level
,
2079 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2080 GLint width
, GLint height
, GLint depth
,
2081 GLenum format
, GLenum type
, const GLvoid
*pixels
,
2082 bool dsa
, const char *callerName
)
2084 struct gl_texture_image
*texImage
;
2088 /* must be out of memory */
2089 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", callerName
);
2094 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2095 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(level=%d)", callerName
, level
);
2099 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
2101 /* non-existant texture level */
2102 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid texture image)",
2107 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
2108 if (err
!= GL_NO_ERROR
) {
2109 _mesa_error(ctx
, err
,
2110 "%s(incompatible format = %s, type = %s)",
2111 callerName
, _mesa_enum_to_string(format
),
2112 _mesa_enum_to_string(type
));
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.
2121 if (_mesa_is_gles(ctx
) &&
2122 texture_format_error_check_gles(ctx
, format
, type
,
2123 texImage
->InternalFormat
,
2124 dimensions
, callerName
)) {
2128 /* validate the bound PBO, if any */
2129 if (!_mesa_validate_pbo_source(ctx
, dimensions
, &ctx
->Unpack
,
2130 width
, height
, depth
, format
, type
,
2131 INT_MAX
, pixels
, callerName
)) {
2135 if (error_check_subtexture_dimensions(ctx
, dimensions
,
2136 texImage
, xoffset
, yoffset
, zoffset
,
2137 width
, height
, depth
, callerName
)) {
2141 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2142 if (_mesa_format_no_online_compression(ctx
, texImage
->InternalFormat
)) {
2143 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2144 "%s(no compression for format)", callerName
);
2149 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
2150 /* both source and dest must be integer-valued, or neither */
2151 if (_mesa_is_format_integer_color(texImage
->TexFormat
) !=
2152 _mesa_is_enum_format_integer(format
)) {
2153 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2154 "%s(integer/non-integer format mismatch)", callerName
);
2164 * Test glCopyTexImage[12]D() parameters for errors.
2166 * \param ctx GL context.
2167 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2168 * \param target texture target given by the user.
2169 * \param level image level given by the user.
2170 * \param internalFormat internal format given by the user.
2171 * \param width image width given by the user.
2172 * \param height image height given by the user.
2173 * \param border texture border.
2175 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2177 * Verifies each of the parameters against the constants specified in
2178 * __struct gl_contextRec::Const and the supported extensions, and according
2179 * to the OpenGL specification.
2182 copytexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
2183 GLenum target
, GLint level
, GLint internalFormat
,
2184 GLint width
, GLint height
, GLint border
)
2187 GLint rb_base_format
;
2188 struct gl_renderbuffer
*rb
;
2189 GLenum rb_internal_format
;
2192 if (!legal_texsubimage_target(ctx
, dimensions
, target
, false)) {
2193 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexImage%uD(target=%s)",
2194 dimensions
, _mesa_enum_to_string(target
));
2199 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2200 _mesa_error(ctx
, GL_INVALID_VALUE
,
2201 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
2205 /* Check that the source buffer is complete */
2206 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2207 if (ctx
->ReadBuffer
->_Status
== 0) {
2208 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2210 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2211 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2212 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
2216 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2217 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2218 "glCopyTexImage%dD(multisample FBO)", dimensions
);
2224 if (border
< 0 || border
> 1 ||
2225 ((ctx
->API
!= API_OPENGL_COMPAT
||
2226 target
== GL_TEXTURE_RECTANGLE_NV
||
2227 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
2228 _mesa_error(ctx
, GL_INVALID_VALUE
,
2229 "glCopyTexImage%dD(border=%d)", dimensions
, border
);
2233 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2236 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
2237 switch (internalFormat
) {
2242 case GL_LUMINANCE_ALPHA
:
2245 _mesa_error(ctx
, GL_INVALID_ENUM
,
2246 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2247 _mesa_enum_to_string(internalFormat
));
2252 baseFormat
= _mesa_base_tex_format(ctx
, internalFormat
);
2253 if (baseFormat
< 0) {
2254 _mesa_error(ctx
, GL_INVALID_ENUM
,
2255 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2256 _mesa_enum_to_string(internalFormat
));
2260 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, internalFormat
);
2262 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2263 "glCopyTexImage%dD(read buffer)", dimensions
);
2267 rb_internal_format
= rb
->InternalFormat
;
2268 rb_base_format
= _mesa_base_tex_format(ctx
, rb
->InternalFormat
);
2269 if (_mesa_is_color_format(internalFormat
)) {
2270 if (rb_base_format
< 0) {
2271 _mesa_error(ctx
, GL_INVALID_VALUE
,
2272 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2273 _mesa_enum_to_string(internalFormat
));
2278 if (_mesa_is_gles(ctx
)) {
2280 if (_mesa_base_format_component_count(baseFormat
) >
2281 _mesa_base_format_component_count(rb_base_format
)) {
2284 if (baseFormat
== GL_DEPTH_COMPONENT
||
2285 baseFormat
== GL_DEPTH_STENCIL
||
2286 rb_base_format
== GL_DEPTH_COMPONENT
||
2287 rb_base_format
== GL_DEPTH_STENCIL
||
2288 ((baseFormat
== GL_LUMINANCE_ALPHA
||
2289 baseFormat
== GL_ALPHA
) &&
2290 rb_base_format
!= GL_RGBA
) ||
2291 internalFormat
== GL_RGB9_E5
) {
2294 if (internalFormat
== GL_RGB9_E5
) {
2298 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2299 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2300 _mesa_enum_to_string(internalFormat
));
2305 if (_mesa_is_gles3(ctx
)) {
2306 bool rb_is_srgb
= false;
2307 bool dst_is_srgb
= false;
2309 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&&
2310 _mesa_get_format_color_encoding(rb
->Format
) == GL_SRGB
) {
2314 if (_mesa_get_linear_internalformat(internalFormat
) != internalFormat
) {
2318 if (rb_is_srgb
!= dst_is_srgb
) {
2319 /* Page 137 (page 149 of the PDF) in section 3.8.5 of the
2320 * OpenGLES 3.0.0 spec says:
2322 * "The error INVALID_OPERATION is also generated if the
2323 * value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the
2324 * framebuffer attachment corresponding to the read buffer
2325 * is LINEAR (see section 6.1.13) and internalformat is
2326 * one of the sRGB formats described in section 3.8.16, or
2327 * if the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING is
2328 * SRGB and internalformat is not one of the sRGB formats."
2330 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2331 "glCopyTexImage%dD(srgb usage mismatch)", dimensions
);
2335 /* Page 139, Table 3.15 of OpenGL ES 3.0 spec does not define ReadPixels
2336 * types for SNORM formats. Also, conversion to SNORM formats is not
2337 * allowed by Table 3.2 on Page 110.
2339 if (_mesa_is_enum_format_snorm(internalFormat
)) {
2340 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2341 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2342 _mesa_enum_to_string(internalFormat
));
2347 if (!_mesa_source_buffer_exists(ctx
, baseFormat
)) {
2348 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2349 "glCopyTexImage%dD(missing readbuffer)", dimensions
);
2353 /* From the EXT_texture_integer spec:
2355 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2356 * if the texture internalformat is an integer format and the read color
2357 * buffer is not an integer format, or if the internalformat is not an
2358 * integer format and the read color buffer is an integer format."
2360 if (_mesa_is_color_format(internalFormat
)) {
2361 bool is_int
= _mesa_is_enum_format_integer(internalFormat
);
2362 bool is_rbint
= _mesa_is_enum_format_integer(rb_internal_format
);
2363 bool is_unorm
= _mesa_is_enum_format_unorm(internalFormat
);
2364 bool is_rbunorm
= _mesa_is_enum_format_unorm(rb_internal_format
);
2365 if (is_int
|| is_rbint
) {
2366 if (is_int
!= is_rbint
) {
2367 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2368 "glCopyTexImage%dD(integer vs non-integer)", dimensions
);
2370 } else if (_mesa_is_gles(ctx
) &&
2371 _mesa_is_enum_format_unsigned_int(internalFormat
) !=
2372 _mesa_is_enum_format_unsigned_int(rb_internal_format
)) {
2373 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2374 "glCopyTexImage%dD(signed vs unsigned integer)", dimensions
);
2379 /* From page 138 of OpenGL ES 3.0 spec:
2380 * "The error INVALID_OPERATION is generated if floating-point RGBA
2381 * data is required; if signed integer RGBA data is required and the
2382 * format of the current color buffer is not signed integer; if
2383 * unsigned integer RGBA data is required and the format of the
2384 * current color buffer is not unsigned integer; or if fixed-point
2385 * RGBA data is required and the format of the current color buffer
2386 * is not fixed-point.
2388 if (_mesa_is_gles(ctx
) && is_unorm
!= is_rbunorm
)
2389 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2390 "glCopyTexImage%dD(unorm vs non-unorm)", dimensions
);
2393 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
2395 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
, &err
)) {
2396 _mesa_error(ctx
, err
,
2397 "glCopyTexImage%dD(target can't be compressed)", dimensions
);
2400 if (_mesa_format_no_online_compression(ctx
, internalFormat
)) {
2401 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2402 "glCopyTexImage%dD(no compression for format)", dimensions
);
2406 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2407 "glCopyTexImage%dD(border!=0)", dimensions
);
2412 if (!mutable_tex_object(ctx
, target
)) {
2413 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2414 "glCopyTexImage%dD(immutable texture)", dimensions
);
2418 /* if we get here, the parameters are OK */
2424 * Test glCopyTexSubImage[12]D() parameters for errors.
2425 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2428 copytexsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2429 const struct gl_texture_object
*texObj
,
2430 GLenum target
, GLint level
,
2431 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2432 GLint width
, GLint height
, const char *caller
)
2434 struct gl_texture_image
*texImage
;
2436 /* Check that the source buffer is complete */
2437 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2438 if (ctx
->ReadBuffer
->_Status
== 0) {
2439 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2441 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2442 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2443 "%s(invalid readbuffer)", caller
);
2447 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2448 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2449 "%s(multisample FBO)", caller
);
2455 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2456 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(level=%d)", caller
, level
);
2460 /* Get dest image pointers */
2462 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", caller
);
2466 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
2468 /* destination image does not exist */
2469 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2470 "%s(invalid texture image)", caller
);
2474 if (error_check_subtexture_dimensions(ctx
, dimensions
, texImage
,
2475 xoffset
, yoffset
, zoffset
,
2476 width
, height
, 1, caller
)) {
2480 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2481 if (_mesa_format_no_online_compression(ctx
, texImage
->InternalFormat
)) {
2482 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2483 "%s(no compression for format)", caller
);
2488 if (texImage
->InternalFormat
== GL_YCBCR_MESA
) {
2489 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s()", caller
);
2493 if (!_mesa_source_buffer_exists(ctx
, texImage
->_BaseFormat
)) {
2494 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2495 "%s(missing readbuffer, format=%s)", caller
,
2496 _mesa_enum_to_string(texImage
->_BaseFormat
));
2500 /* From the EXT_texture_integer spec:
2502 * "INVALID_OPERATION is generated by CopyTexImage* and
2503 * CopyTexSubImage* if the texture internalformat is an integer format
2504 * and the read color buffer is not an integer format, or if the
2505 * internalformat is not an integer format and the read color buffer
2506 * is an integer format."
2508 if (_mesa_is_color_format(texImage
->InternalFormat
)) {
2509 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
2511 if (_mesa_is_format_integer_color(rb
->Format
) !=
2512 _mesa_is_format_integer_color(texImage
->TexFormat
)) {
2513 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2514 "%s(integer vs non-integer)", caller
);
2519 /* if we get here, the parameters are OK */
2524 /** Callback info for walking over FBO hash table */
2527 struct gl_context
*ctx
;
2528 struct gl_texture_object
*texObj
;
2534 * Check render to texture callback. Called from _mesa_HashWalk().
2537 check_rtt_cb(GLuint key
, void *data
, void *userData
)
2539 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
2540 const struct cb_info
*info
= (struct cb_info
*) userData
;
2541 struct gl_context
*ctx
= info
->ctx
;
2542 const struct gl_texture_object
*texObj
= info
->texObj
;
2543 const GLuint level
= info
->level
, face
= info
->face
;
2545 /* If this is a user-created FBO */
2546 if (_mesa_is_user_fbo(fb
)) {
2548 /* check if any of the FBO's attachments point to 'texObj' */
2549 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2550 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2551 if (att
->Type
== GL_TEXTURE
&&
2552 att
->Texture
== texObj
&&
2553 att
->TextureLevel
== level
&&
2554 att
->CubeMapFace
== face
) {
2555 _mesa_update_texture_renderbuffer(ctx
, ctx
->DrawBuffer
, att
);
2556 assert(att
->Renderbuffer
->TexImage
);
2557 /* Mark fb status as indeterminate to force re-validation */
2566 * When a texture image is specified we have to check if it's bound to
2567 * any framebuffer objects (render to texture) in order to detect changes
2568 * in size or format since that effects FBO completeness.
2569 * Any FBOs rendering into the texture must be re-validated.
2572 _mesa_update_fbo_texture(struct gl_context
*ctx
,
2573 struct gl_texture_object
*texObj
,
2574 GLuint face
, GLuint level
)
2576 /* Only check this texture if it's been marked as RenderToTexture */
2577 if (texObj
->_RenderToTexture
) {
2578 struct cb_info info
;
2580 info
.texObj
= texObj
;
2583 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, check_rtt_cb
, &info
);
2589 * If the texture object's GenerateMipmap flag is set and we've
2590 * changed the texture base level image, regenerate the rest of the
2591 * mipmap levels now.
2594 check_gen_mipmap(struct gl_context
*ctx
, GLenum target
,
2595 struct gl_texture_object
*texObj
, GLint level
)
2597 if (texObj
->GenerateMipmap
&&
2598 level
== texObj
->BaseLevel
&&
2599 level
< texObj
->MaxLevel
) {
2600 assert(ctx
->Driver
.GenerateMipmap
);
2601 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2606 /** Debug helper: override the user-requested internal format */
2608 override_internal_format(GLenum internalFormat
, GLint width
, GLint height
)
2611 if (internalFormat
== GL_RGBA16F_ARB
||
2612 internalFormat
== GL_RGBA32F_ARB
) {
2613 printf("Convert rgba float tex to int %d x %d\n", width
, height
);
2616 else if (internalFormat
== GL_RGB16F_ARB
||
2617 internalFormat
== GL_RGB32F_ARB
) {
2618 printf("Convert rgb float tex to int %d x %d\n", width
, height
);
2621 else if (internalFormat
== GL_LUMINANCE_ALPHA16F_ARB
||
2622 internalFormat
== GL_LUMINANCE_ALPHA32F_ARB
) {
2623 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2624 return GL_LUMINANCE_ALPHA
;
2626 else if (internalFormat
== GL_LUMINANCE16F_ARB
||
2627 internalFormat
== GL_LUMINANCE32F_ARB
) {
2628 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2629 return GL_LUMINANCE
;
2631 else if (internalFormat
== GL_ALPHA16F_ARB
||
2632 internalFormat
== GL_ALPHA32F_ARB
) {
2633 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2637 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2638 internalFormat = GL_RGBA;
2642 return internalFormat
;
2645 return internalFormat
;
2651 * Choose the actual hardware format for a texture image.
2652 * Try to use the same format as the previous image level when possible.
2653 * Otherwise, ask the driver for the best format.
2654 * It's important to try to choose a consistant format for all levels
2655 * for efficient texture memory layout/allocation. In particular, this
2656 * comes up during automatic mipmap generation.
2659 _mesa_choose_texture_format(struct gl_context
*ctx
,
2660 struct gl_texture_object
*texObj
,
2661 GLenum target
, GLint level
,
2662 GLenum internalFormat
, GLenum format
, GLenum type
)
2666 /* see if we've already chosen a format for the previous level */
2668 struct gl_texture_image
*prevImage
=
2669 _mesa_select_tex_image(texObj
, target
, level
- 1);
2670 /* See if the prev level is defined and has an internal format which
2671 * matches the new internal format.
2674 prevImage
->Width
> 0 &&
2675 prevImage
->InternalFormat
== internalFormat
) {
2676 /* use the same format */
2677 assert(prevImage
->TexFormat
!= MESA_FORMAT_NONE
);
2678 return prevImage
->TexFormat
;
2682 /* If the application requested compression to an S3TC format but we don't
2683 * have the DXTn library, force a generic compressed format instead.
2685 if (internalFormat
!= format
&& format
!= GL_NONE
) {
2686 const GLenum before
= internalFormat
;
2688 switch (internalFormat
) {
2689 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
2690 if (!ctx
->Mesa_DXTn
)
2691 internalFormat
= GL_COMPRESSED_RGB
;
2693 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
2694 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
2695 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
2696 if (!ctx
->Mesa_DXTn
)
2697 internalFormat
= GL_COMPRESSED_RGBA
;
2703 if (before
!= internalFormat
) {
2705 "DXT compression requested (%s), "
2706 "but libtxc_dxtn library not installed. Using %s "
2708 _mesa_enum_to_string(before
),
2709 _mesa_enum_to_string(internalFormat
));
2713 /* choose format from scratch */
2714 f
= ctx
->Driver
.ChooseTextureFormat(ctx
, target
, internalFormat
,
2716 assert(f
!= MESA_FORMAT_NONE
);
2722 * Adjust pixel unpack params and image dimensions to strip off the
2723 * one-pixel texture border.
2725 * Gallium and intel don't support texture borders. They've seldem been used
2726 * and seldom been implemented correctly anyway.
2728 * \param unpackNew returns the new pixel unpack parameters
2731 strip_texture_border(GLenum target
,
2732 GLint
*width
, GLint
*height
, GLint
*depth
,
2733 const struct gl_pixelstore_attrib
*unpack
,
2734 struct gl_pixelstore_attrib
*unpackNew
)
2740 *unpackNew
= *unpack
;
2742 if (unpackNew
->RowLength
== 0)
2743 unpackNew
->RowLength
= *width
;
2745 if (unpackNew
->ImageHeight
== 0)
2746 unpackNew
->ImageHeight
= *height
;
2748 assert(*width
>= 3);
2749 unpackNew
->SkipPixels
++; /* skip the border */
2750 *width
= *width
- 2; /* reduce the width by two border pixels */
2752 /* The min height of a texture with a border is 3 */
2753 if (*height
>= 3 && target
!= GL_TEXTURE_1D_ARRAY
) {
2754 unpackNew
->SkipRows
++; /* skip the border */
2755 *height
= *height
- 2; /* reduce the height by two border pixels */
2759 target
!= GL_TEXTURE_2D_ARRAY
&&
2760 target
!= GL_TEXTURE_CUBE_MAP_ARRAY
) {
2761 unpackNew
->SkipImages
++; /* skip the border */
2762 *depth
= *depth
- 2; /* reduce the depth by two border pixels */
2768 * Common code to implement all the glTexImage1D/2D/3D functions
2769 * as well as glCompressedTexImage1D/2D/3D.
2770 * \param compressed only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
2771 * \param format the user's image format (only used if !compressed)
2772 * \param type the user's image type (only used if !compressed)
2773 * \param imageSize only used for glCompressedTexImage1D/2D/3D calls.
2776 teximage(struct gl_context
*ctx
, GLboolean compressed
, GLuint dims
,
2777 GLenum target
, GLint level
, GLint internalFormat
,
2778 GLsizei width
, GLsizei height
, GLsizei depth
,
2779 GLint border
, GLenum format
, GLenum type
,
2780 GLsizei imageSize
, const GLvoid
*pixels
)
2782 const char *func
= compressed
? "glCompressedTexImage" : "glTexImage";
2783 struct gl_pixelstore_attrib unpack_no_border
;
2784 const struct gl_pixelstore_attrib
*unpack
= &ctx
->Unpack
;
2785 struct gl_texture_object
*texObj
;
2786 mesa_format texFormat
;
2787 GLboolean dimensionsOK
, sizeOK
;
2789 FLUSH_VERTICES(ctx
, 0);
2791 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
)) {
2794 "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
2796 _mesa_enum_to_string(target
), level
,
2797 _mesa_enum_to_string(internalFormat
),
2798 width
, height
, depth
, border
, pixels
);
2801 "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2803 _mesa_enum_to_string(target
), level
,
2804 _mesa_enum_to_string(internalFormat
),
2805 width
, height
, depth
, border
,
2806 _mesa_enum_to_string(format
),
2807 _mesa_enum_to_string(type
), pixels
);
2810 internalFormat
= override_internal_format(internalFormat
, width
, height
);
2812 /* target error checking */
2813 if (!legal_teximage_target(ctx
, dims
, target
)) {
2814 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s%uD(target=%s)",
2815 func
, dims
, _mesa_enum_to_string(target
));
2819 /* general error checking */
2821 if (compressed_texture_error_check(ctx
, dims
, target
, level
,
2823 width
, height
, depth
,
2824 border
, imageSize
, pixels
))
2828 if (texture_error_check(ctx
, dims
, target
, level
, internalFormat
,
2829 format
, type
, width
, height
, depth
, border
,
2834 /* Here we convert a cpal compressed image into a regular glTexImage2D
2835 * call by decompressing the texture. If we really want to support cpal
2836 * textures in any driver this would have to be changed.
2838 if (ctx
->API
== API_OPENGLES
&& compressed
&& dims
== 2) {
2839 switch (internalFormat
) {
2840 case GL_PALETTE4_RGB8_OES
:
2841 case GL_PALETTE4_RGBA8_OES
:
2842 case GL_PALETTE4_R5_G6_B5_OES
:
2843 case GL_PALETTE4_RGBA4_OES
:
2844 case GL_PALETTE4_RGB5_A1_OES
:
2845 case GL_PALETTE8_RGB8_OES
:
2846 case GL_PALETTE8_RGBA8_OES
:
2847 case GL_PALETTE8_R5_G6_B5_OES
:
2848 case GL_PALETTE8_RGBA4_OES
:
2849 case GL_PALETTE8_RGB5_A1_OES
:
2850 _mesa_cpal_compressed_teximage2d(target
, level
, internalFormat
,
2851 width
, height
, imageSize
, pixels
);
2856 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2860 /* For glCompressedTexImage() the driver has no choice about the
2861 * texture format since we'll never transcode the user's compressed
2862 * image data. The internalFormat was error checked earlier.
2864 texFormat
= _mesa_glenum_to_compressed_format(internalFormat
);
2867 /* In case of HALF_FLOAT_OES or FLOAT_OES, find corresponding sized
2868 * internal floating point format for the given base format.
2870 if (_mesa_is_gles(ctx
) && format
== internalFormat
) {
2871 if (type
== GL_FLOAT
) {
2872 texObj
->_IsFloat
= GL_TRUE
;
2873 } else if (type
== GL_HALF_FLOAT_OES
|| type
== GL_HALF_FLOAT
) {
2874 texObj
->_IsHalfFloat
= GL_TRUE
;
2877 internalFormat
= adjust_for_oes_float_texture(format
, type
);
2880 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
2881 internalFormat
, format
, type
);
2884 assert(texFormat
!= MESA_FORMAT_NONE
);
2886 /* check that width, height, depth are legal for the mipmap level */
2887 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, level
, width
,
2888 height
, depth
, border
);
2890 /* check that the texture won't take too much memory, etc */
2891 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target(target
),
2893 width
, height
, depth
, border
);
2895 if (_mesa_is_proxy_texture(target
)) {
2896 /* Proxy texture: just clear or set state depending on error checking */
2897 struct gl_texture_image
*texImage
=
2898 get_proxy_tex_image(ctx
, target
, level
);
2901 return; /* GL_OUT_OF_MEMORY already recorded */
2903 if (dimensionsOK
&& sizeOK
) {
2904 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, depth
,
2905 border
, internalFormat
, texFormat
);
2908 clear_teximage_fields(texImage
);
2912 /* non-proxy target */
2913 const GLuint face
= _mesa_tex_target_to_face(target
);
2914 struct gl_texture_image
*texImage
;
2916 if (!dimensionsOK
) {
2917 _mesa_error(ctx
, GL_INVALID_VALUE
,
2918 "%s%uD(invalid width or height or depth)",
2924 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
2925 "%s%uD(image too large: %d x %d x %d, %s format)",
2926 func
, dims
, width
, height
, depth
,
2927 _mesa_enum_to_string(internalFormat
));
2931 /* Allow a hardware driver to just strip out the border, to provide
2932 * reliable but slightly incorrect hardware rendering instead of
2933 * rarely-tested software fallback rendering.
2935 if (border
&& ctx
->Const
.StripTextureBorder
) {
2936 strip_texture_border(target
, &width
, &height
, &depth
, unpack
,
2939 unpack
= &unpack_no_border
;
2942 if (ctx
->NewState
& _NEW_PIXEL
)
2943 _mesa_update_state(ctx
);
2945 _mesa_lock_texture(ctx
, texObj
);
2947 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2950 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s%uD", func
, dims
);
2953 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
2955 _mesa_init_teximage_fields(ctx
, texImage
,
2956 width
, height
, depth
,
2957 border
, internalFormat
, texFormat
);
2959 /* Give the texture to the driver. <pixels> may be null. */
2960 if (width
> 0 && height
> 0 && depth
> 0) {
2962 ctx
->Driver
.CompressedTexImage(ctx
, dims
, texImage
,
2966 ctx
->Driver
.TexImage(ctx
, dims
, texImage
, format
,
2967 type
, pixels
, unpack
);
2971 check_gen_mipmap(ctx
, target
, texObj
, level
);
2973 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
2975 _mesa_dirty_texobj(ctx
, texObj
);
2978 _mesa_unlock_texture(ctx
, texObj
);
2985 * Called from the API. Note that width includes the border.
2988 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
2989 GLsizei width
, GLint border
, GLenum format
,
2990 GLenum type
, const GLvoid
*pixels
)
2992 GET_CURRENT_CONTEXT(ctx
);
2993 teximage(ctx
, GL_FALSE
, 1, target
, level
, internalFormat
, width
, 1, 1,
2994 border
, format
, type
, 0, pixels
);
2999 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
3000 GLsizei width
, GLsizei height
, GLint border
,
3001 GLenum format
, GLenum type
,
3002 const GLvoid
*pixels
)
3004 GET_CURRENT_CONTEXT(ctx
);
3005 teximage(ctx
, GL_FALSE
, 2, target
, level
, internalFormat
, width
, height
, 1,
3006 border
, format
, type
, 0, pixels
);
3011 * Called by the API or display list executor.
3012 * Note that width and height include the border.
3015 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
3016 GLsizei width
, GLsizei height
, GLsizei depth
,
3017 GLint border
, GLenum format
, GLenum type
,
3018 const GLvoid
*pixels
)
3020 GET_CURRENT_CONTEXT(ctx
);
3021 teximage(ctx
, GL_FALSE
, 3, target
, level
, internalFormat
,
3022 width
, height
, depth
,
3023 border
, format
, type
, 0, pixels
);
3028 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
3029 GLsizei width
, GLsizei height
, GLsizei depth
,
3030 GLint border
, GLenum format
, GLenum type
,
3031 const GLvoid
*pixels
)
3033 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
3034 depth
, border
, format
, type
, pixels
);
3039 _mesa_EGLImageTargetTexture2DOES (GLenum target
, GLeglImageOES image
)
3041 struct gl_texture_object
*texObj
;
3042 struct gl_texture_image
*texImage
;
3044 GET_CURRENT_CONTEXT(ctx
);
3045 FLUSH_VERTICES(ctx
, 0);
3049 valid_target
= ctx
->Extensions
.OES_EGL_image
;
3051 case GL_TEXTURE_EXTERNAL_OES
:
3053 _mesa_is_gles(ctx
) ? ctx
->Extensions
.OES_EGL_image_external
: false;
3056 valid_target
= false;
3060 if (!valid_target
) {
3061 _mesa_error(ctx
, GL_INVALID_ENUM
,
3062 "glEGLImageTargetTexture2D(target=%d)", target
);
3067 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3068 "glEGLImageTargetTexture2D(image=%p)", image
);
3072 if (ctx
->NewState
& _NEW_PIXEL
)
3073 _mesa_update_state(ctx
);
3075 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3079 _mesa_lock_texture(ctx
, texObj
);
3081 if (texObj
->Immutable
) {
3082 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3083 "glEGLImageTargetTexture2D(texture is immutable)");
3084 _mesa_unlock_texture(ctx
, texObj
);
3088 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
3090 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glEGLImageTargetTexture2D");
3092 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3094 ctx
->Driver
.EGLImageTargetTexture2D(ctx
, target
,
3095 texObj
, texImage
, image
);
3097 _mesa_dirty_texobj(ctx
, texObj
);
3099 _mesa_unlock_texture(ctx
, texObj
);
3104 * Helper that implements the glTexSubImage1/2/3D()
3105 * and glTextureSubImage1/2/3D() functions.
3108 _mesa_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
3109 struct gl_texture_object
*texObj
,
3110 struct gl_texture_image
*texImage
,
3111 GLenum target
, GLint level
,
3112 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3113 GLsizei width
, GLsizei height
, GLsizei depth
,
3114 GLenum format
, GLenum type
, const GLvoid
*pixels
,
3117 FLUSH_VERTICES(ctx
, 0);
3119 if (ctx
->NewState
& _NEW_PIXEL
)
3120 _mesa_update_state(ctx
);
3122 _mesa_lock_texture(ctx
, texObj
);
3124 if (width
> 0 && height
> 0 && depth
> 0) {
3125 /* If we have a border, offset=-1 is legal. Bias by border width. */
3128 if (target
!= GL_TEXTURE_2D_ARRAY
)
3129 zoffset
+= texImage
->Border
;
3132 if (target
!= GL_TEXTURE_1D_ARRAY
)
3133 yoffset
+= texImage
->Border
;
3136 xoffset
+= texImage
->Border
;
3139 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3140 xoffset
, yoffset
, zoffset
,
3141 width
, height
, depth
,
3142 format
, type
, pixels
, &ctx
->Unpack
);
3144 check_gen_mipmap(ctx
, target
, texObj
, level
);
3146 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3147 * the texel data, not the texture format, size, etc.
3151 _mesa_unlock_texture(ctx
, texObj
);
3155 * Implement all the glTexSubImage1/2/3D() functions.
3156 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3159 texsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
3160 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3161 GLsizei width
, GLsizei height
, GLsizei depth
,
3162 GLenum format
, GLenum type
, const GLvoid
*pixels
,
3163 const char *callerName
)
3165 struct gl_texture_object
*texObj
;
3166 struct gl_texture_image
*texImage
;
3168 /* check target (proxies not allowed) */
3169 if (!legal_texsubimage_target(ctx
, dims
, target
, false)) {
3170 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage%uD(target=%s)",
3171 dims
, _mesa_enum_to_string(target
));
3175 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3179 if (texsubimage_error_check(ctx
, dims
, texObj
, target
, level
,
3180 xoffset
, yoffset
, zoffset
,
3181 width
, height
, depth
, format
, type
,
3182 pixels
, false, callerName
)) {
3183 return; /* error was detected */
3186 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
3187 /* texsubimage_error_check ensures that texImage is not NULL */
3189 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3190 _mesa_debug(ctx
, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3192 _mesa_enum_to_string(target
), level
,
3193 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3194 _mesa_enum_to_string(format
),
3195 _mesa_enum_to_string(type
), pixels
);
3197 _mesa_texture_sub_image(ctx
, dims
, texObj
, texImage
, target
, level
,
3198 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3199 format
, type
, pixels
, false);
3204 * Implement all the glTextureSubImage1/2/3D() functions.
3205 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3208 texturesubimage(struct gl_context
*ctx
, GLuint dims
,
3209 GLuint texture
, GLint level
,
3210 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3211 GLsizei width
, GLsizei height
, GLsizei depth
,
3212 GLenum format
, GLenum type
, const GLvoid
*pixels
,
3213 const char *callerName
)
3215 struct gl_texture_object
*texObj
;
3216 struct gl_texture_image
*texImage
;
3219 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3221 "glTextureSubImage%uD %d %d %d %d %d %d %d %d %s %s %p\n",
3222 dims
, texture
, level
,
3223 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3224 _mesa_enum_to_string(format
),
3225 _mesa_enum_to_string(type
), pixels
);
3227 /* Get the texture object by Name. */
3228 texObj
= _mesa_lookup_texture(ctx
, texture
);
3230 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTextureSubImage%uD(texture)",
3235 /* check target (proxies not allowed) */
3236 if (!legal_texsubimage_target(ctx
, dims
, texObj
->Target
, true)) {
3237 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target=%s)",
3238 callerName
, _mesa_enum_to_string(texObj
->Target
));
3242 if (texsubimage_error_check(ctx
, dims
, texObj
, texObj
->Target
, level
,
3243 xoffset
, yoffset
, zoffset
,
3244 width
, height
, depth
, format
, type
,
3245 pixels
, true, callerName
)) {
3246 return; /* error was detected */
3250 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
3251 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
3255 * What do we do if the user created a texture with the following code
3256 * and then called this function with its handle?
3259 * glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &tex);
3260 * glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
3261 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, ...);
3262 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, ...);
3263 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, ...);
3264 * // Note: GL_TEXTURE_CUBE_MAP_NEGATIVE_Y not set, or given the
3265 * // wrong format, or given the wrong size, etc.
3266 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, ...);
3267 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, ...);
3269 * A bug has been filed against the spec for this case. In the
3270 * meantime, we will check for cube completeness.
3272 * According to Section 8.17 Texture Completeness in the OpenGL 4.5
3273 * Core Profile spec (30.10.2014):
3274 * "[A] cube map texture is cube complete if the
3275 * following conditions all hold true: The [base level] texture
3276 * images of each of the six cube map faces have identical, positive,
3277 * and square dimensions. The [base level] images were each specified
3278 * with the same internal format."
3280 * It seems reasonable to check for cube completeness of an arbitrary
3281 * level here so that the image data has a consistent format and size.
3283 if (!_mesa_cube_level_complete(texObj
, level
)) {
3284 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3285 "glTextureSubImage%uD(cube map incomplete)",
3290 imageStride
= _mesa_image_image_stride(&ctx
->Unpack
, width
, height
,
3292 /* Copy in each face. */
3293 for (i
= zoffset
; i
< zoffset
+ depth
; ++i
) {
3294 texImage
= texObj
->Image
[i
][level
];
3297 _mesa_texture_sub_image(ctx
, 3, texObj
, texImage
, texObj
->Target
,
3298 level
, xoffset
, yoffset
, 0,
3299 width
, height
, 1, format
,
3300 type
, pixels
, true);
3301 pixels
= (GLubyte
*) pixels
+ imageStride
;
3305 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
3308 _mesa_texture_sub_image(ctx
, dims
, texObj
, texImage
, texObj
->Target
,
3309 level
, xoffset
, yoffset
, zoffset
,
3310 width
, height
, depth
, format
,
3311 type
, pixels
, true);
3317 _mesa_TexSubImage1D( GLenum target
, GLint level
,
3318 GLint xoffset
, GLsizei width
,
3319 GLenum format
, GLenum type
,
3320 const GLvoid
*pixels
)
3322 GET_CURRENT_CONTEXT(ctx
);
3323 texsubimage(ctx
, 1, target
, level
,
3326 format
, type
, pixels
, "glTexSubImage1D");
3331 _mesa_TexSubImage2D( GLenum target
, GLint level
,
3332 GLint xoffset
, GLint yoffset
,
3333 GLsizei width
, GLsizei height
,
3334 GLenum format
, GLenum type
,
3335 const GLvoid
*pixels
)
3337 GET_CURRENT_CONTEXT(ctx
);
3338 texsubimage(ctx
, 2, target
, level
,
3339 xoffset
, yoffset
, 0,
3341 format
, type
, pixels
, "glTexSubImage2D");
3347 _mesa_TexSubImage3D( GLenum target
, GLint level
,
3348 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3349 GLsizei width
, GLsizei height
, GLsizei depth
,
3350 GLenum format
, GLenum type
,
3351 const GLvoid
*pixels
)
3353 GET_CURRENT_CONTEXT(ctx
);
3354 texsubimage(ctx
, 3, target
, level
,
3355 xoffset
, yoffset
, zoffset
,
3356 width
, height
, depth
,
3357 format
, type
, pixels
, "glTexSubImage3D");
3361 _mesa_TextureSubImage1D(GLuint texture
, GLint level
,
3362 GLint xoffset
, GLsizei width
,
3363 GLenum format
, GLenum type
,
3364 const GLvoid
*pixels
)
3366 GET_CURRENT_CONTEXT(ctx
);
3367 texturesubimage(ctx
, 1, texture
, level
,
3370 format
, type
, pixels
, "glTextureSubImage1D");
3375 _mesa_TextureSubImage2D(GLuint texture
, GLint level
,
3376 GLint xoffset
, GLint yoffset
,
3377 GLsizei width
, GLsizei height
,
3378 GLenum format
, GLenum type
,
3379 const GLvoid
*pixels
)
3381 GET_CURRENT_CONTEXT(ctx
);
3382 texturesubimage(ctx
, 2, texture
, level
,
3383 xoffset
, yoffset
, 0,
3385 format
, type
, pixels
, "glTextureSubImage2D");
3390 _mesa_TextureSubImage3D(GLuint texture
, GLint level
,
3391 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3392 GLsizei width
, GLsizei height
, GLsizei depth
,
3393 GLenum format
, GLenum type
,
3394 const GLvoid
*pixels
)
3396 GET_CURRENT_CONTEXT(ctx
);
3397 texturesubimage(ctx
, 3, texture
, level
,
3398 xoffset
, yoffset
, zoffset
,
3399 width
, height
, depth
,
3400 format
, type
, pixels
, "glTextureSubImage3D");
3405 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3406 * from. This depends on whether the texture contains color or depth values.
3408 static struct gl_renderbuffer
*
3409 get_copy_tex_image_source(struct gl_context
*ctx
, mesa_format texFormat
)
3411 if (_mesa_get_format_bits(texFormat
, GL_DEPTH_BITS
) > 0) {
3412 /* reading from depth/stencil buffer */
3413 return ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
3416 /* copying from color buffer */
3417 return ctx
->ReadBuffer
->_ColorReadBuffer
;
3422 copytexsubimage_by_slice(struct gl_context
*ctx
,
3423 struct gl_texture_image
*texImage
,
3425 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3426 struct gl_renderbuffer
*rb
,
3428 GLsizei width
, GLsizei height
)
3430 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
3433 /* For 1D arrays, we copy each scanline of the source rectangle into the
3436 assert(zoffset
== 0);
3438 for (slice
= 0; slice
< height
; slice
++) {
3439 assert(yoffset
+ slice
< texImage
->Height
);
3440 ctx
->Driver
.CopyTexSubImage(ctx
, 2, texImage
,
3441 xoffset
, 0, yoffset
+ slice
,
3442 rb
, x
, y
+ slice
, width
, 1);
3445 ctx
->Driver
.CopyTexSubImage(ctx
, dims
, texImage
,
3446 xoffset
, yoffset
, zoffset
,
3447 rb
, x
, y
, width
, height
);
3452 formats_differ_in_component_sizes(mesa_format f1
, mesa_format f2
)
3454 GLint f1_r_bits
= _mesa_get_format_bits(f1
, GL_RED_BITS
);
3455 GLint f1_g_bits
= _mesa_get_format_bits(f1
, GL_GREEN_BITS
);
3456 GLint f1_b_bits
= _mesa_get_format_bits(f1
, GL_BLUE_BITS
);
3457 GLint f1_a_bits
= _mesa_get_format_bits(f1
, GL_ALPHA_BITS
);
3459 GLint f2_r_bits
= _mesa_get_format_bits(f2
, GL_RED_BITS
);
3460 GLint f2_g_bits
= _mesa_get_format_bits(f2
, GL_GREEN_BITS
);
3461 GLint f2_b_bits
= _mesa_get_format_bits(f2
, GL_BLUE_BITS
);
3462 GLint f2_a_bits
= _mesa_get_format_bits(f2
, GL_ALPHA_BITS
);
3464 if ((f1_r_bits
&& f2_r_bits
&& f1_r_bits
!= f2_r_bits
)
3465 || (f1_g_bits
&& f2_g_bits
&& f1_g_bits
!= f2_g_bits
)
3466 || (f1_b_bits
&& f2_b_bits
&& f1_b_bits
!= f2_b_bits
)
3467 || (f1_a_bits
&& f2_a_bits
&& f1_a_bits
!= f2_a_bits
))
3474 * Implement the glCopyTexImage1/2D() functions.
3477 copyteximage(struct gl_context
*ctx
, GLuint dims
,
3478 GLenum target
, GLint level
, GLenum internalFormat
,
3479 GLint x
, GLint y
, GLsizei width
, GLsizei height
, GLint border
)
3481 struct gl_texture_object
*texObj
;
3482 struct gl_texture_image
*texImage
;
3483 const GLuint face
= _mesa_tex_target_to_face(target
);
3484 mesa_format texFormat
;
3485 struct gl_renderbuffer
*rb
;
3487 FLUSH_VERTICES(ctx
, 0);
3489 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3490 _mesa_debug(ctx
, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3492 _mesa_enum_to_string(target
), level
,
3493 _mesa_enum_to_string(internalFormat
),
3494 x
, y
, width
, height
, border
);
3496 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3497 _mesa_update_state(ctx
);
3499 if (copytexture_error_check(ctx
, dims
, target
, level
, internalFormat
,
3500 width
, height
, border
))
3503 if (!_mesa_legal_texture_dimensions(ctx
, target
, level
, width
, height
,
3505 _mesa_error(ctx
, GL_INVALID_VALUE
,
3506 "glCopyTexImage%uD(invalid width or height)", dims
);
3510 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3513 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3514 internalFormat
, GL_NONE
, GL_NONE
);
3516 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, internalFormat
);
3518 if (_mesa_is_gles3(ctx
)) {
3519 if (_mesa_is_enum_format_unsized(internalFormat
)) {
3520 /* Conversion from GL_RGB10_A2 source buffer format is not allowed in
3521 * OpenGL ES 3.0. Khronos bug# 9807.
3523 if (rb
->InternalFormat
== GL_RGB10_A2
) {
3524 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3525 "glCopyTexImage%uD(Reading from GL_RGB10_A2 buffer"
3526 " and writing to unsized internal format)", dims
);
3530 /* From Page 139 of OpenGL ES 3.0 spec:
3531 * "If internalformat is sized, the internal format of the new texel
3532 * array is internalformat, and this is also the new texel array’s
3533 * effective internal format. If the component sizes of internalformat
3534 * do not exactly match the corresponding component sizes of the source
3535 * buffer’s effective internal format, described below, an
3536 * INVALID_OPERATION error is generated. If internalformat is unsized,
3537 * the internal format of the new texel array is the effective internal
3538 * format of the source buffer, and this is also the new texel array’s
3539 * effective internal format.
3541 else if (formats_differ_in_component_sizes (texFormat
, rb
->Format
)) {
3542 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3543 "glCopyTexImage%uD(componenet size changed in"
3544 " internal format)", dims
);
3549 assert(texFormat
!= MESA_FORMAT_NONE
);
3551 if (!ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target(target
),
3553 width
, height
, 1, border
)) {
3554 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3555 "glCopyTexImage%uD(image too large)", dims
);
3559 if (border
&& ctx
->Const
.StripTextureBorder
) {
3561 width
-= border
* 2;
3564 height
-= border
* 2;
3569 _mesa_lock_texture(ctx
, texObj
);
3571 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3574 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
3577 GLint srcX
= x
, srcY
= y
, dstX
= 0, dstY
= 0, dstZ
= 0;
3579 /* Free old texture image */
3580 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3582 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, 1,
3583 border
, internalFormat
, texFormat
);
3585 if (width
&& height
) {
3586 /* Allocate texture memory (no pixel data yet) */
3587 ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
);
3589 if (_mesa_clip_copytexsubimage(ctx
, &dstX
, &dstY
, &srcX
, &srcY
,
3591 struct gl_renderbuffer
*srcRb
=
3592 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3594 copytexsubimage_by_slice(ctx
, texImage
, dims
,
3596 srcRb
, srcX
, srcY
, width
, height
);
3599 check_gen_mipmap(ctx
, target
, texObj
, level
);
3602 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3604 _mesa_dirty_texobj(ctx
, texObj
);
3607 _mesa_unlock_texture(ctx
, texObj
);
3613 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
3614 GLenum internalFormat
,
3616 GLsizei width
, GLint border
)
3618 GET_CURRENT_CONTEXT(ctx
);
3619 copyteximage(ctx
, 1, target
, level
, internalFormat
, x
, y
, width
, 1, border
);
3625 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
3626 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
3629 GET_CURRENT_CONTEXT(ctx
);
3630 copyteximage(ctx
, 2, target
, level
, internalFormat
,
3631 x
, y
, width
, height
, border
);
3635 * Implementation for glCopyTex(ture)SubImage1/2/3D() functions.
3638 _mesa_copy_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
3639 struct gl_texture_object
*texObj
,
3640 GLenum target
, GLint level
,
3641 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3643 GLsizei width
, GLsizei height
,
3646 struct gl_texture_image
*texImage
;
3648 FLUSH_VERTICES(ctx
, 0);
3650 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3651 _mesa_debug(ctx
, "%s %s %d %d %d %d %d %d %d %d\n", caller
,
3652 _mesa_enum_to_string(target
),
3653 level
, xoffset
, yoffset
, zoffset
, x
, y
, width
, height
);
3655 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3656 _mesa_update_state(ctx
);
3658 if (copytexsubimage_error_check(ctx
, dims
, texObj
, target
, level
,
3659 xoffset
, yoffset
, zoffset
,
3660 width
, height
, caller
)) {
3664 _mesa_lock_texture(ctx
, texObj
);
3666 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
3668 /* If we have a border, offset=-1 is legal. Bias by border width. */
3671 if (target
!= GL_TEXTURE_2D_ARRAY
)
3672 zoffset
+= texImage
->Border
;
3675 if (target
!= GL_TEXTURE_1D_ARRAY
)
3676 yoffset
+= texImage
->Border
;
3679 xoffset
+= texImage
->Border
;
3682 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3684 struct gl_renderbuffer
*srcRb
=
3685 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3687 copytexsubimage_by_slice(ctx
, texImage
, dims
,
3688 xoffset
, yoffset
, zoffset
,
3689 srcRb
, x
, y
, width
, height
);
3691 check_gen_mipmap(ctx
, target
, texObj
, level
);
3693 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3694 * the texel data, not the texture format, size, etc.
3698 _mesa_unlock_texture(ctx
, texObj
);
3702 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
3703 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
3705 struct gl_texture_object
* texObj
;
3706 const char *self
= "glCopyTexSubImage1D";
3707 GET_CURRENT_CONTEXT(ctx
);
3709 /* Check target (proxies not allowed). Target must be checked prior to
3710 * calling _mesa_get_current_tex_object.
3712 if (!legal_texsubimage_target(ctx
, 1, target
, false)) {
3713 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3714 _mesa_enum_to_string(target
));
3718 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3722 _mesa_copy_texture_sub_image(ctx
, 1, texObj
, target
, level
, xoffset
, 0, 0,
3723 x
, y
, width
, 1, self
);
3729 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
3730 GLint xoffset
, GLint yoffset
,
3731 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3733 struct gl_texture_object
* texObj
;
3734 const char *self
= "glCopyTexSubImage2D";
3735 GET_CURRENT_CONTEXT(ctx
);
3737 /* Check target (proxies not allowed). Target must be checked prior to
3738 * calling _mesa_get_current_tex_object.
3740 if (!legal_texsubimage_target(ctx
, 2, target
, false)) {
3741 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3742 _mesa_enum_to_string(target
));
3746 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3750 _mesa_copy_texture_sub_image(ctx
, 2, texObj
, target
, level
,
3751 xoffset
, yoffset
, 0,
3752 x
, y
, width
, height
, self
);
3758 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
3759 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3760 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3762 struct gl_texture_object
* texObj
;
3763 const char *self
= "glCopyTexSubImage3D";
3764 GET_CURRENT_CONTEXT(ctx
);
3766 /* Check target (proxies not allowed). Target must be checked prior to
3767 * calling _mesa_get_current_tex_object.
3769 if (!legal_texsubimage_target(ctx
, 3, target
, false)) {
3770 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3771 _mesa_enum_to_string(target
));
3775 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3779 _mesa_copy_texture_sub_image(ctx
, 3, texObj
, target
, level
,
3780 xoffset
, yoffset
, zoffset
,
3781 x
, y
, width
, height
, self
);
3785 _mesa_CopyTextureSubImage1D(GLuint texture
, GLint level
,
3786 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
3788 struct gl_texture_object
* texObj
;
3789 const char *self
= "glCopyTextureSubImage1D";
3790 GET_CURRENT_CONTEXT(ctx
);
3792 texObj
= _mesa_lookup_texture_err(ctx
, texture
, self
);
3796 /* Check target (proxies not allowed). */
3797 if (!legal_texsubimage_target(ctx
, 1, texObj
->Target
, true)) {
3798 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3799 _mesa_enum_to_string(texObj
->Target
));
3803 _mesa_copy_texture_sub_image(ctx
, 1, texObj
, texObj
->Target
, level
,
3804 xoffset
, 0, 0, x
, y
, width
, 1, self
);
3808 _mesa_CopyTextureSubImage2D(GLuint texture
, GLint level
,
3809 GLint xoffset
, GLint yoffset
,
3810 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3812 struct gl_texture_object
* texObj
;
3813 const char *self
= "glCopyTextureSubImage2D";
3814 GET_CURRENT_CONTEXT(ctx
);
3816 texObj
= _mesa_lookup_texture_err(ctx
, texture
, self
);
3820 /* Check target (proxies not allowed). */
3821 if (!legal_texsubimage_target(ctx
, 2, texObj
->Target
, true)) {
3822 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3823 _mesa_enum_to_string(texObj
->Target
));
3827 _mesa_copy_texture_sub_image(ctx
, 2, texObj
, texObj
->Target
, level
,
3828 xoffset
, yoffset
, 0,
3829 x
, y
, width
, height
, self
);
3835 _mesa_CopyTextureSubImage3D(GLuint texture
, GLint level
,
3836 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3837 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3839 struct gl_texture_object
* texObj
;
3840 const char *self
= "glCopyTextureSubImage3D";
3841 GET_CURRENT_CONTEXT(ctx
);
3843 texObj
= _mesa_lookup_texture_err(ctx
, texture
, self
);
3847 /* Check target (proxies not allowed). */
3848 if (!legal_texsubimage_target(ctx
, 3, texObj
->Target
, true)) {
3849 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3850 _mesa_enum_to_string(texObj
->Target
));
3854 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
3855 /* Act like CopyTexSubImage2D */
3856 _mesa_copy_texture_sub_image(ctx
, 2, texObj
,
3857 GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ zoffset
,
3858 level
, xoffset
, yoffset
, 0,
3859 x
, y
, width
, height
, self
);
3862 _mesa_copy_texture_sub_image(ctx
, 3, texObj
, texObj
->Target
, level
,
3863 xoffset
, yoffset
, zoffset
,
3864 x
, y
, width
, height
, self
);
3868 check_clear_tex_image(struct gl_context
*ctx
,
3869 const char *function
,
3870 struct gl_texture_image
*texImage
,
3871 GLenum format
, GLenum type
,
3873 GLubyte
*clearValue
)
3875 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3876 static const GLubyte zeroData
[MAX_PIXEL_BYTES
];
3877 GLenum internalFormat
= texImage
->InternalFormat
;
3880 if (texObj
->Target
== GL_TEXTURE_BUFFER
) {
3881 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3882 "%s(buffer texture)", function
);
3886 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
3887 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3888 "%s(compressed texture)", function
);
3892 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
3893 if (err
!= GL_NO_ERROR
) {
3894 _mesa_error(ctx
, err
,
3895 "%s(incompatible format = %s, type = %s)",
3897 _mesa_enum_to_string(format
),
3898 _mesa_enum_to_string(type
));
3902 /* make sure internal format and format basically agree */
3903 if (!texture_formats_agree(internalFormat
, format
)) {
3904 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3905 "%s(incompatible internalFormat = %s, format = %s)",
3907 _mesa_enum_to_string(internalFormat
),
3908 _mesa_enum_to_string(format
));
3912 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
3913 /* both source and dest must be integer-valued, or neither */
3914 if (_mesa_is_format_integer_color(texImage
->TexFormat
) !=
3915 _mesa_is_enum_format_integer(format
)) {
3916 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3917 "%s(integer/non-integer format mismatch)",
3923 if (!_mesa_texstore(ctx
,
3925 texImage
->_BaseFormat
,
3926 texImage
->TexFormat
,
3927 0, /* dstRowStride */
3929 1, 1, 1, /* srcWidth/Height/Depth */
3931 data
? data
: zeroData
,
3932 &ctx
->DefaultPacking
)) {
3933 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid format)", function
);
3940 static struct gl_texture_object
*
3941 get_tex_obj_for_clear(struct gl_context
*ctx
,
3942 const char *function
,
3945 struct gl_texture_object
*texObj
;
3948 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(zero texture)", function
);
3952 texObj
= _mesa_lookup_texture(ctx
, texture
);
3954 if (texObj
== NULL
) {
3955 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", function
);
3959 if (texObj
->Target
== 0) {
3960 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unbound tex)", function
);
3968 get_tex_images_for_clear(struct gl_context
*ctx
,
3969 const char *function
,
3970 struct gl_texture_object
*texObj
,
3972 struct gl_texture_image
**texImages
)
3977 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
3978 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid level)", function
);
3982 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
3983 for (i
= 0; i
< MAX_FACES
; i
++) {
3984 target
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ i
;
3986 texImages
[i
] = _mesa_select_tex_image(texObj
, target
, level
);
3987 if (texImages
[i
] == NULL
) {
3988 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3989 "%s(invalid level)", function
);
3997 texImages
[0] = _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
3999 if (texImages
[0] == NULL
) {
4000 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid level)", function
);
4008 _mesa_ClearTexSubImage( GLuint texture
, GLint level
,
4009 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4010 GLsizei width
, GLsizei height
, GLsizei depth
,
4011 GLenum format
, GLenum type
, const void *data
)
4013 GET_CURRENT_CONTEXT(ctx
);
4014 struct gl_texture_object
*texObj
;
4015 struct gl_texture_image
*texImages
[MAX_FACES
];
4016 GLubyte clearValue
[MAX_FACES
][MAX_PIXEL_BYTES
];
4018 int minDepth
, maxDepth
;
4020 texObj
= get_tex_obj_for_clear(ctx
, "glClearTexSubImage", texture
);
4025 _mesa_lock_texture(ctx
, texObj
);
4027 numImages
= get_tex_images_for_clear(ctx
, "glClearTexSubImage",
4028 texObj
, level
, texImages
);
4032 if (numImages
== 1) {
4033 minDepth
= -(int) texImages
[0]->Border
;
4034 maxDepth
= texImages
[0]->Depth
;
4037 maxDepth
= numImages
;
4040 if (xoffset
< -(GLint
) texImages
[0]->Border
||
4041 yoffset
< -(GLint
) texImages
[0]->Border
||
4042 zoffset
< minDepth
||
4046 xoffset
+ width
> texImages
[0]->Width
||
4047 yoffset
+ height
> texImages
[0]->Height
||
4048 zoffset
+ depth
> maxDepth
) {
4049 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4050 "glClearSubTexImage(invalid dimensions)");
4054 if (numImages
== 1) {
4055 if (check_clear_tex_image(ctx
, "glClearTexSubImage",
4057 format
, type
, data
, clearValue
[0])) {
4058 ctx
->Driver
.ClearTexSubImage(ctx
,
4060 xoffset
, yoffset
, zoffset
,
4061 width
, height
, depth
,
4062 data
? clearValue
[0] : NULL
);
4065 for (i
= zoffset
; i
< zoffset
+ depth
; i
++) {
4066 if (!check_clear_tex_image(ctx
, "glClearTexSubImage",
4068 format
, type
, data
, clearValue
[i
]))
4071 for (i
= zoffset
; i
< zoffset
+ depth
; i
++) {
4072 ctx
->Driver
.ClearTexSubImage(ctx
,
4074 xoffset
, yoffset
, 0,
4076 data
? clearValue
[i
] : NULL
);
4081 _mesa_unlock_texture(ctx
, texObj
);
4085 _mesa_ClearTexImage( GLuint texture
, GLint level
,
4086 GLenum format
, GLenum type
, const void *data
)
4088 GET_CURRENT_CONTEXT(ctx
);
4089 struct gl_texture_object
*texObj
;
4090 struct gl_texture_image
*texImages
[MAX_FACES
];
4091 GLubyte clearValue
[MAX_FACES
][MAX_PIXEL_BYTES
];
4094 texObj
= get_tex_obj_for_clear(ctx
, "glClearTexImage", texture
);
4099 _mesa_lock_texture(ctx
, texObj
);
4101 numImages
= get_tex_images_for_clear(ctx
, "glClearTexImage",
4102 texObj
, level
, texImages
);
4104 for (i
= 0; i
< numImages
; i
++) {
4105 if (!check_clear_tex_image(ctx
, "glClearTexImage",
4112 for (i
= 0; i
< numImages
; i
++) {
4113 ctx
->Driver
.ClearTexSubImage(ctx
, texImages
[i
],
4114 -(GLint
) texImages
[i
]->Border
, /* xoffset */
4115 -(GLint
) texImages
[i
]->Border
, /* yoffset */
4116 -(GLint
) texImages
[i
]->Border
, /* zoffset */
4117 texImages
[i
]->Width
,
4118 texImages
[i
]->Height
,
4119 texImages
[i
]->Depth
,
4120 data
? clearValue
[i
] : NULL
);
4124 _mesa_unlock_texture(ctx
, texObj
);
4130 /**********************************************************************/
4131 /****** Compressed Textures ******/
4132 /**********************************************************************/
4136 * Target checking for glCompressedTexSubImage[123]D().
4137 * \return GL_TRUE if error, GL_FALSE if no error
4138 * Must come before other error checking so that the texture object can
4139 * be correctly retrieved using _mesa_get_current_tex_object.
4142 compressed_subtexture_target_check(struct gl_context
*ctx
, GLenum target
,
4143 GLint dims
, GLenum format
, bool dsa
,
4148 if (dsa
&& target
== GL_TEXTURE_RECTANGLE
) {
4149 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid target %s)", caller
,
4150 _mesa_enum_to_string(target
));
4160 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
4161 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
4162 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
4163 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
4164 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
4165 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
4166 targetOK
= ctx
->Extensions
.ARB_texture_cube_map
;
4169 targetOK
= GL_FALSE
;
4175 case GL_TEXTURE_CUBE_MAP
:
4176 targetOK
= dsa
&& ctx
->Extensions
.ARB_texture_cube_map
;
4178 case GL_TEXTURE_2D_ARRAY
:
4179 targetOK
= _mesa_is_gles3(ctx
) ||
4180 (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
);
4182 case GL_TEXTURE_CUBE_MAP_ARRAY
:
4183 targetOK
= ctx
->Extensions
.ARB_texture_cube_map_array
;
4188 * OpenGL 4.5 spec (30.10.2014) says in Section 8.7 Compressed Texture
4190 * "An INVALID_OPERATION error is generated by
4191 * CompressedTex*SubImage3D if the internal format of the texture
4192 * is one of the EAC, ETC2, or RGTC formats and either border is
4193 * non-zero, or the effective target for the texture is not
4194 * TEXTURE_2D_ARRAY."
4196 * NOTE: that's probably a spec error. It should probably say
4197 * "... or the effective target for the texture is not
4198 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP, nor
4199 * GL_TEXTURE_CUBE_MAP_ARRAY."
4200 * since those targets are 2D images and they support all compression
4203 * Instead of listing all these, just list those which are allowed,
4204 * which is (at this time) only bptc. Otherwise we'd say s3tc (and
4205 * more) are valid here, which they are not, but of course not
4206 * mentioned by core spec.
4209 /* These are the only 3D compression formats supported at this time */
4210 case GL_COMPRESSED_RGBA_BPTC_UNORM
:
4211 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM
:
4212 case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT
:
4213 case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT
:
4217 /* invalid format */
4218 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4219 "%s(invalid target %s for format %s)", caller
,
4220 _mesa_enum_to_string(target
),
4221 _mesa_enum_to_string(format
));
4226 targetOK
= GL_FALSE
;
4232 /* no 1D compressed textures at this time */
4233 targetOK
= GL_FALSE
;
4238 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", caller
,
4239 _mesa_enum_to_string(target
));
4247 * Error checking for glCompressedTexSubImage[123]D().
4248 * \return GL_TRUE if error, GL_FALSE if no error
4251 compressed_subtexture_error_check(struct gl_context
*ctx
, GLint dims
,
4252 const struct gl_texture_object
*texObj
,
4253 GLenum target
, GLint level
,
4254 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4255 GLsizei width
, GLsizei height
, GLsizei depth
,
4256 GLenum format
, GLsizei imageSize
,
4257 const GLvoid
*data
, const char *callerName
)
4259 struct gl_texture_image
*texImage
;
4262 /* this will catch any invalid compressed format token */
4263 if (!_mesa_is_compressed_format(ctx
, format
)) {
4264 _mesa_error(ctx
, GL_INVALID_ENUM
,
4265 "%s(format)", callerName
);
4269 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
4270 _mesa_error(ctx
, GL_INVALID_VALUE
,
4276 /* validate the bound PBO, if any */
4277 if (!_mesa_validate_pbo_source_compressed(ctx
, dims
, &ctx
->Unpack
,
4278 imageSize
, data
, callerName
)) {
4282 /* Check for invalid pixel storage modes */
4283 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dims
,
4284 &ctx
->Unpack
, callerName
)) {
4288 expectedSize
= compressed_tex_size(width
, height
, depth
, format
);
4289 if (expectedSize
!= imageSize
) {
4290 _mesa_error(ctx
, GL_INVALID_VALUE
,
4292 callerName
, imageSize
);
4296 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4298 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4299 "%s(invalid texture image)",
4304 if ((GLint
) format
!= texImage
->InternalFormat
) {
4305 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4307 callerName
, _mesa_enum_to_string(format
));
4311 if (compressedteximage_only_format(ctx
, format
)) {
4312 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4313 "%s(format=%s cannot be updated)",
4314 callerName
, _mesa_enum_to_string(format
));
4318 if (error_check_subtexture_dimensions(ctx
, dims
,
4319 texImage
, xoffset
, yoffset
, zoffset
,
4320 width
, height
, depth
,
4330 _mesa_CompressedTexImage1D(GLenum target
, GLint level
,
4331 GLenum internalFormat
, GLsizei width
,
4332 GLint border
, GLsizei imageSize
,
4335 GET_CURRENT_CONTEXT(ctx
);
4336 teximage(ctx
, GL_TRUE
, 1, target
, level
, internalFormat
,
4337 width
, 1, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4342 _mesa_CompressedTexImage2D(GLenum target
, GLint level
,
4343 GLenum internalFormat
, GLsizei width
,
4344 GLsizei height
, GLint border
, GLsizei imageSize
,
4347 GET_CURRENT_CONTEXT(ctx
);
4348 teximage(ctx
, GL_TRUE
, 2, target
, level
, internalFormat
,
4349 width
, height
, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4354 _mesa_CompressedTexImage3D(GLenum target
, GLint level
,
4355 GLenum internalFormat
, GLsizei width
,
4356 GLsizei height
, GLsizei depth
, GLint border
,
4357 GLsizei imageSize
, const GLvoid
*data
)
4359 GET_CURRENT_CONTEXT(ctx
);
4360 teximage(ctx
, GL_TRUE
, 3, target
, level
, internalFormat
,
4361 width
, height
, depth
, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4366 * Common helper for glCompressedTexSubImage1/2/3D() and
4367 * glCompressedTextureSubImage1/2/3D().
4370 _mesa_compressed_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
4371 struct gl_texture_object
*texObj
,
4372 struct gl_texture_image
*texImage
,
4373 GLenum target
, GLint level
,
4374 GLint xoffset
, GLint yoffset
,
4376 GLsizei width
, GLsizei height
,
4378 GLenum format
, GLsizei imageSize
,
4381 FLUSH_VERTICES(ctx
, 0);
4383 _mesa_lock_texture(ctx
, texObj
);
4385 if (width
> 0 && height
> 0 && depth
> 0) {
4386 ctx
->Driver
.CompressedTexSubImage(ctx
, dims
, texImage
,
4387 xoffset
, yoffset
, zoffset
,
4388 width
, height
, depth
,
4389 format
, imageSize
, data
);
4391 check_gen_mipmap(ctx
, target
, texObj
, level
);
4393 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
4394 * the texel data, not the texture format, size, etc.
4398 _mesa_unlock_texture(ctx
, texObj
);
4403 _mesa_CompressedTexSubImage1D(GLenum target
, GLint level
, GLint xoffset
,
4404 GLsizei width
, GLenum format
,
4405 GLsizei imageSize
, const GLvoid
*data
)
4407 struct gl_texture_object
*texObj
;
4408 struct gl_texture_image
*texImage
;
4410 GET_CURRENT_CONTEXT(ctx
);
4412 if (compressed_subtexture_target_check(ctx
, target
, 1, format
, false,
4413 "glCompressedTexSubImage1D")) {
4417 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4421 if (compressed_subtexture_error_check(ctx
, 1, texObj
, target
,
4422 level
, xoffset
, 0, 0,
4424 format
, imageSize
, data
,
4425 "glCompressedTexSubImage1D")) {
4429 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4432 _mesa_compressed_texture_sub_image(ctx
, 1, texObj
, texImage
, target
, level
,
4433 xoffset
, 0, 0, width
, 1, 1,
4434 format
, imageSize
, data
);
4438 _mesa_CompressedTextureSubImage1D(GLuint texture
, GLint level
, GLint xoffset
,
4439 GLsizei width
, GLenum format
,
4440 GLsizei imageSize
, const GLvoid
*data
)
4442 struct gl_texture_object
*texObj
;
4443 struct gl_texture_image
*texImage
;
4445 GET_CURRENT_CONTEXT(ctx
);
4447 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4448 "glCompressedTextureSubImage1D");
4452 if (compressed_subtexture_target_check(ctx
, texObj
->Target
, 1, format
, true,
4453 "glCompressedTextureSubImage1D")) {
4457 if (compressed_subtexture_error_check(ctx
, 1, texObj
, texObj
->Target
,
4458 level
, xoffset
, 0, 0,
4460 format
, imageSize
, data
,
4461 "glCompressedTextureSubImage1D")) {
4465 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4468 _mesa_compressed_texture_sub_image(ctx
, 1, texObj
, texImage
,
4469 texObj
->Target
, level
,
4470 xoffset
, 0, 0, width
, 1, 1,
4471 format
, imageSize
, data
);
4476 _mesa_CompressedTexSubImage2D(GLenum target
, GLint level
, GLint xoffset
,
4477 GLint yoffset
, GLsizei width
, GLsizei height
,
4478 GLenum format
, GLsizei imageSize
,
4481 struct gl_texture_object
*texObj
;
4482 struct gl_texture_image
*texImage
;
4484 GET_CURRENT_CONTEXT(ctx
);
4486 if (compressed_subtexture_target_check(ctx
, target
, 2, format
, false,
4487 "glCompressedTexSubImage2D")) {
4491 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4495 if (compressed_subtexture_error_check(ctx
, 2, texObj
, target
,
4496 level
, xoffset
, yoffset
, 0,
4498 format
, imageSize
, data
,
4499 "glCompressedTexSubImage2D")) {
4504 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4507 _mesa_compressed_texture_sub_image(ctx
, 2, texObj
, texImage
, target
, level
,
4508 xoffset
, yoffset
, 0, width
, height
, 1,
4509 format
, imageSize
, data
);
4513 _mesa_CompressedTextureSubImage2D(GLuint texture
, GLint level
, GLint xoffset
,
4515 GLsizei width
, GLsizei height
,
4516 GLenum format
, GLsizei imageSize
,
4519 struct gl_texture_object
*texObj
;
4520 struct gl_texture_image
*texImage
;
4522 GET_CURRENT_CONTEXT(ctx
);
4524 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4525 "glCompressedTextureSubImage2D");
4529 if (compressed_subtexture_target_check(ctx
, texObj
->Target
, 2, format
, true,
4530 "glCompressedTextureSubImage2D")) {
4534 if (compressed_subtexture_error_check(ctx
, 2, texObj
, texObj
->Target
,
4535 level
, xoffset
, yoffset
, 0,
4537 format
, imageSize
, data
,
4538 "glCompressedTextureSubImage2D")) {
4542 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4545 _mesa_compressed_texture_sub_image(ctx
, 2, texObj
, texImage
,
4546 texObj
->Target
, level
,
4547 xoffset
, yoffset
, 0, width
, height
, 1,
4548 format
, imageSize
, data
);
4552 _mesa_CompressedTexSubImage3D(GLenum target
, GLint level
, GLint xoffset
,
4553 GLint yoffset
, GLint zoffset
, GLsizei width
,
4554 GLsizei height
, GLsizei depth
, GLenum format
,
4555 GLsizei imageSize
, const GLvoid
*data
)
4557 struct gl_texture_object
*texObj
;
4558 struct gl_texture_image
*texImage
;
4560 GET_CURRENT_CONTEXT(ctx
);
4562 if (compressed_subtexture_target_check(ctx
, target
, 3, format
, false,
4563 "glCompressedTexSubImage3D")) {
4567 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4571 if (compressed_subtexture_error_check(ctx
, 3, texObj
, target
,
4572 level
, xoffset
, yoffset
, zoffset
,
4573 width
, height
, depth
,
4574 format
, imageSize
, data
,
4575 "glCompressedTexSubImage3D")) {
4580 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4583 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, texImage
, target
, level
,
4584 xoffset
, yoffset
, zoffset
,
4585 width
, height
, depth
,
4586 format
, imageSize
, data
);
4590 _mesa_CompressedTextureSubImage3D(GLuint texture
, GLint level
, GLint xoffset
,
4591 GLint yoffset
, GLint zoffset
, GLsizei width
,
4592 GLsizei height
, GLsizei depth
,
4593 GLenum format
, GLsizei imageSize
,
4596 struct gl_texture_object
*texObj
;
4597 struct gl_texture_image
*texImage
;
4599 GET_CURRENT_CONTEXT(ctx
);
4601 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4602 "glCompressedTextureSubImage3D");
4606 if (compressed_subtexture_target_check(ctx
, texObj
->Target
, 3, format
, true,
4607 "glCompressedTextureSubImage3D")) {
4611 if (compressed_subtexture_error_check(ctx
, 3, texObj
, texObj
->Target
,
4612 level
, xoffset
, yoffset
, zoffset
,
4613 width
, height
, depth
,
4614 format
, imageSize
, data
,
4615 "glCompressedTextureSubImage3D")) {
4619 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
4620 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
4621 const char *pixels
= data
;
4625 /* Make sure the texture object is a proper cube.
4626 * (See texturesubimage in teximage.c for details on why this check is
4629 if (!_mesa_cube_level_complete(texObj
, level
)) {
4630 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4631 "glCompressedTextureSubImage3D(cube map incomplete)");
4635 /* Copy in each face. */
4636 for (i
= 0; i
< 6; ++i
) {
4637 texImage
= texObj
->Image
[i
][level
];
4640 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, texImage
,
4641 texObj
->Target
, level
,
4642 xoffset
, yoffset
, zoffset
,
4644 format
, imageSize
, pixels
);
4646 /* Compressed images don't have a client format */
4647 image_stride
= _mesa_format_image_size(texImage
->TexFormat
,
4649 texImage
->Height
, 1);
4651 pixels
+= image_stride
;
4652 imageSize
-= image_stride
;
4656 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4659 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, texImage
,
4660 texObj
->Target
, level
,
4661 xoffset
, yoffset
, zoffset
,
4662 width
, height
, depth
,
4663 format
, imageSize
, data
);
4668 get_texbuffer_format(const struct gl_context
*ctx
, GLenum internalFormat
)
4670 if (ctx
->API
!= API_OPENGL_CORE
) {
4671 switch (internalFormat
) {
4673 return MESA_FORMAT_A_UNORM8
;
4675 return MESA_FORMAT_A_UNORM16
;
4676 case GL_ALPHA16F_ARB
:
4677 return MESA_FORMAT_A_FLOAT16
;
4678 case GL_ALPHA32F_ARB
:
4679 return MESA_FORMAT_A_FLOAT32
;
4680 case GL_ALPHA8I_EXT
:
4681 return MESA_FORMAT_A_SINT8
;
4682 case GL_ALPHA16I_EXT
:
4683 return MESA_FORMAT_A_SINT16
;
4684 case GL_ALPHA32I_EXT
:
4685 return MESA_FORMAT_A_SINT32
;
4686 case GL_ALPHA8UI_EXT
:
4687 return MESA_FORMAT_A_UINT8
;
4688 case GL_ALPHA16UI_EXT
:
4689 return MESA_FORMAT_A_UINT16
;
4690 case GL_ALPHA32UI_EXT
:
4691 return MESA_FORMAT_A_UINT32
;
4693 return MESA_FORMAT_L_UNORM8
;
4694 case GL_LUMINANCE16
:
4695 return MESA_FORMAT_L_UNORM16
;
4696 case GL_LUMINANCE16F_ARB
:
4697 return MESA_FORMAT_L_FLOAT16
;
4698 case GL_LUMINANCE32F_ARB
:
4699 return MESA_FORMAT_L_FLOAT32
;
4700 case GL_LUMINANCE8I_EXT
:
4701 return MESA_FORMAT_L_SINT8
;
4702 case GL_LUMINANCE16I_EXT
:
4703 return MESA_FORMAT_L_SINT16
;
4704 case GL_LUMINANCE32I_EXT
:
4705 return MESA_FORMAT_L_SINT32
;
4706 case GL_LUMINANCE8UI_EXT
:
4707 return MESA_FORMAT_L_UINT8
;
4708 case GL_LUMINANCE16UI_EXT
:
4709 return MESA_FORMAT_L_UINT16
;
4710 case GL_LUMINANCE32UI_EXT
:
4711 return MESA_FORMAT_L_UINT32
;
4712 case GL_LUMINANCE8_ALPHA8
:
4713 return MESA_FORMAT_L8A8_UNORM
;
4714 case GL_LUMINANCE16_ALPHA16
:
4715 return MESA_FORMAT_L16A16_UNORM
;
4716 case GL_LUMINANCE_ALPHA16F_ARB
:
4717 return MESA_FORMAT_LA_FLOAT16
;
4718 case GL_LUMINANCE_ALPHA32F_ARB
:
4719 return MESA_FORMAT_LA_FLOAT32
;
4720 case GL_LUMINANCE_ALPHA8I_EXT
:
4721 return MESA_FORMAT_LA_SINT8
;
4722 case GL_LUMINANCE_ALPHA16I_EXT
:
4723 return MESA_FORMAT_LA_SINT16
;
4724 case GL_LUMINANCE_ALPHA32I_EXT
:
4725 return MESA_FORMAT_LA_SINT32
;
4726 case GL_LUMINANCE_ALPHA8UI_EXT
:
4727 return MESA_FORMAT_LA_UINT8
;
4728 case GL_LUMINANCE_ALPHA16UI_EXT
:
4729 return MESA_FORMAT_LA_UINT16
;
4730 case GL_LUMINANCE_ALPHA32UI_EXT
:
4731 return MESA_FORMAT_LA_UINT32
;
4733 return MESA_FORMAT_I_UNORM8
;
4734 case GL_INTENSITY16
:
4735 return MESA_FORMAT_I_UNORM16
;
4736 case GL_INTENSITY16F_ARB
:
4737 return MESA_FORMAT_I_FLOAT16
;
4738 case GL_INTENSITY32F_ARB
:
4739 return MESA_FORMAT_I_FLOAT32
;
4740 case GL_INTENSITY8I_EXT
:
4741 return MESA_FORMAT_I_SINT8
;
4742 case GL_INTENSITY16I_EXT
:
4743 return MESA_FORMAT_I_SINT16
;
4744 case GL_INTENSITY32I_EXT
:
4745 return MESA_FORMAT_I_SINT32
;
4746 case GL_INTENSITY8UI_EXT
:
4747 return MESA_FORMAT_I_UINT8
;
4748 case GL_INTENSITY16UI_EXT
:
4749 return MESA_FORMAT_I_UINT16
;
4750 case GL_INTENSITY32UI_EXT
:
4751 return MESA_FORMAT_I_UINT32
;
4757 if (ctx
->API
== API_OPENGL_CORE
&&
4758 ctx
->Extensions
.ARB_texture_buffer_object_rgb32
) {
4759 switch (internalFormat
) {
4761 return MESA_FORMAT_RGB_FLOAT32
;
4763 return MESA_FORMAT_RGB_UINT32
;
4765 return MESA_FORMAT_RGB_SINT32
;
4771 switch (internalFormat
) {
4773 return MESA_FORMAT_R8G8B8A8_UNORM
;
4775 return MESA_FORMAT_RGBA_UNORM16
;
4776 case GL_RGBA16F_ARB
:
4777 return MESA_FORMAT_RGBA_FLOAT16
;
4778 case GL_RGBA32F_ARB
:
4779 return MESA_FORMAT_RGBA_FLOAT32
;
4781 return MESA_FORMAT_RGBA_SINT8
;
4782 case GL_RGBA16I_EXT
:
4783 return MESA_FORMAT_RGBA_SINT16
;
4784 case GL_RGBA32I_EXT
:
4785 return MESA_FORMAT_RGBA_SINT32
;
4786 case GL_RGBA8UI_EXT
:
4787 return MESA_FORMAT_RGBA_UINT8
;
4788 case GL_RGBA16UI_EXT
:
4789 return MESA_FORMAT_RGBA_UINT16
;
4790 case GL_RGBA32UI_EXT
:
4791 return MESA_FORMAT_RGBA_UINT32
;
4794 return MESA_FORMAT_R8G8_UNORM
;
4796 return MESA_FORMAT_R16G16_UNORM
;
4798 return MESA_FORMAT_RG_FLOAT16
;
4800 return MESA_FORMAT_RG_FLOAT32
;
4802 return MESA_FORMAT_RG_SINT8
;
4804 return MESA_FORMAT_RG_SINT16
;
4806 return MESA_FORMAT_RG_SINT32
;
4808 return MESA_FORMAT_RG_UINT8
;
4810 return MESA_FORMAT_RG_UINT16
;
4812 return MESA_FORMAT_RG_UINT32
;
4815 return MESA_FORMAT_R_UNORM8
;
4817 return MESA_FORMAT_R_UNORM16
;
4819 return MESA_FORMAT_R_FLOAT16
;
4821 return MESA_FORMAT_R_FLOAT32
;
4823 return MESA_FORMAT_R_SINT8
;
4825 return MESA_FORMAT_R_SINT16
;
4827 return MESA_FORMAT_R_SINT32
;
4829 return MESA_FORMAT_R_UINT8
;
4831 return MESA_FORMAT_R_UINT16
;
4833 return MESA_FORMAT_R_UINT32
;
4836 return MESA_FORMAT_NONE
;
4842 _mesa_validate_texbuffer_format(const struct gl_context
*ctx
,
4843 GLenum internalFormat
)
4845 mesa_format format
= get_texbuffer_format(ctx
, internalFormat
);
4848 if (format
== MESA_FORMAT_NONE
)
4849 return MESA_FORMAT_NONE
;
4851 datatype
= _mesa_get_format_datatype(format
);
4853 /* The GL_ARB_texture_buffer_object spec says:
4855 * "If ARB_texture_float is not supported, references to the
4856 * floating-point internal formats provided by that extension should be
4857 * removed, and such formats may not be passed to TexBufferARB."
4859 * As a result, GL_HALF_FLOAT internal format depends on both
4860 * GL_ARB_texture_float and GL_ARB_half_float_pixel.
4862 if ((datatype
== GL_FLOAT
|| datatype
== GL_HALF_FLOAT
) &&
4863 !ctx
->Extensions
.ARB_texture_float
)
4864 return MESA_FORMAT_NONE
;
4866 if (!ctx
->Extensions
.ARB_texture_rg
) {
4867 GLenum base_format
= _mesa_get_format_base_format(format
);
4868 if (base_format
== GL_R
|| base_format
== GL_RG
)
4869 return MESA_FORMAT_NONE
;
4872 if (!ctx
->Extensions
.ARB_texture_buffer_object_rgb32
) {
4873 GLenum base_format
= _mesa_get_format_base_format(format
);
4874 if (base_format
== GL_RGB
)
4875 return MESA_FORMAT_NONE
;
4882 _mesa_texture_buffer_range(struct gl_context
*ctx
,
4883 struct gl_texture_object
*texObj
,
4884 GLenum internalFormat
,
4885 struct gl_buffer_object
*bufObj
,
4886 GLintptr offset
, GLsizeiptr size
,
4891 /* NOTE: ARB_texture_buffer_object has interactions with
4892 * the compatibility profile that are not implemented.
4894 if (!(ctx
->API
== API_OPENGL_CORE
&&
4895 ctx
->Extensions
.ARB_texture_buffer_object
)) {
4896 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4897 "%s(ARB_texture_buffer_object is not"
4898 " implemented for the compatibility profile)", caller
);
4902 format
= _mesa_validate_texbuffer_format(ctx
, internalFormat
);
4903 if (format
== MESA_FORMAT_NONE
) {
4904 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat %s)",
4905 caller
, _mesa_enum_to_string(internalFormat
));
4909 FLUSH_VERTICES(ctx
, 0);
4911 _mesa_lock_texture(ctx
, texObj
);
4913 _mesa_reference_buffer_object(ctx
, &texObj
->BufferObject
, bufObj
);
4914 texObj
->BufferObjectFormat
= internalFormat
;
4915 texObj
->_BufferObjectFormat
= format
;
4916 texObj
->BufferOffset
= offset
;
4917 texObj
->BufferSize
= size
;
4919 _mesa_unlock_texture(ctx
, texObj
);
4921 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTextureBuffer
;
4924 bufObj
->UsageHistory
|= USAGE_TEXTURE_BUFFER
;
4930 * Make sure the texture buffer target is GL_TEXTURE_BUFFER.
4931 * Return true if it is, and return false if it is not
4932 * (and throw INVALID ENUM as dictated in the OpenGL 4.5
4933 * core spec, 02.02.2015, PDF page 245).
4936 check_texture_buffer_target(struct gl_context
*ctx
, GLenum target
,
4939 if (target
!= GL_TEXTURE_BUFFER_ARB
) {
4940 _mesa_error(ctx
, GL_INVALID_ENUM
,
4941 "%s(texture target is not GL_TEXTURE_BUFFER)", caller
);
4949 * Check for errors related to the texture buffer range.
4950 * Return false if errors are found, true if none are found.
4953 check_texture_buffer_range(struct gl_context
*ctx
,
4954 struct gl_buffer_object
*bufObj
,
4955 GLintptr offset
, GLsizeiptr size
,
4958 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
4959 * Textures (PDF page 245):
4960 * "An INVALID_VALUE error is generated if offset is negative, if
4961 * size is less than or equal to zero, or if offset + size is greater
4962 * than the value of BUFFER_SIZE for the buffer bound to target."
4965 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset=%d < 0)", caller
,
4971 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size=%d <= 0)", caller
,
4976 if (offset
+ size
> bufObj
->Size
) {
4977 _mesa_error(ctx
, GL_INVALID_VALUE
,
4978 "%s(offset=%d + size=%d > buffer_size=%d)", caller
,
4979 (int) offset
, (int) size
, (int) bufObj
->Size
);
4983 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
4984 * Textures (PDF page 245):
4985 * "An INVALID_VALUE error is generated if offset is not an integer
4986 * multiple of the value of TEXTURE_BUFFER_OFFSET_ALIGNMENT."
4988 if (offset
% ctx
->Const
.TextureBufferOffsetAlignment
) {
4989 _mesa_error(ctx
, GL_INVALID_VALUE
,
4990 "%s(invalid offset alignment)", caller
);
4998 /** GL_ARB_texture_buffer_object */
5000 _mesa_TexBuffer(GLenum target
, GLenum internalFormat
, GLuint buffer
)
5002 struct gl_texture_object
*texObj
;
5003 struct gl_buffer_object
*bufObj
;
5005 GET_CURRENT_CONTEXT(ctx
);
5007 /* Need to catch a bad target before it gets to
5008 * _mesa_get_current_tex_object.
5010 if (!check_texture_buffer_target(ctx
, target
, "glTexBuffer"))
5014 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glTexBuffer");
5020 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5024 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
, bufObj
, 0,
5025 buffer
? -1 : 0, "glTexBuffer");
5029 /** GL_ARB_texture_buffer_range */
5031 _mesa_TexBufferRange(GLenum target
, GLenum internalFormat
, GLuint buffer
,
5032 GLintptr offset
, GLsizeiptr size
)
5034 struct gl_texture_object
*texObj
;
5035 struct gl_buffer_object
*bufObj
;
5037 GET_CURRENT_CONTEXT(ctx
);
5039 /* Need to catch a bad target before it gets to
5040 * _mesa_get_current_tex_object.
5042 if (!check_texture_buffer_target(ctx
, target
, "glTexBufferRange"))
5046 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glTexBufferRange");
5050 if (!check_texture_buffer_range(ctx
, bufObj
, offset
, size
,
5051 "glTexBufferRange"))
5055 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5056 * Textures (PDF page 254):
5057 * "If buffer is zero, then any buffer object attached to the buffer
5058 * texture is detached, the values offset and size are ignored and
5059 * the state for offset and size for the buffer texture are reset to
5067 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5071 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
, bufObj
,
5072 offset
, size
, "glTexBufferRange");
5076 _mesa_TextureBuffer(GLuint texture
, GLenum internalFormat
, GLuint buffer
)
5078 struct gl_texture_object
*texObj
;
5079 struct gl_buffer_object
*bufObj
;
5081 GET_CURRENT_CONTEXT(ctx
);
5084 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glTextureBuffer");
5090 /* Get the texture object by Name. */
5091 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glTextureBuffer");
5095 if (!check_texture_buffer_target(ctx
, texObj
->Target
, "glTextureBuffer"))
5098 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
,
5099 bufObj
, 0, buffer
? -1 : 0, "glTextureBuffer");
5103 _mesa_TextureBufferRange(GLuint texture
, GLenum internalFormat
, GLuint buffer
,
5104 GLintptr offset
, GLsizeiptr size
)
5106 struct gl_texture_object
*texObj
;
5107 struct gl_buffer_object
*bufObj
;
5109 GET_CURRENT_CONTEXT(ctx
);
5112 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
5113 "glTextureBufferRange");
5117 if (!check_texture_buffer_range(ctx
, bufObj
, offset
, size
,
5118 "glTextureBufferRange"))
5122 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5123 * Textures (PDF page 254):
5124 * "If buffer is zero, then any buffer object attached to the buffer
5125 * texture is detached, the values offset and size are ignored and
5126 * the state for offset and size for the buffer texture are reset to
5134 /* Get the texture object by Name. */
5135 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glTextureBufferRange");
5139 if (!check_texture_buffer_target(ctx
, texObj
->Target
,
5140 "glTextureBufferRange"))
5143 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
,
5144 bufObj
, offset
, size
, "glTextureBufferRange");
5148 is_renderable_texture_format(struct gl_context
*ctx
, GLenum internalformat
)
5150 /* Everything that is allowed for renderbuffers,
5151 * except for a base format of GL_STENCIL_INDEX, unless supported.
5153 GLenum baseFormat
= _mesa_base_fbo_format(ctx
, internalformat
);
5154 if (ctx
->Extensions
.ARB_texture_stencil8
)
5155 return baseFormat
!= 0;
5157 return baseFormat
!= 0 && baseFormat
!= GL_STENCIL_INDEX
;
5161 /** GL_ARB_texture_multisample */
5163 check_multisample_target(GLuint dims
, GLenum target
, bool dsa
)
5166 case GL_TEXTURE_2D_MULTISAMPLE
:
5168 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
5169 return dims
== 2 && !dsa
;
5170 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
5172 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
5173 return dims
== 3 && !dsa
;
5181 texture_image_multisample(struct gl_context
*ctx
, GLuint dims
,
5182 struct gl_texture_object
*texObj
,
5183 GLenum target
, GLsizei samples
,
5184 GLint internalformat
, GLsizei width
,
5185 GLsizei height
, GLsizei depth
,
5186 GLboolean fixedsamplelocations
,
5187 GLboolean immutable
, const char *func
)
5189 struct gl_texture_image
*texImage
;
5190 GLboolean sizeOK
, dimensionsOK
, samplesOK
;
5191 mesa_format texFormat
;
5192 GLenum sample_count_error
;
5193 bool dsa
= strstr(func
, "ture") ? true : false;
5195 if (!((ctx
->Extensions
.ARB_texture_multisample
5196 && _mesa_is_desktop_gl(ctx
))) && !_mesa_is_gles31(ctx
)) {
5197 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unsupported)", func
);
5202 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(samples < 1)", func
);
5206 if (!check_multisample_target(dims
, target
, dsa
)) {
5208 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(target)", func
);
5212 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
5217 /* check that the specified internalformat is color/depth/stencil-renderable;
5218 * refer GL3.1 spec 4.4.4
5221 if (immutable
&& !_mesa_is_legal_tex_storage_format(ctx
, internalformat
)) {
5222 _mesa_error(ctx
, GL_INVALID_ENUM
,
5223 "%s(internalformat=%s not legal for immutable-format)",
5224 func
, _mesa_enum_to_string(internalformat
));
5228 if (!is_renderable_texture_format(ctx
, internalformat
)) {
5229 /* Page 172 of OpenGL ES 3.1 spec says:
5230 * "An INVALID_ENUM error is generated if sizedinternalformat is not
5231 * color-renderable, depth-renderable, or stencil-renderable (as
5232 * defined in section 9.4).
5234 * (Same error is also defined for desktop OpenGL for multisample
5235 * teximage/texstorage functions.)
5237 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalformat=%s)", func
,
5238 _mesa_enum_to_string(internalformat
));
5242 sample_count_error
= _mesa_check_sample_count(ctx
, target
,
5243 internalformat
, samples
);
5244 samplesOK
= sample_count_error
== GL_NO_ERROR
;
5246 /* Page 254 of OpenGL 4.4 spec says:
5247 * "Proxy arrays for two-dimensional multisample and two-dimensional
5248 * multisample array textures are operated on in the same way when
5249 * TexImage2DMultisample is called with target specified as
5250 * PROXY_TEXTURE_2D_MULTISAMPLE, or TexImage3DMultisample is called
5251 * with target specified as PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY.
5252 * However, if samples is not supported, then no error is generated.
5254 if (!samplesOK
&& !_mesa_is_proxy_texture(target
)) {
5255 _mesa_error(ctx
, sample_count_error
, "%s(samples)", func
);
5259 if (immutable
&& (!texObj
|| (texObj
->Name
== 0))) {
5260 _mesa_error(ctx
, GL_INVALID_OPERATION
,
5261 "%s(texture object 0)",
5266 texImage
= _mesa_get_tex_image(ctx
, texObj
, 0, 0);
5268 if (texImage
== NULL
) {
5269 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", func
);
5273 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, 0,
5274 internalformat
, GL_NONE
, GL_NONE
);
5275 assert(texFormat
!= MESA_FORMAT_NONE
);
5277 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, 0,
5278 width
, height
, depth
, 0);
5280 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, target
, 0, texFormat
,
5281 width
, height
, depth
, 0);
5283 if (_mesa_is_proxy_texture(target
)) {
5284 if (samplesOK
&& dimensionsOK
&& sizeOK
) {
5285 init_teximage_fields_ms(ctx
, texImage
, width
, height
, depth
, 0,
5286 internalformat
, texFormat
,
5287 samples
, fixedsamplelocations
);
5290 /* clear all image fields */
5291 clear_teximage_fields(texImage
);
5295 if (!dimensionsOK
) {
5296 _mesa_error(ctx
, GL_INVALID_VALUE
,
5297 "%s(invalid width or height)", func
);
5302 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(texture too large)", func
);
5306 /* Check if texObj->Immutable is set */
5307 if (texObj
->Immutable
) {
5308 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
5312 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
5314 init_teximage_fields_ms(ctx
, texImage
, width
, height
, depth
, 0,
5315 internalformat
, texFormat
,
5316 samples
, fixedsamplelocations
);
5318 if (width
> 0 && height
> 0 && depth
> 0) {
5319 if (!ctx
->Driver
.AllocTextureStorage(ctx
, texObj
, 1,
5320 width
, height
, depth
)) {
5321 /* tidy up the texture image state. strictly speaking,
5322 * we're allowed to just leave this in whatever state we
5323 * like, but being tidy is good.
5325 _mesa_init_teximage_fields(ctx
, texImage
,
5326 0, 0, 0, 0, GL_NONE
, MESA_FORMAT_NONE
);
5330 texObj
->Immutable
|= immutable
;
5333 _mesa_set_texture_view_state(ctx
, texObj
, target
, 1);
5336 _mesa_update_fbo_texture(ctx
, texObj
, 0, 0);
5342 _mesa_TexImage2DMultisample(GLenum target
, GLsizei samples
,
5343 GLenum internalformat
, GLsizei width
,
5344 GLsizei height
, GLboolean fixedsamplelocations
)
5346 struct gl_texture_object
*texObj
;
5347 GET_CURRENT_CONTEXT(ctx
);
5349 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5353 texture_image_multisample(ctx
, 2, texObj
, target
, samples
,
5354 internalformat
, width
, height
, 1,
5355 fixedsamplelocations
, GL_FALSE
,
5356 "glTexImage2DMultisample");
5361 _mesa_TexImage3DMultisample(GLenum target
, GLsizei samples
,
5362 GLenum internalformat
, GLsizei width
,
5363 GLsizei height
, GLsizei depth
,
5364 GLboolean fixedsamplelocations
)
5366 struct gl_texture_object
*texObj
;
5367 GET_CURRENT_CONTEXT(ctx
);
5369 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5373 texture_image_multisample(ctx
, 3, texObj
, target
, samples
,
5374 internalformat
, width
, height
, depth
,
5375 fixedsamplelocations
, GL_FALSE
,
5376 "glTexImage3DMultisample");
5380 valid_texstorage_ms_parameters(GLsizei width
, GLsizei height
, GLsizei depth
,
5381 GLsizei samples
, unsigned dims
)
5383 GET_CURRENT_CONTEXT(ctx
);
5385 if (!_mesa_valid_tex_storage_dim(width
, height
, depth
)) {
5386 _mesa_error(ctx
, GL_INVALID_VALUE
,
5387 "glTexStorage%uDMultisample(width=%d,height=%d,depth=%d)",
5388 dims
, width
, height
, depth
);
5395 _mesa_TexStorage2DMultisample(GLenum target
, GLsizei samples
,
5396 GLenum internalformat
, GLsizei width
,
5397 GLsizei height
, GLboolean fixedsamplelocations
)
5399 struct gl_texture_object
*texObj
;
5400 GET_CURRENT_CONTEXT(ctx
);
5402 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5406 if (!valid_texstorage_ms_parameters(width
, height
, 1, samples
, 2))
5409 texture_image_multisample(ctx
, 2, texObj
, target
, samples
,
5410 internalformat
, width
, height
, 1,
5411 fixedsamplelocations
, GL_TRUE
,
5412 "glTexStorage2DMultisample");
5416 _mesa_TexStorage3DMultisample(GLenum target
, GLsizei samples
,
5417 GLenum internalformat
, GLsizei width
,
5418 GLsizei height
, GLsizei depth
,
5419 GLboolean fixedsamplelocations
)
5421 struct gl_texture_object
*texObj
;
5422 GET_CURRENT_CONTEXT(ctx
);
5424 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5428 if (!valid_texstorage_ms_parameters(width
, height
, depth
, samples
, 3))
5431 texture_image_multisample(ctx
, 3, texObj
, target
, samples
,
5432 internalformat
, width
, height
, depth
,
5433 fixedsamplelocations
, GL_TRUE
,
5434 "glTexStorage3DMultisample");
5438 _mesa_TextureStorage2DMultisample(GLuint texture
, GLsizei samples
,
5439 GLenum internalformat
, GLsizei width
,
5441 GLboolean fixedsamplelocations
)
5443 struct gl_texture_object
*texObj
;
5444 GET_CURRENT_CONTEXT(ctx
);
5446 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
5447 "glTextureStorage2DMultisample");
5451 if (!valid_texstorage_ms_parameters(width
, height
, 1, samples
, 2))
5454 texture_image_multisample(ctx
, 2, texObj
, texObj
->Target
, samples
,
5455 internalformat
, width
, height
, 1,
5456 fixedsamplelocations
, GL_TRUE
,
5457 "glTextureStorage2DMultisample");
5461 _mesa_TextureStorage3DMultisample(GLuint texture
, GLsizei samples
,
5462 GLenum internalformat
, GLsizei width
,
5463 GLsizei height
, GLsizei depth
,
5464 GLboolean fixedsamplelocations
)
5466 struct gl_texture_object
*texObj
;
5467 GET_CURRENT_CONTEXT(ctx
);
5469 /* Get the texture object by Name. */
5470 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
5471 "glTextureStorage3DMultisample");
5475 if (!valid_texstorage_ms_parameters(width
, height
, depth
, samples
, 3))
5478 texture_image_multisample(ctx
, 3, texObj
, texObj
->Target
, samples
,
5479 internalformat
, width
, height
, depth
,
5480 fixedsamplelocations
, GL_TRUE
,
5481 "glTextureStorage3DMultisample");