2 * Mesa 3-D graphics library
4 * Copyright (C) 2011 VMware, Inc. All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
27 * GL_ARB_texture_storage functions
38 #include "texstorage.h"
39 #include "textureview.h"
41 #include "glformats.h"
46 * Check if the given texture target is a legal texture object target
47 * for a glTexStorage() command.
48 * This is a bit different than legal_teximage_target() when it comes
52 legal_texobj_target(const struct gl_context
*ctx
, GLuint dims
, GLenum target
)
54 if (dims
< 1 || dims
> 3) {
55 _mesa_problem(ctx
, "invalid dims=%u in legal_texobj_target()", dims
);
64 case GL_TEXTURE_CUBE_MAP
:
65 return ctx
->Extensions
.ARB_texture_cube_map
;
72 case GL_TEXTURE_2D_ARRAY
:
73 return ctx
->Extensions
.EXT_texture_array
;
78 if (!_mesa_is_desktop_gl(ctx
))
85 case GL_PROXY_TEXTURE_1D
:
92 case GL_PROXY_TEXTURE_2D
:
94 case GL_PROXY_TEXTURE_CUBE_MAP
:
95 return ctx
->Extensions
.ARB_texture_cube_map
;
96 case GL_TEXTURE_RECTANGLE
:
97 case GL_PROXY_TEXTURE_RECTANGLE
:
98 return ctx
->Extensions
.NV_texture_rectangle
;
99 case GL_TEXTURE_1D_ARRAY
:
100 case GL_PROXY_TEXTURE_1D_ARRAY
:
101 return ctx
->Extensions
.EXT_texture_array
;
107 case GL_PROXY_TEXTURE_3D
:
109 case GL_PROXY_TEXTURE_2D_ARRAY
:
110 return ctx
->Extensions
.EXT_texture_array
;
111 case GL_TEXTURE_CUBE_MAP_ARRAY
:
112 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
113 return ctx
->Extensions
.ARB_texture_cube_map_array
;
118 unreachable("impossible dimensions");
123 /** Helper to get a particular texture image in a texture object */
124 static struct gl_texture_image
*
125 get_tex_image(struct gl_context
*ctx
,
126 struct gl_texture_object
*texObj
,
127 GLuint face
, GLuint level
)
129 const GLenum faceTarget
=
130 (texObj
->Target
== GL_TEXTURE_CUBE_MAP
||
131 texObj
->Target
== GL_PROXY_TEXTURE_CUBE_MAP
)
132 ? GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ face
: texObj
->Target
;
133 return _mesa_get_tex_image(ctx
, texObj
, faceTarget
, level
);
139 initialize_texture_fields(struct gl_context
*ctx
,
140 struct gl_texture_object
*texObj
,
142 GLsizei width
, GLsizei height
, GLsizei depth
,
143 GLenum internalFormat
, mesa_format texFormat
)
145 const GLenum target
= texObj
->Target
;
146 const GLuint numFaces
= _mesa_num_tex_faces(target
);
147 GLint level
, levelWidth
= width
, levelHeight
= height
, levelDepth
= depth
;
150 /* Set up all the texture object's gl_texture_images */
151 for (level
= 0; level
< levels
; level
++) {
152 for (face
= 0; face
< numFaces
; face
++) {
153 struct gl_texture_image
*texImage
=
154 get_tex_image(ctx
, texObj
, face
, level
);
157 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexStorage");
161 _mesa_init_teximage_fields(ctx
, texImage
,
162 levelWidth
, levelHeight
, levelDepth
,
163 0, internalFormat
, texFormat
);
166 _mesa_next_mipmap_level_size(target
, 0,
167 levelWidth
, levelHeight
, levelDepth
,
168 &levelWidth
, &levelHeight
, &levelDepth
);
175 * Clear all fields of texture object to zeros. Used for proxy texture tests
176 * and to clean up when a texture memory allocation fails.
179 clear_texture_fields(struct gl_context
*ctx
,
180 struct gl_texture_object
*texObj
)
182 const GLenum target
= texObj
->Target
;
183 const GLuint numFaces
= _mesa_num_tex_faces(target
);
187 for (level
= 0; level
< ARRAY_SIZE(texObj
->Image
[0]); level
++) {
188 for (face
= 0; face
< numFaces
; face
++) {
189 struct gl_texture_image
*texImage
=
190 get_tex_image(ctx
, texObj
, face
, level
);
193 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexStorage");
197 _mesa_clear_texture_image(ctx
, texImage
);
204 * Update/re-validate framebuffer object.
207 update_fbo_texture(struct gl_context
*ctx
, struct gl_texture_object
*texObj
)
209 const unsigned numFaces
= _mesa_num_tex_faces(texObj
->Target
);
210 for (int level
= 0; level
< ARRAY_SIZE(texObj
->Image
[0]); level
++) {
211 for (unsigned face
= 0; face
< numFaces
; face
++)
212 _mesa_update_fbo_texture(ctx
, texObj
, face
, level
);
218 _mesa_is_legal_tex_storage_format(const struct gl_context
*ctx
,
219 GLenum internalformat
)
221 /* check internal format - note that only sized formats are allowed */
222 switch (internalformat
) {
225 case GL_LUMINANCE_ALPHA
:
232 case GL_DEPTH_COMPONENT
:
233 case GL_DEPTH_STENCIL
:
234 case GL_COMPRESSED_ALPHA
:
235 case GL_COMPRESSED_LUMINANCE_ALPHA
:
236 case GL_COMPRESSED_LUMINANCE
:
237 case GL_COMPRESSED_INTENSITY
:
238 case GL_COMPRESSED_RGB
:
239 case GL_COMPRESSED_RGBA
:
240 case GL_COMPRESSED_SRGB
:
241 case GL_COMPRESSED_SRGB_ALPHA
:
242 case GL_COMPRESSED_SLUMINANCE
:
243 case GL_COMPRESSED_SLUMINANCE_ALPHA
:
245 case GL_GREEN_INTEGER
:
246 case GL_BLUE_INTEGER
:
247 case GL_ALPHA_INTEGER
:
249 case GL_RGBA_INTEGER
:
251 case GL_BGRA_INTEGER
:
252 case GL_LUMINANCE_INTEGER_EXT
:
253 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
254 /* these unsized formats are illegal */
257 return _mesa_base_tex_format(ctx
, internalformat
) > 0;
263 * Default ctx->Driver.AllocTextureStorage() handler.
265 * The driver can override this with a more specific implementation if it
266 * desires, but this can be used to get the texture images allocated using the
267 * usual texture image handling code. The immutability of
268 * GL_ARB_texture_storage texture layouts is handled by texObj->Immutable
269 * checks at glTexImage* time.
272 _mesa_AllocTextureStorage_sw(struct gl_context
*ctx
,
273 struct gl_texture_object
*texObj
,
274 GLsizei levels
, GLsizei width
,
275 GLsizei height
, GLsizei depth
)
277 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
285 for (face
= 0; face
< numFaces
; face
++) {
286 for (level
= 0; level
< levels
; level
++) {
287 struct gl_texture_image
*const texImage
= texObj
->Image
[face
][level
];
288 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
))
298 * Do error checking for calls to glTexStorage1/2/3D().
299 * If an error is found, record it with _mesa_error(), unless the target
300 * is a proxy texture.
301 * \return GL_TRUE if any error, GL_FALSE otherwise.
304 tex_storage_error_check(struct gl_context
*ctx
,
305 struct gl_texture_object
*texObj
,
306 GLuint dims
, GLenum target
,
307 GLsizei levels
, GLenum internalformat
,
308 GLsizei width
, GLsizei height
, GLsizei depth
,
311 const char* suffix
= dsa
? "ture" : "";
313 /* Legal format checking has been moved to texstorage and texturestorage in
314 * order to allow meta functions to use legacy formats. */
317 if (!_mesa_valid_tex_storage_dim(width
, height
, depth
)) {
318 _mesa_error(ctx
, GL_INVALID_VALUE
,
319 "glTex%sStorage%uD(width, height or depth < 1)",
324 if (_mesa_is_compressed_format(ctx
, internalformat
)) {
326 if (!_mesa_target_can_be_compressed(ctx
, target
, internalformat
, &err
)) {
327 _mesa_error(ctx
, err
,
328 "glTex%sStorage%dD(internalformat = %s)", suffix
, dims
,
329 _mesa_enum_to_string(internalformat
));
336 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTex%sStorage%uD(levels < 1)",
341 /* check levels against maximum (note different error than above) */
342 if (levels
> (GLint
) _mesa_max_texture_levels(ctx
, target
)) {
343 _mesa_error(ctx
, GL_INVALID_OPERATION
,
344 "glTex%sStorage%uD(levels too large)",
349 /* check levels against width/height/depth */
350 if (levels
> _mesa_get_tex_max_num_levels(target
, width
, height
, depth
)) {
351 _mesa_error(ctx
, GL_INVALID_OPERATION
,
352 "glTex%sStorage%uD(too many levels"
353 " for max texture dimension)",
358 /* non-default texture object check */
359 if (!_mesa_is_proxy_texture(target
) && (!texObj
|| (texObj
->Name
== 0))) {
360 _mesa_error(ctx
, GL_INVALID_OPERATION
,
361 "glTex%sStorage%uD(texture object 0)",
366 /* Check if texObj->Immutable is set */
367 if (!_mesa_is_proxy_texture(target
) && texObj
->Immutable
) {
368 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTex%sStorage%uD(immutable)",
373 /* additional checks for depth textures */
374 if (!_mesa_legal_texture_base_format_for_target(ctx
, target
, internalformat
)) {
375 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTex%sStorage%uD(bad target for texture)",
385 * Helper that does the storage allocation for _mesa_TexStorage1/2/3D()
386 * and _mesa_TextureStorage1/2/3D().
389 _mesa_texture_storage(struct gl_context
*ctx
, GLuint dims
,
390 struct gl_texture_object
*texObj
,
391 GLenum target
, GLsizei levels
,
392 GLenum internalformat
, GLsizei width
,
393 GLsizei height
, GLsizei depth
, bool dsa
)
395 GLboolean sizeOK
, dimensionsOK
;
396 mesa_format texFormat
;
397 const char* suffix
= dsa
? "ture" : "";
401 if (tex_storage_error_check(ctx
, texObj
, dims
, target
, levels
,
402 internalformat
, width
, height
, depth
, dsa
)) {
403 return; /* error was recorded */
406 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, 0,
407 internalformat
, GL_NONE
, GL_NONE
);
408 assert(texFormat
!= MESA_FORMAT_NONE
);
410 /* check that width, height, depth are legal for the mipmap level */
411 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, 0,
412 width
, height
, depth
, 0);
414 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, target
, levels
, 0, texFormat
,
415 1, width
, height
, depth
);
417 if (_mesa_is_proxy_texture(target
)) {
418 if (dimensionsOK
&& sizeOK
) {
419 initialize_texture_fields(ctx
, texObj
, levels
, width
, height
, depth
,
420 internalformat
, texFormat
);
423 /* clear all image fields for [levels] */
424 clear_texture_fields(ctx
, texObj
);
429 _mesa_error(ctx
, GL_INVALID_VALUE
,
430 "glTex%sStorage%uD(invalid width, height or depth)",
436 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
437 "glTex%sStorage%uD(texture too large)",
446 if (!initialize_texture_fields(ctx
, texObj
, levels
, width
, height
, depth
,
447 internalformat
, texFormat
)) {
451 /* Do actual texture memory allocation */
452 if (!ctx
->Driver
.AllocTextureStorage(ctx
, texObj
, levels
,
453 width
, height
, depth
)) {
454 /* Reset the texture images' info to zeros.
455 * Strictly speaking, we probably don't have to do this since
456 * generating GL_OUT_OF_MEMORY can leave things in an undefined
457 * state but this puts things in a consistent state.
459 clear_texture_fields(ctx
, texObj
);
460 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTex%sStorage%uD",
465 _mesa_set_texture_view_state(ctx
, texObj
, target
, levels
);
467 update_fbo_texture(ctx
, texObj
);
473 * Helper used by _mesa_TexStorage1/2/3D().
476 texstorage(GLuint dims
, GLenum target
, GLsizei levels
, GLenum internalformat
,
477 GLsizei width
, GLsizei height
, GLsizei depth
)
479 struct gl_texture_object
*texObj
;
480 GET_CURRENT_CONTEXT(ctx
);
482 /* Check target. This is done here so that _mesa_texture_storage
483 * can receive unsized formats.
485 if (!legal_texobj_target(ctx
, dims
, target
)) {
486 _mesa_error(ctx
, GL_INVALID_ENUM
,
487 "glTexStorage%uD(illegal target=%s)",
488 dims
, _mesa_enum_to_string(target
));
492 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
493 _mesa_debug(ctx
, "glTexStorage%uD %s %d %s %d %d %d\n",
495 _mesa_enum_to_string(target
), levels
,
496 _mesa_enum_to_string(internalformat
),
497 width
, height
, depth
);
499 /* Check the format to make sure it is sized. */
500 if (!_mesa_is_legal_tex_storage_format(ctx
, internalformat
)) {
501 _mesa_error(ctx
, GL_INVALID_ENUM
,
502 "glTexStorage%uD(internalformat = %s)", dims
,
503 _mesa_enum_to_string(internalformat
));
507 texObj
= _mesa_get_current_tex_object(ctx
, target
);
511 _mesa_texture_storage(ctx
, dims
, texObj
, target
, levels
,
512 internalformat
, width
, height
, depth
, false);
517 * Helper used by _mesa_TextureStorage1/2/3D().
520 texturestorage(GLuint dims
, GLuint texture
, GLsizei levels
,
521 GLenum internalformat
, GLsizei width
, GLsizei height
,
524 struct gl_texture_object
*texObj
;
525 GET_CURRENT_CONTEXT(ctx
);
527 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
528 _mesa_debug(ctx
, "glTextureStorage%uD %d %d %s %d %d %d\n",
529 dims
, texture
, levels
,
530 _mesa_enum_to_string(internalformat
),
531 width
, height
, depth
);
533 /* Check the format to make sure it is sized. */
534 if (!_mesa_is_legal_tex_storage_format(ctx
, internalformat
)) {
535 _mesa_error(ctx
, GL_INVALID_ENUM
,
536 "glTextureStorage%uD(internalformat = %s)", dims
,
537 _mesa_enum_to_string(internalformat
));
541 /* Get the texture object by Name. */
542 texObj
= _mesa_lookup_texture(ctx
, texture
);
544 _mesa_error(ctx
, GL_INVALID_OPERATION
,
545 "glTextureStorage%uD(texture = %d)", dims
, texture
);
549 /* Check target. This is done here so that _mesa_texture_storage
550 * can receive unsized formats.
552 if (!legal_texobj_target(ctx
, dims
, texObj
->Target
)) {
553 _mesa_error(ctx
, GL_INVALID_ENUM
,
554 "glTextureStorage%uD(illegal target=%s)",
555 dims
, _mesa_enum_to_string(texObj
->Target
));
559 _mesa_texture_storage(ctx
, dims
, texObj
, texObj
->Target
,
560 levels
, internalformat
, width
, height
, depth
, true);
565 _mesa_TexStorage1D(GLenum target
, GLsizei levels
, GLenum internalformat
,
568 texstorage(1, target
, levels
, internalformat
, width
, 1, 1);
573 _mesa_TexStorage2D(GLenum target
, GLsizei levels
, GLenum internalformat
,
574 GLsizei width
, GLsizei height
)
576 texstorage(2, target
, levels
, internalformat
, width
, height
, 1);
581 _mesa_TexStorage3D(GLenum target
, GLsizei levels
, GLenum internalformat
,
582 GLsizei width
, GLsizei height
, GLsizei depth
)
584 texstorage(3, target
, levels
, internalformat
, width
, height
, depth
);
589 _mesa_TextureStorage1D(GLuint texture
, GLsizei levels
, GLenum internalformat
,
592 texturestorage(1, texture
, levels
, internalformat
, width
, 1, 1);
597 _mesa_TextureStorage2D(GLuint texture
, GLsizei levels
,
598 GLenum internalformat
,
599 GLsizei width
, GLsizei height
)
601 texturestorage(2, texture
, levels
, internalformat
, width
, height
, 1);
606 _mesa_TextureStorage3D(GLuint texture
, GLsizei levels
, GLenum internalformat
,
607 GLsizei width
, GLsizei height
, GLsizei depth
)
609 texturestorage(3, texture
, levels
, internalformat
, width
, height
, depth
);
614 * Note: we don't support GL_EXT_direct_state_access and the spec says
615 * we don't need the following functions. However, glew checks for the
616 * presence of all six functions and will say that GL_ARB_texture_storage
617 * is not supported if these functions are missing.
622 _mesa_TextureStorage1DEXT(GLuint texture
, GLenum target
, GLsizei levels
,
623 GLenum internalformat
,
626 GET_CURRENT_CONTEXT(ctx
);
631 (void) internalformat
;
634 _mesa_error(ctx
, GL_INVALID_OPERATION
,
635 "glTextureStorage1DEXT not supported");
640 _mesa_TextureStorage2DEXT(GLuint texture
, GLenum target
, GLsizei levels
,
641 GLenum internalformat
,
642 GLsizei width
, GLsizei height
)
644 GET_CURRENT_CONTEXT(ctx
);
649 (void) internalformat
;
653 _mesa_error(ctx
, GL_INVALID_OPERATION
,
654 "glTextureStorage2DEXT not supported");
659 _mesa_TextureStorage3DEXT(GLuint texture
, GLenum target
, GLsizei levels
,
660 GLenum internalformat
,
661 GLsizei width
, GLsizei height
, GLsizei depth
)
663 GET_CURRENT_CONTEXT(ctx
);
668 (void) internalformat
;
673 _mesa_error(ctx
, GL_INVALID_OPERATION
,
674 "glTextureStorage3DEXT not supported");