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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Texture image-related functions.
34 * State changes which we care about for glCopyTex[Sub]Image() calls.
35 * In particular, we care about pixel transfer state and buffer state
36 * (such as glReadBuffer to make sure we read from the right renderbuffer).
38 #define NEW_COPY_TEX_STATE (_NEW_BUFFERS | _NEW_PIXEL)
43 * Return the simple base format for a given internal texture format.
44 * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
46 * \param ctx GL context.
47 * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
49 * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
50 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
52 * This is the format which is used during texture application (i.e. the
53 * texture format and env mode determine the arithmetic used.
55 * XXX this could be static
58 _mesa_base_tex_format( struct gl_context
*ctx
, GLint internalFormat
)
60 switch (internalFormat
) {
75 case GL_LUMINANCE_ALPHA
:
76 case GL_LUMINANCE4_ALPHA4
:
77 case GL_LUMINANCE6_ALPHA2
:
78 case GL_LUMINANCE8_ALPHA8
:
79 case GL_LUMINANCE12_ALPHA4
:
80 case GL_LUMINANCE12_ALPHA12
:
81 case GL_LUMINANCE16_ALPHA16
:
82 return GL_LUMINANCE_ALPHA
;
113 if (ctx
->Extensions
.MESA_ycbcr_texture
) {
114 if (internalFormat
== GL_YCBCR_MESA
)
115 return GL_YCBCR_MESA
;
118 if (ctx
->VersionMajor
>= 3 ||
119 ctx
->Extensions
.EXT_texture_integer
) {
120 switch (internalFormat
) {
122 case GL_RGBA16UI_EXT
:
123 case GL_RGBA32UI_EXT
:
139 if (ctx
->Extensions
.EXT_texture_integer
) {
140 switch (internalFormat
) {
141 case GL_ALPHA8UI_EXT
:
142 case GL_ALPHA16UI_EXT
:
143 case GL_ALPHA32UI_EXT
:
145 case GL_ALPHA16I_EXT
:
146 case GL_ALPHA32I_EXT
:
148 case GL_INTENSITY8UI_EXT
:
149 case GL_INTENSITY16UI_EXT
:
150 case GL_INTENSITY32UI_EXT
:
151 case GL_INTENSITY8I_EXT
:
152 case GL_INTENSITY16I_EXT
:
153 case GL_INTENSITY32I_EXT
:
155 case GL_LUMINANCE8UI_EXT
:
156 case GL_LUMINANCE16UI_EXT
:
157 case GL_LUMINANCE32UI_EXT
:
158 case GL_LUMINANCE8I_EXT
:
159 case GL_LUMINANCE16I_EXT
:
160 case GL_LUMINANCE32I_EXT
:
162 case GL_LUMINANCE_ALPHA8UI_EXT
:
163 case GL_LUMINANCE_ALPHA16UI_EXT
:
164 case GL_LUMINANCE_ALPHA32UI_EXT
:
165 case GL_LUMINANCE_ALPHA8I_EXT
:
166 case GL_LUMINANCE_ALPHA16I_EXT
:
167 case GL_LUMINANCE_ALPHA32I_EXT
:
168 return GL_LUMINANCE_ALPHA
;
174 return -1; /* error */
179 * Is the given texture format a generic compressed format?
184 * For cube map faces, return a face index in [0,5].
185 * For other targets return 0;
188 _mesa_tex_target_to_face(GLenum target
)
190 if (_mesa_is_cube_face(target
))
191 return (GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
;
199 * Install gl_texture_image in a gl_texture_object according to the target
200 * and level parameters.
202 * \param tObj texture object.
203 * \param target texture target.
204 * \param level image level.
205 * \param texImage texture image.
208 set_tex_image(struct gl_texture_object
*tObj
,
209 GLenum target
, GLint level
,
210 struct gl_texture_image
*texImage
)
212 const GLuint face
= _mesa_tex_target_to_face(target
);
217 tObj
->Image
[face
][level
] = texImage
;
219 /* Set the 'back' pointer */
220 texImage
->TexObject
= tObj
;
221 texImage
->Level
= level
;
222 texImage
->Face
= face
;
227 * Allocate a texture image structure.
229 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
232 * \return a pointer to gl_texture_image struct with all fields initialized to
235 struct gl_texture_image
*
236 _mesa_new_texture_image( struct gl_context
*ctx
)
239 return CALLOC_STRUCT(gl_texture_image
);
244 * Free a gl_texture_image and associated data.
245 * This function is a fallback called via ctx->Driver.DeleteTextureImage().
247 * \param texImage texture image.
249 * Free the texture image structure and the associated image data.
252 _mesa_delete_texture_image(struct gl_context
*ctx
,
253 struct gl_texture_image
*texImage
)
255 /* Free texImage->Data and/or any other driver-specific texture
258 ASSERT(ctx
->Driver
.FreeTextureImageBuffer
);
259 ctx
->Driver
.FreeTextureImageBuffer( ctx
, texImage
);
265 * Test if a target is a proxy target.
267 * \param target texture target.
269 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
272 _mesa_is_proxy_texture(GLenum target
)
275 * NUM_TEXTURE_TARGETS should match number of terms below, except there's no
276 * proxy for GL_TEXTURE_BUFFER and GL_TEXTURE_EXTERNAL_OES.
278 assert(NUM_TEXTURE_TARGETS
== 7 + 2);
280 return (target
== GL_PROXY_TEXTURE_1D
||
281 target
== GL_PROXY_TEXTURE_2D
||
282 target
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
);
287 * Return the proxy target which corresponds to the given texture target
290 get_proxy_target(GLenum target
)
294 case GL_PROXY_TEXTURE_1D
:
295 return GL_PROXY_TEXTURE_1D
;
297 case GL_PROXY_TEXTURE_2D
:
298 return GL_PROXY_TEXTURE_2D
;
299 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
300 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
301 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
302 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
303 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
304 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
305 case GL_TEXTURE_CUBE_MAP_ARB
:
306 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
307 return GL_PROXY_TEXTURE_CUBE_MAP_ARB
;
309 _mesa_problem(NULL
, "unexpected target in get_proxy_target()");
316 * Get the texture object that corresponds to the target of the given
317 * texture unit. The target should have already been checked for validity.
319 * \param ctx GL context.
320 * \param texUnit texture unit.
321 * \param target texture target.
323 * \return pointer to the texture object on success, or NULL on failure.
325 struct gl_texture_object
*
326 _mesa_select_tex_object(struct gl_context
*ctx
,
331 return ctx
->Texture
.Unit
.CurrentTex
[TEXTURE_1D_INDEX
];
332 case GL_PROXY_TEXTURE_1D
:
333 return ctx
->Texture
.ProxyTex
[TEXTURE_1D_INDEX
];
335 return ctx
->Texture
.Unit
.CurrentTex
[TEXTURE_2D_INDEX
];
336 case GL_PROXY_TEXTURE_2D
:
337 return ctx
->Texture
.ProxyTex
[TEXTURE_2D_INDEX
];
339 _mesa_problem(NULL
, "bad target in _mesa_select_tex_object()");
346 * Get a texture image pointer from a texture object, given a texture
347 * target and mipmap level. The target and level parameters should
348 * have already been error-checked.
350 * \param ctx GL context.
351 * \param texObj texture unit.
352 * \param target texture target.
353 * \param level image level.
355 * \return pointer to the texture image structure, or NULL on failure.
357 struct gl_texture_image
*
358 _mesa_select_tex_image(struct gl_context
*ctx
,
359 const struct gl_texture_object
*texObj
,
360 GLenum target
, GLint level
)
362 const GLuint face
= _mesa_tex_target_to_face(target
);
366 ASSERT(level
< MAX_TEXTURE_LEVELS
);
368 return texObj
->Image
[face
][level
];
373 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
374 * it and install it. Only return NULL if passed a bad parameter or run
377 struct gl_texture_image
*
378 _mesa_get_tex_image(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
379 GLenum target
, GLint level
)
381 struct gl_texture_image
*texImage
;
386 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
388 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
390 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture image allocation");
394 set_tex_image(texObj
, target
, level
, texImage
);
402 * Return pointer to the specified proxy texture image.
403 * Note that proxy textures are per-context, not per-texture unit.
404 * \return pointer to texture image or NULL if invalid target, invalid
405 * level, or out of memory.
407 struct gl_texture_image
*
408 _mesa_get_proxy_tex_image(struct gl_context
*ctx
, GLenum target
, GLint level
)
410 struct gl_texture_image
*texImage
;
417 case GL_PROXY_TEXTURE_1D
:
418 if (level
>= ctx
->Const
.MaxTextureLevels
)
420 texIndex
= TEXTURE_1D_INDEX
;
422 case GL_PROXY_TEXTURE_2D
:
423 if (level
>= ctx
->Const
.MaxTextureLevels
)
425 texIndex
= TEXTURE_2D_INDEX
;
427 case GL_PROXY_TEXTURE_CUBE_MAP
:
428 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
430 texIndex
= TEXTURE_CUBE_INDEX
;
436 texImage
= ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
];
438 texImage
= ctx
->Driver
.NewTextureImage(ctx
);
440 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "proxy texture allocation");
443 ctx
->Texture
.ProxyTex
[texIndex
]->Image
[0][level
] = texImage
;
444 /* Set the 'back' pointer */
445 texImage
->TexObject
= ctx
->Texture
.ProxyTex
[texIndex
];
452 * Get the maximum number of allowed mipmap levels.
454 * \param ctx GL context.
455 * \param target texture target.
457 * \return the maximum number of allowed mipmap levels for the given
458 * texture target, or zero if passed a bad target.
463 _mesa_max_texture_levels(struct gl_context
*ctx
, GLenum target
)
467 case GL_PROXY_TEXTURE_1D
:
469 case GL_PROXY_TEXTURE_2D
:
470 return ctx
->Const
.MaxTextureLevels
;
471 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
472 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
473 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
474 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
475 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
476 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
477 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
478 return ctx
->Extensions
.ARB_texture_cube_map
479 ? ctx
->Const
.MaxCubeTextureLevels
: 0;
481 return 0; /* bad target */
487 * Return number of dimensions per mipmap level for the given texture target.
490 _mesa_get_texture_dimensions(GLenum target
)
494 case GL_PROXY_TEXTURE_1D
:
497 case GL_TEXTURE_CUBE_MAP
:
498 case GL_PROXY_TEXTURE_2D
:
499 case GL_PROXY_TEXTURE_CUBE_MAP
:
500 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
501 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
502 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
503 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
504 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
505 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
508 _mesa_problem(NULL
, "invalid target 0x%x in get_texture_dimensions()",
517 #if 000 /* not used anymore */
519 * glTexImage[123]D can accept a NULL image pointer. In this case we
520 * create a texture image with unspecified image contents per the OpenGL
524 make_null_texture(GLint width
, GLint height
, GLint depth
, GLenum format
)
526 const GLint components
= _mesa_components_in_format(format
);
527 const GLint numPixels
= width
* height
* depth
;
528 GLubyte
*data
= (GLubyte
*) MALLOC(numPixels
* components
* sizeof(GLubyte
));
532 * Let's see if anyone finds this. If glTexImage2D() is called with
533 * a NULL image pointer then load the texture image with something
534 * interesting instead of leaving it indeterminate.
537 static const char message
[8][32] = {
541 " X X XXXX XXX XXXXX ",
544 " X X XXXXX XXX X X ",
548 GLubyte
*imgPtr
= data
;
550 for (h
= 0; h
< depth
; h
++) {
551 for (i
= 0; i
< height
; i
++) {
552 GLint srcRow
= 7 - (i
% 8);
553 for (j
= 0; j
< width
; j
++) {
554 GLint srcCol
= j
% 32;
555 GLubyte texel
= (message
[srcRow
][srcCol
]=='X') ? 255 : 70;
556 for (k
= 0; k
< components
; k
++) {
572 * Set the size and format-related fields of a gl_texture_image struct
573 * to zero. This is used when a proxy texture test fails.
576 clear_teximage_fields(struct gl_texture_image
*img
)
579 img
->_BaseFormat
= 0;
580 img
->InternalFormat
= 0;
591 img
->TexFormat
= MESA_FORMAT_NONE
;
596 * Initialize basic fields of the gl_texture_image struct.
598 * \param ctx GL context.
599 * \param img texture image structure to be initialized.
600 * \param width image width.
601 * \param height image height.
602 * \param depth image depth.
603 * \param border image border.
604 * \param internalFormat internal format.
605 * \param format the actual hardware format (one of MESA_FORMAT_*)
607 * Fills in the fields of \p img with the given information.
608 * Note: width, height and depth include the border.
611 _mesa_init_teximage_fields(struct gl_context
*ctx
,
612 struct gl_texture_image
*img
,
613 GLsizei width
, GLsizei height
, GLsizei depth
,
614 GLint border
, GLenum internalFormat
,
623 target
= img
->TexObject
->Target
;
624 img
->_BaseFormat
= _mesa_base_tex_format( ctx
, internalFormat
);
625 ASSERT(img
->_BaseFormat
> 0);
626 img
->InternalFormat
= internalFormat
;
627 img
->Border
= border
;
629 img
->Height
= height
;
632 img
->Width2
= width
- 2 * border
; /* == 1 << img->WidthLog2; */
633 img
->WidthLog2
= _mesa_logbase2(img
->Width2
);
637 case GL_PROXY_TEXTURE_1D
:
650 case GL_TEXTURE_CUBE_MAP
:
651 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
652 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
653 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
654 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
655 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
656 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
657 case GL_PROXY_TEXTURE_2D
:
658 case GL_PROXY_TEXTURE_CUBE_MAP
:
659 img
->Height2
= height
- 2 * border
; /* == 1 << img->HeightLog2; */
660 img
->HeightLog2
= _mesa_logbase2(img
->Height2
);
668 _mesa_problem(NULL
, "invalid target 0x%x in _mesa_init_teximage_fields()",
672 img
->MaxLog2
= MAX2(img
->WidthLog2
, img
->HeightLog2
);
673 img
->TexFormat
= format
;
678 * Free and clear fields of the gl_texture_image struct.
680 * \param ctx GL context.
681 * \param texImage texture image structure to be cleared.
683 * After the call, \p texImage will have no data associated with it. Its
684 * fields are cleared so that its parent object will test incomplete.
687 _mesa_clear_texture_image(struct gl_context
*ctx
,
688 struct gl_texture_image
*texImage
)
690 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
691 clear_teximage_fields(texImage
);
696 * This is the fallback for Driver.TestProxyTexImage(). Test the texture
697 * level, width, height and depth against the ctx->Const limits for textures.
699 * A hardware driver might override this function if, for example, the
700 * max 3D texture size is 512x512x64 (i.e. not a cube).
702 * Note that width, height, depth == 0 is not an error. However, a
703 * texture with zero width/height/depth will be considered "incomplete"
704 * and texturing will effectively be disabled.
706 * \param target one of GL_PROXY_TEXTURE_1D, GL_PROXY_TEXTURE_2D,
707 * GL_PROXY_TEXTURE_3D, GL_PROXY_TEXTURE_RECTANGLE_NV,
708 * GL_PROXY_TEXTURE_CUBE_MAP_ARB.
709 * \param level as passed to glTexImage
710 * \param internalFormat as passed to glTexImage
711 * \param format as passed to glTexImage
712 * \param type as passed to glTexImage
713 * \param width as passed to glTexImage
714 * \param height as passed to glTexImage
715 * \param depth as passed to glTexImage
716 * \param border as passed to glTexImage
717 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
720 _mesa_test_proxy_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
721 GLint internalFormat
, GLenum format
, GLenum type
,
722 GLint width
, GLint height
, GLint depth
, GLint border
)
726 (void) internalFormat
;
731 case GL_PROXY_TEXTURE_1D
:
732 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
733 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
735 if (level
>= ctx
->Const
.MaxTextureLevels
)
737 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
741 case GL_PROXY_TEXTURE_2D
:
742 maxSize
= 1 << (ctx
->Const
.MaxTextureLevels
- 1);
743 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
745 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
747 if (level
>= ctx
->Const
.MaxTextureLevels
)
749 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
751 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
755 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
756 maxSize
= 1 << (ctx
->Const
.MaxCubeTextureLevels
- 1);
757 if (width
< 2 * border
|| width
> 2 * border
+ maxSize
)
759 if (height
< 2 * border
|| height
> 2 * border
+ maxSize
)
761 if (level
>= ctx
->Const
.MaxCubeTextureLevels
)
763 if (width
> 0 && !_mesa_is_pow_two(width
- 2 * border
))
765 if (height
> 0 && !_mesa_is_pow_two(height
- 2 * border
))
770 _mesa_problem(ctx
, "Invalid target in _mesa_test_proxy_teximage");
777 * Check if the memory used by the texture would exceed the driver's limit.
778 * This lets us support a max 3D texture size of 8K (for example) but
779 * prevents allocating a full 8K x 8K x 8K texture.
780 * XXX this could be rolled into the proxy texture size test (above) but
781 * we don't have the actual texture internal format at that point.
784 legal_texture_size(struct gl_context
*ctx
, gl_format format
,
785 GLint width
, GLint height
, GLint depth
)
787 uint64_t bytes
= _mesa_format_image_size64(format
, width
, height
, depth
);
788 uint64_t mbytes
= bytes
/ (1024 * 1024); /* convert to MB */
789 return mbytes
<= (uint64_t) ctx
->Const
.MaxTextureMbytes
;
793 * Check if the given texture target value is legal for a
794 * glTexImage1/2/3D call.
797 legal_teximage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
803 case GL_PROXY_TEXTURE_1D
:
811 case GL_PROXY_TEXTURE_2D
:
813 case GL_PROXY_TEXTURE_CUBE_MAP
:
814 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
815 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
816 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
817 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
818 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
819 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
820 return ctx
->Extensions
.ARB_texture_cube_map
;
825 _mesa_problem(ctx
, "invalid dims=%u in legal_teximage_target()", dims
);
832 * Check if the given texture target value is legal for a
833 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
834 * The difference compared to legal_teximage_target() above is that
835 * proxy targets are not supported.
838 legal_texsubimage_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
842 return target
== GL_TEXTURE_1D
;
847 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
848 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
849 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
850 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
851 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
852 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
853 return ctx
->Extensions
.ARB_texture_cube_map
;
858 _mesa_problem(ctx
, "invalid dims=%u in legal_texsubimage_target()",
866 * Helper function to determine if a texture object is mutable (in terms
867 * of GL_ARB_texture_storage).
870 mutable_tex_object(struct gl_context
*ctx
, GLenum target
)
872 if (ctx
->Extensions
.ARB_texture_storage
) {
873 struct gl_texture_object
*texObj
=
874 _mesa_select_tex_object(ctx
, target
);
875 return !texObj
->Immutable
;
883 * Test the glTexImage[123]D() parameters for errors.
885 * \param ctx GL context.
886 * \param dimensions texture image dimensions (must be 1, 2 or 3).
887 * \param target texture target given by the user.
888 * \param level image level given by the user.
889 * \param internalFormat internal format given by the user.
890 * \param format pixel data format given by the user.
891 * \param type pixel data type given by the user.
892 * \param width image width given by the user.
893 * \param height image height given by the user.
894 * \param depth image depth given by the user.
895 * \param border image border given by the user.
897 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
899 * Verifies each of the parameters against the constants specified in
900 * __struct gl_contextRec::Const and the supported extensions, and according
901 * to the OpenGL specification.
904 texture_error_check( struct gl_context
*ctx
,
905 GLuint dimensions
, GLenum target
,
906 GLint level
, GLint internalFormat
,
907 GLenum format
, GLenum type
,
908 GLint width
, GLint height
,
909 GLint depth
, GLint border
)
911 const GLenum proxyTarget
= get_proxy_target(target
);
912 const GLboolean isProxy
= target
== proxyTarget
;
913 GLboolean sizeOK
= GL_TRUE
;
914 GLboolean colorFormat
;
917 /* Even though there are no color-index textures, we still have to support
918 * uploading color-index data and remapping it to RGB via the
919 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
921 const GLboolean indexFormat
= (format
== GL_COLOR_INDEX
);
923 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
924 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
926 _mesa_error(ctx
, GL_INVALID_VALUE
,
927 "glTexImage%dD(level=%d)", dimensions
, level
);
933 if (border
< 0 || border
> 1) {
935 _mesa_error(ctx
, GL_INVALID_VALUE
,
936 "glTexImage%dD(border=%d)", dimensions
, border
);
941 if (width
< 0 || height
< 0 || depth
< 0) {
943 _mesa_error(ctx
, GL_INVALID_VALUE
,
944 "glTexImage%dD(width, height or depth < 0)", dimensions
);
949 /* Do this simple check before calling the TestProxyTexImage() function */
950 if (proxyTarget
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
) {
951 sizeOK
= (width
== height
);
955 * Use the proxy texture driver hook to see if the size/level/etc are
958 sizeOK
= sizeOK
&& ctx
->Driver
.TestProxyTexImage(ctx
, proxyTarget
, level
,
959 internalFormat
, format
,
964 _mesa_error(ctx
, GL_INVALID_VALUE
,
965 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
966 dimensions
, level
, width
, height
, depth
);
971 /* Check internalFormat */
972 if (_mesa_base_tex_format(ctx
, internalFormat
) < 0) {
974 _mesa_error(ctx
, GL_INVALID_VALUE
,
975 "glTexImage%dD(internalFormat=%s)",
976 dimensions
, _mesa_lookup_enum_by_nr(internalFormat
));
981 /* Check incoming image format and type */
982 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
983 if (err
!= GL_NO_ERROR
) {
985 _mesa_error(ctx
, err
,
986 "glTexImage%dD(incompatible format 0x%x, type 0x%x)",
987 dimensions
, format
, type
);
992 /* make sure internal format and format basically agree */
993 colorFormat
= _mesa_is_color_format(format
);
994 if ((_mesa_is_color_format(internalFormat
) && !colorFormat
&& !indexFormat
) ||
995 (_mesa_is_depth_format(internalFormat
) != _mesa_is_depth_format(format
)) ||
996 (_mesa_is_ycbcr_format(internalFormat
) != _mesa_is_ycbcr_format(format
))) {
998 _mesa_error(ctx
, GL_INVALID_OPERATION
,
999 "glTexImage%dD(incompatible internalFormat 0x%x, format 0x%x)",
1000 dimensions
, internalFormat
, format
);
1004 /* additional checks for ycbcr textures */
1005 if (internalFormat
== GL_YCBCR_MESA
) {
1006 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
1007 if (type
!= GL_UNSIGNED_SHORT_8_8_MESA
&&
1008 type
!= GL_UNSIGNED_SHORT_8_8_REV_MESA
) {
1010 _mesa_snprintf(message
, sizeof(message
),
1011 "glTexImage%dD(format/type YCBCR mismatch", dimensions
);
1012 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", message
);
1013 return GL_TRUE
; /* error */
1015 if (target
!= GL_TEXTURE_2D
&&
1016 target
!= GL_PROXY_TEXTURE_2D
) {
1018 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage(target)");
1024 _mesa_snprintf(message
, sizeof(message
),
1025 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1026 dimensions
, border
);
1027 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", message
);
1033 /* additional checks for depth textures */
1034 if (_mesa_base_tex_format(ctx
, internalFormat
) == GL_DEPTH_COMPONENT
) {
1035 /* Only 1D, 2D, rect, array and cube textures supported, not 3D
1036 * Cubemaps are only supported for GL version > 3.0 or with EXT_gpu_shader4 */
1037 if (target
!= GL_TEXTURE_1D
&&
1038 target
!= GL_PROXY_TEXTURE_1D
&&
1039 target
!= GL_TEXTURE_2D
&&
1040 target
!= GL_PROXY_TEXTURE_2D
&&
1041 !((_mesa_is_cube_face(target
) || target
== GL_PROXY_TEXTURE_CUBE_MAP
) &&
1042 (ctx
->VersionMajor
>= 3))) {
1044 _mesa_error(ctx
, GL_INVALID_ENUM
,
1045 "glTexImage(target/internalFormat)");
1050 /* additional checks for integer textures */
1051 if ((ctx
->VersionMajor
>= 3 || ctx
->Extensions
.EXT_texture_integer
) &&
1052 (_mesa_is_integer_format(format
) !=
1053 _mesa_is_integer_format(internalFormat
))) {
1055 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1056 "glTexImage%dD(integer/non-integer format mismatch)",
1062 if (!mutable_tex_object(ctx
, target
)) {
1063 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1064 "glTexImage%dD(immutable texture)", dimensions
);
1068 /* if we get here, the parameters are OK */
1074 * Test glTexSubImage[123]D() parameters for errors.
1076 * \param ctx GL context.
1077 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1078 * \param target texture target given by the user.
1079 * \param level image level given by the user.
1080 * \param xoffset sub-image x offset given by the user.
1081 * \param yoffset sub-image y offset given by the user.
1082 * \param zoffset sub-image z offset given by the user.
1083 * \param format pixel data format given by the user.
1084 * \param type pixel data type given by the user.
1085 * \param width image width given by the user.
1086 * \param height image height given by the user.
1087 * \param depth image depth given by the user.
1089 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1091 * Verifies each of the parameters against the constants specified in
1092 * __struct gl_contextRec::Const and the supported extensions, and according
1093 * to the OpenGL specification.
1096 subtexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
1097 GLenum target
, GLint level
,
1098 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1099 GLint width
, GLint height
, GLint depth
,
1100 GLenum format
, GLenum type
)
1104 /* Basic level check */
1105 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1106 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage2D(level=%d)", level
);
1110 /* Check for negative sizes */
1112 _mesa_error(ctx
, GL_INVALID_VALUE
,
1113 "glTexSubImage%dD(width=%d)", dimensions
, width
);
1116 if (height
< 0 && dimensions
> 1) {
1117 _mesa_error(ctx
, GL_INVALID_VALUE
,
1118 "glTexSubImage%dD(height=%d)", dimensions
, height
);
1121 if (depth
< 0 && dimensions
> 2) {
1122 _mesa_error(ctx
, GL_INVALID_VALUE
,
1123 "glTexSubImage%dD(depth=%d)", dimensions
, depth
);
1127 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1128 if (err
!= GL_NO_ERROR
) {
1129 _mesa_error(ctx
, err
,
1130 "glTexSubImage%dD(incompatible format 0x%x, type 0x%x)",
1131 dimensions
, format
, type
);
1140 * Do second part of glTexSubImage which depends on the destination texture.
1141 * \return GL_TRUE if error recorded, GL_FALSE otherwise
1144 subtexture_error_check2( struct gl_context
*ctx
, GLuint dimensions
,
1145 GLenum target
, GLint level
,
1146 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1147 GLint width
, GLint height
, GLint depth
,
1148 GLenum format
, GLenum type
,
1149 const struct gl_texture_image
*destTex
)
1152 /* undefined image level */
1153 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexSubImage%dD", dimensions
);
1157 if (xoffset
< -((GLint
)destTex
->Border
)) {
1158 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset)",
1162 if (xoffset
+ width
> (GLint
) (destTex
->Width
+ destTex
->Border
)) {
1163 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(xoffset+width)",
1167 if (dimensions
> 1) {
1168 if (yoffset
< -((GLint
)destTex
->Border
)) {
1169 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset)",
1173 if (yoffset
+ height
> (GLint
) (destTex
->Height
+ destTex
->Border
)) {
1174 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexSubImage%dD(yoffset+height)",
1180 if (ctx
->VersionMajor
>= 3 || ctx
->Extensions
.EXT_texture_integer
) {
1181 /* both source and dest must be integer-valued, or neither */
1182 if (_mesa_is_format_integer_color(destTex
->TexFormat
) !=
1183 _mesa_is_integer_format(format
)) {
1184 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1185 "glTexSubImage%dD(integer/non-integer format mismatch)",
1196 * Test glCopyTexImage[12]D() parameters for errors.
1198 * \param ctx GL context.
1199 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1200 * \param target texture target given by the user.
1201 * \param level image level given by the user.
1202 * \param internalFormat internal format given by the user.
1203 * \param width image width given by the user.
1204 * \param height image height given by the user.
1205 * \param border texture border.
1207 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1209 * Verifies each of the parameters against the constants specified in
1210 * __struct gl_contextRec::Const and the supported extensions, and according
1211 * to the OpenGL specification.
1214 copytexture_error_check( struct gl_context
*ctx
, GLuint dimensions
,
1215 GLenum target
, GLint level
, GLint internalFormat
,
1216 GLint width
, GLint height
, GLint border
)
1218 const GLenum proxyTarget
= get_proxy_target(target
);
1219 const GLenum type
= GL_FLOAT
;
1224 if (!legal_texsubimage_target(ctx
, dimensions
, target
)) {
1225 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexImage%uD(target=%s)",
1226 dimensions
, _mesa_lookup_enum_by_nr(target
));
1230 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1231 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1232 _mesa_error(ctx
, GL_INVALID_VALUE
,
1233 "glCopyTexImage%dD(level=%d)", dimensions
, level
);
1238 if (border
< 0 || border
> 1) {
1242 baseFormat
= _mesa_base_tex_format(ctx
, internalFormat
);
1243 if (baseFormat
< 0) {
1244 _mesa_error(ctx
, GL_INVALID_VALUE
,
1245 "glCopyTexImage%dD(internalFormat)", dimensions
);
1249 if (!_mesa_source_buffer_exists(ctx
, baseFormat
)) {
1250 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1251 "glCopyTexImage%dD(missing readbuffer)", dimensions
);
1255 /* From the EXT_texture_integer spec:
1257 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
1258 * if the texture internalformat is an integer format and the read color
1259 * buffer is not an integer format, or if the internalformat is not an
1260 * integer format and the read color buffer is an integer format."
1262 if (_mesa_is_color_format(internalFormat
)) {
1263 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
1265 if (_mesa_is_integer_format(rb
->InternalFormat
) !=
1266 _mesa_is_integer_format(internalFormat
)) {
1267 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1268 "glCopyTexImage%dD(integer vs non-integer)", dimensions
);
1273 /* Do size, level checking */
1274 sizeOK
= (proxyTarget
== GL_PROXY_TEXTURE_CUBE_MAP_ARB
)
1275 ? (width
== height
) : 1;
1277 sizeOK
= sizeOK
&& ctx
->Driver
.TestProxyTexImage(ctx
, proxyTarget
, level
,
1278 internalFormat
, baseFormat
,
1279 type
, width
, height
,
1283 if (dimensions
== 1) {
1284 _mesa_error(ctx
, GL_INVALID_VALUE
,
1285 "glCopyTexImage1D(width=%d)", width
);
1288 ASSERT(dimensions
== 2);
1289 _mesa_error(ctx
, GL_INVALID_VALUE
,
1290 "glCopyTexImage2D(width=%d, height=%d)", width
, height
);
1295 if (!mutable_tex_object(ctx
, target
)) {
1296 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1297 "glCopyTexImage%dD(immutable texture)", dimensions
);
1301 /* if we get here, the parameters are OK */
1307 * Test glCopyTexSubImage[12]D() parameters for errors.
1308 * Note that this is the first part of error checking.
1309 * See also copytexsubimage_error_check2() below for the second part.
1311 * \param ctx GL context.
1312 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1313 * \param target texture target given by the user.
1314 * \param level image level given by the user.
1316 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1319 copytexsubimage_error_check1( struct gl_context
*ctx
, GLuint dimensions
,
1320 GLenum target
, GLint level
)
1322 /* check target (proxies not allowed) */
1323 if (!legal_texsubimage_target(ctx
, dimensions
, target
)) {
1324 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyTexSubImage%uD(target=%s)",
1325 dimensions
, _mesa_lookup_enum_by_nr(target
));
1330 if (level
< 0 || level
>= MAX_TEXTURE_LEVELS
) {
1331 _mesa_error(ctx
, GL_INVALID_VALUE
,
1332 "glCopyTexSubImage%dD(level=%d)", dimensions
, level
);
1341 * Second part of error checking for glCopyTexSubImage[12]D().
1342 * \param xoffset sub-image x offset given by the user.
1343 * \param yoffset sub-image y offset given by the user.
1344 * \param zoffset sub-image z offset given by the user.
1345 * \param width image width given by the user.
1346 * \param height image height given by the user.
1349 copytexsubimage_error_check2( struct gl_context
*ctx
, GLuint dimensions
,
1350 GLenum target
, GLint level
,
1351 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1352 GLsizei width
, GLsizei height
,
1353 const struct gl_texture_image
*teximage
)
1355 /* check that dest tex image exists */
1357 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1358 "glCopyTexSubImage%dD(undefined texture level: %d)",
1365 _mesa_error(ctx
, GL_INVALID_VALUE
,
1366 "glCopyTexSubImage%dD(width=%d)", dimensions
, width
);
1369 if (dimensions
> 1 && height
< 0) {
1370 _mesa_error(ctx
, GL_INVALID_VALUE
,
1371 "glCopyTexSubImage%dD(height=%d)", dimensions
, height
);
1375 /* check x/y offsets */
1376 if (xoffset
< -((GLint
)teximage
->Border
)) {
1377 _mesa_error(ctx
, GL_INVALID_VALUE
,
1378 "glCopyTexSubImage%dD(xoffset=%d)", dimensions
, xoffset
);
1381 if (xoffset
+ width
> (GLint
) (teximage
->Width
+ teximage
->Border
)) {
1382 _mesa_error(ctx
, GL_INVALID_VALUE
,
1383 "glCopyTexSubImage%dD(xoffset+width)", dimensions
);
1386 if (dimensions
> 1) {
1387 if (yoffset
< -((GLint
)teximage
->Border
)) {
1388 _mesa_error(ctx
, GL_INVALID_VALUE
,
1389 "glCopyTexSubImage%dD(yoffset=%d)", dimensions
, yoffset
);
1392 /* NOTE: we're adding the border here, not subtracting! */
1393 if (yoffset
+ height
> (GLint
) (teximage
->Height
+ teximage
->Border
)) {
1394 _mesa_error(ctx
, GL_INVALID_VALUE
,
1395 "glCopyTexSubImage%dD(yoffset+height)", dimensions
);
1400 /* check z offset */
1401 if (dimensions
> 2) {
1402 if (zoffset
< -((GLint
)teximage
->Border
)) {
1403 _mesa_error(ctx
, GL_INVALID_VALUE
,
1404 "glCopyTexSubImage%dD(zoffset)", dimensions
);
1407 if (zoffset
> (GLint
) (teximage
->Depth
+ teximage
->Border
)) {
1408 _mesa_error(ctx
, GL_INVALID_VALUE
,
1409 "glCopyTexSubImage%dD(zoffset+depth)", dimensions
);
1414 if (teximage
->InternalFormat
== GL_YCBCR_MESA
) {
1415 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glCopyTexSubImage2D");
1419 if (!_mesa_source_buffer_exists(ctx
, teximage
->_BaseFormat
)) {
1420 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1421 "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
1422 dimensions
, teximage
->_BaseFormat
);
1426 /* From the EXT_texture_integer spec:
1428 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
1429 * if the texture internalformat is an integer format and the read color
1430 * buffer is not an integer format, or if the internalformat is not an
1431 * integer format and the read color buffer is an integer format."
1433 if (_mesa_is_color_format(teximage
->InternalFormat
)) {
1434 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
1436 if (_mesa_is_format_integer_color(rb
->Format
) !=
1437 _mesa_is_format_integer_color(teximage
->TexFormat
)) {
1438 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1439 "glCopyTexImage%dD(integer vs non-integer)", dimensions
);
1444 /* if we get here, the parameters are OK */
1449 /** Callback info for walking over FBO hash table */
1452 struct gl_context
*ctx
;
1453 struct gl_texture_object
*texObj
;
1457 /** Debug helper: override the user-requested internal format */
1459 override_internal_format(GLenum internalFormat
, GLint width
, GLint height
)
1462 if (internalFormat
== GL_RGBA16F_ARB
||
1463 internalFormat
== GL_RGBA32F_ARB
) {
1464 printf("Convert rgba float tex to int %d x %d\n", width
, height
);
1467 else if (internalFormat
== GL_RGB16F_ARB
||
1468 internalFormat
== GL_RGB32F_ARB
) {
1469 printf("Convert rgb float tex to int %d x %d\n", width
, height
);
1472 else if (internalFormat
== GL_LUMINANCE_ALPHA16F_ARB
||
1473 internalFormat
== GL_LUMINANCE_ALPHA32F_ARB
) {
1474 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
1475 return GL_LUMINANCE_ALPHA
;
1477 else if (internalFormat
== GL_LUMINANCE16F_ARB
||
1478 internalFormat
== GL_LUMINANCE32F_ARB
) {
1479 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
1480 return GL_LUMINANCE
;
1482 else if (internalFormat
== GL_ALPHA16F_ARB
||
1483 internalFormat
== GL_ALPHA32F_ARB
) {
1484 printf("Convert luminance float tex to int %d x %d\n", width
, height
);
1488 return internalFormat
;
1491 return internalFormat
;
1497 * Choose the actual hardware format for a texture image.
1498 * Try to use the same format as the previous image level when possible.
1499 * Otherwise, ask the driver for the best format.
1500 * It's important to try to choose a consistant format for all levels
1501 * for efficient texture memory layout/allocation. In particular, this
1502 * comes up during automatic mipmap generation.
1505 _mesa_choose_texture_format(struct gl_context
*ctx
,
1506 struct gl_texture_object
*texObj
,
1507 GLenum target
, GLint level
,
1508 GLenum internalFormat
, GLenum format
, GLenum type
)
1512 /* see if we've already chosen a format for the previous level */
1514 struct gl_texture_image
*prevImage
=
1515 _mesa_select_tex_image(ctx
, texObj
, target
, level
- 1);
1516 /* See if the prev level is defined and has an internal format which
1517 * matches the new internal format.
1520 prevImage
->Width
> 0 &&
1521 prevImage
->InternalFormat
== internalFormat
) {
1522 /* use the same format */
1523 ASSERT(prevImage
->TexFormat
!= MESA_FORMAT_NONE
);
1524 return prevImage
->TexFormat
;
1528 /* choose format from scratch */
1529 f
= ctx
->Driver
.ChooseTextureFormat(ctx
, internalFormat
, format
, type
);
1530 ASSERT(f
!= MESA_FORMAT_NONE
);
1535 * Adjust pixel unpack params and image dimensions to strip off the
1538 * Gallium and intel don't support texture borders. They've seldem been used
1539 * and seldom been implemented correctly anyway.
1541 * \param unpackNew returns the new pixel unpack parameters
1544 strip_texture_border(GLint
*border
,
1545 GLint
*width
, GLint
*height
, GLint
*depth
,
1546 const struct gl_pixelstore_attrib
*unpack
,
1547 struct gl_pixelstore_attrib
*unpackNew
)
1549 assert(*border
> 0); /* sanity check */
1551 *unpackNew
= *unpack
;
1553 if (unpackNew
->RowLength
== 0)
1554 unpackNew
->RowLength
= *width
;
1556 if (depth
&& unpackNew
->ImageHeight
== 0)
1557 unpackNew
->ImageHeight
= *height
;
1559 unpackNew
->SkipPixels
+= *border
;
1561 unpackNew
->SkipRows
+= *border
;
1563 unpackNew
->SkipImages
+= *border
;
1565 assert(*width
>= 3);
1566 *width
= *width
- 2 * *border
;
1567 if (height
&& *height
>= 3)
1568 *height
= *height
- 2 * *border
;
1569 if (depth
&& *depth
>= 3)
1570 *depth
= *depth
- 2 * *border
;
1575 * Common code to implement all the glTexImage1D/2D/3D functions.
1578 teximage(struct gl_context
*ctx
, GLuint dims
,
1579 GLenum target
, GLint level
, GLint internalFormat
,
1580 GLsizei width
, GLsizei height
, GLsizei depth
,
1581 GLint border
, GLenum format
, GLenum type
,
1582 const GLvoid
*pixels
)
1585 struct gl_pixelstore_attrib unpack_no_border
;
1586 const struct gl_pixelstore_attrib
*unpack
= &ctx
->Unpack
;
1588 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1590 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1591 _mesa_debug(ctx
, "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
1593 _mesa_lookup_enum_by_nr(target
), level
,
1594 _mesa_lookup_enum_by_nr(internalFormat
),
1595 width
, height
, depth
, border
,
1596 _mesa_lookup_enum_by_nr(format
),
1597 _mesa_lookup_enum_by_nr(type
), pixels
);
1599 internalFormat
= override_internal_format(internalFormat
, width
, height
);
1601 /* target error checking */
1602 if (!legal_teximage_target(ctx
, dims
, target
)) {
1603 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexImage%uD(target=%s)",
1604 dims
, _mesa_lookup_enum_by_nr(target
));
1608 /* general error checking */
1609 error
= texture_error_check(ctx
, dims
, target
, level
, internalFormat
,
1610 format
, type
, width
, height
, depth
, border
);
1612 if (_mesa_is_proxy_texture(target
)) {
1613 /* Proxy texture: just clear or set state depending on error checking */
1614 struct gl_texture_image
*texImage
=
1615 _mesa_get_proxy_tex_image(ctx
, target
, level
);
1618 /* when error, clear all proxy texture image parameters */
1620 clear_teximage_fields(texImage
);
1623 /* no error, set the tex image parameters */
1624 struct gl_texture_object
*texObj
=
1625 _mesa_select_tex_object(ctx
, target
);
1626 gl_format texFormat
= _mesa_choose_texture_format(ctx
, texObj
,
1631 if (legal_texture_size(ctx
, texFormat
, width
, height
, depth
)) {
1632 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
,
1633 depth
, border
, internalFormat
,
1636 else if (texImage
) {
1637 clear_teximage_fields(texImage
);
1642 /* non-proxy target */
1643 struct gl_texture_object
*texObj
;
1644 struct gl_texture_image
*texImage
;
1647 return; /* error was recorded */
1650 /* Allow a hardware driver to just strip out the border, to provide
1651 * reliable but slightly incorrect hardware rendering instead of
1652 * rarely-tested software fallback rendering.
1654 if (border
&& ctx
->Const
.StripTextureBorder
) {
1655 strip_texture_border(&border
, &width
, &height
, &depth
, unpack
,
1657 unpack
= &unpack_no_border
;
1660 if (ctx
->NewState
& _NEW_PIXEL
)
1661 _mesa_update_state(ctx
);
1663 texObj
= _mesa_select_tex_object(ctx
, target
);
1665 _mesa_lock_texture(ctx
, texObj
);
1667 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
1670 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
1673 gl_format texFormat
;
1675 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
1677 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
1678 internalFormat
, format
,
1681 if (legal_texture_size(ctx
, texFormat
, width
, height
, depth
)) {
1682 _mesa_init_teximage_fields(ctx
, texImage
,
1683 width
, height
, depth
,
1684 border
, internalFormat
, texFormat
);
1686 /* Give the texture to the driver. <pixels> may be null. */
1689 ctx
->Driver
.TexImage1D(ctx
, texImage
, internalFormat
,
1690 width
, border
, format
,
1691 type
, pixels
, unpack
);
1694 ctx
->Driver
.TexImage2D(ctx
, texImage
, internalFormat
,
1695 width
, height
, border
, format
,
1696 type
, pixels
, unpack
);
1699 _mesa_problem(ctx
, "invalid dims=%u in teximage()", dims
);
1703 texObj
->_Complete
= GL_FALSE
;
1704 ctx
->NewState
|= _NEW_TEXTURE
;
1707 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
1711 _mesa_unlock_texture(ctx
, texObj
);
1717 * Called from the API. Note that width includes the border.
1720 _mesa_TexImage1D( GLenum target
, GLint level
, GLint internalFormat
,
1721 GLsizei width
, GLint border
, GLenum format
,
1722 GLenum type
, const GLvoid
*pixels
)
1724 GET_CURRENT_CONTEXT(ctx
);
1725 teximage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1,
1726 border
, format
, type
, pixels
);
1731 _mesa_TexImage2D( GLenum target
, GLint level
, GLint internalFormat
,
1732 GLsizei width
, GLsizei height
, GLint border
,
1733 GLenum format
, GLenum type
,
1734 const GLvoid
*pixels
)
1736 GET_CURRENT_CONTEXT(ctx
);
1737 teximage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1,
1738 border
, format
, type
, pixels
);
1743 * Implement all the glTexSubImage1/2/3D() functions.
1746 texsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
1747 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1748 GLsizei width
, GLsizei height
, GLsizei depth
,
1749 GLenum format
, GLenum type
, const GLvoid
*pixels
)
1751 struct gl_texture_object
*texObj
;
1752 struct gl_texture_image
*texImage
;
1754 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1756 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1757 _mesa_debug(ctx
, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
1759 _mesa_lookup_enum_by_nr(target
), level
,
1760 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
1761 _mesa_lookup_enum_by_nr(format
),
1762 _mesa_lookup_enum_by_nr(type
), pixels
);
1764 /* check target (proxies not allowed) */
1765 if (!legal_texsubimage_target(ctx
, dims
, target
)) {
1766 _mesa_error(ctx
, GL_INVALID_ENUM
, "glTexSubImage%uD(target=%s)",
1767 dims
, _mesa_lookup_enum_by_nr(target
));
1771 if (ctx
->NewState
& _NEW_PIXEL
)
1772 _mesa_update_state(ctx
);
1774 if (subtexture_error_check(ctx
, dims
, target
, level
, xoffset
, yoffset
, zoffset
,
1775 width
, height
, depth
, format
, type
)) {
1776 return; /* error was detected */
1779 texObj
= _mesa_select_tex_object(ctx
, target
);
1781 _mesa_lock_texture(ctx
, texObj
);
1783 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1785 if (subtexture_error_check2(ctx
, dims
, target
, level
,
1786 xoffset
, yoffset
, zoffset
,
1787 width
, height
, depth
,
1788 format
, type
, texImage
)) {
1789 /* error was recorded */
1791 else if (width
> 0 && height
> 0 && depth
> 0) {
1792 /* If we have a border, offset=-1 is legal. Bias by border width. */
1795 zoffset
+= texImage
->Border
;
1798 yoffset
+= texImage
->Border
;
1801 xoffset
+= texImage
->Border
;
1806 ctx
->Driver
.TexSubImage1D(ctx
, texImage
,
1808 format
, type
, pixels
, &ctx
->Unpack
);
1811 ctx
->Driver
.TexSubImage2D(ctx
, texImage
,
1812 xoffset
, yoffset
, width
, height
,
1813 format
, type
, pixels
, &ctx
->Unpack
);
1816 _mesa_problem(ctx
, "unexpected dims in subteximage()");
1819 ctx
->NewState
|= _NEW_TEXTURE
;
1822 _mesa_unlock_texture(ctx
, texObj
);
1827 _mesa_TexSubImage1D( GLenum target
, GLint level
,
1828 GLint xoffset
, GLsizei width
,
1829 GLenum format
, GLenum type
,
1830 const GLvoid
*pixels
)
1832 GET_CURRENT_CONTEXT(ctx
);
1833 texsubimage(ctx
, 1, target
, level
,
1836 format
, type
, pixels
);
1841 _mesa_TexSubImage2D( GLenum target
, GLint level
,
1842 GLint xoffset
, GLint yoffset
,
1843 GLsizei width
, GLsizei height
,
1844 GLenum format
, GLenum type
,
1845 const GLvoid
*pixels
)
1847 GET_CURRENT_CONTEXT(ctx
);
1848 texsubimage(ctx
, 2, target
, level
,
1849 xoffset
, yoffset
, 0,
1851 format
, type
, pixels
);
1857 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
1858 * from. This depends on whether the texture contains color or depth values.
1860 static struct gl_renderbuffer
*
1861 get_copy_tex_image_source(struct gl_context
*ctx
, gl_format texFormat
)
1863 if (_mesa_get_format_bits(texFormat
, GL_DEPTH_BITS
) > 0) {
1864 /* reading from depth/stencil buffer */
1865 return ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
1868 /* copying from color buffer */
1869 return ctx
->ReadBuffer
->_ColorReadBuffer
;
1876 * Implement the glCopyTexImage1/2D() functions.
1879 copyteximage(struct gl_context
*ctx
, GLuint dims
,
1880 GLenum target
, GLint level
, GLenum internalFormat
,
1881 GLint x
, GLint y
, GLsizei width
, GLsizei height
, GLint border
)
1883 struct gl_texture_object
*texObj
;
1884 struct gl_texture_image
*texImage
;
1886 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1888 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
1889 _mesa_debug(ctx
, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
1891 _mesa_lookup_enum_by_nr(target
), level
,
1892 _mesa_lookup_enum_by_nr(internalFormat
),
1893 x
, y
, width
, height
, border
);
1895 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
1896 _mesa_update_state(ctx
);
1898 if (copytexture_error_check(ctx
, dims
, target
, level
, internalFormat
,
1899 width
, height
, border
))
1902 texObj
= _mesa_select_tex_object(ctx
, target
);
1904 if (border
&& ctx
->Const
.StripTextureBorder
) {
1906 width
-= border
* 2;
1909 height
-= border
* 2;
1914 _mesa_lock_texture(ctx
, texObj
);
1916 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
1919 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
1922 /* choose actual hw format */
1923 gl_format texFormat
= _mesa_choose_texture_format(ctx
, texObj
,
1928 if (legal_texture_size(ctx
, texFormat
, width
, height
, 1)) {
1929 GLint srcX
= x
, srcY
= y
, dstX
= 0, dstY
= 0;
1931 /* Free old texture image */
1932 ctx
->Driver
.FreeTextureImageBuffer(ctx
, texImage
);
1934 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
, 1,
1935 border
, internalFormat
, texFormat
);
1937 /* Allocate texture memory (no pixel data yet) */
1939 ctx
->Driver
.TexImage1D(ctx
, texImage
, internalFormat
,
1940 width
, border
, GL_NONE
, GL_NONE
, NULL
,
1944 ctx
->Driver
.TexImage2D(ctx
, texImage
, internalFormat
,
1945 width
, height
, border
, GL_NONE
, GL_NONE
,
1946 NULL
, &ctx
->Unpack
);
1949 if (_mesa_clip_copytexsubimage(ctx
, &dstX
, &dstY
, &srcX
, &srcY
,
1951 struct gl_renderbuffer
*srcRb
=
1952 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
1955 ctx
->Driver
.CopyTexSubImage1D(ctx
, texImage
, dstX
,
1956 srcRb
, srcX
, srcY
, width
);
1959 ctx
->Driver
.CopyTexSubImage2D(ctx
, texImage
, dstX
, dstY
,
1960 srcRb
, srcX
, srcY
, width
, height
);
1964 texObj
->_Complete
= GL_FALSE
;
1965 ctx
->NewState
|= _NEW_TEXTURE
;
1968 /* probably too large of image */
1969 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexImage%uD", dims
);
1973 _mesa_unlock_texture(ctx
, texObj
);
1979 _mesa_CopyTexImage1D( GLenum target
, GLint level
,
1980 GLenum internalFormat
,
1982 GLsizei width
, GLint border
)
1984 GET_CURRENT_CONTEXT(ctx
);
1985 copyteximage(ctx
, 1, target
, level
, internalFormat
, x
, y
, width
, 1, border
);
1991 _mesa_CopyTexImage2D( GLenum target
, GLint level
, GLenum internalFormat
,
1992 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1995 GET_CURRENT_CONTEXT(ctx
);
1996 copyteximage(ctx
, 2, target
, level
, internalFormat
,
1997 x
, y
, width
, height
, border
);
2003 * Implementation for glCopyTexSubImage1/2/3D() functions.
2006 copytexsubimage(struct gl_context
*ctx
, GLuint dims
, GLenum target
, GLint level
,
2007 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2008 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
2010 struct gl_texture_object
*texObj
;
2011 struct gl_texture_image
*texImage
;
2013 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
2015 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
2016 _mesa_debug(ctx
, "glCopyTexSubImage%uD %s %d %d %d %d %d %d %d %d\n",
2018 _mesa_lookup_enum_by_nr(target
),
2019 level
, xoffset
, yoffset
, zoffset
, x
, y
, width
, height
);
2021 if (ctx
->NewState
& NEW_COPY_TEX_STATE
)
2022 _mesa_update_state(ctx
);
2024 if (copytexsubimage_error_check1(ctx
, dims
, target
, level
))
2027 texObj
= _mesa_select_tex_object(ctx
, target
);
2029 _mesa_lock_texture(ctx
, texObj
);
2031 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
2033 if (copytexsubimage_error_check2(ctx
, dims
, target
, level
, xoffset
, yoffset
,
2034 zoffset
, width
, height
, texImage
)) {
2035 /* error was recored */
2038 /* If we have a border, offset=-1 is legal. Bias by border width. */
2041 zoffset
+= texImage
->Border
;
2044 yoffset
+= texImage
->Border
;
2047 xoffset
+= texImage
->Border
;
2050 if (_mesa_clip_copytexsubimage(ctx
, &xoffset
, &yoffset
, &x
, &y
,
2052 struct gl_renderbuffer
*srcRb
=
2053 get_copy_tex_image_source(ctx
, texImage
->TexFormat
);
2057 ctx
->Driver
.CopyTexSubImage1D(ctx
, texImage
, xoffset
,
2058 srcRb
, x
, y
, width
);
2061 ctx
->Driver
.CopyTexSubImage2D(ctx
, texImage
, xoffset
, yoffset
,
2062 srcRb
, x
, y
, width
, height
);
2065 _mesa_problem(ctx
, "bad dims in copytexsubimage()");
2068 ctx
->NewState
|= _NEW_TEXTURE
;
2072 _mesa_unlock_texture(ctx
, texObj
);
2077 _mesa_CopyTexSubImage1D( GLenum target
, GLint level
,
2078 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
2080 GET_CURRENT_CONTEXT(ctx
);
2081 copytexsubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, x
, y
, width
, 1);
2087 _mesa_CopyTexSubImage2D( GLenum target
, GLint level
,
2088 GLint xoffset
, GLint yoffset
,
2089 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
2091 GET_CURRENT_CONTEXT(ctx
);
2092 copytexsubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0, x
, y
,