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
;
121 * Install gl_texture_image in a gl_texture_object according to the target
122 * and level parameters.
124 * \param tObj texture object.
125 * \param target texture target.
126 * \param level image level.
127 * \param texImage texture image.
130 set_tex_image(struct gl_texture_object
*tObj
,
131 GLenum target
, GLint level
,
132 struct gl_texture_image
*texImage
)
134 const GLuint face
= _mesa_tex_target_to_face(target
);
138 if (target
== GL_TEXTURE_RECTANGLE_NV
|| target
== GL_TEXTURE_EXTERNAL_OES
)
141 tObj
->Image
[face
][level
] = texImage
;
143 /* Set the 'back' pointer */
144 texImage
->TexObject
= tObj
;
145 texImage
->Level
= level
;
146 texImage
->Face
= face
;
151 * Allocate a texture image structure.
153 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
156 * \return a pointer to gl_texture_image struct with all fields initialized to
159 struct gl_texture_image
*
160 _mesa_new_texture_image( struct gl_context
*ctx
)
163 return CALLOC_STRUCT(gl_texture_image
);
168 * Free a gl_texture_image and associated data.
169 * This function is a fallback called via ctx->Driver.DeleteTextureImage().
171 * \param texImage texture image.
173 * Free the texture image structure and the associated image data.
176 _mesa_delete_texture_image(struct gl_context
*ctx
,
177 struct gl_texture_image
*texImage
)
179 /* Free texImage->Data and/or any other driver-specific texture
182 assert(ctx
->Driver
.FreeTextureImageBuffer
);
183 ctx
->Driver
.FreeTextureImageBuffer( ctx
, texImage
);
189 * Test if a target is a proxy target.
191 * \param target texture target.
193 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
196 _mesa_is_proxy_texture(GLenum target
)
199 static const GLenum targets
[] = {
203 GL_PROXY_TEXTURE_CUBE_MAP
,
204 GL_PROXY_TEXTURE_RECTANGLE
,
205 GL_PROXY_TEXTURE_1D_ARRAY
,
206 GL_PROXY_TEXTURE_2D_ARRAY
,
207 GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
,
208 GL_PROXY_TEXTURE_2D_MULTISAMPLE
,
209 GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
212 * NUM_TEXTURE_TARGETS should match number of terms above, except there's no
213 * proxy for GL_TEXTURE_BUFFER and GL_TEXTURE_EXTERNAL_OES.
215 STATIC_ASSERT(NUM_TEXTURE_TARGETS
== ARRAY_SIZE(targets
) + 2);
217 for (i
= 0; i
< ARRAY_SIZE(targets
); ++i
)
218 if (target
== targets
[i
])
225 * Test if a target is an array target.
227 * \param target texture target.
229 * \return true if the target is an array target, false otherwise.
232 _mesa_is_array_texture(GLenum target
)
235 case GL_TEXTURE_1D_ARRAY
:
236 case GL_TEXTURE_2D_ARRAY
:
237 case GL_TEXTURE_CUBE_MAP_ARRAY
:
238 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
247 * Return the proxy target which corresponds to the given texture target
250 proxy_target(GLenum target
)
254 case GL_PROXY_TEXTURE_1D
:
255 return GL_PROXY_TEXTURE_1D
;
257 case GL_PROXY_TEXTURE_2D
:
258 return GL_PROXY_TEXTURE_2D
;
260 case GL_PROXY_TEXTURE_3D
:
261 return GL_PROXY_TEXTURE_3D
;
262 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
263 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
264 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
265 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
266 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
267 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
268 case GL_TEXTURE_CUBE_MAP
:
269 case GL_PROXY_TEXTURE_CUBE_MAP
:
270 return GL_PROXY_TEXTURE_CUBE_MAP
;
271 case GL_TEXTURE_RECTANGLE_NV
:
272 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
273 return GL_PROXY_TEXTURE_RECTANGLE_NV
;
274 case GL_TEXTURE_1D_ARRAY_EXT
:
275 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
276 return GL_PROXY_TEXTURE_1D_ARRAY_EXT
;
277 case GL_TEXTURE_2D_ARRAY_EXT
:
278 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
279 return GL_PROXY_TEXTURE_2D_ARRAY_EXT
;
280 case GL_TEXTURE_CUBE_MAP_ARRAY
:
281 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
282 return GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
;
283 case GL_TEXTURE_2D_MULTISAMPLE
:
284 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
285 return GL_PROXY_TEXTURE_2D_MULTISAMPLE
;
286 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
287 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
288 return GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
;
290 _mesa_problem(NULL
, "unexpected target in proxy_target()");
299 * Get a texture image pointer from a texture object, given a texture
300 * target and mipmap level. The target and level parameters should
301 * have already been error-checked.
303 * \param texObj texture unit.
304 * \param target texture target.
305 * \param level image level.
307 * \return pointer to the texture image structure, or NULL on failure.
309 struct gl_texture_image
*
310 _mesa_select_tex_image(const struct gl_texture_object
*texObj
,
311 GLenum target
, GLint level
)
313 const GLuint face
= _mesa_tex_target_to_face(target
);
317 assert(level
< MAX_TEXTURE_LEVELS
);
319 return texObj
->Image
[face
][level
];
324 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
325 * it and install it. Only return NULL if passed a bad parameter or run
328 struct gl_texture_image
*
329 _mesa_get_tex_image(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
330 GLenum target
, GLint level
)
332 struct gl_texture_image
*texImage
;
337 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
339 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
341 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture image allocation");
345 set_tex_image(texObj
, target
, level
, texImage
);
353 * Return pointer to the specified proxy texture image.
354 * Note that proxy textures are per-context, not per-texture unit.
355 * \return pointer to texture image or NULL if invalid target, invalid
356 * level, or out of memory.
358 static struct gl_texture_image
*
359 get_proxy_tex_image(struct gl_context
*ctx
, GLenum target
, GLint level
)
361 struct gl_texture_image
*texImage
;
368 case GL_PROXY_TEXTURE_1D
:
369 if (level
>= ctx
->Const
.MaxTextureLevels
)
371 texIndex
= TEXTURE_1D_INDEX
;
373 case GL_PROXY_TEXTURE_2D
:
374 if (level
>= ctx
->Const
.MaxTextureLevels
)
376 texIndex
= TEXTURE_2D_INDEX
;
378 case GL_PROXY_TEXTURE_3D
:
379 if (level
>= ctx
->Const
.Max3DTextureLevels
)
381 texIndex
= TEXTURE_3D_INDEX
;
383 case GL_PROXY_TEXTURE_CUBE_MAP
:
384 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
386 texIndex
= TEXTURE_CUBE_INDEX
;
388 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
391 texIndex
= TEXTURE_RECT_INDEX
;
393 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
394 if (level
>= ctx
->Const
.MaxTextureLevels
)
396 texIndex
= TEXTURE_1D_ARRAY_INDEX
;
398 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
399 if (level
>= ctx
->Const
.MaxTextureLevels
)
401 texIndex
= TEXTURE_2D_ARRAY_INDEX
;
403 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
404 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
406 texIndex
= TEXTURE_CUBE_ARRAY_INDEX
;
408 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
411 texIndex
= TEXTURE_2D_MULTISAMPLE_INDEX
;
413 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
416 texIndex
= TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
;
422 texImage
= ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
];
424 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
426 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
429 ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
] = texImage
;
430 /* Set the 'back' pointer */
431 texImage
->TexObject
= ctx
->Texture
.ProxyTex
[texIndex
];
438 * Get the maximum number of allowed mipmap levels.
440 * \param ctx GL context.
441 * \param target texture target.
443 * \return the maximum number of allowed mipmap levels for the given
444 * texture target, or zero if passed a bad target.
449 _mesa_max_texture_levels(struct gl_context
*ctx
, GLenum target
)
453 case GL_PROXY_TEXTURE_1D
:
455 case GL_PROXY_TEXTURE_2D
:
456 return ctx
->Const
.MaxTextureLevels
;
458 case GL_PROXY_TEXTURE_3D
:
459 return ctx
->Const
.Max3DTextureLevels
;
460 case GL_TEXTURE_CUBE_MAP
:
461 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
462 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
463 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
464 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
465 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
466 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
467 case GL_PROXY_TEXTURE_CUBE_MAP
:
468 return ctx
->Extensions
.ARB_texture_cube_map
469 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
470 case GL_TEXTURE_RECTANGLE_NV
:
471 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
472 return ctx
->Extensions
.NV_texture_rectangle
? 1 : 0;
473 case GL_TEXTURE_1D_ARRAY_EXT
:
474 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
475 case GL_TEXTURE_2D_ARRAY_EXT
:
476 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
477 return ctx
->Extensions
.EXT_texture_array
478 ? ctx
->Const
.MaxTextureLevels
: 0;
479 case GL_TEXTURE_CUBE_MAP_ARRAY
:
480 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
481 return ctx
->Extensions
.ARB_texture_cube_map_array
482 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
483 case GL_TEXTURE_BUFFER
:
484 return ctx
->API
== API_OPENGL_CORE
&&
485 ctx
->Extensions
.ARB_texture_buffer_object
? 1 : 0;
486 case GL_TEXTURE_2D_MULTISAMPLE
:
487 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
488 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
489 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
490 return (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles31(ctx
))
491 && ctx
->Extensions
.ARB_texture_multisample
493 case GL_TEXTURE_EXTERNAL_OES
:
496 return 0; /* bad target */
502 * Return number of dimensions per mipmap level for the given texture target.
505 _mesa_get_texture_dimensions(GLenum target
)
509 case GL_PROXY_TEXTURE_1D
:
512 case GL_TEXTURE_RECTANGLE
:
513 case GL_TEXTURE_CUBE_MAP
:
514 case GL_PROXY_TEXTURE_2D
:
515 case GL_PROXY_TEXTURE_RECTANGLE
:
516 case GL_PROXY_TEXTURE_CUBE_MAP
:
517 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
518 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
519 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
520 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
521 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
522 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
523 case GL_TEXTURE_1D_ARRAY
:
524 case GL_PROXY_TEXTURE_1D_ARRAY
:
525 case GL_TEXTURE_EXTERNAL_OES
:
526 case GL_TEXTURE_2D_MULTISAMPLE
:
527 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
530 case GL_PROXY_TEXTURE_3D
:
531 case GL_TEXTURE_2D_ARRAY
:
532 case GL_PROXY_TEXTURE_2D_ARRAY
:
533 case GL_TEXTURE_CUBE_MAP_ARRAY
:
534 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
535 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
536 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
538 case GL_TEXTURE_BUFFER
:
541 _mesa_problem(NULL
, "invalid target 0x%x in get_texture_dimensions()",
549 * Check if a texture target can have more than one layer.
552 _mesa_tex_target_is_layered(GLenum target
)
556 case GL_PROXY_TEXTURE_1D
:
558 case GL_PROXY_TEXTURE_2D
:
559 case GL_TEXTURE_RECTANGLE
:
560 case GL_PROXY_TEXTURE_RECTANGLE
:
561 case GL_TEXTURE_2D_MULTISAMPLE
:
562 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
563 case GL_TEXTURE_BUFFER
:
564 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
565 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
566 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
567 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
568 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
569 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
570 case GL_TEXTURE_EXTERNAL_OES
:
574 case GL_PROXY_TEXTURE_3D
:
575 case GL_TEXTURE_CUBE_MAP
:
576 case GL_PROXY_TEXTURE_CUBE_MAP
:
577 case GL_TEXTURE_1D_ARRAY
:
578 case GL_PROXY_TEXTURE_1D_ARRAY
:
579 case GL_TEXTURE_2D_ARRAY
:
580 case GL_PROXY_TEXTURE_2D_ARRAY
:
581 case GL_TEXTURE_CUBE_MAP_ARRAY
:
582 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
583 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
584 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
588 assert(!"Invalid texture target.");
595 * Return the number of layers present in the given level of an array,
596 * cubemap or 3D texture. If the texture is not layered return zero.
599 _mesa_get_texture_layers(const struct gl_texture_object
*texObj
, GLint level
)
601 assert(level
>= 0 && level
< MAX_TEXTURE_LEVELS
);
603 switch (texObj
->Target
) {
606 case GL_TEXTURE_RECTANGLE
:
607 case GL_TEXTURE_2D_MULTISAMPLE
:
608 case GL_TEXTURE_BUFFER
:
609 case GL_TEXTURE_EXTERNAL_OES
:
612 case GL_TEXTURE_CUBE_MAP
:
615 case GL_TEXTURE_1D_ARRAY
: {
616 struct gl_texture_image
*img
= texObj
->Image
[0][level
];
617 return img
? img
->Height
: 0;
621 case GL_TEXTURE_2D_ARRAY
:
622 case GL_TEXTURE_CUBE_MAP_ARRAY
:
623 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
: {
624 struct gl_texture_image
*img
= texObj
->Image
[0][level
];
625 return img
? img
->Depth
: 0;
629 assert(!"Invalid texture target.");
636 * Return the maximum number of mipmap levels for the given target
637 * and the dimensions.
638 * The dimensions are expected not to include the border.
641 _mesa_get_tex_max_num_levels(GLenum target
, GLsizei width
, GLsizei height
,
648 case GL_TEXTURE_1D_ARRAY
:
649 case GL_PROXY_TEXTURE_1D
:
650 case GL_PROXY_TEXTURE_1D_ARRAY
:
653 case GL_TEXTURE_CUBE_MAP
:
654 case GL_TEXTURE_CUBE_MAP_ARRAY
:
655 case GL_PROXY_TEXTURE_CUBE_MAP
:
656 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
660 case GL_TEXTURE_2D_ARRAY
:
661 case GL_PROXY_TEXTURE_2D
:
662 case GL_PROXY_TEXTURE_2D_ARRAY
:
663 size
= MAX2(width
, height
);
666 case GL_PROXY_TEXTURE_3D
:
667 size
= MAX3(width
, height
, depth
);
669 case GL_TEXTURE_RECTANGLE
:
670 case GL_TEXTURE_EXTERNAL_OES
:
671 case GL_TEXTURE_2D_MULTISAMPLE
:
672 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
673 case GL_PROXY_TEXTURE_RECTANGLE
:
674 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
675 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
682 return _mesa_logbase2(size
) + 1;
686 #if 000 /* not used anymore */
688 * glTexImage[123]D can accept a NULL image pointer. In this case we
689 * create a texture image with unspecified image contents per the OpenGL
693 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
695 const GLint components
= _mesa_components_in_format(format
);
696 const GLint numPixels
= width
* height
* depth
;
697 GLubyte
*data
= (GLubyte
*) malloc(numPixels
* components
* sizeof(GLubyte
));
701 * Let's see if anyone finds this. If glTexImage2D() is called with
702 * a NULL image pointer then load the texture image with something
703 * interesting instead of leaving it indeterminate.
706 static const char message
[8][32] = {
710 " X X XXXX XXX XXXXX ",
713 " X X XXXXX XXX X X ",
717 GLubyte
*imgPtr
= data
;
719 for (h
= 0; h
< depth
; h
++) {
720 for (i
= 0; i
< height
; i
++) {
721 GLint srcRow
= 7 - (i
% 8);
722 for (j
= 0; j
< width
; j
++) {
723 GLint srcCol
= j
% 32;
724 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
725 for (k
= 0; k
< components
; k
++) {
741 * Set the size and format-related fields of a gl_texture_image struct
742 * to zero. This is used when a proxy texture test fails.
745 clear_teximage_fields(struct gl_texture_image
*img
)
748 img
->_BaseFormat
= 0;
749 img
->InternalFormat
= 0;
760 img
->TexFormat
= MESA_FORMAT_NONE
;
762 img
->FixedSampleLocations
= GL_TRUE
;
767 * Initialize basic fields of the gl_texture_image struct.
769 * \param ctx GL context.
770 * \param img texture image structure to be initialized.
771 * \param width image width.
772 * \param height image height.
773 * \param depth image depth.
774 * \param border image border.
775 * \param internalFormat internal format.
776 * \param format the actual hardware format (one of MESA_FORMAT_*)
777 * \param numSamples number of samples per texel, or zero for non-MS.
778 * \param fixedSampleLocations are sample locations fixed?
780 * Fills in the fields of \p img with the given information.
781 * Note: width, height and depth include the border.
784 init_teximage_fields_ms(struct gl_context
*ctx
,
785 struct gl_texture_image
*img
,
786 GLsizei width
, GLsizei height
, GLsizei depth
,
787 GLint border
, GLenum internalFormat
,
789 GLuint numSamples
, GLboolean fixedSampleLocations
)
797 target
= img
->TexObject
->Target
;
798 img
->_BaseFormat
= _mesa_base_tex_format( ctx
, internalFormat
);
799 assert(img
->_BaseFormat
!= -1);
800 img
->InternalFormat
= internalFormat
;
801 img
->Border
= border
;
803 img
->Height
= height
;
806 img
->Width2
= width
- 2 * border
; /* == 1 << img->WidthLog2; */
807 img
->WidthLog2
= _mesa_logbase2(img
->Width2
);
810 img
->FixedSampleLocations
= GL_TRUE
;
814 case GL_TEXTURE_BUFFER
:
815 case GL_PROXY_TEXTURE_1D
:
827 case GL_TEXTURE_1D_ARRAY
:
828 case GL_PROXY_TEXTURE_1D_ARRAY
:
829 img
->Height2
= height
; /* no border */
830 img
->HeightLog2
= 0; /* not used */
838 case GL_TEXTURE_RECTANGLE
:
839 case GL_TEXTURE_CUBE_MAP
:
840 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
841 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
842 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
843 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
844 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
845 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
846 case GL_TEXTURE_EXTERNAL_OES
:
847 case GL_PROXY_TEXTURE_2D
:
848 case GL_PROXY_TEXTURE_RECTANGLE
:
849 case GL_PROXY_TEXTURE_CUBE_MAP
:
850 case GL_TEXTURE_2D_MULTISAMPLE
:
851 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
852 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
853 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
860 case GL_TEXTURE_2D_ARRAY
:
861 case GL_PROXY_TEXTURE_2D_ARRAY
:
862 case GL_TEXTURE_CUBE_MAP_ARRAY
:
863 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
864 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
865 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
866 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
867 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
868 img
->Depth2
= depth
; /* no border */
869 img
->DepthLog2
= 0; /* not used */
872 case GL_PROXY_TEXTURE_3D
:
873 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
874 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
875 img
->Depth2
= depth
- 2 * border
; /* == 1 << img->DepthLog2; */
876 img
->DepthLog2
= _mesa_logbase2(img
->Depth2
);
879 _mesa_problem(NULL
, "invalid target 0x%x in _mesa_init_teximage_fields()",
884 _mesa_get_tex_max_num_levels(target
,
885 img
->Width2
, img
->Height2
, img
->Depth2
);
886 img
->TexFormat
= format
;
887 img
->NumSamples
= numSamples
;
888 img
->FixedSampleLocations
= fixedSampleLocations
;
893 _mesa_init_teximage_fields(struct gl_context
*ctx
,
894 struct gl_texture_image
*img
,
895 GLsizei width
, GLsizei height
, GLsizei depth
,
896 GLint border
, GLenum internalFormat
,
899 init_teximage_fields_ms(ctx
, img
, width
, height
, depth
, border
,
900 internalFormat
, format
, 0, GL_TRUE
);
905 * Free and clear fields of the gl_texture_image struct.
907 * \param ctx GL context.
908 * \param texImage texture image structure to be cleared.
910 * After the call, \p texImage will have no data associated with it. Its
911 * fields are cleared so that its parent object will test incomplete.
914 _mesa_clear_texture_image(struct gl_context
*ctx
,
915 struct gl_texture_image
*texImage
)
917 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
918 clear_teximage_fields(texImage
);
923 * Check the width, height, depth and border of a texture image are legal.
924 * Used by all the glTexImage, glCompressedTexImage and glCopyTexImage
926 * The target and level parameters will have already been validated.
927 * \return GL_TRUE if size is OK, GL_FALSE otherwise.
930 _mesa_legal_texture_dimensions(struct gl_context
*ctx
, GLenum target
,
931 GLint level
, GLint width
, GLint height
,
932 GLint depth
, GLint border
)
938 case GL_PROXY_TEXTURE_1D
:
939 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1); /* level zero size */
940 maxSize
>>= level
; /* level size */
941 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
943 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
944 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
950 case GL_PROXY_TEXTURE_2D
:
951 case GL_TEXTURE_2D_MULTISAMPLE
:
952 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
953 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
955 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
957 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
959 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
960 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
962 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
968 case GL_PROXY_TEXTURE_3D
:
969 maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
971 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
973 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
975 if (depth
< 2 * border
|| depth
> 2 * border
+ maxSize
)
977 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
978 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
980 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
982 if (depth
> 0 && !_mesa_is_pow_two(depth
- 2 * border
))
987 case GL_TEXTURE_RECTANGLE_NV
:
988 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
991 maxSize
= ctx
->Const
.MaxTextureRectSize
;
992 if (width
< 0 || width
> maxSize
)
994 if (height
< 0 || height
> maxSize
)
998 case GL_TEXTURE_CUBE_MAP
:
999 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1000 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1001 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1002 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1003 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1004 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1005 case GL_PROXY_TEXTURE_CUBE_MAP
:
1006 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1008 if (width
!= height
)
1010 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1012 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1014 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1015 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1017 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1022 case GL_TEXTURE_1D_ARRAY_EXT
:
1023 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1024 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1026 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1028 if (height
< 0 || height
> ctx
->Const
.MaxArrayTextureLayers
)
1030 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1031 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1036 case GL_TEXTURE_2D_ARRAY_EXT
:
1037 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1038 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1039 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1040 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1042 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1044 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1046 if (depth
< 0 || depth
> ctx
->Const
.MaxArrayTextureLayers
)
1048 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1049 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1051 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1056 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1057 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1058 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1059 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1061 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1063 if (depth
< 0 || depth
> ctx
->Const
.MaxArrayTextureLayers
|| depth
% 6)
1065 if (width
!= height
)
1067 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
1069 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1070 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1072 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1077 _mesa_problem(ctx
, "Invalid target in _mesa_legal_texture_dimensions()");
1084 * Do error checking of xoffset, yoffset, zoffset, width, height and depth
1085 * for glTexSubImage, glCopyTexSubImage and glCompressedTexSubImage.
1086 * \param destImage the destination texture image.
1087 * \return GL_TRUE if error found, GL_FALSE otherwise.
1090 error_check_subtexture_dimensions(struct gl_context
*ctx
, GLuint dims
,
1091 const struct gl_texture_image
*destImage
,
1092 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1093 GLsizei subWidth
, GLsizei subHeight
,
1094 GLsizei subDepth
, const char *func
)
1096 const GLenum target
= destImage
->TexObject
->Target
;
1101 _mesa_error(ctx
, GL_INVALID_VALUE
,
1102 "%s(width=%d)", func
, subWidth
);
1106 if (dims
> 1 && subHeight
< 0) {
1107 _mesa_error(ctx
, GL_INVALID_VALUE
,
1108 "%s(height=%d)", func
, subHeight
);
1112 if (dims
> 2 && subDepth
< 0) {
1113 _mesa_error(ctx
, GL_INVALID_VALUE
,
1114 "%s(depth=%d)", func
, subDepth
);
1118 /* check xoffset and width */
1119 if (xoffset
< - (GLint
) destImage
->Border
) {
1120 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(xoffset)", func
);
1124 if (xoffset
+ subWidth
> (GLint
) destImage
->Width
) {
1125 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(xoffset+width)", func
);
1129 /* check yoffset and height */
1131 GLint yBorder
= (target
== GL_TEXTURE_1D_ARRAY
) ? 0 : destImage
->Border
;
1132 if (yoffset
< -yBorder
) {
1133 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(yoffset)", func
);
1136 if (yoffset
+ subHeight
> (GLint
) destImage
->Height
) {
1137 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(yoffset+height)", func
);
1142 /* check zoffset and depth */
1145 GLint zBorder
= (target
== GL_TEXTURE_2D_ARRAY
||
1146 target
== GL_TEXTURE_CUBE_MAP_ARRAY
) ?
1147 0 : destImage
->Border
;
1149 if (zoffset
< -zBorder
) {
1150 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(zoffset)", func
);
1154 depth
= (GLint
) destImage
->Depth
;
1155 if (target
== GL_TEXTURE_CUBE_MAP
)
1157 if (zoffset
+ subDepth
> depth
) {
1158 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(zoffset+depth)", func
);
1164 * The OpenGL spec (and GL_ARB_texture_compression) says only whole
1165 * compressed texture images can be updated. But, that restriction may be
1166 * relaxed for particular compressed formats. At this time, all the
1167 * compressed formats supported by Mesa allow sub-textures to be updated
1168 * along compressed block boundaries.
1170 _mesa_get_format_block_size(destImage
->TexFormat
, &bw
, &bh
);
1172 if (bw
!= 1 || bh
!= 1) {
1173 /* offset must be multiple of block size */
1174 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0)) {
1175 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1176 "%s(xoffset = %d, yoffset = %d)",
1177 func
, xoffset
, yoffset
);
1181 /* The size must be a multiple of bw x bh, or we must be using a
1182 * offset+size that exactly hits the edge of the image. This
1183 * is important for small mipmap levels (1x1, 2x1, etc) and for
1186 if ((subWidth
% bw
!= 0) &&
1187 (xoffset
+ subWidth
!= (GLint
) destImage
->Width
)) {
1188 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1189 "%s(width = %d)", func
, subWidth
);
1193 if ((subHeight
% bh
!= 0) &&
1194 (yoffset
+ subHeight
!= (GLint
) destImage
->Height
)) {
1195 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1196 "%s(height = %d)", func
, subHeight
);
1208 * This is the fallback for Driver.TestProxyTexImage() for doing device-
1209 * specific texture image size checks.
1211 * A hardware driver might override this function if, for example, the
1212 * max 3D texture size is 512x512x64 (i.e. not a cube).
1214 * Note that width, height, depth == 0 is not an error. However, a
1215 * texture with zero width/height/depth will be considered "incomplete"
1216 * and texturing will effectively be disabled.
1218 * \param target any texture target/type
1219 * \param level as passed to glTexImage
1220 * \param format the MESA_FORMAT_x for the tex image
1221 * \param width as passed to glTexImage
1222 * \param height as passed to glTexImage
1223 * \param depth as passed to glTexImage
1224 * \param border as passed to glTexImage
1225 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1228 _mesa_test_proxy_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1230 GLint width
, GLint height
, GLint depth
, GLint border
)
1232 /* We just check if the image size is less than MaxTextureMbytes.
1233 * Some drivers may do more specific checks.
1235 uint64_t bytes
= _mesa_format_image_size64(format
, width
, height
, depth
);
1236 uint64_t mbytes
= bytes
/ (1024 * 1024); /* convert to MB */
1237 mbytes
*= _mesa_num_tex_faces(target
);
1238 return mbytes
<= (uint64_t) ctx
->Const
.MaxTextureMbytes
;
1243 * Return true if the format is only valid for glCompressedTexImage.
1246 compressedteximage_only_format(const struct gl_context
*ctx
, GLenum format
)
1249 case GL_ETC1_RGB8_OES
:
1250 case GL_PALETTE4_RGB8_OES
:
1251 case GL_PALETTE4_RGBA8_OES
:
1252 case GL_PALETTE4_R5_G6_B5_OES
:
1253 case GL_PALETTE4_RGBA4_OES
:
1254 case GL_PALETTE4_RGB5_A1_OES
:
1255 case GL_PALETTE8_RGB8_OES
:
1256 case GL_PALETTE8_RGBA8_OES
:
1257 case GL_PALETTE8_R5_G6_B5_OES
:
1258 case GL_PALETTE8_RGBA4_OES
:
1259 case GL_PALETTE8_RGB5_A1_OES
:
1267 * Return true if the format doesn't support online compression.
1270 _mesa_format_no_online_compression(const struct gl_context
*ctx
, GLenum format
)
1272 return _mesa_is_astc_format(format
) ||
1273 compressedteximage_only_format(ctx
, format
);
1276 /* Writes to an GL error pointer if non-null and returns whether or not the
1277 * error is GL_NO_ERROR */
1279 write_error(GLenum
*err_ptr
, GLenum error
)
1284 return error
== GL_NO_ERROR
;
1288 * Helper function to determine whether a target and specific compression
1289 * format are supported. The error parameter returns GL_NO_ERROR if the
1290 * target can be compressed. Otherwise it returns either GL_INVALID_OPERATION
1291 * or GL_INVALID_ENUM, whichever is more appropriate.
1294 _mesa_target_can_be_compressed(const struct gl_context
*ctx
, GLenum target
,
1295 GLenum intFormat
, GLenum
*error
)
1297 GLboolean target_can_be_compresed
= GL_FALSE
;
1298 mesa_format format
= _mesa_glenum_to_compressed_format(intFormat
);
1299 enum mesa_format_layout layout
= _mesa_get_format_layout(format
);
1303 case GL_PROXY_TEXTURE_2D
:
1304 target_can_be_compresed
= GL_TRUE
; /* true for any compressed format so far */
1306 case GL_PROXY_TEXTURE_CUBE_MAP
:
1307 case GL_TEXTURE_CUBE_MAP
:
1308 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1309 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1310 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1311 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1312 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1313 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1314 target_can_be_compresed
= ctx
->Extensions
.ARB_texture_cube_map
;
1316 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1317 case GL_TEXTURE_2D_ARRAY_EXT
:
1318 target_can_be_compresed
= ctx
->Extensions
.EXT_texture_array
;
1320 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1321 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1322 /* From the KHR_texture_compression_astc_hdr spec:
1324 * Add a second new column "3D Tex." which is empty for all non-ASTC
1325 * formats. If only the LDR profile is supported by the
1326 * implementation, this column is also empty for all ASTC formats. If
1327 * both the LDR and HDR profiles are supported only, this column is
1328 * checked for all ASTC formats.
1330 * Add a third new column "Cube Map Array Tex." which is empty for all
1331 * non-ASTC formats, and checked for all ASTC formats.
1335 * 'An INVALID_OPERATION error is generated by CompressedTexImage3D
1336 * if <internalformat> is TEXTURE_CUBE_MAP_ARRAY and the
1337 * "Cube Map Array" column of table 8.19 is *not* checked, or if
1338 * <internalformat> is TEXTURE_3D and the "3D Tex." column of table
1339 * 8.19 is *not* checked'
1341 * The instances of <internalformat> above should say <target>.
1343 * ETC2/EAC formats are the only alternative in GLES and thus such errors
1344 * have already been handled by normal ETC2/EAC behavior.
1347 /* From section 3.8.6, page 146 of OpenGL ES 3.0 spec:
1349 * "The ETC2/EAC texture compression algorithm supports only
1350 * two-dimensional images. If internalformat is an ETC2/EAC format,
1351 * glCompressedTexImage3D will generate an INVALID_OPERATION error if
1352 * target is not TEXTURE_2D_ARRAY."
1354 * This should also be applicable for glTexStorage3D(). Other available
1355 * targets for these functions are: TEXTURE_3D and TEXTURE_CUBE_MAP_ARRAY.
1357 if (layout
== MESA_FORMAT_LAYOUT_ETC2
&& _mesa_is_gles3(ctx
))
1358 return write_error(error
, GL_INVALID_OPERATION
);
1359 target_can_be_compresed
= ctx
->Extensions
.ARB_texture_cube_map_array
;
1363 case MESA_FORMAT_LAYOUT_ETC2
:
1364 /* See ETC2/EAC comment in case GL_TEXTURE_CUBE_MAP_ARRAY. */
1365 if (_mesa_is_gles3(ctx
))
1366 return write_error(error
, GL_INVALID_OPERATION
);
1368 case MESA_FORMAT_LAYOUT_BPTC
:
1369 target_can_be_compresed
= ctx
->Extensions
.ARB_texture_compression_bptc
;
1371 case MESA_FORMAT_LAYOUT_ASTC
:
1372 target_can_be_compresed
=
1373 ctx
->Extensions
.KHR_texture_compression_astc_hdr
;
1375 /* Throw an INVALID_OPERATION error if the target is TEXTURE_3D and
1376 * and the hdr extension is not supported.
1377 * See comment in switch case GL_TEXTURE_CUBE_MAP_ARRAY for more info.
1379 if (!target_can_be_compresed
)
1380 return write_error(error
, GL_INVALID_OPERATION
);
1388 return write_error(error
,
1389 target_can_be_compresed
? GL_NO_ERROR
: GL_INVALID_ENUM
);
1394 * Check if the given texture target value is legal for a
1395 * glTexImage1/2/3D call.
1398 legal_teximage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
1404 case GL_PROXY_TEXTURE_1D
:
1405 return _mesa_is_desktop_gl(ctx
);
1413 case GL_PROXY_TEXTURE_2D
:
1414 return _mesa_is_desktop_gl(ctx
);
1415 case GL_PROXY_TEXTURE_CUBE_MAP
:
1416 return _mesa_is_desktop_gl(ctx
)
1417 && ctx
->Extensions
.ARB_texture_cube_map
;
1418 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1419 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1420 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1421 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1422 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1423 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1424 return ctx
->Extensions
.ARB_texture_cube_map
;
1425 case GL_TEXTURE_RECTANGLE_NV
:
1426 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1427 return _mesa_is_desktop_gl(ctx
)
1428 && ctx
->Extensions
.NV_texture_rectangle
;
1429 case GL_TEXTURE_1D_ARRAY_EXT
:
1430 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1431 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1439 case GL_PROXY_TEXTURE_3D
:
1440 return _mesa_is_desktop_gl(ctx
);
1441 case GL_TEXTURE_2D_ARRAY_EXT
:
1442 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1443 || _mesa_is_gles3(ctx
);
1444 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1445 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1446 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1447 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1448 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1453 _mesa_problem(ctx
, "invalid dims=%u in legal_teximage_target()", dims
);
1460 * Check if the given texture target value is legal for a
1461 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1462 * The difference compared to legal_teximage_target() above is that
1463 * proxy targets are not supported.
1466 legal_texsubimage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
,
1471 return _mesa_is_desktop_gl(ctx
) && target
== GL_TEXTURE_1D
;
1476 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1477 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1478 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1479 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1480 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1481 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1482 return ctx
->Extensions
.ARB_texture_cube_map
;
1483 case GL_TEXTURE_RECTANGLE_NV
:
1484 return _mesa_is_desktop_gl(ctx
)
1485 && ctx
->Extensions
.NV_texture_rectangle
;
1486 case GL_TEXTURE_1D_ARRAY_EXT
:
1487 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1495 case GL_TEXTURE_2D_ARRAY_EXT
:
1496 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1497 || _mesa_is_gles3(ctx
);
1498 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1499 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1500 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1502 /* Table 8.15 of the OpenGL 4.5 core profile spec
1503 * (20141030) says that TEXTURE_CUBE_MAP is valid for TextureSubImage3D
1504 * and CopyTextureSubImage3D.
1506 case GL_TEXTURE_CUBE_MAP
:
1512 _mesa_problem(ctx
, "invalid dims=%u in legal_texsubimage_target()",
1520 * Helper function to determine if a texture object is mutable (in terms
1521 * of GL_ARB_texture_storage).
1524 mutable_tex_object(struct gl_context
*ctx
, GLenum target
)
1526 struct gl_texture_object
*texObj
= _mesa_get_current_tex_object(ctx
, target
);
1530 return !texObj
->Immutable
;
1535 * Return expected size of a compressed texture.
1538 compressed_tex_size(GLsizei width
, GLsizei height
, GLsizei depth
,
1541 mesa_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
1542 return _mesa_format_image_size(mesaFormat
, width
, height
, depth
);
1546 * Verify that a texture format is valid with a particular target
1548 * In particular, textures with base format of \c GL_DEPTH_COMPONENT or
1549 * \c GL_DEPTH_STENCIL are only valid with certain, context dependent texture
1552 * \param ctx GL context
1553 * \param target Texture target
1554 * \param internalFormat Internal format of the texture image
1555 * \param dimensions Dimensionality at the caller. This is \b not used
1556 * in the validation. It is only used when logging
1558 * \param caller Base name of the calling function (e.g.,
1559 * "glTexImage" or "glTexStorage").
1561 * \returns true if the combination is legal, false otherwise.
1564 _mesa_legal_texture_base_format_for_target(struct gl_context
*ctx
,
1565 GLenum target
, GLenum internalFormat
,
1566 unsigned dimensions
,
1569 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
1570 || _mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_STENCIL
1571 || _mesa_base_tex_format(ctx
, internalFormat
) == GL_STENCIL_INDEX
) {
1572 /* Section 3.8.3 (Texture Image Specification) of the OpenGL 3.3 Core
1573 * Profile spec says:
1575 * "Textures with a base internal format of DEPTH_COMPONENT or
1576 * DEPTH_STENCIL are supported by texture image specification
1577 * commands only if target is TEXTURE_1D, TEXTURE_2D,
1578 * TEXTURE_1D_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_RECTANGLE,
1579 * TEXTURE_CUBE_MAP, PROXY_TEXTURE_1D, PROXY_TEXTURE_2D,
1580 * PROXY_TEXTURE_1D_ARRAY, PROXY_TEXTURE_2D_ARRAY,
1581 * PROXY_TEXTURE_RECTANGLE, or PROXY_TEXTURE_CUBE_MAP. Using these
1582 * formats in conjunction with any other target will result in an
1583 * INVALID_OPERATION error."
1585 * Cubemaps are only supported with desktop OpenGL version >= 3.0,
1586 * EXT_gpu_shader4, or, on OpenGL ES 2.0+, OES_depth_texture_cube_map.
1588 if (target
!= GL_TEXTURE_1D
&&
1589 target
!= GL_PROXY_TEXTURE_1D
&&
1590 target
!= GL_TEXTURE_2D
&&
1591 target
!= GL_PROXY_TEXTURE_2D
&&
1592 target
!= GL_TEXTURE_1D_ARRAY
&&
1593 target
!= GL_PROXY_TEXTURE_1D_ARRAY
&&
1594 target
!= GL_TEXTURE_2D_ARRAY
&&
1595 target
!= GL_PROXY_TEXTURE_2D_ARRAY
&&
1596 target
!= GL_TEXTURE_RECTANGLE_ARB
&&
1597 target
!= GL_PROXY_TEXTURE_RECTANGLE_ARB
&&
1598 !((_mesa_is_cube_face(target
) ||
1599 target
== GL_TEXTURE_CUBE_MAP
||
1600 target
== GL_PROXY_TEXTURE_CUBE_MAP
) &&
1601 (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_gpu_shader4
1602 || (ctx
->API
== API_OPENGLES2
&& ctx
->Extensions
.OES_depth_texture_cube_map
))) &&
1603 !((target
== GL_TEXTURE_CUBE_MAP_ARRAY
||
1604 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
) &&
1605 ctx
->Extensions
.ARB_texture_cube_map_array
)) {
1606 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1607 "%s%dD(bad target for depth texture)",
1608 caller
, dimensions
);
1617 texture_formats_agree(GLenum internalFormat
,
1620 GLboolean colorFormat
;
1621 GLboolean is_format_depth_or_depthstencil
;
1622 GLboolean is_internalFormat_depth_or_depthstencil
;
1624 /* Even though there are no color-index textures, we still have to support
1625 * uploading color-index data and remapping it to RGB via the
1626 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
1628 const GLboolean indexFormat
= (format
== GL_COLOR_INDEX
);
1630 is_internalFormat_depth_or_depthstencil
=
1631 _mesa_is_depth_format(internalFormat
) ||
1632 _mesa_is_depthstencil_format(internalFormat
);
1634 is_format_depth_or_depthstencil
=
1635 _mesa_is_depth_format(format
) ||
1636 _mesa_is_depthstencil_format(format
);
1638 colorFormat
= _mesa_is_color_format(format
);
1640 if (_mesa_is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
)
1643 if (is_internalFormat_depth_or_depthstencil
!=
1644 is_format_depth_or_depthstencil
)
1647 if (_mesa_is_ycbcr_format(internalFormat
) != _mesa_is_ycbcr_format(format
))
1654 * Test the combination of format, type and internal format arguments of
1655 * different texture operations on GLES.
1657 * \param ctx GL context.
1658 * \param format pixel data format given by the user.
1659 * \param type pixel data type given by the user.
1660 * \param internalFormat internal format given by the user.
1661 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1662 * \param callerName name of the caller function to print in the error message
1664 * \return true if a error is found, false otherwise
1666 * Currently, it is used by texture_error_check() and texsubimage_error_check().
1669 texture_format_error_check_gles(struct gl_context
*ctx
, GLenum format
,
1670 GLenum type
, GLenum internalFormat
,
1671 GLuint dimensions
, const char *callerName
)
1675 if (_mesa_is_gles3(ctx
)) {
1676 err
= _mesa_es3_error_check_format_and_type(ctx
, format
, type
,
1678 if (err
!= GL_NO_ERROR
) {
1679 _mesa_error(ctx
, err
,
1680 "%s(format = %s, type = %s, internalformat = %s)",
1681 callerName
, _mesa_enum_to_string(format
),
1682 _mesa_enum_to_string(type
),
1683 _mesa_enum_to_string(internalFormat
));
1688 err
= _mesa_es_error_check_format_and_type(ctx
, format
, type
, dimensions
);
1689 if (err
!= GL_NO_ERROR
) {
1690 _mesa_error(ctx
, err
, "%s(format = %s, type = %s)",
1691 callerName
, _mesa_enum_to_string(format
),
1692 _mesa_enum_to_string(type
));
1701 * Test the glTexImage[123]D() parameters for errors.
1703 * \param ctx GL context.
1704 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1705 * \param target texture target given by the user (already validated).
1706 * \param level image level given by the user.
1707 * \param internalFormat internal format given by the user.
1708 * \param format pixel data format given by the user.
1709 * \param type pixel data type given by the user.
1710 * \param width image width given by the user.
1711 * \param height image height given by the user.
1712 * \param depth image depth given by the user.
1713 * \param border image border given by the user.
1715 * \return GL_TRUE if a error is found, GL_FALSE otherwise
1717 * Verifies each of the parameters against the constants specified in
1718 * __struct gl_contextRec::Const and the supported extensions, and according
1719 * to the OpenGL specification.
1720 * Note that we don't fully error-check the width, height, depth values
1721 * here. That's done in _mesa_legal_texture_dimensions() which is used
1722 * by several other GL entrypoints. Plus, texture dims have a special
1723 * interaction with proxy textures.
1726 texture_error_check( struct gl_context
*ctx
,
1727 GLuint dimensions
, GLenum target
,
1728 GLint level
, GLint internalFormat
,
1729 GLenum format
, GLenum type
,
1730 GLint width
, GLint height
,
1731 GLint depth
, GLint border
,
1732 const GLvoid
*pixels
)
1736 /* Note: for proxy textures, some error conditions immediately generate
1737 * a GL error in the usual way. But others do not generate a GL error.
1738 * Instead, they cause the width, height, depth, format fields of the
1739 * texture image to be zeroed-out. The GL spec seems to indicate that the
1740 * zero-out behaviour is only used in cases related to memory allocation.
1744 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
1745 _mesa_error(ctx
, GL_INVALID_VALUE
,
1746 "glTexImage%dD(level=%d)", dimensions
, level
);
1751 if (border
< 0 || border
> 1 ||
1752 ((ctx
->API
!= API_OPENGL_COMPAT
||
1753 target
== GL_TEXTURE_RECTANGLE_NV
||
1754 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1755 _mesa_error(ctx
, GL_INVALID_VALUE
,
1756 "glTexImage%dD(border=%d)", dimensions
, border
);
1760 if (width
< 0 || height
< 0 || depth
< 0) {
1761 _mesa_error(ctx
, GL_INVALID_VALUE
,
1762 "glTexImage%dD(width, height or depth < 0)", dimensions
);
1766 /* Check incoming image format and type */
1767 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1768 if (err
!= GL_NO_ERROR
) {
1769 /* Prior to OpenGL-ES 2.0, an INVALID_VALUE is expected instead of
1770 * INVALID_ENUM. From page 73 OpenGL ES 1.1 spec:
1772 * "Specifying a value for internalformat that is not one of the
1773 * above (acceptable) values generates the error INVALID VALUE."
1775 if (err
== GL_INVALID_ENUM
&& _mesa_is_gles(ctx
) && ctx
->Version
< 20)
1776 err
= GL_INVALID_VALUE
;
1778 _mesa_error(ctx
, err
,
1779 "glTexImage%dD(incompatible format = %s, type = %s)",
1780 dimensions
, _mesa_enum_to_string(format
),
1781 _mesa_enum_to_string(type
));
1785 /* Check internalFormat */
1786 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1787 _mesa_error(ctx
, GL_INVALID_VALUE
,
1788 "glTexImage%dD(internalFormat=%s)",
1789 dimensions
, _mesa_enum_to_string(internalFormat
));
1793 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1794 * combinations of format, internalFormat, and type that can be used.
1795 * Formats and types that require additional extensions (e.g., GL_FLOAT
1796 * requires GL_OES_texture_float) are filtered elsewhere.
1798 if (_mesa_is_gles(ctx
) &&
1799 texture_format_error_check_gles(ctx
, format
, type
, internalFormat
,
1800 dimensions
, "glTexImage%dD")) {
1804 /* validate the bound PBO, if any */
1805 if (!_mesa_validate_pbo_source(ctx
, dimensions
, &ctx
->Unpack
,
1806 width
, height
, depth
, format
, type
,
1807 INT_MAX
, pixels
, "glTexImage")) {
1811 /* make sure internal format and format basically agree */
1812 if (!texture_formats_agree(internalFormat
, format
)) {
1813 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1814 "glTexImage%dD(incompatible internalFormat = %s, format = %s)",
1815 dimensions
, _mesa_enum_to_string(internalFormat
),
1816 _mesa_enum_to_string(format
));
1820 /* additional checks for ycbcr textures */
1821 if (internalFormat
== GL_YCBCR_MESA
) {
1822 assert(ctx
->Extensions
.MESA_ycbcr_texture
);
1823 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
1824 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
1826 _mesa_snprintf(message
, sizeof(message
),
1827 "glTexImage%dD(format/type YCBCR mismatch)",
1829 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", message
);
1830 return GL_TRUE
; /* error */
1832 if (target
!= GL_TEXTURE_2D
&&
1833 target
!= GL_PROXY_TEXTURE_2D
&&
1834 target
!= GL_TEXTURE_RECTANGLE_NV
&&
1835 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
1836 _mesa_error(ctx
, GL_INVALID_ENUM
,
1837 "glTexImage%dD(bad target for YCbCr texture)",
1843 _mesa_snprintf(message
, sizeof(message
),
1844 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1845 dimensions
, border
);
1846 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", message
);
1851 /* additional checks for depth textures */
1852 if (!_mesa_legal_texture_base_format_for_target(ctx
, target
, internalFormat
,
1853 dimensions
, "glTexImage"))
1856 /* additional checks for compressed textures */
1857 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
1859 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
, &err
)) {
1860 _mesa_error(ctx
, err
,
1861 "glTexImage%dD(target can't be compressed)", dimensions
);
1864 if (_mesa_format_no_online_compression(ctx
, internalFormat
)) {
1865 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1866 "glTexImage%dD(no compression for format)", dimensions
);
1870 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1871 "glTexImage%dD(border!=0)", dimensions
);
1876 /* additional checks for integer textures */
1877 if ((ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) &&
1878 (_mesa_is_enum_format_integer(format
) !=
1879 _mesa_is_enum_format_integer(internalFormat
))) {
1880 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1881 "glTexImage%dD(integer/non-integer format mismatch)",
1886 if (!mutable_tex_object(ctx
, target
)) {
1887 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1888 "glTexImage%dD(immutable texture)", dimensions
);
1892 /* if we get here, the parameters are OK */
1898 * Error checking for glCompressedTexImage[123]D().
1899 * Note that the width, height and depth values are not fully error checked
1901 * \return GL_TRUE if a error is found, GL_FALSE otherwise
1904 compressed_texture_error_check(struct gl_context
*ctx
, GLint dimensions
,
1905 GLenum target
, GLint level
,
1906 GLenum internalFormat
, GLsizei width
,
1907 GLsizei height
, GLsizei depth
, GLint border
,
1908 GLsizei imageSize
, const GLvoid
*data
)
1910 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1912 GLenum error
= GL_NO_ERROR
;
1913 char *reason
= ""; /* no error */
1915 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
, &error
)) {
1920 /* This will detect any invalid internalFormat value */
1921 if (!_mesa_is_compressed_format(ctx
, internalFormat
)) {
1922 _mesa_error(ctx
, GL_INVALID_ENUM
,
1923 "glCompressedTexImage%dD(internalFormat=%s)",
1924 dimensions
, _mesa_enum_to_string(internalFormat
));
1928 /* validate the bound PBO, if any */
1929 if (!_mesa_validate_pbo_source_compressed(ctx
, dimensions
, &ctx
->Unpack
,
1931 "glCompressedTexImage")) {
1935 switch (internalFormat
) {
1936 case GL_PALETTE4_RGB8_OES
:
1937 case GL_PALETTE4_RGBA8_OES
:
1938 case GL_PALETTE4_R5_G6_B5_OES
:
1939 case GL_PALETTE4_RGBA4_OES
:
1940 case GL_PALETTE4_RGB5_A1_OES
:
1941 case GL_PALETTE8_RGB8_OES
:
1942 case GL_PALETTE8_RGBA8_OES
:
1943 case GL_PALETTE8_R5_G6_B5_OES
:
1944 case GL_PALETTE8_RGBA4_OES
:
1945 case GL_PALETTE8_RGB5_A1_OES
:
1946 /* check level (note that level should be zero or less!) */
1947 if (level
> 0 || level
< -maxLevels
) {
1949 error
= GL_INVALID_VALUE
;
1953 if (dimensions
!= 2) {
1954 reason
= "compressed paletted textures must be 2D";
1955 error
= GL_INVALID_OPERATION
;
1959 /* Figure out the expected texture size (in bytes). This will be
1960 * checked against the actual size later.
1962 expectedSize
= _mesa_cpal_compressed_size(level
, internalFormat
,
1965 /* This is for the benefit of the TestProxyTexImage below. It expects
1966 * level to be non-negative. OES_compressed_paletted_texture uses a
1967 * weird mechanism where the level specified to glCompressedTexImage2D
1968 * is -(n-1) number of levels in the texture, and the data specifies the
1969 * complete mipmap stack. This is done to ensure the palette is the
1970 * same for all levels.
1977 if (level
< 0 || level
>= maxLevels
) {
1979 error
= GL_INVALID_VALUE
;
1983 /* Figure out the expected texture size (in bytes). This will be
1984 * checked against the actual size later.
1986 expectedSize
= compressed_tex_size(width
, height
, depth
, internalFormat
);
1990 /* This should really never fail */
1991 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1992 reason
= "internalFormat";
1993 error
= GL_INVALID_ENUM
;
1997 /* No compressed formats support borders at this time */
1999 reason
= "border != 0";
2000 error
= GL_INVALID_VALUE
;
2004 /* Check for invalid pixel storage modes */
2005 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dimensions
,
2007 "glCompressedTexImage")) {
2011 /* check image size in bytes */
2012 if (expectedSize
!= imageSize
) {
2013 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
2014 * if <imageSize> is not consistent with the format, dimensions, and
2015 * contents of the specified image.
2017 reason
= "imageSize inconsistent with width/height/format";
2018 error
= GL_INVALID_VALUE
;
2022 if (!mutable_tex_object(ctx
, target
)) {
2023 reason
= "immutable texture";
2024 error
= GL_INVALID_OPERATION
;
2031 /* Note: not all error paths exit through here. */
2032 _mesa_error(ctx
, error
, "glCompressedTexImage%dD(%s)",
2033 dimensions
, reason
);
2040 * Test glTexSubImage[123]D() parameters for errors.
2042 * \param ctx GL context.
2043 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2044 * \param target texture target given by the user (already validated)
2045 * \param level image level given by the user.
2046 * \param xoffset sub-image x offset given by the user.
2047 * \param yoffset sub-image y offset given by the user.
2048 * \param zoffset sub-image z offset given by the user.
2049 * \param format pixel data format given by the user.
2050 * \param type pixel data type given by the user.
2051 * \param width image width given by the user.
2052 * \param height image height given by the user.
2053 * \param depth image depth given by the user.
2055 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2057 * Verifies each of the parameters against the constants specified in
2058 * __struct gl_contextRec::Const and the supported extensions, and according
2059 * to the OpenGL specification.
2062 texsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2063 struct gl_texture_object
*texObj
,
2064 GLenum target
, GLint level
,
2065 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2066 GLint width
, GLint height
, GLint depth
,
2067 GLenum format
, GLenum type
, const GLvoid
*pixels
,
2068 bool dsa
, const char *callerName
)
2070 struct gl_texture_image
*texImage
;
2074 /* must be out of memory */
2075 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", callerName
);
2080 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2081 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(level=%d)", callerName
, level
);
2085 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
2087 /* non-existant texture level */
2088 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid texture image)",
2093 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
2094 if (err
!= GL_NO_ERROR
) {
2095 _mesa_error(ctx
, err
,
2096 "%s(incompatible format = %s, type = %s)",
2097 callerName
, _mesa_enum_to_string(format
),
2098 _mesa_enum_to_string(type
));
2102 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2103 * combinations of format, internalFormat, and type that can be used.
2104 * Formats and types that require additional extensions (e.g., GL_FLOAT
2105 * requires GL_OES_texture_float) are filtered elsewhere.
2107 if (_mesa_is_gles(ctx
) &&
2108 texture_format_error_check_gles(ctx
, format
, type
,
2109 texImage
->InternalFormat
,
2110 dimensions
, callerName
)) {
2114 /* validate the bound PBO, if any */
2115 if (!_mesa_validate_pbo_source(ctx
, dimensions
, &ctx
->Unpack
,
2116 width
, height
, depth
, format
, type
,
2117 INT_MAX
, pixels
, callerName
)) {
2121 if (error_check_subtexture_dimensions(ctx
, dimensions
,
2122 texImage
, xoffset
, yoffset
, zoffset
,
2123 width
, height
, depth
, callerName
)) {
2127 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2128 if (_mesa_format_no_online_compression(ctx
, texImage
->InternalFormat
)) {
2129 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2130 "%s(no compression for format)", callerName
);
2135 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
2136 /* both source and dest must be integer-valued, or neither */
2137 if (_mesa_is_format_integer_color(texImage
->TexFormat
) !=
2138 _mesa_is_enum_format_integer(format
)) {
2139 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2140 "%s(integer/non-integer format mismatch)", callerName
);
2150 * Test glCopyTexImage[12]D() parameters for errors.
2152 * \param ctx GL context.
2153 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2154 * \param target texture target given by the user.
2155 * \param level image level given by the user.
2156 * \param internalFormat internal format given by the user.
2157 * \param width image width given by the user.
2158 * \param height image height given by the user.
2159 * \param border texture border.
2161 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2163 * Verifies each of the parameters against the constants specified in
2164 * __struct gl_contextRec::Const and the supported extensions, and according
2165 * to the OpenGL specification.
2168 copytexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
2169 GLenum target
, GLint level
, GLint internalFormat
,
2170 GLint width
, GLint height
, GLint border
)
2173 GLint rb_base_format
;
2174 struct gl_renderbuffer
*rb
;
2175 GLenum rb_internal_format
;
2178 if (!legal_texsubimage_target(ctx
, dimensions
, target
, false)) {
2179 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexImage%uD(target=%s)",
2180 dimensions
, _mesa_enum_to_string(target
));
2185 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2186 _mesa_error(ctx
, GL_INVALID_VALUE
,
2187 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
2191 /* Check that the source buffer is complete */
2192 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2193 if (ctx
->ReadBuffer
->_Status
== 0) {
2194 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2196 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2197 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2198 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
2202 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2203 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2204 "glCopyTexImage%dD(multisample FBO)", dimensions
);
2210 if (border
< 0 || border
> 1 ||
2211 ((ctx
->API
!= API_OPENGL_COMPAT
||
2212 target
== GL_TEXTURE_RECTANGLE_NV
||
2213 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
2214 _mesa_error(ctx
, GL_INVALID_VALUE
,
2215 "glCopyTexImage%dD(border=%d)", dimensions
, border
);
2219 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2222 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
2223 switch (internalFormat
) {
2228 case GL_LUMINANCE_ALPHA
:
2231 _mesa_error(ctx
, GL_INVALID_ENUM
,
2232 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2233 _mesa_enum_to_string(internalFormat
));
2238 * Section 8.6 (Alternate Texture Image Specification Commands) of the
2239 * OpenGL 4.5 (Compatibility Profile) spec says:
2241 * "Parameters level, internalformat, and border are specified using
2242 * the same values, with the same meanings, as the corresponding
2243 * arguments of TexImage2D, except that internalformat may not be
2244 * specified as 1, 2, 3, or 4."
2246 if (internalFormat
>= 1 && internalFormat
<= 4) {
2247 _mesa_error(ctx
, GL_INVALID_ENUM
,
2248 "glCopyTexImage%dD(internalFormat=%d)", dimensions
,
2254 baseFormat
= _mesa_base_tex_format(ctx
, internalFormat
);
2255 if (baseFormat
< 0) {
2256 _mesa_error(ctx
, GL_INVALID_ENUM
,
2257 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2258 _mesa_enum_to_string(internalFormat
));
2262 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, internalFormat
);
2264 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2265 "glCopyTexImage%dD(read buffer)", dimensions
);
2269 rb_internal_format
= rb
->InternalFormat
;
2270 rb_base_format
= _mesa_base_tex_format(ctx
, rb
->InternalFormat
);
2271 if (_mesa_is_color_format(internalFormat
)) {
2272 if (rb_base_format
< 0) {
2273 _mesa_error(ctx
, GL_INVALID_VALUE
,
2274 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2275 _mesa_enum_to_string(internalFormat
));
2280 if (_mesa_is_gles(ctx
)) {
2282 if (_mesa_base_format_component_count(baseFormat
) >
2283 _mesa_base_format_component_count(rb_base_format
)) {
2286 if (baseFormat
== GL_DEPTH_COMPONENT
||
2287 baseFormat
== GL_DEPTH_STENCIL
||
2288 rb_base_format
== GL_DEPTH_COMPONENT
||
2289 rb_base_format
== GL_DEPTH_STENCIL
||
2290 ((baseFormat
== GL_LUMINANCE_ALPHA
||
2291 baseFormat
== GL_ALPHA
) &&
2292 rb_base_format
!= GL_RGBA
) ||
2293 internalFormat
== GL_RGB9_E5
) {
2296 if (internalFormat
== GL_RGB9_E5
) {
2300 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2301 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2302 _mesa_enum_to_string(internalFormat
));
2307 if (_mesa_is_gles3(ctx
)) {
2308 bool rb_is_srgb
= false;
2309 bool dst_is_srgb
= false;
2311 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&&
2312 _mesa_get_format_color_encoding(rb
->Format
) == GL_SRGB
) {
2316 if (_mesa_get_linear_internalformat(internalFormat
) != internalFormat
) {
2320 if (rb_is_srgb
!= dst_is_srgb
) {
2321 /* Page 137 (page 149 of the PDF) in section 3.8.5 of the
2322 * OpenGLES 3.0.0 spec says:
2324 * "The error INVALID_OPERATION is also generated if the
2325 * value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the
2326 * framebuffer attachment corresponding to the read buffer
2327 * is LINEAR (see section 6.1.13) and internalformat is
2328 * one of the sRGB formats described in section 3.8.16, or
2329 * if the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING is
2330 * SRGB and internalformat is not one of the sRGB formats."
2332 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2333 "glCopyTexImage%dD(srgb usage mismatch)", dimensions
);
2337 /* Page 139, Table 3.15 of OpenGL ES 3.0 spec does not define ReadPixels
2338 * types for SNORM formats. Also, conversion to SNORM formats is not
2339 * allowed by Table 3.2 on Page 110.
2341 if (_mesa_is_enum_format_snorm(internalFormat
)) {
2342 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2343 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2344 _mesa_enum_to_string(internalFormat
));
2349 if (!_mesa_source_buffer_exists(ctx
, baseFormat
)) {
2350 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2351 "glCopyTexImage%dD(missing readbuffer)", dimensions
);
2355 /* From the EXT_texture_integer spec:
2357 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2358 * if the texture internalformat is an integer format and the read color
2359 * buffer is not an integer format, or if the internalformat is not an
2360 * integer format and the read color buffer is an integer format."
2362 if (_mesa_is_color_format(internalFormat
)) {
2363 bool is_int
= _mesa_is_enum_format_integer(internalFormat
);
2364 bool is_rbint
= _mesa_is_enum_format_integer(rb_internal_format
);
2365 bool is_unorm
= _mesa_is_enum_format_unorm(internalFormat
);
2366 bool is_rbunorm
= _mesa_is_enum_format_unorm(rb_internal_format
);
2367 if (is_int
|| is_rbint
) {
2368 if (is_int
!= is_rbint
) {
2369 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2370 "glCopyTexImage%dD(integer vs non-integer)", dimensions
);
2372 } else if (_mesa_is_gles(ctx
) &&
2373 _mesa_is_enum_format_unsigned_int(internalFormat
) !=
2374 _mesa_is_enum_format_unsigned_int(rb_internal_format
)) {
2375 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2376 "glCopyTexImage%dD(signed vs unsigned integer)", dimensions
);
2381 /* From page 138 of OpenGL ES 3.0 spec:
2382 * "The error INVALID_OPERATION is generated if floating-point RGBA
2383 * data is required; if signed integer RGBA data is required and the
2384 * format of the current color buffer is not signed integer; if
2385 * unsigned integer RGBA data is required and the format of the
2386 * current color buffer is not unsigned integer; or if fixed-point
2387 * RGBA data is required and the format of the current color buffer
2388 * is not fixed-point.
2390 if (_mesa_is_gles(ctx
) && is_unorm
!= is_rbunorm
)
2391 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2392 "glCopyTexImage%dD(unorm vs non-unorm)", dimensions
);
2395 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
2397 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
, &err
)) {
2398 _mesa_error(ctx
, err
,
2399 "glCopyTexImage%dD(target can't be compressed)", dimensions
);
2402 if (_mesa_format_no_online_compression(ctx
, internalFormat
)) {
2403 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2404 "glCopyTexImage%dD(no compression for format)", dimensions
);
2408 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2409 "glCopyTexImage%dD(border!=0)", dimensions
);
2414 if (!mutable_tex_object(ctx
, target
)) {
2415 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2416 "glCopyTexImage%dD(immutable texture)", dimensions
);
2420 /* if we get here, the parameters are OK */
2426 * Test glCopyTexSubImage[12]D() parameters for errors.
2427 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2430 copytexsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2431 const struct gl_texture_object
*texObj
,
2432 GLenum target
, GLint level
,
2433 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2434 GLint width
, GLint height
, const char *caller
)
2436 struct gl_texture_image
*texImage
;
2438 /* Check that the source buffer is complete */
2439 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2440 if (ctx
->ReadBuffer
->_Status
== 0) {
2441 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2443 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2444 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2445 "%s(invalid readbuffer)", caller
);
2449 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2450 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2451 "%s(multisample FBO)", caller
);
2457 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2458 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(level=%d)", caller
, level
);
2462 /* Get dest image pointers */
2464 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", caller
);
2468 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
2470 /* destination image does not exist */
2471 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2472 "%s(invalid texture image)", caller
);
2476 if (error_check_subtexture_dimensions(ctx
, dimensions
, texImage
,
2477 xoffset
, yoffset
, zoffset
,
2478 width
, height
, 1, caller
)) {
2482 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2483 if (_mesa_format_no_online_compression(ctx
, texImage
->InternalFormat
)) {
2484 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2485 "%s(no compression for format)", caller
);
2490 if (texImage
->InternalFormat
== GL_YCBCR_MESA
) {
2491 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s()", caller
);
2495 if (!_mesa_source_buffer_exists(ctx
, texImage
->_BaseFormat
)) {
2496 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2497 "%s(missing readbuffer, format=%s)", caller
,
2498 _mesa_enum_to_string(texImage
->_BaseFormat
));
2502 /* From the EXT_texture_integer spec:
2504 * "INVALID_OPERATION is generated by CopyTexImage* and
2505 * CopyTexSubImage* if the texture internalformat is an integer format
2506 * and the read color buffer is not an integer format, or if the
2507 * internalformat is not an integer format and the read color buffer
2508 * is an integer format."
2510 if (_mesa_is_color_format(texImage
->InternalFormat
)) {
2511 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
2513 if (_mesa_is_format_integer_color(rb
->Format
) !=
2514 _mesa_is_format_integer_color(texImage
->TexFormat
)) {
2515 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2516 "%s(integer vs non-integer)", caller
);
2521 /* if we get here, the parameters are OK */
2526 /** Callback info for walking over FBO hash table */
2529 struct gl_context
*ctx
;
2530 struct gl_texture_object
*texObj
;
2536 * Check render to texture callback. Called from _mesa_HashWalk().
2539 check_rtt_cb(GLuint key
, void *data
, void *userData
)
2541 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
2542 const struct cb_info
*info
= (struct cb_info
*) userData
;
2543 struct gl_context
*ctx
= info
->ctx
;
2544 const struct gl_texture_object
*texObj
= info
->texObj
;
2545 const GLuint level
= info
->level
, face
= info
->face
;
2547 /* If this is a user-created FBO */
2548 if (_mesa_is_user_fbo(fb
)) {
2550 /* check if any of the FBO's attachments point to 'texObj' */
2551 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2552 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2553 if (att
->Type
== GL_TEXTURE
&&
2554 att
->Texture
== texObj
&&
2555 att
->TextureLevel
== level
&&
2556 att
->CubeMapFace
== face
) {
2557 _mesa_update_texture_renderbuffer(ctx
, ctx
->DrawBuffer
, att
);
2558 assert(att
->Renderbuffer
->TexImage
);
2559 /* Mark fb status as indeterminate to force re-validation */
2568 * When a texture image is specified we have to check if it's bound to
2569 * any framebuffer objects (render to texture) in order to detect changes
2570 * in size or format since that effects FBO completeness.
2571 * Any FBOs rendering into the texture must be re-validated.
2574 _mesa_update_fbo_texture(struct gl_context
*ctx
,
2575 struct gl_texture_object
*texObj
,
2576 GLuint face
, GLuint level
)
2578 /* Only check this texture if it's been marked as RenderToTexture */
2579 if (texObj
->_RenderToTexture
) {
2580 struct cb_info info
;
2582 info
.texObj
= texObj
;
2585 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, check_rtt_cb
, &info
);
2591 * If the texture object's GenerateMipmap flag is set and we've
2592 * changed the texture base level image, regenerate the rest of the
2593 * mipmap levels now.
2596 check_gen_mipmap(struct gl_context
*ctx
, GLenum target
,
2597 struct gl_texture_object
*texObj
, GLint level
)
2599 if (texObj
->GenerateMipmap
&&
2600 level
== texObj
->BaseLevel
&&
2601 level
< texObj
->MaxLevel
) {
2602 assert(ctx
->Driver
.GenerateMipmap
);
2603 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2608 /** Debug helper: override the user-requested internal format */
2610 override_internal_format(GLenum internalFormat
, GLint width
, GLint height
)
2613 if (internalFormat
== GL_RGBA16F_ARB
||
2614 internalFormat
== GL_RGBA32F_ARB
) {
2615 printf("Convert rgba float tex to int %d x %d\n", width
, height
);
2618 else if (internalFormat
== GL_RGB16F_ARB
||
2619 internalFormat
== GL_RGB32F_ARB
) {
2620 printf("Convert rgb float tex to int %d x %d\n", width
, height
);
2623 else if (internalFormat
== GL_LUMINANCE_ALPHA16F_ARB
||
2624 internalFormat
== GL_LUMINANCE_ALPHA32F_ARB
) {
2625 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2626 return GL_LUMINANCE_ALPHA
;
2628 else if (internalFormat
== GL_LUMINANCE16F_ARB
||
2629 internalFormat
== GL_LUMINANCE32F_ARB
) {
2630 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2631 return GL_LUMINANCE
;
2633 else if (internalFormat
== GL_ALPHA16F_ARB
||
2634 internalFormat
== GL_ALPHA32F_ARB
) {
2635 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2639 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2640 internalFormat = GL_RGBA;
2644 return internalFormat
;
2647 return internalFormat
;
2653 * Choose the actual hardware format for a texture image.
2654 * Try to use the same format as the previous image level when possible.
2655 * Otherwise, ask the driver for the best format.
2656 * It's important to try to choose a consistant format for all levels
2657 * for efficient texture memory layout/allocation. In particular, this
2658 * comes up during automatic mipmap generation.
2661 _mesa_choose_texture_format(struct gl_context
*ctx
,
2662 struct gl_texture_object
*texObj
,
2663 GLenum target
, GLint level
,
2664 GLenum internalFormat
, GLenum format
, GLenum type
)
2668 /* see if we've already chosen a format for the previous level */
2670 struct gl_texture_image
*prevImage
=
2671 _mesa_select_tex_image(texObj
, target
, level
- 1);
2672 /* See if the prev level is defined and has an internal format which
2673 * matches the new internal format.
2676 prevImage
->Width
> 0 &&
2677 prevImage
->InternalFormat
== internalFormat
) {
2678 /* use the same format */
2679 assert(prevImage
->TexFormat
!= MESA_FORMAT_NONE
);
2680 return prevImage
->TexFormat
;
2684 /* If the application requested compression to an S3TC format but we don't
2685 * have the DXTn library, force a generic compressed format instead.
2687 if (internalFormat
!= format
&& format
!= GL_NONE
) {
2688 const GLenum before
= internalFormat
;
2690 switch (internalFormat
) {
2691 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
2692 if (!ctx
->Mesa_DXTn
)
2693 internalFormat
= GL_COMPRESSED_RGB
;
2695 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
2696 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
2697 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
2698 if (!ctx
->Mesa_DXTn
)
2699 internalFormat
= GL_COMPRESSED_RGBA
;
2705 if (before
!= internalFormat
) {
2707 "DXT compression requested (%s), "
2708 "but libtxc_dxtn library not installed. Using %s "
2710 _mesa_enum_to_string(before
),
2711 _mesa_enum_to_string(internalFormat
));
2715 /* choose format from scratch */
2716 f
= ctx
->Driver
.ChooseTextureFormat(ctx
, target
, internalFormat
,
2718 assert(f
!= MESA_FORMAT_NONE
);
2724 * Adjust pixel unpack params and image dimensions to strip off the
2725 * one-pixel texture border.
2727 * Gallium and intel don't support texture borders. They've seldem been used
2728 * and seldom been implemented correctly anyway.
2730 * \param unpackNew returns the new pixel unpack parameters
2733 strip_texture_border(GLenum target
,
2734 GLint
*width
, GLint
*height
, GLint
*depth
,
2735 const struct gl_pixelstore_attrib
*unpack
,
2736 struct gl_pixelstore_attrib
*unpackNew
)
2742 *unpackNew
= *unpack
;
2744 if (unpackNew
->RowLength
== 0)
2745 unpackNew
->RowLength
= *width
;
2747 if (unpackNew
->ImageHeight
== 0)
2748 unpackNew
->ImageHeight
= *height
;
2750 assert(*width
>= 3);
2751 unpackNew
->SkipPixels
++; /* skip the border */
2752 *width
= *width
- 2; /* reduce the width by two border pixels */
2754 /* The min height of a texture with a border is 3 */
2755 if (*height
>= 3 && target
!= GL_TEXTURE_1D_ARRAY
) {
2756 unpackNew
->SkipRows
++; /* skip the border */
2757 *height
= *height
- 2; /* reduce the height by two border pixels */
2761 target
!= GL_TEXTURE_2D_ARRAY
&&
2762 target
!= GL_TEXTURE_CUBE_MAP_ARRAY
) {
2763 unpackNew
->SkipImages
++; /* skip the border */
2764 *depth
= *depth
- 2; /* reduce the depth by two border pixels */
2770 * Common code to implement all the glTexImage1D/2D/3D functions
2771 * as well as glCompressedTexImage1D/2D/3D.
2772 * \param compressed only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
2773 * \param format the user's image format (only used if !compressed)
2774 * \param type the user's image type (only used if !compressed)
2775 * \param imageSize only used for glCompressedTexImage1D/2D/3D calls.
2778 teximage(struct gl_context
*ctx
, GLboolean compressed
, GLuint dims
,
2779 GLenum target
, GLint level
, GLint internalFormat
,
2780 GLsizei width
, GLsizei height
, GLsizei depth
,
2781 GLint border
, GLenum format
, GLenum type
,
2782 GLsizei imageSize
, const GLvoid
*pixels
)
2784 const char *func
= compressed
? "glCompressedTexImage" : "glTexImage";
2785 struct gl_pixelstore_attrib unpack_no_border
;
2786 const struct gl_pixelstore_attrib
*unpack
= &ctx
->Unpack
;
2787 struct gl_texture_object
*texObj
;
2788 mesa_format texFormat
;
2789 GLboolean dimensionsOK
, sizeOK
;
2791 FLUSH_VERTICES(ctx
, 0);
2793 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
)) {
2796 "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
2798 _mesa_enum_to_string(target
), level
,
2799 _mesa_enum_to_string(internalFormat
),
2800 width
, height
, depth
, border
, pixels
);
2803 "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2805 _mesa_enum_to_string(target
), level
,
2806 _mesa_enum_to_string(internalFormat
),
2807 width
, height
, depth
, border
,
2808 _mesa_enum_to_string(format
),
2809 _mesa_enum_to_string(type
), pixels
);
2812 internalFormat
= override_internal_format(internalFormat
, width
, height
);
2814 /* target error checking */
2815 if (!legal_teximage_target(ctx
, dims
, target
)) {
2816 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s%uD(target=%s)",
2817 func
, dims
, _mesa_enum_to_string(target
));
2821 /* general error checking */
2823 if (compressed_texture_error_check(ctx
, dims
, target
, level
,
2825 width
, height
, depth
,
2826 border
, imageSize
, pixels
))
2830 if (texture_error_check(ctx
, dims
, target
, level
, internalFormat
,
2831 format
, type
, width
, height
, depth
, border
,
2836 /* Here we convert a cpal compressed image into a regular glTexImage2D
2837 * call by decompressing the texture. If we really want to support cpal
2838 * textures in any driver this would have to be changed.
2840 if (ctx
->API
== API_OPENGLES
&& compressed
&& dims
== 2) {
2841 switch (internalFormat
) {
2842 case GL_PALETTE4_RGB8_OES
:
2843 case GL_PALETTE4_RGBA8_OES
:
2844 case GL_PALETTE4_R5_G6_B5_OES
:
2845 case GL_PALETTE4_RGBA4_OES
:
2846 case GL_PALETTE4_RGB5_A1_OES
:
2847 case GL_PALETTE8_RGB8_OES
:
2848 case GL_PALETTE8_RGBA8_OES
:
2849 case GL_PALETTE8_R5_G6_B5_OES
:
2850 case GL_PALETTE8_RGBA4_OES
:
2851 case GL_PALETTE8_RGB5_A1_OES
:
2852 _mesa_cpal_compressed_teximage2d(target
, level
, internalFormat
,
2853 width
, height
, imageSize
, pixels
);
2858 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2862 /* For glCompressedTexImage() the driver has no choice about the
2863 * texture format since we'll never transcode the user's compressed
2864 * image data. The internalFormat was error checked earlier.
2866 texFormat
= _mesa_glenum_to_compressed_format(internalFormat
);
2869 /* In case of HALF_FLOAT_OES or FLOAT_OES, find corresponding sized
2870 * internal floating point format for the given base format.
2872 if (_mesa_is_gles(ctx
) && format
== internalFormat
) {
2873 if (type
== GL_FLOAT
) {
2874 texObj
->_IsFloat
= GL_TRUE
;
2875 } else if (type
== GL_HALF_FLOAT_OES
|| type
== GL_HALF_FLOAT
) {
2876 texObj
->_IsHalfFloat
= GL_TRUE
;
2879 internalFormat
= adjust_for_oes_float_texture(format
, type
);
2882 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
2883 internalFormat
, format
, type
);
2886 assert(texFormat
!= MESA_FORMAT_NONE
);
2888 /* check that width, height, depth are legal for the mipmap level */
2889 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, level
, width
,
2890 height
, depth
, border
);
2892 /* check that the texture won't take too much memory, etc */
2893 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target(target
),
2895 width
, height
, depth
, border
);
2897 if (_mesa_is_proxy_texture(target
)) {
2898 /* Proxy texture: just clear or set state depending on error checking */
2899 struct gl_texture_image
*texImage
=
2900 get_proxy_tex_image(ctx
, target
, level
);
2903 return; /* GL_OUT_OF_MEMORY already recorded */
2905 if (dimensionsOK
&& sizeOK
) {
2906 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, depth
,
2907 border
, internalFormat
, texFormat
);
2910 clear_teximage_fields(texImage
);
2914 /* non-proxy target */
2915 const GLuint face
= _mesa_tex_target_to_face(target
);
2916 struct gl_texture_image
*texImage
;
2918 if (!dimensionsOK
) {
2919 _mesa_error(ctx
, GL_INVALID_VALUE
,
2920 "%s%uD(invalid width or height or depth)",
2926 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
2927 "%s%uD(image too large: %d x %d x %d, %s format)",
2928 func
, dims
, width
, height
, depth
,
2929 _mesa_enum_to_string(internalFormat
));
2933 /* Allow a hardware driver to just strip out the border, to provide
2934 * reliable but slightly incorrect hardware rendering instead of
2935 * rarely-tested software fallback rendering.
2937 if (border
&& ctx
->Const
.StripTextureBorder
) {
2938 strip_texture_border(target
, &width
, &height
, &depth
, unpack
,
2941 unpack
= &unpack_no_border
;
2944 if (ctx
->NewState
& _NEW_PIXEL
)
2945 _mesa_update_state(ctx
);
2947 _mesa_lock_texture(ctx
, texObj
);
2949 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2952 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s%uD", func
, dims
);
2955 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
2957 _mesa_init_teximage_fields(ctx
, texImage
,
2958 width
, height
, depth
,
2959 border
, internalFormat
, texFormat
);
2961 /* Give the texture to the driver. <pixels> may be null. */
2962 if (width
> 0 && height
> 0 && depth
> 0) {
2964 ctx
->Driver
.CompressedTexImage(ctx
, dims
, texImage
,
2968 ctx
->Driver
.TexImage(ctx
, dims
, texImage
, format
,
2969 type
, pixels
, unpack
);
2973 check_gen_mipmap(ctx
, target
, texObj
, level
);
2975 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
2977 _mesa_dirty_texobj(ctx
, texObj
);
2980 _mesa_unlock_texture(ctx
, texObj
);
2987 * Called from the API. Note that width includes the border.
2990 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
2991 GLsizei width
, GLint border
, GLenum format
,
2992 GLenum type
, const GLvoid
*pixels
)
2994 GET_CURRENT_CONTEXT(ctx
);
2995 teximage(ctx
, GL_FALSE
, 1, target
, level
, internalFormat
, width
, 1, 1,
2996 border
, format
, type
, 0, pixels
);
3001 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
3002 GLsizei width
, GLsizei height
, GLint border
,
3003 GLenum format
, GLenum type
,
3004 const GLvoid
*pixels
)
3006 GET_CURRENT_CONTEXT(ctx
);
3007 teximage(ctx
, GL_FALSE
, 2, target
, level
, internalFormat
, width
, height
, 1,
3008 border
, format
, type
, 0, pixels
);
3013 * Called by the API or display list executor.
3014 * Note that width and height include the border.
3017 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
3018 GLsizei width
, GLsizei height
, GLsizei depth
,
3019 GLint border
, GLenum format
, GLenum type
,
3020 const GLvoid
*pixels
)
3022 GET_CURRENT_CONTEXT(ctx
);
3023 teximage(ctx
, GL_FALSE
, 3, target
, level
, internalFormat
,
3024 width
, height
, depth
,
3025 border
, format
, type
, 0, pixels
);
3030 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
3031 GLsizei width
, GLsizei height
, GLsizei depth
,
3032 GLint border
, GLenum format
, GLenum type
,
3033 const GLvoid
*pixels
)
3035 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
3036 depth
, border
, format
, type
, pixels
);
3041 _mesa_EGLImageTargetTexture2DOES (GLenum target
, GLeglImageOES image
)
3043 struct gl_texture_object
*texObj
;
3044 struct gl_texture_image
*texImage
;
3046 GET_CURRENT_CONTEXT(ctx
);
3047 FLUSH_VERTICES(ctx
, 0);
3051 valid_target
= ctx
->Extensions
.OES_EGL_image
;
3053 case GL_TEXTURE_EXTERNAL_OES
:
3055 _mesa_is_gles(ctx
) ? ctx
->Extensions
.OES_EGL_image_external
: false;
3058 valid_target
= false;
3062 if (!valid_target
) {
3063 _mesa_error(ctx
, GL_INVALID_ENUM
,
3064 "glEGLImageTargetTexture2D(target=%d)", target
);
3069 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3070 "glEGLImageTargetTexture2D(image=%p)", image
);
3074 if (ctx
->NewState
& _NEW_PIXEL
)
3075 _mesa_update_state(ctx
);
3077 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3081 _mesa_lock_texture(ctx
, texObj
);
3083 if (texObj
->Immutable
) {
3084 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3085 "glEGLImageTargetTexture2D(texture is immutable)");
3086 _mesa_unlock_texture(ctx
, texObj
);
3090 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
3092 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glEGLImageTargetTexture2D");
3094 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3096 ctx
->Driver
.EGLImageTargetTexture2D(ctx
, target
,
3097 texObj
, texImage
, image
);
3099 _mesa_dirty_texobj(ctx
, texObj
);
3101 _mesa_unlock_texture(ctx
, texObj
);
3106 * Helper that implements the glTexSubImage1/2/3D()
3107 * and glTextureSubImage1/2/3D() functions.
3110 _mesa_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
3111 struct gl_texture_object
*texObj
,
3112 struct gl_texture_image
*texImage
,
3113 GLenum target
, GLint level
,
3114 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3115 GLsizei width
, GLsizei height
, GLsizei depth
,
3116 GLenum format
, GLenum type
, const GLvoid
*pixels
,
3119 FLUSH_VERTICES(ctx
, 0);
3121 if (ctx
->NewState
& _NEW_PIXEL
)
3122 _mesa_update_state(ctx
);
3124 _mesa_lock_texture(ctx
, texObj
);
3126 if (width
> 0 && height
> 0 && depth
> 0) {
3127 /* If we have a border, offset=-1 is legal. Bias by border width. */
3130 if (target
!= GL_TEXTURE_2D_ARRAY
)
3131 zoffset
+= texImage
->Border
;
3134 if (target
!= GL_TEXTURE_1D_ARRAY
)
3135 yoffset
+= texImage
->Border
;
3138 xoffset
+= texImage
->Border
;
3141 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3142 xoffset
, yoffset
, zoffset
,
3143 width
, height
, depth
,
3144 format
, type
, pixels
, &ctx
->Unpack
);
3146 check_gen_mipmap(ctx
, target
, texObj
, level
);
3148 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3149 * the texel data, not the texture format, size, etc.
3153 _mesa_unlock_texture(ctx
, texObj
);
3157 * Implement all the glTexSubImage1/2/3D() functions.
3158 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3161 texsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
3162 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3163 GLsizei width
, GLsizei height
, GLsizei depth
,
3164 GLenum format
, GLenum type
, const GLvoid
*pixels
,
3165 const char *callerName
)
3167 struct gl_texture_object
*texObj
;
3168 struct gl_texture_image
*texImage
;
3170 /* check target (proxies not allowed) */
3171 if (!legal_texsubimage_target(ctx
, dims
, target
, false)) {
3172 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage%uD(target=%s)",
3173 dims
, _mesa_enum_to_string(target
));
3177 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3181 if (texsubimage_error_check(ctx
, dims
, texObj
, target
, level
,
3182 xoffset
, yoffset
, zoffset
,
3183 width
, height
, depth
, format
, type
,
3184 pixels
, false, callerName
)) {
3185 return; /* error was detected */
3188 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
3189 /* texsubimage_error_check ensures that texImage is not NULL */
3191 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3192 _mesa_debug(ctx
, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3194 _mesa_enum_to_string(target
), level
,
3195 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3196 _mesa_enum_to_string(format
),
3197 _mesa_enum_to_string(type
), pixels
);
3199 _mesa_texture_sub_image(ctx
, dims
, texObj
, texImage
, target
, level
,
3200 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3201 format
, type
, pixels
, false);
3206 * Implement all the glTextureSubImage1/2/3D() functions.
3207 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3210 texturesubimage(struct gl_context
*ctx
, GLuint dims
,
3211 GLuint texture
, GLint level
,
3212 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3213 GLsizei width
, GLsizei height
, GLsizei depth
,
3214 GLenum format
, GLenum type
, const GLvoid
*pixels
,
3215 const char *callerName
)
3217 struct gl_texture_object
*texObj
;
3218 struct gl_texture_image
*texImage
;
3221 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3223 "glTextureSubImage%uD %d %d %d %d %d %d %d %d %s %s %p\n",
3224 dims
, texture
, level
,
3225 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3226 _mesa_enum_to_string(format
),
3227 _mesa_enum_to_string(type
), pixels
);
3229 /* Get the texture object by Name. */
3230 texObj
= _mesa_lookup_texture(ctx
, texture
);
3232 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTextureSubImage%uD(texture)",
3237 /* check target (proxies not allowed) */
3238 if (!legal_texsubimage_target(ctx
, dims
, texObj
->Target
, true)) {
3239 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target=%s)",
3240 callerName
, _mesa_enum_to_string(texObj
->Target
));
3244 if (texsubimage_error_check(ctx
, dims
, texObj
, texObj
->Target
, level
,
3245 xoffset
, yoffset
, zoffset
,
3246 width
, height
, depth
, format
, type
,
3247 pixels
, true, callerName
)) {
3248 return; /* error was detected */
3252 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
3253 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
3257 * What do we do if the user created a texture with the following code
3258 * and then called this function with its handle?
3261 * glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &tex);
3262 * glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
3263 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, ...);
3264 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, ...);
3265 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, ...);
3266 * // Note: GL_TEXTURE_CUBE_MAP_NEGATIVE_Y not set, or given the
3267 * // wrong format, or given the wrong size, etc.
3268 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, ...);
3269 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, ...);
3271 * A bug has been filed against the spec for this case. In the
3272 * meantime, we will check for cube completeness.
3274 * According to Section 8.17 Texture Completeness in the OpenGL 4.5
3275 * Core Profile spec (30.10.2014):
3276 * "[A] cube map texture is cube complete if the
3277 * following conditions all hold true: The [base level] texture
3278 * images of each of the six cube map faces have identical, positive,
3279 * and square dimensions. The [base level] images were each specified
3280 * with the same internal format."
3282 * It seems reasonable to check for cube completeness of an arbitrary
3283 * level here so that the image data has a consistent format and size.
3285 if (!_mesa_cube_level_complete(texObj
, level
)) {
3286 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3287 "glTextureSubImage%uD(cube map incomplete)",
3292 imageStride
= _mesa_image_image_stride(&ctx
->Unpack
, width
, height
,
3294 /* Copy in each face. */
3295 for (i
= zoffset
; i
< zoffset
+ depth
; ++i
) {
3296 texImage
= texObj
->Image
[i
][level
];
3299 _mesa_texture_sub_image(ctx
, 3, texObj
, texImage
, texObj
->Target
,
3300 level
, xoffset
, yoffset
, 0,
3301 width
, height
, 1, format
,
3302 type
, pixels
, true);
3303 pixels
= (GLubyte
*) pixels
+ imageStride
;
3307 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
3310 _mesa_texture_sub_image(ctx
, dims
, texObj
, texImage
, texObj
->Target
,
3311 level
, xoffset
, yoffset
, zoffset
,
3312 width
, height
, depth
, format
,
3313 type
, pixels
, true);
3319 _mesa_TexSubImage1D( GLenum target
, GLint level
,
3320 GLint xoffset
, GLsizei width
,
3321 GLenum format
, GLenum type
,
3322 const GLvoid
*pixels
)
3324 GET_CURRENT_CONTEXT(ctx
);
3325 texsubimage(ctx
, 1, target
, level
,
3328 format
, type
, pixels
, "glTexSubImage1D");
3333 _mesa_TexSubImage2D( GLenum target
, GLint level
,
3334 GLint xoffset
, GLint yoffset
,
3335 GLsizei width
, GLsizei height
,
3336 GLenum format
, GLenum type
,
3337 const GLvoid
*pixels
)
3339 GET_CURRENT_CONTEXT(ctx
);
3340 texsubimage(ctx
, 2, target
, level
,
3341 xoffset
, yoffset
, 0,
3343 format
, type
, pixels
, "glTexSubImage2D");
3349 _mesa_TexSubImage3D( GLenum target
, GLint level
,
3350 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3351 GLsizei width
, GLsizei height
, GLsizei depth
,
3352 GLenum format
, GLenum type
,
3353 const GLvoid
*pixels
)
3355 GET_CURRENT_CONTEXT(ctx
);
3356 texsubimage(ctx
, 3, target
, level
,
3357 xoffset
, yoffset
, zoffset
,
3358 width
, height
, depth
,
3359 format
, type
, pixels
, "glTexSubImage3D");
3363 _mesa_TextureSubImage1D(GLuint texture
, GLint level
,
3364 GLint xoffset
, GLsizei width
,
3365 GLenum format
, GLenum type
,
3366 const GLvoid
*pixels
)
3368 GET_CURRENT_CONTEXT(ctx
);
3369 texturesubimage(ctx
, 1, texture
, level
,
3372 format
, type
, pixels
, "glTextureSubImage1D");
3377 _mesa_TextureSubImage2D(GLuint texture
, GLint level
,
3378 GLint xoffset
, GLint yoffset
,
3379 GLsizei width
, GLsizei height
,
3380 GLenum format
, GLenum type
,
3381 const GLvoid
*pixels
)
3383 GET_CURRENT_CONTEXT(ctx
);
3384 texturesubimage(ctx
, 2, texture
, level
,
3385 xoffset
, yoffset
, 0,
3387 format
, type
, pixels
, "glTextureSubImage2D");
3392 _mesa_TextureSubImage3D(GLuint texture
, GLint level
,
3393 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3394 GLsizei width
, GLsizei height
, GLsizei depth
,
3395 GLenum format
, GLenum type
,
3396 const GLvoid
*pixels
)
3398 GET_CURRENT_CONTEXT(ctx
);
3399 texturesubimage(ctx
, 3, texture
, level
,
3400 xoffset
, yoffset
, zoffset
,
3401 width
, height
, depth
,
3402 format
, type
, pixels
, "glTextureSubImage3D");
3407 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3408 * from. This depends on whether the texture contains color or depth values.
3410 static struct gl_renderbuffer
*
3411 get_copy_tex_image_source(struct gl_context
*ctx
, mesa_format texFormat
)
3413 if (_mesa_get_format_bits(texFormat
, GL_DEPTH_BITS
) > 0) {
3414 /* reading from depth/stencil buffer */
3415 return ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
3418 /* copying from color buffer */
3419 return ctx
->ReadBuffer
->_ColorReadBuffer
;
3424 copytexsubimage_by_slice(struct gl_context
*ctx
,
3425 struct gl_texture_image
*texImage
,
3427 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3428 struct gl_renderbuffer
*rb
,
3430 GLsizei width
, GLsizei height
)
3432 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
3435 /* For 1D arrays, we copy each scanline of the source rectangle into the
3438 assert(zoffset
== 0);
3440 for (slice
= 0; slice
< height
; slice
++) {
3441 assert(yoffset
+ slice
< texImage
->Height
);
3442 ctx
->Driver
.CopyTexSubImage(ctx
, 2, texImage
,
3443 xoffset
, 0, yoffset
+ slice
,
3444 rb
, x
, y
+ slice
, width
, 1);
3447 ctx
->Driver
.CopyTexSubImage(ctx
, dims
, texImage
,
3448 xoffset
, yoffset
, zoffset
,
3449 rb
, x
, y
, width
, height
);
3454 formats_differ_in_component_sizes(mesa_format f1
, mesa_format f2
)
3456 GLint f1_r_bits
= _mesa_get_format_bits(f1
, GL_RED_BITS
);
3457 GLint f1_g_bits
= _mesa_get_format_bits(f1
, GL_GREEN_BITS
);
3458 GLint f1_b_bits
= _mesa_get_format_bits(f1
, GL_BLUE_BITS
);
3459 GLint f1_a_bits
= _mesa_get_format_bits(f1
, GL_ALPHA_BITS
);
3461 GLint f2_r_bits
= _mesa_get_format_bits(f2
, GL_RED_BITS
);
3462 GLint f2_g_bits
= _mesa_get_format_bits(f2
, GL_GREEN_BITS
);
3463 GLint f2_b_bits
= _mesa_get_format_bits(f2
, GL_BLUE_BITS
);
3464 GLint f2_a_bits
= _mesa_get_format_bits(f2
, GL_ALPHA_BITS
);
3466 if ((f1_r_bits
&& f2_r_bits
&& f1_r_bits
!= f2_r_bits
)
3467 || (f1_g_bits
&& f2_g_bits
&& f1_g_bits
!= f2_g_bits
)
3468 || (f1_b_bits
&& f2_b_bits
&& f1_b_bits
!= f2_b_bits
)
3469 || (f1_a_bits
&& f2_a_bits
&& f1_a_bits
!= f2_a_bits
))
3476 * Implement the glCopyTexImage1/2D() functions.
3479 copyteximage(struct gl_context
*ctx
, GLuint dims
,
3480 GLenum target
, GLint level
, GLenum internalFormat
,
3481 GLint x
, GLint y
, GLsizei width
, GLsizei height
, GLint border
)
3483 struct gl_texture_object
*texObj
;
3484 struct gl_texture_image
*texImage
;
3485 const GLuint face
= _mesa_tex_target_to_face(target
);
3486 mesa_format texFormat
;
3487 struct gl_renderbuffer
*rb
;
3489 FLUSH_VERTICES(ctx
, 0);
3491 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3492 _mesa_debug(ctx
, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3494 _mesa_enum_to_string(target
), level
,
3495 _mesa_enum_to_string(internalFormat
),
3496 x
, y
, width
, height
, border
);
3498 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3499 _mesa_update_state(ctx
);
3501 if (copytexture_error_check(ctx
, dims
, target
, level
, internalFormat
,
3502 width
, height
, border
))
3505 if (!_mesa_legal_texture_dimensions(ctx
, target
, level
, width
, height
,
3507 _mesa_error(ctx
, GL_INVALID_VALUE
,
3508 "glCopyTexImage%uD(invalid width or height)", dims
);
3512 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3515 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3516 internalFormat
, GL_NONE
, GL_NONE
);
3518 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, internalFormat
);
3520 if (_mesa_is_gles3(ctx
)) {
3521 if (_mesa_is_enum_format_unsized(internalFormat
)) {
3522 /* Conversion from GL_RGB10_A2 source buffer format is not allowed in
3523 * OpenGL ES 3.0. Khronos bug# 9807.
3525 if (rb
->InternalFormat
== GL_RGB10_A2
) {
3526 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3527 "glCopyTexImage%uD(Reading from GL_RGB10_A2 buffer"
3528 " and writing to unsized internal format)", dims
);
3532 /* From Page 139 of OpenGL ES 3.0 spec:
3533 * "If internalformat is sized, the internal format of the new texel
3534 * array is internalformat, and this is also the new texel array’s
3535 * effective internal format. If the component sizes of internalformat
3536 * do not exactly match the corresponding component sizes of the source
3537 * buffer’s effective internal format, described below, an
3538 * INVALID_OPERATION error is generated. If internalformat is unsized,
3539 * the internal format of the new texel array is the effective internal
3540 * format of the source buffer, and this is also the new texel array’s
3541 * effective internal format.
3543 else if (formats_differ_in_component_sizes (texFormat
, rb
->Format
)) {
3544 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3545 "glCopyTexImage%uD(componenet size changed in"
3546 " internal format)", dims
);
3551 assert(texFormat
!= MESA_FORMAT_NONE
);
3553 if (!ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target(target
),
3555 width
, height
, 1, border
)) {
3556 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3557 "glCopyTexImage%uD(image too large)", dims
);
3561 if (border
&& ctx
->Const
.StripTextureBorder
) {
3563 width
-= border
* 2;
3566 height
-= border
* 2;
3571 _mesa_lock_texture(ctx
, texObj
);
3573 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3576 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
3579 GLint srcX
= x
, srcY
= y
, dstX
= 0, dstY
= 0, dstZ
= 0;
3581 /* Free old texture image */
3582 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3584 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, 1,
3585 border
, internalFormat
, texFormat
);
3587 if (width
&& height
) {
3588 /* Allocate texture memory (no pixel data yet) */
3589 ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
);
3591 if (_mesa_clip_copytexsubimage(ctx
, &dstX
, &dstY
, &srcX
, &srcY
,
3593 struct gl_renderbuffer
*srcRb
=
3594 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3596 copytexsubimage_by_slice(ctx
, texImage
, dims
,
3598 srcRb
, srcX
, srcY
, width
, height
);
3601 check_gen_mipmap(ctx
, target
, texObj
, level
);
3604 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3606 _mesa_dirty_texobj(ctx
, texObj
);
3609 _mesa_unlock_texture(ctx
, texObj
);
3615 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
3616 GLenum internalFormat
,
3618 GLsizei width
, GLint border
)
3620 GET_CURRENT_CONTEXT(ctx
);
3621 copyteximage(ctx
, 1, target
, level
, internalFormat
, x
, y
, width
, 1, border
);
3627 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
3628 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
3631 GET_CURRENT_CONTEXT(ctx
);
3632 copyteximage(ctx
, 2, target
, level
, internalFormat
,
3633 x
, y
, width
, height
, border
);
3637 * Implementation for glCopyTex(ture)SubImage1/2/3D() functions.
3640 _mesa_copy_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
3641 struct gl_texture_object
*texObj
,
3642 GLenum target
, GLint level
,
3643 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3645 GLsizei width
, GLsizei height
,
3648 struct gl_texture_image
*texImage
;
3650 FLUSH_VERTICES(ctx
, 0);
3652 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3653 _mesa_debug(ctx
, "%s %s %d %d %d %d %d %d %d %d\n", caller
,
3654 _mesa_enum_to_string(target
),
3655 level
, xoffset
, yoffset
, zoffset
, x
, y
, width
, height
);
3657 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3658 _mesa_update_state(ctx
);
3660 if (copytexsubimage_error_check(ctx
, dims
, texObj
, target
, level
,
3661 xoffset
, yoffset
, zoffset
,
3662 width
, height
, caller
)) {
3666 _mesa_lock_texture(ctx
, texObj
);
3668 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
3670 /* If we have a border, offset=-1 is legal. Bias by border width. */
3673 if (target
!= GL_TEXTURE_2D_ARRAY
)
3674 zoffset
+= texImage
->Border
;
3677 if (target
!= GL_TEXTURE_1D_ARRAY
)
3678 yoffset
+= texImage
->Border
;
3681 xoffset
+= texImage
->Border
;
3684 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3686 struct gl_renderbuffer
*srcRb
=
3687 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3689 copytexsubimage_by_slice(ctx
, texImage
, dims
,
3690 xoffset
, yoffset
, zoffset
,
3691 srcRb
, x
, y
, width
, height
);
3693 check_gen_mipmap(ctx
, target
, texObj
, level
);
3695 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3696 * the texel data, not the texture format, size, etc.
3700 _mesa_unlock_texture(ctx
, texObj
);
3704 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
3705 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
3707 struct gl_texture_object
* texObj
;
3708 const char *self
= "glCopyTexSubImage1D";
3709 GET_CURRENT_CONTEXT(ctx
);
3711 /* Check target (proxies not allowed). Target must be checked prior to
3712 * calling _mesa_get_current_tex_object.
3714 if (!legal_texsubimage_target(ctx
, 1, target
, false)) {
3715 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3716 _mesa_enum_to_string(target
));
3720 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3724 _mesa_copy_texture_sub_image(ctx
, 1, texObj
, target
, level
, xoffset
, 0, 0,
3725 x
, y
, width
, 1, self
);
3731 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
3732 GLint xoffset
, GLint yoffset
,
3733 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3735 struct gl_texture_object
* texObj
;
3736 const char *self
= "glCopyTexSubImage2D";
3737 GET_CURRENT_CONTEXT(ctx
);
3739 /* Check target (proxies not allowed). Target must be checked prior to
3740 * calling _mesa_get_current_tex_object.
3742 if (!legal_texsubimage_target(ctx
, 2, target
, false)) {
3743 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3744 _mesa_enum_to_string(target
));
3748 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3752 _mesa_copy_texture_sub_image(ctx
, 2, texObj
, target
, level
,
3753 xoffset
, yoffset
, 0,
3754 x
, y
, width
, height
, self
);
3760 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
3761 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3762 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3764 struct gl_texture_object
* texObj
;
3765 const char *self
= "glCopyTexSubImage3D";
3766 GET_CURRENT_CONTEXT(ctx
);
3768 /* Check target (proxies not allowed). Target must be checked prior to
3769 * calling _mesa_get_current_tex_object.
3771 if (!legal_texsubimage_target(ctx
, 3, target
, false)) {
3772 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3773 _mesa_enum_to_string(target
));
3777 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3781 _mesa_copy_texture_sub_image(ctx
, 3, texObj
, target
, level
,
3782 xoffset
, yoffset
, zoffset
,
3783 x
, y
, width
, height
, self
);
3787 _mesa_CopyTextureSubImage1D(GLuint texture
, GLint level
,
3788 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
3790 struct gl_texture_object
* texObj
;
3791 const char *self
= "glCopyTextureSubImage1D";
3792 GET_CURRENT_CONTEXT(ctx
);
3794 texObj
= _mesa_lookup_texture_err(ctx
, texture
, self
);
3798 /* Check target (proxies not allowed). */
3799 if (!legal_texsubimage_target(ctx
, 1, texObj
->Target
, true)) {
3800 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3801 _mesa_enum_to_string(texObj
->Target
));
3805 _mesa_copy_texture_sub_image(ctx
, 1, texObj
, texObj
->Target
, level
,
3806 xoffset
, 0, 0, x
, y
, width
, 1, self
);
3810 _mesa_CopyTextureSubImage2D(GLuint texture
, GLint level
,
3811 GLint xoffset
, GLint yoffset
,
3812 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3814 struct gl_texture_object
* texObj
;
3815 const char *self
= "glCopyTextureSubImage2D";
3816 GET_CURRENT_CONTEXT(ctx
);
3818 texObj
= _mesa_lookup_texture_err(ctx
, texture
, self
);
3822 /* Check target (proxies not allowed). */
3823 if (!legal_texsubimage_target(ctx
, 2, texObj
->Target
, true)) {
3824 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3825 _mesa_enum_to_string(texObj
->Target
));
3829 _mesa_copy_texture_sub_image(ctx
, 2, texObj
, texObj
->Target
, level
,
3830 xoffset
, yoffset
, 0,
3831 x
, y
, width
, height
, self
);
3837 _mesa_CopyTextureSubImage3D(GLuint texture
, GLint level
,
3838 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3839 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3841 struct gl_texture_object
* texObj
;
3842 const char *self
= "glCopyTextureSubImage3D";
3843 GET_CURRENT_CONTEXT(ctx
);
3845 texObj
= _mesa_lookup_texture_err(ctx
, texture
, self
);
3849 /* Check target (proxies not allowed). */
3850 if (!legal_texsubimage_target(ctx
, 3, texObj
->Target
, true)) {
3851 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3852 _mesa_enum_to_string(texObj
->Target
));
3856 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
3857 /* Act like CopyTexSubImage2D */
3858 _mesa_copy_texture_sub_image(ctx
, 2, texObj
,
3859 GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ zoffset
,
3860 level
, xoffset
, yoffset
, 0,
3861 x
, y
, width
, height
, self
);
3864 _mesa_copy_texture_sub_image(ctx
, 3, texObj
, texObj
->Target
, level
,
3865 xoffset
, yoffset
, zoffset
,
3866 x
, y
, width
, height
, self
);
3870 check_clear_tex_image(struct gl_context
*ctx
,
3871 const char *function
,
3872 struct gl_texture_image
*texImage
,
3873 GLenum format
, GLenum type
,
3875 GLubyte
*clearValue
)
3877 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3878 static const GLubyte zeroData
[MAX_PIXEL_BYTES
];
3879 GLenum internalFormat
= texImage
->InternalFormat
;
3882 if (texObj
->Target
== GL_TEXTURE_BUFFER
) {
3883 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3884 "%s(buffer texture)", function
);
3888 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
3889 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3890 "%s(compressed texture)", function
);
3894 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
3895 if (err
!= GL_NO_ERROR
) {
3896 _mesa_error(ctx
, err
,
3897 "%s(incompatible format = %s, type = %s)",
3899 _mesa_enum_to_string(format
),
3900 _mesa_enum_to_string(type
));
3904 /* make sure internal format and format basically agree */
3905 if (!texture_formats_agree(internalFormat
, format
)) {
3906 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3907 "%s(incompatible internalFormat = %s, format = %s)",
3909 _mesa_enum_to_string(internalFormat
),
3910 _mesa_enum_to_string(format
));
3914 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
3915 /* both source and dest must be integer-valued, or neither */
3916 if (_mesa_is_format_integer_color(texImage
->TexFormat
) !=
3917 _mesa_is_enum_format_integer(format
)) {
3918 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3919 "%s(integer/non-integer format mismatch)",
3925 if (!_mesa_texstore(ctx
,
3927 texImage
->_BaseFormat
,
3928 texImage
->TexFormat
,
3929 0, /* dstRowStride */
3931 1, 1, 1, /* srcWidth/Height/Depth */
3933 data
? data
: zeroData
,
3934 &ctx
->DefaultPacking
)) {
3935 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid format)", function
);
3942 static struct gl_texture_object
*
3943 get_tex_obj_for_clear(struct gl_context
*ctx
,
3944 const char *function
,
3947 struct gl_texture_object
*texObj
;
3950 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(zero texture)", function
);
3954 texObj
= _mesa_lookup_texture(ctx
, texture
);
3956 if (texObj
== NULL
) {
3957 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", function
);
3961 if (texObj
->Target
== 0) {
3962 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unbound tex)", function
);
3970 get_tex_images_for_clear(struct gl_context
*ctx
,
3971 const char *function
,
3972 struct gl_texture_object
*texObj
,
3974 struct gl_texture_image
**texImages
)
3979 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
3980 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid level)", function
);
3984 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
3985 for (i
= 0; i
< MAX_FACES
; i
++) {
3986 target
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ i
;
3988 texImages
[i
] = _mesa_select_tex_image(texObj
, target
, level
);
3989 if (texImages
[i
] == NULL
) {
3990 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3991 "%s(invalid level)", function
);
3999 texImages
[0] = _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4001 if (texImages
[0] == NULL
) {
4002 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid level)", function
);
4010 _mesa_ClearTexSubImage( GLuint texture
, GLint level
,
4011 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4012 GLsizei width
, GLsizei height
, GLsizei depth
,
4013 GLenum format
, GLenum type
, const void *data
)
4015 GET_CURRENT_CONTEXT(ctx
);
4016 struct gl_texture_object
*texObj
;
4017 struct gl_texture_image
*texImages
[MAX_FACES
];
4018 GLubyte clearValue
[MAX_FACES
][MAX_PIXEL_BYTES
];
4020 int minDepth
, maxDepth
;
4022 texObj
= get_tex_obj_for_clear(ctx
, "glClearTexSubImage", texture
);
4027 _mesa_lock_texture(ctx
, texObj
);
4029 numImages
= get_tex_images_for_clear(ctx
, "glClearTexSubImage",
4030 texObj
, level
, texImages
);
4034 if (numImages
== 1) {
4035 minDepth
= -(int) texImages
[0]->Border
;
4036 maxDepth
= texImages
[0]->Depth
;
4039 maxDepth
= numImages
;
4042 if (xoffset
< -(GLint
) texImages
[0]->Border
||
4043 yoffset
< -(GLint
) texImages
[0]->Border
||
4044 zoffset
< minDepth
||
4048 xoffset
+ width
> texImages
[0]->Width
||
4049 yoffset
+ height
> texImages
[0]->Height
||
4050 zoffset
+ depth
> maxDepth
) {
4051 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4052 "glClearSubTexImage(invalid dimensions)");
4056 if (numImages
== 1) {
4057 if (check_clear_tex_image(ctx
, "glClearTexSubImage",
4059 format
, type
, data
, clearValue
[0])) {
4060 ctx
->Driver
.ClearTexSubImage(ctx
,
4062 xoffset
, yoffset
, zoffset
,
4063 width
, height
, depth
,
4064 data
? clearValue
[0] : NULL
);
4067 for (i
= zoffset
; i
< zoffset
+ depth
; i
++) {
4068 if (!check_clear_tex_image(ctx
, "glClearTexSubImage",
4070 format
, type
, data
, clearValue
[i
]))
4073 for (i
= zoffset
; i
< zoffset
+ depth
; i
++) {
4074 ctx
->Driver
.ClearTexSubImage(ctx
,
4076 xoffset
, yoffset
, 0,
4078 data
? clearValue
[i
] : NULL
);
4083 _mesa_unlock_texture(ctx
, texObj
);
4087 _mesa_ClearTexImage( GLuint texture
, GLint level
,
4088 GLenum format
, GLenum type
, const void *data
)
4090 GET_CURRENT_CONTEXT(ctx
);
4091 struct gl_texture_object
*texObj
;
4092 struct gl_texture_image
*texImages
[MAX_FACES
];
4093 GLubyte clearValue
[MAX_FACES
][MAX_PIXEL_BYTES
];
4096 texObj
= get_tex_obj_for_clear(ctx
, "glClearTexImage", texture
);
4101 _mesa_lock_texture(ctx
, texObj
);
4103 numImages
= get_tex_images_for_clear(ctx
, "glClearTexImage",
4104 texObj
, level
, texImages
);
4106 for (i
= 0; i
< numImages
; i
++) {
4107 if (!check_clear_tex_image(ctx
, "glClearTexImage",
4114 for (i
= 0; i
< numImages
; i
++) {
4115 ctx
->Driver
.ClearTexSubImage(ctx
, texImages
[i
],
4116 -(GLint
) texImages
[i
]->Border
, /* xoffset */
4117 -(GLint
) texImages
[i
]->Border
, /* yoffset */
4118 -(GLint
) texImages
[i
]->Border
, /* zoffset */
4119 texImages
[i
]->Width
,
4120 texImages
[i
]->Height
,
4121 texImages
[i
]->Depth
,
4122 data
? clearValue
[i
] : NULL
);
4126 _mesa_unlock_texture(ctx
, texObj
);
4132 /**********************************************************************/
4133 /****** Compressed Textures ******/
4134 /**********************************************************************/
4138 * Target checking for glCompressedTexSubImage[123]D().
4139 * \return GL_TRUE if error, GL_FALSE if no error
4140 * Must come before other error checking so that the texture object can
4141 * be correctly retrieved using _mesa_get_current_tex_object.
4144 compressed_subtexture_target_check(struct gl_context
*ctx
, GLenum target
,
4145 GLint dims
, GLenum format
, bool dsa
,
4150 if (dsa
&& target
== GL_TEXTURE_RECTANGLE
) {
4151 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid target %s)", caller
,
4152 _mesa_enum_to_string(target
));
4162 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
4163 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
4164 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
4165 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
4166 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
4167 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
4168 targetOK
= ctx
->Extensions
.ARB_texture_cube_map
;
4171 targetOK
= GL_FALSE
;
4177 case GL_TEXTURE_CUBE_MAP
:
4178 targetOK
= dsa
&& ctx
->Extensions
.ARB_texture_cube_map
;
4180 case GL_TEXTURE_2D_ARRAY
:
4181 targetOK
= _mesa_is_gles3(ctx
) ||
4182 (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
);
4184 case GL_TEXTURE_CUBE_MAP_ARRAY
:
4185 targetOK
= ctx
->Extensions
.ARB_texture_cube_map_array
;
4190 * OpenGL 4.5 spec (30.10.2014) says in Section 8.7 Compressed Texture
4192 * "An INVALID_OPERATION error is generated by
4193 * CompressedTex*SubImage3D if the internal format of the texture
4194 * is one of the EAC, ETC2, or RGTC formats and either border is
4195 * non-zero, or the effective target for the texture is not
4196 * TEXTURE_2D_ARRAY."
4198 * NOTE: that's probably a spec error. It should probably say
4199 * "... or the effective target for the texture is not
4200 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP, nor
4201 * GL_TEXTURE_CUBE_MAP_ARRAY."
4202 * since those targets are 2D images and they support all compression
4205 * Instead of listing all these, just list those which are allowed,
4206 * which is (at this time) only bptc. Otherwise we'd say s3tc (and
4207 * more) are valid here, which they are not, but of course not
4208 * mentioned by core spec.
4211 /* These are the only 3D compression formats supported at this time */
4212 case GL_COMPRESSED_RGBA_BPTC_UNORM
:
4213 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM
:
4214 case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT
:
4215 case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT
:
4219 /* invalid format */
4220 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4221 "%s(invalid target %s for format %s)", caller
,
4222 _mesa_enum_to_string(target
),
4223 _mesa_enum_to_string(format
));
4228 targetOK
= GL_FALSE
;
4234 /* no 1D compressed textures at this time */
4235 targetOK
= GL_FALSE
;
4240 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", caller
,
4241 _mesa_enum_to_string(target
));
4249 * Error checking for glCompressedTexSubImage[123]D().
4250 * \return GL_TRUE if error, GL_FALSE if no error
4253 compressed_subtexture_error_check(struct gl_context
*ctx
, GLint dims
,
4254 const struct gl_texture_object
*texObj
,
4255 GLenum target
, GLint level
,
4256 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4257 GLsizei width
, GLsizei height
, GLsizei depth
,
4258 GLenum format
, GLsizei imageSize
,
4259 const GLvoid
*data
, const char *callerName
)
4261 struct gl_texture_image
*texImage
;
4264 /* this will catch any invalid compressed format token */
4265 if (!_mesa_is_compressed_format(ctx
, format
)) {
4266 _mesa_error(ctx
, GL_INVALID_ENUM
,
4267 "%s(format)", callerName
);
4271 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
4272 _mesa_error(ctx
, GL_INVALID_VALUE
,
4278 /* validate the bound PBO, if any */
4279 if (!_mesa_validate_pbo_source_compressed(ctx
, dims
, &ctx
->Unpack
,
4280 imageSize
, data
, callerName
)) {
4284 /* Check for invalid pixel storage modes */
4285 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dims
,
4286 &ctx
->Unpack
, callerName
)) {
4290 expectedSize
= compressed_tex_size(width
, height
, depth
, format
);
4291 if (expectedSize
!= imageSize
) {
4292 _mesa_error(ctx
, GL_INVALID_VALUE
,
4294 callerName
, imageSize
);
4298 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4300 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4301 "%s(invalid texture image)",
4306 if ((GLint
) format
!= texImage
->InternalFormat
) {
4307 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4309 callerName
, _mesa_enum_to_string(format
));
4313 if (compressedteximage_only_format(ctx
, format
)) {
4314 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4315 "%s(format=%s cannot be updated)",
4316 callerName
, _mesa_enum_to_string(format
));
4320 if (error_check_subtexture_dimensions(ctx
, dims
,
4321 texImage
, xoffset
, yoffset
, zoffset
,
4322 width
, height
, depth
,
4332 _mesa_CompressedTexImage1D(GLenum target
, GLint level
,
4333 GLenum internalFormat
, GLsizei width
,
4334 GLint border
, GLsizei imageSize
,
4337 GET_CURRENT_CONTEXT(ctx
);
4338 teximage(ctx
, GL_TRUE
, 1, target
, level
, internalFormat
,
4339 width
, 1, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4344 _mesa_CompressedTexImage2D(GLenum target
, GLint level
,
4345 GLenum internalFormat
, GLsizei width
,
4346 GLsizei height
, GLint border
, GLsizei imageSize
,
4349 GET_CURRENT_CONTEXT(ctx
);
4350 teximage(ctx
, GL_TRUE
, 2, target
, level
, internalFormat
,
4351 width
, height
, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4356 _mesa_CompressedTexImage3D(GLenum target
, GLint level
,
4357 GLenum internalFormat
, GLsizei width
,
4358 GLsizei height
, GLsizei depth
, GLint border
,
4359 GLsizei imageSize
, const GLvoid
*data
)
4361 GET_CURRENT_CONTEXT(ctx
);
4362 teximage(ctx
, GL_TRUE
, 3, target
, level
, internalFormat
,
4363 width
, height
, depth
, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4368 * Common helper for glCompressedTexSubImage1/2/3D() and
4369 * glCompressedTextureSubImage1/2/3D().
4372 _mesa_compressed_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
4373 struct gl_texture_object
*texObj
,
4374 struct gl_texture_image
*texImage
,
4375 GLenum target
, GLint level
,
4376 GLint xoffset
, GLint yoffset
,
4378 GLsizei width
, GLsizei height
,
4380 GLenum format
, GLsizei imageSize
,
4383 FLUSH_VERTICES(ctx
, 0);
4385 _mesa_lock_texture(ctx
, texObj
);
4387 if (width
> 0 && height
> 0 && depth
> 0) {
4388 ctx
->Driver
.CompressedTexSubImage(ctx
, dims
, texImage
,
4389 xoffset
, yoffset
, zoffset
,
4390 width
, height
, depth
,
4391 format
, imageSize
, data
);
4393 check_gen_mipmap(ctx
, target
, texObj
, level
);
4395 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
4396 * the texel data, not the texture format, size, etc.
4400 _mesa_unlock_texture(ctx
, texObj
);
4405 _mesa_CompressedTexSubImage1D(GLenum target
, GLint level
, GLint xoffset
,
4406 GLsizei width
, GLenum format
,
4407 GLsizei imageSize
, const GLvoid
*data
)
4409 struct gl_texture_object
*texObj
;
4410 struct gl_texture_image
*texImage
;
4412 GET_CURRENT_CONTEXT(ctx
);
4414 if (compressed_subtexture_target_check(ctx
, target
, 1, format
, false,
4415 "glCompressedTexSubImage1D")) {
4419 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4423 if (compressed_subtexture_error_check(ctx
, 1, texObj
, target
,
4424 level
, xoffset
, 0, 0,
4426 format
, imageSize
, data
,
4427 "glCompressedTexSubImage1D")) {
4431 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4434 _mesa_compressed_texture_sub_image(ctx
, 1, texObj
, texImage
, target
, level
,
4435 xoffset
, 0, 0, width
, 1, 1,
4436 format
, imageSize
, data
);
4440 _mesa_CompressedTextureSubImage1D(GLuint texture
, GLint level
, GLint xoffset
,
4441 GLsizei width
, GLenum format
,
4442 GLsizei imageSize
, const GLvoid
*data
)
4444 struct gl_texture_object
*texObj
;
4445 struct gl_texture_image
*texImage
;
4447 GET_CURRENT_CONTEXT(ctx
);
4449 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4450 "glCompressedTextureSubImage1D");
4454 if (compressed_subtexture_target_check(ctx
, texObj
->Target
, 1, format
, true,
4455 "glCompressedTextureSubImage1D")) {
4459 if (compressed_subtexture_error_check(ctx
, 1, texObj
, texObj
->Target
,
4460 level
, xoffset
, 0, 0,
4462 format
, imageSize
, data
,
4463 "glCompressedTextureSubImage1D")) {
4467 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4470 _mesa_compressed_texture_sub_image(ctx
, 1, texObj
, texImage
,
4471 texObj
->Target
, level
,
4472 xoffset
, 0, 0, width
, 1, 1,
4473 format
, imageSize
, data
);
4478 _mesa_CompressedTexSubImage2D(GLenum target
, GLint level
, GLint xoffset
,
4479 GLint yoffset
, GLsizei width
, GLsizei height
,
4480 GLenum format
, GLsizei imageSize
,
4483 struct gl_texture_object
*texObj
;
4484 struct gl_texture_image
*texImage
;
4486 GET_CURRENT_CONTEXT(ctx
);
4488 if (compressed_subtexture_target_check(ctx
, target
, 2, format
, false,
4489 "glCompressedTexSubImage2D")) {
4493 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4497 if (compressed_subtexture_error_check(ctx
, 2, texObj
, target
,
4498 level
, xoffset
, yoffset
, 0,
4500 format
, imageSize
, data
,
4501 "glCompressedTexSubImage2D")) {
4506 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4509 _mesa_compressed_texture_sub_image(ctx
, 2, texObj
, texImage
, target
, level
,
4510 xoffset
, yoffset
, 0, width
, height
, 1,
4511 format
, imageSize
, data
);
4515 _mesa_CompressedTextureSubImage2D(GLuint texture
, GLint level
, GLint xoffset
,
4517 GLsizei width
, GLsizei height
,
4518 GLenum format
, GLsizei imageSize
,
4521 struct gl_texture_object
*texObj
;
4522 struct gl_texture_image
*texImage
;
4524 GET_CURRENT_CONTEXT(ctx
);
4526 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4527 "glCompressedTextureSubImage2D");
4531 if (compressed_subtexture_target_check(ctx
, texObj
->Target
, 2, format
, true,
4532 "glCompressedTextureSubImage2D")) {
4536 if (compressed_subtexture_error_check(ctx
, 2, texObj
, texObj
->Target
,
4537 level
, xoffset
, yoffset
, 0,
4539 format
, imageSize
, data
,
4540 "glCompressedTextureSubImage2D")) {
4544 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4547 _mesa_compressed_texture_sub_image(ctx
, 2, texObj
, texImage
,
4548 texObj
->Target
, level
,
4549 xoffset
, yoffset
, 0, width
, height
, 1,
4550 format
, imageSize
, data
);
4554 _mesa_CompressedTexSubImage3D(GLenum target
, GLint level
, GLint xoffset
,
4555 GLint yoffset
, GLint zoffset
, GLsizei width
,
4556 GLsizei height
, GLsizei depth
, GLenum format
,
4557 GLsizei imageSize
, const GLvoid
*data
)
4559 struct gl_texture_object
*texObj
;
4560 struct gl_texture_image
*texImage
;
4562 GET_CURRENT_CONTEXT(ctx
);
4564 if (compressed_subtexture_target_check(ctx
, target
, 3, format
, false,
4565 "glCompressedTexSubImage3D")) {
4569 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4573 if (compressed_subtexture_error_check(ctx
, 3, texObj
, target
,
4574 level
, xoffset
, yoffset
, zoffset
,
4575 width
, height
, depth
,
4576 format
, imageSize
, data
,
4577 "glCompressedTexSubImage3D")) {
4582 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4585 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, texImage
, target
, level
,
4586 xoffset
, yoffset
, zoffset
,
4587 width
, height
, depth
,
4588 format
, imageSize
, data
);
4592 _mesa_CompressedTextureSubImage3D(GLuint texture
, GLint level
, GLint xoffset
,
4593 GLint yoffset
, GLint zoffset
, GLsizei width
,
4594 GLsizei height
, GLsizei depth
,
4595 GLenum format
, GLsizei imageSize
,
4598 struct gl_texture_object
*texObj
;
4599 struct gl_texture_image
*texImage
;
4601 GET_CURRENT_CONTEXT(ctx
);
4603 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4604 "glCompressedTextureSubImage3D");
4608 if (compressed_subtexture_target_check(ctx
, texObj
->Target
, 3, format
, true,
4609 "glCompressedTextureSubImage3D")) {
4613 if (compressed_subtexture_error_check(ctx
, 3, texObj
, texObj
->Target
,
4614 level
, xoffset
, yoffset
, zoffset
,
4615 width
, height
, depth
,
4616 format
, imageSize
, data
,
4617 "glCompressedTextureSubImage3D")) {
4621 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
4622 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
4623 const char *pixels
= data
;
4627 /* Make sure the texture object is a proper cube.
4628 * (See texturesubimage in teximage.c for details on why this check is
4631 if (!_mesa_cube_level_complete(texObj
, level
)) {
4632 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4633 "glCompressedTextureSubImage3D(cube map incomplete)");
4637 /* Copy in each face. */
4638 for (i
= 0; i
< 6; ++i
) {
4639 texImage
= texObj
->Image
[i
][level
];
4642 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, texImage
,
4643 texObj
->Target
, level
,
4644 xoffset
, yoffset
, zoffset
,
4646 format
, imageSize
, pixels
);
4648 /* Compressed images don't have a client format */
4649 image_stride
= _mesa_format_image_size(texImage
->TexFormat
,
4651 texImage
->Height
, 1);
4653 pixels
+= image_stride
;
4654 imageSize
-= image_stride
;
4658 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4661 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, texImage
,
4662 texObj
->Target
, level
,
4663 xoffset
, yoffset
, zoffset
,
4664 width
, height
, depth
,
4665 format
, imageSize
, data
);
4670 get_texbuffer_format(const struct gl_context
*ctx
, GLenum internalFormat
)
4672 if (ctx
->API
!= API_OPENGL_CORE
) {
4673 switch (internalFormat
) {
4675 return MESA_FORMAT_A_UNORM8
;
4677 return MESA_FORMAT_A_UNORM16
;
4678 case GL_ALPHA16F_ARB
:
4679 return MESA_FORMAT_A_FLOAT16
;
4680 case GL_ALPHA32F_ARB
:
4681 return MESA_FORMAT_A_FLOAT32
;
4682 case GL_ALPHA8I_EXT
:
4683 return MESA_FORMAT_A_SINT8
;
4684 case GL_ALPHA16I_EXT
:
4685 return MESA_FORMAT_A_SINT16
;
4686 case GL_ALPHA32I_EXT
:
4687 return MESA_FORMAT_A_SINT32
;
4688 case GL_ALPHA8UI_EXT
:
4689 return MESA_FORMAT_A_UINT8
;
4690 case GL_ALPHA16UI_EXT
:
4691 return MESA_FORMAT_A_UINT16
;
4692 case GL_ALPHA32UI_EXT
:
4693 return MESA_FORMAT_A_UINT32
;
4695 return MESA_FORMAT_L_UNORM8
;
4696 case GL_LUMINANCE16
:
4697 return MESA_FORMAT_L_UNORM16
;
4698 case GL_LUMINANCE16F_ARB
:
4699 return MESA_FORMAT_L_FLOAT16
;
4700 case GL_LUMINANCE32F_ARB
:
4701 return MESA_FORMAT_L_FLOAT32
;
4702 case GL_LUMINANCE8I_EXT
:
4703 return MESA_FORMAT_L_SINT8
;
4704 case GL_LUMINANCE16I_EXT
:
4705 return MESA_FORMAT_L_SINT16
;
4706 case GL_LUMINANCE32I_EXT
:
4707 return MESA_FORMAT_L_SINT32
;
4708 case GL_LUMINANCE8UI_EXT
:
4709 return MESA_FORMAT_L_UINT8
;
4710 case GL_LUMINANCE16UI_EXT
:
4711 return MESA_FORMAT_L_UINT16
;
4712 case GL_LUMINANCE32UI_EXT
:
4713 return MESA_FORMAT_L_UINT32
;
4714 case GL_LUMINANCE8_ALPHA8
:
4715 return MESA_FORMAT_L8A8_UNORM
;
4716 case GL_LUMINANCE16_ALPHA16
:
4717 return MESA_FORMAT_L16A16_UNORM
;
4718 case GL_LUMINANCE_ALPHA16F_ARB
:
4719 return MESA_FORMAT_LA_FLOAT16
;
4720 case GL_LUMINANCE_ALPHA32F_ARB
:
4721 return MESA_FORMAT_LA_FLOAT32
;
4722 case GL_LUMINANCE_ALPHA8I_EXT
:
4723 return MESA_FORMAT_LA_SINT8
;
4724 case GL_LUMINANCE_ALPHA16I_EXT
:
4725 return MESA_FORMAT_LA_SINT16
;
4726 case GL_LUMINANCE_ALPHA32I_EXT
:
4727 return MESA_FORMAT_LA_SINT32
;
4728 case GL_LUMINANCE_ALPHA8UI_EXT
:
4729 return MESA_FORMAT_LA_UINT8
;
4730 case GL_LUMINANCE_ALPHA16UI_EXT
:
4731 return MESA_FORMAT_LA_UINT16
;
4732 case GL_LUMINANCE_ALPHA32UI_EXT
:
4733 return MESA_FORMAT_LA_UINT32
;
4735 return MESA_FORMAT_I_UNORM8
;
4736 case GL_INTENSITY16
:
4737 return MESA_FORMAT_I_UNORM16
;
4738 case GL_INTENSITY16F_ARB
:
4739 return MESA_FORMAT_I_FLOAT16
;
4740 case GL_INTENSITY32F_ARB
:
4741 return MESA_FORMAT_I_FLOAT32
;
4742 case GL_INTENSITY8I_EXT
:
4743 return MESA_FORMAT_I_SINT8
;
4744 case GL_INTENSITY16I_EXT
:
4745 return MESA_FORMAT_I_SINT16
;
4746 case GL_INTENSITY32I_EXT
:
4747 return MESA_FORMAT_I_SINT32
;
4748 case GL_INTENSITY8UI_EXT
:
4749 return MESA_FORMAT_I_UINT8
;
4750 case GL_INTENSITY16UI_EXT
:
4751 return MESA_FORMAT_I_UINT16
;
4752 case GL_INTENSITY32UI_EXT
:
4753 return MESA_FORMAT_I_UINT32
;
4759 if (ctx
->API
== API_OPENGL_CORE
&&
4760 ctx
->Extensions
.ARB_texture_buffer_object_rgb32
) {
4761 switch (internalFormat
) {
4763 return MESA_FORMAT_RGB_FLOAT32
;
4765 return MESA_FORMAT_RGB_UINT32
;
4767 return MESA_FORMAT_RGB_SINT32
;
4773 switch (internalFormat
) {
4775 return MESA_FORMAT_R8G8B8A8_UNORM
;
4777 return MESA_FORMAT_RGBA_UNORM16
;
4778 case GL_RGBA16F_ARB
:
4779 return MESA_FORMAT_RGBA_FLOAT16
;
4780 case GL_RGBA32F_ARB
:
4781 return MESA_FORMAT_RGBA_FLOAT32
;
4783 return MESA_FORMAT_RGBA_SINT8
;
4784 case GL_RGBA16I_EXT
:
4785 return MESA_FORMAT_RGBA_SINT16
;
4786 case GL_RGBA32I_EXT
:
4787 return MESA_FORMAT_RGBA_SINT32
;
4788 case GL_RGBA8UI_EXT
:
4789 return MESA_FORMAT_RGBA_UINT8
;
4790 case GL_RGBA16UI_EXT
:
4791 return MESA_FORMAT_RGBA_UINT16
;
4792 case GL_RGBA32UI_EXT
:
4793 return MESA_FORMAT_RGBA_UINT32
;
4796 return MESA_FORMAT_R8G8_UNORM
;
4798 return MESA_FORMAT_R16G16_UNORM
;
4800 return MESA_FORMAT_RG_FLOAT16
;
4802 return MESA_FORMAT_RG_FLOAT32
;
4804 return MESA_FORMAT_RG_SINT8
;
4806 return MESA_FORMAT_RG_SINT16
;
4808 return MESA_FORMAT_RG_SINT32
;
4810 return MESA_FORMAT_RG_UINT8
;
4812 return MESA_FORMAT_RG_UINT16
;
4814 return MESA_FORMAT_RG_UINT32
;
4817 return MESA_FORMAT_R_UNORM8
;
4819 return MESA_FORMAT_R_UNORM16
;
4821 return MESA_FORMAT_R_FLOAT16
;
4823 return MESA_FORMAT_R_FLOAT32
;
4825 return MESA_FORMAT_R_SINT8
;
4827 return MESA_FORMAT_R_SINT16
;
4829 return MESA_FORMAT_R_SINT32
;
4831 return MESA_FORMAT_R_UINT8
;
4833 return MESA_FORMAT_R_UINT16
;
4835 return MESA_FORMAT_R_UINT32
;
4838 return MESA_FORMAT_NONE
;
4844 _mesa_validate_texbuffer_format(const struct gl_context
*ctx
,
4845 GLenum internalFormat
)
4847 mesa_format format
= get_texbuffer_format(ctx
, internalFormat
);
4850 if (format
== MESA_FORMAT_NONE
)
4851 return MESA_FORMAT_NONE
;
4853 datatype
= _mesa_get_format_datatype(format
);
4855 /* The GL_ARB_texture_buffer_object spec says:
4857 * "If ARB_texture_float is not supported, references to the
4858 * floating-point internal formats provided by that extension should be
4859 * removed, and such formats may not be passed to TexBufferARB."
4861 * As a result, GL_HALF_FLOAT internal format depends on both
4862 * GL_ARB_texture_float and GL_ARB_half_float_pixel.
4864 if ((datatype
== GL_FLOAT
|| datatype
== GL_HALF_FLOAT
) &&
4865 !ctx
->Extensions
.ARB_texture_float
)
4866 return MESA_FORMAT_NONE
;
4868 if (!ctx
->Extensions
.ARB_texture_rg
) {
4869 GLenum base_format
= _mesa_get_format_base_format(format
);
4870 if (base_format
== GL_R
|| base_format
== GL_RG
)
4871 return MESA_FORMAT_NONE
;
4874 if (!ctx
->Extensions
.ARB_texture_buffer_object_rgb32
) {
4875 GLenum base_format
= _mesa_get_format_base_format(format
);
4876 if (base_format
== GL_RGB
)
4877 return MESA_FORMAT_NONE
;
4884 _mesa_texture_buffer_range(struct gl_context
*ctx
,
4885 struct gl_texture_object
*texObj
,
4886 GLenum internalFormat
,
4887 struct gl_buffer_object
*bufObj
,
4888 GLintptr offset
, GLsizeiptr size
,
4893 /* NOTE: ARB_texture_buffer_object has interactions with
4894 * the compatibility profile that are not implemented.
4896 if (!(ctx
->API
== API_OPENGL_CORE
&&
4897 ctx
->Extensions
.ARB_texture_buffer_object
)) {
4898 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4899 "%s(ARB_texture_buffer_object is not"
4900 " implemented for the compatibility profile)", caller
);
4904 format
= _mesa_validate_texbuffer_format(ctx
, internalFormat
);
4905 if (format
== MESA_FORMAT_NONE
) {
4906 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat %s)",
4907 caller
, _mesa_enum_to_string(internalFormat
));
4911 FLUSH_VERTICES(ctx
, 0);
4913 _mesa_lock_texture(ctx
, texObj
);
4915 _mesa_reference_buffer_object(ctx
, &texObj
->BufferObject
, bufObj
);
4916 texObj
->BufferObjectFormat
= internalFormat
;
4917 texObj
->_BufferObjectFormat
= format
;
4918 texObj
->BufferOffset
= offset
;
4919 texObj
->BufferSize
= size
;
4921 _mesa_unlock_texture(ctx
, texObj
);
4923 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTextureBuffer
;
4926 bufObj
->UsageHistory
|= USAGE_TEXTURE_BUFFER
;
4932 * Make sure the texture buffer target is GL_TEXTURE_BUFFER.
4933 * Return true if it is, and return false if it is not
4934 * (and throw INVALID ENUM as dictated in the OpenGL 4.5
4935 * core spec, 02.02.2015, PDF page 245).
4938 check_texture_buffer_target(struct gl_context
*ctx
, GLenum target
,
4941 if (target
!= GL_TEXTURE_BUFFER_ARB
) {
4942 _mesa_error(ctx
, GL_INVALID_ENUM
,
4943 "%s(texture target is not GL_TEXTURE_BUFFER)", caller
);
4951 * Check for errors related to the texture buffer range.
4952 * Return false if errors are found, true if none are found.
4955 check_texture_buffer_range(struct gl_context
*ctx
,
4956 struct gl_buffer_object
*bufObj
,
4957 GLintptr offset
, GLsizeiptr size
,
4960 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
4961 * Textures (PDF page 245):
4962 * "An INVALID_VALUE error is generated if offset is negative, if
4963 * size is less than or equal to zero, or if offset + size is greater
4964 * than the value of BUFFER_SIZE for the buffer bound to target."
4967 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset=%d < 0)", caller
,
4973 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size=%d <= 0)", caller
,
4978 if (offset
+ size
> bufObj
->Size
) {
4979 _mesa_error(ctx
, GL_INVALID_VALUE
,
4980 "%s(offset=%d + size=%d > buffer_size=%d)", caller
,
4981 (int) offset
, (int) size
, (int) bufObj
->Size
);
4985 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
4986 * Textures (PDF page 245):
4987 * "An INVALID_VALUE error is generated if offset is not an integer
4988 * multiple of the value of TEXTURE_BUFFER_OFFSET_ALIGNMENT."
4990 if (offset
% ctx
->Const
.TextureBufferOffsetAlignment
) {
4991 _mesa_error(ctx
, GL_INVALID_VALUE
,
4992 "%s(invalid offset alignment)", caller
);
5000 /** GL_ARB_texture_buffer_object */
5002 _mesa_TexBuffer(GLenum target
, GLenum internalFormat
, GLuint buffer
)
5004 struct gl_texture_object
*texObj
;
5005 struct gl_buffer_object
*bufObj
;
5007 GET_CURRENT_CONTEXT(ctx
);
5009 /* Need to catch a bad target before it gets to
5010 * _mesa_get_current_tex_object.
5012 if (!check_texture_buffer_target(ctx
, target
, "glTexBuffer"))
5016 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glTexBuffer");
5022 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5026 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
, bufObj
, 0,
5027 buffer
? -1 : 0, "glTexBuffer");
5031 /** GL_ARB_texture_buffer_range */
5033 _mesa_TexBufferRange(GLenum target
, GLenum internalFormat
, GLuint buffer
,
5034 GLintptr offset
, GLsizeiptr size
)
5036 struct gl_texture_object
*texObj
;
5037 struct gl_buffer_object
*bufObj
;
5039 GET_CURRENT_CONTEXT(ctx
);
5041 /* Need to catch a bad target before it gets to
5042 * _mesa_get_current_tex_object.
5044 if (!check_texture_buffer_target(ctx
, target
, "glTexBufferRange"))
5048 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glTexBufferRange");
5052 if (!check_texture_buffer_range(ctx
, bufObj
, offset
, size
,
5053 "glTexBufferRange"))
5057 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5058 * Textures (PDF page 254):
5059 * "If buffer is zero, then any buffer object attached to the buffer
5060 * texture is detached, the values offset and size are ignored and
5061 * the state for offset and size for the buffer texture are reset to
5069 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5073 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
, bufObj
,
5074 offset
, size
, "glTexBufferRange");
5078 _mesa_TextureBuffer(GLuint texture
, GLenum internalFormat
, GLuint buffer
)
5080 struct gl_texture_object
*texObj
;
5081 struct gl_buffer_object
*bufObj
;
5083 GET_CURRENT_CONTEXT(ctx
);
5086 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glTextureBuffer");
5092 /* Get the texture object by Name. */
5093 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glTextureBuffer");
5097 if (!check_texture_buffer_target(ctx
, texObj
->Target
, "glTextureBuffer"))
5100 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
,
5101 bufObj
, 0, buffer
? -1 : 0, "glTextureBuffer");
5105 _mesa_TextureBufferRange(GLuint texture
, GLenum internalFormat
, GLuint buffer
,
5106 GLintptr offset
, GLsizeiptr size
)
5108 struct gl_texture_object
*texObj
;
5109 struct gl_buffer_object
*bufObj
;
5111 GET_CURRENT_CONTEXT(ctx
);
5114 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
5115 "glTextureBufferRange");
5119 if (!check_texture_buffer_range(ctx
, bufObj
, offset
, size
,
5120 "glTextureBufferRange"))
5124 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5125 * Textures (PDF page 254):
5126 * "If buffer is zero, then any buffer object attached to the buffer
5127 * texture is detached, the values offset and size are ignored and
5128 * the state for offset and size for the buffer texture are reset to
5136 /* Get the texture object by Name. */
5137 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glTextureBufferRange");
5141 if (!check_texture_buffer_target(ctx
, texObj
->Target
,
5142 "glTextureBufferRange"))
5145 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
,
5146 bufObj
, offset
, size
, "glTextureBufferRange");
5150 is_renderable_texture_format(struct gl_context
*ctx
, GLenum internalformat
)
5152 /* Everything that is allowed for renderbuffers,
5153 * except for a base format of GL_STENCIL_INDEX, unless supported.
5155 GLenum baseFormat
= _mesa_base_fbo_format(ctx
, internalformat
);
5156 if (ctx
->Extensions
.ARB_texture_stencil8
)
5157 return baseFormat
!= 0;
5159 return baseFormat
!= 0 && baseFormat
!= GL_STENCIL_INDEX
;
5163 /** GL_ARB_texture_multisample */
5165 check_multisample_target(GLuint dims
, GLenum target
, bool dsa
)
5168 case GL_TEXTURE_2D_MULTISAMPLE
:
5170 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
5171 return dims
== 2 && !dsa
;
5172 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
5174 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
5175 return dims
== 3 && !dsa
;
5183 texture_image_multisample(struct gl_context
*ctx
, GLuint dims
,
5184 struct gl_texture_object
*texObj
,
5185 GLenum target
, GLsizei samples
,
5186 GLint internalformat
, GLsizei width
,
5187 GLsizei height
, GLsizei depth
,
5188 GLboolean fixedsamplelocations
,
5189 GLboolean immutable
, const char *func
)
5191 struct gl_texture_image
*texImage
;
5192 GLboolean sizeOK
, dimensionsOK
, samplesOK
;
5193 mesa_format texFormat
;
5194 GLenum sample_count_error
;
5195 bool dsa
= strstr(func
, "ture") ? true : false;
5197 if (!((ctx
->Extensions
.ARB_texture_multisample
5198 && _mesa_is_desktop_gl(ctx
))) && !_mesa_is_gles31(ctx
)) {
5199 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unsupported)", func
);
5204 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(samples < 1)", func
);
5208 if (!check_multisample_target(dims
, target
, dsa
)) {
5210 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(target)", func
);
5214 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
5219 /* check that the specified internalformat is color/depth/stencil-renderable;
5220 * refer GL3.1 spec 4.4.4
5223 if (immutable
&& !_mesa_is_legal_tex_storage_format(ctx
, internalformat
)) {
5224 _mesa_error(ctx
, GL_INVALID_ENUM
,
5225 "%s(internalformat=%s not legal for immutable-format)",
5226 func
, _mesa_enum_to_string(internalformat
));
5230 if (!is_renderable_texture_format(ctx
, internalformat
)) {
5231 /* Page 172 of OpenGL ES 3.1 spec says:
5232 * "An INVALID_ENUM error is generated if sizedinternalformat is not
5233 * color-renderable, depth-renderable, or stencil-renderable (as
5234 * defined in section 9.4).
5236 * (Same error is also defined for desktop OpenGL for multisample
5237 * teximage/texstorage functions.)
5239 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalformat=%s)", func
,
5240 _mesa_enum_to_string(internalformat
));
5244 sample_count_error
= _mesa_check_sample_count(ctx
, target
,
5245 internalformat
, samples
);
5246 samplesOK
= sample_count_error
== GL_NO_ERROR
;
5248 /* Page 254 of OpenGL 4.4 spec says:
5249 * "Proxy arrays for two-dimensional multisample and two-dimensional
5250 * multisample array textures are operated on in the same way when
5251 * TexImage2DMultisample is called with target specified as
5252 * PROXY_TEXTURE_2D_MULTISAMPLE, or TexImage3DMultisample is called
5253 * with target specified as PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY.
5254 * However, if samples is not supported, then no error is generated.
5256 if (!samplesOK
&& !_mesa_is_proxy_texture(target
)) {
5257 _mesa_error(ctx
, sample_count_error
, "%s(samples)", func
);
5261 if (immutable
&& (!texObj
|| (texObj
->Name
== 0))) {
5262 _mesa_error(ctx
, GL_INVALID_OPERATION
,
5263 "%s(texture object 0)",
5268 texImage
= _mesa_get_tex_image(ctx
, texObj
, 0, 0);
5270 if (texImage
== NULL
) {
5271 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", func
);
5275 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, 0,
5276 internalformat
, GL_NONE
, GL_NONE
);
5277 assert(texFormat
!= MESA_FORMAT_NONE
);
5279 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, 0,
5280 width
, height
, depth
, 0);
5282 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, target
, 0, texFormat
,
5283 width
, height
, depth
, 0);
5285 if (_mesa_is_proxy_texture(target
)) {
5286 if (samplesOK
&& dimensionsOK
&& sizeOK
) {
5287 init_teximage_fields_ms(ctx
, texImage
, width
, height
, depth
, 0,
5288 internalformat
, texFormat
,
5289 samples
, fixedsamplelocations
);
5292 /* clear all image fields */
5293 clear_teximage_fields(texImage
);
5297 if (!dimensionsOK
) {
5298 _mesa_error(ctx
, GL_INVALID_VALUE
,
5299 "%s(invalid width or height)", func
);
5304 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(texture too large)", func
);
5308 /* Check if texObj->Immutable is set */
5309 if (texObj
->Immutable
) {
5310 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
5314 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
5316 init_teximage_fields_ms(ctx
, texImage
, width
, height
, depth
, 0,
5317 internalformat
, texFormat
,
5318 samples
, fixedsamplelocations
);
5320 if (width
> 0 && height
> 0 && depth
> 0) {
5321 if (!ctx
->Driver
.AllocTextureStorage(ctx
, texObj
, 1,
5322 width
, height
, depth
)) {
5323 /* tidy up the texture image state. strictly speaking,
5324 * we're allowed to just leave this in whatever state we
5325 * like, but being tidy is good.
5327 _mesa_init_teximage_fields(ctx
, texImage
,
5328 0, 0, 0, 0, GL_NONE
, MESA_FORMAT_NONE
);
5332 texObj
->Immutable
|= immutable
;
5335 _mesa_set_texture_view_state(ctx
, texObj
, target
, 1);
5338 _mesa_update_fbo_texture(ctx
, texObj
, 0, 0);
5344 _mesa_TexImage2DMultisample(GLenum target
, GLsizei samples
,
5345 GLenum internalformat
, GLsizei width
,
5346 GLsizei height
, GLboolean fixedsamplelocations
)
5348 struct gl_texture_object
*texObj
;
5349 GET_CURRENT_CONTEXT(ctx
);
5351 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5355 texture_image_multisample(ctx
, 2, texObj
, target
, samples
,
5356 internalformat
, width
, height
, 1,
5357 fixedsamplelocations
, GL_FALSE
,
5358 "glTexImage2DMultisample");
5363 _mesa_TexImage3DMultisample(GLenum target
, GLsizei samples
,
5364 GLenum internalformat
, GLsizei width
,
5365 GLsizei height
, GLsizei depth
,
5366 GLboolean fixedsamplelocations
)
5368 struct gl_texture_object
*texObj
;
5369 GET_CURRENT_CONTEXT(ctx
);
5371 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5375 texture_image_multisample(ctx
, 3, texObj
, target
, samples
,
5376 internalformat
, width
, height
, depth
,
5377 fixedsamplelocations
, GL_FALSE
,
5378 "glTexImage3DMultisample");
5382 valid_texstorage_ms_parameters(GLsizei width
, GLsizei height
, GLsizei depth
,
5383 GLsizei samples
, unsigned dims
)
5385 GET_CURRENT_CONTEXT(ctx
);
5387 if (!_mesa_valid_tex_storage_dim(width
, height
, depth
)) {
5388 _mesa_error(ctx
, GL_INVALID_VALUE
,
5389 "glTexStorage%uDMultisample(width=%d,height=%d,depth=%d)",
5390 dims
, width
, height
, depth
);
5397 _mesa_TexStorage2DMultisample(GLenum target
, GLsizei samples
,
5398 GLenum internalformat
, GLsizei width
,
5399 GLsizei height
, GLboolean fixedsamplelocations
)
5401 struct gl_texture_object
*texObj
;
5402 GET_CURRENT_CONTEXT(ctx
);
5404 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5408 if (!valid_texstorage_ms_parameters(width
, height
, 1, samples
, 2))
5411 texture_image_multisample(ctx
, 2, texObj
, target
, samples
,
5412 internalformat
, width
, height
, 1,
5413 fixedsamplelocations
, GL_TRUE
,
5414 "glTexStorage2DMultisample");
5418 _mesa_TexStorage3DMultisample(GLenum target
, GLsizei samples
,
5419 GLenum internalformat
, GLsizei width
,
5420 GLsizei height
, GLsizei depth
,
5421 GLboolean fixedsamplelocations
)
5423 struct gl_texture_object
*texObj
;
5424 GET_CURRENT_CONTEXT(ctx
);
5426 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5430 if (!valid_texstorage_ms_parameters(width
, height
, depth
, samples
, 3))
5433 texture_image_multisample(ctx
, 3, texObj
, target
, samples
,
5434 internalformat
, width
, height
, depth
,
5435 fixedsamplelocations
, GL_TRUE
,
5436 "glTexStorage3DMultisample");
5440 _mesa_TextureStorage2DMultisample(GLuint texture
, GLsizei samples
,
5441 GLenum internalformat
, GLsizei width
,
5443 GLboolean fixedsamplelocations
)
5445 struct gl_texture_object
*texObj
;
5446 GET_CURRENT_CONTEXT(ctx
);
5448 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
5449 "glTextureStorage2DMultisample");
5453 if (!valid_texstorage_ms_parameters(width
, height
, 1, samples
, 2))
5456 texture_image_multisample(ctx
, 2, texObj
, texObj
->Target
, samples
,
5457 internalformat
, width
, height
, 1,
5458 fixedsamplelocations
, GL_TRUE
,
5459 "glTextureStorage2DMultisample");
5463 _mesa_TextureStorage3DMultisample(GLuint texture
, GLsizei samples
,
5464 GLenum internalformat
, GLsizei width
,
5465 GLsizei height
, GLsizei depth
,
5466 GLboolean fixedsamplelocations
)
5468 struct gl_texture_object
*texObj
;
5469 GET_CURRENT_CONTEXT(ctx
);
5471 /* Get the texture object by Name. */
5472 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
5473 "glTextureStorage3DMultisample");
5477 if (!valid_texstorage_ms_parameters(width
, height
, depth
, samples
, 3))
5480 texture_image_multisample(ctx
, 3, texObj
, texObj
->Target
, samples
,
5481 internalformat
, width
, height
, depth
,
5482 fixedsamplelocations
, GL_TRUE
,
5483 "glTextureStorage3DMultisample");