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.
28 * GL_ARB_texture_storage functions
41 #include "texstorage.h"
42 #include "textureview.h"
44 #include "glformats.h"
49 * Check if the given texture target is a legal texture object target
50 * for a glTexStorage() command.
51 * This is a bit different than legal_teximage_target() when it comes
55 legal_texobj_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
57 if (_mesa_is_gles3(ctx
)
58 && target
!= GL_TEXTURE_2D
59 && target
!= GL_TEXTURE_CUBE_MAP
60 && target
!= GL_TEXTURE_3D
61 && target
!= GL_TEXTURE_2D_ARRAY
)
68 case GL_PROXY_TEXTURE_1D
:
76 case GL_PROXY_TEXTURE_2D
:
78 case GL_TEXTURE_CUBE_MAP
:
79 case GL_PROXY_TEXTURE_CUBE_MAP
:
80 return ctx
->Extensions
.ARB_texture_cube_map
;
81 case GL_TEXTURE_RECTANGLE
:
82 case GL_PROXY_TEXTURE_RECTANGLE
:
83 return ctx
->Extensions
.NV_texture_rectangle
;
84 case GL_TEXTURE_1D_ARRAY
:
85 case GL_PROXY_TEXTURE_1D_ARRAY
:
86 return ctx
->Extensions
.EXT_texture_array
;
93 case GL_PROXY_TEXTURE_3D
:
95 case GL_TEXTURE_2D_ARRAY
:
96 case GL_PROXY_TEXTURE_2D_ARRAY
:
97 return ctx
->Extensions
.EXT_texture_array
;
98 case GL_TEXTURE_CUBE_MAP_ARRAY
:
99 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
100 return ctx
->Extensions
.ARB_texture_cube_map_array
;
105 _mesa_problem(ctx
, "invalid dims=%u in legal_texobj_target()", dims
);
111 /** Helper to get a particular texture image in a texture object */
112 static struct gl_texture_image
*
113 get_tex_image(struct gl_context
*ctx
,
114 struct gl_texture_object
*texObj
,
115 GLuint face
, GLuint level
)
117 const GLenum faceTarget
=
118 (texObj
->Target
== GL_TEXTURE_CUBE_MAP
||
119 texObj
->Target
== GL_PROXY_TEXTURE_CUBE_MAP
)
120 ? GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ face
: texObj
->Target
;
121 return _mesa_get_tex_image(ctx
, texObj
, faceTarget
, level
);
127 initialize_texture_fields(struct gl_context
*ctx
,
128 struct gl_texture_object
*texObj
,
130 GLsizei width
, GLsizei height
, GLsizei depth
,
131 GLenum internalFormat
, mesa_format texFormat
)
133 const GLenum target
= texObj
->Target
;
134 const GLuint numFaces
= _mesa_num_tex_faces(target
);
135 GLint level
, levelWidth
= width
, levelHeight
= height
, levelDepth
= depth
;
138 /* Set up all the texture object's gl_texture_images */
139 for (level
= 0; level
< levels
; level
++) {
140 for (face
= 0; face
< numFaces
; face
++) {
141 struct gl_texture_image
*texImage
=
142 get_tex_image(ctx
, texObj
, face
, level
);
145 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexStorage");
149 _mesa_init_teximage_fields(ctx
, texImage
,
150 levelWidth
, levelHeight
, levelDepth
,
151 0, internalFormat
, texFormat
);
154 _mesa_next_mipmap_level_size(target
, 0, levelWidth
, levelHeight
, levelDepth
,
155 &levelWidth
, &levelHeight
, &levelDepth
);
162 * Clear all fields of texture object to zeros. Used for proxy texture tests
163 * and to clean up when a texture memory allocation fails.
166 clear_texture_fields(struct gl_context
*ctx
,
167 struct gl_texture_object
*texObj
)
169 const GLenum target
= texObj
->Target
;
170 const GLuint numFaces
= _mesa_num_tex_faces(target
);
174 for (level
= 0; level
< ARRAY_SIZE(texObj
->Image
[0]); level
++) {
175 for (face
= 0; face
< numFaces
; face
++) {
176 struct gl_texture_image
*texImage
=
177 get_tex_image(ctx
, texObj
, face
, level
);
180 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexStorage");
184 _mesa_init_teximage_fields(ctx
, texImage
,
185 0, 0, 0, 0, /* w, h, d, border */
186 GL_NONE
, MESA_FORMAT_NONE
);
193 _mesa_is_legal_tex_storage_format(struct gl_context
*ctx
, GLenum internalformat
)
195 /* check internal format - note that only sized formats are allowed */
196 switch (internalformat
) {
199 case GL_LUMINANCE_ALPHA
:
206 case GL_DEPTH_COMPONENT
:
207 case GL_DEPTH_STENCIL
:
208 case GL_COMPRESSED_ALPHA
:
209 case GL_COMPRESSED_LUMINANCE_ALPHA
:
210 case GL_COMPRESSED_LUMINANCE
:
211 case GL_COMPRESSED_INTENSITY
:
212 case GL_COMPRESSED_RGB
:
213 case GL_COMPRESSED_RGBA
:
214 case GL_COMPRESSED_SRGB
:
215 case GL_COMPRESSED_SRGB_ALPHA
:
216 case GL_COMPRESSED_SLUMINANCE
:
217 case GL_COMPRESSED_SLUMINANCE_ALPHA
:
219 case GL_GREEN_INTEGER
:
220 case GL_BLUE_INTEGER
:
221 case GL_ALPHA_INTEGER
:
223 case GL_RGBA_INTEGER
:
225 case GL_BGRA_INTEGER
:
226 case GL_LUMINANCE_INTEGER_EXT
:
227 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
228 /* these unsized formats are illegal */
231 return _mesa_base_tex_format(ctx
, internalformat
) > 0;
236 * Default ctx->Driver.AllocTextureStorage() handler.
238 * The driver can override this with a more specific implementation if it
239 * desires, but this can be used to get the texture images allocated using the
240 * usual texture image handling code. The immutability of
241 * GL_ARB_texture_storage texture layouts is handled by texObj->Immutable
242 * checks at glTexImage* time.
245 _mesa_AllocTextureStorage_sw(struct gl_context
*ctx
,
246 struct gl_texture_object
*texObj
,
247 GLsizei levels
, GLsizei width
,
248 GLsizei height
, GLsizei depth
)
250 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
258 for (face
= 0; face
< numFaces
; face
++) {
259 for (level
= 0; level
< levels
; level
++) {
260 struct gl_texture_image
*const texImage
= texObj
->Image
[face
][level
];
261 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
))
271 * Do error checking for calls to glTexStorage1/2/3D().
272 * If an error is found, record it with _mesa_error(), unless the target
273 * is a proxy texture.
274 * \return GL_TRUE if any error, GL_FALSE otherwise.
277 tex_storage_error_check(struct gl_context
*ctx
,
278 struct gl_texture_object
*texObj
,
279 GLuint dims
, GLenum target
,
280 GLsizei levels
, GLenum internalformat
,
281 GLsizei width
, GLsizei height
, GLsizei depth
,
284 const char* suffix
= dsa
? "ture" : "";
286 /* Legal format checking has been moved to texstorage and texturestorage in
287 * order to allow meta functions to use legacy formats. */
290 if (!_mesa_valid_tex_storage_dim(width
, height
, depth
)) {
291 _mesa_error(ctx
, GL_INVALID_VALUE
,
292 "glTex%sStorage%uD(width, height or depth < 1)",
297 /* From section 3.8.6, page 146 of OpenGL ES 3.0 spec:
299 * "The ETC2/EAC texture compression algorithm supports only
300 * two-dimensional images. If internalformat is an ETC2/EAC format,
301 * CompressedTexImage3D will generate an INVALID_OPERATION error if
302 * target is not TEXTURE_2D_ARRAY."
304 * This should also be applicable for glTexStorage3D().
306 if (_mesa_is_compressed_format(ctx
, internalformat
)
307 && !_mesa_target_can_be_compressed(ctx
, target
, internalformat
)) {
308 _mesa_error(ctx
, _mesa_is_desktop_gl(ctx
)?
309 GL_INVALID_ENUM
: GL_INVALID_OPERATION
,
310 "glTex%sStorage%dD(internalformat = %s)", suffix
, dims
,
311 _mesa_enum_to_string(internalformat
));
317 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTex%sStorage%uD(levels < 1)",
322 /* check levels against maximum (note different error than above) */
323 if (levels
> (GLint
) _mesa_max_texture_levels(ctx
, target
)) {
324 _mesa_error(ctx
, GL_INVALID_OPERATION
,
325 "glTex%sStorage%uD(levels too large)",
330 /* check levels against width/height/depth */
331 if (levels
> _mesa_get_tex_max_num_levels(target
, width
, height
, depth
)) {
332 _mesa_error(ctx
, GL_INVALID_OPERATION
,
333 "glTex%sStorage%uD(too many levels"
334 " for max texture dimension)",
339 /* non-default texture object check */
340 if (!_mesa_is_proxy_texture(target
) && (!texObj
|| (texObj
->Name
== 0))) {
341 _mesa_error(ctx
, GL_INVALID_OPERATION
,
342 "glTex%sStorage%uD(texture object 0)",
347 /* Check if texObj->Immutable is set */
348 if (!_mesa_is_proxy_texture(target
) && texObj
->Immutable
) {
349 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTex%sStorage%uD(immutable)",
354 /* additional checks for depth textures */
355 if (!_mesa_legal_texture_base_format_for_target(ctx
, target
, internalformat
,
366 * Helper that does the storage allocation for _mesa_TexStorage1/2/3D()
367 * and _mesa_TextureStorage1/2/3D().
370 _mesa_texture_storage(struct gl_context
*ctx
, GLuint dims
,
371 struct gl_texture_object
*texObj
,
372 GLenum target
, GLsizei levels
,
373 GLenum internalformat
, GLsizei width
,
374 GLsizei height
, GLsizei depth
, bool dsa
)
376 GLboolean sizeOK
, dimensionsOK
;
377 mesa_format texFormat
;
378 const char* suffix
= dsa
? "ture" : "";
382 if (tex_storage_error_check(ctx
, texObj
, dims
, target
, levels
,
383 internalformat
, width
, height
, depth
, dsa
)) {
384 return; /* error was recorded */
388 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, 0,
389 internalformat
, GL_NONE
, GL_NONE
);
390 assert(texFormat
!= MESA_FORMAT_NONE
);
392 /* check that width, height, depth are legal for the mipmap level */
393 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, 0,
394 width
, height
, depth
, 0);
396 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, target
, 0, texFormat
,
397 width
, height
, depth
, 0);
399 if (_mesa_is_proxy_texture(target
)) {
400 if (dimensionsOK
&& sizeOK
) {
401 initialize_texture_fields(ctx
, texObj
, levels
, width
, height
, depth
,
402 internalformat
, texFormat
);
405 /* clear all image fields for [levels] */
406 clear_texture_fields(ctx
, texObj
);
411 _mesa_error(ctx
, GL_INVALID_VALUE
,
412 "glTex%sStorage%uD(invalid width, height or depth)",
418 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
419 "glTex%sStorage%uD(texture too large)",
428 if (!initialize_texture_fields(ctx
, texObj
, levels
, width
, height
, depth
,
429 internalformat
, texFormat
)) {
433 /* Do actual texture memory allocation */
434 if (!ctx
->Driver
.AllocTextureStorage(ctx
, texObj
, levels
,
435 width
, height
, depth
)) {
436 /* Reset the texture images' info to zeros.
437 * Strictly speaking, we probably don't have to do this since
438 * generating GL_OUT_OF_MEMORY can leave things in an undefined
439 * state but this puts things in a consistent state.
441 clear_texture_fields(ctx
, texObj
);
442 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTex%sStorage%uD",
447 _mesa_set_texture_view_state(ctx
, texObj
, target
, levels
);
453 * Helper used by _mesa_TexStorage1/2/3D().
456 texstorage(GLuint dims
, GLenum target
, GLsizei levels
, GLenum internalformat
,
457 GLsizei width
, GLsizei height
, GLsizei depth
)
459 struct gl_texture_object
*texObj
;
460 GET_CURRENT_CONTEXT(ctx
);
463 /* This is done here so that _mesa_texture_storage can receive unsized
465 if (!legal_texobj_target(ctx
, dims
, target
)) {
466 _mesa_error(ctx
, GL_INVALID_ENUM
,
467 "glTexStorage%uD(illegal target=%s)",
468 dims
, _mesa_enum_to_string(target
));
472 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
473 _mesa_debug(ctx
, "glTexStorage%uD %s %d %s %d %d %d\n",
475 _mesa_enum_to_string(target
), levels
,
476 _mesa_enum_to_string(internalformat
),
477 width
, height
, depth
);
478 /* Check the format to make sure it is sized. */
479 if (!_mesa_is_legal_tex_storage_format(ctx
, internalformat
)) {
480 _mesa_error(ctx
, GL_INVALID_ENUM
,
481 "glTexStorage%uD(internalformat = %s)", dims
,
482 _mesa_enum_to_string(internalformat
));
486 texObj
= _mesa_get_current_tex_object(ctx
, target
);
490 _mesa_texture_storage(ctx
, dims
, texObj
, target
, levels
,
491 internalformat
, width
, height
, depth
, false);
495 * Helper used by _mesa_TextureStorage1/2/3D().
498 texturestorage(GLuint dims
, GLuint texture
, GLsizei levels
,
499 GLenum internalformat
, GLsizei width
, GLsizei height
,
502 struct gl_texture_object
*texObj
;
503 GET_CURRENT_CONTEXT(ctx
);
505 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
506 _mesa_debug(ctx
, "glTextureStorage%uD %d %d %s %d %d %d\n",
507 dims
, texture
, levels
,
508 _mesa_enum_to_string(internalformat
),
509 width
, height
, depth
);
511 /* Check the format to make sure it is sized. */
512 if (!_mesa_is_legal_tex_storage_format(ctx
, internalformat
)) {
513 _mesa_error(ctx
, GL_INVALID_ENUM
,
514 "glTextureStorage%uD(internalformat = %s)", dims
,
515 _mesa_enum_to_string(internalformat
));
519 /* Get the texture object by Name. */
520 texObj
= _mesa_lookup_texture(ctx
, texture
);
522 _mesa_error(ctx
, GL_INVALID_OPERATION
,
523 "glTextureStorage%uD(texture = %d)", dims
, texture
);
528 /* This is done here so that _mesa_texture_storage can receive unsized
530 if (!legal_texobj_target(ctx
, dims
, texObj
->Target
)) {
531 _mesa_error(ctx
, GL_INVALID_ENUM
,
532 "glTextureStorage%uD(illegal target=%s)",
533 dims
, _mesa_enum_to_string(texObj
->Target
));
537 _mesa_texture_storage(ctx
, dims
, texObj
, texObj
->Target
,
538 levels
, internalformat
, width
, height
, depth
, true);
542 _mesa_TexStorage1D(GLenum target
, GLsizei levels
, GLenum internalformat
,
545 texstorage(1, target
, levels
, internalformat
, width
, 1, 1);
550 _mesa_TexStorage2D(GLenum target
, GLsizei levels
, GLenum internalformat
,
551 GLsizei width
, GLsizei height
)
553 texstorage(2, target
, levels
, internalformat
, width
, height
, 1);
558 _mesa_TexStorage3D(GLenum target
, GLsizei levels
, GLenum internalformat
,
559 GLsizei width
, GLsizei height
, GLsizei depth
)
561 texstorage(3, target
, levels
, internalformat
, width
, height
, depth
);
565 _mesa_TextureStorage1D(GLuint texture
, GLsizei levels
, GLenum internalformat
,
568 texturestorage(1, texture
, levels
, internalformat
, width
, 1, 1);
573 _mesa_TextureStorage2D(GLuint texture
, GLsizei levels
,
574 GLenum internalformat
,
575 GLsizei width
, GLsizei height
)
577 texturestorage(2, texture
, levels
, internalformat
, width
, height
, 1);
581 _mesa_TextureStorage3D(GLuint texture
, GLsizei levels
, GLenum internalformat
,
582 GLsizei width
, GLsizei height
, GLsizei depth
)
584 texturestorage(3, texture
, levels
, internalformat
, width
, height
, depth
);
589 * Note: we don't support GL_EXT_direct_state_access and the spec says
590 * we don't need the following functions. However, glew checks for the
591 * presence of all six functions and will say that GL_ARB_texture_storage
592 * is not supported if these functions are missing.
597 _mesa_TextureStorage1DEXT(GLuint texture
, GLenum target
, GLsizei levels
,
598 GLenum internalformat
,
601 GET_CURRENT_CONTEXT(ctx
);
606 (void) internalformat
;
609 _mesa_error(ctx
, GL_INVALID_OPERATION
,
610 "glTextureStorage1DEXT not supported");
615 _mesa_TextureStorage2DEXT(GLuint texture
, GLenum target
, GLsizei levels
,
616 GLenum internalformat
,
617 GLsizei width
, GLsizei height
)
619 GET_CURRENT_CONTEXT(ctx
);
624 (void) internalformat
;
628 _mesa_error(ctx
, GL_INVALID_OPERATION
,
629 "glTextureStorage2DEXT not supported");
635 _mesa_TextureStorage3DEXT(GLuint texture
, GLenum target
, GLsizei levels
,
636 GLenum internalformat
,
637 GLsizei width
, GLsizei height
, GLsizei depth
)
639 GET_CURRENT_CONTEXT(ctx
);
644 (void) internalformat
;
649 _mesa_error(ctx
, GL_INVALID_OPERATION
,
650 "glTextureStorage3DEXT not supported");