2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
29 * Texture image-related functions.
34 #include "bufferobj.h"
38 #include "framebuffer.h"
43 #include "multisample.h"
44 #include "pixelstore.h"
46 #include "texcompress.h"
47 #include "texcompress_cpal.h"
51 #include "texstorage.h"
52 #include "textureview.h"
54 #include "glformats.h"
60 * State changes which we care about for glCopyTex[Sub]Image() calls.
61 * In particular, we care about pixel transfer state and buffer state
62 * (such as glReadBuffer to make sure we read from the right renderbuffer).
64 #define NEW_COPY_TEX_STATE (_NEW_BUFFERS | _NEW_PIXEL)
67 * Returns a corresponding internal floating point format for a given base
68 * format as specifed by OES_texture_float. In case of GL_FLOAT, the internal
69 * format needs to be a 32 bit component and in case of GL_HALF_FLOAT_OES it
70 * needs to be a 16 bit component.
72 * For example, given base format GL_RGBA, type GL_FLOAT return GL_RGBA32F_ARB.
75 adjust_for_oes_float_texture(const struct gl_context
*ctx
,
76 GLenum format
, GLenum type
)
80 if (ctx
->Extensions
.OES_texture_float
) {
87 return GL_ALPHA32F_ARB
;
89 return GL_LUMINANCE32F_ARB
;
90 case GL_LUMINANCE_ALPHA
:
91 return GL_LUMINANCE_ALPHA32F_ARB
;
98 case GL_HALF_FLOAT_OES
:
99 if (ctx
->Extensions
.OES_texture_half_float
) {
106 return GL_ALPHA16F_ARB
;
108 return GL_LUMINANCE16F_ARB
;
109 case GL_LUMINANCE_ALPHA
:
110 return GL_LUMINANCE_ALPHA16F_ARB
;
126 * Install gl_texture_image in a gl_texture_object according to the target
127 * and level parameters.
129 * \param tObj texture object.
130 * \param target texture target.
131 * \param level image level.
132 * \param texImage texture image.
135 set_tex_image(struct gl_texture_object
*tObj
,
136 GLenum target
, GLint level
,
137 struct gl_texture_image
*texImage
)
139 const GLuint face
= _mesa_tex_target_to_face(target
);
143 if (target
== GL_TEXTURE_RECTANGLE_NV
|| target
== GL_TEXTURE_EXTERNAL_OES
)
146 tObj
->Image
[face
][level
] = texImage
;
148 /* Set the 'back' pointer */
149 texImage
->TexObject
= tObj
;
150 texImage
->Level
= level
;
151 texImage
->Face
= face
;
156 * Allocate a texture image structure.
158 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
161 * \return a pointer to gl_texture_image struct with all fields initialized to
164 struct gl_texture_image
*
165 _mesa_new_texture_image( struct gl_context
*ctx
)
168 return CALLOC_STRUCT(gl_texture_image
);
173 * Free a gl_texture_image and associated data.
174 * This function is a fallback called via ctx->Driver.DeleteTextureImage().
176 * \param texImage texture image.
178 * Free the texture image structure and the associated image data.
181 _mesa_delete_texture_image(struct gl_context
*ctx
,
182 struct gl_texture_image
*texImage
)
184 /* Free texImage->Data and/or any other driver-specific texture
187 assert(ctx
->Driver
.FreeTextureImageBuffer
);
188 ctx
->Driver
.FreeTextureImageBuffer( ctx
, texImage
);
194 * Test if a target is a proxy target.
196 * \param target texture target.
198 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
201 _mesa_is_proxy_texture(GLenum target
)
204 static const GLenum targets
[] = {
208 GL_PROXY_TEXTURE_CUBE_MAP
,
209 GL_PROXY_TEXTURE_RECTANGLE
,
210 GL_PROXY_TEXTURE_1D_ARRAY
,
211 GL_PROXY_TEXTURE_2D_ARRAY
,
212 GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
,
213 GL_PROXY_TEXTURE_2D_MULTISAMPLE
,
214 GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
217 * NUM_TEXTURE_TARGETS should match number of terms above, except there's no
218 * proxy for GL_TEXTURE_BUFFER and GL_TEXTURE_EXTERNAL_OES.
220 STATIC_ASSERT(NUM_TEXTURE_TARGETS
== ARRAY_SIZE(targets
) + 2);
222 for (i
= 0; i
< ARRAY_SIZE(targets
); ++i
)
223 if (target
== targets
[i
])
230 * Test if a target is an array target.
232 * \param target texture target.
234 * \return true if the target is an array target, false otherwise.
237 _mesa_is_array_texture(GLenum target
)
240 case GL_TEXTURE_1D_ARRAY
:
241 case GL_TEXTURE_2D_ARRAY
:
242 case GL_TEXTURE_CUBE_MAP_ARRAY
:
243 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
251 * Test if a target is a cube map.
253 * \param target texture target.
255 * \return true if the target is a cube map, false otherwise.
258 _mesa_is_cube_map_texture(GLenum target
)
261 case GL_TEXTURE_CUBE_MAP
:
262 case GL_TEXTURE_CUBE_MAP_ARRAY
:
270 * Return the proxy target which corresponds to the given texture target
273 proxy_target(GLenum target
)
277 case GL_PROXY_TEXTURE_1D
:
278 return GL_PROXY_TEXTURE_1D
;
280 case GL_PROXY_TEXTURE_2D
:
281 return GL_PROXY_TEXTURE_2D
;
283 case GL_PROXY_TEXTURE_3D
:
284 return GL_PROXY_TEXTURE_3D
;
285 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
286 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
287 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
288 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
289 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
290 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
291 case GL_TEXTURE_CUBE_MAP
:
292 case GL_PROXY_TEXTURE_CUBE_MAP
:
293 return GL_PROXY_TEXTURE_CUBE_MAP
;
294 case GL_TEXTURE_RECTANGLE_NV
:
295 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
296 return GL_PROXY_TEXTURE_RECTANGLE_NV
;
297 case GL_TEXTURE_1D_ARRAY_EXT
:
298 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
299 return GL_PROXY_TEXTURE_1D_ARRAY_EXT
;
300 case GL_TEXTURE_2D_ARRAY_EXT
:
301 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
302 return GL_PROXY_TEXTURE_2D_ARRAY_EXT
;
303 case GL_TEXTURE_CUBE_MAP_ARRAY
:
304 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
305 return GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
;
306 case GL_TEXTURE_2D_MULTISAMPLE
:
307 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
308 return GL_PROXY_TEXTURE_2D_MULTISAMPLE
;
309 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
310 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
311 return GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
;
313 _mesa_problem(NULL
, "unexpected target in proxy_target()");
322 * Get a texture image pointer from a texture object, given a texture
323 * target and mipmap level. The target and level parameters should
324 * have already been error-checked.
326 * \param texObj texture unit.
327 * \param target texture target.
328 * \param level image level.
330 * \return pointer to the texture image structure, or NULL on failure.
332 struct gl_texture_image
*
333 _mesa_select_tex_image(const struct gl_texture_object
*texObj
,
334 GLenum target
, GLint level
)
336 const GLuint face
= _mesa_tex_target_to_face(target
);
340 assert(level
< MAX_TEXTURE_LEVELS
);
342 return texObj
->Image
[face
][level
];
347 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
348 * it and install it. Only return NULL if passed a bad parameter or run
351 struct gl_texture_image
*
352 _mesa_get_tex_image(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
353 GLenum target
, GLint level
)
355 struct gl_texture_image
*texImage
;
360 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
362 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
364 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture image allocation");
368 set_tex_image(texObj
, target
, level
, texImage
);
376 * Return pointer to the specified proxy texture image.
377 * Note that proxy textures are per-context, not per-texture unit.
378 * \return pointer to texture image or NULL if invalid target, invalid
379 * level, or out of memory.
381 static struct gl_texture_image
*
382 get_proxy_tex_image(struct gl_context
*ctx
, GLenum target
, GLint level
)
384 struct gl_texture_image
*texImage
;
391 case GL_PROXY_TEXTURE_1D
:
392 if (level
>= ctx
->Const
.MaxTextureLevels
)
394 texIndex
= TEXTURE_1D_INDEX
;
396 case GL_PROXY_TEXTURE_2D
:
397 if (level
>= ctx
->Const
.MaxTextureLevels
)
399 texIndex
= TEXTURE_2D_INDEX
;
401 case GL_PROXY_TEXTURE_3D
:
402 if (level
>= ctx
->Const
.Max3DTextureLevels
)
404 texIndex
= TEXTURE_3D_INDEX
;
406 case GL_PROXY_TEXTURE_CUBE_MAP
:
407 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
409 texIndex
= TEXTURE_CUBE_INDEX
;
411 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
414 texIndex
= TEXTURE_RECT_INDEX
;
416 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
417 if (level
>= ctx
->Const
.MaxTextureLevels
)
419 texIndex
= TEXTURE_1D_ARRAY_INDEX
;
421 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
422 if (level
>= ctx
->Const
.MaxTextureLevels
)
424 texIndex
= TEXTURE_2D_ARRAY_INDEX
;
426 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
427 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
429 texIndex
= TEXTURE_CUBE_ARRAY_INDEX
;
431 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
434 texIndex
= TEXTURE_2D_MULTISAMPLE_INDEX
;
436 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
439 texIndex
= TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
;
445 texImage
= ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
];
447 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
449 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
452 ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
] = texImage
;
453 /* Set the 'back' pointer */
454 texImage
->TexObject
= ctx
->Texture
.ProxyTex
[texIndex
];
461 * Get the maximum number of allowed mipmap levels.
463 * \param ctx GL context.
464 * \param target texture target.
466 * \return the maximum number of allowed mipmap levels for the given
467 * texture target, or zero if passed a bad target.
472 _mesa_max_texture_levels(struct gl_context
*ctx
, GLenum target
)
476 case GL_PROXY_TEXTURE_1D
:
478 case GL_PROXY_TEXTURE_2D
:
479 return ctx
->Const
.MaxTextureLevels
;
481 case GL_PROXY_TEXTURE_3D
:
482 return ctx
->Const
.Max3DTextureLevels
;
483 case GL_TEXTURE_CUBE_MAP
:
484 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
485 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
486 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
487 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
488 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
489 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
490 case GL_PROXY_TEXTURE_CUBE_MAP
:
491 return ctx
->Extensions
.ARB_texture_cube_map
492 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
493 case GL_TEXTURE_RECTANGLE_NV
:
494 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
495 return ctx
->Extensions
.NV_texture_rectangle
? 1 : 0;
496 case GL_TEXTURE_1D_ARRAY_EXT
:
497 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
498 case GL_TEXTURE_2D_ARRAY_EXT
:
499 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
500 return ctx
->Extensions
.EXT_texture_array
501 ? ctx
->Const
.MaxTextureLevels
: 0;
502 case GL_TEXTURE_CUBE_MAP_ARRAY
:
503 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
504 return ctx
->Extensions
.ARB_texture_cube_map_array
505 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
506 case GL_TEXTURE_BUFFER
:
507 return (_mesa_has_ARB_texture_buffer_object(ctx
) ||
508 _mesa_has_OES_texture_buffer(ctx
)) ? 1 : 0;
509 case GL_TEXTURE_2D_MULTISAMPLE
:
510 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
511 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
512 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
513 return (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles31(ctx
))
514 && ctx
->Extensions
.ARB_texture_multisample
516 case GL_TEXTURE_EXTERNAL_OES
:
519 return 0; /* bad target */
525 * Return number of dimensions per mipmap level for the given texture target.
528 _mesa_get_texture_dimensions(GLenum target
)
532 case GL_PROXY_TEXTURE_1D
:
535 case GL_TEXTURE_RECTANGLE
:
536 case GL_TEXTURE_CUBE_MAP
:
537 case GL_PROXY_TEXTURE_2D
:
538 case GL_PROXY_TEXTURE_RECTANGLE
:
539 case GL_PROXY_TEXTURE_CUBE_MAP
:
540 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
541 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
542 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
543 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
544 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
545 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
546 case GL_TEXTURE_1D_ARRAY
:
547 case GL_PROXY_TEXTURE_1D_ARRAY
:
548 case GL_TEXTURE_EXTERNAL_OES
:
549 case GL_TEXTURE_2D_MULTISAMPLE
:
550 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
553 case GL_PROXY_TEXTURE_3D
:
554 case GL_TEXTURE_2D_ARRAY
:
555 case GL_PROXY_TEXTURE_2D_ARRAY
:
556 case GL_TEXTURE_CUBE_MAP_ARRAY
:
557 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
558 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
559 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
561 case GL_TEXTURE_BUFFER
:
564 _mesa_problem(NULL
, "invalid target 0x%x in get_texture_dimensions()",
572 * Check if a texture target can have more than one layer.
575 _mesa_tex_target_is_layered(GLenum target
)
579 case GL_PROXY_TEXTURE_1D
:
581 case GL_PROXY_TEXTURE_2D
:
582 case GL_TEXTURE_RECTANGLE
:
583 case GL_PROXY_TEXTURE_RECTANGLE
:
584 case GL_TEXTURE_2D_MULTISAMPLE
:
585 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
586 case GL_TEXTURE_BUFFER
:
587 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
588 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
589 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
590 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
591 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
592 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
593 case GL_TEXTURE_EXTERNAL_OES
:
597 case GL_PROXY_TEXTURE_3D
:
598 case GL_TEXTURE_CUBE_MAP
:
599 case GL_PROXY_TEXTURE_CUBE_MAP
:
600 case GL_TEXTURE_1D_ARRAY
:
601 case GL_PROXY_TEXTURE_1D_ARRAY
:
602 case GL_TEXTURE_2D_ARRAY
:
603 case GL_PROXY_TEXTURE_2D_ARRAY
:
604 case GL_TEXTURE_CUBE_MAP_ARRAY
:
605 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
606 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
607 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
611 assert(!"Invalid texture target.");
618 * Return the number of layers present in the given level of an array,
619 * cubemap or 3D texture. If the texture is not layered return zero.
622 _mesa_get_texture_layers(const struct gl_texture_object
*texObj
, GLint level
)
624 assert(level
>= 0 && level
< MAX_TEXTURE_LEVELS
);
626 switch (texObj
->Target
) {
629 case GL_TEXTURE_RECTANGLE
:
630 case GL_TEXTURE_2D_MULTISAMPLE
:
631 case GL_TEXTURE_BUFFER
:
632 case GL_TEXTURE_EXTERNAL_OES
:
635 case GL_TEXTURE_CUBE_MAP
:
638 case GL_TEXTURE_1D_ARRAY
: {
639 struct gl_texture_image
*img
= texObj
->Image
[0][level
];
640 return img
? img
->Height
: 0;
644 case GL_TEXTURE_2D_ARRAY
:
645 case GL_TEXTURE_CUBE_MAP_ARRAY
:
646 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
: {
647 struct gl_texture_image
*img
= texObj
->Image
[0][level
];
648 return img
? img
->Depth
: 0;
652 assert(!"Invalid texture target.");
659 * Return the maximum number of mipmap levels for the given target
660 * and the dimensions.
661 * The dimensions are expected not to include the border.
664 _mesa_get_tex_max_num_levels(GLenum target
, GLsizei width
, GLsizei height
,
671 case GL_TEXTURE_1D_ARRAY
:
672 case GL_PROXY_TEXTURE_1D
:
673 case GL_PROXY_TEXTURE_1D_ARRAY
:
676 case GL_TEXTURE_CUBE_MAP
:
677 case GL_TEXTURE_CUBE_MAP_ARRAY
:
678 case GL_PROXY_TEXTURE_CUBE_MAP
:
679 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
683 case GL_TEXTURE_2D_ARRAY
:
684 case GL_PROXY_TEXTURE_2D
:
685 case GL_PROXY_TEXTURE_2D_ARRAY
:
686 size
= MAX2(width
, height
);
689 case GL_PROXY_TEXTURE_3D
:
690 size
= MAX3(width
, height
, depth
);
692 case GL_TEXTURE_RECTANGLE
:
693 case GL_TEXTURE_EXTERNAL_OES
:
694 case GL_TEXTURE_2D_MULTISAMPLE
:
695 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
696 case GL_PROXY_TEXTURE_RECTANGLE
:
697 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
698 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
705 return _mesa_logbase2(size
) + 1;
709 #if 000 /* not used anymore */
711 * glTexImage[123]D can accept a NULL image pointer. In this case we
712 * create a texture image with unspecified image contents per the OpenGL
716 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
718 const GLint components
= _mesa_components_in_format(format
);
719 const GLint numPixels
= width
* height
* depth
;
720 GLubyte
*data
= (GLubyte
*) malloc(numPixels
* components
* sizeof(GLubyte
));
724 * Let's see if anyone finds this. If glTexImage2D() is called with
725 * a NULL image pointer then load the texture image with something
726 * interesting instead of leaving it indeterminate.
729 static const char message
[8][32] = {
733 " X X XXXX XXX XXXXX ",
736 " X X XXXXX XXX X X ",
740 GLubyte
*imgPtr
= data
;
742 for (h
= 0; h
< depth
; h
++) {
743 for (i
= 0; i
< height
; i
++) {
744 GLint srcRow
= 7 - (i
% 8);
745 for (j
= 0; j
< width
; j
++) {
746 GLint srcCol
= j
% 32;
747 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
748 for (k
= 0; k
< components
; k
++) {
764 * Set the size and format-related fields of a gl_texture_image struct
765 * to zero. This is used when a proxy texture test fails.
768 clear_teximage_fields(struct gl_texture_image
*img
)
771 img
->_BaseFormat
= 0;
772 img
->InternalFormat
= 0;
783 img
->TexFormat
= MESA_FORMAT_NONE
;
785 img
->FixedSampleLocations
= GL_TRUE
;
790 * Initialize basic fields of the gl_texture_image struct.
792 * \param ctx GL context.
793 * \param img texture image structure to be initialized.
794 * \param width image width.
795 * \param height image height.
796 * \param depth image depth.
797 * \param border image border.
798 * \param internalFormat internal format.
799 * \param format the actual hardware format (one of MESA_FORMAT_*)
800 * \param numSamples number of samples per texel, or zero for non-MS.
801 * \param fixedSampleLocations are sample locations fixed?
803 * Fills in the fields of \p img with the given information.
804 * Note: width, height and depth include the border.
807 init_teximage_fields_ms(struct gl_context
*ctx
,
808 struct gl_texture_image
*img
,
809 GLsizei width
, GLsizei height
, GLsizei depth
,
810 GLint border
, GLenum internalFormat
,
812 GLuint numSamples
, GLboolean fixedSampleLocations
)
820 target
= img
->TexObject
->Target
;
821 img
->_BaseFormat
= _mesa_base_tex_format( ctx
, internalFormat
);
822 assert(img
->_BaseFormat
!= -1);
823 img
->InternalFormat
= internalFormat
;
824 img
->Border
= border
;
826 img
->Height
= height
;
829 img
->Width2
= width
- 2 * border
; /* == 1 << img->WidthLog2; */
830 img
->WidthLog2
= _mesa_logbase2(img
->Width2
);
833 img
->FixedSampleLocations
= GL_TRUE
;
837 case GL_TEXTURE_BUFFER
:
838 case GL_PROXY_TEXTURE_1D
:
850 case GL_TEXTURE_1D_ARRAY
:
851 case GL_PROXY_TEXTURE_1D_ARRAY
:
852 img
->Height2
= height
; /* no border */
853 img
->HeightLog2
= 0; /* not used */
861 case GL_TEXTURE_RECTANGLE
:
862 case GL_TEXTURE_CUBE_MAP
:
863 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
864 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
865 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
866 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
867 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
868 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
869 case GL_TEXTURE_EXTERNAL_OES
:
870 case GL_PROXY_TEXTURE_2D
:
871 case GL_PROXY_TEXTURE_RECTANGLE
:
872 case GL_PROXY_TEXTURE_CUBE_MAP
:
873 case GL_TEXTURE_2D_MULTISAMPLE
:
874 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
875 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
876 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
883 case GL_TEXTURE_2D_ARRAY
:
884 case GL_PROXY_TEXTURE_2D_ARRAY
:
885 case GL_TEXTURE_CUBE_MAP_ARRAY
:
886 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
887 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
888 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
889 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
890 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
891 img
->Depth2
= depth
; /* no border */
892 img
->DepthLog2
= 0; /* not used */
895 case GL_PROXY_TEXTURE_3D
:
896 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
897 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
898 img
->Depth2
= depth
- 2 * border
; /* == 1 << img->DepthLog2; */
899 img
->DepthLog2
= _mesa_logbase2(img
->Depth2
);
902 _mesa_problem(NULL
, "invalid target 0x%x in _mesa_init_teximage_fields()",
907 _mesa_get_tex_max_num_levels(target
,
908 img
->Width2
, img
->Height2
, img
->Depth2
);
909 img
->TexFormat
= format
;
910 img
->NumSamples
= numSamples
;
911 img
->FixedSampleLocations
= fixedSampleLocations
;
916 _mesa_init_teximage_fields(struct gl_context
*ctx
,
917 struct gl_texture_image
*img
,
918 GLsizei width
, GLsizei height
, GLsizei depth
,
919 GLint border
, GLenum internalFormat
,
922 init_teximage_fields_ms(ctx
, img
, width
, height
, depth
, border
,
923 internalFormat
, format
, 0, GL_TRUE
);
928 * Free and clear fields of the gl_texture_image struct.
930 * \param ctx GL context.
931 * \param texImage texture image structure to be cleared.
933 * After the call, \p texImage will have no data associated with it. Its
934 * fields are cleared so that its parent object will test incomplete.
937 _mesa_clear_texture_image(struct gl_context
*ctx
,
938 struct gl_texture_image
*texImage
)
940 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
941 clear_teximage_fields(texImage
);
946 * Check the width, height, depth and border of a texture image are legal.
947 * Used by all the glTexImage, glCompressedTexImage and glCopyTexImage
949 * The target and level parameters will have already been validated.
950 * \return GL_TRUE if size is OK, GL_FALSE otherwise.
953 _mesa_legal_texture_dimensions(struct gl_context
*ctx
, GLenum target
,
954 GLint level
, GLint width
, GLint height
,
955 GLint depth
, GLint border
)
961 case GL_PROXY_TEXTURE_1D
:
962 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1); /* level zero size */
963 maxSize
>>= level
; /* level size */
964 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
966 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
967 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
973 case GL_PROXY_TEXTURE_2D
:
974 case GL_TEXTURE_2D_MULTISAMPLE
:
975 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
976 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
978 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
980 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
982 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
983 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
985 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
991 case GL_PROXY_TEXTURE_3D
:
992 maxSize
= 1 << (ctx
->Const
.Max3DTextureLevels
- 1);
994 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
996 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
998 if (depth
< 2 * border
|| depth
> 2 * border
+ maxSize
)
1000 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1001 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1003 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1005 if (depth
> 0 && !_mesa_is_pow_two(depth
- 2 * border
))
1010 case GL_TEXTURE_RECTANGLE_NV
:
1011 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1014 maxSize
= ctx
->Const
.MaxTextureRectSize
;
1015 if (width
< 0 || width
> maxSize
)
1017 if (height
< 0 || height
> maxSize
)
1021 case GL_TEXTURE_CUBE_MAP
:
1022 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1023 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1024 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1025 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1026 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1027 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1028 case GL_PROXY_TEXTURE_CUBE_MAP
:
1029 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1031 if (width
!= height
)
1033 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1035 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1037 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1038 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1040 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1045 case GL_TEXTURE_1D_ARRAY_EXT
:
1046 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1047 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1049 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1051 if (height
< 0 || height
> ctx
->Const
.MaxArrayTextureLayers
)
1053 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1054 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1059 case GL_TEXTURE_2D_ARRAY_EXT
:
1060 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1061 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1062 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1063 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
1065 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1067 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1069 if (depth
< 0 || depth
> ctx
->Const
.MaxArrayTextureLayers
)
1071 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1072 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1074 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1079 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1080 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1081 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
1082 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
1084 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
1086 if (depth
< 0 || depth
> ctx
->Const
.MaxArrayTextureLayers
|| depth
% 6)
1088 if (width
!= height
)
1090 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
1092 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
) {
1093 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
1095 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
1100 _mesa_problem(ctx
, "Invalid target in _mesa_legal_texture_dimensions()");
1106 error_check_subtexture_negative_dimensions(struct gl_context
*ctx
,
1115 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width=%d)", func
, subWidth
);
1119 if (dims
> 1 && subHeight
< 0) {
1120 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height=%d)", func
, subHeight
);
1124 if (dims
> 2 && subDepth
< 0) {
1125 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(depth=%d)", func
, subDepth
);
1133 * Do error checking of xoffset, yoffset, zoffset, width, height and depth
1134 * for glTexSubImage, glCopyTexSubImage and glCompressedTexSubImage.
1135 * \param destImage the destination texture image.
1136 * \return GL_TRUE if error found, GL_FALSE otherwise.
1139 error_check_subtexture_dimensions(struct gl_context
*ctx
, GLuint dims
,
1140 const struct gl_texture_image
*destImage
,
1141 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1142 GLsizei subWidth
, GLsizei subHeight
,
1143 GLsizei subDepth
, const char *func
)
1145 const GLenum target
= destImage
->TexObject
->Target
;
1148 /* check xoffset and width */
1149 if (xoffset
< - (GLint
) destImage
->Border
) {
1150 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(xoffset)", func
);
1154 if (xoffset
+ subWidth
> (GLint
) destImage
->Width
) {
1155 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(xoffset+width)", func
);
1159 /* check yoffset and height */
1161 GLint yBorder
= (target
== GL_TEXTURE_1D_ARRAY
) ? 0 : destImage
->Border
;
1162 if (yoffset
< -yBorder
) {
1163 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(yoffset)", func
);
1166 if (yoffset
+ subHeight
> (GLint
) destImage
->Height
) {
1167 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(yoffset+height)", func
);
1172 /* check zoffset and depth */
1175 GLint zBorder
= (target
== GL_TEXTURE_2D_ARRAY
||
1176 target
== GL_TEXTURE_CUBE_MAP_ARRAY
) ?
1177 0 : destImage
->Border
;
1179 if (zoffset
< -zBorder
) {
1180 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(zoffset)", func
);
1184 depth
= (GLint
) destImage
->Depth
;
1185 if (target
== GL_TEXTURE_CUBE_MAP
)
1187 if (zoffset
+ subDepth
> depth
) {
1188 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(zoffset+depth)", func
);
1194 * The OpenGL spec (and GL_ARB_texture_compression) says only whole
1195 * compressed texture images can be updated. But, that restriction may be
1196 * relaxed for particular compressed formats. At this time, all the
1197 * compressed formats supported by Mesa allow sub-textures to be updated
1198 * along compressed block boundaries.
1200 _mesa_get_format_block_size_3d(destImage
->TexFormat
, &bw
, &bh
, &bd
);
1202 if (bw
!= 1 || bh
!= 1 || bd
!= 1) {
1203 /* offset must be multiple of block size */
1204 if ((xoffset
% bw
!= 0) || (yoffset
% bh
!= 0) || (zoffset
% bd
!= 0)) {
1205 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1206 "%s(xoffset = %d, yoffset = %d, zoffset = %d)",
1207 func
, xoffset
, yoffset
, zoffset
);
1211 /* The size must be a multiple of bw x bh, or we must be using a
1212 * offset+size that exactly hits the edge of the image. This
1213 * is important for small mipmap levels (1x1, 2x1, etc) and for
1216 if ((subWidth
% bw
!= 0) &&
1217 (xoffset
+ subWidth
!= (GLint
) destImage
->Width
)) {
1218 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1219 "%s(width = %d)", func
, subWidth
);
1223 if ((subHeight
% bh
!= 0) &&
1224 (yoffset
+ subHeight
!= (GLint
) destImage
->Height
)) {
1225 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1226 "%s(height = %d)", func
, subHeight
);
1230 if ((subDepth
% bd
!= 0) &&
1231 (zoffset
+ subDepth
!= (GLint
) destImage
->Depth
)) {
1232 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1233 "%s(depth = %d)", func
, subDepth
);
1245 * This is the fallback for Driver.TestProxyTexImage() for doing device-
1246 * specific texture image size checks.
1248 * A hardware driver might override this function if, for example, the
1249 * max 3D texture size is 512x512x64 (i.e. not a cube).
1251 * Note that width, height, depth == 0 is not an error. However, a
1252 * texture with zero width/height/depth will be considered "incomplete"
1253 * and texturing will effectively be disabled.
1255 * \param target any texture target/type
1256 * \param numLevels number of mipmap levels in the texture or 0 if not known
1257 * \param level as passed to glTexImage
1258 * \param format the MESA_FORMAT_x for the tex image
1259 * \param numSamples number of samples per texel
1260 * \param width as passed to glTexImage
1261 * \param height as passed to glTexImage
1262 * \param depth as passed to glTexImage
1263 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1266 _mesa_test_proxy_teximage(struct gl_context
*ctx
, GLenum target
,
1267 GLuint numLevels
, GLint level
,
1268 mesa_format format
, GLuint numSamples
,
1269 GLint width
, GLint height
, GLint depth
)
1271 /* We just check if the image size is less than MaxTextureMbytes.
1272 * Some drivers may do more specific checks.
1274 uint64_t bytes
= _mesa_format_image_size64(format
, width
, height
, depth
);
1275 uint64_t mbytes
= bytes
/ (1024 * 1024); /* convert to MB */
1276 mbytes
*= _mesa_num_tex_faces(target
);
1277 return mbytes
<= (uint64_t) ctx
->Const
.MaxTextureMbytes
;
1282 * Return true if the format is only valid for glCompressedTexImage.
1285 compressedteximage_only_format(const struct gl_context
*ctx
, GLenum format
)
1288 case GL_ETC1_RGB8_OES
:
1289 case GL_PALETTE4_RGB8_OES
:
1290 case GL_PALETTE4_RGBA8_OES
:
1291 case GL_PALETTE4_R5_G6_B5_OES
:
1292 case GL_PALETTE4_RGBA4_OES
:
1293 case GL_PALETTE4_RGB5_A1_OES
:
1294 case GL_PALETTE8_RGB8_OES
:
1295 case GL_PALETTE8_RGBA8_OES
:
1296 case GL_PALETTE8_R5_G6_B5_OES
:
1297 case GL_PALETTE8_RGBA4_OES
:
1298 case GL_PALETTE8_RGB5_A1_OES
:
1306 * Return true if the format doesn't support online compression.
1309 _mesa_format_no_online_compression(const struct gl_context
*ctx
, GLenum format
)
1311 return _mesa_is_astc_format(format
) ||
1312 _mesa_is_etc2_format(format
) ||
1313 compressedteximage_only_format(ctx
, format
);
1316 /* Writes to an GL error pointer if non-null and returns whether or not the
1317 * error is GL_NO_ERROR */
1319 write_error(GLenum
*err_ptr
, GLenum error
)
1324 return error
== GL_NO_ERROR
;
1328 * Helper function to determine whether a target and specific compression
1329 * format are supported. The error parameter returns GL_NO_ERROR if the
1330 * target can be compressed. Otherwise it returns either GL_INVALID_OPERATION
1331 * or GL_INVALID_ENUM, whichever is more appropriate.
1334 _mesa_target_can_be_compressed(const struct gl_context
*ctx
, GLenum target
,
1335 GLenum intFormat
, GLenum
*error
)
1337 GLboolean target_can_be_compresed
= GL_FALSE
;
1338 mesa_format format
= _mesa_glenum_to_compressed_format(intFormat
);
1339 enum mesa_format_layout layout
= _mesa_get_format_layout(format
);
1343 case GL_PROXY_TEXTURE_2D
:
1344 target_can_be_compresed
= GL_TRUE
; /* true for any compressed format so far */
1346 case GL_PROXY_TEXTURE_CUBE_MAP
:
1347 case GL_TEXTURE_CUBE_MAP
:
1348 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1349 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1350 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1351 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1352 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1353 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1354 target_can_be_compresed
= ctx
->Extensions
.ARB_texture_cube_map
;
1356 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1357 case GL_TEXTURE_2D_ARRAY_EXT
:
1358 target_can_be_compresed
= ctx
->Extensions
.EXT_texture_array
;
1360 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1361 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1362 /* From the KHR_texture_compression_astc_hdr spec:
1364 * Add a second new column "3D Tex." which is empty for all non-ASTC
1365 * formats. If only the LDR profile is supported by the
1366 * implementation, this column is also empty for all ASTC formats. If
1367 * both the LDR and HDR profiles are supported only, this column is
1368 * checked for all ASTC formats.
1370 * Add a third new column "Cube Map Array Tex." which is empty for all
1371 * non-ASTC formats, and checked for all ASTC formats.
1375 * 'An INVALID_OPERATION error is generated by CompressedTexImage3D
1376 * if <internalformat> is TEXTURE_CUBE_MAP_ARRAY and the
1377 * "Cube Map Array" column of table 8.19 is *not* checked, or if
1378 * <internalformat> is TEXTURE_3D and the "3D Tex." column of table
1379 * 8.19 is *not* checked'
1381 * The instances of <internalformat> above should say <target>.
1383 * ETC2/EAC formats are the only alternative in GLES and thus such errors
1384 * have already been handled by normal ETC2/EAC behavior.
1387 /* From section 3.8.6, page 146 of OpenGL ES 3.0 spec:
1389 * "The ETC2/EAC texture compression algorithm supports only
1390 * two-dimensional images. If internalformat is an ETC2/EAC format,
1391 * glCompressedTexImage3D will generate an INVALID_OPERATION error if
1392 * target is not TEXTURE_2D_ARRAY."
1394 * This should also be applicable for glTexStorage3D(). Other available
1395 * targets for these functions are: TEXTURE_3D and TEXTURE_CUBE_MAP_ARRAY.
1397 if (layout
== MESA_FORMAT_LAYOUT_ETC2
&& _mesa_is_gles3(ctx
))
1398 return write_error(error
, GL_INVALID_OPERATION
);
1399 target_can_be_compresed
= ctx
->Extensions
.ARB_texture_cube_map_array
;
1403 case MESA_FORMAT_LAYOUT_ETC2
:
1404 /* See ETC2/EAC comment in case GL_TEXTURE_CUBE_MAP_ARRAY. */
1405 if (_mesa_is_gles3(ctx
))
1406 return write_error(error
, GL_INVALID_OPERATION
);
1408 case MESA_FORMAT_LAYOUT_BPTC
:
1409 target_can_be_compresed
= ctx
->Extensions
.ARB_texture_compression_bptc
;
1411 case MESA_FORMAT_LAYOUT_ASTC
:
1412 target_can_be_compresed
=
1413 ctx
->Extensions
.KHR_texture_compression_astc_hdr
;
1415 /* Throw an INVALID_OPERATION error if the target is TEXTURE_3D and
1416 * and the hdr extension is not supported.
1417 * See comment in switch case GL_TEXTURE_CUBE_MAP_ARRAY for more info.
1419 if (!target_can_be_compresed
)
1420 return write_error(error
, GL_INVALID_OPERATION
);
1428 return write_error(error
,
1429 target_can_be_compresed
? GL_NO_ERROR
: GL_INVALID_ENUM
);
1434 * Check if the given texture target value is legal for a
1435 * glTexImage1/2/3D call.
1438 legal_teximage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
1444 case GL_PROXY_TEXTURE_1D
:
1445 return _mesa_is_desktop_gl(ctx
);
1453 case GL_PROXY_TEXTURE_2D
:
1454 return _mesa_is_desktop_gl(ctx
);
1455 case GL_PROXY_TEXTURE_CUBE_MAP
:
1456 return _mesa_is_desktop_gl(ctx
)
1457 && ctx
->Extensions
.ARB_texture_cube_map
;
1458 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1459 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1460 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1461 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1462 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1463 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1464 return ctx
->Extensions
.ARB_texture_cube_map
;
1465 case GL_TEXTURE_RECTANGLE_NV
:
1466 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
1467 return _mesa_is_desktop_gl(ctx
)
1468 && ctx
->Extensions
.NV_texture_rectangle
;
1469 case GL_TEXTURE_1D_ARRAY_EXT
:
1470 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
1471 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1479 case GL_PROXY_TEXTURE_3D
:
1480 return _mesa_is_desktop_gl(ctx
);
1481 case GL_TEXTURE_2D_ARRAY_EXT
:
1482 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1483 || _mesa_is_gles3(ctx
);
1484 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
1485 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1486 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1487 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1488 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1493 _mesa_problem(ctx
, "invalid dims=%u in legal_teximage_target()", dims
);
1500 * Check if the given texture target value is legal for a
1501 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1502 * The difference compared to legal_teximage_target() above is that
1503 * proxy targets are not supported.
1506 legal_texsubimage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
,
1511 return _mesa_is_desktop_gl(ctx
) && target
== GL_TEXTURE_1D
;
1516 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
1517 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
1518 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
1519 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
1520 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
1521 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
1522 return ctx
->Extensions
.ARB_texture_cube_map
;
1523 case GL_TEXTURE_RECTANGLE_NV
:
1524 return _mesa_is_desktop_gl(ctx
)
1525 && ctx
->Extensions
.NV_texture_rectangle
;
1526 case GL_TEXTURE_1D_ARRAY_EXT
:
1527 return _mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
;
1535 case GL_TEXTURE_2D_ARRAY_EXT
:
1536 return (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
)
1537 || _mesa_is_gles3(ctx
);
1538 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1539 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
1540 return ctx
->Extensions
.ARB_texture_cube_map_array
;
1542 /* Table 8.15 of the OpenGL 4.5 core profile spec
1543 * (20141030) says that TEXTURE_CUBE_MAP is valid for TextureSubImage3D
1544 * and CopyTextureSubImage3D.
1546 case GL_TEXTURE_CUBE_MAP
:
1552 _mesa_problem(ctx
, "invalid dims=%u in legal_texsubimage_target()",
1560 * Helper function to determine if a texture object is mutable (in terms
1561 * of GL_ARB_texture_storage).
1564 mutable_tex_object(struct gl_context
*ctx
, GLenum target
)
1566 struct gl_texture_object
*texObj
= _mesa_get_current_tex_object(ctx
, target
);
1570 return !texObj
->Immutable
;
1575 * Return expected size of a compressed texture.
1578 compressed_tex_size(GLsizei width
, GLsizei height
, GLsizei depth
,
1581 mesa_format mesaFormat
= _mesa_glenum_to_compressed_format(glformat
);
1582 return _mesa_format_image_size(mesaFormat
, width
, height
, depth
);
1586 * Verify that a texture format is valid with a particular target
1588 * In particular, textures with base format of \c GL_DEPTH_COMPONENT or
1589 * \c GL_DEPTH_STENCIL are only valid with certain, context dependent texture
1592 * \param ctx GL context
1593 * \param target Texture target
1594 * \param internalFormat Internal format of the texture image
1596 * \returns true if the combination is legal, false otherwise.
1599 _mesa_legal_texture_base_format_for_target(struct gl_context
*ctx
,
1600 GLenum target
, GLenum internalFormat
)
1602 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
1603 || _mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_STENCIL
1604 || _mesa_base_tex_format(ctx
, internalFormat
) == GL_STENCIL_INDEX
) {
1605 /* Section 3.8.3 (Texture Image Specification) of the OpenGL 3.3 Core
1606 * Profile spec says:
1608 * "Textures with a base internal format of DEPTH_COMPONENT or
1609 * DEPTH_STENCIL are supported by texture image specification
1610 * commands only if target is TEXTURE_1D, TEXTURE_2D,
1611 * TEXTURE_1D_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_RECTANGLE,
1612 * TEXTURE_CUBE_MAP, PROXY_TEXTURE_1D, PROXY_TEXTURE_2D,
1613 * PROXY_TEXTURE_1D_ARRAY, PROXY_TEXTURE_2D_ARRAY,
1614 * PROXY_TEXTURE_RECTANGLE, or PROXY_TEXTURE_CUBE_MAP. Using these
1615 * formats in conjunction with any other target will result in an
1616 * INVALID_OPERATION error."
1618 * Cubemaps are only supported with desktop OpenGL version >= 3.0,
1619 * EXT_gpu_shader4, or, on OpenGL ES 2.0+, OES_depth_texture_cube_map.
1621 if (target
!= GL_TEXTURE_1D
&&
1622 target
!= GL_PROXY_TEXTURE_1D
&&
1623 target
!= GL_TEXTURE_2D
&&
1624 target
!= GL_PROXY_TEXTURE_2D
&&
1625 target
!= GL_TEXTURE_1D_ARRAY
&&
1626 target
!= GL_PROXY_TEXTURE_1D_ARRAY
&&
1627 target
!= GL_TEXTURE_2D_ARRAY
&&
1628 target
!= GL_PROXY_TEXTURE_2D_ARRAY
&&
1629 target
!= GL_TEXTURE_RECTANGLE_ARB
&&
1630 target
!= GL_PROXY_TEXTURE_RECTANGLE_ARB
&&
1631 !((_mesa_is_cube_face(target
) ||
1632 target
== GL_TEXTURE_CUBE_MAP
||
1633 target
== GL_PROXY_TEXTURE_CUBE_MAP
) &&
1634 (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_gpu_shader4
1635 || (ctx
->API
== API_OPENGLES2
&& ctx
->Extensions
.OES_depth_texture_cube_map
))) &&
1636 !((target
== GL_TEXTURE_CUBE_MAP_ARRAY
||
1637 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
) &&
1638 ctx
->Extensions
.ARB_texture_cube_map_array
)) {
1647 texture_formats_agree(GLenum internalFormat
,
1650 GLboolean colorFormat
;
1651 GLboolean is_format_depth_or_depthstencil
;
1652 GLboolean is_internalFormat_depth_or_depthstencil
;
1654 /* Even though there are no color-index textures, we still have to support
1655 * uploading color-index data and remapping it to RGB via the
1656 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
1658 const GLboolean indexFormat
= (format
== GL_COLOR_INDEX
);
1660 is_internalFormat_depth_or_depthstencil
=
1661 _mesa_is_depth_format(internalFormat
) ||
1662 _mesa_is_depthstencil_format(internalFormat
);
1664 is_format_depth_or_depthstencil
=
1665 _mesa_is_depth_format(format
) ||
1666 _mesa_is_depthstencil_format(format
);
1668 colorFormat
= _mesa_is_color_format(format
);
1670 if (_mesa_is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
)
1673 if (is_internalFormat_depth_or_depthstencil
!=
1674 is_format_depth_or_depthstencil
)
1677 if (_mesa_is_ycbcr_format(internalFormat
) != _mesa_is_ycbcr_format(format
))
1684 * Test the combination of format, type and internal format arguments of
1685 * different texture operations on GLES.
1687 * \param ctx GL context.
1688 * \param format pixel data format given by the user.
1689 * \param type pixel data type given by the user.
1690 * \param internalFormat internal format given by the user.
1691 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1692 * \param callerName name of the caller function to print in the error message
1694 * \return true if a error is found, false otherwise
1696 * Currently, it is used by texture_error_check() and texsubimage_error_check().
1699 texture_format_error_check_gles(struct gl_context
*ctx
, GLenum format
,
1700 GLenum type
, GLenum internalFormat
,
1701 GLuint dimensions
, const char *callerName
)
1705 if (_mesa_is_gles3(ctx
)) {
1706 err
= _mesa_es3_error_check_format_and_type(ctx
, format
, type
,
1708 if (err
!= GL_NO_ERROR
) {
1709 _mesa_error(ctx
, err
,
1710 "%s(format = %s, type = %s, internalformat = %s)",
1711 callerName
, _mesa_enum_to_string(format
),
1712 _mesa_enum_to_string(type
),
1713 _mesa_enum_to_string(internalFormat
));
1718 err
= _mesa_es_error_check_format_and_type(ctx
, format
, type
, dimensions
);
1719 if (err
!= GL_NO_ERROR
) {
1720 _mesa_error(ctx
, err
, "%s(format = %s, type = %s)",
1721 callerName
, _mesa_enum_to_string(format
),
1722 _mesa_enum_to_string(type
));
1731 * Test the glTexImage[123]D() parameters for errors.
1733 * \param ctx GL context.
1734 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1735 * \param target texture target given by the user (already validated).
1736 * \param level image level given by the user.
1737 * \param internalFormat internal format given by the user.
1738 * \param format pixel data format given by the user.
1739 * \param type pixel data type given by the user.
1740 * \param width image width given by the user.
1741 * \param height image height given by the user.
1742 * \param depth image depth given by the user.
1743 * \param border image border given by the user.
1745 * \return GL_TRUE if a error is found, GL_FALSE otherwise
1747 * Verifies each of the parameters against the constants specified in
1748 * __struct gl_contextRec::Const and the supported extensions, and according
1749 * to the OpenGL specification.
1750 * Note that we don't fully error-check the width, height, depth values
1751 * here. That's done in _mesa_legal_texture_dimensions() which is used
1752 * by several other GL entrypoints. Plus, texture dims have a special
1753 * interaction with proxy textures.
1756 texture_error_check( struct gl_context
*ctx
,
1757 GLuint dimensions
, GLenum target
,
1758 GLint level
, GLint internalFormat
,
1759 GLenum format
, GLenum type
,
1760 GLint width
, GLint height
,
1761 GLint depth
, GLint border
,
1762 const GLvoid
*pixels
)
1766 /* Note: for proxy textures, some error conditions immediately generate
1767 * a GL error in the usual way. But others do not generate a GL error.
1768 * Instead, they cause the width, height, depth, format fields of the
1769 * texture image to be zeroed-out. The GL spec seems to indicate that the
1770 * zero-out behaviour is only used in cases related to memory allocation.
1774 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
1775 _mesa_error(ctx
, GL_INVALID_VALUE
,
1776 "glTexImage%dD(level=%d)", dimensions
, level
);
1781 if (border
< 0 || border
> 1 ||
1782 ((ctx
->API
!= API_OPENGL_COMPAT
||
1783 target
== GL_TEXTURE_RECTANGLE_NV
||
1784 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
1785 _mesa_error(ctx
, GL_INVALID_VALUE
,
1786 "glTexImage%dD(border=%d)", dimensions
, border
);
1790 if (width
< 0 || height
< 0 || depth
< 0) {
1791 _mesa_error(ctx
, GL_INVALID_VALUE
,
1792 "glTexImage%dD(width, height or depth < 0)", dimensions
);
1796 /* Check incoming image format and type */
1797 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1798 if (err
!= GL_NO_ERROR
) {
1799 /* Prior to OpenGL-ES 2.0, an INVALID_VALUE is expected instead of
1800 * INVALID_ENUM. From page 73 OpenGL ES 1.1 spec:
1802 * "Specifying a value for internalformat that is not one of the
1803 * above (acceptable) values generates the error INVALID VALUE."
1805 if (err
== GL_INVALID_ENUM
&& _mesa_is_gles(ctx
) && ctx
->Version
< 20)
1806 err
= GL_INVALID_VALUE
;
1808 _mesa_error(ctx
, err
,
1809 "glTexImage%dD(incompatible format = %s, type = %s)",
1810 dimensions
, _mesa_enum_to_string(format
),
1811 _mesa_enum_to_string(type
));
1815 /* Check internalFormat */
1816 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
1817 _mesa_error(ctx
, GL_INVALID_VALUE
,
1818 "glTexImage%dD(internalFormat=%s)",
1819 dimensions
, _mesa_enum_to_string(internalFormat
));
1823 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1824 * combinations of format, internalFormat, and type that can be used.
1825 * Formats and types that require additional extensions (e.g., GL_FLOAT
1826 * requires GL_OES_texture_float) are filtered elsewhere.
1828 if (_mesa_is_gles(ctx
) &&
1829 texture_format_error_check_gles(ctx
, format
, type
, internalFormat
,
1830 dimensions
, "glTexImage%dD")) {
1834 /* validate the bound PBO, if any */
1835 if (!_mesa_validate_pbo_source(ctx
, dimensions
, &ctx
->Unpack
,
1836 width
, height
, depth
, format
, type
,
1837 INT_MAX
, pixels
, "glTexImage")) {
1841 /* make sure internal format and format basically agree */
1842 if (!texture_formats_agree(internalFormat
, format
)) {
1843 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1844 "glTexImage%dD(incompatible internalFormat = %s, format = %s)",
1845 dimensions
, _mesa_enum_to_string(internalFormat
),
1846 _mesa_enum_to_string(format
));
1850 /* additional checks for ycbcr textures */
1851 if (internalFormat
== GL_YCBCR_MESA
) {
1852 assert(ctx
->Extensions
.MESA_ycbcr_texture
);
1853 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
1854 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
1856 _mesa_snprintf(message
, sizeof(message
),
1857 "glTexImage%dD(format/type YCBCR mismatch)",
1859 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", message
);
1860 return GL_TRUE
; /* error */
1862 if (target
!= GL_TEXTURE_2D
&&
1863 target
!= GL_PROXY_TEXTURE_2D
&&
1864 target
!= GL_TEXTURE_RECTANGLE_NV
&&
1865 target
!= GL_PROXY_TEXTURE_RECTANGLE_NV
) {
1866 _mesa_error(ctx
, GL_INVALID_ENUM
,
1867 "glTexImage%dD(bad target for YCbCr texture)",
1873 _mesa_snprintf(message
, sizeof(message
),
1874 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1875 dimensions
, border
);
1876 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", message
);
1881 /* additional checks for depth textures */
1882 if (!_mesa_legal_texture_base_format_for_target(ctx
, target
, internalFormat
)) {
1883 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1884 "glTexImage%dD(bad target for texture)", dimensions
);
1888 /* additional checks for compressed textures */
1889 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
1891 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
, &err
)) {
1892 _mesa_error(ctx
, err
,
1893 "glTexImage%dD(target can't be compressed)", dimensions
);
1896 if (_mesa_format_no_online_compression(ctx
, internalFormat
)) {
1897 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1898 "glTexImage%dD(no compression for format)", dimensions
);
1902 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1903 "glTexImage%dD(border!=0)", dimensions
);
1908 /* additional checks for integer textures */
1909 if ((ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) &&
1910 (_mesa_is_enum_format_integer(format
) !=
1911 _mesa_is_enum_format_integer(internalFormat
))) {
1912 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1913 "glTexImage%dD(integer/non-integer format mismatch)",
1918 if (!mutable_tex_object(ctx
, target
)) {
1919 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1920 "glTexImage%dD(immutable texture)", dimensions
);
1924 /* if we get here, the parameters are OK */
1930 * Error checking for glCompressedTexImage[123]D().
1931 * Note that the width, height and depth values are not fully error checked
1933 * \return GL_TRUE if a error is found, GL_FALSE otherwise
1936 compressed_texture_error_check(struct gl_context
*ctx
, GLint dimensions
,
1937 GLenum target
, GLint level
,
1938 GLenum internalFormat
, GLsizei width
,
1939 GLsizei height
, GLsizei depth
, GLint border
,
1940 GLsizei imageSize
, const GLvoid
*data
)
1942 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1944 GLenum error
= GL_NO_ERROR
;
1945 char *reason
= ""; /* no error */
1947 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
, &error
)) {
1952 /* This will detect any invalid internalFormat value */
1953 if (!_mesa_is_compressed_format(ctx
, internalFormat
)) {
1954 _mesa_error(ctx
, GL_INVALID_ENUM
,
1955 "glCompressedTexImage%dD(internalFormat=%s)",
1956 dimensions
, _mesa_enum_to_string(internalFormat
));
1960 /* validate the bound PBO, if any */
1961 if (!_mesa_validate_pbo_source_compressed(ctx
, dimensions
, &ctx
->Unpack
,
1963 "glCompressedTexImage")) {
1967 switch (internalFormat
) {
1968 case GL_PALETTE4_RGB8_OES
:
1969 case GL_PALETTE4_RGBA8_OES
:
1970 case GL_PALETTE4_R5_G6_B5_OES
:
1971 case GL_PALETTE4_RGBA4_OES
:
1972 case GL_PALETTE4_RGB5_A1_OES
:
1973 case GL_PALETTE8_RGB8_OES
:
1974 case GL_PALETTE8_RGBA8_OES
:
1975 case GL_PALETTE8_R5_G6_B5_OES
:
1976 case GL_PALETTE8_RGBA4_OES
:
1977 case GL_PALETTE8_RGB5_A1_OES
:
1978 /* check level (note that level should be zero or less!) */
1979 if (level
> 0 || level
< -maxLevels
) {
1981 error
= GL_INVALID_VALUE
;
1985 if (dimensions
!= 2) {
1986 reason
= "compressed paletted textures must be 2D";
1987 error
= GL_INVALID_OPERATION
;
1991 /* Figure out the expected texture size (in bytes). This will be
1992 * checked against the actual size later.
1994 expectedSize
= _mesa_cpal_compressed_size(level
, internalFormat
,
1997 /* This is for the benefit of the TestProxyTexImage below. It expects
1998 * level to be non-negative. OES_compressed_paletted_texture uses a
1999 * weird mechanism where the level specified to glCompressedTexImage2D
2000 * is -(n-1) number of levels in the texture, and the data specifies the
2001 * complete mipmap stack. This is done to ensure the palette is the
2002 * same for all levels.
2009 if (level
< 0 || level
>= maxLevels
) {
2011 error
= GL_INVALID_VALUE
;
2015 /* Figure out the expected texture size (in bytes). This will be
2016 * checked against the actual size later.
2018 expectedSize
= compressed_tex_size(width
, height
, depth
, internalFormat
);
2022 /* This should really never fail */
2023 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
2024 reason
= "internalFormat";
2025 error
= GL_INVALID_ENUM
;
2029 /* No compressed formats support borders at this time */
2031 reason
= "border != 0";
2032 error
= GL_INVALID_VALUE
;
2036 /* Check for invalid pixel storage modes */
2037 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dimensions
,
2039 "glCompressedTexImage")) {
2043 /* check image size in bytes */
2044 if (expectedSize
!= imageSize
) {
2045 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
2046 * if <imageSize> is not consistent with the format, dimensions, and
2047 * contents of the specified image.
2049 reason
= "imageSize inconsistent with width/height/format";
2050 error
= GL_INVALID_VALUE
;
2054 if (!mutable_tex_object(ctx
, target
)) {
2055 reason
= "immutable texture";
2056 error
= GL_INVALID_OPERATION
;
2063 /* Note: not all error paths exit through here. */
2064 _mesa_error(ctx
, error
, "glCompressedTexImage%dD(%s)",
2065 dimensions
, reason
);
2072 * Test glTexSubImage[123]D() parameters for errors.
2074 * \param ctx GL context.
2075 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2076 * \param target texture target given by the user (already validated)
2077 * \param level image level given by the user.
2078 * \param xoffset sub-image x offset given by the user.
2079 * \param yoffset sub-image y offset given by the user.
2080 * \param zoffset sub-image z offset given by the user.
2081 * \param format pixel data format given by the user.
2082 * \param type pixel data type given by the user.
2083 * \param width image width given by the user.
2084 * \param height image height given by the user.
2085 * \param depth image depth given by the user.
2087 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2089 * Verifies each of the parameters against the constants specified in
2090 * __struct gl_contextRec::Const and the supported extensions, and according
2091 * to the OpenGL specification.
2094 texsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2095 struct gl_texture_object
*texObj
,
2096 GLenum target
, GLint level
,
2097 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2098 GLint width
, GLint height
, GLint depth
,
2099 GLenum format
, GLenum type
, const GLvoid
*pixels
,
2100 bool dsa
, const char *callerName
)
2102 struct gl_texture_image
*texImage
;
2106 /* must be out of memory */
2107 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", callerName
);
2112 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2113 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(level=%d)", callerName
, level
);
2117 if (error_check_subtexture_negative_dimensions(ctx
, dimensions
,
2118 width
, height
, depth
,
2123 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
2125 /* non-existant texture level */
2126 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid texture image)",
2131 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
2132 if (err
!= GL_NO_ERROR
) {
2133 _mesa_error(ctx
, err
,
2134 "%s(incompatible format = %s, type = %s)",
2135 callerName
, _mesa_enum_to_string(format
),
2136 _mesa_enum_to_string(type
));
2140 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2141 * combinations of format, internalFormat, and type that can be used.
2142 * Formats and types that require additional extensions (e.g., GL_FLOAT
2143 * requires GL_OES_texture_float) are filtered elsewhere.
2145 if (_mesa_is_gles(ctx
) &&
2146 texture_format_error_check_gles(ctx
, format
, type
,
2147 texImage
->InternalFormat
,
2148 dimensions
, callerName
)) {
2152 /* validate the bound PBO, if any */
2153 if (!_mesa_validate_pbo_source(ctx
, dimensions
, &ctx
->Unpack
,
2154 width
, height
, depth
, format
, type
,
2155 INT_MAX
, pixels
, callerName
)) {
2159 if (error_check_subtexture_dimensions(ctx
, dimensions
,
2160 texImage
, xoffset
, yoffset
, zoffset
,
2161 width
, height
, depth
, callerName
)) {
2165 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2166 if (_mesa_format_no_online_compression(ctx
, texImage
->InternalFormat
)) {
2167 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2168 "%s(no compression for format)", callerName
);
2173 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
2174 /* both source and dest must be integer-valued, or neither */
2175 if (_mesa_is_format_integer_color(texImage
->TexFormat
) !=
2176 _mesa_is_enum_format_integer(format
)) {
2177 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2178 "%s(integer/non-integer format mismatch)", callerName
);
2188 * Test glCopyTexImage[12]D() parameters for errors.
2190 * \param ctx GL context.
2191 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2192 * \param target texture target given by the user.
2193 * \param level image level given by the user.
2194 * \param internalFormat internal format given by the user.
2195 * \param width image width given by the user.
2196 * \param height image height given by the user.
2197 * \param border texture border.
2199 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2201 * Verifies each of the parameters against the constants specified in
2202 * __struct gl_contextRec::Const and the supported extensions, and according
2203 * to the OpenGL specification.
2206 copytexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
2207 GLenum target
, GLint level
, GLint internalFormat
,
2208 GLint width
, GLint height
, GLint border
)
2211 GLint rb_base_format
;
2212 struct gl_renderbuffer
*rb
;
2213 GLenum rb_internal_format
;
2216 if (!legal_texsubimage_target(ctx
, dimensions
, target
, false)) {
2217 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexImage%uD(target=%s)",
2218 dimensions
, _mesa_enum_to_string(target
));
2223 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2224 _mesa_error(ctx
, GL_INVALID_VALUE
,
2225 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
2229 /* Check that the source buffer is complete */
2230 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2231 if (ctx
->ReadBuffer
->_Status
== 0) {
2232 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2234 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2235 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2236 "glCopyTexImage%dD(invalid readbuffer)", dimensions
);
2240 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2241 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2242 "glCopyTexImage%dD(multisample FBO)", dimensions
);
2248 if (border
< 0 || border
> 1 ||
2249 ((ctx
->API
!= API_OPENGL_COMPAT
||
2250 target
== GL_TEXTURE_RECTANGLE_NV
||
2251 target
== GL_PROXY_TEXTURE_RECTANGLE_NV
) && border
!= 0)) {
2252 _mesa_error(ctx
, GL_INVALID_VALUE
,
2253 "glCopyTexImage%dD(border=%d)", dimensions
, border
);
2257 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2260 if (_mesa_is_gles(ctx
) && !_mesa_is_gles3(ctx
)) {
2261 switch (internalFormat
) {
2266 case GL_LUMINANCE_ALPHA
:
2269 _mesa_error(ctx
, GL_INVALID_ENUM
,
2270 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2271 _mesa_enum_to_string(internalFormat
));
2276 * Section 8.6 (Alternate Texture Image Specification Commands) of the
2277 * OpenGL 4.5 (Compatibility Profile) spec says:
2279 * "Parameters level, internalformat, and border are specified using
2280 * the same values, with the same meanings, as the corresponding
2281 * arguments of TexImage2D, except that internalformat may not be
2282 * specified as 1, 2, 3, or 4."
2284 if (internalFormat
>= 1 && internalFormat
<= 4) {
2285 _mesa_error(ctx
, GL_INVALID_ENUM
,
2286 "glCopyTexImage%dD(internalFormat=%d)", dimensions
,
2292 baseFormat
= _mesa_base_tex_format(ctx
, internalFormat
);
2293 if (baseFormat
< 0) {
2294 _mesa_error(ctx
, GL_INVALID_ENUM
,
2295 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2296 _mesa_enum_to_string(internalFormat
));
2300 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, internalFormat
);
2302 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2303 "glCopyTexImage%dD(read buffer)", dimensions
);
2307 rb_internal_format
= rb
->InternalFormat
;
2308 rb_base_format
= _mesa_base_tex_format(ctx
, rb
->InternalFormat
);
2309 if (_mesa_is_color_format(internalFormat
)) {
2310 if (rb_base_format
< 0) {
2311 _mesa_error(ctx
, GL_INVALID_VALUE
,
2312 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2313 _mesa_enum_to_string(internalFormat
));
2318 if (_mesa_is_gles(ctx
)) {
2320 if (_mesa_base_format_component_count(baseFormat
) >
2321 _mesa_base_format_component_count(rb_base_format
)) {
2324 if (baseFormat
== GL_DEPTH_COMPONENT
||
2325 baseFormat
== GL_DEPTH_STENCIL
||
2326 baseFormat
== GL_STENCIL_INDEX
||
2327 rb_base_format
== GL_DEPTH_COMPONENT
||
2328 rb_base_format
== GL_DEPTH_STENCIL
||
2329 rb_base_format
== GL_STENCIL_INDEX
||
2330 ((baseFormat
== GL_LUMINANCE_ALPHA
||
2331 baseFormat
== GL_ALPHA
) &&
2332 rb_base_format
!= GL_RGBA
) ||
2333 internalFormat
== GL_RGB9_E5
) {
2336 if (internalFormat
== GL_RGB9_E5
) {
2340 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2341 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2342 _mesa_enum_to_string(internalFormat
));
2347 if (_mesa_is_gles3(ctx
)) {
2348 bool rb_is_srgb
= false;
2349 bool dst_is_srgb
= false;
2351 if (ctx
->Extensions
.EXT_framebuffer_sRGB
&&
2352 _mesa_get_format_color_encoding(rb
->Format
) == GL_SRGB
) {
2356 if (_mesa_get_linear_internalformat(internalFormat
) != internalFormat
) {
2360 if (rb_is_srgb
!= dst_is_srgb
) {
2361 /* Page 137 (page 149 of the PDF) in section 3.8.5 of the
2362 * OpenGLES 3.0.0 spec says:
2364 * "The error INVALID_OPERATION is also generated if the
2365 * value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the
2366 * framebuffer attachment corresponding to the read buffer
2367 * is LINEAR (see section 6.1.13) and internalformat is
2368 * one of the sRGB formats described in section 3.8.16, or
2369 * if the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING is
2370 * SRGB and internalformat is not one of the sRGB formats."
2372 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2373 "glCopyTexImage%dD(srgb usage mismatch)", dimensions
);
2377 /* Page 139, Table 3.15 of OpenGL ES 3.0 spec does not define ReadPixels
2378 * types for SNORM formats. Also, conversion to SNORM formats is not
2379 * allowed by Table 3.2 on Page 110.
2381 if (_mesa_is_enum_format_snorm(internalFormat
)) {
2382 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2383 "glCopyTexImage%dD(internalFormat=%s)", dimensions
,
2384 _mesa_enum_to_string(internalFormat
));
2389 if (!_mesa_source_buffer_exists(ctx
, baseFormat
)) {
2390 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2391 "glCopyTexImage%dD(missing readbuffer)", dimensions
);
2395 /* From the EXT_texture_integer spec:
2397 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2398 * if the texture internalformat is an integer format and the read color
2399 * buffer is not an integer format, or if the internalformat is not an
2400 * integer format and the read color buffer is an integer format."
2402 if (_mesa_is_color_format(internalFormat
)) {
2403 bool is_int
= _mesa_is_enum_format_integer(internalFormat
);
2404 bool is_rbint
= _mesa_is_enum_format_integer(rb_internal_format
);
2405 bool is_unorm
= _mesa_is_enum_format_unorm(internalFormat
);
2406 bool is_rbunorm
= _mesa_is_enum_format_unorm(rb_internal_format
);
2407 if (is_int
|| is_rbint
) {
2408 if (is_int
!= is_rbint
) {
2409 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2410 "glCopyTexImage%dD(integer vs non-integer)", dimensions
);
2412 } else if (_mesa_is_gles(ctx
) &&
2413 _mesa_is_enum_format_unsigned_int(internalFormat
) !=
2414 _mesa_is_enum_format_unsigned_int(rb_internal_format
)) {
2415 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2416 "glCopyTexImage%dD(signed vs unsigned integer)", dimensions
);
2421 /* From page 138 of OpenGL ES 3.0 spec:
2422 * "The error INVALID_OPERATION is generated if floating-point RGBA
2423 * data is required; if signed integer RGBA data is required and the
2424 * format of the current color buffer is not signed integer; if
2425 * unsigned integer RGBA data is required and the format of the
2426 * current color buffer is not unsigned integer; or if fixed-point
2427 * RGBA data is required and the format of the current color buffer
2428 * is not fixed-point.
2430 if (_mesa_is_gles(ctx
) && is_unorm
!= is_rbunorm
)
2431 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2432 "glCopyTexImage%dD(unorm vs non-unorm)", dimensions
);
2435 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
2437 if (!_mesa_target_can_be_compressed(ctx
, target
, internalFormat
, &err
)) {
2438 _mesa_error(ctx
, err
,
2439 "glCopyTexImage%dD(target can't be compressed)", dimensions
);
2442 if (_mesa_format_no_online_compression(ctx
, internalFormat
)) {
2443 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2444 "glCopyTexImage%dD(no compression for format)", dimensions
);
2448 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2449 "glCopyTexImage%dD(border!=0)", dimensions
);
2454 if (!mutable_tex_object(ctx
, target
)) {
2455 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2456 "glCopyTexImage%dD(immutable texture)", dimensions
);
2460 /* if we get here, the parameters are OK */
2466 * Test glCopyTexSubImage[12]D() parameters for errors.
2467 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2470 copytexsubimage_error_check(struct gl_context
*ctx
, GLuint dimensions
,
2471 const struct gl_texture_object
*texObj
,
2472 GLenum target
, GLint level
,
2473 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2474 GLint width
, GLint height
, const char *caller
)
2476 struct gl_texture_image
*texImage
;
2478 /* Check that the source buffer is complete */
2479 if (_mesa_is_user_fbo(ctx
->ReadBuffer
)) {
2480 if (ctx
->ReadBuffer
->_Status
== 0) {
2481 _mesa_test_framebuffer_completeness(ctx
, ctx
->ReadBuffer
);
2483 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
2484 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
2485 "%s(invalid readbuffer)", caller
);
2489 if (ctx
->ReadBuffer
->Visual
.samples
> 0) {
2490 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2491 "%s(multisample FBO)", caller
);
2497 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
2498 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(level=%d)", caller
, level
);
2502 /* Get dest image pointers */
2504 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", caller
);
2508 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
2510 /* destination image does not exist */
2511 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2512 "%s(invalid texture image)", caller
);
2516 if (error_check_subtexture_negative_dimensions(ctx
, dimensions
,
2517 width
, height
, 1, caller
)) {
2521 if (error_check_subtexture_dimensions(ctx
, dimensions
, texImage
,
2522 xoffset
, yoffset
, zoffset
,
2523 width
, height
, 1, caller
)) {
2527 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
2528 if (_mesa_format_no_online_compression(ctx
, texImage
->InternalFormat
)) {
2529 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2530 "%s(no compression for format)", caller
);
2535 if (texImage
->InternalFormat
== GL_YCBCR_MESA
) {
2536 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s()", caller
);
2540 if (!_mesa_source_buffer_exists(ctx
, texImage
->_BaseFormat
)) {
2541 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2542 "%s(missing readbuffer, format=%s)", caller
,
2543 _mesa_enum_to_string(texImage
->_BaseFormat
));
2547 /* From the EXT_texture_integer spec:
2549 * "INVALID_OPERATION is generated by CopyTexImage* and
2550 * CopyTexSubImage* if the texture internalformat is an integer format
2551 * and the read color buffer is not an integer format, or if the
2552 * internalformat is not an integer format and the read color buffer
2553 * is an integer format."
2555 if (_mesa_is_color_format(texImage
->InternalFormat
)) {
2556 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
2558 if (_mesa_is_format_integer_color(rb
->Format
) !=
2559 _mesa_is_format_integer_color(texImage
->TexFormat
)) {
2560 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2561 "%s(integer vs non-integer)", caller
);
2566 /* In the ES 3.2 specification's Table 8.13 (Valid CopyTexImage source
2567 * framebuffer/destination texture base internal format combinations),
2568 * all the entries for stencil are left blank (unsupported).
2570 if (_mesa_is_gles(ctx
) && _mesa_is_stencil_format(texImage
->_BaseFormat
)) {
2571 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(stencil disallowed)", caller
);
2575 /* if we get here, the parameters are OK */
2580 /** Callback info for walking over FBO hash table */
2583 struct gl_context
*ctx
;
2584 struct gl_texture_object
*texObj
;
2590 * Check render to texture callback. Called from _mesa_HashWalk().
2593 check_rtt_cb(GLuint key
, void *data
, void *userData
)
2595 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
2596 const struct cb_info
*info
= (struct cb_info
*) userData
;
2597 struct gl_context
*ctx
= info
->ctx
;
2598 const struct gl_texture_object
*texObj
= info
->texObj
;
2599 const GLuint level
= info
->level
, face
= info
->face
;
2601 /* If this is a user-created FBO */
2602 if (_mesa_is_user_fbo(fb
)) {
2604 /* check if any of the FBO's attachments point to 'texObj' */
2605 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
2606 struct gl_renderbuffer_attachment
*att
= fb
->Attachment
+ i
;
2607 if (att
->Type
== GL_TEXTURE
&&
2608 att
->Texture
== texObj
&&
2609 att
->TextureLevel
== level
&&
2610 att
->CubeMapFace
== face
) {
2611 _mesa_update_texture_renderbuffer(ctx
, fb
, att
);
2612 assert(att
->Renderbuffer
->TexImage
);
2613 /* Mark fb status as indeterminate to force re-validation */
2616 /* Make sure that the revalidation actually happens if this is
2617 * being done to currently-bound buffers.
2619 if (fb
== ctx
->DrawBuffer
|| fb
== ctx
->ReadBuffer
)
2620 ctx
->NewState
|= _NEW_BUFFERS
;
2628 * When a texture image is specified we have to check if it's bound to
2629 * any framebuffer objects (render to texture) in order to detect changes
2630 * in size or format since that effects FBO completeness.
2631 * Any FBOs rendering into the texture must be re-validated.
2634 _mesa_update_fbo_texture(struct gl_context
*ctx
,
2635 struct gl_texture_object
*texObj
,
2636 GLuint face
, GLuint level
)
2638 /* Only check this texture if it's been marked as RenderToTexture */
2639 if (texObj
->_RenderToTexture
) {
2640 struct cb_info info
;
2642 info
.texObj
= texObj
;
2645 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, check_rtt_cb
, &info
);
2651 * If the texture object's GenerateMipmap flag is set and we've
2652 * changed the texture base level image, regenerate the rest of the
2653 * mipmap levels now.
2656 check_gen_mipmap(struct gl_context
*ctx
, GLenum target
,
2657 struct gl_texture_object
*texObj
, GLint level
)
2659 if (texObj
->GenerateMipmap
&&
2660 level
== texObj
->BaseLevel
&&
2661 level
< texObj
->MaxLevel
) {
2662 assert(ctx
->Driver
.GenerateMipmap
);
2663 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
2668 /** Debug helper: override the user-requested internal format */
2670 override_internal_format(GLenum internalFormat
, GLint width
, GLint height
)
2673 if (internalFormat
== GL_RGBA16F_ARB
||
2674 internalFormat
== GL_RGBA32F_ARB
) {
2675 printf("Convert rgba float tex to int %d x %d\n", width
, height
);
2678 else if (internalFormat
== GL_RGB16F_ARB
||
2679 internalFormat
== GL_RGB32F_ARB
) {
2680 printf("Convert rgb float tex to int %d x %d\n", width
, height
);
2683 else if (internalFormat
== GL_LUMINANCE_ALPHA16F_ARB
||
2684 internalFormat
== GL_LUMINANCE_ALPHA32F_ARB
) {
2685 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2686 return GL_LUMINANCE_ALPHA
;
2688 else if (internalFormat
== GL_LUMINANCE16F_ARB
||
2689 internalFormat
== GL_LUMINANCE32F_ARB
) {
2690 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2691 return GL_LUMINANCE
;
2693 else if (internalFormat
== GL_ALPHA16F_ARB
||
2694 internalFormat
== GL_ALPHA32F_ARB
) {
2695 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
2699 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2700 internalFormat = GL_RGBA;
2704 return internalFormat
;
2707 return internalFormat
;
2713 * Choose the actual hardware format for a texture image.
2714 * Try to use the same format as the previous image level when possible.
2715 * Otherwise, ask the driver for the best format.
2716 * It's important to try to choose a consistant format for all levels
2717 * for efficient texture memory layout/allocation. In particular, this
2718 * comes up during automatic mipmap generation.
2721 _mesa_choose_texture_format(struct gl_context
*ctx
,
2722 struct gl_texture_object
*texObj
,
2723 GLenum target
, GLint level
,
2724 GLenum internalFormat
, GLenum format
, GLenum type
)
2728 /* see if we've already chosen a format for the previous level */
2730 struct gl_texture_image
*prevImage
=
2731 _mesa_select_tex_image(texObj
, target
, level
- 1);
2732 /* See if the prev level is defined and has an internal format which
2733 * matches the new internal format.
2736 prevImage
->Width
> 0 &&
2737 prevImage
->InternalFormat
== internalFormat
) {
2738 /* use the same format */
2739 assert(prevImage
->TexFormat
!= MESA_FORMAT_NONE
);
2740 return prevImage
->TexFormat
;
2744 /* If the application requested compression to an S3TC format but we don't
2745 * have the DXTn library, force a generic compressed format instead.
2747 if (internalFormat
!= format
&& format
!= GL_NONE
) {
2748 const GLenum before
= internalFormat
;
2750 switch (internalFormat
) {
2751 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
2752 if (!ctx
->Mesa_DXTn
)
2753 internalFormat
= GL_COMPRESSED_RGB
;
2755 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
2756 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
2757 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
2758 if (!ctx
->Mesa_DXTn
)
2759 internalFormat
= GL_COMPRESSED_RGBA
;
2765 if (before
!= internalFormat
) {
2767 "DXT compression requested (%s), "
2768 "but libtxc_dxtn library not installed. Using %s "
2770 _mesa_enum_to_string(before
),
2771 _mesa_enum_to_string(internalFormat
));
2775 /* choose format from scratch */
2776 f
= ctx
->Driver
.ChooseTextureFormat(ctx
, target
, internalFormat
,
2778 assert(f
!= MESA_FORMAT_NONE
);
2784 * Adjust pixel unpack params and image dimensions to strip off the
2785 * one-pixel texture border.
2787 * Gallium and intel don't support texture borders. They've seldem been used
2788 * and seldom been implemented correctly anyway.
2790 * \param unpackNew returns the new pixel unpack parameters
2793 strip_texture_border(GLenum target
,
2794 GLint
*width
, GLint
*height
, GLint
*depth
,
2795 const struct gl_pixelstore_attrib
*unpack
,
2796 struct gl_pixelstore_attrib
*unpackNew
)
2802 *unpackNew
= *unpack
;
2804 if (unpackNew
->RowLength
== 0)
2805 unpackNew
->RowLength
= *width
;
2807 if (unpackNew
->ImageHeight
== 0)
2808 unpackNew
->ImageHeight
= *height
;
2810 assert(*width
>= 3);
2811 unpackNew
->SkipPixels
++; /* skip the border */
2812 *width
= *width
- 2; /* reduce the width by two border pixels */
2814 /* The min height of a texture with a border is 3 */
2815 if (*height
>= 3 && target
!= GL_TEXTURE_1D_ARRAY
) {
2816 unpackNew
->SkipRows
++; /* skip the border */
2817 *height
= *height
- 2; /* reduce the height by two border pixels */
2821 target
!= GL_TEXTURE_2D_ARRAY
&&
2822 target
!= GL_TEXTURE_CUBE_MAP_ARRAY
) {
2823 unpackNew
->SkipImages
++; /* skip the border */
2824 *depth
= *depth
- 2; /* reduce the depth by two border pixels */
2830 * Common code to implement all the glTexImage1D/2D/3D functions
2831 * as well as glCompressedTexImage1D/2D/3D.
2832 * \param compressed only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
2833 * \param format the user's image format (only used if !compressed)
2834 * \param type the user's image type (only used if !compressed)
2835 * \param imageSize only used for glCompressedTexImage1D/2D/3D calls.
2838 teximage(struct gl_context
*ctx
, GLboolean compressed
, GLuint dims
,
2839 GLenum target
, GLint level
, GLint internalFormat
,
2840 GLsizei width
, GLsizei height
, GLsizei depth
,
2841 GLint border
, GLenum format
, GLenum type
,
2842 GLsizei imageSize
, const GLvoid
*pixels
)
2844 const char *func
= compressed
? "glCompressedTexImage" : "glTexImage";
2845 struct gl_pixelstore_attrib unpack_no_border
;
2846 const struct gl_pixelstore_attrib
*unpack
= &ctx
->Unpack
;
2847 struct gl_texture_object
*texObj
;
2848 mesa_format texFormat
;
2849 GLboolean dimensionsOK
, sizeOK
;
2851 FLUSH_VERTICES(ctx
, 0);
2853 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
)) {
2856 "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
2858 _mesa_enum_to_string(target
), level
,
2859 _mesa_enum_to_string(internalFormat
),
2860 width
, height
, depth
, border
, pixels
);
2863 "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2865 _mesa_enum_to_string(target
), level
,
2866 _mesa_enum_to_string(internalFormat
),
2867 width
, height
, depth
, border
,
2868 _mesa_enum_to_string(format
),
2869 _mesa_enum_to_string(type
), pixels
);
2872 internalFormat
= override_internal_format(internalFormat
, width
, height
);
2874 /* target error checking */
2875 if (!legal_teximage_target(ctx
, dims
, target
)) {
2876 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s%uD(target=%s)",
2877 func
, dims
, _mesa_enum_to_string(target
));
2881 /* general error checking */
2883 if (compressed_texture_error_check(ctx
, dims
, target
, level
,
2885 width
, height
, depth
,
2886 border
, imageSize
, pixels
))
2890 if (texture_error_check(ctx
, dims
, target
, level
, internalFormat
,
2891 format
, type
, width
, height
, depth
, border
,
2896 /* Here we convert a cpal compressed image into a regular glTexImage2D
2897 * call by decompressing the texture. If we really want to support cpal
2898 * textures in any driver this would have to be changed.
2900 if (ctx
->API
== API_OPENGLES
&& compressed
&& dims
== 2) {
2901 switch (internalFormat
) {
2902 case GL_PALETTE4_RGB8_OES
:
2903 case GL_PALETTE4_RGBA8_OES
:
2904 case GL_PALETTE4_R5_G6_B5_OES
:
2905 case GL_PALETTE4_RGBA4_OES
:
2906 case GL_PALETTE4_RGB5_A1_OES
:
2907 case GL_PALETTE8_RGB8_OES
:
2908 case GL_PALETTE8_RGBA8_OES
:
2909 case GL_PALETTE8_R5_G6_B5_OES
:
2910 case GL_PALETTE8_RGBA4_OES
:
2911 case GL_PALETTE8_RGB5_A1_OES
:
2912 _mesa_cpal_compressed_teximage2d(target
, level
, internalFormat
,
2913 width
, height
, imageSize
, pixels
);
2918 texObj
= _mesa_get_current_tex_object(ctx
, target
);
2922 /* For glCompressedTexImage() the driver has no choice about the
2923 * texture format since we'll never transcode the user's compressed
2924 * image data. The internalFormat was error checked earlier.
2926 texFormat
= _mesa_glenum_to_compressed_format(internalFormat
);
2929 /* In case of HALF_FLOAT_OES or FLOAT_OES, find corresponding sized
2930 * internal floating point format for the given base format.
2932 if (_mesa_is_gles(ctx
) && format
== internalFormat
) {
2933 if (type
== GL_FLOAT
) {
2934 texObj
->_IsFloat
= GL_TRUE
;
2935 } else if (type
== GL_HALF_FLOAT_OES
|| type
== GL_HALF_FLOAT
) {
2936 texObj
->_IsHalfFloat
= GL_TRUE
;
2939 internalFormat
= adjust_for_oes_float_texture(ctx
, format
, type
);
2942 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
2943 internalFormat
, format
, type
);
2946 assert(texFormat
!= MESA_FORMAT_NONE
);
2948 /* check that width, height, depth are legal for the mipmap level */
2949 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, level
, width
,
2950 height
, depth
, border
);
2952 /* check that the texture won't take too much memory, etc */
2953 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target(target
),
2954 0, level
, texFormat
, 1,
2955 width
, height
, depth
);
2957 if (_mesa_is_proxy_texture(target
)) {
2958 /* Proxy texture: just clear or set state depending on error checking */
2959 struct gl_texture_image
*texImage
=
2960 get_proxy_tex_image(ctx
, target
, level
);
2963 return; /* GL_OUT_OF_MEMORY already recorded */
2965 if (dimensionsOK
&& sizeOK
) {
2966 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, depth
,
2967 border
, internalFormat
, texFormat
);
2970 clear_teximage_fields(texImage
);
2974 /* non-proxy target */
2975 const GLuint face
= _mesa_tex_target_to_face(target
);
2976 struct gl_texture_image
*texImage
;
2978 if (!dimensionsOK
) {
2979 _mesa_error(ctx
, GL_INVALID_VALUE
,
2980 "%s%uD(invalid width or height or depth)",
2986 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
2987 "%s%uD(image too large: %d x %d x %d, %s format)",
2988 func
, dims
, width
, height
, depth
,
2989 _mesa_enum_to_string(internalFormat
));
2993 /* Allow a hardware driver to just strip out the border, to provide
2994 * reliable but slightly incorrect hardware rendering instead of
2995 * rarely-tested software fallback rendering.
2997 if (border
&& ctx
->Const
.StripTextureBorder
) {
2998 strip_texture_border(target
, &width
, &height
, &depth
, unpack
,
3001 unpack
= &unpack_no_border
;
3004 if (ctx
->NewState
& _NEW_PIXEL
)
3005 _mesa_update_state(ctx
);
3007 _mesa_lock_texture(ctx
, texObj
);
3009 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3012 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s%uD", func
, dims
);
3015 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3017 _mesa_init_teximage_fields(ctx
, texImage
,
3018 width
, height
, depth
,
3019 border
, internalFormat
, texFormat
);
3021 /* Give the texture to the driver. <pixels> may be null. */
3022 if (width
> 0 && height
> 0 && depth
> 0) {
3024 ctx
->Driver
.CompressedTexImage(ctx
, dims
, texImage
,
3028 ctx
->Driver
.TexImage(ctx
, dims
, texImage
, format
,
3029 type
, pixels
, unpack
);
3033 check_gen_mipmap(ctx
, target
, texObj
, level
);
3035 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3037 _mesa_dirty_texobj(ctx
, texObj
);
3040 _mesa_unlock_texture(ctx
, texObj
);
3047 * Called from the API. Note that width includes the border.
3050 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
3051 GLsizei width
, GLint border
, GLenum format
,
3052 GLenum type
, const GLvoid
*pixels
)
3054 GET_CURRENT_CONTEXT(ctx
);
3055 teximage(ctx
, GL_FALSE
, 1, target
, level
, internalFormat
, width
, 1, 1,
3056 border
, format
, type
, 0, pixels
);
3061 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
3062 GLsizei width
, GLsizei height
, GLint border
,
3063 GLenum format
, GLenum type
,
3064 const GLvoid
*pixels
)
3066 GET_CURRENT_CONTEXT(ctx
);
3067 teximage(ctx
, GL_FALSE
, 2, target
, level
, internalFormat
, width
, height
, 1,
3068 border
, format
, type
, 0, pixels
);
3073 * Called by the API or display list executor.
3074 * Note that width and height include the border.
3077 _mesa_TexImage3D( GLenum target
, GLint level
, GLint internalFormat
,
3078 GLsizei width
, GLsizei height
, GLsizei depth
,
3079 GLint border
, GLenum format
, GLenum type
,
3080 const GLvoid
*pixels
)
3082 GET_CURRENT_CONTEXT(ctx
);
3083 teximage(ctx
, GL_FALSE
, 3, target
, level
, internalFormat
,
3084 width
, height
, depth
,
3085 border
, format
, type
, 0, pixels
);
3090 _mesa_TexImage3DEXT( GLenum target
, GLint level
, GLenum internalFormat
,
3091 GLsizei width
, GLsizei height
, GLsizei depth
,
3092 GLint border
, GLenum format
, GLenum type
,
3093 const GLvoid
*pixels
)
3095 _mesa_TexImage3D(target
, level
, (GLint
) internalFormat
, width
, height
,
3096 depth
, border
, format
, type
, pixels
);
3101 _mesa_EGLImageTargetTexture2DOES (GLenum target
, GLeglImageOES image
)
3103 struct gl_texture_object
*texObj
;
3104 struct gl_texture_image
*texImage
;
3106 GET_CURRENT_CONTEXT(ctx
);
3107 FLUSH_VERTICES(ctx
, 0);
3111 valid_target
= ctx
->Extensions
.OES_EGL_image
;
3113 case GL_TEXTURE_EXTERNAL_OES
:
3115 _mesa_is_gles(ctx
) ? ctx
->Extensions
.OES_EGL_image_external
: false;
3118 valid_target
= false;
3122 if (!valid_target
) {
3123 _mesa_error(ctx
, GL_INVALID_ENUM
,
3124 "glEGLImageTargetTexture2D(target=%d)", target
);
3129 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3130 "glEGLImageTargetTexture2D(image=%p)", image
);
3134 if (ctx
->NewState
& _NEW_PIXEL
)
3135 _mesa_update_state(ctx
);
3137 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3141 _mesa_lock_texture(ctx
, texObj
);
3143 if (texObj
->Immutable
) {
3144 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3145 "glEGLImageTargetTexture2D(texture is immutable)");
3146 _mesa_unlock_texture(ctx
, texObj
);
3150 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
3152 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glEGLImageTargetTexture2D");
3154 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3156 ctx
->Driver
.EGLImageTargetTexture2D(ctx
, target
,
3157 texObj
, texImage
, image
);
3159 _mesa_dirty_texobj(ctx
, texObj
);
3161 _mesa_unlock_texture(ctx
, texObj
);
3166 * Helper that implements the glTexSubImage1/2/3D()
3167 * and glTextureSubImage1/2/3D() functions.
3170 _mesa_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
3171 struct gl_texture_object
*texObj
,
3172 struct gl_texture_image
*texImage
,
3173 GLenum target
, GLint level
,
3174 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3175 GLsizei width
, GLsizei height
, GLsizei depth
,
3176 GLenum format
, GLenum type
, const GLvoid
*pixels
,
3179 FLUSH_VERTICES(ctx
, 0);
3181 if (ctx
->NewState
& _NEW_PIXEL
)
3182 _mesa_update_state(ctx
);
3184 _mesa_lock_texture(ctx
, texObj
);
3186 if (width
> 0 && height
> 0 && depth
> 0) {
3187 /* If we have a border, offset=-1 is legal. Bias by border width. */
3190 if (target
!= GL_TEXTURE_2D_ARRAY
)
3191 zoffset
+= texImage
->Border
;
3194 if (target
!= GL_TEXTURE_1D_ARRAY
)
3195 yoffset
+= texImage
->Border
;
3198 xoffset
+= texImage
->Border
;
3201 ctx
->Driver
.TexSubImage(ctx
, dims
, texImage
,
3202 xoffset
, yoffset
, zoffset
,
3203 width
, height
, depth
,
3204 format
, type
, pixels
, &ctx
->Unpack
);
3206 check_gen_mipmap(ctx
, target
, texObj
, level
);
3208 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3209 * the texel data, not the texture format, size, etc.
3213 _mesa_unlock_texture(ctx
, texObj
);
3217 * Implement all the glTexSubImage1/2/3D() functions.
3218 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3221 texsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
3222 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3223 GLsizei width
, GLsizei height
, GLsizei depth
,
3224 GLenum format
, GLenum type
, const GLvoid
*pixels
,
3225 const char *callerName
)
3227 struct gl_texture_object
*texObj
;
3228 struct gl_texture_image
*texImage
;
3230 /* check target (proxies not allowed) */
3231 if (!legal_texsubimage_target(ctx
, dims
, target
, false)) {
3232 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage%uD(target=%s)",
3233 dims
, _mesa_enum_to_string(target
));
3237 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3241 if (texsubimage_error_check(ctx
, dims
, texObj
, target
, level
,
3242 xoffset
, yoffset
, zoffset
,
3243 width
, height
, depth
, format
, type
,
3244 pixels
, false, callerName
)) {
3245 return; /* error was detected */
3248 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
3249 /* texsubimage_error_check ensures that texImage is not NULL */
3251 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3252 _mesa_debug(ctx
, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3254 _mesa_enum_to_string(target
), level
,
3255 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3256 _mesa_enum_to_string(format
),
3257 _mesa_enum_to_string(type
), pixels
);
3259 _mesa_texture_sub_image(ctx
, dims
, texObj
, texImage
, target
, level
,
3260 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3261 format
, type
, pixels
, false);
3266 * Implement all the glTextureSubImage1/2/3D() functions.
3267 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3270 texturesubimage(struct gl_context
*ctx
, GLuint dims
,
3271 GLuint texture
, GLint level
,
3272 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3273 GLsizei width
, GLsizei height
, GLsizei depth
,
3274 GLenum format
, GLenum type
, const GLvoid
*pixels
,
3275 const char *callerName
)
3277 struct gl_texture_object
*texObj
;
3278 struct gl_texture_image
*texImage
;
3281 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3283 "glTextureSubImage%uD %d %d %d %d %d %d %d %d %s %s %p\n",
3284 dims
, texture
, level
,
3285 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
3286 _mesa_enum_to_string(format
),
3287 _mesa_enum_to_string(type
), pixels
);
3289 /* Get the texture object by Name. */
3290 texObj
= _mesa_lookup_texture(ctx
, texture
);
3292 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTextureSubImage%uD(texture)",
3297 /* check target (proxies not allowed) */
3298 if (!legal_texsubimage_target(ctx
, dims
, texObj
->Target
, true)) {
3299 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target=%s)",
3300 callerName
, _mesa_enum_to_string(texObj
->Target
));
3304 if (texsubimage_error_check(ctx
, dims
, texObj
, texObj
->Target
, level
,
3305 xoffset
, yoffset
, zoffset
,
3306 width
, height
, depth
, format
, type
,
3307 pixels
, true, callerName
)) {
3308 return; /* error was detected */
3312 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
3313 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
3317 * What do we do if the user created a texture with the following code
3318 * and then called this function with its handle?
3321 * glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &tex);
3322 * glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
3323 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, ...);
3324 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, ...);
3325 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, ...);
3326 * // Note: GL_TEXTURE_CUBE_MAP_NEGATIVE_Y not set, or given the
3327 * // wrong format, or given the wrong size, etc.
3328 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, ...);
3329 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, ...);
3331 * A bug has been filed against the spec for this case. In the
3332 * meantime, we will check for cube completeness.
3334 * According to Section 8.17 Texture Completeness in the OpenGL 4.5
3335 * Core Profile spec (30.10.2014):
3336 * "[A] cube map texture is cube complete if the
3337 * following conditions all hold true: The [base level] texture
3338 * images of each of the six cube map faces have identical, positive,
3339 * and square dimensions. The [base level] images were each specified
3340 * with the same internal format."
3342 * It seems reasonable to check for cube completeness of an arbitrary
3343 * level here so that the image data has a consistent format and size.
3345 if (!_mesa_cube_level_complete(texObj
, level
)) {
3346 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3347 "glTextureSubImage%uD(cube map incomplete)",
3352 imageStride
= _mesa_image_image_stride(&ctx
->Unpack
, width
, height
,
3354 /* Copy in each face. */
3355 for (i
= zoffset
; i
< zoffset
+ depth
; ++i
) {
3356 texImage
= texObj
->Image
[i
][level
];
3359 _mesa_texture_sub_image(ctx
, 3, texObj
, texImage
, texObj
->Target
,
3360 level
, xoffset
, yoffset
, 0,
3361 width
, height
, 1, format
,
3362 type
, pixels
, true);
3363 pixels
= (GLubyte
*) pixels
+ imageStride
;
3367 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
3370 _mesa_texture_sub_image(ctx
, dims
, texObj
, texImage
, texObj
->Target
,
3371 level
, xoffset
, yoffset
, zoffset
,
3372 width
, height
, depth
, format
,
3373 type
, pixels
, true);
3379 _mesa_TexSubImage1D( GLenum target
, GLint level
,
3380 GLint xoffset
, GLsizei width
,
3381 GLenum format
, GLenum type
,
3382 const GLvoid
*pixels
)
3384 GET_CURRENT_CONTEXT(ctx
);
3385 texsubimage(ctx
, 1, target
, level
,
3388 format
, type
, pixels
, "glTexSubImage1D");
3393 _mesa_TexSubImage2D( GLenum target
, GLint level
,
3394 GLint xoffset
, GLint yoffset
,
3395 GLsizei width
, GLsizei height
,
3396 GLenum format
, GLenum type
,
3397 const GLvoid
*pixels
)
3399 GET_CURRENT_CONTEXT(ctx
);
3400 texsubimage(ctx
, 2, target
, level
,
3401 xoffset
, yoffset
, 0,
3403 format
, type
, pixels
, "glTexSubImage2D");
3409 _mesa_TexSubImage3D( GLenum target
, GLint level
,
3410 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3411 GLsizei width
, GLsizei height
, GLsizei depth
,
3412 GLenum format
, GLenum type
,
3413 const GLvoid
*pixels
)
3415 GET_CURRENT_CONTEXT(ctx
);
3416 texsubimage(ctx
, 3, target
, level
,
3417 xoffset
, yoffset
, zoffset
,
3418 width
, height
, depth
,
3419 format
, type
, pixels
, "glTexSubImage3D");
3423 _mesa_TextureSubImage1D(GLuint texture
, GLint level
,
3424 GLint xoffset
, GLsizei width
,
3425 GLenum format
, GLenum type
,
3426 const GLvoid
*pixels
)
3428 GET_CURRENT_CONTEXT(ctx
);
3429 texturesubimage(ctx
, 1, texture
, level
,
3432 format
, type
, pixels
, "glTextureSubImage1D");
3437 _mesa_TextureSubImage2D(GLuint texture
, GLint level
,
3438 GLint xoffset
, GLint yoffset
,
3439 GLsizei width
, GLsizei height
,
3440 GLenum format
, GLenum type
,
3441 const GLvoid
*pixels
)
3443 GET_CURRENT_CONTEXT(ctx
);
3444 texturesubimage(ctx
, 2, texture
, level
,
3445 xoffset
, yoffset
, 0,
3447 format
, type
, pixels
, "glTextureSubImage2D");
3452 _mesa_TextureSubImage3D(GLuint texture
, GLint level
,
3453 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3454 GLsizei width
, GLsizei height
, GLsizei depth
,
3455 GLenum format
, GLenum type
,
3456 const GLvoid
*pixels
)
3458 GET_CURRENT_CONTEXT(ctx
);
3459 texturesubimage(ctx
, 3, texture
, level
,
3460 xoffset
, yoffset
, zoffset
,
3461 width
, height
, depth
,
3462 format
, type
, pixels
, "glTextureSubImage3D");
3467 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3468 * from. This depends on whether the texture contains color or depth values.
3470 static struct gl_renderbuffer
*
3471 get_copy_tex_image_source(struct gl_context
*ctx
, mesa_format texFormat
)
3473 if (_mesa_get_format_bits(texFormat
, GL_DEPTH_BITS
) > 0) {
3474 /* reading from depth/stencil buffer */
3475 return ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
3476 } else if (_mesa_get_format_bits(texFormat
, GL_STENCIL_BITS
) > 0) {
3477 return ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
3479 /* copying from color buffer */
3480 return ctx
->ReadBuffer
->_ColorReadBuffer
;
3485 copytexsubimage_by_slice(struct gl_context
*ctx
,
3486 struct gl_texture_image
*texImage
,
3488 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3489 struct gl_renderbuffer
*rb
,
3491 GLsizei width
, GLsizei height
)
3493 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
3496 /* For 1D arrays, we copy each scanline of the source rectangle into the
3499 assert(zoffset
== 0);
3501 for (slice
= 0; slice
< height
; slice
++) {
3502 assert(yoffset
+ slice
< texImage
->Height
);
3503 ctx
->Driver
.CopyTexSubImage(ctx
, 2, texImage
,
3504 xoffset
, 0, yoffset
+ slice
,
3505 rb
, x
, y
+ slice
, width
, 1);
3508 ctx
->Driver
.CopyTexSubImage(ctx
, dims
, texImage
,
3509 xoffset
, yoffset
, zoffset
,
3510 rb
, x
, y
, width
, height
);
3515 formats_differ_in_component_sizes(mesa_format f1
, mesa_format f2
)
3517 GLint f1_r_bits
= _mesa_get_format_bits(f1
, GL_RED_BITS
);
3518 GLint f1_g_bits
= _mesa_get_format_bits(f1
, GL_GREEN_BITS
);
3519 GLint f1_b_bits
= _mesa_get_format_bits(f1
, GL_BLUE_BITS
);
3520 GLint f1_a_bits
= _mesa_get_format_bits(f1
, GL_ALPHA_BITS
);
3522 GLint f2_r_bits
= _mesa_get_format_bits(f2
, GL_RED_BITS
);
3523 GLint f2_g_bits
= _mesa_get_format_bits(f2
, GL_GREEN_BITS
);
3524 GLint f2_b_bits
= _mesa_get_format_bits(f2
, GL_BLUE_BITS
);
3525 GLint f2_a_bits
= _mesa_get_format_bits(f2
, GL_ALPHA_BITS
);
3527 if ((f1_r_bits
&& f2_r_bits
&& f1_r_bits
!= f2_r_bits
)
3528 || (f1_g_bits
&& f2_g_bits
&& f1_g_bits
!= f2_g_bits
)
3529 || (f1_b_bits
&& f2_b_bits
&& f1_b_bits
!= f2_b_bits
)
3530 || (f1_a_bits
&& f2_a_bits
&& f1_a_bits
!= f2_a_bits
))
3537 can_avoid_reallocation(struct gl_texture_image
*texImage
, GLenum internalFormat
,
3538 mesa_format texFormat
, GLint x
, GLint y
, GLsizei width
,
3539 GLsizei height
, GLint border
)
3541 if (texImage
->InternalFormat
!= internalFormat
)
3543 if (texImage
->TexFormat
!= texFormat
)
3545 if (texImage
->Border
!= border
)
3547 if (texImage
->Width2
!= width
)
3549 if (texImage
->Height2
!= height
)
3555 * Implement the glCopyTexImage1/2D() functions.
3558 copyteximage(struct gl_context
*ctx
, GLuint dims
,
3559 GLenum target
, GLint level
, GLenum internalFormat
,
3560 GLint x
, GLint y
, GLsizei width
, GLsizei height
, GLint border
)
3562 struct gl_texture_object
*texObj
;
3563 struct gl_texture_image
*texImage
;
3564 const GLuint face
= _mesa_tex_target_to_face(target
);
3565 mesa_format texFormat
;
3566 struct gl_renderbuffer
*rb
;
3568 FLUSH_VERTICES(ctx
, 0);
3570 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3571 _mesa_debug(ctx
, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3573 _mesa_enum_to_string(target
), level
,
3574 _mesa_enum_to_string(internalFormat
),
3575 x
, y
, width
, height
, border
);
3577 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3578 _mesa_update_state(ctx
);
3580 if (copytexture_error_check(ctx
, dims
, target
, level
, internalFormat
,
3581 width
, height
, border
))
3584 if (!_mesa_legal_texture_dimensions(ctx
, target
, level
, width
, height
,
3586 _mesa_error(ctx
, GL_INVALID_VALUE
,
3587 "glCopyTexImage%uD(invalid width or height)", dims
);
3591 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3594 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
3595 internalFormat
, GL_NONE
, GL_NONE
);
3597 /* First check if reallocating the texture buffer can be avoided.
3598 * Without the realloc the copy can be 20x faster.
3600 _mesa_lock_texture(ctx
, texObj
);
3602 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
3603 if (texImage
&& can_avoid_reallocation(texImage
, internalFormat
, texFormat
,
3604 x
, y
, width
, height
, border
)) {
3605 _mesa_unlock_texture(ctx
, texObj
);
3606 return _mesa_copy_texture_sub_image(ctx
, dims
, texObj
, target
, level
,
3607 0, 0, 0, x
, y
, width
, height
,
3611 _mesa_unlock_texture(ctx
, texObj
);
3612 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_LOW
, "glCopyTexImage "
3613 "can't avoid reallocating texture storage\n");
3615 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, internalFormat
);
3617 if (_mesa_is_gles3(ctx
)) {
3618 if (_mesa_is_enum_format_unsized(internalFormat
)) {
3619 /* Conversion from GL_RGB10_A2 source buffer format is not allowed in
3620 * OpenGL ES 3.0. Khronos bug# 9807.
3622 if (rb
->InternalFormat
== GL_RGB10_A2
) {
3623 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3624 "glCopyTexImage%uD(Reading from GL_RGB10_A2 buffer"
3625 " and writing to unsized internal format)", dims
);
3629 /* From Page 139 of OpenGL ES 3.0 spec:
3630 * "If internalformat is sized, the internal format of the new texel
3631 * array is internalformat, and this is also the new texel array’s
3632 * effective internal format. If the component sizes of internalformat
3633 * do not exactly match the corresponding component sizes of the source
3634 * buffer’s effective internal format, described below, an
3635 * INVALID_OPERATION error is generated. If internalformat is unsized,
3636 * the internal format of the new texel array is the effective internal
3637 * format of the source buffer, and this is also the new texel array’s
3638 * effective internal format.
3640 else if (formats_differ_in_component_sizes (texFormat
, rb
->Format
)) {
3641 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3642 "glCopyTexImage%uD(componenet size changed in"
3643 " internal format)", dims
);
3648 assert(texFormat
!= MESA_FORMAT_NONE
);
3650 if (!ctx
->Driver
.TestProxyTexImage(ctx
, proxy_target(target
),
3651 0, level
, texFormat
, 1,
3652 width
, height
, 1)) {
3653 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
3654 "glCopyTexImage%uD(image too large)", dims
);
3658 if (border
&& ctx
->Const
.StripTextureBorder
) {
3660 width
-= border
* 2;
3663 height
-= border
* 2;
3668 _mesa_lock_texture(ctx
, texObj
);
3670 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
3673 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
3676 GLint srcX
= x
, srcY
= y
, dstX
= 0, dstY
= 0, dstZ
= 0;
3678 /* Free old texture image */
3679 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
3681 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, 1,
3682 border
, internalFormat
, texFormat
);
3684 if (width
&& height
) {
3685 /* Allocate texture memory (no pixel data yet) */
3686 ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
);
3688 if (_mesa_clip_copytexsubimage(ctx
, &dstX
, &dstY
, &srcX
, &srcY
,
3690 struct gl_renderbuffer
*srcRb
=
3691 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3693 copytexsubimage_by_slice(ctx
, texImage
, dims
,
3695 srcRb
, srcX
, srcY
, width
, height
);
3698 check_gen_mipmap(ctx
, target
, texObj
, level
);
3701 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
3703 _mesa_dirty_texobj(ctx
, texObj
);
3706 _mesa_unlock_texture(ctx
, texObj
);
3712 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
3713 GLenum internalFormat
,
3715 GLsizei width
, GLint border
)
3717 GET_CURRENT_CONTEXT(ctx
);
3718 copyteximage(ctx
, 1, target
, level
, internalFormat
, x
, y
, width
, 1, border
);
3724 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
3725 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
3728 GET_CURRENT_CONTEXT(ctx
);
3729 copyteximage(ctx
, 2, target
, level
, internalFormat
,
3730 x
, y
, width
, height
, border
);
3734 * Implementation for glCopyTex(ture)SubImage1/2/3D() functions.
3737 _mesa_copy_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
3738 struct gl_texture_object
*texObj
,
3739 GLenum target
, GLint level
,
3740 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3742 GLsizei width
, GLsizei height
,
3745 struct gl_texture_image
*texImage
;
3747 FLUSH_VERTICES(ctx
, 0);
3749 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
3750 _mesa_debug(ctx
, "%s %s %d %d %d %d %d %d %d %d\n", caller
,
3751 _mesa_enum_to_string(target
),
3752 level
, xoffset
, yoffset
, zoffset
, x
, y
, width
, height
);
3754 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
3755 _mesa_update_state(ctx
);
3757 if (copytexsubimage_error_check(ctx
, dims
, texObj
, target
, level
,
3758 xoffset
, yoffset
, zoffset
,
3759 width
, height
, caller
)) {
3763 _mesa_lock_texture(ctx
, texObj
);
3765 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
3767 /* If we have a border, offset=-1 is legal. Bias by border width. */
3770 if (target
!= GL_TEXTURE_2D_ARRAY
)
3771 zoffset
+= texImage
->Border
;
3774 if (target
!= GL_TEXTURE_1D_ARRAY
)
3775 yoffset
+= texImage
->Border
;
3778 xoffset
+= texImage
->Border
;
3781 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
3783 struct gl_renderbuffer
*srcRb
=
3784 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
3786 copytexsubimage_by_slice(ctx
, texImage
, dims
,
3787 xoffset
, yoffset
, zoffset
,
3788 srcRb
, x
, y
, width
, height
);
3790 check_gen_mipmap(ctx
, target
, texObj
, level
);
3792 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3793 * the texel data, not the texture format, size, etc.
3797 _mesa_unlock_texture(ctx
, texObj
);
3801 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
3802 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
3804 struct gl_texture_object
* texObj
;
3805 const char *self
= "glCopyTexSubImage1D";
3806 GET_CURRENT_CONTEXT(ctx
);
3808 /* Check target (proxies not allowed). Target must be checked prior to
3809 * calling _mesa_get_current_tex_object.
3811 if (!legal_texsubimage_target(ctx
, 1, target
, false)) {
3812 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3813 _mesa_enum_to_string(target
));
3817 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3821 _mesa_copy_texture_sub_image(ctx
, 1, texObj
, target
, level
, xoffset
, 0, 0,
3822 x
, y
, width
, 1, self
);
3828 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
3829 GLint xoffset
, GLint yoffset
,
3830 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3832 struct gl_texture_object
* texObj
;
3833 const char *self
= "glCopyTexSubImage2D";
3834 GET_CURRENT_CONTEXT(ctx
);
3836 /* Check target (proxies not allowed). Target must be checked prior to
3837 * calling _mesa_get_current_tex_object.
3839 if (!legal_texsubimage_target(ctx
, 2, target
, false)) {
3840 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3841 _mesa_enum_to_string(target
));
3845 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3849 _mesa_copy_texture_sub_image(ctx
, 2, texObj
, target
, level
,
3850 xoffset
, yoffset
, 0,
3851 x
, y
, width
, height
, self
);
3857 _mesa_CopyTexSubImage3D( GLenum target
, GLint level
,
3858 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3859 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3861 struct gl_texture_object
* texObj
;
3862 const char *self
= "glCopyTexSubImage3D";
3863 GET_CURRENT_CONTEXT(ctx
);
3865 /* Check target (proxies not allowed). Target must be checked prior to
3866 * calling _mesa_get_current_tex_object.
3868 if (!legal_texsubimage_target(ctx
, 3, target
, false)) {
3869 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3870 _mesa_enum_to_string(target
));
3874 texObj
= _mesa_get_current_tex_object(ctx
, target
);
3878 _mesa_copy_texture_sub_image(ctx
, 3, texObj
, target
, level
,
3879 xoffset
, yoffset
, zoffset
,
3880 x
, y
, width
, height
, self
);
3884 _mesa_CopyTextureSubImage1D(GLuint texture
, GLint level
,
3885 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
3887 struct gl_texture_object
* texObj
;
3888 const char *self
= "glCopyTextureSubImage1D";
3889 GET_CURRENT_CONTEXT(ctx
);
3891 texObj
= _mesa_lookup_texture_err(ctx
, texture
, self
);
3895 /* Check target (proxies not allowed). */
3896 if (!legal_texsubimage_target(ctx
, 1, texObj
->Target
, true)) {
3897 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3898 _mesa_enum_to_string(texObj
->Target
));
3902 _mesa_copy_texture_sub_image(ctx
, 1, texObj
, texObj
->Target
, level
,
3903 xoffset
, 0, 0, x
, y
, width
, 1, self
);
3907 _mesa_CopyTextureSubImage2D(GLuint texture
, GLint level
,
3908 GLint xoffset
, GLint yoffset
,
3909 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3911 struct gl_texture_object
* texObj
;
3912 const char *self
= "glCopyTextureSubImage2D";
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
, 2, texObj
->Target
, true)) {
3921 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3922 _mesa_enum_to_string(texObj
->Target
));
3926 _mesa_copy_texture_sub_image(ctx
, 2, texObj
, texObj
->Target
, level
,
3927 xoffset
, yoffset
, 0,
3928 x
, y
, width
, height
, self
);
3934 _mesa_CopyTextureSubImage3D(GLuint texture
, GLint level
,
3935 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3936 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
3938 struct gl_texture_object
* texObj
;
3939 const char *self
= "glCopyTextureSubImage3D";
3940 GET_CURRENT_CONTEXT(ctx
);
3942 texObj
= _mesa_lookup_texture_err(ctx
, texture
, self
);
3946 /* Check target (proxies not allowed). */
3947 if (!legal_texsubimage_target(ctx
, 3, texObj
->Target
, true)) {
3948 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", self
,
3949 _mesa_enum_to_string(texObj
->Target
));
3953 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
3954 /* Act like CopyTexSubImage2D */
3955 _mesa_copy_texture_sub_image(ctx
, 2, texObj
,
3956 GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ zoffset
,
3957 level
, xoffset
, yoffset
, 0,
3958 x
, y
, width
, height
, self
);
3961 _mesa_copy_texture_sub_image(ctx
, 3, texObj
, texObj
->Target
, level
,
3962 xoffset
, yoffset
, zoffset
,
3963 x
, y
, width
, height
, self
);
3967 check_clear_tex_image(struct gl_context
*ctx
,
3968 const char *function
,
3969 struct gl_texture_image
*texImage
,
3970 GLenum format
, GLenum type
,
3972 GLubyte
*clearValue
)
3974 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3975 static const GLubyte zeroData
[MAX_PIXEL_BYTES
];
3976 GLenum internalFormat
= texImage
->InternalFormat
;
3979 if (texObj
->Target
== GL_TEXTURE_BUFFER
) {
3980 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3981 "%s(buffer texture)", function
);
3985 if (_mesa_is_compressed_format(ctx
, internalFormat
)) {
3986 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3987 "%s(compressed texture)", function
);
3991 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
3992 if (err
!= GL_NO_ERROR
) {
3993 _mesa_error(ctx
, err
,
3994 "%s(incompatible format = %s, type = %s)",
3996 _mesa_enum_to_string(format
),
3997 _mesa_enum_to_string(type
));
4001 /* make sure internal format and format basically agree */
4002 if (!texture_formats_agree(internalFormat
, format
)) {
4003 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4004 "%s(incompatible internalFormat = %s, format = %s)",
4006 _mesa_enum_to_string(internalFormat
),
4007 _mesa_enum_to_string(format
));
4011 if (ctx
->Version
>= 30 || ctx
->Extensions
.EXT_texture_integer
) {
4012 /* both source and dest must be integer-valued, or neither */
4013 if (_mesa_is_format_integer_color(texImage
->TexFormat
) !=
4014 _mesa_is_enum_format_integer(format
)) {
4015 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4016 "%s(integer/non-integer format mismatch)",
4022 if (!_mesa_texstore(ctx
,
4024 texImage
->_BaseFormat
,
4025 texImage
->TexFormat
,
4026 0, /* dstRowStride */
4028 1, 1, 1, /* srcWidth/Height/Depth */
4030 data
? data
: zeroData
,
4031 &ctx
->DefaultPacking
)) {
4032 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid format)", function
);
4039 static struct gl_texture_object
*
4040 get_tex_obj_for_clear(struct gl_context
*ctx
,
4041 const char *function
,
4044 struct gl_texture_object
*texObj
;
4047 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(zero texture)", function
);
4051 texObj
= _mesa_lookup_texture(ctx
, texture
);
4053 if (texObj
== NULL
) {
4054 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", function
);
4058 if (texObj
->Target
== 0) {
4059 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unbound tex)", function
);
4067 get_tex_images_for_clear(struct gl_context
*ctx
,
4068 const char *function
,
4069 struct gl_texture_object
*texObj
,
4071 struct gl_texture_image
**texImages
)
4076 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
4077 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid level)", function
);
4081 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
4082 for (i
= 0; i
< MAX_FACES
; i
++) {
4083 target
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ i
;
4085 texImages
[i
] = _mesa_select_tex_image(texObj
, target
, level
);
4086 if (texImages
[i
] == NULL
) {
4087 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4088 "%s(invalid level)", function
);
4096 texImages
[0] = _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4098 if (texImages
[0] == NULL
) {
4099 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid level)", function
);
4107 _mesa_ClearTexSubImage( GLuint texture
, GLint level
,
4108 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4109 GLsizei width
, GLsizei height
, GLsizei depth
,
4110 GLenum format
, GLenum type
, const void *data
)
4112 GET_CURRENT_CONTEXT(ctx
);
4113 struct gl_texture_object
*texObj
;
4114 struct gl_texture_image
*texImages
[MAX_FACES
];
4115 GLubyte clearValue
[MAX_FACES
][MAX_PIXEL_BYTES
];
4117 int minDepth
, maxDepth
;
4119 texObj
= get_tex_obj_for_clear(ctx
, "glClearTexSubImage", texture
);
4124 _mesa_lock_texture(ctx
, texObj
);
4126 numImages
= get_tex_images_for_clear(ctx
, "glClearTexSubImage",
4127 texObj
, level
, texImages
);
4131 if (numImages
== 1) {
4132 minDepth
= -(int) texImages
[0]->Border
;
4133 maxDepth
= texImages
[0]->Depth
;
4136 maxDepth
= numImages
;
4139 if (xoffset
< -(GLint
) texImages
[0]->Border
||
4140 yoffset
< -(GLint
) texImages
[0]->Border
||
4141 zoffset
< minDepth
||
4145 xoffset
+ width
> texImages
[0]->Width
||
4146 yoffset
+ height
> texImages
[0]->Height
||
4147 zoffset
+ depth
> maxDepth
) {
4148 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4149 "glClearSubTexImage(invalid dimensions)");
4153 if (numImages
== 1) {
4154 if (check_clear_tex_image(ctx
, "glClearTexSubImage",
4156 format
, type
, data
, clearValue
[0])) {
4157 ctx
->Driver
.ClearTexSubImage(ctx
,
4159 xoffset
, yoffset
, zoffset
,
4160 width
, height
, depth
,
4161 data
? clearValue
[0] : NULL
);
4164 for (i
= zoffset
; i
< zoffset
+ depth
; i
++) {
4165 if (!check_clear_tex_image(ctx
, "glClearTexSubImage",
4167 format
, type
, data
, clearValue
[i
]))
4170 for (i
= zoffset
; i
< zoffset
+ depth
; i
++) {
4171 ctx
->Driver
.ClearTexSubImage(ctx
,
4173 xoffset
, yoffset
, 0,
4175 data
? clearValue
[i
] : NULL
);
4180 _mesa_unlock_texture(ctx
, texObj
);
4184 _mesa_ClearTexImage( GLuint texture
, GLint level
,
4185 GLenum format
, GLenum type
, const void *data
)
4187 GET_CURRENT_CONTEXT(ctx
);
4188 struct gl_texture_object
*texObj
;
4189 struct gl_texture_image
*texImages
[MAX_FACES
];
4190 GLubyte clearValue
[MAX_FACES
][MAX_PIXEL_BYTES
];
4193 texObj
= get_tex_obj_for_clear(ctx
, "glClearTexImage", texture
);
4198 _mesa_lock_texture(ctx
, texObj
);
4200 numImages
= get_tex_images_for_clear(ctx
, "glClearTexImage",
4201 texObj
, level
, texImages
);
4203 for (i
= 0; i
< numImages
; i
++) {
4204 if (!check_clear_tex_image(ctx
, "glClearTexImage",
4211 for (i
= 0; i
< numImages
; i
++) {
4212 ctx
->Driver
.ClearTexSubImage(ctx
, texImages
[i
],
4213 -(GLint
) texImages
[i
]->Border
, /* xoffset */
4214 -(GLint
) texImages
[i
]->Border
, /* yoffset */
4215 -(GLint
) texImages
[i
]->Border
, /* zoffset */
4216 texImages
[i
]->Width
,
4217 texImages
[i
]->Height
,
4218 texImages
[i
]->Depth
,
4219 data
? clearValue
[i
] : NULL
);
4223 _mesa_unlock_texture(ctx
, texObj
);
4229 /**********************************************************************/
4230 /****** Compressed Textures ******/
4231 /**********************************************************************/
4235 * Target checking for glCompressedTexSubImage[123]D().
4236 * \return GL_TRUE if error, GL_FALSE if no error
4237 * Must come before other error checking so that the texture object can
4238 * be correctly retrieved using _mesa_get_current_tex_object.
4241 compressed_subtexture_target_check(struct gl_context
*ctx
, GLenum target
,
4242 GLint dims
, GLenum format
, bool dsa
,
4247 if (dsa
&& target
== GL_TEXTURE_RECTANGLE
) {
4248 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid target %s)", caller
,
4249 _mesa_enum_to_string(target
));
4259 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
4260 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
4261 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
4262 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
4263 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
4264 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
4265 targetOK
= ctx
->Extensions
.ARB_texture_cube_map
;
4268 targetOK
= GL_FALSE
;
4274 case GL_TEXTURE_CUBE_MAP
:
4275 targetOK
= dsa
&& ctx
->Extensions
.ARB_texture_cube_map
;
4277 case GL_TEXTURE_2D_ARRAY
:
4278 targetOK
= _mesa_is_gles3(ctx
) ||
4279 (_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.EXT_texture_array
);
4281 case GL_TEXTURE_CUBE_MAP_ARRAY
:
4282 targetOK
= ctx
->Extensions
.ARB_texture_cube_map_array
;
4287 * OpenGL 4.5 spec (30.10.2014) says in Section 8.7 Compressed Texture
4289 * "An INVALID_OPERATION error is generated by
4290 * CompressedTex*SubImage3D if the internal format of the texture
4291 * is one of the EAC, ETC2, or RGTC formats and either border is
4292 * non-zero, or the effective target for the texture is not
4293 * TEXTURE_2D_ARRAY."
4295 * NOTE: that's probably a spec error. It should probably say
4296 * "... or the effective target for the texture is not
4297 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP, nor
4298 * GL_TEXTURE_CUBE_MAP_ARRAY."
4299 * since those targets are 2D images and they support all compression
4302 * Instead of listing all these, just list those which are allowed,
4303 * which is (at this time) only bptc. Otherwise we'd say s3tc (and
4304 * more) are valid here, which they are not, but of course not
4305 * mentioned by core spec.
4308 /* These are the only 3D compression formats supported at this time */
4309 case GL_COMPRESSED_RGBA_BPTC_UNORM
:
4310 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM
:
4311 case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT
:
4312 case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT
:
4316 /* invalid format */
4317 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4318 "%s(invalid target %s for format %s)", caller
,
4319 _mesa_enum_to_string(target
),
4320 _mesa_enum_to_string(format
));
4325 targetOK
= GL_FALSE
;
4331 /* no 1D compressed textures at this time */
4332 targetOK
= GL_FALSE
;
4337 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid target %s)", caller
,
4338 _mesa_enum_to_string(target
));
4346 * Error checking for glCompressedTexSubImage[123]D().
4347 * \return GL_TRUE if error, GL_FALSE if no error
4350 compressed_subtexture_error_check(struct gl_context
*ctx
, GLint dims
,
4351 const struct gl_texture_object
*texObj
,
4352 GLenum target
, GLint level
,
4353 GLint xoffset
, GLint yoffset
, GLint zoffset
,
4354 GLsizei width
, GLsizei height
, GLsizei depth
,
4355 GLenum format
, GLsizei imageSize
,
4356 const GLvoid
*data
, const char *callerName
)
4358 struct gl_texture_image
*texImage
;
4361 /* this will catch any invalid compressed format token */
4362 if (!_mesa_is_compressed_format(ctx
, format
)) {
4363 _mesa_error(ctx
, GL_INVALID_ENUM
,
4364 "%s(format)", callerName
);
4368 if (level
< 0 || level
>= _mesa_max_texture_levels(ctx
, target
)) {
4369 _mesa_error(ctx
, GL_INVALID_VALUE
,
4375 /* validate the bound PBO, if any */
4376 if (!_mesa_validate_pbo_source_compressed(ctx
, dims
, &ctx
->Unpack
,
4377 imageSize
, data
, callerName
)) {
4381 /* Check for invalid pixel storage modes */
4382 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dims
,
4383 &ctx
->Unpack
, callerName
)) {
4387 expectedSize
= compressed_tex_size(width
, height
, depth
, format
);
4388 if (expectedSize
!= imageSize
) {
4389 _mesa_error(ctx
, GL_INVALID_VALUE
,
4391 callerName
, imageSize
);
4395 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4397 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4398 "%s(invalid texture image)",
4403 if ((GLint
) format
!= texImage
->InternalFormat
) {
4404 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4406 callerName
, _mesa_enum_to_string(format
));
4410 if (compressedteximage_only_format(ctx
, format
)) {
4411 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4412 "%s(format=%s cannot be updated)",
4413 callerName
, _mesa_enum_to_string(format
));
4417 if (error_check_subtexture_negative_dimensions(ctx
, dims
,
4418 width
, height
, depth
,
4423 if (error_check_subtexture_dimensions(ctx
, dims
,
4424 texImage
, xoffset
, yoffset
, zoffset
,
4425 width
, height
, depth
,
4435 _mesa_CompressedTexImage1D(GLenum target
, GLint level
,
4436 GLenum internalFormat
, GLsizei width
,
4437 GLint border
, GLsizei imageSize
,
4440 GET_CURRENT_CONTEXT(ctx
);
4441 teximage(ctx
, GL_TRUE
, 1, target
, level
, internalFormat
,
4442 width
, 1, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4447 _mesa_CompressedTexImage2D(GLenum target
, GLint level
,
4448 GLenum internalFormat
, GLsizei width
,
4449 GLsizei height
, GLint border
, GLsizei imageSize
,
4452 GET_CURRENT_CONTEXT(ctx
);
4453 teximage(ctx
, GL_TRUE
, 2, target
, level
, internalFormat
,
4454 width
, height
, 1, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4459 _mesa_CompressedTexImage3D(GLenum target
, GLint level
,
4460 GLenum internalFormat
, GLsizei width
,
4461 GLsizei height
, GLsizei depth
, GLint border
,
4462 GLsizei imageSize
, const GLvoid
*data
)
4464 GET_CURRENT_CONTEXT(ctx
);
4465 teximage(ctx
, GL_TRUE
, 3, target
, level
, internalFormat
,
4466 width
, height
, depth
, border
, GL_NONE
, GL_NONE
, imageSize
, data
);
4471 * Common helper for glCompressedTexSubImage1/2/3D() and
4472 * glCompressedTextureSubImage1/2/3D().
4475 _mesa_compressed_texture_sub_image(struct gl_context
*ctx
, GLuint dims
,
4476 struct gl_texture_object
*texObj
,
4477 struct gl_texture_image
*texImage
,
4478 GLenum target
, GLint level
,
4479 GLint xoffset
, GLint yoffset
,
4481 GLsizei width
, GLsizei height
,
4483 GLenum format
, GLsizei imageSize
,
4486 FLUSH_VERTICES(ctx
, 0);
4488 _mesa_lock_texture(ctx
, texObj
);
4490 if (width
> 0 && height
> 0 && depth
> 0) {
4491 ctx
->Driver
.CompressedTexSubImage(ctx
, dims
, texImage
,
4492 xoffset
, yoffset
, zoffset
,
4493 width
, height
, depth
,
4494 format
, imageSize
, data
);
4496 check_gen_mipmap(ctx
, target
, texObj
, level
);
4498 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
4499 * the texel data, not the texture format, size, etc.
4503 _mesa_unlock_texture(ctx
, texObj
);
4508 _mesa_CompressedTexSubImage1D(GLenum target
, GLint level
, GLint xoffset
,
4509 GLsizei width
, GLenum format
,
4510 GLsizei imageSize
, const GLvoid
*data
)
4512 struct gl_texture_object
*texObj
;
4513 struct gl_texture_image
*texImage
;
4515 GET_CURRENT_CONTEXT(ctx
);
4517 if (compressed_subtexture_target_check(ctx
, target
, 1, format
, false,
4518 "glCompressedTexSubImage1D")) {
4522 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4526 if (compressed_subtexture_error_check(ctx
, 1, texObj
, target
,
4527 level
, xoffset
, 0, 0,
4529 format
, imageSize
, data
,
4530 "glCompressedTexSubImage1D")) {
4534 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4537 _mesa_compressed_texture_sub_image(ctx
, 1, texObj
, texImage
, target
, level
,
4538 xoffset
, 0, 0, width
, 1, 1,
4539 format
, imageSize
, data
);
4543 _mesa_CompressedTextureSubImage1D(GLuint texture
, GLint level
, GLint xoffset
,
4544 GLsizei width
, GLenum format
,
4545 GLsizei imageSize
, const GLvoid
*data
)
4547 struct gl_texture_object
*texObj
;
4548 struct gl_texture_image
*texImage
;
4550 GET_CURRENT_CONTEXT(ctx
);
4552 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4553 "glCompressedTextureSubImage1D");
4557 if (compressed_subtexture_target_check(ctx
, texObj
->Target
, 1, format
, true,
4558 "glCompressedTextureSubImage1D")) {
4562 if (compressed_subtexture_error_check(ctx
, 1, texObj
, texObj
->Target
,
4563 level
, xoffset
, 0, 0,
4565 format
, imageSize
, data
,
4566 "glCompressedTextureSubImage1D")) {
4570 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4573 _mesa_compressed_texture_sub_image(ctx
, 1, texObj
, texImage
,
4574 texObj
->Target
, level
,
4575 xoffset
, 0, 0, width
, 1, 1,
4576 format
, imageSize
, data
);
4581 _mesa_CompressedTexSubImage2D(GLenum target
, GLint level
, GLint xoffset
,
4582 GLint yoffset
, GLsizei width
, GLsizei height
,
4583 GLenum format
, GLsizei imageSize
,
4586 struct gl_texture_object
*texObj
;
4587 struct gl_texture_image
*texImage
;
4589 GET_CURRENT_CONTEXT(ctx
);
4591 if (compressed_subtexture_target_check(ctx
, target
, 2, format
, false,
4592 "glCompressedTexSubImage2D")) {
4596 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4600 if (compressed_subtexture_error_check(ctx
, 2, texObj
, target
,
4601 level
, xoffset
, yoffset
, 0,
4603 format
, imageSize
, data
,
4604 "glCompressedTexSubImage2D")) {
4609 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4612 _mesa_compressed_texture_sub_image(ctx
, 2, texObj
, texImage
, target
, level
,
4613 xoffset
, yoffset
, 0, width
, height
, 1,
4614 format
, imageSize
, data
);
4618 _mesa_CompressedTextureSubImage2D(GLuint texture
, GLint level
, GLint xoffset
,
4620 GLsizei width
, GLsizei height
,
4621 GLenum format
, GLsizei imageSize
,
4624 struct gl_texture_object
*texObj
;
4625 struct gl_texture_image
*texImage
;
4627 GET_CURRENT_CONTEXT(ctx
);
4629 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4630 "glCompressedTextureSubImage2D");
4634 if (compressed_subtexture_target_check(ctx
, texObj
->Target
, 2, format
, true,
4635 "glCompressedTextureSubImage2D")) {
4639 if (compressed_subtexture_error_check(ctx
, 2, texObj
, texObj
->Target
,
4640 level
, xoffset
, yoffset
, 0,
4642 format
, imageSize
, data
,
4643 "glCompressedTextureSubImage2D")) {
4647 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4650 _mesa_compressed_texture_sub_image(ctx
, 2, texObj
, texImage
,
4651 texObj
->Target
, level
,
4652 xoffset
, yoffset
, 0, width
, height
, 1,
4653 format
, imageSize
, data
);
4657 _mesa_CompressedTexSubImage3D(GLenum target
, GLint level
, GLint xoffset
,
4658 GLint yoffset
, GLint zoffset
, GLsizei width
,
4659 GLsizei height
, GLsizei depth
, GLenum format
,
4660 GLsizei imageSize
, const GLvoid
*data
)
4662 struct gl_texture_object
*texObj
;
4663 struct gl_texture_image
*texImage
;
4665 GET_CURRENT_CONTEXT(ctx
);
4667 if (compressed_subtexture_target_check(ctx
, target
, 3, format
, false,
4668 "glCompressedTexSubImage3D")) {
4672 texObj
= _mesa_get_current_tex_object(ctx
, target
);
4676 if (compressed_subtexture_error_check(ctx
, 3, texObj
, target
,
4677 level
, xoffset
, yoffset
, zoffset
,
4678 width
, height
, depth
,
4679 format
, imageSize
, data
,
4680 "glCompressedTexSubImage3D")) {
4685 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
4688 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, texImage
, target
, level
,
4689 xoffset
, yoffset
, zoffset
,
4690 width
, height
, depth
,
4691 format
, imageSize
, data
);
4695 _mesa_CompressedTextureSubImage3D(GLuint texture
, GLint level
, GLint xoffset
,
4696 GLint yoffset
, GLint zoffset
, GLsizei width
,
4697 GLsizei height
, GLsizei depth
,
4698 GLenum format
, GLsizei imageSize
,
4701 struct gl_texture_object
*texObj
;
4702 struct gl_texture_image
*texImage
;
4704 GET_CURRENT_CONTEXT(ctx
);
4706 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
4707 "glCompressedTextureSubImage3D");
4711 if (compressed_subtexture_target_check(ctx
, texObj
->Target
, 3, format
, true,
4712 "glCompressedTextureSubImage3D")) {
4716 if (compressed_subtexture_error_check(ctx
, 3, texObj
, texObj
->Target
,
4717 level
, xoffset
, yoffset
, zoffset
,
4718 width
, height
, depth
,
4719 format
, imageSize
, data
,
4720 "glCompressedTextureSubImage3D")) {
4724 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
4725 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
4726 const char *pixels
= data
;
4730 /* Make sure the texture object is a proper cube.
4731 * (See texturesubimage in teximage.c for details on why this check is
4734 if (!_mesa_cube_level_complete(texObj
, level
)) {
4735 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4736 "glCompressedTextureSubImage3D(cube map incomplete)");
4740 /* Copy in each face. */
4741 for (i
= 0; i
< 6; ++i
) {
4742 texImage
= texObj
->Image
[i
][level
];
4745 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, texImage
,
4746 texObj
->Target
, level
,
4747 xoffset
, yoffset
, zoffset
,
4749 format
, imageSize
, pixels
);
4751 /* Compressed images don't have a client format */
4752 image_stride
= _mesa_format_image_size(texImage
->TexFormat
,
4754 texImage
->Height
, 1);
4756 pixels
+= image_stride
;
4757 imageSize
-= image_stride
;
4761 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
4764 _mesa_compressed_texture_sub_image(ctx
, 3, texObj
, texImage
,
4765 texObj
->Target
, level
,
4766 xoffset
, yoffset
, zoffset
,
4767 width
, height
, depth
,
4768 format
, imageSize
, data
);
4773 get_texbuffer_format(const struct gl_context
*ctx
, GLenum internalFormat
)
4775 if (ctx
->API
== API_OPENGL_COMPAT
) {
4776 switch (internalFormat
) {
4778 return MESA_FORMAT_A_UNORM8
;
4780 return MESA_FORMAT_A_UNORM16
;
4781 case GL_ALPHA16F_ARB
:
4782 return MESA_FORMAT_A_FLOAT16
;
4783 case GL_ALPHA32F_ARB
:
4784 return MESA_FORMAT_A_FLOAT32
;
4785 case GL_ALPHA8I_EXT
:
4786 return MESA_FORMAT_A_SINT8
;
4787 case GL_ALPHA16I_EXT
:
4788 return MESA_FORMAT_A_SINT16
;
4789 case GL_ALPHA32I_EXT
:
4790 return MESA_FORMAT_A_SINT32
;
4791 case GL_ALPHA8UI_EXT
:
4792 return MESA_FORMAT_A_UINT8
;
4793 case GL_ALPHA16UI_EXT
:
4794 return MESA_FORMAT_A_UINT16
;
4795 case GL_ALPHA32UI_EXT
:
4796 return MESA_FORMAT_A_UINT32
;
4798 return MESA_FORMAT_L_UNORM8
;
4799 case GL_LUMINANCE16
:
4800 return MESA_FORMAT_L_UNORM16
;
4801 case GL_LUMINANCE16F_ARB
:
4802 return MESA_FORMAT_L_FLOAT16
;
4803 case GL_LUMINANCE32F_ARB
:
4804 return MESA_FORMAT_L_FLOAT32
;
4805 case GL_LUMINANCE8I_EXT
:
4806 return MESA_FORMAT_L_SINT8
;
4807 case GL_LUMINANCE16I_EXT
:
4808 return MESA_FORMAT_L_SINT16
;
4809 case GL_LUMINANCE32I_EXT
:
4810 return MESA_FORMAT_L_SINT32
;
4811 case GL_LUMINANCE8UI_EXT
:
4812 return MESA_FORMAT_L_UINT8
;
4813 case GL_LUMINANCE16UI_EXT
:
4814 return MESA_FORMAT_L_UINT16
;
4815 case GL_LUMINANCE32UI_EXT
:
4816 return MESA_FORMAT_L_UINT32
;
4817 case GL_LUMINANCE8_ALPHA8
:
4818 return MESA_FORMAT_L8A8_UNORM
;
4819 case GL_LUMINANCE16_ALPHA16
:
4820 return MESA_FORMAT_L16A16_UNORM
;
4821 case GL_LUMINANCE_ALPHA16F_ARB
:
4822 return MESA_FORMAT_LA_FLOAT16
;
4823 case GL_LUMINANCE_ALPHA32F_ARB
:
4824 return MESA_FORMAT_LA_FLOAT32
;
4825 case GL_LUMINANCE_ALPHA8I_EXT
:
4826 return MESA_FORMAT_LA_SINT8
;
4827 case GL_LUMINANCE_ALPHA16I_EXT
:
4828 return MESA_FORMAT_LA_SINT16
;
4829 case GL_LUMINANCE_ALPHA32I_EXT
:
4830 return MESA_FORMAT_LA_SINT32
;
4831 case GL_LUMINANCE_ALPHA8UI_EXT
:
4832 return MESA_FORMAT_LA_UINT8
;
4833 case GL_LUMINANCE_ALPHA16UI_EXT
:
4834 return MESA_FORMAT_LA_UINT16
;
4835 case GL_LUMINANCE_ALPHA32UI_EXT
:
4836 return MESA_FORMAT_LA_UINT32
;
4838 return MESA_FORMAT_I_UNORM8
;
4839 case GL_INTENSITY16
:
4840 return MESA_FORMAT_I_UNORM16
;
4841 case GL_INTENSITY16F_ARB
:
4842 return MESA_FORMAT_I_FLOAT16
;
4843 case GL_INTENSITY32F_ARB
:
4844 return MESA_FORMAT_I_FLOAT32
;
4845 case GL_INTENSITY8I_EXT
:
4846 return MESA_FORMAT_I_SINT8
;
4847 case GL_INTENSITY16I_EXT
:
4848 return MESA_FORMAT_I_SINT16
;
4849 case GL_INTENSITY32I_EXT
:
4850 return MESA_FORMAT_I_SINT32
;
4851 case GL_INTENSITY8UI_EXT
:
4852 return MESA_FORMAT_I_UINT8
;
4853 case GL_INTENSITY16UI_EXT
:
4854 return MESA_FORMAT_I_UINT16
;
4855 case GL_INTENSITY32UI_EXT
:
4856 return MESA_FORMAT_I_UINT32
;
4862 if (_mesa_has_ARB_texture_buffer_object_rgb32(ctx
) ||
4863 _mesa_has_OES_texture_buffer(ctx
)) {
4864 switch (internalFormat
) {
4866 return MESA_FORMAT_RGB_FLOAT32
;
4868 return MESA_FORMAT_RGB_UINT32
;
4870 return MESA_FORMAT_RGB_SINT32
;
4876 switch (internalFormat
) {
4878 return MESA_FORMAT_R8G8B8A8_UNORM
;
4880 if (_mesa_is_gles(ctx
))
4881 return MESA_FORMAT_NONE
;
4882 return MESA_FORMAT_RGBA_UNORM16
;
4883 case GL_RGBA16F_ARB
:
4884 return MESA_FORMAT_RGBA_FLOAT16
;
4885 case GL_RGBA32F_ARB
:
4886 return MESA_FORMAT_RGBA_FLOAT32
;
4888 return MESA_FORMAT_RGBA_SINT8
;
4889 case GL_RGBA16I_EXT
:
4890 return MESA_FORMAT_RGBA_SINT16
;
4891 case GL_RGBA32I_EXT
:
4892 return MESA_FORMAT_RGBA_SINT32
;
4893 case GL_RGBA8UI_EXT
:
4894 return MESA_FORMAT_RGBA_UINT8
;
4895 case GL_RGBA16UI_EXT
:
4896 return MESA_FORMAT_RGBA_UINT16
;
4897 case GL_RGBA32UI_EXT
:
4898 return MESA_FORMAT_RGBA_UINT32
;
4901 return MESA_FORMAT_R8G8_UNORM
;
4903 if (_mesa_is_gles(ctx
))
4904 return MESA_FORMAT_NONE
;
4905 return MESA_FORMAT_R16G16_UNORM
;
4907 return MESA_FORMAT_RG_FLOAT16
;
4909 return MESA_FORMAT_RG_FLOAT32
;
4911 return MESA_FORMAT_RG_SINT8
;
4913 return MESA_FORMAT_RG_SINT16
;
4915 return MESA_FORMAT_RG_SINT32
;
4917 return MESA_FORMAT_RG_UINT8
;
4919 return MESA_FORMAT_RG_UINT16
;
4921 return MESA_FORMAT_RG_UINT32
;
4924 return MESA_FORMAT_R_UNORM8
;
4926 if (_mesa_is_gles(ctx
))
4927 return MESA_FORMAT_NONE
;
4928 return MESA_FORMAT_R_UNORM16
;
4930 return MESA_FORMAT_R_FLOAT16
;
4932 return MESA_FORMAT_R_FLOAT32
;
4934 return MESA_FORMAT_R_SINT8
;
4936 return MESA_FORMAT_R_SINT16
;
4938 return MESA_FORMAT_R_SINT32
;
4940 return MESA_FORMAT_R_UINT8
;
4942 return MESA_FORMAT_R_UINT16
;
4944 return MESA_FORMAT_R_UINT32
;
4947 return MESA_FORMAT_NONE
;
4953 _mesa_validate_texbuffer_format(const struct gl_context
*ctx
,
4954 GLenum internalFormat
)
4956 mesa_format format
= get_texbuffer_format(ctx
, internalFormat
);
4959 if (format
== MESA_FORMAT_NONE
)
4960 return MESA_FORMAT_NONE
;
4962 datatype
= _mesa_get_format_datatype(format
);
4964 /* The GL_ARB_texture_buffer_object spec says:
4966 * "If ARB_texture_float is not supported, references to the
4967 * floating-point internal formats provided by that extension should be
4968 * removed, and such formats may not be passed to TexBufferARB."
4970 * As a result, GL_HALF_FLOAT internal format depends on both
4971 * GL_ARB_texture_float and GL_ARB_half_float_pixel.
4973 if ((datatype
== GL_FLOAT
|| datatype
== GL_HALF_FLOAT
) &&
4974 !ctx
->Extensions
.ARB_texture_float
)
4975 return MESA_FORMAT_NONE
;
4977 if (!ctx
->Extensions
.ARB_texture_rg
) {
4978 GLenum base_format
= _mesa_get_format_base_format(format
);
4979 if (base_format
== GL_R
|| base_format
== GL_RG
)
4980 return MESA_FORMAT_NONE
;
4983 if (!ctx
->Extensions
.ARB_texture_buffer_object_rgb32
) {
4984 GLenum base_format
= _mesa_get_format_base_format(format
);
4985 if (base_format
== GL_RGB
)
4986 return MESA_FORMAT_NONE
;
4993 _mesa_texture_buffer_range(struct gl_context
*ctx
,
4994 struct gl_texture_object
*texObj
,
4995 GLenum internalFormat
,
4996 struct gl_buffer_object
*bufObj
,
4997 GLintptr offset
, GLsizeiptr size
,
5002 /* NOTE: ARB_texture_buffer_object has interactions with
5003 * the compatibility profile that are not implemented.
5005 if (!_mesa_has_ARB_texture_buffer_object(ctx
) &&
5006 !_mesa_has_OES_texture_buffer(ctx
)) {
5007 _mesa_error(ctx
, GL_INVALID_OPERATION
,
5008 "%s(ARB_texture_buffer_object is not"
5009 " implemented for the compatibility profile)", caller
);
5013 format
= _mesa_validate_texbuffer_format(ctx
, internalFormat
);
5014 if (format
== MESA_FORMAT_NONE
) {
5015 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalFormat %s)",
5016 caller
, _mesa_enum_to_string(internalFormat
));
5020 FLUSH_VERTICES(ctx
, 0);
5022 _mesa_lock_texture(ctx
, texObj
);
5024 _mesa_reference_buffer_object(ctx
, &texObj
->BufferObject
, bufObj
);
5025 texObj
->BufferObjectFormat
= internalFormat
;
5026 texObj
->_BufferObjectFormat
= format
;
5027 texObj
->BufferOffset
= offset
;
5028 texObj
->BufferSize
= size
;
5030 _mesa_unlock_texture(ctx
, texObj
);
5032 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTextureBuffer
;
5035 bufObj
->UsageHistory
|= USAGE_TEXTURE_BUFFER
;
5041 * Make sure the texture buffer target is GL_TEXTURE_BUFFER.
5042 * Return true if it is, and return false if it is not
5043 * (and throw INVALID ENUM as dictated in the OpenGL 4.5
5044 * core spec, 02.02.2015, PDF page 245).
5047 check_texture_buffer_target(struct gl_context
*ctx
, GLenum target
,
5050 if (target
!= GL_TEXTURE_BUFFER_ARB
) {
5051 _mesa_error(ctx
, GL_INVALID_ENUM
,
5052 "%s(texture target is not GL_TEXTURE_BUFFER)", caller
);
5060 * Check for errors related to the texture buffer range.
5061 * Return false if errors are found, true if none are found.
5064 check_texture_buffer_range(struct gl_context
*ctx
,
5065 struct gl_buffer_object
*bufObj
,
5066 GLintptr offset
, GLsizeiptr size
,
5069 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5070 * Textures (PDF page 245):
5071 * "An INVALID_VALUE error is generated if offset is negative, if
5072 * size is less than or equal to zero, or if offset + size is greater
5073 * than the value of BUFFER_SIZE for the buffer bound to target."
5076 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset=%d < 0)", caller
,
5082 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size=%d <= 0)", caller
,
5087 if (offset
+ size
> bufObj
->Size
) {
5088 _mesa_error(ctx
, GL_INVALID_VALUE
,
5089 "%s(offset=%d + size=%d > buffer_size=%d)", caller
,
5090 (int) offset
, (int) size
, (int) bufObj
->Size
);
5094 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5095 * Textures (PDF page 245):
5096 * "An INVALID_VALUE error is generated if offset is not an integer
5097 * multiple of the value of TEXTURE_BUFFER_OFFSET_ALIGNMENT."
5099 if (offset
% ctx
->Const
.TextureBufferOffsetAlignment
) {
5100 _mesa_error(ctx
, GL_INVALID_VALUE
,
5101 "%s(invalid offset alignment)", caller
);
5109 /** GL_ARB_texture_buffer_object */
5111 _mesa_TexBuffer(GLenum target
, GLenum internalFormat
, GLuint buffer
)
5113 struct gl_texture_object
*texObj
;
5114 struct gl_buffer_object
*bufObj
;
5116 GET_CURRENT_CONTEXT(ctx
);
5118 /* Need to catch a bad target before it gets to
5119 * _mesa_get_current_tex_object.
5121 if (!check_texture_buffer_target(ctx
, target
, "glTexBuffer"))
5125 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glTexBuffer");
5131 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5135 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
, bufObj
, 0,
5136 buffer
? -1 : 0, "glTexBuffer");
5140 /** GL_ARB_texture_buffer_range */
5142 _mesa_TexBufferRange(GLenum target
, GLenum internalFormat
, GLuint buffer
,
5143 GLintptr offset
, GLsizeiptr size
)
5145 struct gl_texture_object
*texObj
;
5146 struct gl_buffer_object
*bufObj
;
5148 GET_CURRENT_CONTEXT(ctx
);
5150 /* Need to catch a bad target before it gets to
5151 * _mesa_get_current_tex_object.
5153 if (!check_texture_buffer_target(ctx
, target
, "glTexBufferRange"))
5157 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glTexBufferRange");
5161 if (!check_texture_buffer_range(ctx
, bufObj
, offset
, size
,
5162 "glTexBufferRange"))
5166 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5167 * Textures (PDF page 254):
5168 * "If buffer is zero, then any buffer object attached to the buffer
5169 * texture is detached, the values offset and size are ignored and
5170 * the state for offset and size for the buffer texture are reset to
5178 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5182 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
, bufObj
,
5183 offset
, size
, "glTexBufferRange");
5187 _mesa_TextureBuffer(GLuint texture
, GLenum internalFormat
, GLuint buffer
)
5189 struct gl_texture_object
*texObj
;
5190 struct gl_buffer_object
*bufObj
;
5192 GET_CURRENT_CONTEXT(ctx
);
5195 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glTextureBuffer");
5201 /* Get the texture object by Name. */
5202 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glTextureBuffer");
5206 if (!check_texture_buffer_target(ctx
, texObj
->Target
, "glTextureBuffer"))
5209 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
,
5210 bufObj
, 0, buffer
? -1 : 0, "glTextureBuffer");
5214 _mesa_TextureBufferRange(GLuint texture
, GLenum internalFormat
, GLuint buffer
,
5215 GLintptr offset
, GLsizeiptr size
)
5217 struct gl_texture_object
*texObj
;
5218 struct gl_buffer_object
*bufObj
;
5220 GET_CURRENT_CONTEXT(ctx
);
5223 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
5224 "glTextureBufferRange");
5228 if (!check_texture_buffer_range(ctx
, bufObj
, offset
, size
,
5229 "glTextureBufferRange"))
5233 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5234 * Textures (PDF page 254):
5235 * "If buffer is zero, then any buffer object attached to the buffer
5236 * texture is detached, the values offset and size are ignored and
5237 * the state for offset and size for the buffer texture are reset to
5245 /* Get the texture object by Name. */
5246 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glTextureBufferRange");
5250 if (!check_texture_buffer_target(ctx
, texObj
->Target
,
5251 "glTextureBufferRange"))
5254 _mesa_texture_buffer_range(ctx
, texObj
, internalFormat
,
5255 bufObj
, offset
, size
, "glTextureBufferRange");
5259 _mesa_is_renderable_texture_format(struct gl_context
*ctx
, GLenum internalformat
)
5261 /* Everything that is allowed for renderbuffers,
5262 * except for a base format of GL_STENCIL_INDEX, unless supported.
5264 GLenum baseFormat
= _mesa_base_fbo_format(ctx
, internalformat
);
5265 if (ctx
->Extensions
.ARB_texture_stencil8
)
5266 return baseFormat
!= 0;
5268 return baseFormat
!= 0 && baseFormat
!= GL_STENCIL_INDEX
;
5272 /** GL_ARB_texture_multisample */
5274 check_multisample_target(GLuint dims
, GLenum target
, bool dsa
)
5277 case GL_TEXTURE_2D_MULTISAMPLE
:
5279 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
5280 return dims
== 2 && !dsa
;
5281 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
5283 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
5284 return dims
== 3 && !dsa
;
5292 texture_image_multisample(struct gl_context
*ctx
, GLuint dims
,
5293 struct gl_texture_object
*texObj
,
5294 GLenum target
, GLsizei samples
,
5295 GLint internalformat
, GLsizei width
,
5296 GLsizei height
, GLsizei depth
,
5297 GLboolean fixedsamplelocations
,
5298 GLboolean immutable
, const char *func
)
5300 struct gl_texture_image
*texImage
;
5301 GLboolean sizeOK
, dimensionsOK
, samplesOK
;
5302 mesa_format texFormat
;
5303 GLenum sample_count_error
;
5304 bool dsa
= strstr(func
, "ture") ? true : false;
5306 if (!((ctx
->Extensions
.ARB_texture_multisample
5307 && _mesa_is_desktop_gl(ctx
))) && !_mesa_is_gles31(ctx
)) {
5308 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unsupported)", func
);
5313 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(samples < 1)", func
);
5317 if (!check_multisample_target(dims
, target
, dsa
)) {
5319 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(target)", func
);
5323 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
5328 /* check that the specified internalformat is color/depth/stencil-renderable;
5329 * refer GL3.1 spec 4.4.4
5332 if (immutable
&& !_mesa_is_legal_tex_storage_format(ctx
, internalformat
)) {
5333 _mesa_error(ctx
, GL_INVALID_ENUM
,
5334 "%s(internalformat=%s not legal for immutable-format)",
5335 func
, _mesa_enum_to_string(internalformat
));
5339 if (!_mesa_is_renderable_texture_format(ctx
, internalformat
)) {
5340 /* Page 172 of OpenGL ES 3.1 spec says:
5341 * "An INVALID_ENUM error is generated if sizedinternalformat is not
5342 * color-renderable, depth-renderable, or stencil-renderable (as
5343 * defined in section 9.4).
5345 * (Same error is also defined for desktop OpenGL for multisample
5346 * teximage/texstorage functions.)
5348 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(internalformat=%s)", func
,
5349 _mesa_enum_to_string(internalformat
));
5353 sample_count_error
= _mesa_check_sample_count(ctx
, target
,
5354 internalformat
, samples
);
5355 samplesOK
= sample_count_error
== GL_NO_ERROR
;
5357 /* Page 254 of OpenGL 4.4 spec says:
5358 * "Proxy arrays for two-dimensional multisample and two-dimensional
5359 * multisample array textures are operated on in the same way when
5360 * TexImage2DMultisample is called with target specified as
5361 * PROXY_TEXTURE_2D_MULTISAMPLE, or TexImage3DMultisample is called
5362 * with target specified as PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY.
5363 * However, if samples is not supported, then no error is generated.
5365 if (!samplesOK
&& !_mesa_is_proxy_texture(target
)) {
5366 _mesa_error(ctx
, sample_count_error
, "%s(samples)", func
);
5370 if (immutable
&& (!texObj
|| (texObj
->Name
== 0))) {
5371 _mesa_error(ctx
, GL_INVALID_OPERATION
,
5372 "%s(texture object 0)",
5377 texImage
= _mesa_get_tex_image(ctx
, texObj
, 0, 0);
5379 if (texImage
== NULL
) {
5380 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", func
);
5384 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, 0,
5385 internalformat
, GL_NONE
, GL_NONE
);
5386 assert(texFormat
!= MESA_FORMAT_NONE
);
5388 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, 0,
5389 width
, height
, depth
, 0);
5391 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, target
, 0, 0, texFormat
,
5392 samples
, width
, height
, depth
);
5394 if (_mesa_is_proxy_texture(target
)) {
5395 if (samplesOK
&& dimensionsOK
&& sizeOK
) {
5396 init_teximage_fields_ms(ctx
, texImage
, width
, height
, depth
, 0,
5397 internalformat
, texFormat
,
5398 samples
, fixedsamplelocations
);
5401 /* clear all image fields */
5402 clear_teximage_fields(texImage
);
5406 if (!dimensionsOK
) {
5407 _mesa_error(ctx
, GL_INVALID_VALUE
,
5408 "%s(invalid width or height)", func
);
5413 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(texture too large)", func
);
5417 /* Check if texObj->Immutable is set */
5418 if (texObj
->Immutable
) {
5419 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
5423 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
5425 init_teximage_fields_ms(ctx
, texImage
, width
, height
, depth
, 0,
5426 internalformat
, texFormat
,
5427 samples
, fixedsamplelocations
);
5429 if (width
> 0 && height
> 0 && depth
> 0) {
5430 if (!ctx
->Driver
.AllocTextureStorage(ctx
, texObj
, 1,
5431 width
, height
, depth
)) {
5432 /* tidy up the texture image state. strictly speaking,
5433 * we're allowed to just leave this in whatever state we
5434 * like, but being tidy is good.
5436 _mesa_init_teximage_fields(ctx
, texImage
,
5437 0, 0, 0, 0, GL_NONE
, MESA_FORMAT_NONE
);
5441 texObj
->Immutable
|= immutable
;
5444 _mesa_set_texture_view_state(ctx
, texObj
, target
, 1);
5447 _mesa_update_fbo_texture(ctx
, texObj
, 0, 0);
5453 _mesa_TexImage2DMultisample(GLenum target
, GLsizei samples
,
5454 GLenum internalformat
, GLsizei width
,
5455 GLsizei height
, GLboolean fixedsamplelocations
)
5457 struct gl_texture_object
*texObj
;
5458 GET_CURRENT_CONTEXT(ctx
);
5460 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5464 texture_image_multisample(ctx
, 2, texObj
, target
, samples
,
5465 internalformat
, width
, height
, 1,
5466 fixedsamplelocations
, GL_FALSE
,
5467 "glTexImage2DMultisample");
5472 _mesa_TexImage3DMultisample(GLenum target
, GLsizei samples
,
5473 GLenum internalformat
, GLsizei width
,
5474 GLsizei height
, GLsizei depth
,
5475 GLboolean fixedsamplelocations
)
5477 struct gl_texture_object
*texObj
;
5478 GET_CURRENT_CONTEXT(ctx
);
5480 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5484 texture_image_multisample(ctx
, 3, texObj
, target
, samples
,
5485 internalformat
, width
, height
, depth
,
5486 fixedsamplelocations
, GL_FALSE
,
5487 "glTexImage3DMultisample");
5491 valid_texstorage_ms_parameters(GLsizei width
, GLsizei height
, GLsizei depth
,
5492 GLsizei samples
, unsigned dims
)
5494 GET_CURRENT_CONTEXT(ctx
);
5496 if (!_mesa_valid_tex_storage_dim(width
, height
, depth
)) {
5497 _mesa_error(ctx
, GL_INVALID_VALUE
,
5498 "glTexStorage%uDMultisample(width=%d,height=%d,depth=%d)",
5499 dims
, width
, height
, depth
);
5506 _mesa_TexStorage2DMultisample(GLenum target
, GLsizei samples
,
5507 GLenum internalformat
, GLsizei width
,
5508 GLsizei height
, GLboolean fixedsamplelocations
)
5510 struct gl_texture_object
*texObj
;
5511 GET_CURRENT_CONTEXT(ctx
);
5513 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5517 if (!valid_texstorage_ms_parameters(width
, height
, 1, samples
, 2))
5520 texture_image_multisample(ctx
, 2, texObj
, target
, samples
,
5521 internalformat
, width
, height
, 1,
5522 fixedsamplelocations
, GL_TRUE
,
5523 "glTexStorage2DMultisample");
5527 _mesa_TexStorage3DMultisample(GLenum target
, GLsizei samples
,
5528 GLenum internalformat
, GLsizei width
,
5529 GLsizei height
, GLsizei depth
,
5530 GLboolean fixedsamplelocations
)
5532 struct gl_texture_object
*texObj
;
5533 GET_CURRENT_CONTEXT(ctx
);
5535 texObj
= _mesa_get_current_tex_object(ctx
, target
);
5539 if (!valid_texstorage_ms_parameters(width
, height
, depth
, samples
, 3))
5542 texture_image_multisample(ctx
, 3, texObj
, target
, samples
,
5543 internalformat
, width
, height
, depth
,
5544 fixedsamplelocations
, GL_TRUE
,
5545 "glTexStorage3DMultisample");
5549 _mesa_TextureStorage2DMultisample(GLuint texture
, GLsizei samples
,
5550 GLenum internalformat
, GLsizei width
,
5552 GLboolean fixedsamplelocations
)
5554 struct gl_texture_object
*texObj
;
5555 GET_CURRENT_CONTEXT(ctx
);
5557 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
5558 "glTextureStorage2DMultisample");
5562 if (!valid_texstorage_ms_parameters(width
, height
, 1, samples
, 2))
5565 texture_image_multisample(ctx
, 2, texObj
, texObj
->Target
, samples
,
5566 internalformat
, width
, height
, 1,
5567 fixedsamplelocations
, GL_TRUE
,
5568 "glTextureStorage2DMultisample");
5572 _mesa_TextureStorage3DMultisample(GLuint texture
, GLsizei samples
,
5573 GLenum internalformat
, GLsizei width
,
5574 GLsizei height
, GLsizei depth
,
5575 GLboolean fixedsamplelocations
)
5577 struct gl_texture_object
*texObj
;
5578 GET_CURRENT_CONTEXT(ctx
);
5580 /* Get the texture object by Name. */
5581 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
5582 "glTextureStorage3DMultisample");
5586 if (!valid_texstorage_ms_parameters(width
, height
, depth
, samples
, 3))
5589 texture_image_multisample(ctx
, 3, texObj
, texObj
->Target
, samples
,
5590 internalformat
, width
, height
, depth
,
5591 fixedsamplelocations
, GL_TRUE
,
5592 "glTextureStorage3DMultisample");