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
:
246 * Test if a target is a cube map.
248 * \param target texture target.
250 * \return true if the target is a cube map, false otherwise.
253 _mesa_is_cube_map_texture(GLenum target
)
256 case GL_TEXTURE_CUBE_MAP
:
257 case GL_TEXTURE_CUBE_MAP_ARRAY
:
265 * Return the proxy target which corresponds to the given texture target
268 proxy_target(GLenum target
)
272 case GL_PROXY_TEXTURE_1D
:
273 return GL_PROXY_TEXTURE_1D
;
275 case GL_PROXY_TEXTURE_2D
:
276 return GL_PROXY_TEXTURE_2D
;
278 case GL_PROXY_TEXTURE_3D
:
279 return GL_PROXY_TEXTURE_3D
;
280 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
281 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
282 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
283 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
284 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
285 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
286 case GL_TEXTURE_CUBE_MAP
:
287 case GL_PROXY_TEXTURE_CUBE_MAP
:
288 return GL_PROXY_TEXTURE_CUBE_MAP
;
289 case GL_TEXTURE_RECTANGLE_NV
:
290 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
291 return GL_PROXY_TEXTURE_RECTANGLE_NV
;
292 case GL_TEXTURE_1D_ARRAY_EXT
:
293 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
294 return GL_PROXY_TEXTURE_1D_ARRAY_EXT
;
295 case GL_TEXTURE_2D_ARRAY_EXT
:
296 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
297 return GL_PROXY_TEXTURE_2D_ARRAY_EXT
;
298 case GL_TEXTURE_CUBE_MAP_ARRAY
:
299 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
300 return GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
;
301 case GL_TEXTURE_2D_MULTISAMPLE
:
302 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
303 return GL_PROXY_TEXTURE_2D_MULTISAMPLE
;
304 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
305 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
306 return GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
;
308 _mesa_problem(NULL
, "unexpected target in proxy_target()");
317 * Get a texture image pointer from a texture object, given a texture
318 * target and mipmap level. The target and level parameters should
319 * have already been error-checked.
321 * \param texObj texture unit.
322 * \param target texture target.
323 * \param level image level.
325 * \return pointer to the texture image structure, or NULL on failure.
327 struct gl_texture_image
*
328 _mesa_select_tex_image(const struct gl_texture_object
*texObj
,
329 GLenum target
, GLint level
)
331 const GLuint face
= _mesa_tex_target_to_face(target
);
335 assert(level
< MAX_TEXTURE_LEVELS
);
337 return texObj
->Image
[face
][level
];
342 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
343 * it and install it. Only return NULL if passed a bad parameter or run
346 struct gl_texture_image
*
347 _mesa_get_tex_image(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
348 GLenum target
, GLint level
)
350 struct gl_texture_image
*texImage
;
355 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
357 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
359 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture image allocation");
363 set_tex_image(texObj
, target
, level
, texImage
);
371 * Return pointer to the specified proxy texture image.
372 * Note that proxy textures are per-context, not per-texture unit.
373 * \return pointer to texture image or NULL if invalid target, invalid
374 * level, or out of memory.
376 static struct gl_texture_image
*
377 get_proxy_tex_image(struct gl_context
*ctx
, GLenum target
, GLint level
)
379 struct gl_texture_image
*texImage
;
386 case GL_PROXY_TEXTURE_1D
:
387 if (level
>= ctx
->Const
.MaxTextureLevels
)
389 texIndex
= TEXTURE_1D_INDEX
;
391 case GL_PROXY_TEXTURE_2D
:
392 if (level
>= ctx
->Const
.MaxTextureLevels
)
394 texIndex
= TEXTURE_2D_INDEX
;
396 case GL_PROXY_TEXTURE_3D
:
397 if (level
>= ctx
->Const
.Max3DTextureLevels
)
399 texIndex
= TEXTURE_3D_INDEX
;
401 case GL_PROXY_TEXTURE_CUBE_MAP
:
402 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
404 texIndex
= TEXTURE_CUBE_INDEX
;
406 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
409 texIndex
= TEXTURE_RECT_INDEX
;
411 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
412 if (level
>= ctx
->Const
.MaxTextureLevels
)
414 texIndex
= TEXTURE_1D_ARRAY_INDEX
;
416 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
417 if (level
>= ctx
->Const
.MaxTextureLevels
)
419 texIndex
= TEXTURE_2D_ARRAY_INDEX
;
421 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
422 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
424 texIndex
= TEXTURE_CUBE_ARRAY_INDEX
;
426 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
429 texIndex
= TEXTURE_2D_MULTISAMPLE_INDEX
;
431 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
434 texIndex
= TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
;
440 texImage
= ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
];
442 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
444 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
447 ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
] = texImage
;
448 /* Set the 'back' pointer */
449 texImage
->TexObject
= ctx
->Texture
.ProxyTex
[texIndex
];
456 * Get the maximum number of allowed mipmap levels.
458 * \param ctx GL context.
459 * \param target texture target.
461 * \return the maximum number of allowed mipmap levels for the given
462 * texture target, or zero if passed a bad target.
467 _mesa_max_texture_levels(struct gl_context
*ctx
, GLenum target
)
471 case GL_PROXY_TEXTURE_1D
:
473 case GL_PROXY_TEXTURE_2D
:
474 return ctx
->Const
.MaxTextureLevels
;
476 case GL_PROXY_TEXTURE_3D
:
477 return ctx
->Const
.Max3DTextureLevels
;
478 case GL_TEXTURE_CUBE_MAP
:
479 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
480 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
481 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
482 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
483 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
484 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
485 case GL_PROXY_TEXTURE_CUBE_MAP
:
486 return ctx
->Extensions
.ARB_texture_cube_map
487 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
488 case GL_TEXTURE_RECTANGLE_NV
:
489 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
490 return ctx
->Extensions
.NV_texture_rectangle
? 1 : 0;
491 case GL_TEXTURE_1D_ARRAY_EXT
:
492 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
493 case GL_TEXTURE_2D_ARRAY_EXT
:
494 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
495 return ctx
->Extensions
.EXT_texture_array
496 ? ctx
->Const
.MaxTextureLevels
: 0;
497 case GL_TEXTURE_CUBE_MAP_ARRAY
:
498 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
499 return ctx
->Extensions
.ARB_texture_cube_map_array
500 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
501 case GL_TEXTURE_BUFFER
:
502 return ctx
->API
== API_OPENGL_CORE
&&
503 ctx
->Extensions
.ARB_texture_buffer_object
? 1 : 0;
504 case GL_TEXTURE_2D_MULTISAMPLE
:
505 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
506 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
507 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
508 return (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles31(ctx
))
509 && ctx
->Extensions
.ARB_texture_multisample
511 case GL_TEXTURE_EXTERNAL_OES
:
514 return 0; /* bad target */
520 * Return number of dimensions per mipmap level for the given texture target.
523 _mesa_get_texture_dimensions(GLenum target
)
527 case GL_PROXY_TEXTURE_1D
:
530 case GL_TEXTURE_RECTANGLE
:
531 case GL_TEXTURE_CUBE_MAP
:
532 case GL_PROXY_TEXTURE_2D
:
533 case GL_PROXY_TEXTURE_RECTANGLE
:
534 case GL_PROXY_TEXTURE_CUBE_MAP
:
535 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
536 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
537 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
538 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
539 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
540 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
541 case GL_TEXTURE_1D_ARRAY
:
542 case GL_PROXY_TEXTURE_1D_ARRAY
:
543 case GL_TEXTURE_EXTERNAL_OES
:
544 case GL_TEXTURE_2D_MULTISAMPLE
:
545 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
548 case GL_PROXY_TEXTURE_3D
:
549 case GL_TEXTURE_2D_ARRAY
:
550 case GL_PROXY_TEXTURE_2D_ARRAY
:
551 case GL_TEXTURE_CUBE_MAP_ARRAY
:
552 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
553 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
554 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
556 case GL_TEXTURE_BUFFER
:
559 _mesa_problem(NULL
, "invalid target 0x%x in get_texture_dimensions()",
567 * Check if a texture target can have more than one layer.
570 _mesa_tex_target_is_layered(GLenum target
)
574 case GL_PROXY_TEXTURE_1D
:
576 case GL_PROXY_TEXTURE_2D
:
577 case GL_TEXTURE_RECTANGLE
:
578 case GL_PROXY_TEXTURE_RECTANGLE
:
579 case GL_TEXTURE_2D_MULTISAMPLE
:
580 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
581 case GL_TEXTURE_BUFFER
:
582 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
583 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
584 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
585 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
586 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
587 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
588 case GL_TEXTURE_EXTERNAL_OES
:
592 case GL_PROXY_TEXTURE_3D
:
593 case GL_TEXTURE_CUBE_MAP
:
594 case GL_PROXY_TEXTURE_CUBE_MAP
:
595 case GL_TEXTURE_1D_ARRAY
:
596 case GL_PROXY_TEXTURE_1D_ARRAY
:
597 case GL_TEXTURE_2D_ARRAY
:
598 case GL_PROXY_TEXTURE_2D_ARRAY
:
599 case GL_TEXTURE_CUBE_MAP_ARRAY
:
600 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
601 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
602 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
606 assert(!"Invalid texture target.");
613 * Return the number of layers present in the given level of an array,
614 * cubemap or 3D texture. If the texture is not layered return zero.
617 _mesa_get_texture_layers(const struct gl_texture_object
*texObj
, GLint level
)
619 assert(level
>= 0 && level
< MAX_TEXTURE_LEVELS
);
621 switch (texObj
->Target
) {
624 case GL_TEXTURE_RECTANGLE
:
625 case GL_TEXTURE_2D_MULTISAMPLE
:
626 case GL_TEXTURE_BUFFER
:
627 case GL_TEXTURE_EXTERNAL_OES
:
630 case GL_TEXTURE_CUBE_MAP
:
633 case GL_TEXTURE_1D_ARRAY
: {
634 struct gl_texture_image
*img
= texObj
->Image
[0][level
];
635 return img
? img
->Height
: 0;
639 case GL_TEXTURE_2D_ARRAY
:
640 case GL_TEXTURE_CUBE_MAP_ARRAY
:
641 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
: {
642 struct gl_texture_image
*img
= texObj
->Image
[0][level
];
643 return img
? img
->Depth
: 0;
647 assert(!"Invalid texture target.");
654 * Return the maximum number of mipmap levels for the given target
655 * and the dimensions.
656 * The dimensions are expected not to include the border.
659 _mesa_get_tex_max_num_levels(GLenum target
, GLsizei width
, GLsizei height
,
666 case GL_TEXTURE_1D_ARRAY
:
667 case GL_PROXY_TEXTURE_1D
:
668 case GL_PROXY_TEXTURE_1D_ARRAY
:
671 case GL_TEXTURE_CUBE_MAP
:
672 case GL_TEXTURE_CUBE_MAP_ARRAY
:
673 case GL_PROXY_TEXTURE_CUBE_MAP
:
674 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
678 case GL_TEXTURE_2D_ARRAY
:
679 case GL_PROXY_TEXTURE_2D
:
680 case GL_PROXY_TEXTURE_2D_ARRAY
:
681 size
= MAX2(width
, height
);
684 case GL_PROXY_TEXTURE_3D
:
685 size
= MAX3(width
, height
, depth
);
687 case GL_TEXTURE_RECTANGLE
:
688 case GL_TEXTURE_EXTERNAL_OES
:
689 case GL_TEXTURE_2D_MULTISAMPLE
:
690 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
691 case GL_PROXY_TEXTURE_RECTANGLE
:
692 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
693 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
700 return _mesa_logbase2(size
) + 1;
704 #if 000 /* not used anymore */
706 * glTexImage[123]D can accept a NULL image pointer. In this case we
707 * create a texture image with unspecified image contents per the OpenGL
711 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
713 const GLint components
= _mesa_components_in_format(format
);
714 const GLint numPixels
= width
* height
* depth
;
715 GLubyte
*data
= (GLubyte
*) malloc(numPixels
* components
* sizeof(GLubyte
));
719 * Let's see if anyone finds this. If glTexImage2D() is called with
720 * a NULL image pointer then load the texture image with something
721 * interesting instead of leaving it indeterminate.
724 static const char message
[8][32] = {
728 " X X XXXX XXX XXXXX ",
731 " X X XXXXX XXX X X ",
735 GLubyte
*imgPtr
= data
;
737 for (h
= 0; h
< depth
; h
++) {
738 for (i
= 0; i
< height
; i
++) {
739 GLint srcRow
= 7 - (i
% 8);
740 for (j
= 0; j
< width
; j
++) {
741 GLint srcCol
= j
% 32;
742 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
743 for (k
= 0; k
< components
; k
++) {
759 * Set the size and format-related fields of a gl_texture_image struct
760 * to zero. This is used when a proxy texture test fails.
763 clear_teximage_fields(struct gl_texture_image
*img
)
766 img
->_BaseFormat
= 0;
767 img
->InternalFormat
= 0;
778 img
->TexFormat
= MESA_FORMAT_NONE
;
780 img
->FixedSampleLocations
= GL_TRUE
;
785 * Initialize basic fields of the gl_texture_image struct.
787 * \param ctx GL context.
788 * \param img texture image structure to be initialized.
789 * \param width image width.
790 * \param height image height.
791 * \param depth image depth.
792 * \param border image border.
793 * \param internalFormat internal format.
794 * \param format the actual hardware format (one of MESA_FORMAT_*)
795 * \param numSamples number of samples per texel, or zero for non-MS.
796 * \param fixedSampleLocations are sample locations fixed?
798 * Fills in the fields of \p img with the given information.
799 * Note: width, height and depth include the border.
802 init_teximage_fields_ms(struct gl_context
*ctx
,
803 struct gl_texture_image
*img
,
804 GLsizei width
, GLsizei height
, GLsizei depth
,
805 GLint border
, GLenum internalFormat
,
807 GLuint numSamples
, GLboolean fixedSampleLocations
)
815 target
= img
->TexObject
->Target
;
816 img
->_BaseFormat
= _mesa_base_tex_format( ctx
, internalFormat
);
817 assert(img
->_BaseFormat
!= -1);
818 img
->InternalFormat
= internalFormat
;
819 img
->Border
= border
;
821 img
->Height
= height
;
824 img
->Width2
= width
- 2 * border
; /* == 1 << img->WidthLog2; */
825 img
->WidthLog2
= _mesa_logbase2(img
->Width2
);
828 img
->FixedSampleLocations
= GL_TRUE
;
832 case GL_TEXTURE_BUFFER
:
833 case GL_PROXY_TEXTURE_1D
:
845 case GL_TEXTURE_1D_ARRAY
:
846 case GL_PROXY_TEXTURE_1D_ARRAY
:
847 img
->Height2
= height
; /* no border */
848 img
->HeightLog2
= 0; /* not used */
856 case GL_TEXTURE_RECTANGLE
:
857 case GL_TEXTURE_CUBE_MAP
:
858 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
859 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
860 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
861 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
862 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
863 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
864 case GL_TEXTURE_EXTERNAL_OES
:
865 case GL_PROXY_TEXTURE_2D
:
866 case GL_PROXY_TEXTURE_RECTANGLE
:
867 case GL_PROXY_TEXTURE_CUBE_MAP
:
868 case GL_TEXTURE_2D_MULTISAMPLE
:
869 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
870 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
871 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
878 case GL_TEXTURE_2D_ARRAY
:
879 case GL_PROXY_TEXTURE_2D_ARRAY
:
880 case GL_TEXTURE_CUBE_MAP_ARRAY
:
881 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
882 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
883 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
884 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
885 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
886 img
->Depth2
= depth
; /* no border */
887 img
->DepthLog2
= 0; /* not used */
890 case GL_PROXY_TEXTURE_3D
:
891 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
892 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
893 img
->Depth2
= depth
- 2 * border
; /* == 1 << img->DepthLog2; */
894 img
->DepthLog2
= _mesa_logbase2(img
->Depth2
);
897 _mesa_problem(NULL
, "invalid target 0x%x in _mesa_init_teximage_fields()",
902 _mesa_get_tex_max_num_levels(target
,
903 img
->Width2
, img
->Height2
, img
->Depth2
);
904 img
->TexFormat
= format
;
905 img
->NumSamples
= numSamples
;
906 img
->FixedSampleLocations
= fixedSampleLocations
;
911 _mesa_init_teximage_fields(struct gl_context
*ctx
,
912 struct gl_texture_image
*img
,
913 GLsizei width
, GLsizei height
, GLsizei depth
,
914 GLint border
, GLenum internalFormat
,
917 init_teximage_fields_ms(ctx
, img
, width
, height
, depth
, border
,
918 internalFormat
, format
, 0, GL_TRUE
);
923 * Free and clear fields of the gl_texture_image struct.
925 * \param ctx GL context.
926 * \param texImage texture image structure to be cleared.
928 * After the call, \p texImage will have no data associated with it. Its
929 * fields are cleared so that its parent object will test incomplete.
932 _mesa_clear_texture_image(struct gl_context
*ctx
,
933 struct gl_texture_image
*texImage
)
935 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
936 clear_teximage_fields(texImage
);
941 * Check the width, height, depth and border of a texture image are legal.
942 * Used by all the glTexImage, glCompressedTexImage and glCopyTexImage
944 * The target and level parameters will have already been validated.
945 * \return GL_TRUE if size is OK, GL_FALSE otherwise.
948 _mesa_legal_texture_dimensions(struct gl_context
*ctx
, GLenum target
,
949 GLint level
, GLint width
, GLint height
,
950 GLint depth
, GLint border
)
956 case GL_PROXY_TEXTURE_1D
:
957 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1); /* level zero size */
958 maxSize
>>= level
; /* level size */
959 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
961 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
962 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
968 case GL_PROXY_TEXTURE_2D
:
969 case GL_TEXTURE_2D_MULTISAMPLE
:
970 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
971 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
973 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
975 if (height
< 2 * border
|| height
> 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
))
986 case GL_PROXY_TEXTURE_3D
:
987 maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
989 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
991 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
993 if (depth
< 2 * border
|| depth
> 2 * border
+ maxSize
)
995 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
996 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
998 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1000 if (depth
> 0 && !_mesa_is_pow_two(depth
- 2 * border
))
1005 case GL_TEXTURE_RECTANGLE_NV
:
1006 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1009 maxSize
= ctx
->Const
.MaxTextureRectSize
;
1010 if (width
< 0 || width
> maxSize
)
1012 if (height
< 0 || height
> maxSize
)
1016 case GL_TEXTURE_CUBE_MAP
:
1017 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1018 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1019 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1020 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1021 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1022 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1023 case GL_PROXY_TEXTURE_CUBE_MAP
:
1024 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1026 if (width
!= height
)
1028 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1030 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1032 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1033 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1035 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1040 case GL_TEXTURE_1D_ARRAY_EXT
:
1041 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1042 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1044 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1046 if (height
< 0 || height
> ctx
->Const
.MaxArrayTextureLayers
)
1048 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1049 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1054 case GL_TEXTURE_2D_ARRAY_EXT
:
1055 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1056 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1057 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1058 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1060 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1062 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1064 if (depth
< 0 || depth
> ctx
->Const
.MaxArrayTextureLayers
)
1066 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1067 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1069 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1074 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1075 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1076 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1077 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1079 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1081 if (depth
< 0 || depth
> ctx
->Const
.MaxArrayTextureLayers
|| depth
% 6)
1083 if (width
!= height
)
1085 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
1087 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1088 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1090 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1095 _mesa_problem(ctx
, "Invalid target in _mesa_legal_texture_dimensions()");
1102 * Do error checking of xoffset, yoffset, zoffset, width, height and depth
1103 * for glTexSubImage, glCopyTexSubImage and glCompressedTexSubImage.
1104 * \param destImage the destination texture image.
1105 * \return GL_TRUE if error found, GL_FALSE otherwise.
1108 error_check_subtexture_dimensions(struct gl_context
*ctx
, GLuint dims
,
1109 const struct gl_texture_image
*destImage
,
1110 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1111 GLsizei subWidth
, GLsizei subHeight
,
1112 GLsizei subDepth
, const char *func
)
1114 const GLenum target
= destImage
->TexObject
->Target
;
1119 _mesa_error(ctx
, GL_INVALID_VALUE
,
1120 "%s(width=%d)", func
, subWidth
);
1124 if (dims
> 1 && subHeight
< 0) {
1125 _mesa_error(ctx
, GL_INVALID_VALUE
,
1126 "%s(height=%d)", func
, subHeight
);
1130 if (dims
> 2 && subDepth
< 0) {
1131 _mesa_error(ctx
, GL_INVALID_VALUE
,
1132 "%s(depth=%d)", func
, subDepth
);
1136 /* check xoffset and width */
1137 if (xoffset
< - (GLint
) destImage
->Border
) {
1138 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(xoffset)", func
);
1142 if (xoffset
+ subWidth
> (GLint
) destImage
->Width
) {
1143 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(xoffset+width)", func
);
1147 /* check yoffset and height */
1149 GLint yBorder
= (target
== GL_TEXTURE_1D_ARRAY
) ? 0 : destImage
->Border
;
1150 if (yoffset
< -yBorder
) {
1151 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(yoffset)", func
);
1154 if (yoffset
+ subHeight
> (GLint
) destImage
->Height
) {
1155 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(yoffset+height)", func
);
1160 /* check zoffset and depth */
1163 GLint zBorder
= (target
== GL_TEXTURE_2D_ARRAY
||
1164 target
== GL_TEXTURE_CUBE_MAP_ARRAY
) ?
1165 0 : destImage
->Border
;
1167 if (zoffset
< -zBorder
) {
1168 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(zoffset)", func
);
1172 depth
= (GLint
) destImage
->Depth
;
1173 if (target
== GL_TEXTURE_CUBE_MAP
)
1175 if (zoffset
+ subDepth
> depth
) {
1176 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(zoffset+depth)", func
);
1182 * The OpenGL spec (and GL_ARB_texture_compression) says only whole
1183 * compressed texture images can be updated. But, that restriction may be
1184 * relaxed for particular compressed formats. At this time, all the
1185 * compressed formats supported by Mesa allow sub-textures to be updated
1186 * along compressed block boundaries.
1188 _mesa_get_format_block_size(destImage
->TexFormat
, &bw
, &bh
);
1190 if (bw
!= 1 || bh
!= 1) {
1191 /* offset must be multiple of block size */
1192 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0)) {
1193 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1194 "%s(xoffset = %d, yoffset = %d)",
1195 func
, xoffset
, yoffset
);
1199 /* The size must be a multiple of bw x bh, or we must be using a
1200 * offset+size that exactly hits the edge of the image. This
1201 * is important for small mipmap levels (1x1, 2x1, etc) and for
1204 if ((subWidth
% bw
!= 0) &&
1205 (xoffset
+ subWidth
!= (GLint
) destImage
->Width
)) {
1206 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1207 "%s(width = %d)", func
, subWidth
);
1211 if ((subHeight
% bh
!= 0) &&
1212 (yoffset
+ subHeight
!= (GLint
) destImage
->Height
)) {
1213 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1214 "%s(height = %d)", func
, subHeight
);
1226 * This is the fallback for Driver.TestProxyTexImage() for doing device-
1227 * specific texture image size checks.
1229 * A hardware driver might override this function if, for example, the
1230 * max 3D texture size is 512x512x64 (i.e. not a cube).
1232 * Note that width, height, depth == 0 is not an error. However, a
1233 * texture with zero width/height/depth will be considered "incomplete"
1234 * and texturing will effectively be disabled.
1236 * \param target any texture target/type
1237 * \param level as passed to glTexImage
1238 * \param format the MESA_FORMAT_x for the tex image
1239 * \param width as passed to glTexImage
1240 * \param height as passed to glTexImage
1241 * \param depth as passed to glTexImage
1242 * \param border as passed to glTexImage
1243 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1246 _mesa_test_proxy_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1248 GLint width
, GLint height
, GLint depth
, GLint border
)
1250 /* We just check if the image size is less than MaxTextureMbytes.
1251 * Some drivers may do more specific checks.
1253 uint64_t bytes
= _mesa_format_image_size64(format
, width
, height
, depth
);
1254 uint64_t mbytes
= bytes
/ (1024 * 1024); /* convert to MB */
1255 mbytes
*= _mesa_num_tex_faces(target
);
1256 return mbytes
<= (uint64_t) ctx
->Const
.MaxTextureMbytes
;
1261 * Return true if the format is only valid for glCompressedTexImage.
1264 compressedteximage_only_format(const struct gl_context
*ctx
, GLenum format
)
1267 case GL_ETC1_RGB8_OES
:
1268 case GL_PALETTE4_RGB8_OES
:
1269 case GL_PALETTE4_RGBA8_OES
:
1270 case GL_PALETTE4_R5_G6_B5_OES
:
1271 case GL_PALETTE4_RGBA4_OES
:
1272 case GL_PALETTE4_RGB5_A1_OES
:
1273 case GL_PALETTE8_RGB8_OES
:
1274 case GL_PALETTE8_RGBA8_OES
:
1275 case GL_PALETTE8_R5_G6_B5_OES
:
1276 case GL_PALETTE8_RGBA4_OES
:
1277 case GL_PALETTE8_RGB5_A1_OES
:
1285 * Return true if the format doesn't support online compression.
1288 _mesa_format_no_online_compression(const struct gl_context
*ctx
, GLenum format
)
1290 return _mesa_is_astc_format(format
) ||
1291 compressedteximage_only_format(ctx
, format
);
1294 /* Writes to an GL error pointer if non-null and returns whether or not the
1295 * error is GL_NO_ERROR */
1297 write_error(GLenum
*err_ptr
, GLenum error
)
1302 return error
== GL_NO_ERROR
;
1306 * Helper function to determine whether a target and specific compression
1307 * format are supported. The error parameter returns GL_NO_ERROR if the
1308 * target can be compressed. Otherwise it returns either GL_INVALID_OPERATION
1309 * or GL_INVALID_ENUM, whichever is more appropriate.
1312 _mesa_target_can_be_compressed(const struct gl_context
*ctx
, GLenum target
,
1313 GLenum intFormat
, GLenum
*error
)
1315 GLboolean target_can_be_compresed
= GL_FALSE
;
1316 mesa_format format
= _mesa_glenum_to_compressed_format(intFormat
);
1317 enum mesa_format_layout layout
= _mesa_get_format_layout(format
);
1321 case GL_PROXY_TEXTURE_2D
:
1322 target_can_be_compresed
= GL_TRUE
; /* true for any compressed format so far */
1324 case GL_PROXY_TEXTURE_CUBE_MAP
:
1325 case GL_TEXTURE_CUBE_MAP
:
1326 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1327 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1328 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1329 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1330 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1331 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1332 target_can_be_compresed
= ctx
->Extensions
.ARB_texture_cube_map
;
1334 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1335 case GL_TEXTURE_2D_ARRAY_EXT
:
1336 target_can_be_compresed
= ctx
->Extensions
.EXT_texture_array
;
1338 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1339 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1340 /* From the KHR_texture_compression_astc_hdr spec:
1342 * Add a second new column "3D Tex." which is empty for all non-ASTC
1343 * formats. If only the LDR profile is supported by the
1344 * implementation, this column is also empty for all ASTC formats. If
1345 * both the LDR and HDR profiles are supported only, this column is
1346 * checked for all ASTC formats.
1348 * Add a third new column "Cube Map Array Tex." which is empty for all
1349 * non-ASTC formats, and checked for all ASTC formats.
1353 * 'An INVALID_OPERATION error is generated by CompressedTexImage3D
1354 * if <internalformat> is TEXTURE_CUBE_MAP_ARRAY and the
1355 * "Cube Map Array" column of table 8.19 is *not* checked, or if
1356 * <internalformat> is TEXTURE_3D and the "3D Tex." column of table
1357 * 8.19 is *not* checked'
1359 * The instances of <internalformat> above should say <target>.
1361 * ETC2/EAC formats are the only alternative in GLES and thus such errors
1362 * have already been handled by normal ETC2/EAC behavior.
1365 /* From section 3.8.6, page 146 of OpenGL ES 3.0 spec:
1367 * "The ETC2/EAC texture compression algorithm supports only
1368 * two-dimensional images. If internalformat is an ETC2/EAC format,
1369 * glCompressedTexImage3D will generate an INVALID_OPERATION error if
1370 * target is not TEXTURE_2D_ARRAY."
1372 * This should also be applicable for glTexStorage3D(). Other available
1373 * targets for these functions are: TEXTURE_3D and TEXTURE_CUBE_MAP_ARRAY.
1375 if (layout
== MESA_FORMAT_LAYOUT_ETC2
&& _mesa_is_gles3(ctx
))
1376 return write_error(error
, GL_INVALID_OPERATION
);
1377 target_can_be_compresed
= ctx
->Extensions
.ARB_texture_cube_map_array
;
1381 case MESA_FORMAT_LAYOUT_ETC2
:
1382 /* See ETC2/EAC comment in case GL_TEXTURE_CUBE_MAP_ARRAY. */
1383 if (_mesa_is_gles3(ctx
))
1384 return write_error(error
, GL_INVALID_OPERATION
);
1386 case MESA_FORMAT_LAYOUT_BPTC
:
1387 target_can_be_compresed
= ctx
->Extensions
.ARB_texture_compression_bptc
;
1389 case MESA_FORMAT_LAYOUT_ASTC
:
1390 target_can_be_compresed
=
1391 ctx
->Extensions
.KHR_texture_compression_astc_hdr
;
1393 /* Throw an INVALID_OPERATION error if the target is TEXTURE_3D and
1394 * and the hdr extension is not supported.
1395 * See comment in switch case GL_TEXTURE_CUBE_MAP_ARRAY for more info.
1397 if (!target_can_be_compresed
)
1398 return write_error(error
, GL_INVALID_OPERATION
);
1406 return write_error(error
,
1407 target_can_be_compresed
? GL_NO_ERROR
: GL_INVALID_ENUM
);
1412 * Check if the given texture target value is legal for a
1413 * glTexImage1/2/3D call.
1416 legal_teximage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
1422 case GL_PROXY_TEXTURE_1D
:
1423 return _mesa_is_desktop_gl(ctx
);
1431 case GL_PROXY_TEXTURE_2D
:
1432 return _mesa_is_desktop_gl(ctx
);
1433 case GL_PROXY_TEXTURE_CUBE_MAP
:
1434 return _mesa_is_desktop_gl(ctx
)
1435 && ctx
->Extensions
.ARB_texture_cube_map
;
1436 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1437 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1438 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1439 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1440 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1441 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1442 return ctx
->Extensions
.ARB_texture_cube_map
;
1443 case GL_TEXTURE_RECTANGLE_NV
:
1444 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1445 return _mesa_is_desktop_gl(ctx
)
1446 && ctx
->Extensions
.NV_texture_rectangle
;
1447 case GL_TEXTURE_1D_ARRAY_EXT
:
1448 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1449 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1457 case GL_PROXY_TEXTURE_3D
:
1458 return _mesa_is_desktop_gl(ctx
);
1459 case GL_TEXTURE_2D_ARRAY_EXT
:
1460 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1461 || _mesa_is_gles3(ctx
);
1462 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1463 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1464 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1465 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1466 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1471 _mesa_problem(ctx
, "invalid dims=%u in legal_teximage_target()", dims
);
1478 * Check if the given texture target value is legal for a
1479 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1480 * The difference compared to legal_teximage_target() above is that
1481 * proxy targets are not supported.
1484 legal_texsubimage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
,
1489 return _mesa_is_desktop_gl(ctx
) && target
== GL_TEXTURE_1D
;
1494 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1495 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1496 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1497 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1498 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1499 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1500 return ctx
->Extensions
.ARB_texture_cube_map
;
1501 case GL_TEXTURE_RECTANGLE_NV
:
1502 return _mesa_is_desktop_gl(ctx
)
1503 && ctx
->Extensions
.NV_texture_rectangle
;
1504 case GL_TEXTURE_1D_ARRAY_EXT
:
1505 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1513 case GL_TEXTURE_2D_ARRAY_EXT
:
1514 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1515 || _mesa_is_gles3(ctx
);
1516 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1517 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1518 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1520 /* Table 8.15 of the OpenGL 4.5 core profile spec
1521 * (20141030) says that TEXTURE_CUBE_MAP is valid for TextureSubImage3D
1522 * and CopyTextureSubImage3D.
1524 case GL_TEXTURE_CUBE_MAP
:
1530 _mesa_problem(ctx
, "invalid dims=%u in legal_texsubimage_target()",
1538 * Helper function to determine if a texture object is mutable (in terms
1539 * of GL_ARB_texture_storage).
1542 mutable_tex_object(struct gl_context
*ctx
, GLenum target
)
1544 struct gl_texture_object
*texObj
= _mesa_get_current_tex_object(ctx
, target
);
1548 return !texObj
->Immutable
;
1553 * Return expected size of a compressed texture.
1556 compressed_tex_size(GLsizei width
, GLsizei height
, GLsizei depth
,
1559 mesa_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
1560 return _mesa_format_image_size(mesaFormat
, width
, height
, depth
);
1564 * Verify that a texture format is valid with a particular target
1566 * In particular, textures with base format of \c GL_DEPTH_COMPONENT or
1567 * \c GL_DEPTH_STENCIL are only valid with certain, context dependent texture
1570 * \param ctx GL context
1571 * \param target Texture target
1572 * \param internalFormat Internal format of the texture image
1574 * \returns true if the combination is legal, false otherwise.
1577 _mesa_legal_texture_base_format_for_target(struct gl_context
*ctx
,
1578 GLenum target
, GLenum internalFormat
)
1580 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
1581 || _mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_STENCIL
1582 || _mesa_base_tex_format(ctx
, internalFormat
) == GL_STENCIL_INDEX
) {
1583 /* Section 3.8.3 (Texture Image Specification) of the OpenGL 3.3 Core
1584 * Profile spec says:
1586 * "Textures with a base internal format of DEPTH_COMPONENT or
1587 * DEPTH_STENCIL are supported by texture image specification
1588 * commands only if target is TEXTURE_1D, TEXTURE_2D,
1589 * TEXTURE_1D_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_RECTANGLE,
1590 * TEXTURE_CUBE_MAP, PROXY_TEXTURE_1D, PROXY_TEXTURE_2D,
1591 * PROXY_TEXTURE_1D_ARRAY, PROXY_TEXTURE_2D_ARRAY,
1592 * PROXY_TEXTURE_RECTANGLE, or PROXY_TEXTURE_CUBE_MAP. Using these
1593 * formats in conjunction with any other target will result in an
1594 * INVALID_OPERATION error."
1596 * Cubemaps are only supported with desktop OpenGL version >= 3.0,
1597 * EXT_gpu_shader4, or, on OpenGL ES 2.0+, OES_depth_texture_cube_map.
1599 if (target
!= GL_TEXTURE_1D
&&
1600 target
!= GL_PROXY_TEXTURE_1D
&&
1601 target
!= GL_TEXTURE_2D
&&
1602 target
!= GL_PROXY_TEXTURE_2D
&&
1603 target
!= GL_TEXTURE_1D_ARRAY
&&
1604 target
!= GL_PROXY_TEXTURE_1D_ARRAY
&&
1605 target
!= GL_TEXTURE_2D_ARRAY
&&
1606 target
!= GL_PROXY_TEXTURE_2D_ARRAY
&&
1607 target
!= GL_TEXTURE_RECTANGLE_ARB
&&
1608 target
!= GL_PROXY_TEXTURE_RECTANGLE_ARB
&&
1609 !((_mesa_is_cube_face(target
) ||
1610 target
== GL_TEXTURE_CUBE_MAP
||
1611 target
== GL_PROXY_TEXTURE_CUBE_MAP
) &&
1612 (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_gpu_shader4
1613 || (ctx
->API
== API_OPENGLES2
&& ctx
->Extensions
.OES_depth_texture_cube_map
))) &&
1614 !((target
== GL_TEXTURE_CUBE_MAP_ARRAY
||
1615 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
) &&
1616 ctx
->Extensions
.ARB_texture_cube_map_array
)) {
1625 texture_formats_agree(GLenum internalFormat
,
1628 GLboolean colorFormat
;
1629 GLboolean is_format_depth_or_depthstencil
;
1630 GLboolean is_internalFormat_depth_or_depthstencil
;
1632 /* Even though there are no color-index textures, we still have to support
1633 * uploading color-index data and remapping it to RGB via the
1634 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
1636 const GLboolean indexFormat
= (format
== GL_COLOR_INDEX
);
1638 is_internalFormat_depth_or_depthstencil
=
1639 _mesa_is_depth_format(internalFormat
) ||
1640 _mesa_is_depthstencil_format(internalFormat
);
1642 is_format_depth_or_depthstencil
=
1643 _mesa_is_depth_format(format
) ||
1644 _mesa_is_depthstencil_format(format
);
1646 colorFormat
= _mesa_is_color_format(format
);
1648 if (_mesa_is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
)
1651 if (is_internalFormat_depth_or_depthstencil
!=
1652 is_format_depth_or_depthstencil
)
1655 if (_mesa_is_ycbcr_format(internalFormat
) != _mesa_is_ycbcr_format(format
))
1662 * Test the combination of format, type and internal format arguments of
1663 * different texture operations on GLES.
1665 * \param ctx GL context.
1666 * \param format pixel data format given by the user.
1667 * \param type pixel data type given by the user.
1668 * \param internalFormat internal format given by the user.
1669 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1670 * \param callerName name of the caller function to print in the error message
1672 * \return true if a error is found, false otherwise
1674 * Currently, it is used by texture_error_check() and texsubimage_error_check().
1677 texture_format_error_check_gles(struct gl_context
*ctx
, GLenum format
,
1678 GLenum type
, GLenum internalFormat
,
1679 GLuint dimensions
, const char *callerName
)
1683 if (_mesa_is_gles3(ctx
)) {
1684 err
= _mesa_es3_error_check_format_and_type(ctx
, format
, type
,
1686 if (err
!= GL_NO_ERROR
) {
1687 _mesa_error(ctx
, err
,
1688 "%s(format = %s, type = %s, internalformat = %s)",
1689 callerName
, _mesa_enum_to_string(format
),
1690 _mesa_enum_to_string(type
),
1691 _mesa_enum_to_string(internalFormat
));
1696 err
= _mesa_es_error_check_format_and_type(ctx
, format
, type
, dimensions
);
1697 if (err
!= GL_NO_ERROR
) {
1698 _mesa_error(ctx
, err
, "%s(format = %s, type = %s)",
1699 callerName
, _mesa_enum_to_string(format
),
1700 _mesa_enum_to_string(type
));
1709 * Test the glTexImage[123]D() parameters for errors.
1711 * \param ctx GL context.
1712 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1713 * \param target texture target given by the user (already validated).
1714 * \param level image level given by the user.
1715 * \param internalFormat internal format given by the user.
1716 * \param format pixel data format given by the user.
1717 * \param type pixel data type given by the user.
1718 * \param width image width given by the user.
1719 * \param height image height given by the user.
1720 * \param depth image depth given by the user.
1721 * \param border image border given by the user.
1723 * \return GL_TRUE if a error is found, GL_FALSE otherwise
1725 * Verifies each of the parameters against the constants specified in
1726 * __struct gl_contextRec::Const and the supported extensions, and according
1727 * to the OpenGL specification.
1728 * Note that we don't fully error-check the width, height, depth values
1729 * here. That's done in _mesa_legal_texture_dimensions() which is used
1730 * by several other GL entrypoints. Plus, texture dims have a special
1731 * interaction with proxy textures.
1734 texture_error_check( struct gl_context
*ctx
,
1735 GLuint dimensions
, GLenum target
,
1736 GLint level
, GLint internalFormat
,
1737 GLenum format
, GLenum type
,
1738 GLint width
, GLint height
,
1739 GLint depth
, GLint border
,
1740 const GLvoid
*pixels
)
1744 /* Note: for proxy textures, some error conditions immediately generate
1745 * a GL error in the usual way. But others do not generate a GL error.
1746 * Instead, they cause the width, height, depth, format fields of the
1747 * texture image to be zeroed-out. The GL spec seems to indicate that the
1748 * zero-out behaviour is only used in cases related to memory allocation.
1752 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
1753 _mesa_error(ctx
, GL_INVALID_VALUE
,
1754 "glTexImage%dD(level=%d)", dimensions
, level
);
1759 if (border
< 0 || border
> 1 ||
1760 ((ctx
->API
!= API_OPENGL_COMPAT
||
1761 target
== GL_TEXTURE_RECTANGLE_NV
||
1762 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1763 _mesa_error(ctx
, GL_INVALID_VALUE
,
1764 "glTexImage%dD(border=%d)", dimensions
, border
);
1768 if (width
< 0 || height
< 0 || depth
< 0) {
1769 _mesa_error(ctx
, GL_INVALID_VALUE
,
1770 "glTexImage%dD(width, height or depth < 0)", dimensions
);
1774 /* Check incoming image format and type */
1775 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1776 if (err
!= GL_NO_ERROR
) {
1777 /* Prior to OpenGL-ES 2.0, an INVALID_VALUE is expected instead of
1778 * INVALID_ENUM. From page 73 OpenGL ES 1.1 spec:
1780 * "Specifying a value for internalformat that is not one of the
1781 * above (acceptable) values generates the error INVALID VALUE."
1783 if (err
== GL_INVALID_ENUM
&& _mesa_is_gles(ctx
) && ctx
->Version
< 20)
1784 err
= GL_INVALID_VALUE
;
1786 _mesa_error(ctx
, err
,
1787 "glTexImage%dD(incompatible format = %s, type = %s)",
1788 dimensions
, _mesa_enum_to_string(format
),
1789 _mesa_enum_to_string(type
));
1793 /* Check internalFormat */
1794 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1795 _mesa_error(ctx
, GL_INVALID_VALUE
,
1796 "glTexImage%dD(internalFormat=%s)",
1797 dimensions
, _mesa_enum_to_string(internalFormat
));
1801 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1802 * combinations of format, internalFormat, and type that can be used.
1803 * Formats and types that require additional extensions (e.g., GL_FLOAT
1804 * requires GL_OES_texture_float) are filtered elsewhere.
1806 if (_mesa_is_gles(ctx
) &&
1807 texture_format_error_check_gles(ctx
, format
, type
, internalFormat
,
1808 dimensions
, "glTexImage%dD")) {
1812 /* validate the bound PBO, if any */
1813 if (!_mesa_validate_pbo_source(ctx
, dimensions
, &ctx
->Unpack
,
1814 width
, height
, depth
, format
, type
,
1815 INT_MAX
, pixels
, "glTexImage")) {
1819 /* make sure internal format and format basically agree */
1820 if (!texture_formats_agree(internalFormat
, format
)) {
1821 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1822 "glTexImage%dD(incompatible internalFormat = %s, format = %s)",
1823 dimensions
, _mesa_enum_to_string(internalFormat
),
1824 _mesa_enum_to_string(format
));
1828 /* additional checks for ycbcr textures */
1829 if (internalFormat
== GL_YCBCR_MESA
) {
1830 assert(ctx
->Extensions
.MESA_ycbcr_texture
);
1831 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
1832 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
1834 _mesa_snprintf(message
, sizeof(message
),
1835 "glTexImage%dD(format/type YCBCR mismatch)",
1837 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", message
);
1838 return GL_TRUE
; /* error */
1840 if (target
!= GL_TEXTURE_2D
&&
1841 target
!= GL_PROXY_TEXTURE_2D
&&
1842 target
!= GL_TEXTURE_RECTANGLE_NV
&&
1843 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
1844 _mesa_error(ctx
, GL_INVALID_ENUM
,
1845 "glTexImage%dD(bad target for YCbCr texture)",
1851 _mesa_snprintf(message
, sizeof(message
),
1852 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1853 dimensions
, border
);
1854 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", message
);
1859 /* additional checks for depth textures */
1860 if (!_mesa_legal_texture_base_format_for_target(ctx
, target
, internalFormat
)) {
1861 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1862 "glTexImage%dD(bad target for texture)", dimensions
);
1866 /* additional checks for compressed textures */
1867 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
1869 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
, &err
)) {
1870 _mesa_error(ctx
, err
,
1871 "glTexImage%dD(target can't be compressed)", dimensions
);
1874 if (_mesa_format_no_online_compression(ctx
, internalFormat
)) {
1875 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1876 "glTexImage%dD(no compression for format)", dimensions
);
1880 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1881 "glTexImage%dD(border!=0)", dimensions
);
1886 /* additional checks for integer textures */
1887 if ((ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) &&
1888 (_mesa_is_enum_format_integer(format
) !=
1889 _mesa_is_enum_format_integer(internalFormat
))) {
1890 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1891 "glTexImage%dD(integer/non-integer format mismatch)",
1896 if (!mutable_tex_object(ctx
, target
)) {
1897 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1898 "glTexImage%dD(immutable texture)", dimensions
);
1902 /* if we get here, the parameters are OK */
1908 * Error checking for glCompressedTexImage[123]D().
1909 * Note that the width, height and depth values are not fully error checked
1911 * \return GL_TRUE if a error is found, GL_FALSE otherwise
1914 compressed_texture_error_check(struct gl_context
*ctx
, GLint dimensions
,
1915 GLenum target
, GLint level
,
1916 GLenum internalFormat
, GLsizei width
,
1917 GLsizei height
, GLsizei depth
, GLint border
,
1918 GLsizei imageSize
, const GLvoid
*data
)
1920 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1922 GLenum error
= GL_NO_ERROR
;
1923 char *reason
= ""; /* no error */
1925 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
, &error
)) {
1930 /* This will detect any invalid internalFormat value */
1931 if (!_mesa_is_compressed_format(ctx
, internalFormat
)) {
1932 _mesa_error(ctx
, GL_INVALID_ENUM
,
1933 "glCompressedTexImage%dD(internalFormat=%s)",
1934 dimensions
, _mesa_enum_to_string(internalFormat
));
1938 /* validate the bound PBO, if any */
1939 if (!_mesa_validate_pbo_source_compressed(ctx
, dimensions
, &ctx
->Unpack
,
1941 "glCompressedTexImage")) {
1945 switch (internalFormat
) {
1946 case GL_PALETTE4_RGB8_OES
:
1947 case GL_PALETTE4_RGBA8_OES
:
1948 case GL_PALETTE4_R5_G6_B5_OES
:
1949 case GL_PALETTE4_RGBA4_OES
:
1950 case GL_PALETTE4_RGB5_A1_OES
:
1951 case GL_PALETTE8_RGB8_OES
:
1952 case GL_PALETTE8_RGBA8_OES
:
1953 case GL_PALETTE8_R5_G6_B5_OES
:
1954 case GL_PALETTE8_RGBA4_OES
:
1955 case GL_PALETTE8_RGB5_A1_OES
:
1956 /* check level (note that level should be zero or less!) */
1957 if (level
> 0 || level
< -maxLevels
) {
1959 error
= GL_INVALID_VALUE
;
1963 if (dimensions
!= 2) {
1964 reason
= "compressed paletted textures must be 2D";
1965 error
= GL_INVALID_OPERATION
;
1969 /* Figure out the expected texture size (in bytes). This will be
1970 * checked against the actual size later.
1972 expectedSize
= _mesa_cpal_compressed_size(level
, internalFormat
,
1975 /* This is for the benefit of the TestProxyTexImage below. It expects
1976 * level to be non-negative. OES_compressed_paletted_texture uses a
1977 * weird mechanism where the level specified to glCompressedTexImage2D
1978 * is -(n-1) number of levels in the texture, and the data specifies the
1979 * complete mipmap stack. This is done to ensure the palette is the
1980 * same for all levels.
1987 if (level
< 0 || level
>= maxLevels
) {
1989 error
= GL_INVALID_VALUE
;
1993 /* Figure out the expected texture size (in bytes). This will be
1994 * checked against the actual size later.
1996 expectedSize
= compressed_tex_size(width
, height
, depth
, internalFormat
);
2000 /* This should really never fail */
2001 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
2002 reason
= "internalFormat";
2003 error
= GL_INVALID_ENUM
;
2007 /* No compressed formats support borders at this time */
2009 reason
= "border != 0";
2010 error
= GL_INVALID_VALUE
;
2014 /* Check for invalid pixel storage modes */
2015 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dimensions
,
2017 "glCompressedTexImage")) {
2021 /* check image size in bytes */
2022 if (expectedSize
!= imageSize
) {
2023 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
2024 * if <imageSize> is not consistent with the format, dimensions, and
2025 * contents of the specified image.
2027 reason
= "imageSize inconsistent with width/height/format";
2028 error
= GL_INVALID_VALUE
;
2032 if (!mutable_tex_object(ctx
, target
)) {
2033 reason
= "immutable texture";
2034 error
= GL_INVALID_OPERATION
;
2041 /* Note: not all error paths exit through here. */
2042 _mesa_error(ctx
, error
, "glCompressedTexImage%dD(%s)",
2043 dimensions
, reason
);
2050 * Test glTexSubImage[123]D() parameters for errors.
2052 * \param ctx GL context.
2053 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2054 * \param target texture target given by the user (already validated)
2055 * \param level image level given by the user.
2056 * \param xoffset sub-image x offset given by the user.
2057 * \param yoffset sub-image y offset given by the user.
2058 * \param zoffset sub-image z offset given by the user.
2059 * \param format pixel data format given by the user.
2060 * \param type pixel data type given by the user.
2061 * \param width image width given by the user.
2062 * \param height image height given by the user.
2063 * \param depth image depth given by the user.
2065 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2067 * Verifies each of the parameters against the constants specified in
2068 * __struct gl_contextRec::Const and the supported extensions, and according
2069 * to the OpenGL specification.
2072 texsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2073 struct gl_texture_object
*texObj
,
2074 GLenum target
, GLint level
,
2075 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2076 GLint width
, GLint height
, GLint depth
,
2077 GLenum format
, GLenum type
, const GLvoid
*pixels
,
2078 bool dsa
, const char *callerName
)
2080 struct gl_texture_image
*texImage
;
2084 /* must be out of memory */
2085 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", callerName
);
2090 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2091 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(level=%d)", callerName
, level
);
2095 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
2097 /* non-existant texture level */
2098 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid texture image)",
2103 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
2104 if (err
!= GL_NO_ERROR
) {
2105 _mesa_error(ctx
, err
,
2106 "%s(incompatible format = %s, type = %s)",
2107 callerName
, _mesa_enum_to_string(format
),
2108 _mesa_enum_to_string(type
));
2112 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2113 * combinations of format, internalFormat, and type that can be used.
2114 * Formats and types that require additional extensions (e.g., GL_FLOAT
2115 * requires GL_OES_texture_float) are filtered elsewhere.
2117 if (_mesa_is_gles(ctx
) &&
2118 texture_format_error_check_gles(ctx
, format
, type
,
2119 texImage
->InternalFormat
,
2120 dimensions
, callerName
)) {
2124 /* validate the bound PBO, if any */
2125 if (!_mesa_validate_pbo_source(ctx
, dimensions
, &ctx
->Unpack
,
2126 width
, height
, depth
, format
, type
,
2127 INT_MAX
, pixels
, callerName
)) {
2131 if (error_check_subtexture_dimensions(ctx
, dimensions
,
2132 texImage
, xoffset
, yoffset
, zoffset
,
2133 width
, height
, depth
, callerName
)) {
2137 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2138 if (_mesa_format_no_online_compression(ctx
, texImage
->InternalFormat
)) {
2139 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2140 "%s(no compression for format)", callerName
);
2145 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
2146 /* both source and dest must be integer-valued, or neither */
2147 if (_mesa_is_format_integer_color(texImage
->TexFormat
) !=
2148 _mesa_is_enum_format_integer(format
)) {
2149 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2150 "%s(integer/non-integer format mismatch)", callerName
);
2160 * Test glCopyTexImage[12]D() parameters for errors.
2162 * \param ctx GL context.
2163 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2164 * \param target texture target given by the user.
2165 * \param level image level given by the user.
2166 * \param internalFormat internal format given by the user.
2167 * \param width image width given by the user.
2168 * \param height image height given by the user.
2169 * \param border texture border.
2171 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2173 * Verifies each of the parameters against the constants specified in
2174 * __struct gl_contextRec::Const and the supported extensions, and according
2175 * to the OpenGL specification.
2178 copytexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
2179 GLenum target
, GLint level
, GLint internalFormat
,
2180 GLint width
, GLint height
, GLint border
)
2183 GLint rb_base_format
;
2184 struct gl_renderbuffer
*rb
;
2185 GLenum rb_internal_format
;
2188 if (!legal_texsubimage_target(ctx
, dimensions
, target
, false)) {
2189 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexImage%uD(target=%s)",
2190 dimensions
, _mesa_enum_to_string(target
));
2195 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2196 _mesa_error(ctx
, GL_INVALID_VALUE
,
2197 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
2201 /* Check that the source buffer is complete */
2202 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2203 if (ctx
->ReadBuffer
->_Status
== 0) {
2204 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2206 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2207 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2208 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
2212 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2213 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2214 "glCopyTexImage%dD(multisample FBO)", dimensions
);
2220 if (border
< 0 || border
> 1 ||
2221 ((ctx
->API
!= API_OPENGL_COMPAT
||
2222 target
== GL_TEXTURE_RECTANGLE_NV
||
2223 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
2224 _mesa_error(ctx
, GL_INVALID_VALUE
,
2225 "glCopyTexImage%dD(border=%d)", dimensions
, border
);
2229 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2232 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
2233 switch (internalFormat
) {
2238 case GL_LUMINANCE_ALPHA
:
2241 _mesa_error(ctx
, GL_INVALID_ENUM
,
2242 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2243 _mesa_enum_to_string(internalFormat
));
2248 * Section 8.6 (Alternate Texture Image Specification Commands) of the
2249 * OpenGL 4.5 (Compatibility Profile) spec says:
2251 * "Parameters level, internalformat, and border are specified using
2252 * the same values, with the same meanings, as the corresponding
2253 * arguments of TexImage2D, except that internalformat may not be
2254 * specified as 1, 2, 3, or 4."
2256 if (internalFormat
>= 1 && internalFormat
<= 4) {
2257 _mesa_error(ctx
, GL_INVALID_ENUM
,
2258 "glCopyTexImage%dD(internalFormat=%d)", dimensions
,
2264 baseFormat
= _mesa_base_tex_format(ctx
, internalFormat
);
2265 if (baseFormat
< 0) {
2266 _mesa_error(ctx
, GL_INVALID_ENUM
,
2267 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2268 _mesa_enum_to_string(internalFormat
));
2272 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, internalFormat
);
2274 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2275 "glCopyTexImage%dD(read buffer)", dimensions
);
2279 rb_internal_format
= rb
->InternalFormat
;
2280 rb_base_format
= _mesa_base_tex_format(ctx
, rb
->InternalFormat
);
2281 if (_mesa_is_color_format(internalFormat
)) {
2282 if (rb_base_format
< 0) {
2283 _mesa_error(ctx
, GL_INVALID_VALUE
,
2284 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2285 _mesa_enum_to_string(internalFormat
));
2290 if (_mesa_is_gles(ctx
)) {
2292 if (_mesa_base_format_component_count(baseFormat
) >
2293 _mesa_base_format_component_count(rb_base_format
)) {
2296 if (baseFormat
== GL_DEPTH_COMPONENT
||
2297 baseFormat
== GL_DEPTH_STENCIL
||
2298 baseFormat
== GL_STENCIL_INDEX
||
2299 rb_base_format
== GL_DEPTH_COMPONENT
||
2300 rb_base_format
== GL_DEPTH_STENCIL
||
2301 rb_base_format
== GL_STENCIL_INDEX
||
2302 ((baseFormat
== GL_LUMINANCE_ALPHA
||
2303 baseFormat
== GL_ALPHA
) &&
2304 rb_base_format
!= GL_RGBA
) ||
2305 internalFormat
== GL_RGB9_E5
) {
2308 if (internalFormat
== GL_RGB9_E5
) {
2312 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2313 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2314 _mesa_enum_to_string(internalFormat
));
2319 if (_mesa_is_gles3(ctx
)) {
2320 bool rb_is_srgb
= false;
2321 bool dst_is_srgb
= false;
2323 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&&
2324 _mesa_get_format_color_encoding(rb
->Format
) == GL_SRGB
) {
2328 if (_mesa_get_linear_internalformat(internalFormat
) != internalFormat
) {
2332 if (rb_is_srgb
!= dst_is_srgb
) {
2333 /* Page 137 (page 149 of the PDF) in section 3.8.5 of the
2334 * OpenGLES 3.0.0 spec says:
2336 * "The error INVALID_OPERATION is also generated if the
2337 * value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the
2338 * framebuffer attachment corresponding to the read buffer
2339 * is LINEAR (see section 6.1.13) and internalformat is
2340 * one of the sRGB formats described in section 3.8.16, or
2341 * if the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING is
2342 * SRGB and internalformat is not one of the sRGB formats."
2344 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2345 "glCopyTexImage%dD(srgb usage mismatch)", dimensions
);
2349 /* Page 139, Table 3.15 of OpenGL ES 3.0 spec does not define ReadPixels
2350 * types for SNORM formats. Also, conversion to SNORM formats is not
2351 * allowed by Table 3.2 on Page 110.
2353 if (_mesa_is_enum_format_snorm(internalFormat
)) {
2354 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2355 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2356 _mesa_enum_to_string(internalFormat
));
2361 if (!_mesa_source_buffer_exists(ctx
, baseFormat
)) {
2362 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2363 "glCopyTexImage%dD(missing readbuffer)", dimensions
);
2367 /* From the EXT_texture_integer spec:
2369 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2370 * if the texture internalformat is an integer format and the read color
2371 * buffer is not an integer format, or if the internalformat is not an
2372 * integer format and the read color buffer is an integer format."
2374 if (_mesa_is_color_format(internalFormat
)) {
2375 bool is_int
= _mesa_is_enum_format_integer(internalFormat
);
2376 bool is_rbint
= _mesa_is_enum_format_integer(rb_internal_format
);
2377 bool is_unorm
= _mesa_is_enum_format_unorm(internalFormat
);
2378 bool is_rbunorm
= _mesa_is_enum_format_unorm(rb_internal_format
);
2379 if (is_int
|| is_rbint
) {
2380 if (is_int
!= is_rbint
) {
2381 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2382 "glCopyTexImage%dD(integer vs non-integer)", dimensions
);
2384 } else if (_mesa_is_gles(ctx
) &&
2385 _mesa_is_enum_format_unsigned_int(internalFormat
) !=
2386 _mesa_is_enum_format_unsigned_int(rb_internal_format
)) {
2387 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2388 "glCopyTexImage%dD(signed vs unsigned integer)", dimensions
);
2393 /* From page 138 of OpenGL ES 3.0 spec:
2394 * "The error INVALID_OPERATION is generated if floating-point RGBA
2395 * data is required; if signed integer RGBA data is required and the
2396 * format of the current color buffer is not signed integer; if
2397 * unsigned integer RGBA data is required and the format of the
2398 * current color buffer is not unsigned integer; or if fixed-point
2399 * RGBA data is required and the format of the current color buffer
2400 * is not fixed-point.
2402 if (_mesa_is_gles(ctx
) && is_unorm
!= is_rbunorm
)
2403 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2404 "glCopyTexImage%dD(unorm vs non-unorm)", dimensions
);
2407 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
2409 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
, &err
)) {
2410 _mesa_error(ctx
, err
,
2411 "glCopyTexImage%dD(target can't be compressed)", dimensions
);
2414 if (_mesa_format_no_online_compression(ctx
, internalFormat
)) {
2415 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2416 "glCopyTexImage%dD(no compression for format)", dimensions
);
2420 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2421 "glCopyTexImage%dD(border!=0)", dimensions
);
2426 if (!mutable_tex_object(ctx
, target
)) {
2427 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2428 "glCopyTexImage%dD(immutable texture)", dimensions
);
2432 /* if we get here, the parameters are OK */
2438 * Test glCopyTexSubImage[12]D() parameters for errors.
2439 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2442 copytexsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2443 const struct gl_texture_object
*texObj
,
2444 GLenum target
, GLint level
,
2445 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2446 GLint width
, GLint height
, const char *caller
)
2448 struct gl_texture_image
*texImage
;
2450 /* Check that the source buffer is complete */
2451 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2452 if (ctx
->ReadBuffer
->_Status
== 0) {
2453 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2455 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2456 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2457 "%s(invalid readbuffer)", caller
);
2461 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2462 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2463 "%s(multisample FBO)", caller
);
2469 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2470 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(level=%d)", caller
, level
);
2474 /* Get dest image pointers */
2476 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", caller
);
2480 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
2482 /* destination image does not exist */
2483 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2484 "%s(invalid texture image)", caller
);
2488 if (error_check_subtexture_dimensions(ctx
, dimensions
, texImage
,
2489 xoffset
, yoffset
, zoffset
,
2490 width
, height
, 1, caller
)) {
2494 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2495 if (_mesa_format_no_online_compression(ctx
, texImage
->InternalFormat
)) {
2496 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2497 "%s(no compression for format)", caller
);
2502 if (texImage
->InternalFormat
== GL_YCBCR_MESA
) {
2503 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s()", caller
);
2507 if (!_mesa_source_buffer_exists(ctx
, texImage
->_BaseFormat
)) {
2508 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2509 "%s(missing readbuffer, format=%s)", caller
,
2510 _mesa_enum_to_string(texImage
->_BaseFormat
));
2514 /* From the EXT_texture_integer spec:
2516 * "INVALID_OPERATION is generated by CopyTexImage* and
2517 * CopyTexSubImage* if the texture internalformat is an integer format
2518 * and the read color buffer is not an integer format, or if the
2519 * internalformat is not an integer format and the read color buffer
2520 * is an integer format."
2522 if (_mesa_is_color_format(texImage
->InternalFormat
)) {
2523 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
2525 if (_mesa_is_format_integer_color(rb
->Format
) !=
2526 _mesa_is_format_integer_color(texImage
->TexFormat
)) {
2527 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2528 "%s(integer vs non-integer)", caller
);
2533 /* if we get here, the parameters are OK */
2538 /** Callback info for walking over FBO hash table */
2541 struct gl_context
*ctx
;
2542 struct gl_texture_object
*texObj
;
2548 * Check render to texture callback. Called from _mesa_HashWalk().
2551 check_rtt_cb(GLuint key
, void *data
, void *userData
)
2553 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
2554 const struct cb_info
*info
= (struct cb_info
*) userData
;
2555 struct gl_context
*ctx
= info
->ctx
;
2556 const struct gl_texture_object
*texObj
= info
->texObj
;
2557 const GLuint level
= info
->level
, face
= info
->face
;
2559 /* If this is a user-created FBO */
2560 if (_mesa_is_user_fbo(fb
)) {
2562 /* check if any of the FBO's attachments point to 'texObj' */
2563 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2564 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2565 if (att
->Type
== GL_TEXTURE
&&
2566 att
->Texture
== texObj
&&
2567 att
->TextureLevel
== level
&&
2568 att
->CubeMapFace
== face
) {
2569 _mesa_update_texture_renderbuffer(ctx
, ctx
->DrawBuffer
, att
);
2570 assert(att
->Renderbuffer
->TexImage
);
2571 /* Mark fb status as indeterminate to force re-validation */
2580 * When a texture image is specified we have to check if it's bound to
2581 * any framebuffer objects (render to texture) in order to detect changes
2582 * in size or format since that effects FBO completeness.
2583 * Any FBOs rendering into the texture must be re-validated.
2586 _mesa_update_fbo_texture(struct gl_context
*ctx
,
2587 struct gl_texture_object
*texObj
,
2588 GLuint face
, GLuint level
)
2590 /* Only check this texture if it's been marked as RenderToTexture */
2591 if (texObj
->_RenderToTexture
) {
2592 struct cb_info info
;
2594 info
.texObj
= texObj
;
2597 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, check_rtt_cb
, &info
);
2603 * If the texture object's GenerateMipmap flag is set and we've
2604 * changed the texture base level image, regenerate the rest of the
2605 * mipmap levels now.
2608 check_gen_mipmap(struct gl_context
*ctx
, GLenum target
,
2609 struct gl_texture_object
*texObj
, GLint level
)
2611 if (texObj
->GenerateMipmap
&&
2612 level
== texObj
->BaseLevel
&&
2613 level
< texObj
->MaxLevel
) {
2614 assert(ctx
->Driver
.GenerateMipmap
);
2615 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2620 /** Debug helper: override the user-requested internal format */
2622 override_internal_format(GLenum internalFormat
, GLint width
, GLint height
)
2625 if (internalFormat
== GL_RGBA16F_ARB
||
2626 internalFormat
== GL_RGBA32F_ARB
) {
2627 printf("Convert rgba float tex to int %d x %d\n", width
, height
);
2630 else if (internalFormat
== GL_RGB16F_ARB
||
2631 internalFormat
== GL_RGB32F_ARB
) {
2632 printf("Convert rgb float tex to int %d x %d\n", width
, height
);
2635 else if (internalFormat
== GL_LUMINANCE_ALPHA16F_ARB
||
2636 internalFormat
== GL_LUMINANCE_ALPHA32F_ARB
) {
2637 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2638 return GL_LUMINANCE_ALPHA
;
2640 else if (internalFormat
== GL_LUMINANCE16F_ARB
||
2641 internalFormat
== GL_LUMINANCE32F_ARB
) {
2642 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2643 return GL_LUMINANCE
;
2645 else if (internalFormat
== GL_ALPHA16F_ARB
||
2646 internalFormat
== GL_ALPHA32F_ARB
) {
2647 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2651 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2652 internalFormat = GL_RGBA;
2656 return internalFormat
;
2659 return internalFormat
;
2665 * Choose the actual hardware format for a texture image.
2666 * Try to use the same format as the previous image level when possible.
2667 * Otherwise, ask the driver for the best format.
2668 * It's important to try to choose a consistant format for all levels
2669 * for efficient texture memory layout/allocation. In particular, this
2670 * comes up during automatic mipmap generation.
2673 _mesa_choose_texture_format(struct gl_context
*ctx
,
2674 struct gl_texture_object
*texObj
,
2675 GLenum target
, GLint level
,
2676 GLenum internalFormat
, GLenum format
, GLenum type
)
2680 /* see if we've already chosen a format for the previous level */
2682 struct gl_texture_image
*prevImage
=
2683 _mesa_select_tex_image(texObj
, target
, level
- 1);
2684 /* See if the prev level is defined and has an internal format which
2685 * matches the new internal format.
2688 prevImage
->Width
> 0 &&
2689 prevImage
->InternalFormat
== internalFormat
) {
2690 /* use the same format */
2691 assert(prevImage
->TexFormat
!= MESA_FORMAT_NONE
);
2692 return prevImage
->TexFormat
;
2696 /* If the application requested compression to an S3TC format but we don't
2697 * have the DXTn library, force a generic compressed format instead.
2699 if (internalFormat
!= format
&& format
!= GL_NONE
) {
2700 const GLenum before
= internalFormat
;
2702 switch (internalFormat
) {
2703 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
2704 if (!ctx
->Mesa_DXTn
)
2705 internalFormat
= GL_COMPRESSED_RGB
;
2707 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
2708 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
2709 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
2710 if (!ctx
->Mesa_DXTn
)
2711 internalFormat
= GL_COMPRESSED_RGBA
;
2717 if (before
!= internalFormat
) {
2719 "DXT compression requested (%s), "
2720 "but libtxc_dxtn library not installed. Using %s "
2722 _mesa_enum_to_string(before
),
2723 _mesa_enum_to_string(internalFormat
));
2727 /* choose format from scratch */
2728 f
= ctx
->Driver
.ChooseTextureFormat(ctx
, target
, internalFormat
,
2730 assert(f
!= MESA_FORMAT_NONE
);
2736 * Adjust pixel unpack params and image dimensions to strip off the
2737 * one-pixel texture border.
2739 * Gallium and intel don't support texture borders. They've seldem been used
2740 * and seldom been implemented correctly anyway.
2742 * \param unpackNew returns the new pixel unpack parameters
2745 strip_texture_border(GLenum target
,
2746 GLint
*width
, GLint
*height
, GLint
*depth
,
2747 const struct gl_pixelstore_attrib
*unpack
,
2748 struct gl_pixelstore_attrib
*unpackNew
)
2754 *unpackNew
= *unpack
;
2756 if (unpackNew
->RowLength
== 0)
2757 unpackNew
->RowLength
= *width
;
2759 if (unpackNew
->ImageHeight
== 0)
2760 unpackNew
->ImageHeight
= *height
;
2762 assert(*width
>= 3);
2763 unpackNew
->SkipPixels
++; /* skip the border */
2764 *width
= *width
- 2; /* reduce the width by two border pixels */
2766 /* The min height of a texture with a border is 3 */
2767 if (*height
>= 3 && target
!= GL_TEXTURE_1D_ARRAY
) {
2768 unpackNew
->SkipRows
++; /* skip the border */
2769 *height
= *height
- 2; /* reduce the height by two border pixels */
2773 target
!= GL_TEXTURE_2D_ARRAY
&&
2774 target
!= GL_TEXTURE_CUBE_MAP_ARRAY
) {
2775 unpackNew
->SkipImages
++; /* skip the border */
2776 *depth
= *depth
- 2; /* reduce the depth by two border pixels */
2782 * Common code to implement all the glTexImage1D/2D/3D functions
2783 * as well as glCompressedTexImage1D/2D/3D.
2784 * \param compressed only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
2785 * \param format the user's image format (only used if !compressed)
2786 * \param type the user's image type (only used if !compressed)
2787 * \param imageSize only used for glCompressedTexImage1D/2D/3D calls.
2790 teximage(struct gl_context
*ctx
, GLboolean compressed
, GLuint dims
,
2791 GLenum target
, GLint level
, GLint internalFormat
,
2792 GLsizei width
, GLsizei height
, GLsizei depth
,
2793 GLint border
, GLenum format
, GLenum type
,
2794 GLsizei imageSize
, const GLvoid
*pixels
)
2796 const char *func
= compressed
? "glCompressedTexImage" : "glTexImage";
2797 struct gl_pixelstore_attrib unpack_no_border
;
2798 const struct gl_pixelstore_attrib
*unpack
= &ctx
->Unpack
;
2799 struct gl_texture_object
*texObj
;
2800 mesa_format texFormat
;
2801 GLboolean dimensionsOK
, sizeOK
;
2803 FLUSH_VERTICES(ctx
, 0);
2805 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
)) {
2808 "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
2810 _mesa_enum_to_string(target
), level
,
2811 _mesa_enum_to_string(internalFormat
),
2812 width
, height
, depth
, border
, pixels
);
2815 "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2817 _mesa_enum_to_string(target
), level
,
2818 _mesa_enum_to_string(internalFormat
),
2819 width
, height
, depth
, border
,
2820 _mesa_enum_to_string(format
),
2821 _mesa_enum_to_string(type
), pixels
);
2824 internalFormat
= override_internal_format(internalFormat
, width
, height
);
2826 /* target error checking */
2827 if (!legal_teximage_target(ctx
, dims
, target
)) {
2828 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s%uD(target=%s)",
2829 func
, dims
, _mesa_enum_to_string(target
));
2833 /* general error checking */
2835 if (compressed_texture_error_check(ctx
, dims
, target
, level
,
2837 width
, height
, depth
,
2838 border
, imageSize
, pixels
))
2842 if (texture_error_check(ctx
, dims
, target
, level
, internalFormat
,
2843 format
, type
, width
, height
, depth
, border
,
2848 /* Here we convert a cpal compressed image into a regular glTexImage2D
2849 * call by decompressing the texture. If we really want to support cpal
2850 * textures in any driver this would have to be changed.
2852 if (ctx
->API
== API_OPENGLES
&& compressed
&& dims
== 2) {
2853 switch (internalFormat
) {
2854 case GL_PALETTE4_RGB8_OES
:
2855 case GL_PALETTE4_RGBA8_OES
:
2856 case GL_PALETTE4_R5_G6_B5_OES
:
2857 case GL_PALETTE4_RGBA4_OES
:
2858 case GL_PALETTE4_RGB5_A1_OES
:
2859 case GL_PALETTE8_RGB8_OES
:
2860 case GL_PALETTE8_RGBA8_OES
:
2861 case GL_PALETTE8_R5_G6_B5_OES
:
2862 case GL_PALETTE8_RGBA4_OES
:
2863 case GL_PALETTE8_RGB5_A1_OES
:
2864 _mesa_cpal_compressed_teximage2d(target
, level
, internalFormat
,
2865 width
, height
, imageSize
, pixels
);
2870 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2874 /* For glCompressedTexImage() the driver has no choice about the
2875 * texture format since we'll never transcode the user's compressed
2876 * image data. The internalFormat was error checked earlier.
2878 texFormat
= _mesa_glenum_to_compressed_format(internalFormat
);
2881 /* In case of HALF_FLOAT_OES or FLOAT_OES, find corresponding sized
2882 * internal floating point format for the given base format.
2884 if (_mesa_is_gles(ctx
) && format
== internalFormat
) {
2885 if (type
== GL_FLOAT
) {
2886 texObj
->_IsFloat
= GL_TRUE
;
2887 } else if (type
== GL_HALF_FLOAT_OES
|| type
== GL_HALF_FLOAT
) {
2888 texObj
->_IsHalfFloat
= GL_TRUE
;
2891 internalFormat
= adjust_for_oes_float_texture(format
, type
);
2894 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
2895 internalFormat
, format
, type
);
2898 assert(texFormat
!= MESA_FORMAT_NONE
);
2900 /* check that width, height, depth are legal for the mipmap level */
2901 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, level
, width
,
2902 height
, depth
, border
);
2904 /* check that the texture won't take too much memory, etc */
2905 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target(target
),
2907 width
, height
, depth
, border
);
2909 if (_mesa_is_proxy_texture(target
)) {
2910 /* Proxy texture: just clear or set state depending on error checking */
2911 struct gl_texture_image
*texImage
=
2912 get_proxy_tex_image(ctx
, target
, level
);
2915 return; /* GL_OUT_OF_MEMORY already recorded */
2917 if (dimensionsOK
&& sizeOK
) {
2918 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, depth
,
2919 border
, internalFormat
, texFormat
);
2922 clear_teximage_fields(texImage
);
2926 /* non-proxy target */
2927 const GLuint face
= _mesa_tex_target_to_face(target
);
2928 struct gl_texture_image
*texImage
;
2930 if (!dimensionsOK
) {
2931 _mesa_error(ctx
, GL_INVALID_VALUE
,
2932 "%s%uD(invalid width or height or depth)",
2938 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
2939 "%s%uD(image too large: %d x %d x %d, %s format)",
2940 func
, dims
, width
, height
, depth
,
2941 _mesa_enum_to_string(internalFormat
));
2945 /* Allow a hardware driver to just strip out the border, to provide
2946 * reliable but slightly incorrect hardware rendering instead of
2947 * rarely-tested software fallback rendering.
2949 if (border
&& ctx
->Const
.StripTextureBorder
) {
2950 strip_texture_border(target
, &width
, &height
, &depth
, unpack
,
2953 unpack
= &unpack_no_border
;
2956 if (ctx
->NewState
& _NEW_PIXEL
)
2957 _mesa_update_state(ctx
);
2959 _mesa_lock_texture(ctx
, texObj
);
2961 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2964 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s%uD", func
, dims
);
2967 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
2969 _mesa_init_teximage_fields(ctx
, texImage
,
2970 width
, height
, depth
,
2971 border
, internalFormat
, texFormat
);
2973 /* Give the texture to the driver. <pixels> may be null. */
2974 if (width
> 0 && height
> 0 && depth
> 0) {
2976 ctx
->Driver
.CompressedTexImage(ctx
, dims
, texImage
,
2980 ctx
->Driver
.TexImage(ctx
, dims
, texImage
, format
,
2981 type
, pixels
, unpack
);
2985 check_gen_mipmap(ctx
, target
, texObj
, level
);
2987 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
2989 _mesa_dirty_texobj(ctx
, texObj
);
2992 _mesa_unlock_texture(ctx
, texObj
);
2999 * Called from the API. Note that width includes the border.
3002 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
3003 GLsizei width
, GLint border
, GLenum format
,
3004 GLenum type
, const GLvoid
*pixels
)
3006 GET_CURRENT_CONTEXT(ctx
);
3007 teximage(ctx
, GL_FALSE
, 1, target
, level
, internalFormat
, width
, 1, 1,
3008 border
, format
, type
, 0, pixels
);
3013 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
3014 GLsizei width
, GLsizei height
, GLint border
,
3015 GLenum format
, GLenum type
,
3016 const GLvoid
*pixels
)
3018 GET_CURRENT_CONTEXT(ctx
);
3019 teximage(ctx
, GL_FALSE
, 2, target
, level
, internalFormat
, width
, height
, 1,
3020 border
, format
, type
, 0, pixels
);
3025 * Called by the API or display list executor.
3026 * Note that width and height include the border.
3029 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
3030 GLsizei width
, GLsizei height
, GLsizei depth
,
3031 GLint border
, GLenum format
, GLenum type
,
3032 const GLvoid
*pixels
)
3034 GET_CURRENT_CONTEXT(ctx
);
3035 teximage(ctx
, GL_FALSE
, 3, target
, level
, internalFormat
,
3036 width
, height
, depth
,
3037 border
, format
, type
, 0, pixels
);
3042 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
3043 GLsizei width
, GLsizei height
, GLsizei depth
,
3044 GLint border
, GLenum format
, GLenum type
,
3045 const GLvoid
*pixels
)
3047 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
3048 depth
, border
, format
, type
, pixels
);
3053 _mesa_EGLImageTargetTexture2DOES (GLenum target
, GLeglImageOES image
)
3055 struct gl_texture_object
*texObj
;
3056 struct gl_texture_image
*texImage
;
3058 GET_CURRENT_CONTEXT(ctx
);
3059 FLUSH_VERTICES(ctx
, 0);
3063 valid_target
= ctx
->Extensions
.OES_EGL_image
;
3065 case GL_TEXTURE_EXTERNAL_OES
:
3067 _mesa_is_gles(ctx
) ? ctx
->Extensions
.OES_EGL_image_external
: false;
3070 valid_target
= false;
3074 if (!valid_target
) {
3075 _mesa_error(ctx
, GL_INVALID_ENUM
,
3076 "glEGLImageTargetTexture2D(target=%d)", target
);
3081 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3082 "glEGLImageTargetTexture2D(image=%p)", image
);
3086 if (ctx
->NewState
& _NEW_PIXEL
)
3087 _mesa_update_state(ctx
);
3089 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3093 _mesa_lock_texture(ctx
, texObj
);
3095 if (texObj
->Immutable
) {
3096 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3097 "glEGLImageTargetTexture2D(texture is immutable)");
3098 _mesa_unlock_texture(ctx
, texObj
);
3102 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
3104 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glEGLImageTargetTexture2D");
3106 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3108 ctx
->Driver
.EGLImageTargetTexture2D(ctx
, target
,
3109 texObj
, texImage
, image
);
3111 _mesa_dirty_texobj(ctx
, texObj
);
3113 _mesa_unlock_texture(ctx
, texObj
);
3118 * Helper that implements the glTexSubImage1/2/3D()
3119 * and glTextureSubImage1/2/3D() functions.
3122 _mesa_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
3123 struct gl_texture_object
*texObj
,
3124 struct gl_texture_image
*texImage
,
3125 GLenum target
, GLint level
,
3126 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3127 GLsizei width
, GLsizei height
, GLsizei depth
,
3128 GLenum format
, GLenum type
, const GLvoid
*pixels
,
3131 FLUSH_VERTICES(ctx
, 0);
3133 if (ctx
->NewState
& _NEW_PIXEL
)
3134 _mesa_update_state(ctx
);
3136 _mesa_lock_texture(ctx
, texObj
);
3138 if (width
> 0 && height
> 0 && depth
> 0) {
3139 /* If we have a border, offset=-1 is legal. Bias by border width. */
3142 if (target
!= GL_TEXTURE_2D_ARRAY
)
3143 zoffset
+= texImage
->Border
;
3146 if (target
!= GL_TEXTURE_1D_ARRAY
)
3147 yoffset
+= texImage
->Border
;
3150 xoffset
+= texImage
->Border
;
3153 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3154 xoffset
, yoffset
, zoffset
,
3155 width
, height
, depth
,
3156 format
, type
, pixels
, &ctx
->Unpack
);
3158 check_gen_mipmap(ctx
, target
, texObj
, level
);
3160 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3161 * the texel data, not the texture format, size, etc.
3165 _mesa_unlock_texture(ctx
, texObj
);
3169 * Implement all the glTexSubImage1/2/3D() functions.
3170 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3173 texsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
3174 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3175 GLsizei width
, GLsizei height
, GLsizei depth
,
3176 GLenum format
, GLenum type
, const GLvoid
*pixels
,
3177 const char *callerName
)
3179 struct gl_texture_object
*texObj
;
3180 struct gl_texture_image
*texImage
;
3182 /* check target (proxies not allowed) */
3183 if (!legal_texsubimage_target(ctx
, dims
, target
, false)) {
3184 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage%uD(target=%s)",
3185 dims
, _mesa_enum_to_string(target
));
3189 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3193 if (texsubimage_error_check(ctx
, dims
, texObj
, target
, level
,
3194 xoffset
, yoffset
, zoffset
,
3195 width
, height
, depth
, format
, type
,
3196 pixels
, false, callerName
)) {
3197 return; /* error was detected */
3200 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
3201 /* texsubimage_error_check ensures that texImage is not NULL */
3203 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3204 _mesa_debug(ctx
, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3206 _mesa_enum_to_string(target
), level
,
3207 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3208 _mesa_enum_to_string(format
),
3209 _mesa_enum_to_string(type
), pixels
);
3211 _mesa_texture_sub_image(ctx
, dims
, texObj
, texImage
, target
, level
,
3212 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3213 format
, type
, pixels
, false);
3218 * Implement all the glTextureSubImage1/2/3D() functions.
3219 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3222 texturesubimage(struct gl_context
*ctx
, GLuint dims
,
3223 GLuint texture
, GLint level
,
3224 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3225 GLsizei width
, GLsizei height
, GLsizei depth
,
3226 GLenum format
, GLenum type
, const GLvoid
*pixels
,
3227 const char *callerName
)
3229 struct gl_texture_object
*texObj
;
3230 struct gl_texture_image
*texImage
;
3233 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3235 "glTextureSubImage%uD %d %d %d %d %d %d %d %d %s %s %p\n",
3236 dims
, texture
, level
,
3237 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3238 _mesa_enum_to_string(format
),
3239 _mesa_enum_to_string(type
), pixels
);
3241 /* Get the texture object by Name. */
3242 texObj
= _mesa_lookup_texture(ctx
, texture
);
3244 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTextureSubImage%uD(texture)",
3249 /* check target (proxies not allowed) */
3250 if (!legal_texsubimage_target(ctx
, dims
, texObj
->Target
, true)) {
3251 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target=%s)",
3252 callerName
, _mesa_enum_to_string(texObj
->Target
));
3256 if (texsubimage_error_check(ctx
, dims
, texObj
, texObj
->Target
, level
,
3257 xoffset
, yoffset
, zoffset
,
3258 width
, height
, depth
, format
, type
,
3259 pixels
, true, callerName
)) {
3260 return; /* error was detected */
3264 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
3265 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
3269 * What do we do if the user created a texture with the following code
3270 * and then called this function with its handle?
3273 * glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &tex);
3274 * glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
3275 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, ...);
3276 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, ...);
3277 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, ...);
3278 * // Note: GL_TEXTURE_CUBE_MAP_NEGATIVE_Y not set, or given the
3279 * // wrong format, or given the wrong size, etc.
3280 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, ...);
3281 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, ...);
3283 * A bug has been filed against the spec for this case. In the
3284 * meantime, we will check for cube completeness.
3286 * According to Section 8.17 Texture Completeness in the OpenGL 4.5
3287 * Core Profile spec (30.10.2014):
3288 * "[A] cube map texture is cube complete if the
3289 * following conditions all hold true: The [base level] texture
3290 * images of each of the six cube map faces have identical, positive,
3291 * and square dimensions. The [base level] images were each specified
3292 * with the same internal format."
3294 * It seems reasonable to check for cube completeness of an arbitrary
3295 * level here so that the image data has a consistent format and size.
3297 if (!_mesa_cube_level_complete(texObj
, level
)) {
3298 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3299 "glTextureSubImage%uD(cube map incomplete)",
3304 imageStride
= _mesa_image_image_stride(&ctx
->Unpack
, width
, height
,
3306 /* Copy in each face. */
3307 for (i
= zoffset
; i
< zoffset
+ depth
; ++i
) {
3308 texImage
= texObj
->Image
[i
][level
];
3311 _mesa_texture_sub_image(ctx
, 3, texObj
, texImage
, texObj
->Target
,
3312 level
, xoffset
, yoffset
, 0,
3313 width
, height
, 1, format
,
3314 type
, pixels
, true);
3315 pixels
= (GLubyte
*) pixels
+ imageStride
;
3319 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
3322 _mesa_texture_sub_image(ctx
, dims
, texObj
, texImage
, texObj
->Target
,
3323 level
, xoffset
, yoffset
, zoffset
,
3324 width
, height
, depth
, format
,
3325 type
, pixels
, true);
3331 _mesa_TexSubImage1D( GLenum target
, GLint level
,
3332 GLint xoffset
, GLsizei width
,
3333 GLenum format
, GLenum type
,
3334 const GLvoid
*pixels
)
3336 GET_CURRENT_CONTEXT(ctx
);
3337 texsubimage(ctx
, 1, target
, level
,
3340 format
, type
, pixels
, "glTexSubImage1D");
3345 _mesa_TexSubImage2D( GLenum target
, GLint level
,
3346 GLint xoffset
, GLint yoffset
,
3347 GLsizei width
, GLsizei height
,
3348 GLenum format
, GLenum type
,
3349 const GLvoid
*pixels
)
3351 GET_CURRENT_CONTEXT(ctx
);
3352 texsubimage(ctx
, 2, target
, level
,
3353 xoffset
, yoffset
, 0,
3355 format
, type
, pixels
, "glTexSubImage2D");
3361 _mesa_TexSubImage3D( GLenum target
, GLint level
,
3362 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3363 GLsizei width
, GLsizei height
, GLsizei depth
,
3364 GLenum format
, GLenum type
,
3365 const GLvoid
*pixels
)
3367 GET_CURRENT_CONTEXT(ctx
);
3368 texsubimage(ctx
, 3, target
, level
,
3369 xoffset
, yoffset
, zoffset
,
3370 width
, height
, depth
,
3371 format
, type
, pixels
, "glTexSubImage3D");
3375 _mesa_TextureSubImage1D(GLuint texture
, GLint level
,
3376 GLint xoffset
, GLsizei width
,
3377 GLenum format
, GLenum type
,
3378 const GLvoid
*pixels
)
3380 GET_CURRENT_CONTEXT(ctx
);
3381 texturesubimage(ctx
, 1, texture
, level
,
3384 format
, type
, pixels
, "glTextureSubImage1D");
3389 _mesa_TextureSubImage2D(GLuint texture
, GLint level
,
3390 GLint xoffset
, GLint yoffset
,
3391 GLsizei width
, GLsizei height
,
3392 GLenum format
, GLenum type
,
3393 const GLvoid
*pixels
)
3395 GET_CURRENT_CONTEXT(ctx
);
3396 texturesubimage(ctx
, 2, texture
, level
,
3397 xoffset
, yoffset
, 0,
3399 format
, type
, pixels
, "glTextureSubImage2D");
3404 _mesa_TextureSubImage3D(GLuint texture
, GLint level
,
3405 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3406 GLsizei width
, GLsizei height
, GLsizei depth
,
3407 GLenum format
, GLenum type
,
3408 const GLvoid
*pixels
)
3410 GET_CURRENT_CONTEXT(ctx
);
3411 texturesubimage(ctx
, 3, texture
, level
,
3412 xoffset
, yoffset
, zoffset
,
3413 width
, height
, depth
,
3414 format
, type
, pixels
, "glTextureSubImage3D");
3419 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3420 * from. This depends on whether the texture contains color or depth values.
3422 static struct gl_renderbuffer
*
3423 get_copy_tex_image_source(struct gl_context
*ctx
, mesa_format texFormat
)
3425 if (_mesa_get_format_bits(texFormat
, GL_DEPTH_BITS
) > 0) {
3426 /* reading from depth/stencil buffer */
3427 return ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
3430 /* copying from color buffer */
3431 return ctx
->ReadBuffer
->_ColorReadBuffer
;
3436 copytexsubimage_by_slice(struct gl_context
*ctx
,
3437 struct gl_texture_image
*texImage
,
3439 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3440 struct gl_renderbuffer
*rb
,
3442 GLsizei width
, GLsizei height
)
3444 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
3447 /* For 1D arrays, we copy each scanline of the source rectangle into the
3450 assert(zoffset
== 0);
3452 for (slice
= 0; slice
< height
; slice
++) {
3453 assert(yoffset
+ slice
< texImage
->Height
);
3454 ctx
->Driver
.CopyTexSubImage(ctx
, 2, texImage
,
3455 xoffset
, 0, yoffset
+ slice
,
3456 rb
, x
, y
+ slice
, width
, 1);
3459 ctx
->Driver
.CopyTexSubImage(ctx
, dims
, texImage
,
3460 xoffset
, yoffset
, zoffset
,
3461 rb
, x
, y
, width
, height
);
3466 formats_differ_in_component_sizes(mesa_format f1
, mesa_format f2
)
3468 GLint f1_r_bits
= _mesa_get_format_bits(f1
, GL_RED_BITS
);
3469 GLint f1_g_bits
= _mesa_get_format_bits(f1
, GL_GREEN_BITS
);
3470 GLint f1_b_bits
= _mesa_get_format_bits(f1
, GL_BLUE_BITS
);
3471 GLint f1_a_bits
= _mesa_get_format_bits(f1
, GL_ALPHA_BITS
);
3473 GLint f2_r_bits
= _mesa_get_format_bits(f2
, GL_RED_BITS
);
3474 GLint f2_g_bits
= _mesa_get_format_bits(f2
, GL_GREEN_BITS
);
3475 GLint f2_b_bits
= _mesa_get_format_bits(f2
, GL_BLUE_BITS
);
3476 GLint f2_a_bits
= _mesa_get_format_bits(f2
, GL_ALPHA_BITS
);
3478 if ((f1_r_bits
&& f2_r_bits
&& f1_r_bits
!= f2_r_bits
)
3479 || (f1_g_bits
&& f2_g_bits
&& f1_g_bits
!= f2_g_bits
)
3480 || (f1_b_bits
&& f2_b_bits
&& f1_b_bits
!= f2_b_bits
)
3481 || (f1_a_bits
&& f2_a_bits
&& f1_a_bits
!= f2_a_bits
))
3488 can_avoid_reallocation(struct gl_texture_image
*texImage
, GLenum internalFormat
,
3489 mesa_format texFormat
, GLint x
, GLint y
, GLsizei width
,
3490 GLsizei height
, GLint border
)
3492 if (texImage
->InternalFormat
!= internalFormat
)
3494 if (texImage
->TexFormat
!= texFormat
)
3496 if (texImage
->Border
!= border
)
3498 if (texImage
->Width2
!= width
)
3500 if (texImage
->Height2
!= height
)
3506 * Implement the glCopyTexImage1/2D() functions.
3509 copyteximage(struct gl_context
*ctx
, GLuint dims
,
3510 GLenum target
, GLint level
, GLenum internalFormat
,
3511 GLint x
, GLint y
, GLsizei width
, GLsizei height
, GLint border
)
3513 struct gl_texture_object
*texObj
;
3514 struct gl_texture_image
*texImage
;
3515 const GLuint face
= _mesa_tex_target_to_face(target
);
3516 mesa_format texFormat
;
3517 struct gl_renderbuffer
*rb
;
3519 FLUSH_VERTICES(ctx
, 0);
3521 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3522 _mesa_debug(ctx
, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3524 _mesa_enum_to_string(target
), level
,
3525 _mesa_enum_to_string(internalFormat
),
3526 x
, y
, width
, height
, border
);
3528 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3529 _mesa_update_state(ctx
);
3531 if (copytexture_error_check(ctx
, dims
, target
, level
, internalFormat
,
3532 width
, height
, border
))
3535 if (!_mesa_legal_texture_dimensions(ctx
, target
, level
, width
, height
,
3537 _mesa_error(ctx
, GL_INVALID_VALUE
,
3538 "glCopyTexImage%uD(invalid width or height)", dims
);
3542 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3545 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3546 internalFormat
, GL_NONE
, GL_NONE
);
3548 /* First check if reallocating the texture buffer can be avoided.
3549 * Without the realloc the copy can be 20x faster.
3551 _mesa_lock_texture(ctx
, texObj
);
3553 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
3554 if (texImage
&& can_avoid_reallocation(texImage
, internalFormat
, texFormat
,
3555 x
, y
, width
, height
, border
)) {
3556 _mesa_unlock_texture(ctx
, texObj
);
3557 return _mesa_copy_texture_sub_image(ctx
, dims
, texObj
, target
, level
,
3558 0, 0, 0, x
, y
, width
, height
,
3562 _mesa_unlock_texture(ctx
, texObj
);
3563 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_LOW
, "glCopyTexImage "
3564 "can't avoid reallocating texture storage\n");
3566 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, internalFormat
);
3568 if (_mesa_is_gles3(ctx
)) {
3569 if (_mesa_is_enum_format_unsized(internalFormat
)) {
3570 /* Conversion from GL_RGB10_A2 source buffer format is not allowed in
3571 * OpenGL ES 3.0. Khronos bug# 9807.
3573 if (rb
->InternalFormat
== GL_RGB10_A2
) {
3574 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3575 "glCopyTexImage%uD(Reading from GL_RGB10_A2 buffer"
3576 " and writing to unsized internal format)", dims
);
3580 /* From Page 139 of OpenGL ES 3.0 spec:
3581 * "If internalformat is sized, the internal format of the new texel
3582 * array is internalformat, and this is also the new texel array’s
3583 * effective internal format. If the component sizes of internalformat
3584 * do not exactly match the corresponding component sizes of the source
3585 * buffer’s effective internal format, described below, an
3586 * INVALID_OPERATION error is generated. If internalformat is unsized,
3587 * the internal format of the new texel array is the effective internal
3588 * format of the source buffer, and this is also the new texel array’s
3589 * effective internal format.
3591 else if (formats_differ_in_component_sizes (texFormat
, rb
->Format
)) {
3592 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3593 "glCopyTexImage%uD(componenet size changed in"
3594 " internal format)", dims
);
3599 assert(texFormat
!= MESA_FORMAT_NONE
);
3601 if (!ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target(target
),
3603 width
, height
, 1, border
)) {
3604 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3605 "glCopyTexImage%uD(image too large)", dims
);
3609 if (border
&& ctx
->Const
.StripTextureBorder
) {
3611 width
-= border
* 2;
3614 height
-= border
* 2;
3619 _mesa_lock_texture(ctx
, texObj
);
3621 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3624 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
3627 GLint srcX
= x
, srcY
= y
, dstX
= 0, dstY
= 0, dstZ
= 0;
3629 /* Free old texture image */
3630 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3632 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, 1,
3633 border
, internalFormat
, texFormat
);
3635 if (width
&& height
) {
3636 /* Allocate texture memory (no pixel data yet) */
3637 ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
);
3639 if (_mesa_clip_copytexsubimage(ctx
, &dstX
, &dstY
, &srcX
, &srcY
,
3641 struct gl_renderbuffer
*srcRb
=
3642 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3644 copytexsubimage_by_slice(ctx
, texImage
, dims
,
3646 srcRb
, srcX
, srcY
, width
, height
);
3649 check_gen_mipmap(ctx
, target
, texObj
, level
);
3652 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3654 _mesa_dirty_texobj(ctx
, texObj
);
3657 _mesa_unlock_texture(ctx
, texObj
);
3663 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
3664 GLenum internalFormat
,
3666 GLsizei width
, GLint border
)
3668 GET_CURRENT_CONTEXT(ctx
);
3669 copyteximage(ctx
, 1, target
, level
, internalFormat
, x
, y
, width
, 1, border
);
3675 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
3676 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
3679 GET_CURRENT_CONTEXT(ctx
);
3680 copyteximage(ctx
, 2, target
, level
, internalFormat
,
3681 x
, y
, width
, height
, border
);
3685 * Implementation for glCopyTex(ture)SubImage1/2/3D() functions.
3688 _mesa_copy_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
3689 struct gl_texture_object
*texObj
,
3690 GLenum target
, GLint level
,
3691 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3693 GLsizei width
, GLsizei height
,
3696 struct gl_texture_image
*texImage
;
3698 FLUSH_VERTICES(ctx
, 0);
3700 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3701 _mesa_debug(ctx
, "%s %s %d %d %d %d %d %d %d %d\n", caller
,
3702 _mesa_enum_to_string(target
),
3703 level
, xoffset
, yoffset
, zoffset
, x
, y
, width
, height
);
3705 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3706 _mesa_update_state(ctx
);
3708 if (copytexsubimage_error_check(ctx
, dims
, texObj
, target
, level
,
3709 xoffset
, yoffset
, zoffset
,
3710 width
, height
, caller
)) {
3714 _mesa_lock_texture(ctx
, texObj
);
3716 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
3718 /* If we have a border, offset=-1 is legal. Bias by border width. */
3721 if (target
!= GL_TEXTURE_2D_ARRAY
)
3722 zoffset
+= texImage
->Border
;
3725 if (target
!= GL_TEXTURE_1D_ARRAY
)
3726 yoffset
+= texImage
->Border
;
3729 xoffset
+= texImage
->Border
;
3732 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3734 struct gl_renderbuffer
*srcRb
=
3735 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3737 copytexsubimage_by_slice(ctx
, texImage
, dims
,
3738 xoffset
, yoffset
, zoffset
,
3739 srcRb
, x
, y
, width
, height
);
3741 check_gen_mipmap(ctx
, target
, texObj
, level
);
3743 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3744 * the texel data, not the texture format, size, etc.
3748 _mesa_unlock_texture(ctx
, texObj
);
3752 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
3753 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
3755 struct gl_texture_object
* texObj
;
3756 const char *self
= "glCopyTexSubImage1D";
3757 GET_CURRENT_CONTEXT(ctx
);
3759 /* Check target (proxies not allowed). Target must be checked prior to
3760 * calling _mesa_get_current_tex_object.
3762 if (!legal_texsubimage_target(ctx
, 1, target
, false)) {
3763 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3764 _mesa_enum_to_string(target
));
3768 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3772 _mesa_copy_texture_sub_image(ctx
, 1, texObj
, target
, level
, xoffset
, 0, 0,
3773 x
, y
, width
, 1, self
);
3779 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
3780 GLint xoffset
, GLint yoffset
,
3781 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3783 struct gl_texture_object
* texObj
;
3784 const char *self
= "glCopyTexSubImage2D";
3785 GET_CURRENT_CONTEXT(ctx
);
3787 /* Check target (proxies not allowed). Target must be checked prior to
3788 * calling _mesa_get_current_tex_object.
3790 if (!legal_texsubimage_target(ctx
, 2, target
, false)) {
3791 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3792 _mesa_enum_to_string(target
));
3796 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3800 _mesa_copy_texture_sub_image(ctx
, 2, texObj
, target
, level
,
3801 xoffset
, yoffset
, 0,
3802 x
, y
, width
, height
, self
);
3808 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
3809 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3810 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3812 struct gl_texture_object
* texObj
;
3813 const char *self
= "glCopyTexSubImage3D";
3814 GET_CURRENT_CONTEXT(ctx
);
3816 /* Check target (proxies not allowed). Target must be checked prior to
3817 * calling _mesa_get_current_tex_object.
3819 if (!legal_texsubimage_target(ctx
, 3, target
, false)) {
3820 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3821 _mesa_enum_to_string(target
));
3825 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3829 _mesa_copy_texture_sub_image(ctx
, 3, texObj
, target
, level
,
3830 xoffset
, yoffset
, zoffset
,
3831 x
, y
, width
, height
, self
);
3835 _mesa_CopyTextureSubImage1D(GLuint texture
, GLint level
,
3836 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
3838 struct gl_texture_object
* texObj
;
3839 const char *self
= "glCopyTextureSubImage1D";
3840 GET_CURRENT_CONTEXT(ctx
);
3842 texObj
= _mesa_lookup_texture_err(ctx
, texture
, self
);
3846 /* Check target (proxies not allowed). */
3847 if (!legal_texsubimage_target(ctx
, 1, texObj
->Target
, true)) {
3848 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3849 _mesa_enum_to_string(texObj
->Target
));
3853 _mesa_copy_texture_sub_image(ctx
, 1, texObj
, texObj
->Target
, level
,
3854 xoffset
, 0, 0, x
, y
, width
, 1, self
);
3858 _mesa_CopyTextureSubImage2D(GLuint texture
, GLint level
,
3859 GLint xoffset
, GLint yoffset
,
3860 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3862 struct gl_texture_object
* texObj
;
3863 const char *self
= "glCopyTextureSubImage2D";
3864 GET_CURRENT_CONTEXT(ctx
);
3866 texObj
= _mesa_lookup_texture_err(ctx
, texture
, self
);
3870 /* Check target (proxies not allowed). */
3871 if (!legal_texsubimage_target(ctx
, 2, texObj
->Target
, true)) {
3872 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3873 _mesa_enum_to_string(texObj
->Target
));
3877 _mesa_copy_texture_sub_image(ctx
, 2, texObj
, texObj
->Target
, level
,
3878 xoffset
, yoffset
, 0,
3879 x
, y
, width
, height
, self
);
3885 _mesa_CopyTextureSubImage3D(GLuint texture
, GLint level
,
3886 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3887 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3889 struct gl_texture_object
* texObj
;
3890 const char *self
= "glCopyTextureSubImage3D";
3891 GET_CURRENT_CONTEXT(ctx
);
3893 texObj
= _mesa_lookup_texture_err(ctx
, texture
, self
);
3897 /* Check target (proxies not allowed). */
3898 if (!legal_texsubimage_target(ctx
, 3, texObj
->Target
, true)) {
3899 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3900 _mesa_enum_to_string(texObj
->Target
));
3904 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
3905 /* Act like CopyTexSubImage2D */
3906 _mesa_copy_texture_sub_image(ctx
, 2, texObj
,
3907 GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ zoffset
,
3908 level
, xoffset
, yoffset
, 0,
3909 x
, y
, width
, height
, self
);
3912 _mesa_copy_texture_sub_image(ctx
, 3, texObj
, texObj
->Target
, level
,
3913 xoffset
, yoffset
, zoffset
,
3914 x
, y
, width
, height
, self
);
3918 check_clear_tex_image(struct gl_context
*ctx
,
3919 const char *function
,
3920 struct gl_texture_image
*texImage
,
3921 GLenum format
, GLenum type
,
3923 GLubyte
*clearValue
)
3925 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3926 static const GLubyte zeroData
[MAX_PIXEL_BYTES
];
3927 GLenum internalFormat
= texImage
->InternalFormat
;
3930 if (texObj
->Target
== GL_TEXTURE_BUFFER
) {
3931 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3932 "%s(buffer texture)", function
);
3936 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
3937 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3938 "%s(compressed texture)", function
);
3942 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
3943 if (err
!= GL_NO_ERROR
) {
3944 _mesa_error(ctx
, err
,
3945 "%s(incompatible format = %s, type = %s)",
3947 _mesa_enum_to_string(format
),
3948 _mesa_enum_to_string(type
));
3952 /* make sure internal format and format basically agree */
3953 if (!texture_formats_agree(internalFormat
, format
)) {
3954 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3955 "%s(incompatible internalFormat = %s, format = %s)",
3957 _mesa_enum_to_string(internalFormat
),
3958 _mesa_enum_to_string(format
));
3962 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
3963 /* both source and dest must be integer-valued, or neither */
3964 if (_mesa_is_format_integer_color(texImage
->TexFormat
) !=
3965 _mesa_is_enum_format_integer(format
)) {
3966 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3967 "%s(integer/non-integer format mismatch)",
3973 if (!_mesa_texstore(ctx
,
3975 texImage
->_BaseFormat
,
3976 texImage
->TexFormat
,
3977 0, /* dstRowStride */
3979 1, 1, 1, /* srcWidth/Height/Depth */
3981 data
? data
: zeroData
,
3982 &ctx
->DefaultPacking
)) {
3983 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid format)", function
);
3990 static struct gl_texture_object
*
3991 get_tex_obj_for_clear(struct gl_context
*ctx
,
3992 const char *function
,
3995 struct gl_texture_object
*texObj
;
3998 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(zero texture)", function
);
4002 texObj
= _mesa_lookup_texture(ctx
, texture
);
4004 if (texObj
== NULL
) {
4005 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", function
);
4009 if (texObj
->Target
== 0) {
4010 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unbound tex)", function
);
4018 get_tex_images_for_clear(struct gl_context
*ctx
,
4019 const char *function
,
4020 struct gl_texture_object
*texObj
,
4022 struct gl_texture_image
**texImages
)
4027 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
4028 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid level)", function
);
4032 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
4033 for (i
= 0; i
< MAX_FACES
; i
++) {
4034 target
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ i
;
4036 texImages
[i
] = _mesa_select_tex_image(texObj
, target
, level
);
4037 if (texImages
[i
] == NULL
) {
4038 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4039 "%s(invalid level)", function
);
4047 texImages
[0] = _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4049 if (texImages
[0] == NULL
) {
4050 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid level)", function
);
4058 _mesa_ClearTexSubImage( GLuint texture
, GLint level
,
4059 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4060 GLsizei width
, GLsizei height
, GLsizei depth
,
4061 GLenum format
, GLenum type
, const void *data
)
4063 GET_CURRENT_CONTEXT(ctx
);
4064 struct gl_texture_object
*texObj
;
4065 struct gl_texture_image
*texImages
[MAX_FACES
];
4066 GLubyte clearValue
[MAX_FACES
][MAX_PIXEL_BYTES
];
4068 int minDepth
, maxDepth
;
4070 texObj
= get_tex_obj_for_clear(ctx
, "glClearTexSubImage", texture
);
4075 _mesa_lock_texture(ctx
, texObj
);
4077 numImages
= get_tex_images_for_clear(ctx
, "glClearTexSubImage",
4078 texObj
, level
, texImages
);
4082 if (numImages
== 1) {
4083 minDepth
= -(int) texImages
[0]->Border
;
4084 maxDepth
= texImages
[0]->Depth
;
4087 maxDepth
= numImages
;
4090 if (xoffset
< -(GLint
) texImages
[0]->Border
||
4091 yoffset
< -(GLint
) texImages
[0]->Border
||
4092 zoffset
< minDepth
||
4096 xoffset
+ width
> texImages
[0]->Width
||
4097 yoffset
+ height
> texImages
[0]->Height
||
4098 zoffset
+ depth
> maxDepth
) {
4099 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4100 "glClearSubTexImage(invalid dimensions)");
4104 if (numImages
== 1) {
4105 if (check_clear_tex_image(ctx
, "glClearTexSubImage",
4107 format
, type
, data
, clearValue
[0])) {
4108 ctx
->Driver
.ClearTexSubImage(ctx
,
4110 xoffset
, yoffset
, zoffset
,
4111 width
, height
, depth
,
4112 data
? clearValue
[0] : NULL
);
4115 for (i
= zoffset
; i
< zoffset
+ depth
; i
++) {
4116 if (!check_clear_tex_image(ctx
, "glClearTexSubImage",
4118 format
, type
, data
, clearValue
[i
]))
4121 for (i
= zoffset
; i
< zoffset
+ depth
; i
++) {
4122 ctx
->Driver
.ClearTexSubImage(ctx
,
4124 xoffset
, yoffset
, 0,
4126 data
? clearValue
[i
] : NULL
);
4131 _mesa_unlock_texture(ctx
, texObj
);
4135 _mesa_ClearTexImage( GLuint texture
, GLint level
,
4136 GLenum format
, GLenum type
, const void *data
)
4138 GET_CURRENT_CONTEXT(ctx
);
4139 struct gl_texture_object
*texObj
;
4140 struct gl_texture_image
*texImages
[MAX_FACES
];
4141 GLubyte clearValue
[MAX_FACES
][MAX_PIXEL_BYTES
];
4144 texObj
= get_tex_obj_for_clear(ctx
, "glClearTexImage", texture
);
4149 _mesa_lock_texture(ctx
, texObj
);
4151 numImages
= get_tex_images_for_clear(ctx
, "glClearTexImage",
4152 texObj
, level
, texImages
);
4154 for (i
= 0; i
< numImages
; i
++) {
4155 if (!check_clear_tex_image(ctx
, "glClearTexImage",
4162 for (i
= 0; i
< numImages
; i
++) {
4163 ctx
->Driver
.ClearTexSubImage(ctx
, texImages
[i
],
4164 -(GLint
) texImages
[i
]->Border
, /* xoffset */
4165 -(GLint
) texImages
[i
]->Border
, /* yoffset */
4166 -(GLint
) texImages
[i
]->Border
, /* zoffset */
4167 texImages
[i
]->Width
,
4168 texImages
[i
]->Height
,
4169 texImages
[i
]->Depth
,
4170 data
? clearValue
[i
] : NULL
);
4174 _mesa_unlock_texture(ctx
, texObj
);
4180 /**********************************************************************/
4181 /****** Compressed Textures ******/
4182 /**********************************************************************/
4186 * Target checking for glCompressedTexSubImage[123]D().
4187 * \return GL_TRUE if error, GL_FALSE if no error
4188 * Must come before other error checking so that the texture object can
4189 * be correctly retrieved using _mesa_get_current_tex_object.
4192 compressed_subtexture_target_check(struct gl_context
*ctx
, GLenum target
,
4193 GLint dims
, GLenum format
, bool dsa
,
4198 if (dsa
&& target
== GL_TEXTURE_RECTANGLE
) {
4199 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid target %s)", caller
,
4200 _mesa_enum_to_string(target
));
4210 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
4211 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
4212 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
4213 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
4214 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
4215 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
4216 targetOK
= ctx
->Extensions
.ARB_texture_cube_map
;
4219 targetOK
= GL_FALSE
;
4225 case GL_TEXTURE_CUBE_MAP
:
4226 targetOK
= dsa
&& ctx
->Extensions
.ARB_texture_cube_map
;
4228 case GL_TEXTURE_2D_ARRAY
:
4229 targetOK
= _mesa_is_gles3(ctx
) ||
4230 (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
);
4232 case GL_TEXTURE_CUBE_MAP_ARRAY
:
4233 targetOK
= ctx
->Extensions
.ARB_texture_cube_map_array
;
4238 * OpenGL 4.5 spec (30.10.2014) says in Section 8.7 Compressed Texture
4240 * "An INVALID_OPERATION error is generated by
4241 * CompressedTex*SubImage3D if the internal format of the texture
4242 * is one of the EAC, ETC2, or RGTC formats and either border is
4243 * non-zero, or the effective target for the texture is not
4244 * TEXTURE_2D_ARRAY."
4246 * NOTE: that's probably a spec error. It should probably say
4247 * "... or the effective target for the texture is not
4248 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP, nor
4249 * GL_TEXTURE_CUBE_MAP_ARRAY."
4250 * since those targets are 2D images and they support all compression
4253 * Instead of listing all these, just list those which are allowed,
4254 * which is (at this time) only bptc. Otherwise we'd say s3tc (and
4255 * more) are valid here, which they are not, but of course not
4256 * mentioned by core spec.
4259 /* These are the only 3D compression formats supported at this time */
4260 case GL_COMPRESSED_RGBA_BPTC_UNORM
:
4261 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM
:
4262 case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT
:
4263 case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT
:
4267 /* invalid format */
4268 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4269 "%s(invalid target %s for format %s)", caller
,
4270 _mesa_enum_to_string(target
),
4271 _mesa_enum_to_string(format
));
4276 targetOK
= GL_FALSE
;
4282 /* no 1D compressed textures at this time */
4283 targetOK
= GL_FALSE
;
4288 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", caller
,
4289 _mesa_enum_to_string(target
));
4297 * Error checking for glCompressedTexSubImage[123]D().
4298 * \return GL_TRUE if error, GL_FALSE if no error
4301 compressed_subtexture_error_check(struct gl_context
*ctx
, GLint dims
,
4302 const struct gl_texture_object
*texObj
,
4303 GLenum target
, GLint level
,
4304 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4305 GLsizei width
, GLsizei height
, GLsizei depth
,
4306 GLenum format
, GLsizei imageSize
,
4307 const GLvoid
*data
, const char *callerName
)
4309 struct gl_texture_image
*texImage
;
4312 /* this will catch any invalid compressed format token */
4313 if (!_mesa_is_compressed_format(ctx
, format
)) {
4314 _mesa_error(ctx
, GL_INVALID_ENUM
,
4315 "%s(format)", callerName
);
4319 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
4320 _mesa_error(ctx
, GL_INVALID_VALUE
,
4326 /* validate the bound PBO, if any */
4327 if (!_mesa_validate_pbo_source_compressed(ctx
, dims
, &ctx
->Unpack
,
4328 imageSize
, data
, callerName
)) {
4332 /* Check for invalid pixel storage modes */
4333 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dims
,
4334 &ctx
->Unpack
, callerName
)) {
4338 expectedSize
= compressed_tex_size(width
, height
, depth
, format
);
4339 if (expectedSize
!= imageSize
) {
4340 _mesa_error(ctx
, GL_INVALID_VALUE
,
4342 callerName
, imageSize
);
4346 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4348 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4349 "%s(invalid texture image)",
4354 if ((GLint
) format
!= texImage
->InternalFormat
) {
4355 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4357 callerName
, _mesa_enum_to_string(format
));
4361 if (compressedteximage_only_format(ctx
, format
)) {
4362 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4363 "%s(format=%s cannot be updated)",
4364 callerName
, _mesa_enum_to_string(format
));
4368 if (error_check_subtexture_dimensions(ctx
, dims
,
4369 texImage
, xoffset
, yoffset
, zoffset
,
4370 width
, height
, depth
,
4380 _mesa_CompressedTexImage1D(GLenum target
, GLint level
,
4381 GLenum internalFormat
, GLsizei width
,
4382 GLint border
, GLsizei imageSize
,
4385 GET_CURRENT_CONTEXT(ctx
);
4386 teximage(ctx
, GL_TRUE
, 1, target
, level
, internalFormat
,
4387 width
, 1, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4392 _mesa_CompressedTexImage2D(GLenum target
, GLint level
,
4393 GLenum internalFormat
, GLsizei width
,
4394 GLsizei height
, GLint border
, GLsizei imageSize
,
4397 GET_CURRENT_CONTEXT(ctx
);
4398 teximage(ctx
, GL_TRUE
, 2, target
, level
, internalFormat
,
4399 width
, height
, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4404 _mesa_CompressedTexImage3D(GLenum target
, GLint level
,
4405 GLenum internalFormat
, GLsizei width
,
4406 GLsizei height
, GLsizei depth
, GLint border
,
4407 GLsizei imageSize
, const GLvoid
*data
)
4409 GET_CURRENT_CONTEXT(ctx
);
4410 teximage(ctx
, GL_TRUE
, 3, target
, level
, internalFormat
,
4411 width
, height
, depth
, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4416 * Common helper for glCompressedTexSubImage1/2/3D() and
4417 * glCompressedTextureSubImage1/2/3D().
4420 _mesa_compressed_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
4421 struct gl_texture_object
*texObj
,
4422 struct gl_texture_image
*texImage
,
4423 GLenum target
, GLint level
,
4424 GLint xoffset
, GLint yoffset
,
4426 GLsizei width
, GLsizei height
,
4428 GLenum format
, GLsizei imageSize
,
4431 FLUSH_VERTICES(ctx
, 0);
4433 _mesa_lock_texture(ctx
, texObj
);
4435 if (width
> 0 && height
> 0 && depth
> 0) {
4436 ctx
->Driver
.CompressedTexSubImage(ctx
, dims
, texImage
,
4437 xoffset
, yoffset
, zoffset
,
4438 width
, height
, depth
,
4439 format
, imageSize
, data
);
4441 check_gen_mipmap(ctx
, target
, texObj
, level
);
4443 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
4444 * the texel data, not the texture format, size, etc.
4448 _mesa_unlock_texture(ctx
, texObj
);
4453 _mesa_CompressedTexSubImage1D(GLenum target
, GLint level
, GLint xoffset
,
4454 GLsizei width
, GLenum format
,
4455 GLsizei imageSize
, const GLvoid
*data
)
4457 struct gl_texture_object
*texObj
;
4458 struct gl_texture_image
*texImage
;
4460 GET_CURRENT_CONTEXT(ctx
);
4462 if (compressed_subtexture_target_check(ctx
, target
, 1, format
, false,
4463 "glCompressedTexSubImage1D")) {
4467 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4471 if (compressed_subtexture_error_check(ctx
, 1, texObj
, target
,
4472 level
, xoffset
, 0, 0,
4474 format
, imageSize
, data
,
4475 "glCompressedTexSubImage1D")) {
4479 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4482 _mesa_compressed_texture_sub_image(ctx
, 1, texObj
, texImage
, target
, level
,
4483 xoffset
, 0, 0, width
, 1, 1,
4484 format
, imageSize
, data
);
4488 _mesa_CompressedTextureSubImage1D(GLuint texture
, GLint level
, GLint xoffset
,
4489 GLsizei width
, GLenum format
,
4490 GLsizei imageSize
, const GLvoid
*data
)
4492 struct gl_texture_object
*texObj
;
4493 struct gl_texture_image
*texImage
;
4495 GET_CURRENT_CONTEXT(ctx
);
4497 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4498 "glCompressedTextureSubImage1D");
4502 if (compressed_subtexture_target_check(ctx
, texObj
->Target
, 1, format
, true,
4503 "glCompressedTextureSubImage1D")) {
4507 if (compressed_subtexture_error_check(ctx
, 1, texObj
, texObj
->Target
,
4508 level
, xoffset
, 0, 0,
4510 format
, imageSize
, data
,
4511 "glCompressedTextureSubImage1D")) {
4515 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4518 _mesa_compressed_texture_sub_image(ctx
, 1, texObj
, texImage
,
4519 texObj
->Target
, level
,
4520 xoffset
, 0, 0, width
, 1, 1,
4521 format
, imageSize
, data
);
4526 _mesa_CompressedTexSubImage2D(GLenum target
, GLint level
, GLint xoffset
,
4527 GLint yoffset
, GLsizei width
, GLsizei height
,
4528 GLenum format
, GLsizei imageSize
,
4531 struct gl_texture_object
*texObj
;
4532 struct gl_texture_image
*texImage
;
4534 GET_CURRENT_CONTEXT(ctx
);
4536 if (compressed_subtexture_target_check(ctx
, target
, 2, format
, false,
4537 "glCompressedTexSubImage2D")) {
4541 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4545 if (compressed_subtexture_error_check(ctx
, 2, texObj
, target
,
4546 level
, xoffset
, yoffset
, 0,
4548 format
, imageSize
, data
,
4549 "glCompressedTexSubImage2D")) {
4554 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4557 _mesa_compressed_texture_sub_image(ctx
, 2, texObj
, texImage
, target
, level
,
4558 xoffset
, yoffset
, 0, width
, height
, 1,
4559 format
, imageSize
, data
);
4563 _mesa_CompressedTextureSubImage2D(GLuint texture
, GLint level
, GLint xoffset
,
4565 GLsizei width
, GLsizei height
,
4566 GLenum format
, GLsizei imageSize
,
4569 struct gl_texture_object
*texObj
;
4570 struct gl_texture_image
*texImage
;
4572 GET_CURRENT_CONTEXT(ctx
);
4574 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4575 "glCompressedTextureSubImage2D");
4579 if (compressed_subtexture_target_check(ctx
, texObj
->Target
, 2, format
, true,
4580 "glCompressedTextureSubImage2D")) {
4584 if (compressed_subtexture_error_check(ctx
, 2, texObj
, texObj
->Target
,
4585 level
, xoffset
, yoffset
, 0,
4587 format
, imageSize
, data
,
4588 "glCompressedTextureSubImage2D")) {
4592 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4595 _mesa_compressed_texture_sub_image(ctx
, 2, texObj
, texImage
,
4596 texObj
->Target
, level
,
4597 xoffset
, yoffset
, 0, width
, height
, 1,
4598 format
, imageSize
, data
);
4602 _mesa_CompressedTexSubImage3D(GLenum target
, GLint level
, GLint xoffset
,
4603 GLint yoffset
, GLint zoffset
, GLsizei width
,
4604 GLsizei height
, GLsizei depth
, GLenum format
,
4605 GLsizei imageSize
, const GLvoid
*data
)
4607 struct gl_texture_object
*texObj
;
4608 struct gl_texture_image
*texImage
;
4610 GET_CURRENT_CONTEXT(ctx
);
4612 if (compressed_subtexture_target_check(ctx
, target
, 3, format
, false,
4613 "glCompressedTexSubImage3D")) {
4617 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4621 if (compressed_subtexture_error_check(ctx
, 3, texObj
, target
,
4622 level
, xoffset
, yoffset
, zoffset
,
4623 width
, height
, depth
,
4624 format
, imageSize
, data
,
4625 "glCompressedTexSubImage3D")) {
4630 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4633 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, texImage
, target
, level
,
4634 xoffset
, yoffset
, zoffset
,
4635 width
, height
, depth
,
4636 format
, imageSize
, data
);
4640 _mesa_CompressedTextureSubImage3D(GLuint texture
, GLint level
, GLint xoffset
,
4641 GLint yoffset
, GLint zoffset
, GLsizei width
,
4642 GLsizei height
, GLsizei depth
,
4643 GLenum format
, GLsizei imageSize
,
4646 struct gl_texture_object
*texObj
;
4647 struct gl_texture_image
*texImage
;
4649 GET_CURRENT_CONTEXT(ctx
);
4651 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4652 "glCompressedTextureSubImage3D");
4656 if (compressed_subtexture_target_check(ctx
, texObj
->Target
, 3, format
, true,
4657 "glCompressedTextureSubImage3D")) {
4661 if (compressed_subtexture_error_check(ctx
, 3, texObj
, texObj
->Target
,
4662 level
, xoffset
, yoffset
, zoffset
,
4663 width
, height
, depth
,
4664 format
, imageSize
, data
,
4665 "glCompressedTextureSubImage3D")) {
4669 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
4670 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
4671 const char *pixels
= data
;
4675 /* Make sure the texture object is a proper cube.
4676 * (See texturesubimage in teximage.c for details on why this check is
4679 if (!_mesa_cube_level_complete(texObj
, level
)) {
4680 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4681 "glCompressedTextureSubImage3D(cube map incomplete)");
4685 /* Copy in each face. */
4686 for (i
= 0; i
< 6; ++i
) {
4687 texImage
= texObj
->Image
[i
][level
];
4690 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, texImage
,
4691 texObj
->Target
, level
,
4692 xoffset
, yoffset
, zoffset
,
4694 format
, imageSize
, pixels
);
4696 /* Compressed images don't have a client format */
4697 image_stride
= _mesa_format_image_size(texImage
->TexFormat
,
4699 texImage
->Height
, 1);
4701 pixels
+= image_stride
;
4702 imageSize
-= image_stride
;
4706 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4709 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, texImage
,
4710 texObj
->Target
, level
,
4711 xoffset
, yoffset
, zoffset
,
4712 width
, height
, depth
,
4713 format
, imageSize
, data
);
4718 get_texbuffer_format(const struct gl_context
*ctx
, GLenum internalFormat
)
4720 if (ctx
->API
!= API_OPENGL_CORE
) {
4721 switch (internalFormat
) {
4723 return MESA_FORMAT_A_UNORM8
;
4725 return MESA_FORMAT_A_UNORM16
;
4726 case GL_ALPHA16F_ARB
:
4727 return MESA_FORMAT_A_FLOAT16
;
4728 case GL_ALPHA32F_ARB
:
4729 return MESA_FORMAT_A_FLOAT32
;
4730 case GL_ALPHA8I_EXT
:
4731 return MESA_FORMAT_A_SINT8
;
4732 case GL_ALPHA16I_EXT
:
4733 return MESA_FORMAT_A_SINT16
;
4734 case GL_ALPHA32I_EXT
:
4735 return MESA_FORMAT_A_SINT32
;
4736 case GL_ALPHA8UI_EXT
:
4737 return MESA_FORMAT_A_UINT8
;
4738 case GL_ALPHA16UI_EXT
:
4739 return MESA_FORMAT_A_UINT16
;
4740 case GL_ALPHA32UI_EXT
:
4741 return MESA_FORMAT_A_UINT32
;
4743 return MESA_FORMAT_L_UNORM8
;
4744 case GL_LUMINANCE16
:
4745 return MESA_FORMAT_L_UNORM16
;
4746 case GL_LUMINANCE16F_ARB
:
4747 return MESA_FORMAT_L_FLOAT16
;
4748 case GL_LUMINANCE32F_ARB
:
4749 return MESA_FORMAT_L_FLOAT32
;
4750 case GL_LUMINANCE8I_EXT
:
4751 return MESA_FORMAT_L_SINT8
;
4752 case GL_LUMINANCE16I_EXT
:
4753 return MESA_FORMAT_L_SINT16
;
4754 case GL_LUMINANCE32I_EXT
:
4755 return MESA_FORMAT_L_SINT32
;
4756 case GL_LUMINANCE8UI_EXT
:
4757 return MESA_FORMAT_L_UINT8
;
4758 case GL_LUMINANCE16UI_EXT
:
4759 return MESA_FORMAT_L_UINT16
;
4760 case GL_LUMINANCE32UI_EXT
:
4761 return MESA_FORMAT_L_UINT32
;
4762 case GL_LUMINANCE8_ALPHA8
:
4763 return MESA_FORMAT_L8A8_UNORM
;
4764 case GL_LUMINANCE16_ALPHA16
:
4765 return MESA_FORMAT_L16A16_UNORM
;
4766 case GL_LUMINANCE_ALPHA16F_ARB
:
4767 return MESA_FORMAT_LA_FLOAT16
;
4768 case GL_LUMINANCE_ALPHA32F_ARB
:
4769 return MESA_FORMAT_LA_FLOAT32
;
4770 case GL_LUMINANCE_ALPHA8I_EXT
:
4771 return MESA_FORMAT_LA_SINT8
;
4772 case GL_LUMINANCE_ALPHA16I_EXT
:
4773 return MESA_FORMAT_LA_SINT16
;
4774 case GL_LUMINANCE_ALPHA32I_EXT
:
4775 return MESA_FORMAT_LA_SINT32
;
4776 case GL_LUMINANCE_ALPHA8UI_EXT
:
4777 return MESA_FORMAT_LA_UINT8
;
4778 case GL_LUMINANCE_ALPHA16UI_EXT
:
4779 return MESA_FORMAT_LA_UINT16
;
4780 case GL_LUMINANCE_ALPHA32UI_EXT
:
4781 return MESA_FORMAT_LA_UINT32
;
4783 return MESA_FORMAT_I_UNORM8
;
4784 case GL_INTENSITY16
:
4785 return MESA_FORMAT_I_UNORM16
;
4786 case GL_INTENSITY16F_ARB
:
4787 return MESA_FORMAT_I_FLOAT16
;
4788 case GL_INTENSITY32F_ARB
:
4789 return MESA_FORMAT_I_FLOAT32
;
4790 case GL_INTENSITY8I_EXT
:
4791 return MESA_FORMAT_I_SINT8
;
4792 case GL_INTENSITY16I_EXT
:
4793 return MESA_FORMAT_I_SINT16
;
4794 case GL_INTENSITY32I_EXT
:
4795 return MESA_FORMAT_I_SINT32
;
4796 case GL_INTENSITY8UI_EXT
:
4797 return MESA_FORMAT_I_UINT8
;
4798 case GL_INTENSITY16UI_EXT
:
4799 return MESA_FORMAT_I_UINT16
;
4800 case GL_INTENSITY32UI_EXT
:
4801 return MESA_FORMAT_I_UINT32
;
4807 if (ctx
->API
== API_OPENGL_CORE
&&
4808 ctx
->Extensions
.ARB_texture_buffer_object_rgb32
) {
4809 switch (internalFormat
) {
4811 return MESA_FORMAT_RGB_FLOAT32
;
4813 return MESA_FORMAT_RGB_UINT32
;
4815 return MESA_FORMAT_RGB_SINT32
;
4821 switch (internalFormat
) {
4823 return MESA_FORMAT_R8G8B8A8_UNORM
;
4825 return MESA_FORMAT_RGBA_UNORM16
;
4826 case GL_RGBA16F_ARB
:
4827 return MESA_FORMAT_RGBA_FLOAT16
;
4828 case GL_RGBA32F_ARB
:
4829 return MESA_FORMAT_RGBA_FLOAT32
;
4831 return MESA_FORMAT_RGBA_SINT8
;
4832 case GL_RGBA16I_EXT
:
4833 return MESA_FORMAT_RGBA_SINT16
;
4834 case GL_RGBA32I_EXT
:
4835 return MESA_FORMAT_RGBA_SINT32
;
4836 case GL_RGBA8UI_EXT
:
4837 return MESA_FORMAT_RGBA_UINT8
;
4838 case GL_RGBA16UI_EXT
:
4839 return MESA_FORMAT_RGBA_UINT16
;
4840 case GL_RGBA32UI_EXT
:
4841 return MESA_FORMAT_RGBA_UINT32
;
4844 return MESA_FORMAT_R8G8_UNORM
;
4846 return MESA_FORMAT_R16G16_UNORM
;
4848 return MESA_FORMAT_RG_FLOAT16
;
4850 return MESA_FORMAT_RG_FLOAT32
;
4852 return MESA_FORMAT_RG_SINT8
;
4854 return MESA_FORMAT_RG_SINT16
;
4856 return MESA_FORMAT_RG_SINT32
;
4858 return MESA_FORMAT_RG_UINT8
;
4860 return MESA_FORMAT_RG_UINT16
;
4862 return MESA_FORMAT_RG_UINT32
;
4865 return MESA_FORMAT_R_UNORM8
;
4867 return MESA_FORMAT_R_UNORM16
;
4869 return MESA_FORMAT_R_FLOAT16
;
4871 return MESA_FORMAT_R_FLOAT32
;
4873 return MESA_FORMAT_R_SINT8
;
4875 return MESA_FORMAT_R_SINT16
;
4877 return MESA_FORMAT_R_SINT32
;
4879 return MESA_FORMAT_R_UINT8
;
4881 return MESA_FORMAT_R_UINT16
;
4883 return MESA_FORMAT_R_UINT32
;
4886 return MESA_FORMAT_NONE
;
4892 _mesa_validate_texbuffer_format(const struct gl_context
*ctx
,
4893 GLenum internalFormat
)
4895 mesa_format format
= get_texbuffer_format(ctx
, internalFormat
);
4898 if (format
== MESA_FORMAT_NONE
)
4899 return MESA_FORMAT_NONE
;
4901 datatype
= _mesa_get_format_datatype(format
);
4903 /* The GL_ARB_texture_buffer_object spec says:
4905 * "If ARB_texture_float is not supported, references to the
4906 * floating-point internal formats provided by that extension should be
4907 * removed, and such formats may not be passed to TexBufferARB."
4909 * As a result, GL_HALF_FLOAT internal format depends on both
4910 * GL_ARB_texture_float and GL_ARB_half_float_pixel.
4912 if ((datatype
== GL_FLOAT
|| datatype
== GL_HALF_FLOAT
) &&
4913 !ctx
->Extensions
.ARB_texture_float
)
4914 return MESA_FORMAT_NONE
;
4916 if (!ctx
->Extensions
.ARB_texture_rg
) {
4917 GLenum base_format
= _mesa_get_format_base_format(format
);
4918 if (base_format
== GL_R
|| base_format
== GL_RG
)
4919 return MESA_FORMAT_NONE
;
4922 if (!ctx
->Extensions
.ARB_texture_buffer_object_rgb32
) {
4923 GLenum base_format
= _mesa_get_format_base_format(format
);
4924 if (base_format
== GL_RGB
)
4925 return MESA_FORMAT_NONE
;
4932 _mesa_texture_buffer_range(struct gl_context
*ctx
,
4933 struct gl_texture_object
*texObj
,
4934 GLenum internalFormat
,
4935 struct gl_buffer_object
*bufObj
,
4936 GLintptr offset
, GLsizeiptr size
,
4941 /* NOTE: ARB_texture_buffer_object has interactions with
4942 * the compatibility profile that are not implemented.
4944 if (!(ctx
->API
== API_OPENGL_CORE
&&
4945 ctx
->Extensions
.ARB_texture_buffer_object
)) {
4946 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4947 "%s(ARB_texture_buffer_object is not"
4948 " implemented for the compatibility profile)", caller
);
4952 format
= _mesa_validate_texbuffer_format(ctx
, internalFormat
);
4953 if (format
== MESA_FORMAT_NONE
) {
4954 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat %s)",
4955 caller
, _mesa_enum_to_string(internalFormat
));
4959 FLUSH_VERTICES(ctx
, 0);
4961 _mesa_lock_texture(ctx
, texObj
);
4963 _mesa_reference_buffer_object(ctx
, &texObj
->BufferObject
, bufObj
);
4964 texObj
->BufferObjectFormat
= internalFormat
;
4965 texObj
->_BufferObjectFormat
= format
;
4966 texObj
->BufferOffset
= offset
;
4967 texObj
->BufferSize
= size
;
4969 _mesa_unlock_texture(ctx
, texObj
);
4971 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTextureBuffer
;
4974 bufObj
->UsageHistory
|= USAGE_TEXTURE_BUFFER
;
4980 * Make sure the texture buffer target is GL_TEXTURE_BUFFER.
4981 * Return true if it is, and return false if it is not
4982 * (and throw INVALID ENUM as dictated in the OpenGL 4.5
4983 * core spec, 02.02.2015, PDF page 245).
4986 check_texture_buffer_target(struct gl_context
*ctx
, GLenum target
,
4989 if (target
!= GL_TEXTURE_BUFFER_ARB
) {
4990 _mesa_error(ctx
, GL_INVALID_ENUM
,
4991 "%s(texture target is not GL_TEXTURE_BUFFER)", caller
);
4999 * Check for errors related to the texture buffer range.
5000 * Return false if errors are found, true if none are found.
5003 check_texture_buffer_range(struct gl_context
*ctx
,
5004 struct gl_buffer_object
*bufObj
,
5005 GLintptr offset
, GLsizeiptr size
,
5008 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5009 * Textures (PDF page 245):
5010 * "An INVALID_VALUE error is generated if offset is negative, if
5011 * size is less than or equal to zero, or if offset + size is greater
5012 * than the value of BUFFER_SIZE for the buffer bound to target."
5015 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset=%d < 0)", caller
,
5021 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size=%d <= 0)", caller
,
5026 if (offset
+ size
> bufObj
->Size
) {
5027 _mesa_error(ctx
, GL_INVALID_VALUE
,
5028 "%s(offset=%d + size=%d > buffer_size=%d)", caller
,
5029 (int) offset
, (int) size
, (int) bufObj
->Size
);
5033 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5034 * Textures (PDF page 245):
5035 * "An INVALID_VALUE error is generated if offset is not an integer
5036 * multiple of the value of TEXTURE_BUFFER_OFFSET_ALIGNMENT."
5038 if (offset
% ctx
->Const
.TextureBufferOffsetAlignment
) {
5039 _mesa_error(ctx
, GL_INVALID_VALUE
,
5040 "%s(invalid offset alignment)", caller
);
5048 /** GL_ARB_texture_buffer_object */
5050 _mesa_TexBuffer(GLenum target
, GLenum internalFormat
, GLuint buffer
)
5052 struct gl_texture_object
*texObj
;
5053 struct gl_buffer_object
*bufObj
;
5055 GET_CURRENT_CONTEXT(ctx
);
5057 /* Need to catch a bad target before it gets to
5058 * _mesa_get_current_tex_object.
5060 if (!check_texture_buffer_target(ctx
, target
, "glTexBuffer"))
5064 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glTexBuffer");
5070 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5074 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
, bufObj
, 0,
5075 buffer
? -1 : 0, "glTexBuffer");
5079 /** GL_ARB_texture_buffer_range */
5081 _mesa_TexBufferRange(GLenum target
, GLenum internalFormat
, GLuint buffer
,
5082 GLintptr offset
, GLsizeiptr size
)
5084 struct gl_texture_object
*texObj
;
5085 struct gl_buffer_object
*bufObj
;
5087 GET_CURRENT_CONTEXT(ctx
);
5089 /* Need to catch a bad target before it gets to
5090 * _mesa_get_current_tex_object.
5092 if (!check_texture_buffer_target(ctx
, target
, "glTexBufferRange"))
5096 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glTexBufferRange");
5100 if (!check_texture_buffer_range(ctx
, bufObj
, offset
, size
,
5101 "glTexBufferRange"))
5105 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5106 * Textures (PDF page 254):
5107 * "If buffer is zero, then any buffer object attached to the buffer
5108 * texture is detached, the values offset and size are ignored and
5109 * the state for offset and size for the buffer texture are reset to
5117 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5121 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
, bufObj
,
5122 offset
, size
, "glTexBufferRange");
5126 _mesa_TextureBuffer(GLuint texture
, GLenum internalFormat
, GLuint buffer
)
5128 struct gl_texture_object
*texObj
;
5129 struct gl_buffer_object
*bufObj
;
5131 GET_CURRENT_CONTEXT(ctx
);
5134 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glTextureBuffer");
5140 /* Get the texture object by Name. */
5141 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glTextureBuffer");
5145 if (!check_texture_buffer_target(ctx
, texObj
->Target
, "glTextureBuffer"))
5148 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
,
5149 bufObj
, 0, buffer
? -1 : 0, "glTextureBuffer");
5153 _mesa_TextureBufferRange(GLuint texture
, GLenum internalFormat
, GLuint buffer
,
5154 GLintptr offset
, GLsizeiptr size
)
5156 struct gl_texture_object
*texObj
;
5157 struct gl_buffer_object
*bufObj
;
5159 GET_CURRENT_CONTEXT(ctx
);
5162 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
5163 "glTextureBufferRange");
5167 if (!check_texture_buffer_range(ctx
, bufObj
, offset
, size
,
5168 "glTextureBufferRange"))
5172 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5173 * Textures (PDF page 254):
5174 * "If buffer is zero, then any buffer object attached to the buffer
5175 * texture is detached, the values offset and size are ignored and
5176 * the state for offset and size for the buffer texture are reset to
5184 /* Get the texture object by Name. */
5185 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glTextureBufferRange");
5189 if (!check_texture_buffer_target(ctx
, texObj
->Target
,
5190 "glTextureBufferRange"))
5193 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
,
5194 bufObj
, offset
, size
, "glTextureBufferRange");
5198 _mesa_is_renderable_texture_format(struct gl_context
*ctx
, GLenum internalformat
)
5200 /* Everything that is allowed for renderbuffers,
5201 * except for a base format of GL_STENCIL_INDEX, unless supported.
5203 GLenum baseFormat
= _mesa_base_fbo_format(ctx
, internalformat
);
5204 if (ctx
->Extensions
.ARB_texture_stencil8
)
5205 return baseFormat
!= 0;
5207 return baseFormat
!= 0 && baseFormat
!= GL_STENCIL_INDEX
;
5211 /** GL_ARB_texture_multisample */
5213 check_multisample_target(GLuint dims
, GLenum target
, bool dsa
)
5216 case GL_TEXTURE_2D_MULTISAMPLE
:
5218 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
5219 return dims
== 2 && !dsa
;
5220 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
5222 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
5223 return dims
== 3 && !dsa
;
5231 texture_image_multisample(struct gl_context
*ctx
, GLuint dims
,
5232 struct gl_texture_object
*texObj
,
5233 GLenum target
, GLsizei samples
,
5234 GLint internalformat
, GLsizei width
,
5235 GLsizei height
, GLsizei depth
,
5236 GLboolean fixedsamplelocations
,
5237 GLboolean immutable
, const char *func
)
5239 struct gl_texture_image
*texImage
;
5240 GLboolean sizeOK
, dimensionsOK
, samplesOK
;
5241 mesa_format texFormat
;
5242 GLenum sample_count_error
;
5243 bool dsa
= strstr(func
, "ture") ? true : false;
5245 if (!((ctx
->Extensions
.ARB_texture_multisample
5246 && _mesa_is_desktop_gl(ctx
))) && !_mesa_is_gles31(ctx
)) {
5247 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unsupported)", func
);
5252 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(samples < 1)", func
);
5256 if (!check_multisample_target(dims
, target
, dsa
)) {
5258 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(target)", func
);
5262 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
5267 /* check that the specified internalformat is color/depth/stencil-renderable;
5268 * refer GL3.1 spec 4.4.4
5271 if (immutable
&& !_mesa_is_legal_tex_storage_format(ctx
, internalformat
)) {
5272 _mesa_error(ctx
, GL_INVALID_ENUM
,
5273 "%s(internalformat=%s not legal for immutable-format)",
5274 func
, _mesa_enum_to_string(internalformat
));
5278 if (!_mesa_is_renderable_texture_format(ctx
, internalformat
)) {
5279 /* Page 172 of OpenGL ES 3.1 spec says:
5280 * "An INVALID_ENUM error is generated if sizedinternalformat is not
5281 * color-renderable, depth-renderable, or stencil-renderable (as
5282 * defined in section 9.4).
5284 * (Same error is also defined for desktop OpenGL for multisample
5285 * teximage/texstorage functions.)
5287 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalformat=%s)", func
,
5288 _mesa_enum_to_string(internalformat
));
5292 sample_count_error
= _mesa_check_sample_count(ctx
, target
,
5293 internalformat
, samples
);
5294 samplesOK
= sample_count_error
== GL_NO_ERROR
;
5296 /* Page 254 of OpenGL 4.4 spec says:
5297 * "Proxy arrays for two-dimensional multisample and two-dimensional
5298 * multisample array textures are operated on in the same way when
5299 * TexImage2DMultisample is called with target specified as
5300 * PROXY_TEXTURE_2D_MULTISAMPLE, or TexImage3DMultisample is called
5301 * with target specified as PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY.
5302 * However, if samples is not supported, then no error is generated.
5304 if (!samplesOK
&& !_mesa_is_proxy_texture(target
)) {
5305 _mesa_error(ctx
, sample_count_error
, "%s(samples)", func
);
5309 if (immutable
&& (!texObj
|| (texObj
->Name
== 0))) {
5310 _mesa_error(ctx
, GL_INVALID_OPERATION
,
5311 "%s(texture object 0)",
5316 texImage
= _mesa_get_tex_image(ctx
, texObj
, 0, 0);
5318 if (texImage
== NULL
) {
5319 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", func
);
5323 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, 0,
5324 internalformat
, GL_NONE
, GL_NONE
);
5325 assert(texFormat
!= MESA_FORMAT_NONE
);
5327 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, 0,
5328 width
, height
, depth
, 0);
5330 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, target
, 0, texFormat
,
5331 width
, height
, depth
, 0);
5333 if (_mesa_is_proxy_texture(target
)) {
5334 if (samplesOK
&& dimensionsOK
&& sizeOK
) {
5335 init_teximage_fields_ms(ctx
, texImage
, width
, height
, depth
, 0,
5336 internalformat
, texFormat
,
5337 samples
, fixedsamplelocations
);
5340 /* clear all image fields */
5341 clear_teximage_fields(texImage
);
5345 if (!dimensionsOK
) {
5346 _mesa_error(ctx
, GL_INVALID_VALUE
,
5347 "%s(invalid width or height)", func
);
5352 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(texture too large)", func
);
5356 /* Check if texObj->Immutable is set */
5357 if (texObj
->Immutable
) {
5358 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
5362 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
5364 init_teximage_fields_ms(ctx
, texImage
, width
, height
, depth
, 0,
5365 internalformat
, texFormat
,
5366 samples
, fixedsamplelocations
);
5368 if (width
> 0 && height
> 0 && depth
> 0) {
5369 if (!ctx
->Driver
.AllocTextureStorage(ctx
, texObj
, 1,
5370 width
, height
, depth
)) {
5371 /* tidy up the texture image state. strictly speaking,
5372 * we're allowed to just leave this in whatever state we
5373 * like, but being tidy is good.
5375 _mesa_init_teximage_fields(ctx
, texImage
,
5376 0, 0, 0, 0, GL_NONE
, MESA_FORMAT_NONE
);
5380 texObj
->Immutable
|= immutable
;
5383 _mesa_set_texture_view_state(ctx
, texObj
, target
, 1);
5386 _mesa_update_fbo_texture(ctx
, texObj
, 0, 0);
5392 _mesa_TexImage2DMultisample(GLenum target
, GLsizei samples
,
5393 GLenum internalformat
, GLsizei width
,
5394 GLsizei height
, GLboolean fixedsamplelocations
)
5396 struct gl_texture_object
*texObj
;
5397 GET_CURRENT_CONTEXT(ctx
);
5399 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5403 texture_image_multisample(ctx
, 2, texObj
, target
, samples
,
5404 internalformat
, width
, height
, 1,
5405 fixedsamplelocations
, GL_FALSE
,
5406 "glTexImage2DMultisample");
5411 _mesa_TexImage3DMultisample(GLenum target
, GLsizei samples
,
5412 GLenum internalformat
, GLsizei width
,
5413 GLsizei height
, GLsizei depth
,
5414 GLboolean fixedsamplelocations
)
5416 struct gl_texture_object
*texObj
;
5417 GET_CURRENT_CONTEXT(ctx
);
5419 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5423 texture_image_multisample(ctx
, 3, texObj
, target
, samples
,
5424 internalformat
, width
, height
, depth
,
5425 fixedsamplelocations
, GL_FALSE
,
5426 "glTexImage3DMultisample");
5430 valid_texstorage_ms_parameters(GLsizei width
, GLsizei height
, GLsizei depth
,
5431 GLsizei samples
, unsigned dims
)
5433 GET_CURRENT_CONTEXT(ctx
);
5435 if (!_mesa_valid_tex_storage_dim(width
, height
, depth
)) {
5436 _mesa_error(ctx
, GL_INVALID_VALUE
,
5437 "glTexStorage%uDMultisample(width=%d,height=%d,depth=%d)",
5438 dims
, width
, height
, depth
);
5445 _mesa_TexStorage2DMultisample(GLenum target
, GLsizei samples
,
5446 GLenum internalformat
, GLsizei width
,
5447 GLsizei height
, GLboolean fixedsamplelocations
)
5449 struct gl_texture_object
*texObj
;
5450 GET_CURRENT_CONTEXT(ctx
);
5452 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5456 if (!valid_texstorage_ms_parameters(width
, height
, 1, samples
, 2))
5459 texture_image_multisample(ctx
, 2, texObj
, target
, samples
,
5460 internalformat
, width
, height
, 1,
5461 fixedsamplelocations
, GL_TRUE
,
5462 "glTexStorage2DMultisample");
5466 _mesa_TexStorage3DMultisample(GLenum target
, GLsizei samples
,
5467 GLenum internalformat
, GLsizei width
,
5468 GLsizei height
, GLsizei depth
,
5469 GLboolean fixedsamplelocations
)
5471 struct gl_texture_object
*texObj
;
5472 GET_CURRENT_CONTEXT(ctx
);
5474 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5478 if (!valid_texstorage_ms_parameters(width
, height
, depth
, samples
, 3))
5481 texture_image_multisample(ctx
, 3, texObj
, target
, samples
,
5482 internalformat
, width
, height
, depth
,
5483 fixedsamplelocations
, GL_TRUE
,
5484 "glTexStorage3DMultisample");
5488 _mesa_TextureStorage2DMultisample(GLuint texture
, GLsizei samples
,
5489 GLenum internalformat
, GLsizei width
,
5491 GLboolean fixedsamplelocations
)
5493 struct gl_texture_object
*texObj
;
5494 GET_CURRENT_CONTEXT(ctx
);
5496 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
5497 "glTextureStorage2DMultisample");
5501 if (!valid_texstorage_ms_parameters(width
, height
, 1, samples
, 2))
5504 texture_image_multisample(ctx
, 2, texObj
, texObj
->Target
, samples
,
5505 internalformat
, width
, height
, 1,
5506 fixedsamplelocations
, GL_TRUE
,
5507 "glTextureStorage2DMultisample");
5511 _mesa_TextureStorage3DMultisample(GLuint texture
, GLsizei samples
,
5512 GLenum internalformat
, GLsizei width
,
5513 GLsizei height
, GLsizei depth
,
5514 GLboolean fixedsamplelocations
)
5516 struct gl_texture_object
*texObj
;
5517 GET_CURRENT_CONTEXT(ctx
);
5519 /* Get the texture object by Name. */
5520 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
5521 "glTextureStorage3DMultisample");
5525 if (!valid_texstorage_ms_parameters(width
, height
, depth
, samples
, 3))
5528 texture_image_multisample(ctx
, 3, texObj
, texObj
->Target
, samples
,
5529 internalformat
, width
, height
, depth
,
5530 fixedsamplelocations
, GL_TRUE
,
5531 "glTextureStorage3DMultisample");