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()");
1107 * Do error checking of xoffset, yoffset, zoffset, width, height and depth
1108 * for glTexSubImage, glCopyTexSubImage and glCompressedTexSubImage.
1109 * \param destImage the destination texture image.
1110 * \return GL_TRUE if error found, GL_FALSE otherwise.
1113 error_check_subtexture_dimensions(struct gl_context
*ctx
, GLuint dims
,
1114 const struct gl_texture_image
*destImage
,
1115 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1116 GLsizei subWidth
, GLsizei subHeight
,
1117 GLsizei subDepth
, const char *func
)
1119 const GLenum target
= destImage
->TexObject
->Target
;
1124 _mesa_error(ctx
, GL_INVALID_VALUE
,
1125 "%s(width=%d)", func
, subWidth
);
1129 if (dims
> 1 && subHeight
< 0) {
1130 _mesa_error(ctx
, GL_INVALID_VALUE
,
1131 "%s(height=%d)", func
, subHeight
);
1135 if (dims
> 2 && subDepth
< 0) {
1136 _mesa_error(ctx
, GL_INVALID_VALUE
,
1137 "%s(depth=%d)", func
, subDepth
);
1141 /* check xoffset and width */
1142 if (xoffset
< - (GLint
) destImage
->Border
) {
1143 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(xoffset)", func
);
1147 if (xoffset
+ subWidth
> (GLint
) destImage
->Width
) {
1148 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(xoffset+width)", func
);
1152 /* check yoffset and height */
1154 GLint yBorder
= (target
== GL_TEXTURE_1D_ARRAY
) ? 0 : destImage
->Border
;
1155 if (yoffset
< -yBorder
) {
1156 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(yoffset)", func
);
1159 if (yoffset
+ subHeight
> (GLint
) destImage
->Height
) {
1160 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(yoffset+height)", func
);
1165 /* check zoffset and depth */
1168 GLint zBorder
= (target
== GL_TEXTURE_2D_ARRAY
||
1169 target
== GL_TEXTURE_CUBE_MAP_ARRAY
) ?
1170 0 : destImage
->Border
;
1172 if (zoffset
< -zBorder
) {
1173 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(zoffset)", func
);
1177 depth
= (GLint
) destImage
->Depth
;
1178 if (target
== GL_TEXTURE_CUBE_MAP
)
1180 if (zoffset
+ subDepth
> depth
) {
1181 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(zoffset+depth)", func
);
1187 * The OpenGL spec (and GL_ARB_texture_compression) says only whole
1188 * compressed texture images can be updated. But, that restriction may be
1189 * relaxed for particular compressed formats. At this time, all the
1190 * compressed formats supported by Mesa allow sub-textures to be updated
1191 * along compressed block boundaries.
1193 _mesa_get_format_block_size_3d(destImage
->TexFormat
, &bw
, &bh
, &bd
);
1195 if (bw
!= 1 || bh
!= 1 || bd
!= 1) {
1196 /* offset must be multiple of block size */
1197 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0) || (zoffset
% bd
!= 0)) {
1198 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1199 "%s(xoffset = %d, yoffset = %d, zoffset = %d)",
1200 func
, xoffset
, yoffset
, zoffset
);
1204 /* The size must be a multiple of bw x bh, or we must be using a
1205 * offset+size that exactly hits the edge of the image. This
1206 * is important for small mipmap levels (1x1, 2x1, etc) and for
1209 if ((subWidth
% bw
!= 0) &&
1210 (xoffset
+ subWidth
!= (GLint
) destImage
->Width
)) {
1211 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1212 "%s(width = %d)", func
, subWidth
);
1216 if ((subHeight
% bh
!= 0) &&
1217 (yoffset
+ subHeight
!= (GLint
) destImage
->Height
)) {
1218 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1219 "%s(height = %d)", func
, subHeight
);
1223 if ((subDepth
% bd
!= 0) &&
1224 (zoffset
+ subDepth
!= (GLint
) destImage
->Depth
)) {
1225 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1226 "%s(depth = %d)", func
, subDepth
);
1238 * This is the fallback for Driver.TestProxyTexImage() for doing device-
1239 * specific texture image size checks.
1241 * A hardware driver might override this function if, for example, the
1242 * max 3D texture size is 512x512x64 (i.e. not a cube).
1244 * Note that width, height, depth == 0 is not an error. However, a
1245 * texture with zero width/height/depth will be considered "incomplete"
1246 * and texturing will effectively be disabled.
1248 * \param target any texture target/type
1249 * \param level as passed to glTexImage
1250 * \param format the MESA_FORMAT_x for the tex image
1251 * \param width as passed to glTexImage
1252 * \param height as passed to glTexImage
1253 * \param depth as passed to glTexImage
1254 * \param border as passed to glTexImage
1255 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1258 _mesa_test_proxy_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1260 GLint width
, GLint height
, GLint depth
, GLint border
)
1262 /* We just check if the image size is less than MaxTextureMbytes.
1263 * Some drivers may do more specific checks.
1265 uint64_t bytes
= _mesa_format_image_size64(format
, width
, height
, depth
);
1266 uint64_t mbytes
= bytes
/ (1024 * 1024); /* convert to MB */
1267 mbytes
*= _mesa_num_tex_faces(target
);
1268 return mbytes
<= (uint64_t) ctx
->Const
.MaxTextureMbytes
;
1273 * Return true if the format is only valid for glCompressedTexImage.
1276 compressedteximage_only_format(const struct gl_context
*ctx
, GLenum format
)
1279 case GL_ETC1_RGB8_OES
:
1280 case GL_PALETTE4_RGB8_OES
:
1281 case GL_PALETTE4_RGBA8_OES
:
1282 case GL_PALETTE4_R5_G6_B5_OES
:
1283 case GL_PALETTE4_RGBA4_OES
:
1284 case GL_PALETTE4_RGB5_A1_OES
:
1285 case GL_PALETTE8_RGB8_OES
:
1286 case GL_PALETTE8_RGBA8_OES
:
1287 case GL_PALETTE8_R5_G6_B5_OES
:
1288 case GL_PALETTE8_RGBA4_OES
:
1289 case GL_PALETTE8_RGB5_A1_OES
:
1297 * Return true if the format doesn't support online compression.
1300 _mesa_format_no_online_compression(const struct gl_context
*ctx
, GLenum format
)
1302 return _mesa_is_astc_format(format
) ||
1303 compressedteximage_only_format(ctx
, format
);
1306 /* Writes to an GL error pointer if non-null and returns whether or not the
1307 * error is GL_NO_ERROR */
1309 write_error(GLenum
*err_ptr
, GLenum error
)
1314 return error
== GL_NO_ERROR
;
1318 * Helper function to determine whether a target and specific compression
1319 * format are supported. The error parameter returns GL_NO_ERROR if the
1320 * target can be compressed. Otherwise it returns either GL_INVALID_OPERATION
1321 * or GL_INVALID_ENUM, whichever is more appropriate.
1324 _mesa_target_can_be_compressed(const struct gl_context
*ctx
, GLenum target
,
1325 GLenum intFormat
, GLenum
*error
)
1327 GLboolean target_can_be_compresed
= GL_FALSE
;
1328 mesa_format format
= _mesa_glenum_to_compressed_format(intFormat
);
1329 enum mesa_format_layout layout
= _mesa_get_format_layout(format
);
1333 case GL_PROXY_TEXTURE_2D
:
1334 target_can_be_compresed
= GL_TRUE
; /* true for any compressed format so far */
1336 case GL_PROXY_TEXTURE_CUBE_MAP
:
1337 case GL_TEXTURE_CUBE_MAP
:
1338 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1339 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1340 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1341 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1342 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1343 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1344 target_can_be_compresed
= ctx
->Extensions
.ARB_texture_cube_map
;
1346 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1347 case GL_TEXTURE_2D_ARRAY_EXT
:
1348 target_can_be_compresed
= ctx
->Extensions
.EXT_texture_array
;
1350 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1351 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1352 /* From the KHR_texture_compression_astc_hdr spec:
1354 * Add a second new column "3D Tex." which is empty for all non-ASTC
1355 * formats. If only the LDR profile is supported by the
1356 * implementation, this column is also empty for all ASTC formats. If
1357 * both the LDR and HDR profiles are supported only, this column is
1358 * checked for all ASTC formats.
1360 * Add a third new column "Cube Map Array Tex." which is empty for all
1361 * non-ASTC formats, and checked for all ASTC formats.
1365 * 'An INVALID_OPERATION error is generated by CompressedTexImage3D
1366 * if <internalformat> is TEXTURE_CUBE_MAP_ARRAY and the
1367 * "Cube Map Array" column of table 8.19 is *not* checked, or if
1368 * <internalformat> is TEXTURE_3D and the "3D Tex." column of table
1369 * 8.19 is *not* checked'
1371 * The instances of <internalformat> above should say <target>.
1373 * ETC2/EAC formats are the only alternative in GLES and thus such errors
1374 * have already been handled by normal ETC2/EAC behavior.
1377 /* From section 3.8.6, page 146 of OpenGL ES 3.0 spec:
1379 * "The ETC2/EAC texture compression algorithm supports only
1380 * two-dimensional images. If internalformat is an ETC2/EAC format,
1381 * glCompressedTexImage3D will generate an INVALID_OPERATION error if
1382 * target is not TEXTURE_2D_ARRAY."
1384 * This should also be applicable for glTexStorage3D(). Other available
1385 * targets for these functions are: TEXTURE_3D and TEXTURE_CUBE_MAP_ARRAY.
1387 if (layout
== MESA_FORMAT_LAYOUT_ETC2
&& _mesa_is_gles3(ctx
))
1388 return write_error(error
, GL_INVALID_OPERATION
);
1389 target_can_be_compresed
= ctx
->Extensions
.ARB_texture_cube_map_array
;
1393 case MESA_FORMAT_LAYOUT_ETC2
:
1394 /* See ETC2/EAC comment in case GL_TEXTURE_CUBE_MAP_ARRAY. */
1395 if (_mesa_is_gles3(ctx
))
1396 return write_error(error
, GL_INVALID_OPERATION
);
1398 case MESA_FORMAT_LAYOUT_BPTC
:
1399 target_can_be_compresed
= ctx
->Extensions
.ARB_texture_compression_bptc
;
1401 case MESA_FORMAT_LAYOUT_ASTC
:
1402 target_can_be_compresed
=
1403 ctx
->Extensions
.KHR_texture_compression_astc_hdr
;
1405 /* Throw an INVALID_OPERATION error if the target is TEXTURE_3D and
1406 * and the hdr extension is not supported.
1407 * See comment in switch case GL_TEXTURE_CUBE_MAP_ARRAY for more info.
1409 if (!target_can_be_compresed
)
1410 return write_error(error
, GL_INVALID_OPERATION
);
1418 return write_error(error
,
1419 target_can_be_compresed
? GL_NO_ERROR
: GL_INVALID_ENUM
);
1424 * Check if the given texture target value is legal for a
1425 * glTexImage1/2/3D call.
1428 legal_teximage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
1434 case GL_PROXY_TEXTURE_1D
:
1435 return _mesa_is_desktop_gl(ctx
);
1443 case GL_PROXY_TEXTURE_2D
:
1444 return _mesa_is_desktop_gl(ctx
);
1445 case GL_PROXY_TEXTURE_CUBE_MAP
:
1446 return _mesa_is_desktop_gl(ctx
)
1447 && ctx
->Extensions
.ARB_texture_cube_map
;
1448 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1449 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1450 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1451 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1452 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1453 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1454 return ctx
->Extensions
.ARB_texture_cube_map
;
1455 case GL_TEXTURE_RECTANGLE_NV
:
1456 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1457 return _mesa_is_desktop_gl(ctx
)
1458 && ctx
->Extensions
.NV_texture_rectangle
;
1459 case GL_TEXTURE_1D_ARRAY_EXT
:
1460 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1461 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1469 case GL_PROXY_TEXTURE_3D
:
1470 return _mesa_is_desktop_gl(ctx
);
1471 case GL_TEXTURE_2D_ARRAY_EXT
:
1472 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1473 || _mesa_is_gles3(ctx
);
1474 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1475 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1476 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1477 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1478 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1483 _mesa_problem(ctx
, "invalid dims=%u in legal_teximage_target()", dims
);
1490 * Check if the given texture target value is legal for a
1491 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1492 * The difference compared to legal_teximage_target() above is that
1493 * proxy targets are not supported.
1496 legal_texsubimage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
,
1501 return _mesa_is_desktop_gl(ctx
) && target
== GL_TEXTURE_1D
;
1506 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1507 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1508 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1509 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1510 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1511 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1512 return ctx
->Extensions
.ARB_texture_cube_map
;
1513 case GL_TEXTURE_RECTANGLE_NV
:
1514 return _mesa_is_desktop_gl(ctx
)
1515 && ctx
->Extensions
.NV_texture_rectangle
;
1516 case GL_TEXTURE_1D_ARRAY_EXT
:
1517 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1525 case GL_TEXTURE_2D_ARRAY_EXT
:
1526 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1527 || _mesa_is_gles3(ctx
);
1528 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1529 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1530 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1532 /* Table 8.15 of the OpenGL 4.5 core profile spec
1533 * (20141030) says that TEXTURE_CUBE_MAP is valid for TextureSubImage3D
1534 * and CopyTextureSubImage3D.
1536 case GL_TEXTURE_CUBE_MAP
:
1542 _mesa_problem(ctx
, "invalid dims=%u in legal_texsubimage_target()",
1550 * Helper function to determine if a texture object is mutable (in terms
1551 * of GL_ARB_texture_storage).
1554 mutable_tex_object(struct gl_context
*ctx
, GLenum target
)
1556 struct gl_texture_object
*texObj
= _mesa_get_current_tex_object(ctx
, target
);
1560 return !texObj
->Immutable
;
1565 * Return expected size of a compressed texture.
1568 compressed_tex_size(GLsizei width
, GLsizei height
, GLsizei depth
,
1571 mesa_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
1572 return _mesa_format_image_size(mesaFormat
, width
, height
, depth
);
1576 * Verify that a texture format is valid with a particular target
1578 * In particular, textures with base format of \c GL_DEPTH_COMPONENT or
1579 * \c GL_DEPTH_STENCIL are only valid with certain, context dependent texture
1582 * \param ctx GL context
1583 * \param target Texture target
1584 * \param internalFormat Internal format of the texture image
1586 * \returns true if the combination is legal, false otherwise.
1589 _mesa_legal_texture_base_format_for_target(struct gl_context
*ctx
,
1590 GLenum target
, GLenum internalFormat
)
1592 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
1593 || _mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_STENCIL
1594 || _mesa_base_tex_format(ctx
, internalFormat
) == GL_STENCIL_INDEX
) {
1595 /* Section 3.8.3 (Texture Image Specification) of the OpenGL 3.3 Core
1596 * Profile spec says:
1598 * "Textures with a base internal format of DEPTH_COMPONENT or
1599 * DEPTH_STENCIL are supported by texture image specification
1600 * commands only if target is TEXTURE_1D, TEXTURE_2D,
1601 * TEXTURE_1D_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_RECTANGLE,
1602 * TEXTURE_CUBE_MAP, PROXY_TEXTURE_1D, PROXY_TEXTURE_2D,
1603 * PROXY_TEXTURE_1D_ARRAY, PROXY_TEXTURE_2D_ARRAY,
1604 * PROXY_TEXTURE_RECTANGLE, or PROXY_TEXTURE_CUBE_MAP. Using these
1605 * formats in conjunction with any other target will result in an
1606 * INVALID_OPERATION error."
1608 * Cubemaps are only supported with desktop OpenGL version >= 3.0,
1609 * EXT_gpu_shader4, or, on OpenGL ES 2.0+, OES_depth_texture_cube_map.
1611 if (target
!= GL_TEXTURE_1D
&&
1612 target
!= GL_PROXY_TEXTURE_1D
&&
1613 target
!= GL_TEXTURE_2D
&&
1614 target
!= GL_PROXY_TEXTURE_2D
&&
1615 target
!= GL_TEXTURE_1D_ARRAY
&&
1616 target
!= GL_PROXY_TEXTURE_1D_ARRAY
&&
1617 target
!= GL_TEXTURE_2D_ARRAY
&&
1618 target
!= GL_PROXY_TEXTURE_2D_ARRAY
&&
1619 target
!= GL_TEXTURE_RECTANGLE_ARB
&&
1620 target
!= GL_PROXY_TEXTURE_RECTANGLE_ARB
&&
1621 !((_mesa_is_cube_face(target
) ||
1622 target
== GL_TEXTURE_CUBE_MAP
||
1623 target
== GL_PROXY_TEXTURE_CUBE_MAP
) &&
1624 (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_gpu_shader4
1625 || (ctx
->API
== API_OPENGLES2
&& ctx
->Extensions
.OES_depth_texture_cube_map
))) &&
1626 !((target
== GL_TEXTURE_CUBE_MAP_ARRAY
||
1627 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
) &&
1628 ctx
->Extensions
.ARB_texture_cube_map_array
)) {
1637 texture_formats_agree(GLenum internalFormat
,
1640 GLboolean colorFormat
;
1641 GLboolean is_format_depth_or_depthstencil
;
1642 GLboolean is_internalFormat_depth_or_depthstencil
;
1644 /* Even though there are no color-index textures, we still have to support
1645 * uploading color-index data and remapping it to RGB via the
1646 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
1648 const GLboolean indexFormat
= (format
== GL_COLOR_INDEX
);
1650 is_internalFormat_depth_or_depthstencil
=
1651 _mesa_is_depth_format(internalFormat
) ||
1652 _mesa_is_depthstencil_format(internalFormat
);
1654 is_format_depth_or_depthstencil
=
1655 _mesa_is_depth_format(format
) ||
1656 _mesa_is_depthstencil_format(format
);
1658 colorFormat
= _mesa_is_color_format(format
);
1660 if (_mesa_is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
)
1663 if (is_internalFormat_depth_or_depthstencil
!=
1664 is_format_depth_or_depthstencil
)
1667 if (_mesa_is_ycbcr_format(internalFormat
) != _mesa_is_ycbcr_format(format
))
1674 * Test the combination of format, type and internal format arguments of
1675 * different texture operations on GLES.
1677 * \param ctx GL context.
1678 * \param format pixel data format given by the user.
1679 * \param type pixel data type given by the user.
1680 * \param internalFormat internal format given by the user.
1681 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1682 * \param callerName name of the caller function to print in the error message
1684 * \return true if a error is found, false otherwise
1686 * Currently, it is used by texture_error_check() and texsubimage_error_check().
1689 texture_format_error_check_gles(struct gl_context
*ctx
, GLenum format
,
1690 GLenum type
, GLenum internalFormat
,
1691 GLuint dimensions
, const char *callerName
)
1695 if (_mesa_is_gles3(ctx
)) {
1696 err
= _mesa_es3_error_check_format_and_type(ctx
, format
, type
,
1698 if (err
!= GL_NO_ERROR
) {
1699 _mesa_error(ctx
, err
,
1700 "%s(format = %s, type = %s, internalformat = %s)",
1701 callerName
, _mesa_enum_to_string(format
),
1702 _mesa_enum_to_string(type
),
1703 _mesa_enum_to_string(internalFormat
));
1708 err
= _mesa_es_error_check_format_and_type(ctx
, format
, type
, dimensions
);
1709 if (err
!= GL_NO_ERROR
) {
1710 _mesa_error(ctx
, err
, "%s(format = %s, type = %s)",
1711 callerName
, _mesa_enum_to_string(format
),
1712 _mesa_enum_to_string(type
));
1721 * Test the glTexImage[123]D() parameters for errors.
1723 * \param ctx GL context.
1724 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1725 * \param target texture target given by the user (already validated).
1726 * \param level image level given by the user.
1727 * \param internalFormat internal format given by the user.
1728 * \param format pixel data format given by the user.
1729 * \param type pixel data type given by the user.
1730 * \param width image width given by the user.
1731 * \param height image height given by the user.
1732 * \param depth image depth given by the user.
1733 * \param border image border given by the user.
1735 * \return GL_TRUE if a error is found, GL_FALSE otherwise
1737 * Verifies each of the parameters against the constants specified in
1738 * __struct gl_contextRec::Const and the supported extensions, and according
1739 * to the OpenGL specification.
1740 * Note that we don't fully error-check the width, height, depth values
1741 * here. That's done in _mesa_legal_texture_dimensions() which is used
1742 * by several other GL entrypoints. Plus, texture dims have a special
1743 * interaction with proxy textures.
1746 texture_error_check( struct gl_context
*ctx
,
1747 GLuint dimensions
, GLenum target
,
1748 GLint level
, GLint internalFormat
,
1749 GLenum format
, GLenum type
,
1750 GLint width
, GLint height
,
1751 GLint depth
, GLint border
,
1752 const GLvoid
*pixels
)
1756 /* Note: for proxy textures, some error conditions immediately generate
1757 * a GL error in the usual way. But others do not generate a GL error.
1758 * Instead, they cause the width, height, depth, format fields of the
1759 * texture image to be zeroed-out. The GL spec seems to indicate that the
1760 * zero-out behaviour is only used in cases related to memory allocation.
1764 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
1765 _mesa_error(ctx
, GL_INVALID_VALUE
,
1766 "glTexImage%dD(level=%d)", dimensions
, level
);
1771 if (border
< 0 || border
> 1 ||
1772 ((ctx
->API
!= API_OPENGL_COMPAT
||
1773 target
== GL_TEXTURE_RECTANGLE_NV
||
1774 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1775 _mesa_error(ctx
, GL_INVALID_VALUE
,
1776 "glTexImage%dD(border=%d)", dimensions
, border
);
1780 if (width
< 0 || height
< 0 || depth
< 0) {
1781 _mesa_error(ctx
, GL_INVALID_VALUE
,
1782 "glTexImage%dD(width, height or depth < 0)", dimensions
);
1786 /* Check incoming image format and type */
1787 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1788 if (err
!= GL_NO_ERROR
) {
1789 /* Prior to OpenGL-ES 2.0, an INVALID_VALUE is expected instead of
1790 * INVALID_ENUM. From page 73 OpenGL ES 1.1 spec:
1792 * "Specifying a value for internalformat that is not one of the
1793 * above (acceptable) values generates the error INVALID VALUE."
1795 if (err
== GL_INVALID_ENUM
&& _mesa_is_gles(ctx
) && ctx
->Version
< 20)
1796 err
= GL_INVALID_VALUE
;
1798 _mesa_error(ctx
, err
,
1799 "glTexImage%dD(incompatible format = %s, type = %s)",
1800 dimensions
, _mesa_enum_to_string(format
),
1801 _mesa_enum_to_string(type
));
1805 /* Check internalFormat */
1806 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1807 _mesa_error(ctx
, GL_INVALID_VALUE
,
1808 "glTexImage%dD(internalFormat=%s)",
1809 dimensions
, _mesa_enum_to_string(internalFormat
));
1813 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1814 * combinations of format, internalFormat, and type that can be used.
1815 * Formats and types that require additional extensions (e.g., GL_FLOAT
1816 * requires GL_OES_texture_float) are filtered elsewhere.
1818 if (_mesa_is_gles(ctx
) &&
1819 texture_format_error_check_gles(ctx
, format
, type
, internalFormat
,
1820 dimensions
, "glTexImage%dD")) {
1824 /* validate the bound PBO, if any */
1825 if (!_mesa_validate_pbo_source(ctx
, dimensions
, &ctx
->Unpack
,
1826 width
, height
, depth
, format
, type
,
1827 INT_MAX
, pixels
, "glTexImage")) {
1831 /* make sure internal format and format basically agree */
1832 if (!texture_formats_agree(internalFormat
, format
)) {
1833 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1834 "glTexImage%dD(incompatible internalFormat = %s, format = %s)",
1835 dimensions
, _mesa_enum_to_string(internalFormat
),
1836 _mesa_enum_to_string(format
));
1840 /* additional checks for ycbcr textures */
1841 if (internalFormat
== GL_YCBCR_MESA
) {
1842 assert(ctx
->Extensions
.MESA_ycbcr_texture
);
1843 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
1844 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
1846 _mesa_snprintf(message
, sizeof(message
),
1847 "glTexImage%dD(format/type YCBCR mismatch)",
1849 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", message
);
1850 return GL_TRUE
; /* error */
1852 if (target
!= GL_TEXTURE_2D
&&
1853 target
!= GL_PROXY_TEXTURE_2D
&&
1854 target
!= GL_TEXTURE_RECTANGLE_NV
&&
1855 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
1856 _mesa_error(ctx
, GL_INVALID_ENUM
,
1857 "glTexImage%dD(bad target for YCbCr texture)",
1863 _mesa_snprintf(message
, sizeof(message
),
1864 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1865 dimensions
, border
);
1866 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", message
);
1871 /* additional checks for depth textures */
1872 if (!_mesa_legal_texture_base_format_for_target(ctx
, target
, internalFormat
)) {
1873 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1874 "glTexImage%dD(bad target for texture)", dimensions
);
1878 /* additional checks for compressed textures */
1879 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
1881 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
, &err
)) {
1882 _mesa_error(ctx
, err
,
1883 "glTexImage%dD(target can't be compressed)", dimensions
);
1886 if (_mesa_format_no_online_compression(ctx
, internalFormat
)) {
1887 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1888 "glTexImage%dD(no compression for format)", dimensions
);
1892 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1893 "glTexImage%dD(border!=0)", dimensions
);
1898 /* additional checks for integer textures */
1899 if ((ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) &&
1900 (_mesa_is_enum_format_integer(format
) !=
1901 _mesa_is_enum_format_integer(internalFormat
))) {
1902 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1903 "glTexImage%dD(integer/non-integer format mismatch)",
1908 if (!mutable_tex_object(ctx
, target
)) {
1909 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1910 "glTexImage%dD(immutable texture)", dimensions
);
1914 /* if we get here, the parameters are OK */
1920 * Error checking for glCompressedTexImage[123]D().
1921 * Note that the width, height and depth values are not fully error checked
1923 * \return GL_TRUE if a error is found, GL_FALSE otherwise
1926 compressed_texture_error_check(struct gl_context
*ctx
, GLint dimensions
,
1927 GLenum target
, GLint level
,
1928 GLenum internalFormat
, GLsizei width
,
1929 GLsizei height
, GLsizei depth
, GLint border
,
1930 GLsizei imageSize
, const GLvoid
*data
)
1932 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1934 GLenum error
= GL_NO_ERROR
;
1935 char *reason
= ""; /* no error */
1937 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
, &error
)) {
1942 /* This will detect any invalid internalFormat value */
1943 if (!_mesa_is_compressed_format(ctx
, internalFormat
)) {
1944 _mesa_error(ctx
, GL_INVALID_ENUM
,
1945 "glCompressedTexImage%dD(internalFormat=%s)",
1946 dimensions
, _mesa_enum_to_string(internalFormat
));
1950 /* validate the bound PBO, if any */
1951 if (!_mesa_validate_pbo_source_compressed(ctx
, dimensions
, &ctx
->Unpack
,
1953 "glCompressedTexImage")) {
1957 switch (internalFormat
) {
1958 case GL_PALETTE4_RGB8_OES
:
1959 case GL_PALETTE4_RGBA8_OES
:
1960 case GL_PALETTE4_R5_G6_B5_OES
:
1961 case GL_PALETTE4_RGBA4_OES
:
1962 case GL_PALETTE4_RGB5_A1_OES
:
1963 case GL_PALETTE8_RGB8_OES
:
1964 case GL_PALETTE8_RGBA8_OES
:
1965 case GL_PALETTE8_R5_G6_B5_OES
:
1966 case GL_PALETTE8_RGBA4_OES
:
1967 case GL_PALETTE8_RGB5_A1_OES
:
1968 /* check level (note that level should be zero or less!) */
1969 if (level
> 0 || level
< -maxLevels
) {
1971 error
= GL_INVALID_VALUE
;
1975 if (dimensions
!= 2) {
1976 reason
= "compressed paletted textures must be 2D";
1977 error
= GL_INVALID_OPERATION
;
1981 /* Figure out the expected texture size (in bytes). This will be
1982 * checked against the actual size later.
1984 expectedSize
= _mesa_cpal_compressed_size(level
, internalFormat
,
1987 /* This is for the benefit of the TestProxyTexImage below. It expects
1988 * level to be non-negative. OES_compressed_paletted_texture uses a
1989 * weird mechanism where the level specified to glCompressedTexImage2D
1990 * is -(n-1) number of levels in the texture, and the data specifies the
1991 * complete mipmap stack. This is done to ensure the palette is the
1992 * same for all levels.
1999 if (level
< 0 || level
>= maxLevels
) {
2001 error
= GL_INVALID_VALUE
;
2005 /* Figure out the expected texture size (in bytes). This will be
2006 * checked against the actual size later.
2008 expectedSize
= compressed_tex_size(width
, height
, depth
, internalFormat
);
2012 /* This should really never fail */
2013 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
2014 reason
= "internalFormat";
2015 error
= GL_INVALID_ENUM
;
2019 /* No compressed formats support borders at this time */
2021 reason
= "border != 0";
2022 error
= GL_INVALID_VALUE
;
2026 /* Check for invalid pixel storage modes */
2027 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dimensions
,
2029 "glCompressedTexImage")) {
2033 /* check image size in bytes */
2034 if (expectedSize
!= imageSize
) {
2035 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
2036 * if <imageSize> is not consistent with the format, dimensions, and
2037 * contents of the specified image.
2039 reason
= "imageSize inconsistent with width/height/format";
2040 error
= GL_INVALID_VALUE
;
2044 if (!mutable_tex_object(ctx
, target
)) {
2045 reason
= "immutable texture";
2046 error
= GL_INVALID_OPERATION
;
2053 /* Note: not all error paths exit through here. */
2054 _mesa_error(ctx
, error
, "glCompressedTexImage%dD(%s)",
2055 dimensions
, reason
);
2062 * Test glTexSubImage[123]D() parameters for errors.
2064 * \param ctx GL context.
2065 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2066 * \param target texture target given by the user (already validated)
2067 * \param level image level given by the user.
2068 * \param xoffset sub-image x offset given by the user.
2069 * \param yoffset sub-image y offset given by the user.
2070 * \param zoffset sub-image z offset given by the user.
2071 * \param format pixel data format given by the user.
2072 * \param type pixel data type given by the user.
2073 * \param width image width given by the user.
2074 * \param height image height given by the user.
2075 * \param depth image depth given by the user.
2077 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2079 * Verifies each of the parameters against the constants specified in
2080 * __struct gl_contextRec::Const and the supported extensions, and according
2081 * to the OpenGL specification.
2084 texsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2085 struct gl_texture_object
*texObj
,
2086 GLenum target
, GLint level
,
2087 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2088 GLint width
, GLint height
, GLint depth
,
2089 GLenum format
, GLenum type
, const GLvoid
*pixels
,
2090 bool dsa
, const char *callerName
)
2092 struct gl_texture_image
*texImage
;
2096 /* must be out of memory */
2097 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", callerName
);
2102 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2103 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(level=%d)", callerName
, level
);
2107 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
2109 /* non-existant texture level */
2110 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid texture image)",
2115 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
2116 if (err
!= GL_NO_ERROR
) {
2117 _mesa_error(ctx
, err
,
2118 "%s(incompatible format = %s, type = %s)",
2119 callerName
, _mesa_enum_to_string(format
),
2120 _mesa_enum_to_string(type
));
2124 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2125 * combinations of format, internalFormat, and type that can be used.
2126 * Formats and types that require additional extensions (e.g., GL_FLOAT
2127 * requires GL_OES_texture_float) are filtered elsewhere.
2129 if (_mesa_is_gles(ctx
) &&
2130 texture_format_error_check_gles(ctx
, format
, type
,
2131 texImage
->InternalFormat
,
2132 dimensions
, callerName
)) {
2136 /* validate the bound PBO, if any */
2137 if (!_mesa_validate_pbo_source(ctx
, dimensions
, &ctx
->Unpack
,
2138 width
, height
, depth
, format
, type
,
2139 INT_MAX
, pixels
, callerName
)) {
2143 if (error_check_subtexture_dimensions(ctx
, dimensions
,
2144 texImage
, xoffset
, yoffset
, zoffset
,
2145 width
, height
, depth
, callerName
)) {
2149 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2150 if (_mesa_format_no_online_compression(ctx
, texImage
->InternalFormat
)) {
2151 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2152 "%s(no compression for format)", callerName
);
2157 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
2158 /* both source and dest must be integer-valued, or neither */
2159 if (_mesa_is_format_integer_color(texImage
->TexFormat
) !=
2160 _mesa_is_enum_format_integer(format
)) {
2161 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2162 "%s(integer/non-integer format mismatch)", callerName
);
2172 * Test glCopyTexImage[12]D() parameters for errors.
2174 * \param ctx GL context.
2175 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2176 * \param target texture target given by the user.
2177 * \param level image level given by the user.
2178 * \param internalFormat internal format given by the user.
2179 * \param width image width given by the user.
2180 * \param height image height given by the user.
2181 * \param border texture border.
2183 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2185 * Verifies each of the parameters against the constants specified in
2186 * __struct gl_contextRec::Const and the supported extensions, and according
2187 * to the OpenGL specification.
2190 copytexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
2191 GLenum target
, GLint level
, GLint internalFormat
,
2192 GLint width
, GLint height
, GLint border
)
2195 GLint rb_base_format
;
2196 struct gl_renderbuffer
*rb
;
2197 GLenum rb_internal_format
;
2200 if (!legal_texsubimage_target(ctx
, dimensions
, target
, false)) {
2201 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexImage%uD(target=%s)",
2202 dimensions
, _mesa_enum_to_string(target
));
2207 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2208 _mesa_error(ctx
, GL_INVALID_VALUE
,
2209 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
2213 /* Check that the source buffer is complete */
2214 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2215 if (ctx
->ReadBuffer
->_Status
== 0) {
2216 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2218 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2219 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2220 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
2224 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2225 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2226 "glCopyTexImage%dD(multisample FBO)", dimensions
);
2232 if (border
< 0 || border
> 1 ||
2233 ((ctx
->API
!= API_OPENGL_COMPAT
||
2234 target
== GL_TEXTURE_RECTANGLE_NV
||
2235 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
2236 _mesa_error(ctx
, GL_INVALID_VALUE
,
2237 "glCopyTexImage%dD(border=%d)", dimensions
, border
);
2241 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2244 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
2245 switch (internalFormat
) {
2250 case GL_LUMINANCE_ALPHA
:
2253 _mesa_error(ctx
, GL_INVALID_ENUM
,
2254 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2255 _mesa_enum_to_string(internalFormat
));
2260 * Section 8.6 (Alternate Texture Image Specification Commands) of the
2261 * OpenGL 4.5 (Compatibility Profile) spec says:
2263 * "Parameters level, internalformat, and border are specified using
2264 * the same values, with the same meanings, as the corresponding
2265 * arguments of TexImage2D, except that internalformat may not be
2266 * specified as 1, 2, 3, or 4."
2268 if (internalFormat
>= 1 && internalFormat
<= 4) {
2269 _mesa_error(ctx
, GL_INVALID_ENUM
,
2270 "glCopyTexImage%dD(internalFormat=%d)", dimensions
,
2276 baseFormat
= _mesa_base_tex_format(ctx
, internalFormat
);
2277 if (baseFormat
< 0) {
2278 _mesa_error(ctx
, GL_INVALID_ENUM
,
2279 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2280 _mesa_enum_to_string(internalFormat
));
2284 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, internalFormat
);
2286 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2287 "glCopyTexImage%dD(read buffer)", dimensions
);
2291 rb_internal_format
= rb
->InternalFormat
;
2292 rb_base_format
= _mesa_base_tex_format(ctx
, rb
->InternalFormat
);
2293 if (_mesa_is_color_format(internalFormat
)) {
2294 if (rb_base_format
< 0) {
2295 _mesa_error(ctx
, GL_INVALID_VALUE
,
2296 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2297 _mesa_enum_to_string(internalFormat
));
2302 if (_mesa_is_gles(ctx
)) {
2304 if (_mesa_base_format_component_count(baseFormat
) >
2305 _mesa_base_format_component_count(rb_base_format
)) {
2308 if (baseFormat
== GL_DEPTH_COMPONENT
||
2309 baseFormat
== GL_DEPTH_STENCIL
||
2310 baseFormat
== GL_STENCIL_INDEX
||
2311 rb_base_format
== GL_DEPTH_COMPONENT
||
2312 rb_base_format
== GL_DEPTH_STENCIL
||
2313 rb_base_format
== GL_STENCIL_INDEX
||
2314 ((baseFormat
== GL_LUMINANCE_ALPHA
||
2315 baseFormat
== GL_ALPHA
) &&
2316 rb_base_format
!= GL_RGBA
) ||
2317 internalFormat
== GL_RGB9_E5
) {
2320 if (internalFormat
== GL_RGB9_E5
) {
2324 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2325 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2326 _mesa_enum_to_string(internalFormat
));
2331 if (_mesa_is_gles3(ctx
)) {
2332 bool rb_is_srgb
= false;
2333 bool dst_is_srgb
= false;
2335 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&&
2336 _mesa_get_format_color_encoding(rb
->Format
) == GL_SRGB
) {
2340 if (_mesa_get_linear_internalformat(internalFormat
) != internalFormat
) {
2344 if (rb_is_srgb
!= dst_is_srgb
) {
2345 /* Page 137 (page 149 of the PDF) in section 3.8.5 of the
2346 * OpenGLES 3.0.0 spec says:
2348 * "The error INVALID_OPERATION is also generated if the
2349 * value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the
2350 * framebuffer attachment corresponding to the read buffer
2351 * is LINEAR (see section 6.1.13) and internalformat is
2352 * one of the sRGB formats described in section 3.8.16, or
2353 * if the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING is
2354 * SRGB and internalformat is not one of the sRGB formats."
2356 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2357 "glCopyTexImage%dD(srgb usage mismatch)", dimensions
);
2361 /* Page 139, Table 3.15 of OpenGL ES 3.0 spec does not define ReadPixels
2362 * types for SNORM formats. Also, conversion to SNORM formats is not
2363 * allowed by Table 3.2 on Page 110.
2365 if (_mesa_is_enum_format_snorm(internalFormat
)) {
2366 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2367 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2368 _mesa_enum_to_string(internalFormat
));
2373 if (!_mesa_source_buffer_exists(ctx
, baseFormat
)) {
2374 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2375 "glCopyTexImage%dD(missing readbuffer)", dimensions
);
2379 /* From the EXT_texture_integer spec:
2381 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2382 * if the texture internalformat is an integer format and the read color
2383 * buffer is not an integer format, or if the internalformat is not an
2384 * integer format and the read color buffer is an integer format."
2386 if (_mesa_is_color_format(internalFormat
)) {
2387 bool is_int
= _mesa_is_enum_format_integer(internalFormat
);
2388 bool is_rbint
= _mesa_is_enum_format_integer(rb_internal_format
);
2389 bool is_unorm
= _mesa_is_enum_format_unorm(internalFormat
);
2390 bool is_rbunorm
= _mesa_is_enum_format_unorm(rb_internal_format
);
2391 if (is_int
|| is_rbint
) {
2392 if (is_int
!= is_rbint
) {
2393 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2394 "glCopyTexImage%dD(integer vs non-integer)", dimensions
);
2396 } else if (_mesa_is_gles(ctx
) &&
2397 _mesa_is_enum_format_unsigned_int(internalFormat
) !=
2398 _mesa_is_enum_format_unsigned_int(rb_internal_format
)) {
2399 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2400 "glCopyTexImage%dD(signed vs unsigned integer)", dimensions
);
2405 /* From page 138 of OpenGL ES 3.0 spec:
2406 * "The error INVALID_OPERATION is generated if floating-point RGBA
2407 * data is required; if signed integer RGBA data is required and the
2408 * format of the current color buffer is not signed integer; if
2409 * unsigned integer RGBA data is required and the format of the
2410 * current color buffer is not unsigned integer; or if fixed-point
2411 * RGBA data is required and the format of the current color buffer
2412 * is not fixed-point.
2414 if (_mesa_is_gles(ctx
) && is_unorm
!= is_rbunorm
)
2415 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2416 "glCopyTexImage%dD(unorm vs non-unorm)", dimensions
);
2419 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
2421 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
, &err
)) {
2422 _mesa_error(ctx
, err
,
2423 "glCopyTexImage%dD(target can't be compressed)", dimensions
);
2426 if (_mesa_format_no_online_compression(ctx
, internalFormat
)) {
2427 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2428 "glCopyTexImage%dD(no compression for format)", dimensions
);
2432 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2433 "glCopyTexImage%dD(border!=0)", dimensions
);
2438 if (!mutable_tex_object(ctx
, target
)) {
2439 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2440 "glCopyTexImage%dD(immutable texture)", dimensions
);
2444 /* if we get here, the parameters are OK */
2450 * Test glCopyTexSubImage[12]D() parameters for errors.
2451 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2454 copytexsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2455 const struct gl_texture_object
*texObj
,
2456 GLenum target
, GLint level
,
2457 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2458 GLint width
, GLint height
, const char *caller
)
2460 struct gl_texture_image
*texImage
;
2462 /* Check that the source buffer is complete */
2463 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2464 if (ctx
->ReadBuffer
->_Status
== 0) {
2465 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2467 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2468 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2469 "%s(invalid readbuffer)", caller
);
2473 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2474 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2475 "%s(multisample FBO)", caller
);
2481 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2482 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(level=%d)", caller
, level
);
2486 /* Get dest image pointers */
2488 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", caller
);
2492 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
2494 /* destination image does not exist */
2495 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2496 "%s(invalid texture image)", caller
);
2500 if (error_check_subtexture_dimensions(ctx
, dimensions
, texImage
,
2501 xoffset
, yoffset
, zoffset
,
2502 width
, height
, 1, caller
)) {
2506 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2507 if (_mesa_format_no_online_compression(ctx
, texImage
->InternalFormat
)) {
2508 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2509 "%s(no compression for format)", caller
);
2514 if (texImage
->InternalFormat
== GL_YCBCR_MESA
) {
2515 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s()", caller
);
2519 if (!_mesa_source_buffer_exists(ctx
, texImage
->_BaseFormat
)) {
2520 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2521 "%s(missing readbuffer, format=%s)", caller
,
2522 _mesa_enum_to_string(texImage
->_BaseFormat
));
2526 /* From the EXT_texture_integer spec:
2528 * "INVALID_OPERATION is generated by CopyTexImage* and
2529 * CopyTexSubImage* if the texture internalformat is an integer format
2530 * and the read color buffer is not an integer format, or if the
2531 * internalformat is not an integer format and the read color buffer
2532 * is an integer format."
2534 if (_mesa_is_color_format(texImage
->InternalFormat
)) {
2535 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
2537 if (_mesa_is_format_integer_color(rb
->Format
) !=
2538 _mesa_is_format_integer_color(texImage
->TexFormat
)) {
2539 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2540 "%s(integer vs non-integer)", caller
);
2545 /* In the ES 3.2 specification's Table 8.13 (Valid CopyTexImage source
2546 * framebuffer/destination texture base internal format combinations),
2547 * all the entries for stencil are left blank (unsupported).
2549 if (_mesa_is_gles(ctx
) && _mesa_is_stencil_format(texImage
->_BaseFormat
)) {
2550 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(stencil disallowed)", caller
);
2554 /* if we get here, the parameters are OK */
2559 /** Callback info for walking over FBO hash table */
2562 struct gl_context
*ctx
;
2563 struct gl_texture_object
*texObj
;
2569 * Check render to texture callback. Called from _mesa_HashWalk().
2572 check_rtt_cb(GLuint key
, void *data
, void *userData
)
2574 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
2575 const struct cb_info
*info
= (struct cb_info
*) userData
;
2576 struct gl_context
*ctx
= info
->ctx
;
2577 const struct gl_texture_object
*texObj
= info
->texObj
;
2578 const GLuint level
= info
->level
, face
= info
->face
;
2580 /* If this is a user-created FBO */
2581 if (_mesa_is_user_fbo(fb
)) {
2583 /* check if any of the FBO's attachments point to 'texObj' */
2584 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2585 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2586 if (att
->Type
== GL_TEXTURE
&&
2587 att
->Texture
== texObj
&&
2588 att
->TextureLevel
== level
&&
2589 att
->CubeMapFace
== face
) {
2590 _mesa_update_texture_renderbuffer(ctx
, ctx
->DrawBuffer
, att
);
2591 assert(att
->Renderbuffer
->TexImage
);
2592 /* Mark fb status as indeterminate to force re-validation */
2601 * When a texture image is specified we have to check if it's bound to
2602 * any framebuffer objects (render to texture) in order to detect changes
2603 * in size or format since that effects FBO completeness.
2604 * Any FBOs rendering into the texture must be re-validated.
2607 _mesa_update_fbo_texture(struct gl_context
*ctx
,
2608 struct gl_texture_object
*texObj
,
2609 GLuint face
, GLuint level
)
2611 /* Only check this texture if it's been marked as RenderToTexture */
2612 if (texObj
->_RenderToTexture
) {
2613 struct cb_info info
;
2615 info
.texObj
= texObj
;
2618 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, check_rtt_cb
, &info
);
2624 * If the texture object's GenerateMipmap flag is set and we've
2625 * changed the texture base level image, regenerate the rest of the
2626 * mipmap levels now.
2629 check_gen_mipmap(struct gl_context
*ctx
, GLenum target
,
2630 struct gl_texture_object
*texObj
, GLint level
)
2632 if (texObj
->GenerateMipmap
&&
2633 level
== texObj
->BaseLevel
&&
2634 level
< texObj
->MaxLevel
) {
2635 assert(ctx
->Driver
.GenerateMipmap
);
2636 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2641 /** Debug helper: override the user-requested internal format */
2643 override_internal_format(GLenum internalFormat
, GLint width
, GLint height
)
2646 if (internalFormat
== GL_RGBA16F_ARB
||
2647 internalFormat
== GL_RGBA32F_ARB
) {
2648 printf("Convert rgba float tex to int %d x %d\n", width
, height
);
2651 else if (internalFormat
== GL_RGB16F_ARB
||
2652 internalFormat
== GL_RGB32F_ARB
) {
2653 printf("Convert rgb float tex to int %d x %d\n", width
, height
);
2656 else if (internalFormat
== GL_LUMINANCE_ALPHA16F_ARB
||
2657 internalFormat
== GL_LUMINANCE_ALPHA32F_ARB
) {
2658 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2659 return GL_LUMINANCE_ALPHA
;
2661 else if (internalFormat
== GL_LUMINANCE16F_ARB
||
2662 internalFormat
== GL_LUMINANCE32F_ARB
) {
2663 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2664 return GL_LUMINANCE
;
2666 else if (internalFormat
== GL_ALPHA16F_ARB
||
2667 internalFormat
== GL_ALPHA32F_ARB
) {
2668 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2672 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2673 internalFormat = GL_RGBA;
2677 return internalFormat
;
2680 return internalFormat
;
2686 * Choose the actual hardware format for a texture image.
2687 * Try to use the same format as the previous image level when possible.
2688 * Otherwise, ask the driver for the best format.
2689 * It's important to try to choose a consistant format for all levels
2690 * for efficient texture memory layout/allocation. In particular, this
2691 * comes up during automatic mipmap generation.
2694 _mesa_choose_texture_format(struct gl_context
*ctx
,
2695 struct gl_texture_object
*texObj
,
2696 GLenum target
, GLint level
,
2697 GLenum internalFormat
, GLenum format
, GLenum type
)
2701 /* see if we've already chosen a format for the previous level */
2703 struct gl_texture_image
*prevImage
=
2704 _mesa_select_tex_image(texObj
, target
, level
- 1);
2705 /* See if the prev level is defined and has an internal format which
2706 * matches the new internal format.
2709 prevImage
->Width
> 0 &&
2710 prevImage
->InternalFormat
== internalFormat
) {
2711 /* use the same format */
2712 assert(prevImage
->TexFormat
!= MESA_FORMAT_NONE
);
2713 return prevImage
->TexFormat
;
2717 /* If the application requested compression to an S3TC format but we don't
2718 * have the DXTn library, force a generic compressed format instead.
2720 if (internalFormat
!= format
&& format
!= GL_NONE
) {
2721 const GLenum before
= internalFormat
;
2723 switch (internalFormat
) {
2724 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
2725 if (!ctx
->Mesa_DXTn
)
2726 internalFormat
= GL_COMPRESSED_RGB
;
2728 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
2729 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
2730 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
2731 if (!ctx
->Mesa_DXTn
)
2732 internalFormat
= GL_COMPRESSED_RGBA
;
2738 if (before
!= internalFormat
) {
2740 "DXT compression requested (%s), "
2741 "but libtxc_dxtn library not installed. Using %s "
2743 _mesa_enum_to_string(before
),
2744 _mesa_enum_to_string(internalFormat
));
2748 /* choose format from scratch */
2749 f
= ctx
->Driver
.ChooseTextureFormat(ctx
, target
, internalFormat
,
2751 assert(f
!= MESA_FORMAT_NONE
);
2757 * Adjust pixel unpack params and image dimensions to strip off the
2758 * one-pixel texture border.
2760 * Gallium and intel don't support texture borders. They've seldem been used
2761 * and seldom been implemented correctly anyway.
2763 * \param unpackNew returns the new pixel unpack parameters
2766 strip_texture_border(GLenum target
,
2767 GLint
*width
, GLint
*height
, GLint
*depth
,
2768 const struct gl_pixelstore_attrib
*unpack
,
2769 struct gl_pixelstore_attrib
*unpackNew
)
2775 *unpackNew
= *unpack
;
2777 if (unpackNew
->RowLength
== 0)
2778 unpackNew
->RowLength
= *width
;
2780 if (unpackNew
->ImageHeight
== 0)
2781 unpackNew
->ImageHeight
= *height
;
2783 assert(*width
>= 3);
2784 unpackNew
->SkipPixels
++; /* skip the border */
2785 *width
= *width
- 2; /* reduce the width by two border pixels */
2787 /* The min height of a texture with a border is 3 */
2788 if (*height
>= 3 && target
!= GL_TEXTURE_1D_ARRAY
) {
2789 unpackNew
->SkipRows
++; /* skip the border */
2790 *height
= *height
- 2; /* reduce the height by two border pixels */
2794 target
!= GL_TEXTURE_2D_ARRAY
&&
2795 target
!= GL_TEXTURE_CUBE_MAP_ARRAY
) {
2796 unpackNew
->SkipImages
++; /* skip the border */
2797 *depth
= *depth
- 2; /* reduce the depth by two border pixels */
2803 * Common code to implement all the glTexImage1D/2D/3D functions
2804 * as well as glCompressedTexImage1D/2D/3D.
2805 * \param compressed only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
2806 * \param format the user's image format (only used if !compressed)
2807 * \param type the user's image type (only used if !compressed)
2808 * \param imageSize only used for glCompressedTexImage1D/2D/3D calls.
2811 teximage(struct gl_context
*ctx
, GLboolean compressed
, GLuint dims
,
2812 GLenum target
, GLint level
, GLint internalFormat
,
2813 GLsizei width
, GLsizei height
, GLsizei depth
,
2814 GLint border
, GLenum format
, GLenum type
,
2815 GLsizei imageSize
, const GLvoid
*pixels
)
2817 const char *func
= compressed
? "glCompressedTexImage" : "glTexImage";
2818 struct gl_pixelstore_attrib unpack_no_border
;
2819 const struct gl_pixelstore_attrib
*unpack
= &ctx
->Unpack
;
2820 struct gl_texture_object
*texObj
;
2821 mesa_format texFormat
;
2822 GLboolean dimensionsOK
, sizeOK
;
2824 FLUSH_VERTICES(ctx
, 0);
2826 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
)) {
2829 "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
2831 _mesa_enum_to_string(target
), level
,
2832 _mesa_enum_to_string(internalFormat
),
2833 width
, height
, depth
, border
, pixels
);
2836 "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2838 _mesa_enum_to_string(target
), level
,
2839 _mesa_enum_to_string(internalFormat
),
2840 width
, height
, depth
, border
,
2841 _mesa_enum_to_string(format
),
2842 _mesa_enum_to_string(type
), pixels
);
2845 internalFormat
= override_internal_format(internalFormat
, width
, height
);
2847 /* target error checking */
2848 if (!legal_teximage_target(ctx
, dims
, target
)) {
2849 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s%uD(target=%s)",
2850 func
, dims
, _mesa_enum_to_string(target
));
2854 /* general error checking */
2856 if (compressed_texture_error_check(ctx
, dims
, target
, level
,
2858 width
, height
, depth
,
2859 border
, imageSize
, pixels
))
2863 if (texture_error_check(ctx
, dims
, target
, level
, internalFormat
,
2864 format
, type
, width
, height
, depth
, border
,
2869 /* Here we convert a cpal compressed image into a regular glTexImage2D
2870 * call by decompressing the texture. If we really want to support cpal
2871 * textures in any driver this would have to be changed.
2873 if (ctx
->API
== API_OPENGLES
&& compressed
&& dims
== 2) {
2874 switch (internalFormat
) {
2875 case GL_PALETTE4_RGB8_OES
:
2876 case GL_PALETTE4_RGBA8_OES
:
2877 case GL_PALETTE4_R5_G6_B5_OES
:
2878 case GL_PALETTE4_RGBA4_OES
:
2879 case GL_PALETTE4_RGB5_A1_OES
:
2880 case GL_PALETTE8_RGB8_OES
:
2881 case GL_PALETTE8_RGBA8_OES
:
2882 case GL_PALETTE8_R5_G6_B5_OES
:
2883 case GL_PALETTE8_RGBA4_OES
:
2884 case GL_PALETTE8_RGB5_A1_OES
:
2885 _mesa_cpal_compressed_teximage2d(target
, level
, internalFormat
,
2886 width
, height
, imageSize
, pixels
);
2891 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2895 /* For glCompressedTexImage() the driver has no choice about the
2896 * texture format since we'll never transcode the user's compressed
2897 * image data. The internalFormat was error checked earlier.
2899 texFormat
= _mesa_glenum_to_compressed_format(internalFormat
);
2902 /* In case of HALF_FLOAT_OES or FLOAT_OES, find corresponding sized
2903 * internal floating point format for the given base format.
2905 if (_mesa_is_gles(ctx
) && format
== internalFormat
) {
2906 if (type
== GL_FLOAT
) {
2907 texObj
->_IsFloat
= GL_TRUE
;
2908 } else if (type
== GL_HALF_FLOAT_OES
|| type
== GL_HALF_FLOAT
) {
2909 texObj
->_IsHalfFloat
= GL_TRUE
;
2912 internalFormat
= adjust_for_oes_float_texture(ctx
, format
, type
);
2915 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
2916 internalFormat
, format
, type
);
2919 assert(texFormat
!= MESA_FORMAT_NONE
);
2921 /* check that width, height, depth are legal for the mipmap level */
2922 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, level
, width
,
2923 height
, depth
, border
);
2925 /* check that the texture won't take too much memory, etc */
2926 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target(target
),
2928 width
, height
, depth
, border
);
2930 if (_mesa_is_proxy_texture(target
)) {
2931 /* Proxy texture: just clear or set state depending on error checking */
2932 struct gl_texture_image
*texImage
=
2933 get_proxy_tex_image(ctx
, target
, level
);
2936 return; /* GL_OUT_OF_MEMORY already recorded */
2938 if (dimensionsOK
&& sizeOK
) {
2939 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, depth
,
2940 border
, internalFormat
, texFormat
);
2943 clear_teximage_fields(texImage
);
2947 /* non-proxy target */
2948 const GLuint face
= _mesa_tex_target_to_face(target
);
2949 struct gl_texture_image
*texImage
;
2951 if (!dimensionsOK
) {
2952 _mesa_error(ctx
, GL_INVALID_VALUE
,
2953 "%s%uD(invalid width or height or depth)",
2959 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
2960 "%s%uD(image too large: %d x %d x %d, %s format)",
2961 func
, dims
, width
, height
, depth
,
2962 _mesa_enum_to_string(internalFormat
));
2966 /* Allow a hardware driver to just strip out the border, to provide
2967 * reliable but slightly incorrect hardware rendering instead of
2968 * rarely-tested software fallback rendering.
2970 if (border
&& ctx
->Const
.StripTextureBorder
) {
2971 strip_texture_border(target
, &width
, &height
, &depth
, unpack
,
2974 unpack
= &unpack_no_border
;
2977 if (ctx
->NewState
& _NEW_PIXEL
)
2978 _mesa_update_state(ctx
);
2980 _mesa_lock_texture(ctx
, texObj
);
2982 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
2985 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s%uD", func
, dims
);
2988 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
2990 _mesa_init_teximage_fields(ctx
, texImage
,
2991 width
, height
, depth
,
2992 border
, internalFormat
, texFormat
);
2994 /* Give the texture to the driver. <pixels> may be null. */
2995 if (width
> 0 && height
> 0 && depth
> 0) {
2997 ctx
->Driver
.CompressedTexImage(ctx
, dims
, texImage
,
3001 ctx
->Driver
.TexImage(ctx
, dims
, texImage
, format
,
3002 type
, pixels
, unpack
);
3006 check_gen_mipmap(ctx
, target
, texObj
, level
);
3008 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3010 _mesa_dirty_texobj(ctx
, texObj
);
3013 _mesa_unlock_texture(ctx
, texObj
);
3020 * Called from the API. Note that width includes the border.
3023 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
3024 GLsizei width
, GLint border
, GLenum format
,
3025 GLenum type
, const GLvoid
*pixels
)
3027 GET_CURRENT_CONTEXT(ctx
);
3028 teximage(ctx
, GL_FALSE
, 1, target
, level
, internalFormat
, width
, 1, 1,
3029 border
, format
, type
, 0, pixels
);
3034 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
3035 GLsizei width
, GLsizei height
, GLint border
,
3036 GLenum format
, GLenum type
,
3037 const GLvoid
*pixels
)
3039 GET_CURRENT_CONTEXT(ctx
);
3040 teximage(ctx
, GL_FALSE
, 2, target
, level
, internalFormat
, width
, height
, 1,
3041 border
, format
, type
, 0, pixels
);
3046 * Called by the API or display list executor.
3047 * Note that width and height include the border.
3050 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
3051 GLsizei width
, GLsizei height
, GLsizei depth
,
3052 GLint border
, GLenum format
, GLenum type
,
3053 const GLvoid
*pixels
)
3055 GET_CURRENT_CONTEXT(ctx
);
3056 teximage(ctx
, GL_FALSE
, 3, target
, level
, internalFormat
,
3057 width
, height
, depth
,
3058 border
, format
, type
, 0, pixels
);
3063 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
3064 GLsizei width
, GLsizei height
, GLsizei depth
,
3065 GLint border
, GLenum format
, GLenum type
,
3066 const GLvoid
*pixels
)
3068 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
3069 depth
, border
, format
, type
, pixels
);
3074 _mesa_EGLImageTargetTexture2DOES (GLenum target
, GLeglImageOES image
)
3076 struct gl_texture_object
*texObj
;
3077 struct gl_texture_image
*texImage
;
3079 GET_CURRENT_CONTEXT(ctx
);
3080 FLUSH_VERTICES(ctx
, 0);
3084 valid_target
= ctx
->Extensions
.OES_EGL_image
;
3086 case GL_TEXTURE_EXTERNAL_OES
:
3088 _mesa_is_gles(ctx
) ? ctx
->Extensions
.OES_EGL_image_external
: false;
3091 valid_target
= false;
3095 if (!valid_target
) {
3096 _mesa_error(ctx
, GL_INVALID_ENUM
,
3097 "glEGLImageTargetTexture2D(target=%d)", target
);
3102 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3103 "glEGLImageTargetTexture2D(image=%p)", image
);
3107 if (ctx
->NewState
& _NEW_PIXEL
)
3108 _mesa_update_state(ctx
);
3110 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3114 _mesa_lock_texture(ctx
, texObj
);
3116 if (texObj
->Immutable
) {
3117 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3118 "glEGLImageTargetTexture2D(texture is immutable)");
3119 _mesa_unlock_texture(ctx
, texObj
);
3123 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
3125 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glEGLImageTargetTexture2D");
3127 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3129 ctx
->Driver
.EGLImageTargetTexture2D(ctx
, target
,
3130 texObj
, texImage
, image
);
3132 _mesa_dirty_texobj(ctx
, texObj
);
3134 _mesa_unlock_texture(ctx
, texObj
);
3139 * Helper that implements the glTexSubImage1/2/3D()
3140 * and glTextureSubImage1/2/3D() functions.
3143 _mesa_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
3144 struct gl_texture_object
*texObj
,
3145 struct gl_texture_image
*texImage
,
3146 GLenum target
, GLint level
,
3147 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3148 GLsizei width
, GLsizei height
, GLsizei depth
,
3149 GLenum format
, GLenum type
, const GLvoid
*pixels
,
3152 FLUSH_VERTICES(ctx
, 0);
3154 if (ctx
->NewState
& _NEW_PIXEL
)
3155 _mesa_update_state(ctx
);
3157 _mesa_lock_texture(ctx
, texObj
);
3159 if (width
> 0 && height
> 0 && depth
> 0) {
3160 /* If we have a border, offset=-1 is legal. Bias by border width. */
3163 if (target
!= GL_TEXTURE_2D_ARRAY
)
3164 zoffset
+= texImage
->Border
;
3167 if (target
!= GL_TEXTURE_1D_ARRAY
)
3168 yoffset
+= texImage
->Border
;
3171 xoffset
+= texImage
->Border
;
3174 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3175 xoffset
, yoffset
, zoffset
,
3176 width
, height
, depth
,
3177 format
, type
, pixels
, &ctx
->Unpack
);
3179 check_gen_mipmap(ctx
, target
, texObj
, level
);
3181 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3182 * the texel data, not the texture format, size, etc.
3186 _mesa_unlock_texture(ctx
, texObj
);
3190 * Implement all the glTexSubImage1/2/3D() functions.
3191 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3194 texsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
3195 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3196 GLsizei width
, GLsizei height
, GLsizei depth
,
3197 GLenum format
, GLenum type
, const GLvoid
*pixels
,
3198 const char *callerName
)
3200 struct gl_texture_object
*texObj
;
3201 struct gl_texture_image
*texImage
;
3203 /* check target (proxies not allowed) */
3204 if (!legal_texsubimage_target(ctx
, dims
, target
, false)) {
3205 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage%uD(target=%s)",
3206 dims
, _mesa_enum_to_string(target
));
3210 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3214 if (texsubimage_error_check(ctx
, dims
, texObj
, target
, level
,
3215 xoffset
, yoffset
, zoffset
,
3216 width
, height
, depth
, format
, type
,
3217 pixels
, false, callerName
)) {
3218 return; /* error was detected */
3221 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
3222 /* texsubimage_error_check ensures that texImage is not NULL */
3224 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3225 _mesa_debug(ctx
, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3227 _mesa_enum_to_string(target
), level
,
3228 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3229 _mesa_enum_to_string(format
),
3230 _mesa_enum_to_string(type
), pixels
);
3232 _mesa_texture_sub_image(ctx
, dims
, texObj
, texImage
, target
, level
,
3233 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3234 format
, type
, pixels
, false);
3239 * Implement all the glTextureSubImage1/2/3D() functions.
3240 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3243 texturesubimage(struct gl_context
*ctx
, GLuint dims
,
3244 GLuint texture
, GLint level
,
3245 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3246 GLsizei width
, GLsizei height
, GLsizei depth
,
3247 GLenum format
, GLenum type
, const GLvoid
*pixels
,
3248 const char *callerName
)
3250 struct gl_texture_object
*texObj
;
3251 struct gl_texture_image
*texImage
;
3254 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3256 "glTextureSubImage%uD %d %d %d %d %d %d %d %d %s %s %p\n",
3257 dims
, texture
, level
,
3258 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3259 _mesa_enum_to_string(format
),
3260 _mesa_enum_to_string(type
), pixels
);
3262 /* Get the texture object by Name. */
3263 texObj
= _mesa_lookup_texture(ctx
, texture
);
3265 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTextureSubImage%uD(texture)",
3270 /* check target (proxies not allowed) */
3271 if (!legal_texsubimage_target(ctx
, dims
, texObj
->Target
, true)) {
3272 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target=%s)",
3273 callerName
, _mesa_enum_to_string(texObj
->Target
));
3277 if (texsubimage_error_check(ctx
, dims
, texObj
, texObj
->Target
, level
,
3278 xoffset
, yoffset
, zoffset
,
3279 width
, height
, depth
, format
, type
,
3280 pixels
, true, callerName
)) {
3281 return; /* error was detected */
3285 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
3286 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
3290 * What do we do if the user created a texture with the following code
3291 * and then called this function with its handle?
3294 * glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &tex);
3295 * glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
3296 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, ...);
3297 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, ...);
3298 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, ...);
3299 * // Note: GL_TEXTURE_CUBE_MAP_NEGATIVE_Y not set, or given the
3300 * // wrong format, or given the wrong size, etc.
3301 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, ...);
3302 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, ...);
3304 * A bug has been filed against the spec for this case. In the
3305 * meantime, we will check for cube completeness.
3307 * According to Section 8.17 Texture Completeness in the OpenGL 4.5
3308 * Core Profile spec (30.10.2014):
3309 * "[A] cube map texture is cube complete if the
3310 * following conditions all hold true: The [base level] texture
3311 * images of each of the six cube map faces have identical, positive,
3312 * and square dimensions. The [base level] images were each specified
3313 * with the same internal format."
3315 * It seems reasonable to check for cube completeness of an arbitrary
3316 * level here so that the image data has a consistent format and size.
3318 if (!_mesa_cube_level_complete(texObj
, level
)) {
3319 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3320 "glTextureSubImage%uD(cube map incomplete)",
3325 imageStride
= _mesa_image_image_stride(&ctx
->Unpack
, width
, height
,
3327 /* Copy in each face. */
3328 for (i
= zoffset
; i
< zoffset
+ depth
; ++i
) {
3329 texImage
= texObj
->Image
[i
][level
];
3332 _mesa_texture_sub_image(ctx
, 3, texObj
, texImage
, texObj
->Target
,
3333 level
, xoffset
, yoffset
, 0,
3334 width
, height
, 1, format
,
3335 type
, pixels
, true);
3336 pixels
= (GLubyte
*) pixels
+ imageStride
;
3340 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
3343 _mesa_texture_sub_image(ctx
, dims
, texObj
, texImage
, texObj
->Target
,
3344 level
, xoffset
, yoffset
, zoffset
,
3345 width
, height
, depth
, format
,
3346 type
, pixels
, true);
3352 _mesa_TexSubImage1D( GLenum target
, GLint level
,
3353 GLint xoffset
, GLsizei width
,
3354 GLenum format
, GLenum type
,
3355 const GLvoid
*pixels
)
3357 GET_CURRENT_CONTEXT(ctx
);
3358 texsubimage(ctx
, 1, target
, level
,
3361 format
, type
, pixels
, "glTexSubImage1D");
3366 _mesa_TexSubImage2D( GLenum target
, GLint level
,
3367 GLint xoffset
, GLint yoffset
,
3368 GLsizei width
, GLsizei height
,
3369 GLenum format
, GLenum type
,
3370 const GLvoid
*pixels
)
3372 GET_CURRENT_CONTEXT(ctx
);
3373 texsubimage(ctx
, 2, target
, level
,
3374 xoffset
, yoffset
, 0,
3376 format
, type
, pixels
, "glTexSubImage2D");
3382 _mesa_TexSubImage3D( GLenum target
, GLint level
,
3383 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3384 GLsizei width
, GLsizei height
, GLsizei depth
,
3385 GLenum format
, GLenum type
,
3386 const GLvoid
*pixels
)
3388 GET_CURRENT_CONTEXT(ctx
);
3389 texsubimage(ctx
, 3, target
, level
,
3390 xoffset
, yoffset
, zoffset
,
3391 width
, height
, depth
,
3392 format
, type
, pixels
, "glTexSubImage3D");
3396 _mesa_TextureSubImage1D(GLuint texture
, GLint level
,
3397 GLint xoffset
, GLsizei width
,
3398 GLenum format
, GLenum type
,
3399 const GLvoid
*pixels
)
3401 GET_CURRENT_CONTEXT(ctx
);
3402 texturesubimage(ctx
, 1, texture
, level
,
3405 format
, type
, pixels
, "glTextureSubImage1D");
3410 _mesa_TextureSubImage2D(GLuint texture
, GLint level
,
3411 GLint xoffset
, GLint yoffset
,
3412 GLsizei width
, GLsizei height
,
3413 GLenum format
, GLenum type
,
3414 const GLvoid
*pixels
)
3416 GET_CURRENT_CONTEXT(ctx
);
3417 texturesubimage(ctx
, 2, texture
, level
,
3418 xoffset
, yoffset
, 0,
3420 format
, type
, pixels
, "glTextureSubImage2D");
3425 _mesa_TextureSubImage3D(GLuint texture
, GLint level
,
3426 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3427 GLsizei width
, GLsizei height
, GLsizei depth
,
3428 GLenum format
, GLenum type
,
3429 const GLvoid
*pixels
)
3431 GET_CURRENT_CONTEXT(ctx
);
3432 texturesubimage(ctx
, 3, texture
, level
,
3433 xoffset
, yoffset
, zoffset
,
3434 width
, height
, depth
,
3435 format
, type
, pixels
, "glTextureSubImage3D");
3440 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3441 * from. This depends on whether the texture contains color or depth values.
3443 static struct gl_renderbuffer
*
3444 get_copy_tex_image_source(struct gl_context
*ctx
, mesa_format texFormat
)
3446 if (_mesa_get_format_bits(texFormat
, GL_DEPTH_BITS
) > 0) {
3447 /* reading from depth/stencil buffer */
3448 return ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
3449 } else if (_mesa_get_format_bits(texFormat
, GL_STENCIL_BITS
) > 0) {
3450 return ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
3452 /* copying from color buffer */
3453 return ctx
->ReadBuffer
->_ColorReadBuffer
;
3458 copytexsubimage_by_slice(struct gl_context
*ctx
,
3459 struct gl_texture_image
*texImage
,
3461 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3462 struct gl_renderbuffer
*rb
,
3464 GLsizei width
, GLsizei height
)
3466 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
3469 /* For 1D arrays, we copy each scanline of the source rectangle into the
3472 assert(zoffset
== 0);
3474 for (slice
= 0; slice
< height
; slice
++) {
3475 assert(yoffset
+ slice
< texImage
->Height
);
3476 ctx
->Driver
.CopyTexSubImage(ctx
, 2, texImage
,
3477 xoffset
, 0, yoffset
+ slice
,
3478 rb
, x
, y
+ slice
, width
, 1);
3481 ctx
->Driver
.CopyTexSubImage(ctx
, dims
, texImage
,
3482 xoffset
, yoffset
, zoffset
,
3483 rb
, x
, y
, width
, height
);
3488 formats_differ_in_component_sizes(mesa_format f1
, mesa_format f2
)
3490 GLint f1_r_bits
= _mesa_get_format_bits(f1
, GL_RED_BITS
);
3491 GLint f1_g_bits
= _mesa_get_format_bits(f1
, GL_GREEN_BITS
);
3492 GLint f1_b_bits
= _mesa_get_format_bits(f1
, GL_BLUE_BITS
);
3493 GLint f1_a_bits
= _mesa_get_format_bits(f1
, GL_ALPHA_BITS
);
3495 GLint f2_r_bits
= _mesa_get_format_bits(f2
, GL_RED_BITS
);
3496 GLint f2_g_bits
= _mesa_get_format_bits(f2
, GL_GREEN_BITS
);
3497 GLint f2_b_bits
= _mesa_get_format_bits(f2
, GL_BLUE_BITS
);
3498 GLint f2_a_bits
= _mesa_get_format_bits(f2
, GL_ALPHA_BITS
);
3500 if ((f1_r_bits
&& f2_r_bits
&& f1_r_bits
!= f2_r_bits
)
3501 || (f1_g_bits
&& f2_g_bits
&& f1_g_bits
!= f2_g_bits
)
3502 || (f1_b_bits
&& f2_b_bits
&& f1_b_bits
!= f2_b_bits
)
3503 || (f1_a_bits
&& f2_a_bits
&& f1_a_bits
!= f2_a_bits
))
3510 can_avoid_reallocation(struct gl_texture_image
*texImage
, GLenum internalFormat
,
3511 mesa_format texFormat
, GLint x
, GLint y
, GLsizei width
,
3512 GLsizei height
, GLint border
)
3514 if (texImage
->InternalFormat
!= internalFormat
)
3516 if (texImage
->TexFormat
!= texFormat
)
3518 if (texImage
->Border
!= border
)
3520 if (texImage
->Width2
!= width
)
3522 if (texImage
->Height2
!= height
)
3528 * Implement the glCopyTexImage1/2D() functions.
3531 copyteximage(struct gl_context
*ctx
, GLuint dims
,
3532 GLenum target
, GLint level
, GLenum internalFormat
,
3533 GLint x
, GLint y
, GLsizei width
, GLsizei height
, GLint border
)
3535 struct gl_texture_object
*texObj
;
3536 struct gl_texture_image
*texImage
;
3537 const GLuint face
= _mesa_tex_target_to_face(target
);
3538 mesa_format texFormat
;
3539 struct gl_renderbuffer
*rb
;
3541 FLUSH_VERTICES(ctx
, 0);
3543 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3544 _mesa_debug(ctx
, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3546 _mesa_enum_to_string(target
), level
,
3547 _mesa_enum_to_string(internalFormat
),
3548 x
, y
, width
, height
, border
);
3550 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3551 _mesa_update_state(ctx
);
3553 if (copytexture_error_check(ctx
, dims
, target
, level
, internalFormat
,
3554 width
, height
, border
))
3557 if (!_mesa_legal_texture_dimensions(ctx
, target
, level
, width
, height
,
3559 _mesa_error(ctx
, GL_INVALID_VALUE
,
3560 "glCopyTexImage%uD(invalid width or height)", dims
);
3564 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3567 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3568 internalFormat
, GL_NONE
, GL_NONE
);
3570 /* First check if reallocating the texture buffer can be avoided.
3571 * Without the realloc the copy can be 20x faster.
3573 _mesa_lock_texture(ctx
, texObj
);
3575 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
3576 if (texImage
&& can_avoid_reallocation(texImage
, internalFormat
, texFormat
,
3577 x
, y
, width
, height
, border
)) {
3578 _mesa_unlock_texture(ctx
, texObj
);
3579 return _mesa_copy_texture_sub_image(ctx
, dims
, texObj
, target
, level
,
3580 0, 0, 0, x
, y
, width
, height
,
3584 _mesa_unlock_texture(ctx
, texObj
);
3585 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_LOW
, "glCopyTexImage "
3586 "can't avoid reallocating texture storage\n");
3588 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, internalFormat
);
3590 if (_mesa_is_gles3(ctx
)) {
3591 if (_mesa_is_enum_format_unsized(internalFormat
)) {
3592 /* Conversion from GL_RGB10_A2 source buffer format is not allowed in
3593 * OpenGL ES 3.0. Khronos bug# 9807.
3595 if (rb
->InternalFormat
== GL_RGB10_A2
) {
3596 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3597 "glCopyTexImage%uD(Reading from GL_RGB10_A2 buffer"
3598 " and writing to unsized internal format)", dims
);
3602 /* From Page 139 of OpenGL ES 3.0 spec:
3603 * "If internalformat is sized, the internal format of the new texel
3604 * array is internalformat, and this is also the new texel array’s
3605 * effective internal format. If the component sizes of internalformat
3606 * do not exactly match the corresponding component sizes of the source
3607 * buffer’s effective internal format, described below, an
3608 * INVALID_OPERATION error is generated. If internalformat is unsized,
3609 * the internal format of the new texel array is the effective internal
3610 * format of the source buffer, and this is also the new texel array’s
3611 * effective internal format.
3613 else if (formats_differ_in_component_sizes (texFormat
, rb
->Format
)) {
3614 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3615 "glCopyTexImage%uD(componenet size changed in"
3616 " internal format)", dims
);
3621 assert(texFormat
!= MESA_FORMAT_NONE
);
3623 if (!ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target(target
),
3625 width
, height
, 1, border
)) {
3626 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3627 "glCopyTexImage%uD(image too large)", dims
);
3631 if (border
&& ctx
->Const
.StripTextureBorder
) {
3633 width
-= border
* 2;
3636 height
-= border
* 2;
3641 _mesa_lock_texture(ctx
, texObj
);
3643 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3646 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
3649 GLint srcX
= x
, srcY
= y
, dstX
= 0, dstY
= 0, dstZ
= 0;
3651 /* Free old texture image */
3652 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3654 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, 1,
3655 border
, internalFormat
, texFormat
);
3657 if (width
&& height
) {
3658 /* Allocate texture memory (no pixel data yet) */
3659 ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
);
3661 if (_mesa_clip_copytexsubimage(ctx
, &dstX
, &dstY
, &srcX
, &srcY
,
3663 struct gl_renderbuffer
*srcRb
=
3664 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3666 copytexsubimage_by_slice(ctx
, texImage
, dims
,
3668 srcRb
, srcX
, srcY
, width
, height
);
3671 check_gen_mipmap(ctx
, target
, texObj
, level
);
3674 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3676 _mesa_dirty_texobj(ctx
, texObj
);
3679 _mesa_unlock_texture(ctx
, texObj
);
3685 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
3686 GLenum internalFormat
,
3688 GLsizei width
, GLint border
)
3690 GET_CURRENT_CONTEXT(ctx
);
3691 copyteximage(ctx
, 1, target
, level
, internalFormat
, x
, y
, width
, 1, border
);
3697 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
3698 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
3701 GET_CURRENT_CONTEXT(ctx
);
3702 copyteximage(ctx
, 2, target
, level
, internalFormat
,
3703 x
, y
, width
, height
, border
);
3707 * Implementation for glCopyTex(ture)SubImage1/2/3D() functions.
3710 _mesa_copy_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
3711 struct gl_texture_object
*texObj
,
3712 GLenum target
, GLint level
,
3713 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3715 GLsizei width
, GLsizei height
,
3718 struct gl_texture_image
*texImage
;
3720 FLUSH_VERTICES(ctx
, 0);
3722 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3723 _mesa_debug(ctx
, "%s %s %d %d %d %d %d %d %d %d\n", caller
,
3724 _mesa_enum_to_string(target
),
3725 level
, xoffset
, yoffset
, zoffset
, x
, y
, width
, height
);
3727 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3728 _mesa_update_state(ctx
);
3730 if (copytexsubimage_error_check(ctx
, dims
, texObj
, target
, level
,
3731 xoffset
, yoffset
, zoffset
,
3732 width
, height
, caller
)) {
3736 _mesa_lock_texture(ctx
, texObj
);
3738 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
3740 /* If we have a border, offset=-1 is legal. Bias by border width. */
3743 if (target
!= GL_TEXTURE_2D_ARRAY
)
3744 zoffset
+= texImage
->Border
;
3747 if (target
!= GL_TEXTURE_1D_ARRAY
)
3748 yoffset
+= texImage
->Border
;
3751 xoffset
+= texImage
->Border
;
3754 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3756 struct gl_renderbuffer
*srcRb
=
3757 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3759 copytexsubimage_by_slice(ctx
, texImage
, dims
,
3760 xoffset
, yoffset
, zoffset
,
3761 srcRb
, x
, y
, width
, height
);
3763 check_gen_mipmap(ctx
, target
, texObj
, level
);
3765 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3766 * the texel data, not the texture format, size, etc.
3770 _mesa_unlock_texture(ctx
, texObj
);
3774 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
3775 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
3777 struct gl_texture_object
* texObj
;
3778 const char *self
= "glCopyTexSubImage1D";
3779 GET_CURRENT_CONTEXT(ctx
);
3781 /* Check target (proxies not allowed). Target must be checked prior to
3782 * calling _mesa_get_current_tex_object.
3784 if (!legal_texsubimage_target(ctx
, 1, target
, false)) {
3785 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3786 _mesa_enum_to_string(target
));
3790 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3794 _mesa_copy_texture_sub_image(ctx
, 1, texObj
, target
, level
, xoffset
, 0, 0,
3795 x
, y
, width
, 1, self
);
3801 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
3802 GLint xoffset
, GLint yoffset
,
3803 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3805 struct gl_texture_object
* texObj
;
3806 const char *self
= "glCopyTexSubImage2D";
3807 GET_CURRENT_CONTEXT(ctx
);
3809 /* Check target (proxies not allowed). Target must be checked prior to
3810 * calling _mesa_get_current_tex_object.
3812 if (!legal_texsubimage_target(ctx
, 2, target
, false)) {
3813 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3814 _mesa_enum_to_string(target
));
3818 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3822 _mesa_copy_texture_sub_image(ctx
, 2, texObj
, target
, level
,
3823 xoffset
, yoffset
, 0,
3824 x
, y
, width
, height
, self
);
3830 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
3831 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3832 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3834 struct gl_texture_object
* texObj
;
3835 const char *self
= "glCopyTexSubImage3D";
3836 GET_CURRENT_CONTEXT(ctx
);
3838 /* Check target (proxies not allowed). Target must be checked prior to
3839 * calling _mesa_get_current_tex_object.
3841 if (!legal_texsubimage_target(ctx
, 3, target
, false)) {
3842 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3843 _mesa_enum_to_string(target
));
3847 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3851 _mesa_copy_texture_sub_image(ctx
, 3, texObj
, target
, level
,
3852 xoffset
, yoffset
, zoffset
,
3853 x
, y
, width
, height
, self
);
3857 _mesa_CopyTextureSubImage1D(GLuint texture
, GLint level
,
3858 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
3860 struct gl_texture_object
* texObj
;
3861 const char *self
= "glCopyTextureSubImage1D";
3862 GET_CURRENT_CONTEXT(ctx
);
3864 texObj
= _mesa_lookup_texture_err(ctx
, texture
, self
);
3868 /* Check target (proxies not allowed). */
3869 if (!legal_texsubimage_target(ctx
, 1, texObj
->Target
, true)) {
3870 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3871 _mesa_enum_to_string(texObj
->Target
));
3875 _mesa_copy_texture_sub_image(ctx
, 1, texObj
, texObj
->Target
, level
,
3876 xoffset
, 0, 0, x
, y
, width
, 1, self
);
3880 _mesa_CopyTextureSubImage2D(GLuint texture
, GLint level
,
3881 GLint xoffset
, GLint yoffset
,
3882 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3884 struct gl_texture_object
* texObj
;
3885 const char *self
= "glCopyTextureSubImage2D";
3886 GET_CURRENT_CONTEXT(ctx
);
3888 texObj
= _mesa_lookup_texture_err(ctx
, texture
, self
);
3892 /* Check target (proxies not allowed). */
3893 if (!legal_texsubimage_target(ctx
, 2, texObj
->Target
, true)) {
3894 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3895 _mesa_enum_to_string(texObj
->Target
));
3899 _mesa_copy_texture_sub_image(ctx
, 2, texObj
, texObj
->Target
, level
,
3900 xoffset
, yoffset
, 0,
3901 x
, y
, width
, height
, self
);
3907 _mesa_CopyTextureSubImage3D(GLuint texture
, GLint level
,
3908 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3909 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3911 struct gl_texture_object
* texObj
;
3912 const char *self
= "glCopyTextureSubImage3D";
3913 GET_CURRENT_CONTEXT(ctx
);
3915 texObj
= _mesa_lookup_texture_err(ctx
, texture
, self
);
3919 /* Check target (proxies not allowed). */
3920 if (!legal_texsubimage_target(ctx
, 3, texObj
->Target
, true)) {
3921 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3922 _mesa_enum_to_string(texObj
->Target
));
3926 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
3927 /* Act like CopyTexSubImage2D */
3928 _mesa_copy_texture_sub_image(ctx
, 2, texObj
,
3929 GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ zoffset
,
3930 level
, xoffset
, yoffset
, 0,
3931 x
, y
, width
, height
, self
);
3934 _mesa_copy_texture_sub_image(ctx
, 3, texObj
, texObj
->Target
, level
,
3935 xoffset
, yoffset
, zoffset
,
3936 x
, y
, width
, height
, self
);
3940 check_clear_tex_image(struct gl_context
*ctx
,
3941 const char *function
,
3942 struct gl_texture_image
*texImage
,
3943 GLenum format
, GLenum type
,
3945 GLubyte
*clearValue
)
3947 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3948 static const GLubyte zeroData
[MAX_PIXEL_BYTES
];
3949 GLenum internalFormat
= texImage
->InternalFormat
;
3952 if (texObj
->Target
== GL_TEXTURE_BUFFER
) {
3953 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3954 "%s(buffer texture)", function
);
3958 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
3959 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3960 "%s(compressed texture)", function
);
3964 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
3965 if (err
!= GL_NO_ERROR
) {
3966 _mesa_error(ctx
, err
,
3967 "%s(incompatible format = %s, type = %s)",
3969 _mesa_enum_to_string(format
),
3970 _mesa_enum_to_string(type
));
3974 /* make sure internal format and format basically agree */
3975 if (!texture_formats_agree(internalFormat
, format
)) {
3976 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3977 "%s(incompatible internalFormat = %s, format = %s)",
3979 _mesa_enum_to_string(internalFormat
),
3980 _mesa_enum_to_string(format
));
3984 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
3985 /* both source and dest must be integer-valued, or neither */
3986 if (_mesa_is_format_integer_color(texImage
->TexFormat
) !=
3987 _mesa_is_enum_format_integer(format
)) {
3988 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3989 "%s(integer/non-integer format mismatch)",
3995 if (!_mesa_texstore(ctx
,
3997 texImage
->_BaseFormat
,
3998 texImage
->TexFormat
,
3999 0, /* dstRowStride */
4001 1, 1, 1, /* srcWidth/Height/Depth */
4003 data
? data
: zeroData
,
4004 &ctx
->DefaultPacking
)) {
4005 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid format)", function
);
4012 static struct gl_texture_object
*
4013 get_tex_obj_for_clear(struct gl_context
*ctx
,
4014 const char *function
,
4017 struct gl_texture_object
*texObj
;
4020 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(zero texture)", function
);
4024 texObj
= _mesa_lookup_texture(ctx
, texture
);
4026 if (texObj
== NULL
) {
4027 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", function
);
4031 if (texObj
->Target
== 0) {
4032 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unbound tex)", function
);
4040 get_tex_images_for_clear(struct gl_context
*ctx
,
4041 const char *function
,
4042 struct gl_texture_object
*texObj
,
4044 struct gl_texture_image
**texImages
)
4049 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
4050 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid level)", function
);
4054 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
4055 for (i
= 0; i
< MAX_FACES
; i
++) {
4056 target
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ i
;
4058 texImages
[i
] = _mesa_select_tex_image(texObj
, target
, level
);
4059 if (texImages
[i
] == NULL
) {
4060 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4061 "%s(invalid level)", function
);
4069 texImages
[0] = _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4071 if (texImages
[0] == NULL
) {
4072 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid level)", function
);
4080 _mesa_ClearTexSubImage( GLuint texture
, GLint level
,
4081 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4082 GLsizei width
, GLsizei height
, GLsizei depth
,
4083 GLenum format
, GLenum type
, const void *data
)
4085 GET_CURRENT_CONTEXT(ctx
);
4086 struct gl_texture_object
*texObj
;
4087 struct gl_texture_image
*texImages
[MAX_FACES
];
4088 GLubyte clearValue
[MAX_FACES
][MAX_PIXEL_BYTES
];
4090 int minDepth
, maxDepth
;
4092 texObj
= get_tex_obj_for_clear(ctx
, "glClearTexSubImage", texture
);
4097 _mesa_lock_texture(ctx
, texObj
);
4099 numImages
= get_tex_images_for_clear(ctx
, "glClearTexSubImage",
4100 texObj
, level
, texImages
);
4104 if (numImages
== 1) {
4105 minDepth
= -(int) texImages
[0]->Border
;
4106 maxDepth
= texImages
[0]->Depth
;
4109 maxDepth
= numImages
;
4112 if (xoffset
< -(GLint
) texImages
[0]->Border
||
4113 yoffset
< -(GLint
) texImages
[0]->Border
||
4114 zoffset
< minDepth
||
4118 xoffset
+ width
> texImages
[0]->Width
||
4119 yoffset
+ height
> texImages
[0]->Height
||
4120 zoffset
+ depth
> maxDepth
) {
4121 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4122 "glClearSubTexImage(invalid dimensions)");
4126 if (numImages
== 1) {
4127 if (check_clear_tex_image(ctx
, "glClearTexSubImage",
4129 format
, type
, data
, clearValue
[0])) {
4130 ctx
->Driver
.ClearTexSubImage(ctx
,
4132 xoffset
, yoffset
, zoffset
,
4133 width
, height
, depth
,
4134 data
? clearValue
[0] : NULL
);
4137 for (i
= zoffset
; i
< zoffset
+ depth
; i
++) {
4138 if (!check_clear_tex_image(ctx
, "glClearTexSubImage",
4140 format
, type
, data
, clearValue
[i
]))
4143 for (i
= zoffset
; i
< zoffset
+ depth
; i
++) {
4144 ctx
->Driver
.ClearTexSubImage(ctx
,
4146 xoffset
, yoffset
, 0,
4148 data
? clearValue
[i
] : NULL
);
4153 _mesa_unlock_texture(ctx
, texObj
);
4157 _mesa_ClearTexImage( GLuint texture
, GLint level
,
4158 GLenum format
, GLenum type
, const void *data
)
4160 GET_CURRENT_CONTEXT(ctx
);
4161 struct gl_texture_object
*texObj
;
4162 struct gl_texture_image
*texImages
[MAX_FACES
];
4163 GLubyte clearValue
[MAX_FACES
][MAX_PIXEL_BYTES
];
4166 texObj
= get_tex_obj_for_clear(ctx
, "glClearTexImage", texture
);
4171 _mesa_lock_texture(ctx
, texObj
);
4173 numImages
= get_tex_images_for_clear(ctx
, "glClearTexImage",
4174 texObj
, level
, texImages
);
4176 for (i
= 0; i
< numImages
; i
++) {
4177 if (!check_clear_tex_image(ctx
, "glClearTexImage",
4184 for (i
= 0; i
< numImages
; i
++) {
4185 ctx
->Driver
.ClearTexSubImage(ctx
, texImages
[i
],
4186 -(GLint
) texImages
[i
]->Border
, /* xoffset */
4187 -(GLint
) texImages
[i
]->Border
, /* yoffset */
4188 -(GLint
) texImages
[i
]->Border
, /* zoffset */
4189 texImages
[i
]->Width
,
4190 texImages
[i
]->Height
,
4191 texImages
[i
]->Depth
,
4192 data
? clearValue
[i
] : NULL
);
4196 _mesa_unlock_texture(ctx
, texObj
);
4202 /**********************************************************************/
4203 /****** Compressed Textures ******/
4204 /**********************************************************************/
4208 * Target checking for glCompressedTexSubImage[123]D().
4209 * \return GL_TRUE if error, GL_FALSE if no error
4210 * Must come before other error checking so that the texture object can
4211 * be correctly retrieved using _mesa_get_current_tex_object.
4214 compressed_subtexture_target_check(struct gl_context
*ctx
, GLenum target
,
4215 GLint dims
, GLenum format
, bool dsa
,
4220 if (dsa
&& target
== GL_TEXTURE_RECTANGLE
) {
4221 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid target %s)", caller
,
4222 _mesa_enum_to_string(target
));
4232 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
4233 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
4234 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
4235 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
4236 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
4237 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
4238 targetOK
= ctx
->Extensions
.ARB_texture_cube_map
;
4241 targetOK
= GL_FALSE
;
4247 case GL_TEXTURE_CUBE_MAP
:
4248 targetOK
= dsa
&& ctx
->Extensions
.ARB_texture_cube_map
;
4250 case GL_TEXTURE_2D_ARRAY
:
4251 targetOK
= _mesa_is_gles3(ctx
) ||
4252 (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
);
4254 case GL_TEXTURE_CUBE_MAP_ARRAY
:
4255 targetOK
= ctx
->Extensions
.ARB_texture_cube_map_array
;
4260 * OpenGL 4.5 spec (30.10.2014) says in Section 8.7 Compressed Texture
4262 * "An INVALID_OPERATION error is generated by
4263 * CompressedTex*SubImage3D if the internal format of the texture
4264 * is one of the EAC, ETC2, or RGTC formats and either border is
4265 * non-zero, or the effective target for the texture is not
4266 * TEXTURE_2D_ARRAY."
4268 * NOTE: that's probably a spec error. It should probably say
4269 * "... or the effective target for the texture is not
4270 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP, nor
4271 * GL_TEXTURE_CUBE_MAP_ARRAY."
4272 * since those targets are 2D images and they support all compression
4275 * Instead of listing all these, just list those which are allowed,
4276 * which is (at this time) only bptc. Otherwise we'd say s3tc (and
4277 * more) are valid here, which they are not, but of course not
4278 * mentioned by core spec.
4281 /* These are the only 3D compression formats supported at this time */
4282 case GL_COMPRESSED_RGBA_BPTC_UNORM
:
4283 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM
:
4284 case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT
:
4285 case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT
:
4289 /* invalid format */
4290 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4291 "%s(invalid target %s for format %s)", caller
,
4292 _mesa_enum_to_string(target
),
4293 _mesa_enum_to_string(format
));
4298 targetOK
= GL_FALSE
;
4304 /* no 1D compressed textures at this time */
4305 targetOK
= GL_FALSE
;
4310 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", caller
,
4311 _mesa_enum_to_string(target
));
4319 * Error checking for glCompressedTexSubImage[123]D().
4320 * \return GL_TRUE if error, GL_FALSE if no error
4323 compressed_subtexture_error_check(struct gl_context
*ctx
, GLint dims
,
4324 const struct gl_texture_object
*texObj
,
4325 GLenum target
, GLint level
,
4326 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4327 GLsizei width
, GLsizei height
, GLsizei depth
,
4328 GLenum format
, GLsizei imageSize
,
4329 const GLvoid
*data
, const char *callerName
)
4331 struct gl_texture_image
*texImage
;
4334 /* this will catch any invalid compressed format token */
4335 if (!_mesa_is_compressed_format(ctx
, format
)) {
4336 _mesa_error(ctx
, GL_INVALID_ENUM
,
4337 "%s(format)", callerName
);
4341 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
4342 _mesa_error(ctx
, GL_INVALID_VALUE
,
4348 /* validate the bound PBO, if any */
4349 if (!_mesa_validate_pbo_source_compressed(ctx
, dims
, &ctx
->Unpack
,
4350 imageSize
, data
, callerName
)) {
4354 /* Check for invalid pixel storage modes */
4355 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dims
,
4356 &ctx
->Unpack
, callerName
)) {
4360 expectedSize
= compressed_tex_size(width
, height
, depth
, format
);
4361 if (expectedSize
!= imageSize
) {
4362 _mesa_error(ctx
, GL_INVALID_VALUE
,
4364 callerName
, imageSize
);
4368 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4370 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4371 "%s(invalid texture image)",
4376 if ((GLint
) format
!= texImage
->InternalFormat
) {
4377 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4379 callerName
, _mesa_enum_to_string(format
));
4383 if (compressedteximage_only_format(ctx
, format
)) {
4384 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4385 "%s(format=%s cannot be updated)",
4386 callerName
, _mesa_enum_to_string(format
));
4390 if (error_check_subtexture_dimensions(ctx
, dims
,
4391 texImage
, xoffset
, yoffset
, zoffset
,
4392 width
, height
, depth
,
4402 _mesa_CompressedTexImage1D(GLenum target
, GLint level
,
4403 GLenum internalFormat
, GLsizei width
,
4404 GLint border
, GLsizei imageSize
,
4407 GET_CURRENT_CONTEXT(ctx
);
4408 teximage(ctx
, GL_TRUE
, 1, target
, level
, internalFormat
,
4409 width
, 1, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4414 _mesa_CompressedTexImage2D(GLenum target
, GLint level
,
4415 GLenum internalFormat
, GLsizei width
,
4416 GLsizei height
, GLint border
, GLsizei imageSize
,
4419 GET_CURRENT_CONTEXT(ctx
);
4420 teximage(ctx
, GL_TRUE
, 2, target
, level
, internalFormat
,
4421 width
, height
, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4426 _mesa_CompressedTexImage3D(GLenum target
, GLint level
,
4427 GLenum internalFormat
, GLsizei width
,
4428 GLsizei height
, GLsizei depth
, GLint border
,
4429 GLsizei imageSize
, const GLvoid
*data
)
4431 GET_CURRENT_CONTEXT(ctx
);
4432 teximage(ctx
, GL_TRUE
, 3, target
, level
, internalFormat
,
4433 width
, height
, depth
, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4438 * Common helper for glCompressedTexSubImage1/2/3D() and
4439 * glCompressedTextureSubImage1/2/3D().
4442 _mesa_compressed_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
4443 struct gl_texture_object
*texObj
,
4444 struct gl_texture_image
*texImage
,
4445 GLenum target
, GLint level
,
4446 GLint xoffset
, GLint yoffset
,
4448 GLsizei width
, GLsizei height
,
4450 GLenum format
, GLsizei imageSize
,
4453 FLUSH_VERTICES(ctx
, 0);
4455 _mesa_lock_texture(ctx
, texObj
);
4457 if (width
> 0 && height
> 0 && depth
> 0) {
4458 ctx
->Driver
.CompressedTexSubImage(ctx
, dims
, texImage
,
4459 xoffset
, yoffset
, zoffset
,
4460 width
, height
, depth
,
4461 format
, imageSize
, data
);
4463 check_gen_mipmap(ctx
, target
, texObj
, level
);
4465 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
4466 * the texel data, not the texture format, size, etc.
4470 _mesa_unlock_texture(ctx
, texObj
);
4475 _mesa_CompressedTexSubImage1D(GLenum target
, GLint level
, GLint xoffset
,
4476 GLsizei width
, GLenum format
,
4477 GLsizei imageSize
, const GLvoid
*data
)
4479 struct gl_texture_object
*texObj
;
4480 struct gl_texture_image
*texImage
;
4482 GET_CURRENT_CONTEXT(ctx
);
4484 if (compressed_subtexture_target_check(ctx
, target
, 1, format
, false,
4485 "glCompressedTexSubImage1D")) {
4489 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4493 if (compressed_subtexture_error_check(ctx
, 1, texObj
, target
,
4494 level
, xoffset
, 0, 0,
4496 format
, imageSize
, data
,
4497 "glCompressedTexSubImage1D")) {
4501 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4504 _mesa_compressed_texture_sub_image(ctx
, 1, texObj
, texImage
, target
, level
,
4505 xoffset
, 0, 0, width
, 1, 1,
4506 format
, imageSize
, data
);
4510 _mesa_CompressedTextureSubImage1D(GLuint texture
, GLint level
, GLint xoffset
,
4511 GLsizei width
, GLenum format
,
4512 GLsizei imageSize
, const GLvoid
*data
)
4514 struct gl_texture_object
*texObj
;
4515 struct gl_texture_image
*texImage
;
4517 GET_CURRENT_CONTEXT(ctx
);
4519 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4520 "glCompressedTextureSubImage1D");
4524 if (compressed_subtexture_target_check(ctx
, texObj
->Target
, 1, format
, true,
4525 "glCompressedTextureSubImage1D")) {
4529 if (compressed_subtexture_error_check(ctx
, 1, texObj
, texObj
->Target
,
4530 level
, xoffset
, 0, 0,
4532 format
, imageSize
, data
,
4533 "glCompressedTextureSubImage1D")) {
4537 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4540 _mesa_compressed_texture_sub_image(ctx
, 1, texObj
, texImage
,
4541 texObj
->Target
, level
,
4542 xoffset
, 0, 0, width
, 1, 1,
4543 format
, imageSize
, data
);
4548 _mesa_CompressedTexSubImage2D(GLenum target
, GLint level
, GLint xoffset
,
4549 GLint yoffset
, GLsizei width
, GLsizei height
,
4550 GLenum format
, GLsizei imageSize
,
4553 struct gl_texture_object
*texObj
;
4554 struct gl_texture_image
*texImage
;
4556 GET_CURRENT_CONTEXT(ctx
);
4558 if (compressed_subtexture_target_check(ctx
, target
, 2, format
, false,
4559 "glCompressedTexSubImage2D")) {
4563 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4567 if (compressed_subtexture_error_check(ctx
, 2, texObj
, target
,
4568 level
, xoffset
, yoffset
, 0,
4570 format
, imageSize
, data
,
4571 "glCompressedTexSubImage2D")) {
4576 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4579 _mesa_compressed_texture_sub_image(ctx
, 2, texObj
, texImage
, target
, level
,
4580 xoffset
, yoffset
, 0, width
, height
, 1,
4581 format
, imageSize
, data
);
4585 _mesa_CompressedTextureSubImage2D(GLuint texture
, GLint level
, GLint xoffset
,
4587 GLsizei width
, GLsizei height
,
4588 GLenum format
, GLsizei imageSize
,
4591 struct gl_texture_object
*texObj
;
4592 struct gl_texture_image
*texImage
;
4594 GET_CURRENT_CONTEXT(ctx
);
4596 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4597 "glCompressedTextureSubImage2D");
4601 if (compressed_subtexture_target_check(ctx
, texObj
->Target
, 2, format
, true,
4602 "glCompressedTextureSubImage2D")) {
4606 if (compressed_subtexture_error_check(ctx
, 2, texObj
, texObj
->Target
,
4607 level
, xoffset
, yoffset
, 0,
4609 format
, imageSize
, data
,
4610 "glCompressedTextureSubImage2D")) {
4614 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4617 _mesa_compressed_texture_sub_image(ctx
, 2, texObj
, texImage
,
4618 texObj
->Target
, level
,
4619 xoffset
, yoffset
, 0, width
, height
, 1,
4620 format
, imageSize
, data
);
4624 _mesa_CompressedTexSubImage3D(GLenum target
, GLint level
, GLint xoffset
,
4625 GLint yoffset
, GLint zoffset
, GLsizei width
,
4626 GLsizei height
, GLsizei depth
, GLenum format
,
4627 GLsizei imageSize
, const GLvoid
*data
)
4629 struct gl_texture_object
*texObj
;
4630 struct gl_texture_image
*texImage
;
4632 GET_CURRENT_CONTEXT(ctx
);
4634 if (compressed_subtexture_target_check(ctx
, target
, 3, format
, false,
4635 "glCompressedTexSubImage3D")) {
4639 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4643 if (compressed_subtexture_error_check(ctx
, 3, texObj
, target
,
4644 level
, xoffset
, yoffset
, zoffset
,
4645 width
, height
, depth
,
4646 format
, imageSize
, data
,
4647 "glCompressedTexSubImage3D")) {
4652 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4655 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, texImage
, target
, level
,
4656 xoffset
, yoffset
, zoffset
,
4657 width
, height
, depth
,
4658 format
, imageSize
, data
);
4662 _mesa_CompressedTextureSubImage3D(GLuint texture
, GLint level
, GLint xoffset
,
4663 GLint yoffset
, GLint zoffset
, GLsizei width
,
4664 GLsizei height
, GLsizei depth
,
4665 GLenum format
, GLsizei imageSize
,
4668 struct gl_texture_object
*texObj
;
4669 struct gl_texture_image
*texImage
;
4671 GET_CURRENT_CONTEXT(ctx
);
4673 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4674 "glCompressedTextureSubImage3D");
4678 if (compressed_subtexture_target_check(ctx
, texObj
->Target
, 3, format
, true,
4679 "glCompressedTextureSubImage3D")) {
4683 if (compressed_subtexture_error_check(ctx
, 3, texObj
, texObj
->Target
,
4684 level
, xoffset
, yoffset
, zoffset
,
4685 width
, height
, depth
,
4686 format
, imageSize
, data
,
4687 "glCompressedTextureSubImage3D")) {
4691 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
4692 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
4693 const char *pixels
= data
;
4697 /* Make sure the texture object is a proper cube.
4698 * (See texturesubimage in teximage.c for details on why this check is
4701 if (!_mesa_cube_level_complete(texObj
, level
)) {
4702 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4703 "glCompressedTextureSubImage3D(cube map incomplete)");
4707 /* Copy in each face. */
4708 for (i
= 0; i
< 6; ++i
) {
4709 texImage
= texObj
->Image
[i
][level
];
4712 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, texImage
,
4713 texObj
->Target
, level
,
4714 xoffset
, yoffset
, zoffset
,
4716 format
, imageSize
, pixels
);
4718 /* Compressed images don't have a client format */
4719 image_stride
= _mesa_format_image_size(texImage
->TexFormat
,
4721 texImage
->Height
, 1);
4723 pixels
+= image_stride
;
4724 imageSize
-= image_stride
;
4728 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4731 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, texImage
,
4732 texObj
->Target
, level
,
4733 xoffset
, yoffset
, zoffset
,
4734 width
, height
, depth
,
4735 format
, imageSize
, data
);
4740 get_texbuffer_format(const struct gl_context
*ctx
, GLenum internalFormat
)
4742 if (ctx
->API
== API_OPENGL_COMPAT
) {
4743 switch (internalFormat
) {
4745 return MESA_FORMAT_A_UNORM8
;
4747 return MESA_FORMAT_A_UNORM16
;
4748 case GL_ALPHA16F_ARB
:
4749 return MESA_FORMAT_A_FLOAT16
;
4750 case GL_ALPHA32F_ARB
:
4751 return MESA_FORMAT_A_FLOAT32
;
4752 case GL_ALPHA8I_EXT
:
4753 return MESA_FORMAT_A_SINT8
;
4754 case GL_ALPHA16I_EXT
:
4755 return MESA_FORMAT_A_SINT16
;
4756 case GL_ALPHA32I_EXT
:
4757 return MESA_FORMAT_A_SINT32
;
4758 case GL_ALPHA8UI_EXT
:
4759 return MESA_FORMAT_A_UINT8
;
4760 case GL_ALPHA16UI_EXT
:
4761 return MESA_FORMAT_A_UINT16
;
4762 case GL_ALPHA32UI_EXT
:
4763 return MESA_FORMAT_A_UINT32
;
4765 return MESA_FORMAT_L_UNORM8
;
4766 case GL_LUMINANCE16
:
4767 return MESA_FORMAT_L_UNORM16
;
4768 case GL_LUMINANCE16F_ARB
:
4769 return MESA_FORMAT_L_FLOAT16
;
4770 case GL_LUMINANCE32F_ARB
:
4771 return MESA_FORMAT_L_FLOAT32
;
4772 case GL_LUMINANCE8I_EXT
:
4773 return MESA_FORMAT_L_SINT8
;
4774 case GL_LUMINANCE16I_EXT
:
4775 return MESA_FORMAT_L_SINT16
;
4776 case GL_LUMINANCE32I_EXT
:
4777 return MESA_FORMAT_L_SINT32
;
4778 case GL_LUMINANCE8UI_EXT
:
4779 return MESA_FORMAT_L_UINT8
;
4780 case GL_LUMINANCE16UI_EXT
:
4781 return MESA_FORMAT_L_UINT16
;
4782 case GL_LUMINANCE32UI_EXT
:
4783 return MESA_FORMAT_L_UINT32
;
4784 case GL_LUMINANCE8_ALPHA8
:
4785 return MESA_FORMAT_L8A8_UNORM
;
4786 case GL_LUMINANCE16_ALPHA16
:
4787 return MESA_FORMAT_L16A16_UNORM
;
4788 case GL_LUMINANCE_ALPHA16F_ARB
:
4789 return MESA_FORMAT_LA_FLOAT16
;
4790 case GL_LUMINANCE_ALPHA32F_ARB
:
4791 return MESA_FORMAT_LA_FLOAT32
;
4792 case GL_LUMINANCE_ALPHA8I_EXT
:
4793 return MESA_FORMAT_LA_SINT8
;
4794 case GL_LUMINANCE_ALPHA16I_EXT
:
4795 return MESA_FORMAT_LA_SINT16
;
4796 case GL_LUMINANCE_ALPHA32I_EXT
:
4797 return MESA_FORMAT_LA_SINT32
;
4798 case GL_LUMINANCE_ALPHA8UI_EXT
:
4799 return MESA_FORMAT_LA_UINT8
;
4800 case GL_LUMINANCE_ALPHA16UI_EXT
:
4801 return MESA_FORMAT_LA_UINT16
;
4802 case GL_LUMINANCE_ALPHA32UI_EXT
:
4803 return MESA_FORMAT_LA_UINT32
;
4805 return MESA_FORMAT_I_UNORM8
;
4806 case GL_INTENSITY16
:
4807 return MESA_FORMAT_I_UNORM16
;
4808 case GL_INTENSITY16F_ARB
:
4809 return MESA_FORMAT_I_FLOAT16
;
4810 case GL_INTENSITY32F_ARB
:
4811 return MESA_FORMAT_I_FLOAT32
;
4812 case GL_INTENSITY8I_EXT
:
4813 return MESA_FORMAT_I_SINT8
;
4814 case GL_INTENSITY16I_EXT
:
4815 return MESA_FORMAT_I_SINT16
;
4816 case GL_INTENSITY32I_EXT
:
4817 return MESA_FORMAT_I_SINT32
;
4818 case GL_INTENSITY8UI_EXT
:
4819 return MESA_FORMAT_I_UINT8
;
4820 case GL_INTENSITY16UI_EXT
:
4821 return MESA_FORMAT_I_UINT16
;
4822 case GL_INTENSITY32UI_EXT
:
4823 return MESA_FORMAT_I_UINT32
;
4829 if (_mesa_has_ARB_texture_buffer_object_rgb32(ctx
) ||
4830 _mesa_has_OES_texture_buffer(ctx
)) {
4831 switch (internalFormat
) {
4833 return MESA_FORMAT_RGB_FLOAT32
;
4835 return MESA_FORMAT_RGB_UINT32
;
4837 return MESA_FORMAT_RGB_SINT32
;
4843 switch (internalFormat
) {
4845 return MESA_FORMAT_R8G8B8A8_UNORM
;
4847 if (_mesa_is_gles(ctx
))
4848 return MESA_FORMAT_NONE
;
4849 return MESA_FORMAT_RGBA_UNORM16
;
4850 case GL_RGBA16F_ARB
:
4851 return MESA_FORMAT_RGBA_FLOAT16
;
4852 case GL_RGBA32F_ARB
:
4853 return MESA_FORMAT_RGBA_FLOAT32
;
4855 return MESA_FORMAT_RGBA_SINT8
;
4856 case GL_RGBA16I_EXT
:
4857 return MESA_FORMAT_RGBA_SINT16
;
4858 case GL_RGBA32I_EXT
:
4859 return MESA_FORMAT_RGBA_SINT32
;
4860 case GL_RGBA8UI_EXT
:
4861 return MESA_FORMAT_RGBA_UINT8
;
4862 case GL_RGBA16UI_EXT
:
4863 return MESA_FORMAT_RGBA_UINT16
;
4864 case GL_RGBA32UI_EXT
:
4865 return MESA_FORMAT_RGBA_UINT32
;
4868 return MESA_FORMAT_R8G8_UNORM
;
4870 if (_mesa_is_gles(ctx
))
4871 return MESA_FORMAT_NONE
;
4872 return MESA_FORMAT_R16G16_UNORM
;
4874 return MESA_FORMAT_RG_FLOAT16
;
4876 return MESA_FORMAT_RG_FLOAT32
;
4878 return MESA_FORMAT_RG_SINT8
;
4880 return MESA_FORMAT_RG_SINT16
;
4882 return MESA_FORMAT_RG_SINT32
;
4884 return MESA_FORMAT_RG_UINT8
;
4886 return MESA_FORMAT_RG_UINT16
;
4888 return MESA_FORMAT_RG_UINT32
;
4891 return MESA_FORMAT_R_UNORM8
;
4893 if (_mesa_is_gles(ctx
))
4894 return MESA_FORMAT_NONE
;
4895 return MESA_FORMAT_R_UNORM16
;
4897 return MESA_FORMAT_R_FLOAT16
;
4899 return MESA_FORMAT_R_FLOAT32
;
4901 return MESA_FORMAT_R_SINT8
;
4903 return MESA_FORMAT_R_SINT16
;
4905 return MESA_FORMAT_R_SINT32
;
4907 return MESA_FORMAT_R_UINT8
;
4909 return MESA_FORMAT_R_UINT16
;
4911 return MESA_FORMAT_R_UINT32
;
4914 return MESA_FORMAT_NONE
;
4920 _mesa_validate_texbuffer_format(const struct gl_context
*ctx
,
4921 GLenum internalFormat
)
4923 mesa_format format
= get_texbuffer_format(ctx
, internalFormat
);
4926 if (format
== MESA_FORMAT_NONE
)
4927 return MESA_FORMAT_NONE
;
4929 datatype
= _mesa_get_format_datatype(format
);
4931 /* The GL_ARB_texture_buffer_object spec says:
4933 * "If ARB_texture_float is not supported, references to the
4934 * floating-point internal formats provided by that extension should be
4935 * removed, and such formats may not be passed to TexBufferARB."
4937 * As a result, GL_HALF_FLOAT internal format depends on both
4938 * GL_ARB_texture_float and GL_ARB_half_float_pixel.
4940 if ((datatype
== GL_FLOAT
|| datatype
== GL_HALF_FLOAT
) &&
4941 !ctx
->Extensions
.ARB_texture_float
)
4942 return MESA_FORMAT_NONE
;
4944 if (!ctx
->Extensions
.ARB_texture_rg
) {
4945 GLenum base_format
= _mesa_get_format_base_format(format
);
4946 if (base_format
== GL_R
|| base_format
== GL_RG
)
4947 return MESA_FORMAT_NONE
;
4950 if (!ctx
->Extensions
.ARB_texture_buffer_object_rgb32
) {
4951 GLenum base_format
= _mesa_get_format_base_format(format
);
4952 if (base_format
== GL_RGB
)
4953 return MESA_FORMAT_NONE
;
4960 _mesa_texture_buffer_range(struct gl_context
*ctx
,
4961 struct gl_texture_object
*texObj
,
4962 GLenum internalFormat
,
4963 struct gl_buffer_object
*bufObj
,
4964 GLintptr offset
, GLsizeiptr size
,
4969 /* NOTE: ARB_texture_buffer_object has interactions with
4970 * the compatibility profile that are not implemented.
4972 if (!_mesa_has_ARB_texture_buffer_object(ctx
) &&
4973 !_mesa_has_OES_texture_buffer(ctx
)) {
4974 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4975 "%s(ARB_texture_buffer_object is not"
4976 " implemented for the compatibility profile)", caller
);
4980 format
= _mesa_validate_texbuffer_format(ctx
, internalFormat
);
4981 if (format
== MESA_FORMAT_NONE
) {
4982 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat %s)",
4983 caller
, _mesa_enum_to_string(internalFormat
));
4987 FLUSH_VERTICES(ctx
, 0);
4989 _mesa_lock_texture(ctx
, texObj
);
4991 _mesa_reference_buffer_object(ctx
, &texObj
->BufferObject
, bufObj
);
4992 texObj
->BufferObjectFormat
= internalFormat
;
4993 texObj
->_BufferObjectFormat
= format
;
4994 texObj
->BufferOffset
= offset
;
4995 texObj
->BufferSize
= size
;
4997 _mesa_unlock_texture(ctx
, texObj
);
4999 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTextureBuffer
;
5002 bufObj
->UsageHistory
|= USAGE_TEXTURE_BUFFER
;
5008 * Make sure the texture buffer target is GL_TEXTURE_BUFFER.
5009 * Return true if it is, and return false if it is not
5010 * (and throw INVALID ENUM as dictated in the OpenGL 4.5
5011 * core spec, 02.02.2015, PDF page 245).
5014 check_texture_buffer_target(struct gl_context
*ctx
, GLenum target
,
5017 if (target
!= GL_TEXTURE_BUFFER_ARB
) {
5018 _mesa_error(ctx
, GL_INVALID_ENUM
,
5019 "%s(texture target is not GL_TEXTURE_BUFFER)", caller
);
5027 * Check for errors related to the texture buffer range.
5028 * Return false if errors are found, true if none are found.
5031 check_texture_buffer_range(struct gl_context
*ctx
,
5032 struct gl_buffer_object
*bufObj
,
5033 GLintptr offset
, GLsizeiptr size
,
5036 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5037 * Textures (PDF page 245):
5038 * "An INVALID_VALUE error is generated if offset is negative, if
5039 * size is less than or equal to zero, or if offset + size is greater
5040 * than the value of BUFFER_SIZE for the buffer bound to target."
5043 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset=%d < 0)", caller
,
5049 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size=%d <= 0)", caller
,
5054 if (offset
+ size
> bufObj
->Size
) {
5055 _mesa_error(ctx
, GL_INVALID_VALUE
,
5056 "%s(offset=%d + size=%d > buffer_size=%d)", caller
,
5057 (int) offset
, (int) size
, (int) bufObj
->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 not an integer
5064 * multiple of the value of TEXTURE_BUFFER_OFFSET_ALIGNMENT."
5066 if (offset
% ctx
->Const
.TextureBufferOffsetAlignment
) {
5067 _mesa_error(ctx
, GL_INVALID_VALUE
,
5068 "%s(invalid offset alignment)", caller
);
5076 /** GL_ARB_texture_buffer_object */
5078 _mesa_TexBuffer(GLenum target
, GLenum internalFormat
, GLuint buffer
)
5080 struct gl_texture_object
*texObj
;
5081 struct gl_buffer_object
*bufObj
;
5083 GET_CURRENT_CONTEXT(ctx
);
5085 /* Need to catch a bad target before it gets to
5086 * _mesa_get_current_tex_object.
5088 if (!check_texture_buffer_target(ctx
, target
, "glTexBuffer"))
5092 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glTexBuffer");
5098 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5102 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
, bufObj
, 0,
5103 buffer
? -1 : 0, "glTexBuffer");
5107 /** GL_ARB_texture_buffer_range */
5109 _mesa_TexBufferRange(GLenum target
, GLenum internalFormat
, GLuint buffer
,
5110 GLintptr offset
, GLsizeiptr size
)
5112 struct gl_texture_object
*texObj
;
5113 struct gl_buffer_object
*bufObj
;
5115 GET_CURRENT_CONTEXT(ctx
);
5117 /* Need to catch a bad target before it gets to
5118 * _mesa_get_current_tex_object.
5120 if (!check_texture_buffer_target(ctx
, target
, "glTexBufferRange"))
5124 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glTexBufferRange");
5128 if (!check_texture_buffer_range(ctx
, bufObj
, offset
, size
,
5129 "glTexBufferRange"))
5133 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5134 * Textures (PDF page 254):
5135 * "If buffer is zero, then any buffer object attached to the buffer
5136 * texture is detached, the values offset and size are ignored and
5137 * the state for offset and size for the buffer texture are reset to
5145 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5149 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
, bufObj
,
5150 offset
, size
, "glTexBufferRange");
5154 _mesa_TextureBuffer(GLuint texture
, GLenum internalFormat
, GLuint buffer
)
5156 struct gl_texture_object
*texObj
;
5157 struct gl_buffer_object
*bufObj
;
5159 GET_CURRENT_CONTEXT(ctx
);
5162 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glTextureBuffer");
5168 /* Get the texture object by Name. */
5169 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glTextureBuffer");
5173 if (!check_texture_buffer_target(ctx
, texObj
->Target
, "glTextureBuffer"))
5176 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
,
5177 bufObj
, 0, buffer
? -1 : 0, "glTextureBuffer");
5181 _mesa_TextureBufferRange(GLuint texture
, GLenum internalFormat
, GLuint buffer
,
5182 GLintptr offset
, GLsizeiptr size
)
5184 struct gl_texture_object
*texObj
;
5185 struct gl_buffer_object
*bufObj
;
5187 GET_CURRENT_CONTEXT(ctx
);
5190 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
5191 "glTextureBufferRange");
5195 if (!check_texture_buffer_range(ctx
, bufObj
, offset
, size
,
5196 "glTextureBufferRange"))
5200 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5201 * Textures (PDF page 254):
5202 * "If buffer is zero, then any buffer object attached to the buffer
5203 * texture is detached, the values offset and size are ignored and
5204 * the state for offset and size for the buffer texture are reset to
5212 /* Get the texture object by Name. */
5213 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glTextureBufferRange");
5217 if (!check_texture_buffer_target(ctx
, texObj
->Target
,
5218 "glTextureBufferRange"))
5221 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
,
5222 bufObj
, offset
, size
, "glTextureBufferRange");
5226 _mesa_is_renderable_texture_format(struct gl_context
*ctx
, GLenum internalformat
)
5228 /* Everything that is allowed for renderbuffers,
5229 * except for a base format of GL_STENCIL_INDEX, unless supported.
5231 GLenum baseFormat
= _mesa_base_fbo_format(ctx
, internalformat
);
5232 if (ctx
->Extensions
.ARB_texture_stencil8
)
5233 return baseFormat
!= 0;
5235 return baseFormat
!= 0 && baseFormat
!= GL_STENCIL_INDEX
;
5239 /** GL_ARB_texture_multisample */
5241 check_multisample_target(GLuint dims
, GLenum target
, bool dsa
)
5244 case GL_TEXTURE_2D_MULTISAMPLE
:
5246 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
5247 return dims
== 2 && !dsa
;
5248 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
5250 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
5251 return dims
== 3 && !dsa
;
5259 texture_image_multisample(struct gl_context
*ctx
, GLuint dims
,
5260 struct gl_texture_object
*texObj
,
5261 GLenum target
, GLsizei samples
,
5262 GLint internalformat
, GLsizei width
,
5263 GLsizei height
, GLsizei depth
,
5264 GLboolean fixedsamplelocations
,
5265 GLboolean immutable
, const char *func
)
5267 struct gl_texture_image
*texImage
;
5268 GLboolean sizeOK
, dimensionsOK
, samplesOK
;
5269 mesa_format texFormat
;
5270 GLenum sample_count_error
;
5271 bool dsa
= strstr(func
, "ture") ? true : false;
5273 if (!((ctx
->Extensions
.ARB_texture_multisample
5274 && _mesa_is_desktop_gl(ctx
))) && !_mesa_is_gles31(ctx
)) {
5275 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unsupported)", func
);
5280 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(samples < 1)", func
);
5284 if (!check_multisample_target(dims
, target
, dsa
)) {
5286 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(target)", func
);
5290 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
5295 /* check that the specified internalformat is color/depth/stencil-renderable;
5296 * refer GL3.1 spec 4.4.4
5299 if (immutable
&& !_mesa_is_legal_tex_storage_format(ctx
, internalformat
)) {
5300 _mesa_error(ctx
, GL_INVALID_ENUM
,
5301 "%s(internalformat=%s not legal for immutable-format)",
5302 func
, _mesa_enum_to_string(internalformat
));
5306 if (!_mesa_is_renderable_texture_format(ctx
, internalformat
)) {
5307 /* Page 172 of OpenGL ES 3.1 spec says:
5308 * "An INVALID_ENUM error is generated if sizedinternalformat is not
5309 * color-renderable, depth-renderable, or stencil-renderable (as
5310 * defined in section 9.4).
5312 * (Same error is also defined for desktop OpenGL for multisample
5313 * teximage/texstorage functions.)
5315 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalformat=%s)", func
,
5316 _mesa_enum_to_string(internalformat
));
5320 sample_count_error
= _mesa_check_sample_count(ctx
, target
,
5321 internalformat
, samples
);
5322 samplesOK
= sample_count_error
== GL_NO_ERROR
;
5324 /* Page 254 of OpenGL 4.4 spec says:
5325 * "Proxy arrays for two-dimensional multisample and two-dimensional
5326 * multisample array textures are operated on in the same way when
5327 * TexImage2DMultisample is called with target specified as
5328 * PROXY_TEXTURE_2D_MULTISAMPLE, or TexImage3DMultisample is called
5329 * with target specified as PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY.
5330 * However, if samples is not supported, then no error is generated.
5332 if (!samplesOK
&& !_mesa_is_proxy_texture(target
)) {
5333 _mesa_error(ctx
, sample_count_error
, "%s(samples)", func
);
5337 if (immutable
&& (!texObj
|| (texObj
->Name
== 0))) {
5338 _mesa_error(ctx
, GL_INVALID_OPERATION
,
5339 "%s(texture object 0)",
5344 texImage
= _mesa_get_tex_image(ctx
, texObj
, 0, 0);
5346 if (texImage
== NULL
) {
5347 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", func
);
5351 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, 0,
5352 internalformat
, GL_NONE
, GL_NONE
);
5353 assert(texFormat
!= MESA_FORMAT_NONE
);
5355 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, 0,
5356 width
, height
, depth
, 0);
5358 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, target
, 0, texFormat
,
5359 width
, height
, depth
, 0);
5361 if (_mesa_is_proxy_texture(target
)) {
5362 if (samplesOK
&& dimensionsOK
&& sizeOK
) {
5363 init_teximage_fields_ms(ctx
, texImage
, width
, height
, depth
, 0,
5364 internalformat
, texFormat
,
5365 samples
, fixedsamplelocations
);
5368 /* clear all image fields */
5369 clear_teximage_fields(texImage
);
5373 if (!dimensionsOK
) {
5374 _mesa_error(ctx
, GL_INVALID_VALUE
,
5375 "%s(invalid width or height)", func
);
5380 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(texture too large)", func
);
5384 /* Check if texObj->Immutable is set */
5385 if (texObj
->Immutable
) {
5386 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
5390 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
5392 init_teximage_fields_ms(ctx
, texImage
, width
, height
, depth
, 0,
5393 internalformat
, texFormat
,
5394 samples
, fixedsamplelocations
);
5396 if (width
> 0 && height
> 0 && depth
> 0) {
5397 if (!ctx
->Driver
.AllocTextureStorage(ctx
, texObj
, 1,
5398 width
, height
, depth
)) {
5399 /* tidy up the texture image state. strictly speaking,
5400 * we're allowed to just leave this in whatever state we
5401 * like, but being tidy is good.
5403 _mesa_init_teximage_fields(ctx
, texImage
,
5404 0, 0, 0, 0, GL_NONE
, MESA_FORMAT_NONE
);
5408 texObj
->Immutable
|= immutable
;
5411 _mesa_set_texture_view_state(ctx
, texObj
, target
, 1);
5414 _mesa_update_fbo_texture(ctx
, texObj
, 0, 0);
5420 _mesa_TexImage2DMultisample(GLenum target
, GLsizei samples
,
5421 GLenum internalformat
, GLsizei width
,
5422 GLsizei height
, GLboolean fixedsamplelocations
)
5424 struct gl_texture_object
*texObj
;
5425 GET_CURRENT_CONTEXT(ctx
);
5427 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5431 texture_image_multisample(ctx
, 2, texObj
, target
, samples
,
5432 internalformat
, width
, height
, 1,
5433 fixedsamplelocations
, GL_FALSE
,
5434 "glTexImage2DMultisample");
5439 _mesa_TexImage3DMultisample(GLenum target
, GLsizei samples
,
5440 GLenum internalformat
, GLsizei width
,
5441 GLsizei height
, GLsizei depth
,
5442 GLboolean fixedsamplelocations
)
5444 struct gl_texture_object
*texObj
;
5445 GET_CURRENT_CONTEXT(ctx
);
5447 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5451 texture_image_multisample(ctx
, 3, texObj
, target
, samples
,
5452 internalformat
, width
, height
, depth
,
5453 fixedsamplelocations
, GL_FALSE
,
5454 "glTexImage3DMultisample");
5458 valid_texstorage_ms_parameters(GLsizei width
, GLsizei height
, GLsizei depth
,
5459 GLsizei samples
, unsigned dims
)
5461 GET_CURRENT_CONTEXT(ctx
);
5463 if (!_mesa_valid_tex_storage_dim(width
, height
, depth
)) {
5464 _mesa_error(ctx
, GL_INVALID_VALUE
,
5465 "glTexStorage%uDMultisample(width=%d,height=%d,depth=%d)",
5466 dims
, width
, height
, depth
);
5473 _mesa_TexStorage2DMultisample(GLenum target
, GLsizei samples
,
5474 GLenum internalformat
, GLsizei width
,
5475 GLsizei height
, GLboolean fixedsamplelocations
)
5477 struct gl_texture_object
*texObj
;
5478 GET_CURRENT_CONTEXT(ctx
);
5480 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5484 if (!valid_texstorage_ms_parameters(width
, height
, 1, samples
, 2))
5487 texture_image_multisample(ctx
, 2, texObj
, target
, samples
,
5488 internalformat
, width
, height
, 1,
5489 fixedsamplelocations
, GL_TRUE
,
5490 "glTexStorage2DMultisample");
5494 _mesa_TexStorage3DMultisample(GLenum target
, GLsizei samples
,
5495 GLenum internalformat
, GLsizei width
,
5496 GLsizei height
, GLsizei depth
,
5497 GLboolean fixedsamplelocations
)
5499 struct gl_texture_object
*texObj
;
5500 GET_CURRENT_CONTEXT(ctx
);
5502 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5506 if (!valid_texstorage_ms_parameters(width
, height
, depth
, samples
, 3))
5509 texture_image_multisample(ctx
, 3, texObj
, target
, samples
,
5510 internalformat
, width
, height
, depth
,
5511 fixedsamplelocations
, GL_TRUE
,
5512 "glTexStorage3DMultisample");
5516 _mesa_TextureStorage2DMultisample(GLuint texture
, GLsizei samples
,
5517 GLenum internalformat
, GLsizei width
,
5519 GLboolean fixedsamplelocations
)
5521 struct gl_texture_object
*texObj
;
5522 GET_CURRENT_CONTEXT(ctx
);
5524 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
5525 "glTextureStorage2DMultisample");
5529 if (!valid_texstorage_ms_parameters(width
, height
, 1, samples
, 2))
5532 texture_image_multisample(ctx
, 2, texObj
, texObj
->Target
, samples
,
5533 internalformat
, width
, height
, 1,
5534 fixedsamplelocations
, GL_TRUE
,
5535 "glTextureStorage2DMultisample");
5539 _mesa_TextureStorage3DMultisample(GLuint texture
, GLsizei samples
,
5540 GLenum internalformat
, GLsizei width
,
5541 GLsizei height
, GLsizei depth
,
5542 GLboolean fixedsamplelocations
)
5544 struct gl_texture_object
*texObj
;
5545 GET_CURRENT_CONTEXT(ctx
);
5547 /* Get the texture object by Name. */
5548 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
5549 "glTextureStorage3DMultisample");
5553 if (!valid_texstorage_ms_parameters(width
, height
, depth
, samples
, 3))
5556 texture_image_multisample(ctx
, 3, texObj
, texObj
->Target
, samples
,
5557 internalformat
, width
, height
, depth
,
5558 fixedsamplelocations
, GL_TRUE
,
5559 "glTextureStorage3DMultisample");