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(const struct gl_context
*ctx
,
76 GLenum format
, GLenum type
)
80 if (ctx
->Extensions
.OES_texture_float
) {
87 return GL_ALPHA32F_ARB
;
89 return GL_LUMINANCE32F_ARB
;
90 case GL_LUMINANCE_ALPHA
:
91 return GL_LUMINANCE_ALPHA32F_ARB
;
98 case GL_HALF_FLOAT_OES
:
99 if (ctx
->Extensions
.OES_texture_half_float
) {
106 return GL_ALPHA16F_ARB
;
108 return GL_LUMINANCE16F_ARB
;
109 case GL_LUMINANCE_ALPHA
:
110 return GL_LUMINANCE_ALPHA16F_ARB
;
126 * Install gl_texture_image in a gl_texture_object according to the target
127 * and level parameters.
129 * \param tObj texture object.
130 * \param target texture target.
131 * \param level image level.
132 * \param texImage texture image.
135 set_tex_image(struct gl_texture_object
*tObj
,
136 GLenum target
, GLint level
,
137 struct gl_texture_image
*texImage
)
139 const GLuint face
= _mesa_tex_target_to_face(target
);
143 if (target
== GL_TEXTURE_RECTANGLE_NV
|| target
== GL_TEXTURE_EXTERNAL_OES
)
146 tObj
->Image
[face
][level
] = texImage
;
148 /* Set the 'back' pointer */
149 texImage
->TexObject
= tObj
;
150 texImage
->Level
= level
;
151 texImage
->Face
= face
;
156 * Allocate a texture image structure.
158 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
161 * \return a pointer to gl_texture_image struct with all fields initialized to
164 struct gl_texture_image
*
165 _mesa_new_texture_image( struct gl_context
*ctx
)
168 return CALLOC_STRUCT(gl_texture_image
);
173 * Free a gl_texture_image and associated data.
174 * This function is a fallback called via ctx->Driver.DeleteTextureImage().
176 * \param texImage texture image.
178 * Free the texture image structure and the associated image data.
181 _mesa_delete_texture_image(struct gl_context
*ctx
,
182 struct gl_texture_image
*texImage
)
184 /* Free texImage->Data and/or any other driver-specific texture
187 assert(ctx
->Driver
.FreeTextureImageBuffer
);
188 ctx
->Driver
.FreeTextureImageBuffer( ctx
, texImage
);
194 * Test if a target is a proxy target.
196 * \param target texture target.
198 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
201 _mesa_is_proxy_texture(GLenum target
)
204 static const GLenum targets
[] = {
208 GL_PROXY_TEXTURE_CUBE_MAP
,
209 GL_PROXY_TEXTURE_RECTANGLE
,
210 GL_PROXY_TEXTURE_1D_ARRAY
,
211 GL_PROXY_TEXTURE_2D_ARRAY
,
212 GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
,
213 GL_PROXY_TEXTURE_2D_MULTISAMPLE
,
214 GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
217 * NUM_TEXTURE_TARGETS should match number of terms above, except there's no
218 * proxy for GL_TEXTURE_BUFFER and GL_TEXTURE_EXTERNAL_OES.
220 STATIC_ASSERT(NUM_TEXTURE_TARGETS
== ARRAY_SIZE(targets
) + 2);
222 for (i
= 0; i
< ARRAY_SIZE(targets
); ++i
)
223 if (target
== targets
[i
])
230 * Test if a target is an array target.
232 * \param target texture target.
234 * \return true if the target is an array target, false otherwise.
237 _mesa_is_array_texture(GLenum target
)
240 case GL_TEXTURE_1D_ARRAY
:
241 case GL_TEXTURE_2D_ARRAY
:
242 case GL_TEXTURE_CUBE_MAP_ARRAY
:
243 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
251 * Test if a target is a cube map.
253 * \param target texture target.
255 * \return true if the target is a cube map, false otherwise.
258 _mesa_is_cube_map_texture(GLenum target
)
261 case GL_TEXTURE_CUBE_MAP
:
262 case GL_TEXTURE_CUBE_MAP_ARRAY
:
270 * Return the proxy target which corresponds to the given texture target
273 proxy_target(GLenum target
)
277 case GL_PROXY_TEXTURE_1D
:
278 return GL_PROXY_TEXTURE_1D
;
280 case GL_PROXY_TEXTURE_2D
:
281 return GL_PROXY_TEXTURE_2D
;
283 case GL_PROXY_TEXTURE_3D
:
284 return GL_PROXY_TEXTURE_3D
;
285 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
286 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
287 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
288 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
289 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
290 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
291 case GL_TEXTURE_CUBE_MAP
:
292 case GL_PROXY_TEXTURE_CUBE_MAP
:
293 return GL_PROXY_TEXTURE_CUBE_MAP
;
294 case GL_TEXTURE_RECTANGLE_NV
:
295 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
296 return GL_PROXY_TEXTURE_RECTANGLE_NV
;
297 case GL_TEXTURE_1D_ARRAY_EXT
:
298 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
299 return GL_PROXY_TEXTURE_1D_ARRAY_EXT
;
300 case GL_TEXTURE_2D_ARRAY_EXT
:
301 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
302 return GL_PROXY_TEXTURE_2D_ARRAY_EXT
;
303 case GL_TEXTURE_CUBE_MAP_ARRAY
:
304 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
305 return GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
;
306 case GL_TEXTURE_2D_MULTISAMPLE
:
307 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
308 return GL_PROXY_TEXTURE_2D_MULTISAMPLE
;
309 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
310 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
311 return GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
;
313 _mesa_problem(NULL
, "unexpected target in proxy_target()");
322 * Get a texture image pointer from a texture object, given a texture
323 * target and mipmap level. The target and level parameters should
324 * have already been error-checked.
326 * \param texObj texture unit.
327 * \param target texture target.
328 * \param level image level.
330 * \return pointer to the texture image structure, or NULL on failure.
332 struct gl_texture_image
*
333 _mesa_select_tex_image(const struct gl_texture_object
*texObj
,
334 GLenum target
, GLint level
)
336 const GLuint face
= _mesa_tex_target_to_face(target
);
340 assert(level
< MAX_TEXTURE_LEVELS
);
342 return texObj
->Image
[face
][level
];
347 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
348 * it and install it. Only return NULL if passed a bad parameter or run
351 struct gl_texture_image
*
352 _mesa_get_tex_image(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
353 GLenum target
, GLint level
)
355 struct gl_texture_image
*texImage
;
360 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
362 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
364 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture image allocation");
368 set_tex_image(texObj
, target
, level
, texImage
);
376 * Return pointer to the specified proxy texture image.
377 * Note that proxy textures are per-context, not per-texture unit.
378 * \return pointer to texture image or NULL if invalid target, invalid
379 * level, or out of memory.
381 static struct gl_texture_image
*
382 get_proxy_tex_image(struct gl_context
*ctx
, GLenum target
, GLint level
)
384 struct gl_texture_image
*texImage
;
391 case GL_PROXY_TEXTURE_1D
:
392 if (level
>= ctx
->Const
.MaxTextureLevels
)
394 texIndex
= TEXTURE_1D_INDEX
;
396 case GL_PROXY_TEXTURE_2D
:
397 if (level
>= ctx
->Const
.MaxTextureLevels
)
399 texIndex
= TEXTURE_2D_INDEX
;
401 case GL_PROXY_TEXTURE_3D
:
402 if (level
>= ctx
->Const
.Max3DTextureLevels
)
404 texIndex
= TEXTURE_3D_INDEX
;
406 case GL_PROXY_TEXTURE_CUBE_MAP
:
407 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
409 texIndex
= TEXTURE_CUBE_INDEX
;
411 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
414 texIndex
= TEXTURE_RECT_INDEX
;
416 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
417 if (level
>= ctx
->Const
.MaxTextureLevels
)
419 texIndex
= TEXTURE_1D_ARRAY_INDEX
;
421 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
422 if (level
>= ctx
->Const
.MaxTextureLevels
)
424 texIndex
= TEXTURE_2D_ARRAY_INDEX
;
426 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
427 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
429 texIndex
= TEXTURE_CUBE_ARRAY_INDEX
;
431 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
434 texIndex
= TEXTURE_2D_MULTISAMPLE_INDEX
;
436 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
439 texIndex
= TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
;
445 texImage
= ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
];
447 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
449 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
452 ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
] = texImage
;
453 /* Set the 'back' pointer */
454 texImage
->TexObject
= ctx
->Texture
.ProxyTex
[texIndex
];
461 * Get the maximum number of allowed mipmap levels.
463 * \param ctx GL context.
464 * \param target texture target.
466 * \return the maximum number of allowed mipmap levels for the given
467 * texture target, or zero if passed a bad target.
472 _mesa_max_texture_levels(struct gl_context
*ctx
, GLenum target
)
476 case GL_PROXY_TEXTURE_1D
:
478 case GL_PROXY_TEXTURE_2D
:
479 return ctx
->Const
.MaxTextureLevels
;
481 case GL_PROXY_TEXTURE_3D
:
482 return ctx
->Const
.Max3DTextureLevels
;
483 case GL_TEXTURE_CUBE_MAP
:
484 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
485 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
486 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
487 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
488 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
489 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
490 case GL_PROXY_TEXTURE_CUBE_MAP
:
491 return ctx
->Extensions
.ARB_texture_cube_map
492 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
493 case GL_TEXTURE_RECTANGLE_NV
:
494 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
495 return ctx
->Extensions
.NV_texture_rectangle
? 1 : 0;
496 case GL_TEXTURE_1D_ARRAY_EXT
:
497 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
498 case GL_TEXTURE_2D_ARRAY_EXT
:
499 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
500 return ctx
->Extensions
.EXT_texture_array
501 ? ctx
->Const
.MaxTextureLevels
: 0;
502 case GL_TEXTURE_CUBE_MAP_ARRAY
:
503 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
504 return ctx
->Extensions
.ARB_texture_cube_map_array
505 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
506 case GL_TEXTURE_BUFFER
:
507 return (_mesa_has_ARB_texture_buffer_object(ctx
) ||
508 _mesa_has_OES_texture_buffer(ctx
)) ? 1 : 0;
509 case GL_TEXTURE_2D_MULTISAMPLE
:
510 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
511 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
512 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
513 return (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles31(ctx
))
514 && ctx
->Extensions
.ARB_texture_multisample
516 case GL_TEXTURE_EXTERNAL_OES
:
519 return 0; /* bad target */
525 * Return number of dimensions per mipmap level for the given texture target.
528 _mesa_get_texture_dimensions(GLenum target
)
532 case GL_PROXY_TEXTURE_1D
:
535 case GL_TEXTURE_RECTANGLE
:
536 case GL_TEXTURE_CUBE_MAP
:
537 case GL_PROXY_TEXTURE_2D
:
538 case GL_PROXY_TEXTURE_RECTANGLE
:
539 case GL_PROXY_TEXTURE_CUBE_MAP
:
540 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
541 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
542 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
543 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
544 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
545 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
546 case GL_TEXTURE_1D_ARRAY
:
547 case GL_PROXY_TEXTURE_1D_ARRAY
:
548 case GL_TEXTURE_EXTERNAL_OES
:
549 case GL_TEXTURE_2D_MULTISAMPLE
:
550 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
553 case GL_PROXY_TEXTURE_3D
:
554 case GL_TEXTURE_2D_ARRAY
:
555 case GL_PROXY_TEXTURE_2D_ARRAY
:
556 case GL_TEXTURE_CUBE_MAP_ARRAY
:
557 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
558 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
559 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
561 case GL_TEXTURE_BUFFER
:
564 _mesa_problem(NULL
, "invalid target 0x%x in get_texture_dimensions()",
572 * Check if a texture target can have more than one layer.
575 _mesa_tex_target_is_layered(GLenum target
)
579 case GL_PROXY_TEXTURE_1D
:
581 case GL_PROXY_TEXTURE_2D
:
582 case GL_TEXTURE_RECTANGLE
:
583 case GL_PROXY_TEXTURE_RECTANGLE
:
584 case GL_TEXTURE_2D_MULTISAMPLE
:
585 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
586 case GL_TEXTURE_BUFFER
:
587 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
588 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
589 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
590 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
591 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
592 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
593 case GL_TEXTURE_EXTERNAL_OES
:
597 case GL_PROXY_TEXTURE_3D
:
598 case GL_TEXTURE_CUBE_MAP
:
599 case GL_PROXY_TEXTURE_CUBE_MAP
:
600 case GL_TEXTURE_1D_ARRAY
:
601 case GL_PROXY_TEXTURE_1D_ARRAY
:
602 case GL_TEXTURE_2D_ARRAY
:
603 case GL_PROXY_TEXTURE_2D_ARRAY
:
604 case GL_TEXTURE_CUBE_MAP_ARRAY
:
605 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
606 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
607 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
611 assert(!"Invalid texture target.");
618 * Return the number of layers present in the given level of an array,
619 * cubemap or 3D texture. If the texture is not layered return zero.
622 _mesa_get_texture_layers(const struct gl_texture_object
*texObj
, GLint level
)
624 assert(level
>= 0 && level
< MAX_TEXTURE_LEVELS
);
626 switch (texObj
->Target
) {
629 case GL_TEXTURE_RECTANGLE
:
630 case GL_TEXTURE_2D_MULTISAMPLE
:
631 case GL_TEXTURE_BUFFER
:
632 case GL_TEXTURE_EXTERNAL_OES
:
635 case GL_TEXTURE_CUBE_MAP
:
638 case GL_TEXTURE_1D_ARRAY
: {
639 struct gl_texture_image
*img
= texObj
->Image
[0][level
];
640 return img
? img
->Height
: 0;
644 case GL_TEXTURE_2D_ARRAY
:
645 case GL_TEXTURE_CUBE_MAP_ARRAY
:
646 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
: {
647 struct gl_texture_image
*img
= texObj
->Image
[0][level
];
648 return img
? img
->Depth
: 0;
652 assert(!"Invalid texture target.");
659 * Return the maximum number of mipmap levels for the given target
660 * and the dimensions.
661 * The dimensions are expected not to include the border.
664 _mesa_get_tex_max_num_levels(GLenum target
, GLsizei width
, GLsizei height
,
671 case GL_TEXTURE_1D_ARRAY
:
672 case GL_PROXY_TEXTURE_1D
:
673 case GL_PROXY_TEXTURE_1D_ARRAY
:
676 case GL_TEXTURE_CUBE_MAP
:
677 case GL_TEXTURE_CUBE_MAP_ARRAY
:
678 case GL_PROXY_TEXTURE_CUBE_MAP
:
679 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
683 case GL_TEXTURE_2D_ARRAY
:
684 case GL_PROXY_TEXTURE_2D
:
685 case GL_PROXY_TEXTURE_2D_ARRAY
:
686 size
= MAX2(width
, height
);
689 case GL_PROXY_TEXTURE_3D
:
690 size
= MAX3(width
, height
, depth
);
692 case GL_TEXTURE_RECTANGLE
:
693 case GL_TEXTURE_EXTERNAL_OES
:
694 case GL_TEXTURE_2D_MULTISAMPLE
:
695 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
696 case GL_PROXY_TEXTURE_RECTANGLE
:
697 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
698 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
705 return _mesa_logbase2(size
) + 1;
709 #if 000 /* not used anymore */
711 * glTexImage[123]D can accept a NULL image pointer. In this case we
712 * create a texture image with unspecified image contents per the OpenGL
716 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
718 const GLint components
= _mesa_components_in_format(format
);
719 const GLint numPixels
= width
* height
* depth
;
720 GLubyte
*data
= (GLubyte
*) malloc(numPixels
* components
* sizeof(GLubyte
));
724 * Let's see if anyone finds this. If glTexImage2D() is called with
725 * a NULL image pointer then load the texture image with something
726 * interesting instead of leaving it indeterminate.
729 static const char message
[8][32] = {
733 " X X XXXX XXX XXXXX ",
736 " X X XXXXX XXX X X ",
740 GLubyte
*imgPtr
= data
;
742 for (h
= 0; h
< depth
; h
++) {
743 for (i
= 0; i
< height
; i
++) {
744 GLint srcRow
= 7 - (i
% 8);
745 for (j
= 0; j
< width
; j
++) {
746 GLint srcCol
= j
% 32;
747 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
748 for (k
= 0; k
< components
; k
++) {
764 * Set the size and format-related fields of a gl_texture_image struct
765 * to zero. This is used when a proxy texture test fails.
768 clear_teximage_fields(struct gl_texture_image
*img
)
771 img
->_BaseFormat
= 0;
772 img
->InternalFormat
= 0;
783 img
->TexFormat
= MESA_FORMAT_NONE
;
785 img
->FixedSampleLocations
= GL_TRUE
;
790 * Initialize basic fields of the gl_texture_image struct.
792 * \param ctx GL context.
793 * \param img texture image structure to be initialized.
794 * \param width image width.
795 * \param height image height.
796 * \param depth image depth.
797 * \param border image border.
798 * \param internalFormat internal format.
799 * \param format the actual hardware format (one of MESA_FORMAT_*)
800 * \param numSamples number of samples per texel, or zero for non-MS.
801 * \param fixedSampleLocations are sample locations fixed?
803 * Fills in the fields of \p img with the given information.
804 * Note: width, height and depth include the border.
807 init_teximage_fields_ms(struct gl_context
*ctx
,
808 struct gl_texture_image
*img
,
809 GLsizei width
, GLsizei height
, GLsizei depth
,
810 GLint border
, GLenum internalFormat
,
812 GLuint numSamples
, GLboolean fixedSampleLocations
)
820 target
= img
->TexObject
->Target
;
821 img
->_BaseFormat
= _mesa_base_tex_format( ctx
, internalFormat
);
822 assert(img
->_BaseFormat
!= -1);
823 img
->InternalFormat
= internalFormat
;
824 img
->Border
= border
;
826 img
->Height
= height
;
829 img
->Width2
= width
- 2 * border
; /* == 1 << img->WidthLog2; */
830 img
->WidthLog2
= _mesa_logbase2(img
->Width2
);
833 img
->FixedSampleLocations
= GL_TRUE
;
837 case GL_TEXTURE_BUFFER
:
838 case GL_PROXY_TEXTURE_1D
:
850 case GL_TEXTURE_1D_ARRAY
:
851 case GL_PROXY_TEXTURE_1D_ARRAY
:
852 img
->Height2
= height
; /* no border */
853 img
->HeightLog2
= 0; /* not used */
861 case GL_TEXTURE_RECTANGLE
:
862 case GL_TEXTURE_CUBE_MAP
:
863 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
864 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
865 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
866 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
867 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
868 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
869 case GL_TEXTURE_EXTERNAL_OES
:
870 case GL_PROXY_TEXTURE_2D
:
871 case GL_PROXY_TEXTURE_RECTANGLE
:
872 case GL_PROXY_TEXTURE_CUBE_MAP
:
873 case GL_TEXTURE_2D_MULTISAMPLE
:
874 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
875 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
876 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
883 case GL_TEXTURE_2D_ARRAY
:
884 case GL_PROXY_TEXTURE_2D_ARRAY
:
885 case GL_TEXTURE_CUBE_MAP_ARRAY
:
886 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
887 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
888 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
889 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
890 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
891 img
->Depth2
= depth
; /* no border */
892 img
->DepthLog2
= 0; /* not used */
895 case GL_PROXY_TEXTURE_3D
:
896 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
897 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
898 img
->Depth2
= depth
- 2 * border
; /* == 1 << img->DepthLog2; */
899 img
->DepthLog2
= _mesa_logbase2(img
->Depth2
);
902 _mesa_problem(NULL
, "invalid target 0x%x in _mesa_init_teximage_fields()",
907 _mesa_get_tex_max_num_levels(target
,
908 img
->Width2
, img
->Height2
, img
->Depth2
);
909 img
->TexFormat
= format
;
910 img
->NumSamples
= numSamples
;
911 img
->FixedSampleLocations
= fixedSampleLocations
;
916 _mesa_init_teximage_fields(struct gl_context
*ctx
,
917 struct gl_texture_image
*img
,
918 GLsizei width
, GLsizei height
, GLsizei depth
,
919 GLint border
, GLenum internalFormat
,
922 init_teximage_fields_ms(ctx
, img
, width
, height
, depth
, border
,
923 internalFormat
, format
, 0, GL_TRUE
);
928 * Free and clear fields of the gl_texture_image struct.
930 * \param ctx GL context.
931 * \param texImage texture image structure to be cleared.
933 * After the call, \p texImage will have no data associated with it. Its
934 * fields are cleared so that its parent object will test incomplete.
937 _mesa_clear_texture_image(struct gl_context
*ctx
,
938 struct gl_texture_image
*texImage
)
940 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
941 clear_teximage_fields(texImage
);
946 * Check the width, height, depth and border of a texture image are legal.
947 * Used by all the glTexImage, glCompressedTexImage and glCopyTexImage
949 * The target and level parameters will have already been validated.
950 * \return GL_TRUE if size is OK, GL_FALSE otherwise.
953 _mesa_legal_texture_dimensions(struct gl_context
*ctx
, GLenum target
,
954 GLint level
, GLint width
, GLint height
,
955 GLint depth
, GLint border
)
961 case GL_PROXY_TEXTURE_1D
:
962 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1); /* level zero size */
963 maxSize
>>= level
; /* level size */
964 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
966 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
967 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
973 case GL_PROXY_TEXTURE_2D
:
974 case GL_TEXTURE_2D_MULTISAMPLE
:
975 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
976 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
978 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
980 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
982 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
983 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
985 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
991 case GL_PROXY_TEXTURE_3D
:
992 maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
994 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
996 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
998 if (depth
< 2 * border
|| depth
> 2 * border
+ maxSize
)
1000 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1001 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1003 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1005 if (depth
> 0 && !_mesa_is_pow_two(depth
- 2 * border
))
1010 case GL_TEXTURE_RECTANGLE_NV
:
1011 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1014 maxSize
= ctx
->Const
.MaxTextureRectSize
;
1015 if (width
< 0 || width
> maxSize
)
1017 if (height
< 0 || height
> maxSize
)
1021 case GL_TEXTURE_CUBE_MAP
:
1022 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1023 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1024 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1025 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1026 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1027 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1028 case GL_PROXY_TEXTURE_CUBE_MAP
:
1029 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1031 if (width
!= height
)
1033 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1035 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1037 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1038 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1040 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1045 case GL_TEXTURE_1D_ARRAY_EXT
:
1046 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1047 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1049 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1051 if (height
< 0 || height
> ctx
->Const
.MaxArrayTextureLayers
)
1053 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1054 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1059 case GL_TEXTURE_2D_ARRAY_EXT
:
1060 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1061 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1062 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1063 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1065 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1067 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1069 if (depth
< 0 || depth
> ctx
->Const
.MaxArrayTextureLayers
)
1071 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1072 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1074 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1079 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1080 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1081 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1082 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1084 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1086 if (depth
< 0 || depth
> ctx
->Const
.MaxArrayTextureLayers
|| depth
% 6)
1088 if (width
!= height
)
1090 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
1092 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1093 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1095 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1100 _mesa_problem(ctx
, "Invalid target in _mesa_legal_texture_dimensions()");
1106 error_check_subtexture_negative_dimensions(struct gl_context
*ctx
,
1115 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width=%d)", func
, subWidth
);
1119 if (dims
> 1 && subHeight
< 0) {
1120 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height=%d)", func
, subHeight
);
1124 if (dims
> 2 && subDepth
< 0) {
1125 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(depth=%d)", func
, subDepth
);
1133 * Do error checking of xoffset, yoffset, zoffset, width, height and depth
1134 * for glTexSubImage, glCopyTexSubImage and glCompressedTexSubImage.
1135 * \param destImage the destination texture image.
1136 * \return GL_TRUE if error found, GL_FALSE otherwise.
1139 error_check_subtexture_dimensions(struct gl_context
*ctx
, GLuint dims
,
1140 const struct gl_texture_image
*destImage
,
1141 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1142 GLsizei subWidth
, GLsizei subHeight
,
1143 GLsizei subDepth
, const char *func
)
1145 const GLenum target
= destImage
->TexObject
->Target
;
1148 /* check xoffset and width */
1149 if (xoffset
< - (GLint
) destImage
->Border
) {
1150 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(xoffset)", func
);
1154 if (xoffset
+ subWidth
> (GLint
) destImage
->Width
) {
1155 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(xoffset+width)", func
);
1159 /* check yoffset and height */
1161 GLint yBorder
= (target
== GL_TEXTURE_1D_ARRAY
) ? 0 : destImage
->Border
;
1162 if (yoffset
< -yBorder
) {
1163 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(yoffset)", func
);
1166 if (yoffset
+ subHeight
> (GLint
) destImage
->Height
) {
1167 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(yoffset+height)", func
);
1172 /* check zoffset and depth */
1175 GLint zBorder
= (target
== GL_TEXTURE_2D_ARRAY
||
1176 target
== GL_TEXTURE_CUBE_MAP_ARRAY
) ?
1177 0 : destImage
->Border
;
1179 if (zoffset
< -zBorder
) {
1180 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(zoffset)", func
);
1184 depth
= (GLint
) destImage
->Depth
;
1185 if (target
== GL_TEXTURE_CUBE_MAP
)
1187 if (zoffset
+ subDepth
> depth
) {
1188 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(zoffset+depth)", func
);
1194 * The OpenGL spec (and GL_ARB_texture_compression) says only whole
1195 * compressed texture images can be updated. But, that restriction may be
1196 * relaxed for particular compressed formats. At this time, all the
1197 * compressed formats supported by Mesa allow sub-textures to be updated
1198 * along compressed block boundaries.
1200 _mesa_get_format_block_size_3d(destImage
->TexFormat
, &bw
, &bh
, &bd
);
1202 if (bw
!= 1 || bh
!= 1 || bd
!= 1) {
1203 /* offset must be multiple of block size */
1204 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0) || (zoffset
% bd
!= 0)) {
1205 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1206 "%s(xoffset = %d, yoffset = %d, zoffset = %d)",
1207 func
, xoffset
, yoffset
, zoffset
);
1211 /* The size must be a multiple of bw x bh, or we must be using a
1212 * offset+size that exactly hits the edge of the image. This
1213 * is important for small mipmap levels (1x1, 2x1, etc) and for
1216 if ((subWidth
% bw
!= 0) &&
1217 (xoffset
+ subWidth
!= (GLint
) destImage
->Width
)) {
1218 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1219 "%s(width = %d)", func
, subWidth
);
1223 if ((subHeight
% bh
!= 0) &&
1224 (yoffset
+ subHeight
!= (GLint
) destImage
->Height
)) {
1225 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1226 "%s(height = %d)", func
, subHeight
);
1230 if ((subDepth
% bd
!= 0) &&
1231 (zoffset
+ subDepth
!= (GLint
) destImage
->Depth
)) {
1232 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1233 "%s(depth = %d)", func
, subDepth
);
1245 * This is the fallback for Driver.TestProxyTexImage() for doing device-
1246 * specific texture image size checks.
1248 * A hardware driver might override this function if, for example, the
1249 * max 3D texture size is 512x512x64 (i.e. not a cube).
1251 * Note that width, height, depth == 0 is not an error. However, a
1252 * texture with zero width/height/depth will be considered "incomplete"
1253 * and texturing will effectively be disabled.
1255 * \param target any texture target/type
1256 * \param level as passed to glTexImage
1257 * \param format the MESA_FORMAT_x for the tex image
1258 * \param width as passed to glTexImage
1259 * \param height as passed to glTexImage
1260 * \param depth as passed to glTexImage
1261 * \param border as passed to glTexImage
1262 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1265 _mesa_test_proxy_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1267 GLint width
, GLint height
, GLint depth
, GLint border
)
1269 /* We just check if the image size is less than MaxTextureMbytes.
1270 * Some drivers may do more specific checks.
1272 uint64_t bytes
= _mesa_format_image_size64(format
, width
, height
, depth
);
1273 uint64_t mbytes
= bytes
/ (1024 * 1024); /* convert to MB */
1274 mbytes
*= _mesa_num_tex_faces(target
);
1275 return mbytes
<= (uint64_t) ctx
->Const
.MaxTextureMbytes
;
1280 * Return true if the format is only valid for glCompressedTexImage.
1283 compressedteximage_only_format(const struct gl_context
*ctx
, GLenum format
)
1286 case GL_ETC1_RGB8_OES
:
1287 case GL_PALETTE4_RGB8_OES
:
1288 case GL_PALETTE4_RGBA8_OES
:
1289 case GL_PALETTE4_R5_G6_B5_OES
:
1290 case GL_PALETTE4_RGBA4_OES
:
1291 case GL_PALETTE4_RGB5_A1_OES
:
1292 case GL_PALETTE8_RGB8_OES
:
1293 case GL_PALETTE8_RGBA8_OES
:
1294 case GL_PALETTE8_R5_G6_B5_OES
:
1295 case GL_PALETTE8_RGBA4_OES
:
1296 case GL_PALETTE8_RGB5_A1_OES
:
1304 * Return true if the format doesn't support online compression.
1307 _mesa_format_no_online_compression(const struct gl_context
*ctx
, GLenum format
)
1309 return _mesa_is_astc_format(format
) ||
1310 _mesa_is_etc2_format(format
) ||
1311 compressedteximage_only_format(ctx
, format
);
1314 /* Writes to an GL error pointer if non-null and returns whether or not the
1315 * error is GL_NO_ERROR */
1317 write_error(GLenum
*err_ptr
, GLenum error
)
1322 return error
== GL_NO_ERROR
;
1326 * Helper function to determine whether a target and specific compression
1327 * format are supported. The error parameter returns GL_NO_ERROR if the
1328 * target can be compressed. Otherwise it returns either GL_INVALID_OPERATION
1329 * or GL_INVALID_ENUM, whichever is more appropriate.
1332 _mesa_target_can_be_compressed(const struct gl_context
*ctx
, GLenum target
,
1333 GLenum intFormat
, GLenum
*error
)
1335 GLboolean target_can_be_compresed
= GL_FALSE
;
1336 mesa_format format
= _mesa_glenum_to_compressed_format(intFormat
);
1337 enum mesa_format_layout layout
= _mesa_get_format_layout(format
);
1341 case GL_PROXY_TEXTURE_2D
:
1342 target_can_be_compresed
= GL_TRUE
; /* true for any compressed format so far */
1344 case GL_PROXY_TEXTURE_CUBE_MAP
:
1345 case GL_TEXTURE_CUBE_MAP
:
1346 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1347 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1348 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1349 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1350 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1351 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1352 target_can_be_compresed
= ctx
->Extensions
.ARB_texture_cube_map
;
1354 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1355 case GL_TEXTURE_2D_ARRAY_EXT
:
1356 target_can_be_compresed
= ctx
->Extensions
.EXT_texture_array
;
1358 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1359 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1360 /* From the KHR_texture_compression_astc_hdr spec:
1362 * Add a second new column "3D Tex." which is empty for all non-ASTC
1363 * formats. If only the LDR profile is supported by the
1364 * implementation, this column is also empty for all ASTC formats. If
1365 * both the LDR and HDR profiles are supported only, this column is
1366 * checked for all ASTC formats.
1368 * Add a third new column "Cube Map Array Tex." which is empty for all
1369 * non-ASTC formats, and checked for all ASTC formats.
1373 * 'An INVALID_OPERATION error is generated by CompressedTexImage3D
1374 * if <internalformat> is TEXTURE_CUBE_MAP_ARRAY and the
1375 * "Cube Map Array" column of table 8.19 is *not* checked, or if
1376 * <internalformat> is TEXTURE_3D and the "3D Tex." column of table
1377 * 8.19 is *not* checked'
1379 * The instances of <internalformat> above should say <target>.
1381 * ETC2/EAC formats are the only alternative in GLES and thus such errors
1382 * have already been handled by normal ETC2/EAC behavior.
1385 /* From section 3.8.6, page 146 of OpenGL ES 3.0 spec:
1387 * "The ETC2/EAC texture compression algorithm supports only
1388 * two-dimensional images. If internalformat is an ETC2/EAC format,
1389 * glCompressedTexImage3D will generate an INVALID_OPERATION error if
1390 * target is not TEXTURE_2D_ARRAY."
1392 * This should also be applicable for glTexStorage3D(). Other available
1393 * targets for these functions are: TEXTURE_3D and TEXTURE_CUBE_MAP_ARRAY.
1395 if (layout
== MESA_FORMAT_LAYOUT_ETC2
&& _mesa_is_gles3(ctx
))
1396 return write_error(error
, GL_INVALID_OPERATION
);
1397 target_can_be_compresed
= ctx
->Extensions
.ARB_texture_cube_map_array
;
1401 case MESA_FORMAT_LAYOUT_ETC2
:
1402 /* See ETC2/EAC comment in case GL_TEXTURE_CUBE_MAP_ARRAY. */
1403 if (_mesa_is_gles3(ctx
))
1404 return write_error(error
, GL_INVALID_OPERATION
);
1406 case MESA_FORMAT_LAYOUT_BPTC
:
1407 target_can_be_compresed
= ctx
->Extensions
.ARB_texture_compression_bptc
;
1409 case MESA_FORMAT_LAYOUT_ASTC
:
1410 target_can_be_compresed
=
1411 ctx
->Extensions
.KHR_texture_compression_astc_hdr
;
1413 /* Throw an INVALID_OPERATION error if the target is TEXTURE_3D and
1414 * and the hdr extension is not supported.
1415 * See comment in switch case GL_TEXTURE_CUBE_MAP_ARRAY for more info.
1417 if (!target_can_be_compresed
)
1418 return write_error(error
, GL_INVALID_OPERATION
);
1426 return write_error(error
,
1427 target_can_be_compresed
? GL_NO_ERROR
: GL_INVALID_ENUM
);
1432 * Check if the given texture target value is legal for a
1433 * glTexImage1/2/3D call.
1436 legal_teximage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
1442 case GL_PROXY_TEXTURE_1D
:
1443 return _mesa_is_desktop_gl(ctx
);
1451 case GL_PROXY_TEXTURE_2D
:
1452 return _mesa_is_desktop_gl(ctx
);
1453 case GL_PROXY_TEXTURE_CUBE_MAP
:
1454 return _mesa_is_desktop_gl(ctx
)
1455 && ctx
->Extensions
.ARB_texture_cube_map
;
1456 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1457 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1458 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1459 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1460 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1461 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1462 return ctx
->Extensions
.ARB_texture_cube_map
;
1463 case GL_TEXTURE_RECTANGLE_NV
:
1464 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1465 return _mesa_is_desktop_gl(ctx
)
1466 && ctx
->Extensions
.NV_texture_rectangle
;
1467 case GL_TEXTURE_1D_ARRAY_EXT
:
1468 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1469 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1477 case GL_PROXY_TEXTURE_3D
:
1478 return _mesa_is_desktop_gl(ctx
);
1479 case GL_TEXTURE_2D_ARRAY_EXT
:
1480 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1481 || _mesa_is_gles3(ctx
);
1482 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1483 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1484 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1485 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1486 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1491 _mesa_problem(ctx
, "invalid dims=%u in legal_teximage_target()", dims
);
1498 * Check if the given texture target value is legal for a
1499 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1500 * The difference compared to legal_teximage_target() above is that
1501 * proxy targets are not supported.
1504 legal_texsubimage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
,
1509 return _mesa_is_desktop_gl(ctx
) && target
== GL_TEXTURE_1D
;
1514 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1515 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1516 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1517 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1518 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1519 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1520 return ctx
->Extensions
.ARB_texture_cube_map
;
1521 case GL_TEXTURE_RECTANGLE_NV
:
1522 return _mesa_is_desktop_gl(ctx
)
1523 && ctx
->Extensions
.NV_texture_rectangle
;
1524 case GL_TEXTURE_1D_ARRAY_EXT
:
1525 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1533 case GL_TEXTURE_2D_ARRAY_EXT
:
1534 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1535 || _mesa_is_gles3(ctx
);
1536 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1537 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1538 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1540 /* Table 8.15 of the OpenGL 4.5 core profile spec
1541 * (20141030) says that TEXTURE_CUBE_MAP is valid for TextureSubImage3D
1542 * and CopyTextureSubImage3D.
1544 case GL_TEXTURE_CUBE_MAP
:
1550 _mesa_problem(ctx
, "invalid dims=%u in legal_texsubimage_target()",
1558 * Helper function to determine if a texture object is mutable (in terms
1559 * of GL_ARB_texture_storage).
1562 mutable_tex_object(struct gl_context
*ctx
, GLenum target
)
1564 struct gl_texture_object
*texObj
= _mesa_get_current_tex_object(ctx
, target
);
1568 return !texObj
->Immutable
;
1573 * Return expected size of a compressed texture.
1576 compressed_tex_size(GLsizei width
, GLsizei height
, GLsizei depth
,
1579 mesa_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
1580 return _mesa_format_image_size(mesaFormat
, width
, height
, depth
);
1584 * Verify that a texture format is valid with a particular target
1586 * In particular, textures with base format of \c GL_DEPTH_COMPONENT or
1587 * \c GL_DEPTH_STENCIL are only valid with certain, context dependent texture
1590 * \param ctx GL context
1591 * \param target Texture target
1592 * \param internalFormat Internal format of the texture image
1594 * \returns true if the combination is legal, false otherwise.
1597 _mesa_legal_texture_base_format_for_target(struct gl_context
*ctx
,
1598 GLenum target
, GLenum internalFormat
)
1600 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
1601 || _mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_STENCIL
1602 || _mesa_base_tex_format(ctx
, internalFormat
) == GL_STENCIL_INDEX
) {
1603 /* Section 3.8.3 (Texture Image Specification) of the OpenGL 3.3 Core
1604 * Profile spec says:
1606 * "Textures with a base internal format of DEPTH_COMPONENT or
1607 * DEPTH_STENCIL are supported by texture image specification
1608 * commands only if target is TEXTURE_1D, TEXTURE_2D,
1609 * TEXTURE_1D_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_RECTANGLE,
1610 * TEXTURE_CUBE_MAP, PROXY_TEXTURE_1D, PROXY_TEXTURE_2D,
1611 * PROXY_TEXTURE_1D_ARRAY, PROXY_TEXTURE_2D_ARRAY,
1612 * PROXY_TEXTURE_RECTANGLE, or PROXY_TEXTURE_CUBE_MAP. Using these
1613 * formats in conjunction with any other target will result in an
1614 * INVALID_OPERATION error."
1616 * Cubemaps are only supported with desktop OpenGL version >= 3.0,
1617 * EXT_gpu_shader4, or, on OpenGL ES 2.0+, OES_depth_texture_cube_map.
1619 if (target
!= GL_TEXTURE_1D
&&
1620 target
!= GL_PROXY_TEXTURE_1D
&&
1621 target
!= GL_TEXTURE_2D
&&
1622 target
!= GL_PROXY_TEXTURE_2D
&&
1623 target
!= GL_TEXTURE_1D_ARRAY
&&
1624 target
!= GL_PROXY_TEXTURE_1D_ARRAY
&&
1625 target
!= GL_TEXTURE_2D_ARRAY
&&
1626 target
!= GL_PROXY_TEXTURE_2D_ARRAY
&&
1627 target
!= GL_TEXTURE_RECTANGLE_ARB
&&
1628 target
!= GL_PROXY_TEXTURE_RECTANGLE_ARB
&&
1629 !((_mesa_is_cube_face(target
) ||
1630 target
== GL_TEXTURE_CUBE_MAP
||
1631 target
== GL_PROXY_TEXTURE_CUBE_MAP
) &&
1632 (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_gpu_shader4
1633 || (ctx
->API
== API_OPENGLES2
&& ctx
->Extensions
.OES_depth_texture_cube_map
))) &&
1634 !((target
== GL_TEXTURE_CUBE_MAP_ARRAY
||
1635 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
) &&
1636 ctx
->Extensions
.ARB_texture_cube_map_array
)) {
1645 texture_formats_agree(GLenum internalFormat
,
1648 GLboolean colorFormat
;
1649 GLboolean is_format_depth_or_depthstencil
;
1650 GLboolean is_internalFormat_depth_or_depthstencil
;
1652 /* Even though there are no color-index textures, we still have to support
1653 * uploading color-index data and remapping it to RGB via the
1654 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
1656 const GLboolean indexFormat
= (format
== GL_COLOR_INDEX
);
1658 is_internalFormat_depth_or_depthstencil
=
1659 _mesa_is_depth_format(internalFormat
) ||
1660 _mesa_is_depthstencil_format(internalFormat
);
1662 is_format_depth_or_depthstencil
=
1663 _mesa_is_depth_format(format
) ||
1664 _mesa_is_depthstencil_format(format
);
1666 colorFormat
= _mesa_is_color_format(format
);
1668 if (_mesa_is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
)
1671 if (is_internalFormat_depth_or_depthstencil
!=
1672 is_format_depth_or_depthstencil
)
1675 if (_mesa_is_ycbcr_format(internalFormat
) != _mesa_is_ycbcr_format(format
))
1682 * Test the combination of format, type and internal format arguments of
1683 * different texture operations on GLES.
1685 * \param ctx GL context.
1686 * \param format pixel data format given by the user.
1687 * \param type pixel data type given by the user.
1688 * \param internalFormat internal format given by the user.
1689 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1690 * \param callerName name of the caller function to print in the error message
1692 * \return true if a error is found, false otherwise
1694 * Currently, it is used by texture_error_check() and texsubimage_error_check().
1697 texture_format_error_check_gles(struct gl_context
*ctx
, GLenum format
,
1698 GLenum type
, GLenum internalFormat
,
1699 GLuint dimensions
, const char *callerName
)
1703 if (_mesa_is_gles3(ctx
)) {
1704 err
= _mesa_es3_error_check_format_and_type(ctx
, format
, type
,
1706 if (err
!= GL_NO_ERROR
) {
1707 _mesa_error(ctx
, err
,
1708 "%s(format = %s, type = %s, internalformat = %s)",
1709 callerName
, _mesa_enum_to_string(format
),
1710 _mesa_enum_to_string(type
),
1711 _mesa_enum_to_string(internalFormat
));
1716 err
= _mesa_es_error_check_format_and_type(ctx
, format
, type
, dimensions
);
1717 if (err
!= GL_NO_ERROR
) {
1718 _mesa_error(ctx
, err
, "%s(format = %s, type = %s)",
1719 callerName
, _mesa_enum_to_string(format
),
1720 _mesa_enum_to_string(type
));
1729 * Test the glTexImage[123]D() parameters for errors.
1731 * \param ctx GL context.
1732 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1733 * \param target texture target given by the user (already validated).
1734 * \param level image level given by the user.
1735 * \param internalFormat internal format given by the user.
1736 * \param format pixel data format given by the user.
1737 * \param type pixel data type given by the user.
1738 * \param width image width given by the user.
1739 * \param height image height given by the user.
1740 * \param depth image depth given by the user.
1741 * \param border image border given by the user.
1743 * \return GL_TRUE if a error is found, GL_FALSE otherwise
1745 * Verifies each of the parameters against the constants specified in
1746 * __struct gl_contextRec::Const and the supported extensions, and according
1747 * to the OpenGL specification.
1748 * Note that we don't fully error-check the width, height, depth values
1749 * here. That's done in _mesa_legal_texture_dimensions() which is used
1750 * by several other GL entrypoints. Plus, texture dims have a special
1751 * interaction with proxy textures.
1754 texture_error_check( struct gl_context
*ctx
,
1755 GLuint dimensions
, GLenum target
,
1756 GLint level
, GLint internalFormat
,
1757 GLenum format
, GLenum type
,
1758 GLint width
, GLint height
,
1759 GLint depth
, GLint border
,
1760 const GLvoid
*pixels
)
1764 /* Note: for proxy textures, some error conditions immediately generate
1765 * a GL error in the usual way. But others do not generate a GL error.
1766 * Instead, they cause the width, height, depth, format fields of the
1767 * texture image to be zeroed-out. The GL spec seems to indicate that the
1768 * zero-out behaviour is only used in cases related to memory allocation.
1772 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
1773 _mesa_error(ctx
, GL_INVALID_VALUE
,
1774 "glTexImage%dD(level=%d)", dimensions
, level
);
1779 if (border
< 0 || border
> 1 ||
1780 ((ctx
->API
!= API_OPENGL_COMPAT
||
1781 target
== GL_TEXTURE_RECTANGLE_NV
||
1782 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1783 _mesa_error(ctx
, GL_INVALID_VALUE
,
1784 "glTexImage%dD(border=%d)", dimensions
, border
);
1788 if (width
< 0 || height
< 0 || depth
< 0) {
1789 _mesa_error(ctx
, GL_INVALID_VALUE
,
1790 "glTexImage%dD(width, height or depth < 0)", dimensions
);
1794 /* Check incoming image format and type */
1795 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1796 if (err
!= GL_NO_ERROR
) {
1797 /* Prior to OpenGL-ES 2.0, an INVALID_VALUE is expected instead of
1798 * INVALID_ENUM. From page 73 OpenGL ES 1.1 spec:
1800 * "Specifying a value for internalformat that is not one of the
1801 * above (acceptable) values generates the error INVALID VALUE."
1803 if (err
== GL_INVALID_ENUM
&& _mesa_is_gles(ctx
) && ctx
->Version
< 20)
1804 err
= GL_INVALID_VALUE
;
1806 _mesa_error(ctx
, err
,
1807 "glTexImage%dD(incompatible format = %s, type = %s)",
1808 dimensions
, _mesa_enum_to_string(format
),
1809 _mesa_enum_to_string(type
));
1813 /* Check internalFormat */
1814 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1815 _mesa_error(ctx
, GL_INVALID_VALUE
,
1816 "glTexImage%dD(internalFormat=%s)",
1817 dimensions
, _mesa_enum_to_string(internalFormat
));
1821 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1822 * combinations of format, internalFormat, and type that can be used.
1823 * Formats and types that require additional extensions (e.g., GL_FLOAT
1824 * requires GL_OES_texture_float) are filtered elsewhere.
1826 if (_mesa_is_gles(ctx
) &&
1827 texture_format_error_check_gles(ctx
, format
, type
, internalFormat
,
1828 dimensions
, "glTexImage%dD")) {
1832 /* validate the bound PBO, if any */
1833 if (!_mesa_validate_pbo_source(ctx
, dimensions
, &ctx
->Unpack
,
1834 width
, height
, depth
, format
, type
,
1835 INT_MAX
, pixels
, "glTexImage")) {
1839 /* make sure internal format and format basically agree */
1840 if (!texture_formats_agree(internalFormat
, format
)) {
1841 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1842 "glTexImage%dD(incompatible internalFormat = %s, format = %s)",
1843 dimensions
, _mesa_enum_to_string(internalFormat
),
1844 _mesa_enum_to_string(format
));
1848 /* additional checks for ycbcr textures */
1849 if (internalFormat
== GL_YCBCR_MESA
) {
1850 assert(ctx
->Extensions
.MESA_ycbcr_texture
);
1851 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
1852 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
1854 _mesa_snprintf(message
, sizeof(message
),
1855 "glTexImage%dD(format/type YCBCR mismatch)",
1857 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", message
);
1858 return GL_TRUE
; /* error */
1860 if (target
!= GL_TEXTURE_2D
&&
1861 target
!= GL_PROXY_TEXTURE_2D
&&
1862 target
!= GL_TEXTURE_RECTANGLE_NV
&&
1863 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
1864 _mesa_error(ctx
, GL_INVALID_ENUM
,
1865 "glTexImage%dD(bad target for YCbCr texture)",
1871 _mesa_snprintf(message
, sizeof(message
),
1872 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1873 dimensions
, border
);
1874 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", message
);
1879 /* additional checks for depth textures */
1880 if (!_mesa_legal_texture_base_format_for_target(ctx
, target
, internalFormat
)) {
1881 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1882 "glTexImage%dD(bad target for texture)", dimensions
);
1886 /* additional checks for compressed textures */
1887 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
1889 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
, &err
)) {
1890 _mesa_error(ctx
, err
,
1891 "glTexImage%dD(target can't be compressed)", dimensions
);
1894 if (_mesa_format_no_online_compression(ctx
, internalFormat
)) {
1895 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1896 "glTexImage%dD(no compression for format)", dimensions
);
1900 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1901 "glTexImage%dD(border!=0)", dimensions
);
1906 /* additional checks for integer textures */
1907 if ((ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) &&
1908 (_mesa_is_enum_format_integer(format
) !=
1909 _mesa_is_enum_format_integer(internalFormat
))) {
1910 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1911 "glTexImage%dD(integer/non-integer format mismatch)",
1916 if (!mutable_tex_object(ctx
, target
)) {
1917 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1918 "glTexImage%dD(immutable texture)", dimensions
);
1922 /* if we get here, the parameters are OK */
1928 * Error checking for glCompressedTexImage[123]D().
1929 * Note that the width, height and depth values are not fully error checked
1931 * \return GL_TRUE if a error is found, GL_FALSE otherwise
1934 compressed_texture_error_check(struct gl_context
*ctx
, GLint dimensions
,
1935 GLenum target
, GLint level
,
1936 GLenum internalFormat
, GLsizei width
,
1937 GLsizei height
, GLsizei depth
, GLint border
,
1938 GLsizei imageSize
, const GLvoid
*data
)
1940 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1942 GLenum error
= GL_NO_ERROR
;
1943 char *reason
= ""; /* no error */
1945 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
, &error
)) {
1950 /* This will detect any invalid internalFormat value */
1951 if (!_mesa_is_compressed_format(ctx
, internalFormat
)) {
1952 _mesa_error(ctx
, GL_INVALID_ENUM
,
1953 "glCompressedTexImage%dD(internalFormat=%s)",
1954 dimensions
, _mesa_enum_to_string(internalFormat
));
1958 /* validate the bound PBO, if any */
1959 if (!_mesa_validate_pbo_source_compressed(ctx
, dimensions
, &ctx
->Unpack
,
1961 "glCompressedTexImage")) {
1965 switch (internalFormat
) {
1966 case GL_PALETTE4_RGB8_OES
:
1967 case GL_PALETTE4_RGBA8_OES
:
1968 case GL_PALETTE4_R5_G6_B5_OES
:
1969 case GL_PALETTE4_RGBA4_OES
:
1970 case GL_PALETTE4_RGB5_A1_OES
:
1971 case GL_PALETTE8_RGB8_OES
:
1972 case GL_PALETTE8_RGBA8_OES
:
1973 case GL_PALETTE8_R5_G6_B5_OES
:
1974 case GL_PALETTE8_RGBA4_OES
:
1975 case GL_PALETTE8_RGB5_A1_OES
:
1976 /* check level (note that level should be zero or less!) */
1977 if (level
> 0 || level
< -maxLevels
) {
1979 error
= GL_INVALID_VALUE
;
1983 if (dimensions
!= 2) {
1984 reason
= "compressed paletted textures must be 2D";
1985 error
= GL_INVALID_OPERATION
;
1989 /* Figure out the expected texture size (in bytes). This will be
1990 * checked against the actual size later.
1992 expectedSize
= _mesa_cpal_compressed_size(level
, internalFormat
,
1995 /* This is for the benefit of the TestProxyTexImage below. It expects
1996 * level to be non-negative. OES_compressed_paletted_texture uses a
1997 * weird mechanism where the level specified to glCompressedTexImage2D
1998 * is -(n-1) number of levels in the texture, and the data specifies the
1999 * complete mipmap stack. This is done to ensure the palette is the
2000 * same for all levels.
2007 if (level
< 0 || level
>= maxLevels
) {
2009 error
= GL_INVALID_VALUE
;
2013 /* Figure out the expected texture size (in bytes). This will be
2014 * checked against the actual size later.
2016 expectedSize
= compressed_tex_size(width
, height
, depth
, internalFormat
);
2020 /* This should really never fail */
2021 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
2022 reason
= "internalFormat";
2023 error
= GL_INVALID_ENUM
;
2027 /* No compressed formats support borders at this time */
2029 reason
= "border != 0";
2030 error
= GL_INVALID_VALUE
;
2034 /* Check for invalid pixel storage modes */
2035 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dimensions
,
2037 "glCompressedTexImage")) {
2041 /* check image size in bytes */
2042 if (expectedSize
!= imageSize
) {
2043 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
2044 * if <imageSize> is not consistent with the format, dimensions, and
2045 * contents of the specified image.
2047 reason
= "imageSize inconsistent with width/height/format";
2048 error
= GL_INVALID_VALUE
;
2052 if (!mutable_tex_object(ctx
, target
)) {
2053 reason
= "immutable texture";
2054 error
= GL_INVALID_OPERATION
;
2061 /* Note: not all error paths exit through here. */
2062 _mesa_error(ctx
, error
, "glCompressedTexImage%dD(%s)",
2063 dimensions
, reason
);
2070 * Test glTexSubImage[123]D() parameters for errors.
2072 * \param ctx GL context.
2073 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2074 * \param target texture target given by the user (already validated)
2075 * \param level image level given by the user.
2076 * \param xoffset sub-image x offset given by the user.
2077 * \param yoffset sub-image y offset given by the user.
2078 * \param zoffset sub-image z offset given by the user.
2079 * \param format pixel data format given by the user.
2080 * \param type pixel data type given by the user.
2081 * \param width image width given by the user.
2082 * \param height image height given by the user.
2083 * \param depth image depth given by the user.
2085 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2087 * Verifies each of the parameters against the constants specified in
2088 * __struct gl_contextRec::Const and the supported extensions, and according
2089 * to the OpenGL specification.
2092 texsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2093 struct gl_texture_object
*texObj
,
2094 GLenum target
, GLint level
,
2095 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2096 GLint width
, GLint height
, GLint depth
,
2097 GLenum format
, GLenum type
, const GLvoid
*pixels
,
2098 bool dsa
, const char *callerName
)
2100 struct gl_texture_image
*texImage
;
2104 /* must be out of memory */
2105 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", callerName
);
2110 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2111 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(level=%d)", callerName
, level
);
2115 if (error_check_subtexture_negative_dimensions(ctx
, dimensions
,
2116 width
, height
, depth
,
2121 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
2123 /* non-existant texture level */
2124 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid texture image)",
2129 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
2130 if (err
!= GL_NO_ERROR
) {
2131 _mesa_error(ctx
, err
,
2132 "%s(incompatible format = %s, type = %s)",
2133 callerName
, _mesa_enum_to_string(format
),
2134 _mesa_enum_to_string(type
));
2138 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2139 * combinations of format, internalFormat, and type that can be used.
2140 * Formats and types that require additional extensions (e.g., GL_FLOAT
2141 * requires GL_OES_texture_float) are filtered elsewhere.
2143 if (_mesa_is_gles(ctx
) &&
2144 texture_format_error_check_gles(ctx
, format
, type
,
2145 texImage
->InternalFormat
,
2146 dimensions
, callerName
)) {
2150 /* validate the bound PBO, if any */
2151 if (!_mesa_validate_pbo_source(ctx
, dimensions
, &ctx
->Unpack
,
2152 width
, height
, depth
, format
, type
,
2153 INT_MAX
, pixels
, callerName
)) {
2157 if (error_check_subtexture_dimensions(ctx
, dimensions
,
2158 texImage
, xoffset
, yoffset
, zoffset
,
2159 width
, height
, depth
, callerName
)) {
2163 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2164 if (_mesa_format_no_online_compression(ctx
, texImage
->InternalFormat
)) {
2165 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2166 "%s(no compression for format)", callerName
);
2171 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
2172 /* both source and dest must be integer-valued, or neither */
2173 if (_mesa_is_format_integer_color(texImage
->TexFormat
) !=
2174 _mesa_is_enum_format_integer(format
)) {
2175 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2176 "%s(integer/non-integer format mismatch)", callerName
);
2186 * Test glCopyTexImage[12]D() parameters for errors.
2188 * \param ctx GL context.
2189 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2190 * \param target texture target given by the user.
2191 * \param level image level given by the user.
2192 * \param internalFormat internal format given by the user.
2193 * \param width image width given by the user.
2194 * \param height image height given by the user.
2195 * \param border texture border.
2197 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2199 * Verifies each of the parameters against the constants specified in
2200 * __struct gl_contextRec::Const and the supported extensions, and according
2201 * to the OpenGL specification.
2204 copytexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
2205 GLenum target
, GLint level
, GLint internalFormat
,
2206 GLint width
, GLint height
, GLint border
)
2209 GLint rb_base_format
;
2210 struct gl_renderbuffer
*rb
;
2211 GLenum rb_internal_format
;
2214 if (!legal_texsubimage_target(ctx
, dimensions
, target
, false)) {
2215 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexImage%uD(target=%s)",
2216 dimensions
, _mesa_enum_to_string(target
));
2221 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2222 _mesa_error(ctx
, GL_INVALID_VALUE
,
2223 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
2227 /* Check that the source buffer is complete */
2228 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2229 if (ctx
->ReadBuffer
->_Status
== 0) {
2230 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2232 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2233 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2234 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
2238 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2239 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2240 "glCopyTexImage%dD(multisample FBO)", dimensions
);
2246 if (border
< 0 || border
> 1 ||
2247 ((ctx
->API
!= API_OPENGL_COMPAT
||
2248 target
== GL_TEXTURE_RECTANGLE_NV
||
2249 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
2250 _mesa_error(ctx
, GL_INVALID_VALUE
,
2251 "glCopyTexImage%dD(border=%d)", dimensions
, border
);
2255 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2258 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
2259 switch (internalFormat
) {
2264 case GL_LUMINANCE_ALPHA
:
2267 _mesa_error(ctx
, GL_INVALID_ENUM
,
2268 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2269 _mesa_enum_to_string(internalFormat
));
2274 * Section 8.6 (Alternate Texture Image Specification Commands) of the
2275 * OpenGL 4.5 (Compatibility Profile) spec says:
2277 * "Parameters level, internalformat, and border are specified using
2278 * the same values, with the same meanings, as the corresponding
2279 * arguments of TexImage2D, except that internalformat may not be
2280 * specified as 1, 2, 3, or 4."
2282 if (internalFormat
>= 1 && internalFormat
<= 4) {
2283 _mesa_error(ctx
, GL_INVALID_ENUM
,
2284 "glCopyTexImage%dD(internalFormat=%d)", dimensions
,
2290 baseFormat
= _mesa_base_tex_format(ctx
, internalFormat
);
2291 if (baseFormat
< 0) {
2292 _mesa_error(ctx
, GL_INVALID_ENUM
,
2293 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2294 _mesa_enum_to_string(internalFormat
));
2298 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, internalFormat
);
2300 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2301 "glCopyTexImage%dD(read buffer)", dimensions
);
2305 rb_internal_format
= rb
->InternalFormat
;
2306 rb_base_format
= _mesa_base_tex_format(ctx
, rb
->InternalFormat
);
2307 if (_mesa_is_color_format(internalFormat
)) {
2308 if (rb_base_format
< 0) {
2309 _mesa_error(ctx
, GL_INVALID_VALUE
,
2310 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2311 _mesa_enum_to_string(internalFormat
));
2316 if (_mesa_is_gles(ctx
)) {
2318 if (_mesa_base_format_component_count(baseFormat
) >
2319 _mesa_base_format_component_count(rb_base_format
)) {
2322 if (baseFormat
== GL_DEPTH_COMPONENT
||
2323 baseFormat
== GL_DEPTH_STENCIL
||
2324 baseFormat
== GL_STENCIL_INDEX
||
2325 rb_base_format
== GL_DEPTH_COMPONENT
||
2326 rb_base_format
== GL_DEPTH_STENCIL
||
2327 rb_base_format
== GL_STENCIL_INDEX
||
2328 ((baseFormat
== GL_LUMINANCE_ALPHA
||
2329 baseFormat
== GL_ALPHA
) &&
2330 rb_base_format
!= GL_RGBA
) ||
2331 internalFormat
== GL_RGB9_E5
) {
2334 if (internalFormat
== GL_RGB9_E5
) {
2338 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2339 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2340 _mesa_enum_to_string(internalFormat
));
2345 if (_mesa_is_gles3(ctx
)) {
2346 bool rb_is_srgb
= false;
2347 bool dst_is_srgb
= false;
2349 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&&
2350 _mesa_get_format_color_encoding(rb
->Format
) == GL_SRGB
) {
2354 if (_mesa_get_linear_internalformat(internalFormat
) != internalFormat
) {
2358 if (rb_is_srgb
!= dst_is_srgb
) {
2359 /* Page 137 (page 149 of the PDF) in section 3.8.5 of the
2360 * OpenGLES 3.0.0 spec says:
2362 * "The error INVALID_OPERATION is also generated if the
2363 * value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the
2364 * framebuffer attachment corresponding to the read buffer
2365 * is LINEAR (see section 6.1.13) and internalformat is
2366 * one of the sRGB formats described in section 3.8.16, or
2367 * if the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING is
2368 * SRGB and internalformat is not one of the sRGB formats."
2370 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2371 "glCopyTexImage%dD(srgb usage mismatch)", dimensions
);
2375 /* Page 139, Table 3.15 of OpenGL ES 3.0 spec does not define ReadPixels
2376 * types for SNORM formats. Also, conversion to SNORM formats is not
2377 * allowed by Table 3.2 on Page 110.
2379 if (_mesa_is_enum_format_snorm(internalFormat
)) {
2380 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2381 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2382 _mesa_enum_to_string(internalFormat
));
2387 if (!_mesa_source_buffer_exists(ctx
, baseFormat
)) {
2388 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2389 "glCopyTexImage%dD(missing readbuffer)", dimensions
);
2393 /* From the EXT_texture_integer spec:
2395 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2396 * if the texture internalformat is an integer format and the read color
2397 * buffer is not an integer format, or if the internalformat is not an
2398 * integer format and the read color buffer is an integer format."
2400 if (_mesa_is_color_format(internalFormat
)) {
2401 bool is_int
= _mesa_is_enum_format_integer(internalFormat
);
2402 bool is_rbint
= _mesa_is_enum_format_integer(rb_internal_format
);
2403 bool is_unorm
= _mesa_is_enum_format_unorm(internalFormat
);
2404 bool is_rbunorm
= _mesa_is_enum_format_unorm(rb_internal_format
);
2405 if (is_int
|| is_rbint
) {
2406 if (is_int
!= is_rbint
) {
2407 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2408 "glCopyTexImage%dD(integer vs non-integer)", dimensions
);
2410 } else if (_mesa_is_gles(ctx
) &&
2411 _mesa_is_enum_format_unsigned_int(internalFormat
) !=
2412 _mesa_is_enum_format_unsigned_int(rb_internal_format
)) {
2413 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2414 "glCopyTexImage%dD(signed vs unsigned integer)", dimensions
);
2419 /* From page 138 of OpenGL ES 3.0 spec:
2420 * "The error INVALID_OPERATION is generated if floating-point RGBA
2421 * data is required; if signed integer RGBA data is required and the
2422 * format of the current color buffer is not signed integer; if
2423 * unsigned integer RGBA data is required and the format of the
2424 * current color buffer is not unsigned integer; or if fixed-point
2425 * RGBA data is required and the format of the current color buffer
2426 * is not fixed-point.
2428 if (_mesa_is_gles(ctx
) && is_unorm
!= is_rbunorm
)
2429 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2430 "glCopyTexImage%dD(unorm vs non-unorm)", dimensions
);
2433 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
2435 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
, &err
)) {
2436 _mesa_error(ctx
, err
,
2437 "glCopyTexImage%dD(target can't be compressed)", dimensions
);
2440 if (_mesa_format_no_online_compression(ctx
, internalFormat
)) {
2441 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2442 "glCopyTexImage%dD(no compression for format)", dimensions
);
2446 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2447 "glCopyTexImage%dD(border!=0)", dimensions
);
2452 if (!mutable_tex_object(ctx
, target
)) {
2453 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2454 "glCopyTexImage%dD(immutable texture)", dimensions
);
2458 /* if we get here, the parameters are OK */
2464 * Test glCopyTexSubImage[12]D() parameters for errors.
2465 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2468 copytexsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2469 const struct gl_texture_object
*texObj
,
2470 GLenum target
, GLint level
,
2471 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2472 GLint width
, GLint height
, const char *caller
)
2474 struct gl_texture_image
*texImage
;
2476 /* Check that the source buffer is complete */
2477 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2478 if (ctx
->ReadBuffer
->_Status
== 0) {
2479 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2481 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2482 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2483 "%s(invalid readbuffer)", caller
);
2487 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2488 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2489 "%s(multisample FBO)", caller
);
2495 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2496 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(level=%d)", caller
, level
);
2500 /* Get dest image pointers */
2502 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", caller
);
2506 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
2508 /* destination image does not exist */
2509 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2510 "%s(invalid texture image)", caller
);
2514 if (error_check_subtexture_negative_dimensions(ctx
, dimensions
,
2515 width
, height
, 1, caller
)) {
2519 if (error_check_subtexture_dimensions(ctx
, dimensions
, texImage
,
2520 xoffset
, yoffset
, zoffset
,
2521 width
, height
, 1, caller
)) {
2525 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2526 if (_mesa_format_no_online_compression(ctx
, texImage
->InternalFormat
)) {
2527 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2528 "%s(no compression for format)", caller
);
2533 if (texImage
->InternalFormat
== GL_YCBCR_MESA
) {
2534 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s()", caller
);
2538 if (!_mesa_source_buffer_exists(ctx
, texImage
->_BaseFormat
)) {
2539 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2540 "%s(missing readbuffer, format=%s)", caller
,
2541 _mesa_enum_to_string(texImage
->_BaseFormat
));
2545 /* From the EXT_texture_integer spec:
2547 * "INVALID_OPERATION is generated by CopyTexImage* and
2548 * CopyTexSubImage* if the texture internalformat is an integer format
2549 * and the read color buffer is not an integer format, or if the
2550 * internalformat is not an integer format and the read color buffer
2551 * is an integer format."
2553 if (_mesa_is_color_format(texImage
->InternalFormat
)) {
2554 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
2556 if (_mesa_is_format_integer_color(rb
->Format
) !=
2557 _mesa_is_format_integer_color(texImage
->TexFormat
)) {
2558 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2559 "%s(integer vs non-integer)", caller
);
2564 /* In the ES 3.2 specification's Table 8.13 (Valid CopyTexImage source
2565 * framebuffer/destination texture base internal format combinations),
2566 * all the entries for stencil are left blank (unsupported).
2568 if (_mesa_is_gles(ctx
) && _mesa_is_stencil_format(texImage
->_BaseFormat
)) {
2569 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(stencil disallowed)", caller
);
2573 /* if we get here, the parameters are OK */
2578 /** Callback info for walking over FBO hash table */
2581 struct gl_context
*ctx
;
2582 struct gl_texture_object
*texObj
;
2588 * Check render to texture callback. Called from _mesa_HashWalk().
2591 check_rtt_cb(GLuint key
, void *data
, void *userData
)
2593 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
2594 const struct cb_info
*info
= (struct cb_info
*) userData
;
2595 struct gl_context
*ctx
= info
->ctx
;
2596 const struct gl_texture_object
*texObj
= info
->texObj
;
2597 const GLuint level
= info
->level
, face
= info
->face
;
2599 /* If this is a user-created FBO */
2600 if (_mesa_is_user_fbo(fb
)) {
2602 /* check if any of the FBO's attachments point to 'texObj' */
2603 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2604 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2605 if (att
->Type
== GL_TEXTURE
&&
2606 att
->Texture
== texObj
&&
2607 att
->TextureLevel
== level
&&
2608 att
->CubeMapFace
== face
) {
2609 _mesa_update_texture_renderbuffer(ctx
, ctx
->DrawBuffer
, att
);
2610 assert(att
->Renderbuffer
->TexImage
);
2611 /* Mark fb status as indeterminate to force re-validation */
2620 * When a texture image is specified we have to check if it's bound to
2621 * any framebuffer objects (render to texture) in order to detect changes
2622 * in size or format since that effects FBO completeness.
2623 * Any FBOs rendering into the texture must be re-validated.
2626 _mesa_update_fbo_texture(struct gl_context
*ctx
,
2627 struct gl_texture_object
*texObj
,
2628 GLuint face
, GLuint level
)
2630 /* Only check this texture if it's been marked as RenderToTexture */
2631 if (texObj
->_RenderToTexture
) {
2632 struct cb_info info
;
2634 info
.texObj
= texObj
;
2637 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, check_rtt_cb
, &info
);
2643 * If the texture object's GenerateMipmap flag is set and we've
2644 * changed the texture base level image, regenerate the rest of the
2645 * mipmap levels now.
2648 check_gen_mipmap(struct gl_context
*ctx
, GLenum target
,
2649 struct gl_texture_object
*texObj
, GLint level
)
2651 if (texObj
->GenerateMipmap
&&
2652 level
== texObj
->BaseLevel
&&
2653 level
< texObj
->MaxLevel
) {
2654 assert(ctx
->Driver
.GenerateMipmap
);
2655 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2660 /** Debug helper: override the user-requested internal format */
2662 override_internal_format(GLenum internalFormat
, GLint width
, GLint height
)
2665 if (internalFormat
== GL_RGBA16F_ARB
||
2666 internalFormat
== GL_RGBA32F_ARB
) {
2667 printf("Convert rgba float tex to int %d x %d\n", width
, height
);
2670 else if (internalFormat
== GL_RGB16F_ARB
||
2671 internalFormat
== GL_RGB32F_ARB
) {
2672 printf("Convert rgb float tex to int %d x %d\n", width
, height
);
2675 else if (internalFormat
== GL_LUMINANCE_ALPHA16F_ARB
||
2676 internalFormat
== GL_LUMINANCE_ALPHA32F_ARB
) {
2677 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2678 return GL_LUMINANCE_ALPHA
;
2680 else if (internalFormat
== GL_LUMINANCE16F_ARB
||
2681 internalFormat
== GL_LUMINANCE32F_ARB
) {
2682 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2683 return GL_LUMINANCE
;
2685 else if (internalFormat
== GL_ALPHA16F_ARB
||
2686 internalFormat
== GL_ALPHA32F_ARB
) {
2687 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2691 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2692 internalFormat = GL_RGBA;
2696 return internalFormat
;
2699 return internalFormat
;
2705 * Choose the actual hardware format for a texture image.
2706 * Try to use the same format as the previous image level when possible.
2707 * Otherwise, ask the driver for the best format.
2708 * It's important to try to choose a consistant format for all levels
2709 * for efficient texture memory layout/allocation. In particular, this
2710 * comes up during automatic mipmap generation.
2713 _mesa_choose_texture_format(struct gl_context
*ctx
,
2714 struct gl_texture_object
*texObj
,
2715 GLenum target
, GLint level
,
2716 GLenum internalFormat
, GLenum format
, GLenum type
)
2720 /* see if we've already chosen a format for the previous level */
2722 struct gl_texture_image
*prevImage
=
2723 _mesa_select_tex_image(texObj
, target
, level
- 1);
2724 /* See if the prev level is defined and has an internal format which
2725 * matches the new internal format.
2728 prevImage
->Width
> 0 &&
2729 prevImage
->InternalFormat
== internalFormat
) {
2730 /* use the same format */
2731 assert(prevImage
->TexFormat
!= MESA_FORMAT_NONE
);
2732 return prevImage
->TexFormat
;
2736 /* If the application requested compression to an S3TC format but we don't
2737 * have the DXTn library, force a generic compressed format instead.
2739 if (internalFormat
!= format
&& format
!= GL_NONE
) {
2740 const GLenum before
= internalFormat
;
2742 switch (internalFormat
) {
2743 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
2744 if (!ctx
->Mesa_DXTn
)
2745 internalFormat
= GL_COMPRESSED_RGB
;
2747 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
2748 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
2749 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
2750 if (!ctx
->Mesa_DXTn
)
2751 internalFormat
= GL_COMPRESSED_RGBA
;
2757 if (before
!= internalFormat
) {
2759 "DXT compression requested (%s), "
2760 "but libtxc_dxtn library not installed. Using %s "
2762 _mesa_enum_to_string(before
),
2763 _mesa_enum_to_string(internalFormat
));
2767 /* choose format from scratch */
2768 f
= ctx
->Driver
.ChooseTextureFormat(ctx
, target
, internalFormat
,
2770 assert(f
!= MESA_FORMAT_NONE
);
2776 * Adjust pixel unpack params and image dimensions to strip off the
2777 * one-pixel texture border.
2779 * Gallium and intel don't support texture borders. They've seldem been used
2780 * and seldom been implemented correctly anyway.
2782 * \param unpackNew returns the new pixel unpack parameters
2785 strip_texture_border(GLenum target
,
2786 GLint
*width
, GLint
*height
, GLint
*depth
,
2787 const struct gl_pixelstore_attrib
*unpack
,
2788 struct gl_pixelstore_attrib
*unpackNew
)
2794 *unpackNew
= *unpack
;
2796 if (unpackNew
->RowLength
== 0)
2797 unpackNew
->RowLength
= *width
;
2799 if (unpackNew
->ImageHeight
== 0)
2800 unpackNew
->ImageHeight
= *height
;
2802 assert(*width
>= 3);
2803 unpackNew
->SkipPixels
++; /* skip the border */
2804 *width
= *width
- 2; /* reduce the width by two border pixels */
2806 /* The min height of a texture with a border is 3 */
2807 if (*height
>= 3 && target
!= GL_TEXTURE_1D_ARRAY
) {
2808 unpackNew
->SkipRows
++; /* skip the border */
2809 *height
= *height
- 2; /* reduce the height by two border pixels */
2813 target
!= GL_TEXTURE_2D_ARRAY
&&
2814 target
!= GL_TEXTURE_CUBE_MAP_ARRAY
) {
2815 unpackNew
->SkipImages
++; /* skip the border */
2816 *depth
= *depth
- 2; /* reduce the depth by two border pixels */
2822 * Common code to implement all the glTexImage1D/2D/3D functions
2823 * as well as glCompressedTexImage1D/2D/3D.
2824 * \param compressed only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
2825 * \param format the user's image format (only used if !compressed)
2826 * \param type the user's image type (only used if !compressed)
2827 * \param imageSize only used for glCompressedTexImage1D/2D/3D calls.
2830 teximage(struct gl_context
*ctx
, GLboolean compressed
, GLuint dims
,
2831 GLenum target
, GLint level
, GLint internalFormat
,
2832 GLsizei width
, GLsizei height
, GLsizei depth
,
2833 GLint border
, GLenum format
, GLenum type
,
2834 GLsizei imageSize
, const GLvoid
*pixels
)
2836 const char *func
= compressed
? "glCompressedTexImage" : "glTexImage";
2837 struct gl_pixelstore_attrib unpack_no_border
;
2838 const struct gl_pixelstore_attrib
*unpack
= &ctx
->Unpack
;
2839 struct gl_texture_object
*texObj
;
2840 mesa_format texFormat
;
2841 GLboolean dimensionsOK
, sizeOK
;
2843 FLUSH_VERTICES(ctx
, 0);
2845 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
)) {
2848 "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
2850 _mesa_enum_to_string(target
), level
,
2851 _mesa_enum_to_string(internalFormat
),
2852 width
, height
, depth
, border
, pixels
);
2855 "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2857 _mesa_enum_to_string(target
), level
,
2858 _mesa_enum_to_string(internalFormat
),
2859 width
, height
, depth
, border
,
2860 _mesa_enum_to_string(format
),
2861 _mesa_enum_to_string(type
), pixels
);
2864 internalFormat
= override_internal_format(internalFormat
, width
, height
);
2866 /* target error checking */
2867 if (!legal_teximage_target(ctx
, dims
, target
)) {
2868 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s%uD(target=%s)",
2869 func
, dims
, _mesa_enum_to_string(target
));
2873 /* general error checking */
2875 if (compressed_texture_error_check(ctx
, dims
, target
, level
,
2877 width
, height
, depth
,
2878 border
, imageSize
, pixels
))
2882 if (texture_error_check(ctx
, dims
, target
, level
, internalFormat
,
2883 format
, type
, width
, height
, depth
, border
,
2888 /* Here we convert a cpal compressed image into a regular glTexImage2D
2889 * call by decompressing the texture. If we really want to support cpal
2890 * textures in any driver this would have to be changed.
2892 if (ctx
->API
== API_OPENGLES
&& compressed
&& dims
== 2) {
2893 switch (internalFormat
) {
2894 case GL_PALETTE4_RGB8_OES
:
2895 case GL_PALETTE4_RGBA8_OES
:
2896 case GL_PALETTE4_R5_G6_B5_OES
:
2897 case GL_PALETTE4_RGBA4_OES
:
2898 case GL_PALETTE4_RGB5_A1_OES
:
2899 case GL_PALETTE8_RGB8_OES
:
2900 case GL_PALETTE8_RGBA8_OES
:
2901 case GL_PALETTE8_R5_G6_B5_OES
:
2902 case GL_PALETTE8_RGBA4_OES
:
2903 case GL_PALETTE8_RGB5_A1_OES
:
2904 _mesa_cpal_compressed_teximage2d(target
, level
, internalFormat
,
2905 width
, height
, imageSize
, pixels
);
2910 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2914 /* For glCompressedTexImage() the driver has no choice about the
2915 * texture format since we'll never transcode the user's compressed
2916 * image data. The internalFormat was error checked earlier.
2918 texFormat
= _mesa_glenum_to_compressed_format(internalFormat
);
2921 /* In case of HALF_FLOAT_OES or FLOAT_OES, find corresponding sized
2922 * internal floating point format for the given base format.
2924 if (_mesa_is_gles(ctx
) && format
== internalFormat
) {
2925 if (type
== GL_FLOAT
) {
2926 texObj
->_IsFloat
= GL_TRUE
;
2927 } else if (type
== GL_HALF_FLOAT_OES
|| type
== GL_HALF_FLOAT
) {
2928 texObj
->_IsHalfFloat
= GL_TRUE
;
2931 internalFormat
= adjust_for_oes_float_texture(ctx
, format
, type
);
2934 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
2935 internalFormat
, format
, type
);
2938 assert(texFormat
!= MESA_FORMAT_NONE
);
2940 /* check that width, height, depth are legal for the mipmap level */
2941 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, level
, width
,
2942 height
, depth
, border
);
2944 /* check that the texture won't take too much memory, etc */
2945 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target(target
),
2947 width
, height
, depth
, border
);
2949 if (_mesa_is_proxy_texture(target
)) {
2950 /* Proxy texture: just clear or set state depending on error checking */
2951 struct gl_texture_image
*texImage
=
2952 get_proxy_tex_image(ctx
, target
, level
);
2955 return; /* GL_OUT_OF_MEMORY already recorded */
2957 if (dimensionsOK
&& sizeOK
) {
2958 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, depth
,
2959 border
, internalFormat
, texFormat
);
2962 clear_teximage_fields(texImage
);
2966 /* non-proxy target */
2967 const GLuint face
= _mesa_tex_target_to_face(target
);
2968 struct gl_texture_image
*texImage
;
2970 if (!dimensionsOK
) {
2971 _mesa_error(ctx
, GL_INVALID_VALUE
,
2972 "%s%uD(invalid width or height or depth)",
2978 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
2979 "%s%uD(image too large: %d x %d x %d, %s format)",
2980 func
, dims
, width
, height
, depth
,
2981 _mesa_enum_to_string(internalFormat
));
2985 /* Allow a hardware driver to just strip out the border, to provide
2986 * reliable but slightly incorrect hardware rendering instead of
2987 * rarely-tested software fallback rendering.
2989 if (border
&& ctx
->Const
.StripTextureBorder
) {
2990 strip_texture_border(target
, &width
, &height
, &depth
, unpack
,
2993 unpack
= &unpack_no_border
;
2996 if (ctx
->NewState
& _NEW_PIXEL
)
2997 _mesa_update_state(ctx
);
2999 _mesa_lock_texture(ctx
, texObj
);
3001 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3004 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s%uD", func
, dims
);
3007 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3009 _mesa_init_teximage_fields(ctx
, texImage
,
3010 width
, height
, depth
,
3011 border
, internalFormat
, texFormat
);
3013 /* Give the texture to the driver. <pixels> may be null. */
3014 if (width
> 0 && height
> 0 && depth
> 0) {
3016 ctx
->Driver
.CompressedTexImage(ctx
, dims
, texImage
,
3020 ctx
->Driver
.TexImage(ctx
, dims
, texImage
, format
,
3021 type
, pixels
, unpack
);
3025 check_gen_mipmap(ctx
, target
, texObj
, level
);
3027 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3029 _mesa_dirty_texobj(ctx
, texObj
);
3032 _mesa_unlock_texture(ctx
, texObj
);
3039 * Called from the API. Note that width includes the border.
3042 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
3043 GLsizei width
, GLint border
, GLenum format
,
3044 GLenum type
, const GLvoid
*pixels
)
3046 GET_CURRENT_CONTEXT(ctx
);
3047 teximage(ctx
, GL_FALSE
, 1, target
, level
, internalFormat
, width
, 1, 1,
3048 border
, format
, type
, 0, pixels
);
3053 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
3054 GLsizei width
, GLsizei height
, GLint border
,
3055 GLenum format
, GLenum type
,
3056 const GLvoid
*pixels
)
3058 GET_CURRENT_CONTEXT(ctx
);
3059 teximage(ctx
, GL_FALSE
, 2, target
, level
, internalFormat
, width
, height
, 1,
3060 border
, format
, type
, 0, pixels
);
3065 * Called by the API or display list executor.
3066 * Note that width and height include the border.
3069 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
3070 GLsizei width
, GLsizei height
, GLsizei depth
,
3071 GLint border
, GLenum format
, GLenum type
,
3072 const GLvoid
*pixels
)
3074 GET_CURRENT_CONTEXT(ctx
);
3075 teximage(ctx
, GL_FALSE
, 3, target
, level
, internalFormat
,
3076 width
, height
, depth
,
3077 border
, format
, type
, 0, pixels
);
3082 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
3083 GLsizei width
, GLsizei height
, GLsizei depth
,
3084 GLint border
, GLenum format
, GLenum type
,
3085 const GLvoid
*pixels
)
3087 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
3088 depth
, border
, format
, type
, pixels
);
3093 _mesa_EGLImageTargetTexture2DOES (GLenum target
, GLeglImageOES image
)
3095 struct gl_texture_object
*texObj
;
3096 struct gl_texture_image
*texImage
;
3098 GET_CURRENT_CONTEXT(ctx
);
3099 FLUSH_VERTICES(ctx
, 0);
3103 valid_target
= ctx
->Extensions
.OES_EGL_image
;
3105 case GL_TEXTURE_EXTERNAL_OES
:
3107 _mesa_is_gles(ctx
) ? ctx
->Extensions
.OES_EGL_image_external
: false;
3110 valid_target
= false;
3114 if (!valid_target
) {
3115 _mesa_error(ctx
, GL_INVALID_ENUM
,
3116 "glEGLImageTargetTexture2D(target=%d)", target
);
3121 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3122 "glEGLImageTargetTexture2D(image=%p)", image
);
3126 if (ctx
->NewState
& _NEW_PIXEL
)
3127 _mesa_update_state(ctx
);
3129 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3133 _mesa_lock_texture(ctx
, texObj
);
3135 if (texObj
->Immutable
) {
3136 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3137 "glEGLImageTargetTexture2D(texture is immutable)");
3138 _mesa_unlock_texture(ctx
, texObj
);
3142 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
3144 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glEGLImageTargetTexture2D");
3146 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3148 ctx
->Driver
.EGLImageTargetTexture2D(ctx
, target
,
3149 texObj
, texImage
, image
);
3151 _mesa_dirty_texobj(ctx
, texObj
);
3153 _mesa_unlock_texture(ctx
, texObj
);
3158 * Helper that implements the glTexSubImage1/2/3D()
3159 * and glTextureSubImage1/2/3D() functions.
3162 _mesa_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
3163 struct gl_texture_object
*texObj
,
3164 struct gl_texture_image
*texImage
,
3165 GLenum target
, GLint level
,
3166 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3167 GLsizei width
, GLsizei height
, GLsizei depth
,
3168 GLenum format
, GLenum type
, const GLvoid
*pixels
,
3171 FLUSH_VERTICES(ctx
, 0);
3173 if (ctx
->NewState
& _NEW_PIXEL
)
3174 _mesa_update_state(ctx
);
3176 _mesa_lock_texture(ctx
, texObj
);
3178 if (width
> 0 && height
> 0 && depth
> 0) {
3179 /* If we have a border, offset=-1 is legal. Bias by border width. */
3182 if (target
!= GL_TEXTURE_2D_ARRAY
)
3183 zoffset
+= texImage
->Border
;
3186 if (target
!= GL_TEXTURE_1D_ARRAY
)
3187 yoffset
+= texImage
->Border
;
3190 xoffset
+= texImage
->Border
;
3193 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3194 xoffset
, yoffset
, zoffset
,
3195 width
, height
, depth
,
3196 format
, type
, pixels
, &ctx
->Unpack
);
3198 check_gen_mipmap(ctx
, target
, texObj
, level
);
3200 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3201 * the texel data, not the texture format, size, etc.
3205 _mesa_unlock_texture(ctx
, texObj
);
3209 * Implement all the glTexSubImage1/2/3D() functions.
3210 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3213 texsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
3214 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3215 GLsizei width
, GLsizei height
, GLsizei depth
,
3216 GLenum format
, GLenum type
, const GLvoid
*pixels
,
3217 const char *callerName
)
3219 struct gl_texture_object
*texObj
;
3220 struct gl_texture_image
*texImage
;
3222 /* check target (proxies not allowed) */
3223 if (!legal_texsubimage_target(ctx
, dims
, target
, false)) {
3224 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage%uD(target=%s)",
3225 dims
, _mesa_enum_to_string(target
));
3229 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3233 if (texsubimage_error_check(ctx
, dims
, texObj
, target
, level
,
3234 xoffset
, yoffset
, zoffset
,
3235 width
, height
, depth
, format
, type
,
3236 pixels
, false, callerName
)) {
3237 return; /* error was detected */
3240 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
3241 /* texsubimage_error_check ensures that texImage is not NULL */
3243 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3244 _mesa_debug(ctx
, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3246 _mesa_enum_to_string(target
), level
,
3247 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3248 _mesa_enum_to_string(format
),
3249 _mesa_enum_to_string(type
), pixels
);
3251 _mesa_texture_sub_image(ctx
, dims
, texObj
, texImage
, target
, level
,
3252 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3253 format
, type
, pixels
, false);
3258 * Implement all the glTextureSubImage1/2/3D() functions.
3259 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3262 texturesubimage(struct gl_context
*ctx
, GLuint dims
,
3263 GLuint texture
, GLint level
,
3264 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3265 GLsizei width
, GLsizei height
, GLsizei depth
,
3266 GLenum format
, GLenum type
, const GLvoid
*pixels
,
3267 const char *callerName
)
3269 struct gl_texture_object
*texObj
;
3270 struct gl_texture_image
*texImage
;
3273 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3275 "glTextureSubImage%uD %d %d %d %d %d %d %d %d %s %s %p\n",
3276 dims
, texture
, level
,
3277 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3278 _mesa_enum_to_string(format
),
3279 _mesa_enum_to_string(type
), pixels
);
3281 /* Get the texture object by Name. */
3282 texObj
= _mesa_lookup_texture(ctx
, texture
);
3284 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTextureSubImage%uD(texture)",
3289 /* check target (proxies not allowed) */
3290 if (!legal_texsubimage_target(ctx
, dims
, texObj
->Target
, true)) {
3291 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target=%s)",
3292 callerName
, _mesa_enum_to_string(texObj
->Target
));
3296 if (texsubimage_error_check(ctx
, dims
, texObj
, texObj
->Target
, level
,
3297 xoffset
, yoffset
, zoffset
,
3298 width
, height
, depth
, format
, type
,
3299 pixels
, true, callerName
)) {
3300 return; /* error was detected */
3304 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
3305 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
3309 * What do we do if the user created a texture with the following code
3310 * and then called this function with its handle?
3313 * glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &tex);
3314 * glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
3315 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, ...);
3316 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, ...);
3317 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, ...);
3318 * // Note: GL_TEXTURE_CUBE_MAP_NEGATIVE_Y not set, or given the
3319 * // wrong format, or given the wrong size, etc.
3320 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, ...);
3321 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, ...);
3323 * A bug has been filed against the spec for this case. In the
3324 * meantime, we will check for cube completeness.
3326 * According to Section 8.17 Texture Completeness in the OpenGL 4.5
3327 * Core Profile spec (30.10.2014):
3328 * "[A] cube map texture is cube complete if the
3329 * following conditions all hold true: The [base level] texture
3330 * images of each of the six cube map faces have identical, positive,
3331 * and square dimensions. The [base level] images were each specified
3332 * with the same internal format."
3334 * It seems reasonable to check for cube completeness of an arbitrary
3335 * level here so that the image data has a consistent format and size.
3337 if (!_mesa_cube_level_complete(texObj
, level
)) {
3338 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3339 "glTextureSubImage%uD(cube map incomplete)",
3344 imageStride
= _mesa_image_image_stride(&ctx
->Unpack
, width
, height
,
3346 /* Copy in each face. */
3347 for (i
= zoffset
; i
< zoffset
+ depth
; ++i
) {
3348 texImage
= texObj
->Image
[i
][level
];
3351 _mesa_texture_sub_image(ctx
, 3, texObj
, texImage
, texObj
->Target
,
3352 level
, xoffset
, yoffset
, 0,
3353 width
, height
, 1, format
,
3354 type
, pixels
, true);
3355 pixels
= (GLubyte
*) pixels
+ imageStride
;
3359 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
3362 _mesa_texture_sub_image(ctx
, dims
, texObj
, texImage
, texObj
->Target
,
3363 level
, xoffset
, yoffset
, zoffset
,
3364 width
, height
, depth
, format
,
3365 type
, pixels
, true);
3371 _mesa_TexSubImage1D( GLenum target
, GLint level
,
3372 GLint xoffset
, GLsizei width
,
3373 GLenum format
, GLenum type
,
3374 const GLvoid
*pixels
)
3376 GET_CURRENT_CONTEXT(ctx
);
3377 texsubimage(ctx
, 1, target
, level
,
3380 format
, type
, pixels
, "glTexSubImage1D");
3385 _mesa_TexSubImage2D( GLenum target
, GLint level
,
3386 GLint xoffset
, GLint yoffset
,
3387 GLsizei width
, GLsizei height
,
3388 GLenum format
, GLenum type
,
3389 const GLvoid
*pixels
)
3391 GET_CURRENT_CONTEXT(ctx
);
3392 texsubimage(ctx
, 2, target
, level
,
3393 xoffset
, yoffset
, 0,
3395 format
, type
, pixels
, "glTexSubImage2D");
3401 _mesa_TexSubImage3D( GLenum target
, GLint level
,
3402 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3403 GLsizei width
, GLsizei height
, GLsizei depth
,
3404 GLenum format
, GLenum type
,
3405 const GLvoid
*pixels
)
3407 GET_CURRENT_CONTEXT(ctx
);
3408 texsubimage(ctx
, 3, target
, level
,
3409 xoffset
, yoffset
, zoffset
,
3410 width
, height
, depth
,
3411 format
, type
, pixels
, "glTexSubImage3D");
3415 _mesa_TextureSubImage1D(GLuint texture
, GLint level
,
3416 GLint xoffset
, GLsizei width
,
3417 GLenum format
, GLenum type
,
3418 const GLvoid
*pixels
)
3420 GET_CURRENT_CONTEXT(ctx
);
3421 texturesubimage(ctx
, 1, texture
, level
,
3424 format
, type
, pixels
, "glTextureSubImage1D");
3429 _mesa_TextureSubImage2D(GLuint texture
, GLint level
,
3430 GLint xoffset
, GLint yoffset
,
3431 GLsizei width
, GLsizei height
,
3432 GLenum format
, GLenum type
,
3433 const GLvoid
*pixels
)
3435 GET_CURRENT_CONTEXT(ctx
);
3436 texturesubimage(ctx
, 2, texture
, level
,
3437 xoffset
, yoffset
, 0,
3439 format
, type
, pixels
, "glTextureSubImage2D");
3444 _mesa_TextureSubImage3D(GLuint texture
, GLint level
,
3445 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3446 GLsizei width
, GLsizei height
, GLsizei depth
,
3447 GLenum format
, GLenum type
,
3448 const GLvoid
*pixels
)
3450 GET_CURRENT_CONTEXT(ctx
);
3451 texturesubimage(ctx
, 3, texture
, level
,
3452 xoffset
, yoffset
, zoffset
,
3453 width
, height
, depth
,
3454 format
, type
, pixels
, "glTextureSubImage3D");
3459 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3460 * from. This depends on whether the texture contains color or depth values.
3462 static struct gl_renderbuffer
*
3463 get_copy_tex_image_source(struct gl_context
*ctx
, mesa_format texFormat
)
3465 if (_mesa_get_format_bits(texFormat
, GL_DEPTH_BITS
) > 0) {
3466 /* reading from depth/stencil buffer */
3467 return ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
3468 } else if (_mesa_get_format_bits(texFormat
, GL_STENCIL_BITS
) > 0) {
3469 return ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
3471 /* copying from color buffer */
3472 return ctx
->ReadBuffer
->_ColorReadBuffer
;
3477 copytexsubimage_by_slice(struct gl_context
*ctx
,
3478 struct gl_texture_image
*texImage
,
3480 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3481 struct gl_renderbuffer
*rb
,
3483 GLsizei width
, GLsizei height
)
3485 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
3488 /* For 1D arrays, we copy each scanline of the source rectangle into the
3491 assert(zoffset
== 0);
3493 for (slice
= 0; slice
< height
; slice
++) {
3494 assert(yoffset
+ slice
< texImage
->Height
);
3495 ctx
->Driver
.CopyTexSubImage(ctx
, 2, texImage
,
3496 xoffset
, 0, yoffset
+ slice
,
3497 rb
, x
, y
+ slice
, width
, 1);
3500 ctx
->Driver
.CopyTexSubImage(ctx
, dims
, texImage
,
3501 xoffset
, yoffset
, zoffset
,
3502 rb
, x
, y
, width
, height
);
3507 formats_differ_in_component_sizes(mesa_format f1
, mesa_format f2
)
3509 GLint f1_r_bits
= _mesa_get_format_bits(f1
, GL_RED_BITS
);
3510 GLint f1_g_bits
= _mesa_get_format_bits(f1
, GL_GREEN_BITS
);
3511 GLint f1_b_bits
= _mesa_get_format_bits(f1
, GL_BLUE_BITS
);
3512 GLint f1_a_bits
= _mesa_get_format_bits(f1
, GL_ALPHA_BITS
);
3514 GLint f2_r_bits
= _mesa_get_format_bits(f2
, GL_RED_BITS
);
3515 GLint f2_g_bits
= _mesa_get_format_bits(f2
, GL_GREEN_BITS
);
3516 GLint f2_b_bits
= _mesa_get_format_bits(f2
, GL_BLUE_BITS
);
3517 GLint f2_a_bits
= _mesa_get_format_bits(f2
, GL_ALPHA_BITS
);
3519 if ((f1_r_bits
&& f2_r_bits
&& f1_r_bits
!= f2_r_bits
)
3520 || (f1_g_bits
&& f2_g_bits
&& f1_g_bits
!= f2_g_bits
)
3521 || (f1_b_bits
&& f2_b_bits
&& f1_b_bits
!= f2_b_bits
)
3522 || (f1_a_bits
&& f2_a_bits
&& f1_a_bits
!= f2_a_bits
))
3529 can_avoid_reallocation(struct gl_texture_image
*texImage
, GLenum internalFormat
,
3530 mesa_format texFormat
, GLint x
, GLint y
, GLsizei width
,
3531 GLsizei height
, GLint border
)
3533 if (texImage
->InternalFormat
!= internalFormat
)
3535 if (texImage
->TexFormat
!= texFormat
)
3537 if (texImage
->Border
!= border
)
3539 if (texImage
->Width2
!= width
)
3541 if (texImage
->Height2
!= height
)
3547 * Implement the glCopyTexImage1/2D() functions.
3550 copyteximage(struct gl_context
*ctx
, GLuint dims
,
3551 GLenum target
, GLint level
, GLenum internalFormat
,
3552 GLint x
, GLint y
, GLsizei width
, GLsizei height
, GLint border
)
3554 struct gl_texture_object
*texObj
;
3555 struct gl_texture_image
*texImage
;
3556 const GLuint face
= _mesa_tex_target_to_face(target
);
3557 mesa_format texFormat
;
3558 struct gl_renderbuffer
*rb
;
3560 FLUSH_VERTICES(ctx
, 0);
3562 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3563 _mesa_debug(ctx
, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3565 _mesa_enum_to_string(target
), level
,
3566 _mesa_enum_to_string(internalFormat
),
3567 x
, y
, width
, height
, border
);
3569 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3570 _mesa_update_state(ctx
);
3572 if (copytexture_error_check(ctx
, dims
, target
, level
, internalFormat
,
3573 width
, height
, border
))
3576 if (!_mesa_legal_texture_dimensions(ctx
, target
, level
, width
, height
,
3578 _mesa_error(ctx
, GL_INVALID_VALUE
,
3579 "glCopyTexImage%uD(invalid width or height)", dims
);
3583 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3586 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3587 internalFormat
, GL_NONE
, GL_NONE
);
3589 /* First check if reallocating the texture buffer can be avoided.
3590 * Without the realloc the copy can be 20x faster.
3592 _mesa_lock_texture(ctx
, texObj
);
3594 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
3595 if (texImage
&& can_avoid_reallocation(texImage
, internalFormat
, texFormat
,
3596 x
, y
, width
, height
, border
)) {
3597 _mesa_unlock_texture(ctx
, texObj
);
3598 return _mesa_copy_texture_sub_image(ctx
, dims
, texObj
, target
, level
,
3599 0, 0, 0, x
, y
, width
, height
,
3603 _mesa_unlock_texture(ctx
, texObj
);
3604 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_LOW
, "glCopyTexImage "
3605 "can't avoid reallocating texture storage\n");
3607 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, internalFormat
);
3609 if (_mesa_is_gles3(ctx
)) {
3610 if (_mesa_is_enum_format_unsized(internalFormat
)) {
3611 /* Conversion from GL_RGB10_A2 source buffer format is not allowed in
3612 * OpenGL ES 3.0. Khronos bug# 9807.
3614 if (rb
->InternalFormat
== GL_RGB10_A2
) {
3615 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3616 "glCopyTexImage%uD(Reading from GL_RGB10_A2 buffer"
3617 " and writing to unsized internal format)", dims
);
3621 /* From Page 139 of OpenGL ES 3.0 spec:
3622 * "If internalformat is sized, the internal format of the new texel
3623 * array is internalformat, and this is also the new texel array’s
3624 * effective internal format. If the component sizes of internalformat
3625 * do not exactly match the corresponding component sizes of the source
3626 * buffer’s effective internal format, described below, an
3627 * INVALID_OPERATION error is generated. If internalformat is unsized,
3628 * the internal format of the new texel array is the effective internal
3629 * format of the source buffer, and this is also the new texel array’s
3630 * effective internal format.
3632 else if (formats_differ_in_component_sizes (texFormat
, rb
->Format
)) {
3633 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3634 "glCopyTexImage%uD(componenet size changed in"
3635 " internal format)", dims
);
3640 assert(texFormat
!= MESA_FORMAT_NONE
);
3642 if (!ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target(target
),
3644 width
, height
, 1, border
)) {
3645 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3646 "glCopyTexImage%uD(image too large)", dims
);
3650 if (border
&& ctx
->Const
.StripTextureBorder
) {
3652 width
-= border
* 2;
3655 height
-= border
* 2;
3660 _mesa_lock_texture(ctx
, texObj
);
3662 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3665 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
3668 GLint srcX
= x
, srcY
= y
, dstX
= 0, dstY
= 0, dstZ
= 0;
3670 /* Free old texture image */
3671 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3673 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, 1,
3674 border
, internalFormat
, texFormat
);
3676 if (width
&& height
) {
3677 /* Allocate texture memory (no pixel data yet) */
3678 ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
);
3680 if (_mesa_clip_copytexsubimage(ctx
, &dstX
, &dstY
, &srcX
, &srcY
,
3682 struct gl_renderbuffer
*srcRb
=
3683 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3685 copytexsubimage_by_slice(ctx
, texImage
, dims
,
3687 srcRb
, srcX
, srcY
, width
, height
);
3690 check_gen_mipmap(ctx
, target
, texObj
, level
);
3693 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3695 _mesa_dirty_texobj(ctx
, texObj
);
3698 _mesa_unlock_texture(ctx
, texObj
);
3704 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
3705 GLenum internalFormat
,
3707 GLsizei width
, GLint border
)
3709 GET_CURRENT_CONTEXT(ctx
);
3710 copyteximage(ctx
, 1, target
, level
, internalFormat
, x
, y
, width
, 1, border
);
3716 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
3717 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
3720 GET_CURRENT_CONTEXT(ctx
);
3721 copyteximage(ctx
, 2, target
, level
, internalFormat
,
3722 x
, y
, width
, height
, border
);
3726 * Implementation for glCopyTex(ture)SubImage1/2/3D() functions.
3729 _mesa_copy_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
3730 struct gl_texture_object
*texObj
,
3731 GLenum target
, GLint level
,
3732 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3734 GLsizei width
, GLsizei height
,
3737 struct gl_texture_image
*texImage
;
3739 FLUSH_VERTICES(ctx
, 0);
3741 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3742 _mesa_debug(ctx
, "%s %s %d %d %d %d %d %d %d %d\n", caller
,
3743 _mesa_enum_to_string(target
),
3744 level
, xoffset
, yoffset
, zoffset
, x
, y
, width
, height
);
3746 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3747 _mesa_update_state(ctx
);
3749 if (copytexsubimage_error_check(ctx
, dims
, texObj
, target
, level
,
3750 xoffset
, yoffset
, zoffset
,
3751 width
, height
, caller
)) {
3755 _mesa_lock_texture(ctx
, texObj
);
3757 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
3759 /* If we have a border, offset=-1 is legal. Bias by border width. */
3762 if (target
!= GL_TEXTURE_2D_ARRAY
)
3763 zoffset
+= texImage
->Border
;
3766 if (target
!= GL_TEXTURE_1D_ARRAY
)
3767 yoffset
+= texImage
->Border
;
3770 xoffset
+= texImage
->Border
;
3773 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3775 struct gl_renderbuffer
*srcRb
=
3776 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3778 copytexsubimage_by_slice(ctx
, texImage
, dims
,
3779 xoffset
, yoffset
, zoffset
,
3780 srcRb
, x
, y
, width
, height
);
3782 check_gen_mipmap(ctx
, target
, texObj
, level
);
3784 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3785 * the texel data, not the texture format, size, etc.
3789 _mesa_unlock_texture(ctx
, texObj
);
3793 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
3794 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
3796 struct gl_texture_object
* texObj
;
3797 const char *self
= "glCopyTexSubImage1D";
3798 GET_CURRENT_CONTEXT(ctx
);
3800 /* Check target (proxies not allowed). Target must be checked prior to
3801 * calling _mesa_get_current_tex_object.
3803 if (!legal_texsubimage_target(ctx
, 1, target
, false)) {
3804 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3805 _mesa_enum_to_string(target
));
3809 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3813 _mesa_copy_texture_sub_image(ctx
, 1, texObj
, target
, level
, xoffset
, 0, 0,
3814 x
, y
, width
, 1, self
);
3820 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
3821 GLint xoffset
, GLint yoffset
,
3822 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3824 struct gl_texture_object
* texObj
;
3825 const char *self
= "glCopyTexSubImage2D";
3826 GET_CURRENT_CONTEXT(ctx
);
3828 /* Check target (proxies not allowed). Target must be checked prior to
3829 * calling _mesa_get_current_tex_object.
3831 if (!legal_texsubimage_target(ctx
, 2, target
, false)) {
3832 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3833 _mesa_enum_to_string(target
));
3837 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3841 _mesa_copy_texture_sub_image(ctx
, 2, texObj
, target
, level
,
3842 xoffset
, yoffset
, 0,
3843 x
, y
, width
, height
, self
);
3849 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
3850 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3851 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3853 struct gl_texture_object
* texObj
;
3854 const char *self
= "glCopyTexSubImage3D";
3855 GET_CURRENT_CONTEXT(ctx
);
3857 /* Check target (proxies not allowed). Target must be checked prior to
3858 * calling _mesa_get_current_tex_object.
3860 if (!legal_texsubimage_target(ctx
, 3, target
, false)) {
3861 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3862 _mesa_enum_to_string(target
));
3866 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3870 _mesa_copy_texture_sub_image(ctx
, 3, texObj
, target
, level
,
3871 xoffset
, yoffset
, zoffset
,
3872 x
, y
, width
, height
, self
);
3876 _mesa_CopyTextureSubImage1D(GLuint texture
, GLint level
,
3877 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
3879 struct gl_texture_object
* texObj
;
3880 const char *self
= "glCopyTextureSubImage1D";
3881 GET_CURRENT_CONTEXT(ctx
);
3883 texObj
= _mesa_lookup_texture_err(ctx
, texture
, self
);
3887 /* Check target (proxies not allowed). */
3888 if (!legal_texsubimage_target(ctx
, 1, texObj
->Target
, true)) {
3889 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3890 _mesa_enum_to_string(texObj
->Target
));
3894 _mesa_copy_texture_sub_image(ctx
, 1, texObj
, texObj
->Target
, level
,
3895 xoffset
, 0, 0, x
, y
, width
, 1, self
);
3899 _mesa_CopyTextureSubImage2D(GLuint texture
, GLint level
,
3900 GLint xoffset
, GLint yoffset
,
3901 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3903 struct gl_texture_object
* texObj
;
3904 const char *self
= "glCopyTextureSubImage2D";
3905 GET_CURRENT_CONTEXT(ctx
);
3907 texObj
= _mesa_lookup_texture_err(ctx
, texture
, self
);
3911 /* Check target (proxies not allowed). */
3912 if (!legal_texsubimage_target(ctx
, 2, texObj
->Target
, true)) {
3913 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3914 _mesa_enum_to_string(texObj
->Target
));
3918 _mesa_copy_texture_sub_image(ctx
, 2, texObj
, texObj
->Target
, level
,
3919 xoffset
, yoffset
, 0,
3920 x
, y
, width
, height
, self
);
3926 _mesa_CopyTextureSubImage3D(GLuint texture
, GLint level
,
3927 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3928 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3930 struct gl_texture_object
* texObj
;
3931 const char *self
= "glCopyTextureSubImage3D";
3932 GET_CURRENT_CONTEXT(ctx
);
3934 texObj
= _mesa_lookup_texture_err(ctx
, texture
, self
);
3938 /* Check target (proxies not allowed). */
3939 if (!legal_texsubimage_target(ctx
, 3, texObj
->Target
, true)) {
3940 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3941 _mesa_enum_to_string(texObj
->Target
));
3945 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
3946 /* Act like CopyTexSubImage2D */
3947 _mesa_copy_texture_sub_image(ctx
, 2, texObj
,
3948 GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ zoffset
,
3949 level
, xoffset
, yoffset
, 0,
3950 x
, y
, width
, height
, self
);
3953 _mesa_copy_texture_sub_image(ctx
, 3, texObj
, texObj
->Target
, level
,
3954 xoffset
, yoffset
, zoffset
,
3955 x
, y
, width
, height
, self
);
3959 check_clear_tex_image(struct gl_context
*ctx
,
3960 const char *function
,
3961 struct gl_texture_image
*texImage
,
3962 GLenum format
, GLenum type
,
3964 GLubyte
*clearValue
)
3966 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3967 static const GLubyte zeroData
[MAX_PIXEL_BYTES
];
3968 GLenum internalFormat
= texImage
->InternalFormat
;
3971 if (texObj
->Target
== GL_TEXTURE_BUFFER
) {
3972 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3973 "%s(buffer texture)", function
);
3977 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
3978 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3979 "%s(compressed texture)", function
);
3983 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
3984 if (err
!= GL_NO_ERROR
) {
3985 _mesa_error(ctx
, err
,
3986 "%s(incompatible format = %s, type = %s)",
3988 _mesa_enum_to_string(format
),
3989 _mesa_enum_to_string(type
));
3993 /* make sure internal format and format basically agree */
3994 if (!texture_formats_agree(internalFormat
, format
)) {
3995 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3996 "%s(incompatible internalFormat = %s, format = %s)",
3998 _mesa_enum_to_string(internalFormat
),
3999 _mesa_enum_to_string(format
));
4003 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
4004 /* both source and dest must be integer-valued, or neither */
4005 if (_mesa_is_format_integer_color(texImage
->TexFormat
) !=
4006 _mesa_is_enum_format_integer(format
)) {
4007 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4008 "%s(integer/non-integer format mismatch)",
4014 if (!_mesa_texstore(ctx
,
4016 texImage
->_BaseFormat
,
4017 texImage
->TexFormat
,
4018 0, /* dstRowStride */
4020 1, 1, 1, /* srcWidth/Height/Depth */
4022 data
? data
: zeroData
,
4023 &ctx
->DefaultPacking
)) {
4024 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid format)", function
);
4031 static struct gl_texture_object
*
4032 get_tex_obj_for_clear(struct gl_context
*ctx
,
4033 const char *function
,
4036 struct gl_texture_object
*texObj
;
4039 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(zero texture)", function
);
4043 texObj
= _mesa_lookup_texture(ctx
, texture
);
4045 if (texObj
== NULL
) {
4046 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", function
);
4050 if (texObj
->Target
== 0) {
4051 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unbound tex)", function
);
4059 get_tex_images_for_clear(struct gl_context
*ctx
,
4060 const char *function
,
4061 struct gl_texture_object
*texObj
,
4063 struct gl_texture_image
**texImages
)
4068 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
4069 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid level)", function
);
4073 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
4074 for (i
= 0; i
< MAX_FACES
; i
++) {
4075 target
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ i
;
4077 texImages
[i
] = _mesa_select_tex_image(texObj
, target
, level
);
4078 if (texImages
[i
] == NULL
) {
4079 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4080 "%s(invalid level)", function
);
4088 texImages
[0] = _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4090 if (texImages
[0] == NULL
) {
4091 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid level)", function
);
4099 _mesa_ClearTexSubImage( GLuint texture
, GLint level
,
4100 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4101 GLsizei width
, GLsizei height
, GLsizei depth
,
4102 GLenum format
, GLenum type
, const void *data
)
4104 GET_CURRENT_CONTEXT(ctx
);
4105 struct gl_texture_object
*texObj
;
4106 struct gl_texture_image
*texImages
[MAX_FACES
];
4107 GLubyte clearValue
[MAX_FACES
][MAX_PIXEL_BYTES
];
4109 int minDepth
, maxDepth
;
4111 texObj
= get_tex_obj_for_clear(ctx
, "glClearTexSubImage", texture
);
4116 _mesa_lock_texture(ctx
, texObj
);
4118 numImages
= get_tex_images_for_clear(ctx
, "glClearTexSubImage",
4119 texObj
, level
, texImages
);
4123 if (numImages
== 1) {
4124 minDepth
= -(int) texImages
[0]->Border
;
4125 maxDepth
= texImages
[0]->Depth
;
4128 maxDepth
= numImages
;
4131 if (xoffset
< -(GLint
) texImages
[0]->Border
||
4132 yoffset
< -(GLint
) texImages
[0]->Border
||
4133 zoffset
< minDepth
||
4137 xoffset
+ width
> texImages
[0]->Width
||
4138 yoffset
+ height
> texImages
[0]->Height
||
4139 zoffset
+ depth
> maxDepth
) {
4140 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4141 "glClearSubTexImage(invalid dimensions)");
4145 if (numImages
== 1) {
4146 if (check_clear_tex_image(ctx
, "glClearTexSubImage",
4148 format
, type
, data
, clearValue
[0])) {
4149 ctx
->Driver
.ClearTexSubImage(ctx
,
4151 xoffset
, yoffset
, zoffset
,
4152 width
, height
, depth
,
4153 data
? clearValue
[0] : NULL
);
4156 for (i
= zoffset
; i
< zoffset
+ depth
; i
++) {
4157 if (!check_clear_tex_image(ctx
, "glClearTexSubImage",
4159 format
, type
, data
, clearValue
[i
]))
4162 for (i
= zoffset
; i
< zoffset
+ depth
; i
++) {
4163 ctx
->Driver
.ClearTexSubImage(ctx
,
4165 xoffset
, yoffset
, 0,
4167 data
? clearValue
[i
] : NULL
);
4172 _mesa_unlock_texture(ctx
, texObj
);
4176 _mesa_ClearTexImage( GLuint texture
, GLint level
,
4177 GLenum format
, GLenum type
, const void *data
)
4179 GET_CURRENT_CONTEXT(ctx
);
4180 struct gl_texture_object
*texObj
;
4181 struct gl_texture_image
*texImages
[MAX_FACES
];
4182 GLubyte clearValue
[MAX_FACES
][MAX_PIXEL_BYTES
];
4185 texObj
= get_tex_obj_for_clear(ctx
, "glClearTexImage", texture
);
4190 _mesa_lock_texture(ctx
, texObj
);
4192 numImages
= get_tex_images_for_clear(ctx
, "glClearTexImage",
4193 texObj
, level
, texImages
);
4195 for (i
= 0; i
< numImages
; i
++) {
4196 if (!check_clear_tex_image(ctx
, "glClearTexImage",
4203 for (i
= 0; i
< numImages
; i
++) {
4204 ctx
->Driver
.ClearTexSubImage(ctx
, texImages
[i
],
4205 -(GLint
) texImages
[i
]->Border
, /* xoffset */
4206 -(GLint
) texImages
[i
]->Border
, /* yoffset */
4207 -(GLint
) texImages
[i
]->Border
, /* zoffset */
4208 texImages
[i
]->Width
,
4209 texImages
[i
]->Height
,
4210 texImages
[i
]->Depth
,
4211 data
? clearValue
[i
] : NULL
);
4215 _mesa_unlock_texture(ctx
, texObj
);
4221 /**********************************************************************/
4222 /****** Compressed Textures ******/
4223 /**********************************************************************/
4227 * Target checking for glCompressedTexSubImage[123]D().
4228 * \return GL_TRUE if error, GL_FALSE if no error
4229 * Must come before other error checking so that the texture object can
4230 * be correctly retrieved using _mesa_get_current_tex_object.
4233 compressed_subtexture_target_check(struct gl_context
*ctx
, GLenum target
,
4234 GLint dims
, GLenum format
, bool dsa
,
4239 if (dsa
&& target
== GL_TEXTURE_RECTANGLE
) {
4240 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid target %s)", caller
,
4241 _mesa_enum_to_string(target
));
4251 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
4252 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
4253 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
4254 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
4255 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
4256 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
4257 targetOK
= ctx
->Extensions
.ARB_texture_cube_map
;
4260 targetOK
= GL_FALSE
;
4266 case GL_TEXTURE_CUBE_MAP
:
4267 targetOK
= dsa
&& ctx
->Extensions
.ARB_texture_cube_map
;
4269 case GL_TEXTURE_2D_ARRAY
:
4270 targetOK
= _mesa_is_gles3(ctx
) ||
4271 (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
);
4273 case GL_TEXTURE_CUBE_MAP_ARRAY
:
4274 targetOK
= ctx
->Extensions
.ARB_texture_cube_map_array
;
4279 * OpenGL 4.5 spec (30.10.2014) says in Section 8.7 Compressed Texture
4281 * "An INVALID_OPERATION error is generated by
4282 * CompressedTex*SubImage3D if the internal format of the texture
4283 * is one of the EAC, ETC2, or RGTC formats and either border is
4284 * non-zero, or the effective target for the texture is not
4285 * TEXTURE_2D_ARRAY."
4287 * NOTE: that's probably a spec error. It should probably say
4288 * "... or the effective target for the texture is not
4289 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP, nor
4290 * GL_TEXTURE_CUBE_MAP_ARRAY."
4291 * since those targets are 2D images and they support all compression
4294 * Instead of listing all these, just list those which are allowed,
4295 * which is (at this time) only bptc. Otherwise we'd say s3tc (and
4296 * more) are valid here, which they are not, but of course not
4297 * mentioned by core spec.
4300 /* These are the only 3D compression formats supported at this time */
4301 case GL_COMPRESSED_RGBA_BPTC_UNORM
:
4302 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM
:
4303 case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT
:
4304 case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT
:
4308 /* invalid format */
4309 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4310 "%s(invalid target %s for format %s)", caller
,
4311 _mesa_enum_to_string(target
),
4312 _mesa_enum_to_string(format
));
4317 targetOK
= GL_FALSE
;
4323 /* no 1D compressed textures at this time */
4324 targetOK
= GL_FALSE
;
4329 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", caller
,
4330 _mesa_enum_to_string(target
));
4338 * Error checking for glCompressedTexSubImage[123]D().
4339 * \return GL_TRUE if error, GL_FALSE if no error
4342 compressed_subtexture_error_check(struct gl_context
*ctx
, GLint dims
,
4343 const struct gl_texture_object
*texObj
,
4344 GLenum target
, GLint level
,
4345 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4346 GLsizei width
, GLsizei height
, GLsizei depth
,
4347 GLenum format
, GLsizei imageSize
,
4348 const GLvoid
*data
, const char *callerName
)
4350 struct gl_texture_image
*texImage
;
4353 /* this will catch any invalid compressed format token */
4354 if (!_mesa_is_compressed_format(ctx
, format
)) {
4355 _mesa_error(ctx
, GL_INVALID_ENUM
,
4356 "%s(format)", callerName
);
4360 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
4361 _mesa_error(ctx
, GL_INVALID_VALUE
,
4367 /* validate the bound PBO, if any */
4368 if (!_mesa_validate_pbo_source_compressed(ctx
, dims
, &ctx
->Unpack
,
4369 imageSize
, data
, callerName
)) {
4373 /* Check for invalid pixel storage modes */
4374 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dims
,
4375 &ctx
->Unpack
, callerName
)) {
4379 expectedSize
= compressed_tex_size(width
, height
, depth
, format
);
4380 if (expectedSize
!= imageSize
) {
4381 _mesa_error(ctx
, GL_INVALID_VALUE
,
4383 callerName
, imageSize
);
4387 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4389 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4390 "%s(invalid texture image)",
4395 if ((GLint
) format
!= texImage
->InternalFormat
) {
4396 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4398 callerName
, _mesa_enum_to_string(format
));
4402 if (compressedteximage_only_format(ctx
, format
)) {
4403 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4404 "%s(format=%s cannot be updated)",
4405 callerName
, _mesa_enum_to_string(format
));
4409 if (error_check_subtexture_negative_dimensions(ctx
, dims
,
4410 width
, height
, depth
,
4415 if (error_check_subtexture_dimensions(ctx
, dims
,
4416 texImage
, xoffset
, yoffset
, zoffset
,
4417 width
, height
, depth
,
4427 _mesa_CompressedTexImage1D(GLenum target
, GLint level
,
4428 GLenum internalFormat
, GLsizei width
,
4429 GLint border
, GLsizei imageSize
,
4432 GET_CURRENT_CONTEXT(ctx
);
4433 teximage(ctx
, GL_TRUE
, 1, target
, level
, internalFormat
,
4434 width
, 1, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4439 _mesa_CompressedTexImage2D(GLenum target
, GLint level
,
4440 GLenum internalFormat
, GLsizei width
,
4441 GLsizei height
, GLint border
, GLsizei imageSize
,
4444 GET_CURRENT_CONTEXT(ctx
);
4445 teximage(ctx
, GL_TRUE
, 2, target
, level
, internalFormat
,
4446 width
, height
, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4451 _mesa_CompressedTexImage3D(GLenum target
, GLint level
,
4452 GLenum internalFormat
, GLsizei width
,
4453 GLsizei height
, GLsizei depth
, GLint border
,
4454 GLsizei imageSize
, const GLvoid
*data
)
4456 GET_CURRENT_CONTEXT(ctx
);
4457 teximage(ctx
, GL_TRUE
, 3, target
, level
, internalFormat
,
4458 width
, height
, depth
, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4463 * Common helper for glCompressedTexSubImage1/2/3D() and
4464 * glCompressedTextureSubImage1/2/3D().
4467 _mesa_compressed_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
4468 struct gl_texture_object
*texObj
,
4469 struct gl_texture_image
*texImage
,
4470 GLenum target
, GLint level
,
4471 GLint xoffset
, GLint yoffset
,
4473 GLsizei width
, GLsizei height
,
4475 GLenum format
, GLsizei imageSize
,
4478 FLUSH_VERTICES(ctx
, 0);
4480 _mesa_lock_texture(ctx
, texObj
);
4482 if (width
> 0 && height
> 0 && depth
> 0) {
4483 ctx
->Driver
.CompressedTexSubImage(ctx
, dims
, texImage
,
4484 xoffset
, yoffset
, zoffset
,
4485 width
, height
, depth
,
4486 format
, imageSize
, data
);
4488 check_gen_mipmap(ctx
, target
, texObj
, level
);
4490 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
4491 * the texel data, not the texture format, size, etc.
4495 _mesa_unlock_texture(ctx
, texObj
);
4500 _mesa_CompressedTexSubImage1D(GLenum target
, GLint level
, GLint xoffset
,
4501 GLsizei width
, GLenum format
,
4502 GLsizei imageSize
, const GLvoid
*data
)
4504 struct gl_texture_object
*texObj
;
4505 struct gl_texture_image
*texImage
;
4507 GET_CURRENT_CONTEXT(ctx
);
4509 if (compressed_subtexture_target_check(ctx
, target
, 1, format
, false,
4510 "glCompressedTexSubImage1D")) {
4514 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4518 if (compressed_subtexture_error_check(ctx
, 1, texObj
, target
,
4519 level
, xoffset
, 0, 0,
4521 format
, imageSize
, data
,
4522 "glCompressedTexSubImage1D")) {
4526 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4529 _mesa_compressed_texture_sub_image(ctx
, 1, texObj
, texImage
, target
, level
,
4530 xoffset
, 0, 0, width
, 1, 1,
4531 format
, imageSize
, data
);
4535 _mesa_CompressedTextureSubImage1D(GLuint texture
, GLint level
, GLint xoffset
,
4536 GLsizei width
, GLenum format
,
4537 GLsizei imageSize
, const GLvoid
*data
)
4539 struct gl_texture_object
*texObj
;
4540 struct gl_texture_image
*texImage
;
4542 GET_CURRENT_CONTEXT(ctx
);
4544 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4545 "glCompressedTextureSubImage1D");
4549 if (compressed_subtexture_target_check(ctx
, texObj
->Target
, 1, format
, true,
4550 "glCompressedTextureSubImage1D")) {
4554 if (compressed_subtexture_error_check(ctx
, 1, texObj
, texObj
->Target
,
4555 level
, xoffset
, 0, 0,
4557 format
, imageSize
, data
,
4558 "glCompressedTextureSubImage1D")) {
4562 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4565 _mesa_compressed_texture_sub_image(ctx
, 1, texObj
, texImage
,
4566 texObj
->Target
, level
,
4567 xoffset
, 0, 0, width
, 1, 1,
4568 format
, imageSize
, data
);
4573 _mesa_CompressedTexSubImage2D(GLenum target
, GLint level
, GLint xoffset
,
4574 GLint yoffset
, GLsizei width
, GLsizei height
,
4575 GLenum format
, GLsizei imageSize
,
4578 struct gl_texture_object
*texObj
;
4579 struct gl_texture_image
*texImage
;
4581 GET_CURRENT_CONTEXT(ctx
);
4583 if (compressed_subtexture_target_check(ctx
, target
, 2, format
, false,
4584 "glCompressedTexSubImage2D")) {
4588 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4592 if (compressed_subtexture_error_check(ctx
, 2, texObj
, target
,
4593 level
, xoffset
, yoffset
, 0,
4595 format
, imageSize
, data
,
4596 "glCompressedTexSubImage2D")) {
4601 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4604 _mesa_compressed_texture_sub_image(ctx
, 2, texObj
, texImage
, target
, level
,
4605 xoffset
, yoffset
, 0, width
, height
, 1,
4606 format
, imageSize
, data
);
4610 _mesa_CompressedTextureSubImage2D(GLuint texture
, GLint level
, GLint xoffset
,
4612 GLsizei width
, GLsizei height
,
4613 GLenum format
, GLsizei imageSize
,
4616 struct gl_texture_object
*texObj
;
4617 struct gl_texture_image
*texImage
;
4619 GET_CURRENT_CONTEXT(ctx
);
4621 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4622 "glCompressedTextureSubImage2D");
4626 if (compressed_subtexture_target_check(ctx
, texObj
->Target
, 2, format
, true,
4627 "glCompressedTextureSubImage2D")) {
4631 if (compressed_subtexture_error_check(ctx
, 2, texObj
, texObj
->Target
,
4632 level
, xoffset
, yoffset
, 0,
4634 format
, imageSize
, data
,
4635 "glCompressedTextureSubImage2D")) {
4639 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4642 _mesa_compressed_texture_sub_image(ctx
, 2, texObj
, texImage
,
4643 texObj
->Target
, level
,
4644 xoffset
, yoffset
, 0, width
, height
, 1,
4645 format
, imageSize
, data
);
4649 _mesa_CompressedTexSubImage3D(GLenum target
, GLint level
, GLint xoffset
,
4650 GLint yoffset
, GLint zoffset
, GLsizei width
,
4651 GLsizei height
, GLsizei depth
, GLenum format
,
4652 GLsizei imageSize
, const GLvoid
*data
)
4654 struct gl_texture_object
*texObj
;
4655 struct gl_texture_image
*texImage
;
4657 GET_CURRENT_CONTEXT(ctx
);
4659 if (compressed_subtexture_target_check(ctx
, target
, 3, format
, false,
4660 "glCompressedTexSubImage3D")) {
4664 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4668 if (compressed_subtexture_error_check(ctx
, 3, texObj
, target
,
4669 level
, xoffset
, yoffset
, zoffset
,
4670 width
, height
, depth
,
4671 format
, imageSize
, data
,
4672 "glCompressedTexSubImage3D")) {
4677 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4680 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, texImage
, target
, level
,
4681 xoffset
, yoffset
, zoffset
,
4682 width
, height
, depth
,
4683 format
, imageSize
, data
);
4687 _mesa_CompressedTextureSubImage3D(GLuint texture
, GLint level
, GLint xoffset
,
4688 GLint yoffset
, GLint zoffset
, GLsizei width
,
4689 GLsizei height
, GLsizei depth
,
4690 GLenum format
, GLsizei imageSize
,
4693 struct gl_texture_object
*texObj
;
4694 struct gl_texture_image
*texImage
;
4696 GET_CURRENT_CONTEXT(ctx
);
4698 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4699 "glCompressedTextureSubImage3D");
4703 if (compressed_subtexture_target_check(ctx
, texObj
->Target
, 3, format
, true,
4704 "glCompressedTextureSubImage3D")) {
4708 if (compressed_subtexture_error_check(ctx
, 3, texObj
, texObj
->Target
,
4709 level
, xoffset
, yoffset
, zoffset
,
4710 width
, height
, depth
,
4711 format
, imageSize
, data
,
4712 "glCompressedTextureSubImage3D")) {
4716 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
4717 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
4718 const char *pixels
= data
;
4722 /* Make sure the texture object is a proper cube.
4723 * (See texturesubimage in teximage.c for details on why this check is
4726 if (!_mesa_cube_level_complete(texObj
, level
)) {
4727 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4728 "glCompressedTextureSubImage3D(cube map incomplete)");
4732 /* Copy in each face. */
4733 for (i
= 0; i
< 6; ++i
) {
4734 texImage
= texObj
->Image
[i
][level
];
4737 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, texImage
,
4738 texObj
->Target
, level
,
4739 xoffset
, yoffset
, zoffset
,
4741 format
, imageSize
, pixels
);
4743 /* Compressed images don't have a client format */
4744 image_stride
= _mesa_format_image_size(texImage
->TexFormat
,
4746 texImage
->Height
, 1);
4748 pixels
+= image_stride
;
4749 imageSize
-= image_stride
;
4753 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4756 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, texImage
,
4757 texObj
->Target
, level
,
4758 xoffset
, yoffset
, zoffset
,
4759 width
, height
, depth
,
4760 format
, imageSize
, data
);
4765 get_texbuffer_format(const struct gl_context
*ctx
, GLenum internalFormat
)
4767 if (ctx
->API
== API_OPENGL_COMPAT
) {
4768 switch (internalFormat
) {
4770 return MESA_FORMAT_A_UNORM8
;
4772 return MESA_FORMAT_A_UNORM16
;
4773 case GL_ALPHA16F_ARB
:
4774 return MESA_FORMAT_A_FLOAT16
;
4775 case GL_ALPHA32F_ARB
:
4776 return MESA_FORMAT_A_FLOAT32
;
4777 case GL_ALPHA8I_EXT
:
4778 return MESA_FORMAT_A_SINT8
;
4779 case GL_ALPHA16I_EXT
:
4780 return MESA_FORMAT_A_SINT16
;
4781 case GL_ALPHA32I_EXT
:
4782 return MESA_FORMAT_A_SINT32
;
4783 case GL_ALPHA8UI_EXT
:
4784 return MESA_FORMAT_A_UINT8
;
4785 case GL_ALPHA16UI_EXT
:
4786 return MESA_FORMAT_A_UINT16
;
4787 case GL_ALPHA32UI_EXT
:
4788 return MESA_FORMAT_A_UINT32
;
4790 return MESA_FORMAT_L_UNORM8
;
4791 case GL_LUMINANCE16
:
4792 return MESA_FORMAT_L_UNORM16
;
4793 case GL_LUMINANCE16F_ARB
:
4794 return MESA_FORMAT_L_FLOAT16
;
4795 case GL_LUMINANCE32F_ARB
:
4796 return MESA_FORMAT_L_FLOAT32
;
4797 case GL_LUMINANCE8I_EXT
:
4798 return MESA_FORMAT_L_SINT8
;
4799 case GL_LUMINANCE16I_EXT
:
4800 return MESA_FORMAT_L_SINT16
;
4801 case GL_LUMINANCE32I_EXT
:
4802 return MESA_FORMAT_L_SINT32
;
4803 case GL_LUMINANCE8UI_EXT
:
4804 return MESA_FORMAT_L_UINT8
;
4805 case GL_LUMINANCE16UI_EXT
:
4806 return MESA_FORMAT_L_UINT16
;
4807 case GL_LUMINANCE32UI_EXT
:
4808 return MESA_FORMAT_L_UINT32
;
4809 case GL_LUMINANCE8_ALPHA8
:
4810 return MESA_FORMAT_L8A8_UNORM
;
4811 case GL_LUMINANCE16_ALPHA16
:
4812 return MESA_FORMAT_L16A16_UNORM
;
4813 case GL_LUMINANCE_ALPHA16F_ARB
:
4814 return MESA_FORMAT_LA_FLOAT16
;
4815 case GL_LUMINANCE_ALPHA32F_ARB
:
4816 return MESA_FORMAT_LA_FLOAT32
;
4817 case GL_LUMINANCE_ALPHA8I_EXT
:
4818 return MESA_FORMAT_LA_SINT8
;
4819 case GL_LUMINANCE_ALPHA16I_EXT
:
4820 return MESA_FORMAT_LA_SINT16
;
4821 case GL_LUMINANCE_ALPHA32I_EXT
:
4822 return MESA_FORMAT_LA_SINT32
;
4823 case GL_LUMINANCE_ALPHA8UI_EXT
:
4824 return MESA_FORMAT_LA_UINT8
;
4825 case GL_LUMINANCE_ALPHA16UI_EXT
:
4826 return MESA_FORMAT_LA_UINT16
;
4827 case GL_LUMINANCE_ALPHA32UI_EXT
:
4828 return MESA_FORMAT_LA_UINT32
;
4830 return MESA_FORMAT_I_UNORM8
;
4831 case GL_INTENSITY16
:
4832 return MESA_FORMAT_I_UNORM16
;
4833 case GL_INTENSITY16F_ARB
:
4834 return MESA_FORMAT_I_FLOAT16
;
4835 case GL_INTENSITY32F_ARB
:
4836 return MESA_FORMAT_I_FLOAT32
;
4837 case GL_INTENSITY8I_EXT
:
4838 return MESA_FORMAT_I_SINT8
;
4839 case GL_INTENSITY16I_EXT
:
4840 return MESA_FORMAT_I_SINT16
;
4841 case GL_INTENSITY32I_EXT
:
4842 return MESA_FORMAT_I_SINT32
;
4843 case GL_INTENSITY8UI_EXT
:
4844 return MESA_FORMAT_I_UINT8
;
4845 case GL_INTENSITY16UI_EXT
:
4846 return MESA_FORMAT_I_UINT16
;
4847 case GL_INTENSITY32UI_EXT
:
4848 return MESA_FORMAT_I_UINT32
;
4854 if (_mesa_has_ARB_texture_buffer_object_rgb32(ctx
) ||
4855 _mesa_has_OES_texture_buffer(ctx
)) {
4856 switch (internalFormat
) {
4858 return MESA_FORMAT_RGB_FLOAT32
;
4860 return MESA_FORMAT_RGB_UINT32
;
4862 return MESA_FORMAT_RGB_SINT32
;
4868 switch (internalFormat
) {
4870 return MESA_FORMAT_R8G8B8A8_UNORM
;
4872 if (_mesa_is_gles(ctx
))
4873 return MESA_FORMAT_NONE
;
4874 return MESA_FORMAT_RGBA_UNORM16
;
4875 case GL_RGBA16F_ARB
:
4876 return MESA_FORMAT_RGBA_FLOAT16
;
4877 case GL_RGBA32F_ARB
:
4878 return MESA_FORMAT_RGBA_FLOAT32
;
4880 return MESA_FORMAT_RGBA_SINT8
;
4881 case GL_RGBA16I_EXT
:
4882 return MESA_FORMAT_RGBA_SINT16
;
4883 case GL_RGBA32I_EXT
:
4884 return MESA_FORMAT_RGBA_SINT32
;
4885 case GL_RGBA8UI_EXT
:
4886 return MESA_FORMAT_RGBA_UINT8
;
4887 case GL_RGBA16UI_EXT
:
4888 return MESA_FORMAT_RGBA_UINT16
;
4889 case GL_RGBA32UI_EXT
:
4890 return MESA_FORMAT_RGBA_UINT32
;
4893 return MESA_FORMAT_R8G8_UNORM
;
4895 if (_mesa_is_gles(ctx
))
4896 return MESA_FORMAT_NONE
;
4897 return MESA_FORMAT_R16G16_UNORM
;
4899 return MESA_FORMAT_RG_FLOAT16
;
4901 return MESA_FORMAT_RG_FLOAT32
;
4903 return MESA_FORMAT_RG_SINT8
;
4905 return MESA_FORMAT_RG_SINT16
;
4907 return MESA_FORMAT_RG_SINT32
;
4909 return MESA_FORMAT_RG_UINT8
;
4911 return MESA_FORMAT_RG_UINT16
;
4913 return MESA_FORMAT_RG_UINT32
;
4916 return MESA_FORMAT_R_UNORM8
;
4918 if (_mesa_is_gles(ctx
))
4919 return MESA_FORMAT_NONE
;
4920 return MESA_FORMAT_R_UNORM16
;
4922 return MESA_FORMAT_R_FLOAT16
;
4924 return MESA_FORMAT_R_FLOAT32
;
4926 return MESA_FORMAT_R_SINT8
;
4928 return MESA_FORMAT_R_SINT16
;
4930 return MESA_FORMAT_R_SINT32
;
4932 return MESA_FORMAT_R_UINT8
;
4934 return MESA_FORMAT_R_UINT16
;
4936 return MESA_FORMAT_R_UINT32
;
4939 return MESA_FORMAT_NONE
;
4945 _mesa_validate_texbuffer_format(const struct gl_context
*ctx
,
4946 GLenum internalFormat
)
4948 mesa_format format
= get_texbuffer_format(ctx
, internalFormat
);
4951 if (format
== MESA_FORMAT_NONE
)
4952 return MESA_FORMAT_NONE
;
4954 datatype
= _mesa_get_format_datatype(format
);
4956 /* The GL_ARB_texture_buffer_object spec says:
4958 * "If ARB_texture_float is not supported, references to the
4959 * floating-point internal formats provided by that extension should be
4960 * removed, and such formats may not be passed to TexBufferARB."
4962 * As a result, GL_HALF_FLOAT internal format depends on both
4963 * GL_ARB_texture_float and GL_ARB_half_float_pixel.
4965 if ((datatype
== GL_FLOAT
|| datatype
== GL_HALF_FLOAT
) &&
4966 !ctx
->Extensions
.ARB_texture_float
)
4967 return MESA_FORMAT_NONE
;
4969 if (!ctx
->Extensions
.ARB_texture_rg
) {
4970 GLenum base_format
= _mesa_get_format_base_format(format
);
4971 if (base_format
== GL_R
|| base_format
== GL_RG
)
4972 return MESA_FORMAT_NONE
;
4975 if (!ctx
->Extensions
.ARB_texture_buffer_object_rgb32
) {
4976 GLenum base_format
= _mesa_get_format_base_format(format
);
4977 if (base_format
== GL_RGB
)
4978 return MESA_FORMAT_NONE
;
4985 _mesa_texture_buffer_range(struct gl_context
*ctx
,
4986 struct gl_texture_object
*texObj
,
4987 GLenum internalFormat
,
4988 struct gl_buffer_object
*bufObj
,
4989 GLintptr offset
, GLsizeiptr size
,
4994 /* NOTE: ARB_texture_buffer_object has interactions with
4995 * the compatibility profile that are not implemented.
4997 if (!_mesa_has_ARB_texture_buffer_object(ctx
) &&
4998 !_mesa_has_OES_texture_buffer(ctx
)) {
4999 _mesa_error(ctx
, GL_INVALID_OPERATION
,
5000 "%s(ARB_texture_buffer_object is not"
5001 " implemented for the compatibility profile)", caller
);
5005 format
= _mesa_validate_texbuffer_format(ctx
, internalFormat
);
5006 if (format
== MESA_FORMAT_NONE
) {
5007 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat %s)",
5008 caller
, _mesa_enum_to_string(internalFormat
));
5012 FLUSH_VERTICES(ctx
, 0);
5014 _mesa_lock_texture(ctx
, texObj
);
5016 _mesa_reference_buffer_object(ctx
, &texObj
->BufferObject
, bufObj
);
5017 texObj
->BufferObjectFormat
= internalFormat
;
5018 texObj
->_BufferObjectFormat
= format
;
5019 texObj
->BufferOffset
= offset
;
5020 texObj
->BufferSize
= size
;
5022 _mesa_unlock_texture(ctx
, texObj
);
5024 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTextureBuffer
;
5027 bufObj
->UsageHistory
|= USAGE_TEXTURE_BUFFER
;
5033 * Make sure the texture buffer target is GL_TEXTURE_BUFFER.
5034 * Return true if it is, and return false if it is not
5035 * (and throw INVALID ENUM as dictated in the OpenGL 4.5
5036 * core spec, 02.02.2015, PDF page 245).
5039 check_texture_buffer_target(struct gl_context
*ctx
, GLenum target
,
5042 if (target
!= GL_TEXTURE_BUFFER_ARB
) {
5043 _mesa_error(ctx
, GL_INVALID_ENUM
,
5044 "%s(texture target is not GL_TEXTURE_BUFFER)", caller
);
5052 * Check for errors related to the texture buffer range.
5053 * Return false if errors are found, true if none are found.
5056 check_texture_buffer_range(struct gl_context
*ctx
,
5057 struct gl_buffer_object
*bufObj
,
5058 GLintptr offset
, GLsizeiptr size
,
5061 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5062 * Textures (PDF page 245):
5063 * "An INVALID_VALUE error is generated if offset is negative, if
5064 * size is less than or equal to zero, or if offset + size is greater
5065 * than the value of BUFFER_SIZE for the buffer bound to target."
5068 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset=%d < 0)", caller
,
5074 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size=%d <= 0)", caller
,
5079 if (offset
+ size
> bufObj
->Size
) {
5080 _mesa_error(ctx
, GL_INVALID_VALUE
,
5081 "%s(offset=%d + size=%d > buffer_size=%d)", caller
,
5082 (int) offset
, (int) size
, (int) bufObj
->Size
);
5086 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5087 * Textures (PDF page 245):
5088 * "An INVALID_VALUE error is generated if offset is not an integer
5089 * multiple of the value of TEXTURE_BUFFER_OFFSET_ALIGNMENT."
5091 if (offset
% ctx
->Const
.TextureBufferOffsetAlignment
) {
5092 _mesa_error(ctx
, GL_INVALID_VALUE
,
5093 "%s(invalid offset alignment)", caller
);
5101 /** GL_ARB_texture_buffer_object */
5103 _mesa_TexBuffer(GLenum target
, GLenum internalFormat
, GLuint buffer
)
5105 struct gl_texture_object
*texObj
;
5106 struct gl_buffer_object
*bufObj
;
5108 GET_CURRENT_CONTEXT(ctx
);
5110 /* Need to catch a bad target before it gets to
5111 * _mesa_get_current_tex_object.
5113 if (!check_texture_buffer_target(ctx
, target
, "glTexBuffer"))
5117 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glTexBuffer");
5123 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5127 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
, bufObj
, 0,
5128 buffer
? -1 : 0, "glTexBuffer");
5132 /** GL_ARB_texture_buffer_range */
5134 _mesa_TexBufferRange(GLenum target
, GLenum internalFormat
, GLuint buffer
,
5135 GLintptr offset
, GLsizeiptr size
)
5137 struct gl_texture_object
*texObj
;
5138 struct gl_buffer_object
*bufObj
;
5140 GET_CURRENT_CONTEXT(ctx
);
5142 /* Need to catch a bad target before it gets to
5143 * _mesa_get_current_tex_object.
5145 if (!check_texture_buffer_target(ctx
, target
, "glTexBufferRange"))
5149 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glTexBufferRange");
5153 if (!check_texture_buffer_range(ctx
, bufObj
, offset
, size
,
5154 "glTexBufferRange"))
5158 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5159 * Textures (PDF page 254):
5160 * "If buffer is zero, then any buffer object attached to the buffer
5161 * texture is detached, the values offset and size are ignored and
5162 * the state for offset and size for the buffer texture are reset to
5170 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5174 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
, bufObj
,
5175 offset
, size
, "glTexBufferRange");
5179 _mesa_TextureBuffer(GLuint texture
, GLenum internalFormat
, GLuint buffer
)
5181 struct gl_texture_object
*texObj
;
5182 struct gl_buffer_object
*bufObj
;
5184 GET_CURRENT_CONTEXT(ctx
);
5187 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glTextureBuffer");
5193 /* Get the texture object by Name. */
5194 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glTextureBuffer");
5198 if (!check_texture_buffer_target(ctx
, texObj
->Target
, "glTextureBuffer"))
5201 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
,
5202 bufObj
, 0, buffer
? -1 : 0, "glTextureBuffer");
5206 _mesa_TextureBufferRange(GLuint texture
, GLenum internalFormat
, GLuint buffer
,
5207 GLintptr offset
, GLsizeiptr size
)
5209 struct gl_texture_object
*texObj
;
5210 struct gl_buffer_object
*bufObj
;
5212 GET_CURRENT_CONTEXT(ctx
);
5215 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
5216 "glTextureBufferRange");
5220 if (!check_texture_buffer_range(ctx
, bufObj
, offset
, size
,
5221 "glTextureBufferRange"))
5225 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5226 * Textures (PDF page 254):
5227 * "If buffer is zero, then any buffer object attached to the buffer
5228 * texture is detached, the values offset and size are ignored and
5229 * the state for offset and size for the buffer texture are reset to
5237 /* Get the texture object by Name. */
5238 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glTextureBufferRange");
5242 if (!check_texture_buffer_target(ctx
, texObj
->Target
,
5243 "glTextureBufferRange"))
5246 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
,
5247 bufObj
, offset
, size
, "glTextureBufferRange");
5251 _mesa_is_renderable_texture_format(struct gl_context
*ctx
, GLenum internalformat
)
5253 /* Everything that is allowed for renderbuffers,
5254 * except for a base format of GL_STENCIL_INDEX, unless supported.
5256 GLenum baseFormat
= _mesa_base_fbo_format(ctx
, internalformat
);
5257 if (ctx
->Extensions
.ARB_texture_stencil8
)
5258 return baseFormat
!= 0;
5260 return baseFormat
!= 0 && baseFormat
!= GL_STENCIL_INDEX
;
5264 /** GL_ARB_texture_multisample */
5266 check_multisample_target(GLuint dims
, GLenum target
, bool dsa
)
5269 case GL_TEXTURE_2D_MULTISAMPLE
:
5271 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
5272 return dims
== 2 && !dsa
;
5273 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
5275 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
5276 return dims
== 3 && !dsa
;
5284 texture_image_multisample(struct gl_context
*ctx
, GLuint dims
,
5285 struct gl_texture_object
*texObj
,
5286 GLenum target
, GLsizei samples
,
5287 GLint internalformat
, GLsizei width
,
5288 GLsizei height
, GLsizei depth
,
5289 GLboolean fixedsamplelocations
,
5290 GLboolean immutable
, const char *func
)
5292 struct gl_texture_image
*texImage
;
5293 GLboolean sizeOK
, dimensionsOK
, samplesOK
;
5294 mesa_format texFormat
;
5295 GLenum sample_count_error
;
5296 bool dsa
= strstr(func
, "ture") ? true : false;
5298 if (!((ctx
->Extensions
.ARB_texture_multisample
5299 && _mesa_is_desktop_gl(ctx
))) && !_mesa_is_gles31(ctx
)) {
5300 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unsupported)", func
);
5305 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(samples < 1)", func
);
5309 if (!check_multisample_target(dims
, target
, dsa
)) {
5311 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(target)", func
);
5315 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
5320 /* check that the specified internalformat is color/depth/stencil-renderable;
5321 * refer GL3.1 spec 4.4.4
5324 if (immutable
&& !_mesa_is_legal_tex_storage_format(ctx
, internalformat
)) {
5325 _mesa_error(ctx
, GL_INVALID_ENUM
,
5326 "%s(internalformat=%s not legal for immutable-format)",
5327 func
, _mesa_enum_to_string(internalformat
));
5331 if (!_mesa_is_renderable_texture_format(ctx
, internalformat
)) {
5332 /* Page 172 of OpenGL ES 3.1 spec says:
5333 * "An INVALID_ENUM error is generated if sizedinternalformat is not
5334 * color-renderable, depth-renderable, or stencil-renderable (as
5335 * defined in section 9.4).
5337 * (Same error is also defined for desktop OpenGL for multisample
5338 * teximage/texstorage functions.)
5340 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalformat=%s)", func
,
5341 _mesa_enum_to_string(internalformat
));
5345 sample_count_error
= _mesa_check_sample_count(ctx
, target
,
5346 internalformat
, samples
);
5347 samplesOK
= sample_count_error
== GL_NO_ERROR
;
5349 /* Page 254 of OpenGL 4.4 spec says:
5350 * "Proxy arrays for two-dimensional multisample and two-dimensional
5351 * multisample array textures are operated on in the same way when
5352 * TexImage2DMultisample is called with target specified as
5353 * PROXY_TEXTURE_2D_MULTISAMPLE, or TexImage3DMultisample is called
5354 * with target specified as PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY.
5355 * However, if samples is not supported, then no error is generated.
5357 if (!samplesOK
&& !_mesa_is_proxy_texture(target
)) {
5358 _mesa_error(ctx
, sample_count_error
, "%s(samples)", func
);
5362 if (immutable
&& (!texObj
|| (texObj
->Name
== 0))) {
5363 _mesa_error(ctx
, GL_INVALID_OPERATION
,
5364 "%s(texture object 0)",
5369 texImage
= _mesa_get_tex_image(ctx
, texObj
, 0, 0);
5371 if (texImage
== NULL
) {
5372 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", func
);
5376 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, 0,
5377 internalformat
, GL_NONE
, GL_NONE
);
5378 assert(texFormat
!= MESA_FORMAT_NONE
);
5380 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, 0,
5381 width
, height
, depth
, 0);
5383 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, target
, 0, texFormat
,
5384 width
, height
, depth
, 0);
5386 if (_mesa_is_proxy_texture(target
)) {
5387 if (samplesOK
&& dimensionsOK
&& sizeOK
) {
5388 init_teximage_fields_ms(ctx
, texImage
, width
, height
, depth
, 0,
5389 internalformat
, texFormat
,
5390 samples
, fixedsamplelocations
);
5393 /* clear all image fields */
5394 clear_teximage_fields(texImage
);
5398 if (!dimensionsOK
) {
5399 _mesa_error(ctx
, GL_INVALID_VALUE
,
5400 "%s(invalid width or height)", func
);
5405 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(texture too large)", func
);
5409 /* Check if texObj->Immutable is set */
5410 if (texObj
->Immutable
) {
5411 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
5415 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
5417 init_teximage_fields_ms(ctx
, texImage
, width
, height
, depth
, 0,
5418 internalformat
, texFormat
,
5419 samples
, fixedsamplelocations
);
5421 if (width
> 0 && height
> 0 && depth
> 0) {
5422 if (!ctx
->Driver
.AllocTextureStorage(ctx
, texObj
, 1,
5423 width
, height
, depth
)) {
5424 /* tidy up the texture image state. strictly speaking,
5425 * we're allowed to just leave this in whatever state we
5426 * like, but being tidy is good.
5428 _mesa_init_teximage_fields(ctx
, texImage
,
5429 0, 0, 0, 0, GL_NONE
, MESA_FORMAT_NONE
);
5433 texObj
->Immutable
|= immutable
;
5436 _mesa_set_texture_view_state(ctx
, texObj
, target
, 1);
5439 _mesa_update_fbo_texture(ctx
, texObj
, 0, 0);
5445 _mesa_TexImage2DMultisample(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 texture_image_multisample(ctx
, 2, texObj
, target
, samples
,
5457 internalformat
, width
, height
, 1,
5458 fixedsamplelocations
, GL_FALSE
,
5459 "glTexImage2DMultisample");
5464 _mesa_TexImage3DMultisample(GLenum target
, GLsizei samples
,
5465 GLenum internalformat
, GLsizei width
,
5466 GLsizei height
, GLsizei depth
,
5467 GLboolean fixedsamplelocations
)
5469 struct gl_texture_object
*texObj
;
5470 GET_CURRENT_CONTEXT(ctx
);
5472 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5476 texture_image_multisample(ctx
, 3, texObj
, target
, samples
,
5477 internalformat
, width
, height
, depth
,
5478 fixedsamplelocations
, GL_FALSE
,
5479 "glTexImage3DMultisample");
5483 valid_texstorage_ms_parameters(GLsizei width
, GLsizei height
, GLsizei depth
,
5484 GLsizei samples
, unsigned dims
)
5486 GET_CURRENT_CONTEXT(ctx
);
5488 if (!_mesa_valid_tex_storage_dim(width
, height
, depth
)) {
5489 _mesa_error(ctx
, GL_INVALID_VALUE
,
5490 "glTexStorage%uDMultisample(width=%d,height=%d,depth=%d)",
5491 dims
, width
, height
, depth
);
5498 _mesa_TexStorage2DMultisample(GLenum target
, GLsizei samples
,
5499 GLenum internalformat
, GLsizei width
,
5500 GLsizei height
, GLboolean fixedsamplelocations
)
5502 struct gl_texture_object
*texObj
;
5503 GET_CURRENT_CONTEXT(ctx
);
5505 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5509 if (!valid_texstorage_ms_parameters(width
, height
, 1, samples
, 2))
5512 texture_image_multisample(ctx
, 2, texObj
, target
, samples
,
5513 internalformat
, width
, height
, 1,
5514 fixedsamplelocations
, GL_TRUE
,
5515 "glTexStorage2DMultisample");
5519 _mesa_TexStorage3DMultisample(GLenum target
, GLsizei samples
,
5520 GLenum internalformat
, GLsizei width
,
5521 GLsizei height
, GLsizei depth
,
5522 GLboolean fixedsamplelocations
)
5524 struct gl_texture_object
*texObj
;
5525 GET_CURRENT_CONTEXT(ctx
);
5527 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5531 if (!valid_texstorage_ms_parameters(width
, height
, depth
, samples
, 3))
5534 texture_image_multisample(ctx
, 3, texObj
, target
, samples
,
5535 internalformat
, width
, height
, depth
,
5536 fixedsamplelocations
, GL_TRUE
,
5537 "glTexStorage3DMultisample");
5541 _mesa_TextureStorage2DMultisample(GLuint texture
, GLsizei samples
,
5542 GLenum internalformat
, GLsizei width
,
5544 GLboolean fixedsamplelocations
)
5546 struct gl_texture_object
*texObj
;
5547 GET_CURRENT_CONTEXT(ctx
);
5549 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
5550 "glTextureStorage2DMultisample");
5554 if (!valid_texstorage_ms_parameters(width
, height
, 1, samples
, 2))
5557 texture_image_multisample(ctx
, 2, texObj
, texObj
->Target
, samples
,
5558 internalformat
, width
, height
, 1,
5559 fixedsamplelocations
, GL_TRUE
,
5560 "glTextureStorage2DMultisample");
5564 _mesa_TextureStorage3DMultisample(GLuint texture
, GLsizei samples
,
5565 GLenum internalformat
, GLsizei width
,
5566 GLsizei height
, GLsizei depth
,
5567 GLboolean fixedsamplelocations
)
5569 struct gl_texture_object
*texObj
;
5570 GET_CURRENT_CONTEXT(ctx
);
5572 /* Get the texture object by Name. */
5573 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
5574 "glTextureStorage3DMultisample");
5578 if (!valid_texstorage_ms_parameters(width
, height
, depth
, samples
, 3))
5581 texture_image_multisample(ctx
, 3, texObj
, texObj
->Target
, samples
,
5582 internalformat
, width
, height
, depth
,
5583 fixedsamplelocations
, GL_TRUE
,
5584 "glTextureStorage3DMultisample");