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 OTHER LIABILITY, WHETHER IN
20 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * GL_ARB_texture_storage functions
39 #include "texstorage.h"
45 * Check if the given texture target is a legal texture object target
46 * for a glTexStorage() command.
47 * This is a bit different than legal_teximage_target() when it comes
51 legal_texobj_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
57 case GL_PROXY_TEXTURE_1D
:
65 case GL_PROXY_TEXTURE_2D
:
67 case GL_TEXTURE_CUBE_MAP
:
68 case GL_PROXY_TEXTURE_CUBE_MAP
:
69 return ctx
->Extensions
.ARB_texture_cube_map
;
70 case GL_TEXTURE_RECTANGLE
:
71 case GL_PROXY_TEXTURE_RECTANGLE
:
72 return ctx
->Extensions
.NV_texture_rectangle
;
73 case GL_TEXTURE_1D_ARRAY
:
74 case GL_PROXY_TEXTURE_1D_ARRAY
:
75 return (ctx
->Extensions
.MESA_texture_array
||
76 ctx
->Extensions
.EXT_texture_array
);
83 case GL_PROXY_TEXTURE_3D
:
85 case GL_TEXTURE_2D_ARRAY
:
86 case GL_PROXY_TEXTURE_2D_ARRAY
:
87 return (ctx
->Extensions
.MESA_texture_array
||
88 ctx
->Extensions
.EXT_texture_array
);
89 case GL_TEXTURE_CUBE_MAP_ARRAY
:
90 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
91 return ctx
->Extensions
.ARB_texture_cube_map_array
;
96 _mesa_problem(ctx
, "invalid dims=%u in legal_texobj_target()", dims
);
103 * Compute the size of the next mipmap level.
106 next_mipmap_level_size(GLenum target
,
107 GLint
*width
, GLint
*height
, GLint
*depth
)
113 if ((*height
> 1) && (target
!= GL_TEXTURE_1D_ARRAY
)) {
117 if ((*depth
> 1) && (target
!= GL_TEXTURE_2D_ARRAY
)) {
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
, gl_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 next_mipmap_level_size(target
, &levelWidth
, &levelHeight
, &levelDepth
);
173 * Clear all fields of texture object to zeros. Used for proxy texture tests.
174 * Used for proxy texture tests (and to clean up when a texture memory
178 clear_texture_fields(struct gl_context
*ctx
,
179 struct gl_texture_object
*texObj
)
181 const GLenum target
= texObj
->Target
;
182 const GLuint numFaces
= _mesa_num_tex_faces(target
);
186 for (level
= 0; level
< Elements(texObj
->Image
[0]); level
++) {
187 for (face
= 0; face
< numFaces
; face
++) {
188 struct gl_texture_image
*texImage
=
189 get_tex_image(ctx
, texObj
, face
, level
);
192 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexStorage");
196 _mesa_init_teximage_fields(ctx
, texImage
,
197 0, 0, 0, 0, /* w, h, d, border */
198 GL_NONE
, MESA_FORMAT_NONE
);
205 _mesa_is_legal_tex_storage_format(struct gl_context
*ctx
, GLenum internalformat
)
207 /* check internal format - note that only sized formats are allowed */
208 switch (internalformat
) {
211 case GL_LUMINANCE_ALPHA
:
218 case GL_DEPTH_COMPONENT
:
219 case GL_DEPTH_STENCIL
:
220 case GL_COMPRESSED_ALPHA
:
221 case GL_COMPRESSED_LUMINANCE_ALPHA
:
222 case GL_COMPRESSED_LUMINANCE
:
223 case GL_COMPRESSED_INTENSITY
:
224 case GL_COMPRESSED_RGB
:
225 case GL_COMPRESSED_RGBA
:
226 case GL_COMPRESSED_SRGB
:
227 case GL_COMPRESSED_SRGB_ALPHA
:
228 case GL_COMPRESSED_SLUMINANCE
:
229 case GL_COMPRESSED_SLUMINANCE_ALPHA
:
231 case GL_GREEN_INTEGER
:
232 case GL_BLUE_INTEGER
:
233 case GL_ALPHA_INTEGER
:
235 case GL_RGBA_INTEGER
:
237 case GL_BGRA_INTEGER
:
238 case GL_LUMINANCE_INTEGER_EXT
:
239 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
240 /* these unsized formats are illegal */
243 return _mesa_base_tex_format(ctx
, internalformat
) > 0;
248 * Default ctx->Driver.AllocTextureStorage() handler.
250 * The driver can override this with a more specific implementation if it
251 * desires, but this can be used to get the texture images allocated using the
252 * usual texture image handling code. The immutability of
253 * GL_ARB_texture_storage texture layouts is handled by texObj->Immutable
254 * checks at glTexImage* time.
257 _mesa_alloc_texture_storage(struct gl_context
*ctx
,
258 struct gl_texture_object
*texObj
,
259 GLsizei levels
, GLsizei width
,
260 GLsizei height
, GLsizei depth
)
262 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
266 for (face
= 0; face
< numFaces
; face
++) {
267 for (level
= 0; level
< levels
; level
++) {
268 struct gl_texture_image
*const texImage
= texObj
->Image
[face
][level
];
269 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
))
279 * Do error checking for calls to glTexStorage1/2/3D().
280 * If an error is found, record it with _mesa_error(), unless the target
281 * is a proxy texture.
282 * \return GL_TRUE if any error, GL_FALSE otherwise.
285 tex_storage_error_check(struct gl_context
*ctx
, GLuint dims
, GLenum target
,
286 GLsizei levels
, GLenum internalformat
,
287 GLsizei width
, GLsizei height
, GLsizei depth
)
289 struct gl_texture_object
*texObj
;
291 if (!_mesa_is_legal_tex_storage_format(ctx
, internalformat
)) {
292 _mesa_error(ctx
, GL_INVALID_ENUM
,
293 "glTexStorage%uD(internalformat = %s)", dims
,
294 _mesa_lookup_enum_by_nr(internalformat
));
299 if (width
< 1 || height
< 1 || depth
< 1) {
300 _mesa_error(ctx
, GL_INVALID_VALUE
,
301 "glTexStorage%uD(width, height or depth < 1)", dims
);
306 if (!legal_texobj_target(ctx
, dims
, target
)) {
307 _mesa_error(ctx
, GL_INVALID_ENUM
,
308 "glTexStorage%uD(illegal target=%s)",
309 dims
, _mesa_lookup_enum_by_nr(target
));
315 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexStorage%uD(levels < 1)",
320 /* check levels against maximum (note different error than above) */
321 if (levels
> (GLint
) _mesa_max_texture_levels(ctx
, target
)) {
322 _mesa_error(ctx
, GL_INVALID_OPERATION
,
323 "glTexStorage%uD(levels too large)", dims
);
327 /* check levels against width/height/depth */
328 if (levels
> _mesa_get_tex_max_num_levels(target
, width
, height
, depth
)) {
329 _mesa_error(ctx
, GL_INVALID_OPERATION
,
330 "glTexStorage%uD(too many levels for max texture dimension)",
335 /* non-default texture object check */
336 texObj
= _mesa_get_current_tex_object(ctx
, target
);
337 if (!texObj
|| (texObj
->Name
== 0)) {
338 _mesa_error(ctx
, GL_INVALID_OPERATION
,
339 "glTexStorage%uD(texture object 0)", dims
);
343 /* Check if texObj->Immutable is set */
344 if (texObj
->Immutable
) {
345 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexStorage%uD(immutable)",
355 * Helper used by _mesa_TexStorage1/2/3D().
358 texstorage(GLuint dims
, GLenum target
, GLsizei levels
, GLenum internalformat
,
359 GLsizei width
, GLsizei height
, GLsizei depth
)
361 struct gl_texture_object
*texObj
;
362 GLboolean sizeOK
, dimensionsOK
;
365 GET_CURRENT_CONTEXT(ctx
);
367 if (tex_storage_error_check(ctx
, dims
, target
, levels
,
368 internalformat
, width
, height
, depth
)) {
369 return; /* error was recorded */
372 texObj
= _mesa_get_current_tex_object(ctx
, target
);
375 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, 0,
376 internalformat
, GL_NONE
, GL_NONE
);
377 assert(texFormat
!= MESA_FORMAT_NONE
);
379 /* check that width, height, depth are legal for the mipmap level */
380 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, 0,
381 width
, height
, depth
, 0);
383 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, target
, 0, texFormat
,
384 width
, height
, depth
, 0);
386 if (_mesa_is_proxy_texture(texObj
->Target
)) {
387 if (dimensionsOK
&& sizeOK
) {
388 initialize_texture_fields(ctx
, texObj
, levels
, width
, height
, depth
,
389 internalformat
, texFormat
);
392 /* clear all image fields for [levels] */
393 clear_texture_fields(ctx
, texObj
);
398 _mesa_error(ctx
, GL_INVALID_VALUE
,
399 "glTexStorage%uD(invalid width, height or depth)", dims
);
404 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
405 "glTexStorage%uD(texture too large)", dims
);
413 if (!initialize_texture_fields(ctx
, texObj
, levels
, width
, height
, depth
,
414 internalformat
, texFormat
)) {
418 /* Do actual texture memory allocation */
419 if (!ctx
->Driver
.AllocTextureStorage(ctx
, texObj
, levels
,
420 width
, height
, depth
)) {
421 /* Reset the texture images' info to zeros.
422 * Strictly speaking, we probably don't have to do this since
423 * generating GL_OUT_OF_MEMORY can leave things in an undefined
424 * state but this puts things in a consistent state.
426 clear_texture_fields(ctx
, texObj
);
427 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexStorage%uD", dims
);
431 texObj
->Immutable
= GL_TRUE
;
432 texObj
->ImmutableLevels
= levels
;
438 _mesa_TexStorage1D(GLenum target
, GLsizei levels
, GLenum internalformat
,
441 texstorage(1, target
, levels
, internalformat
, width
, 1, 1);
446 _mesa_TexStorage2D(GLenum target
, GLsizei levels
, GLenum internalformat
,
447 GLsizei width
, GLsizei height
)
449 texstorage(2, target
, levels
, internalformat
, width
, height
, 1);
454 _mesa_TexStorage3D(GLenum target
, GLsizei levels
, GLenum internalformat
,
455 GLsizei width
, GLsizei height
, GLsizei depth
)
457 texstorage(3, target
, levels
, internalformat
, width
, height
, depth
);
463 * Note: we don't support GL_EXT_direct_state_access and the spec says
464 * we don't need the following functions. However, glew checks for the
465 * presence of all six functions and will say that GL_ARB_texture_storage
466 * is not supported if these functions are missing.
471 _mesa_TextureStorage1DEXT(GLuint texture
, GLenum target
, GLsizei levels
,
472 GLenum internalformat
,
480 _mesa_TextureStorage2DEXT(GLuint texture
, GLenum target
, GLsizei levels
,
481 GLenum internalformat
,
482 GLsizei width
, GLsizei height
)
490 _mesa_TextureStorage3DEXT(GLuint texture
, GLenum target
, GLsizei levels
,
491 GLenum internalformat
,
492 GLsizei width
, GLsizei height
, GLsizei depth
)