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 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
37 #include "mfeatures.h"
40 #include "texstorage.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(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
58 case GL_PROXY_TEXTURE_1D
:
66 case GL_PROXY_TEXTURE_2D
:
68 case GL_TEXTURE_CUBE_MAP
:
69 case GL_PROXY_TEXTURE_CUBE_MAP
:
70 return ctx
->Extensions
.ARB_texture_cube_map
;
71 case GL_TEXTURE_RECTANGLE
:
72 case GL_PROXY_TEXTURE_RECTANGLE
:
73 return ctx
->Extensions
.NV_texture_rectangle
;
74 case GL_TEXTURE_1D_ARRAY
:
75 case GL_PROXY_TEXTURE_1D_ARRAY
:
76 return (ctx
->Extensions
.MESA_texture_array
||
77 ctx
->Extensions
.EXT_texture_array
);
84 case GL_PROXY_TEXTURE_3D
:
86 case GL_TEXTURE_2D_ARRAY
:
87 case GL_PROXY_TEXTURE_2D_ARRAY
:
88 return (ctx
->Extensions
.MESA_texture_array
||
89 ctx
->Extensions
.EXT_texture_array
);
94 _mesa_problem(ctx
, "invalid dims=%u in legal_texobj_target()", dims
);
101 * Compute the size of the next mipmap level.
104 next_mipmap_level_size(GLenum target
,
105 GLint
*width
, GLint
*height
, GLint
*depth
)
111 if ((*height
> 1) && (target
!= GL_TEXTURE_1D_ARRAY
)) {
115 if ((*depth
> 1) && (target
!= GL_TEXTURE_2D_ARRAY
)) {
122 * Do actual memory allocation for glTexStorage1/2/3D().
125 setup_texstorage(struct gl_context
*ctx
,
126 struct gl_texture_object
*texObj
,
128 GLsizei levels
, GLenum internalFormat
,
129 GLsizei width
, GLsizei height
, GLsizei depth
)
131 const GLenum target
= texObj
->Target
;
132 const GLuint numFaces
= _mesa_num_tex_faces(target
);
134 GLint level
, levelWidth
= width
, levelHeight
= height
, levelDepth
= depth
;
142 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, 0,
143 internalFormat
, GL_NONE
, GL_NONE
);
145 /* Set up all the texture object's gl_texture_images */
146 for (level
= 0; level
< levels
; level
++) {
147 for (face
= 0; face
< numFaces
; face
++) {
148 const GLenum faceTarget
=
149 (target
== GL_TEXTURE_CUBE_MAP
)
150 ? GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ face
: target
;
151 struct gl_texture_image
*texImage
=
152 _mesa_get_tex_image(ctx
, texObj
, faceTarget
, level
);
155 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
159 _mesa_init_teximage_fields(ctx
, texImage
,
160 levelWidth
, levelHeight
, levelDepth
,
161 0, internalFormat
, texFormat
);
164 next_mipmap_level_size(target
, &levelWidth
, &levelHeight
, &levelDepth
);
167 assert(levelWidth
> 0);
168 assert(levelHeight
> 0);
169 assert(levelDepth
> 0);
171 if (!_mesa_is_proxy_texture(texObj
->Target
)) {
172 /* Do actual texture memory allocation */
173 if (!ctx
->Driver
.AllocTextureStorage(ctx
, texObj
, levels
,
174 width
, height
, depth
)) {
175 /* Reset the texture images' info to zeros.
176 * Strictly speaking, we probably don't have to do this since
177 * generating GL_OUT_OF_MEMORY can leave things in an undefined
178 * state but this puts things in a consistent state.
180 for (level
= 0; level
< levels
; level
++) {
181 for (face
= 0; face
< numFaces
; face
++) {
182 struct gl_texture_image
*texImage
= texObj
->Image
[face
][level
];
184 _mesa_init_teximage_fields(ctx
, texImage
,
186 GL_NONE
, MESA_FORMAT_NONE
);
191 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexStorage%uD", dims
);
197 texObj
->Immutable
= GL_TRUE
;
202 * Clear all fields of texture object to zeros. Used for proxy texture tests.
205 clear_image_fields(struct gl_context
*ctx
,
207 struct gl_texture_object
*texObj
)
209 const GLenum target
= texObj
->Target
;
210 const GLuint numFaces
= _mesa_num_tex_faces(target
);
214 for (level
= 0; level
< Elements(texObj
->Image
[0]); level
++) {
215 for (face
= 0; face
< numFaces
; face
++) {
216 const GLenum faceTarget
=
217 (target
== GL_TEXTURE_CUBE_MAP
)
218 ? GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ face
: target
;
219 struct gl_texture_image
*texImage
=
220 _mesa_get_tex_image(ctx
, texObj
, faceTarget
, level
);
223 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexStorage%uD", dims
);
227 _mesa_init_teximage_fields(ctx
, texImage
,
228 0, 0, 0, 0, GL_NONE
, MESA_FORMAT_NONE
);
235 * Do error checking for calls to glTexStorage1/2/3D().
236 * If an error is found, record it with _mesa_error(), unless the target
237 * is a proxy texture.
238 * \return GL_TRUE if any error, GL_FALSE otherwise.
241 tex_storage_error_check(struct gl_context
*ctx
, GLuint dims
, GLenum target
,
242 GLsizei levels
, GLenum internalformat
,
243 GLsizei width
, GLsizei height
, GLsizei depth
)
245 const GLboolean isProxy
= _mesa_is_proxy_texture(target
);
246 struct gl_texture_object
*texObj
;
250 if (width
< 1 || height
< 1 || depth
< 1) {
252 _mesa_error(ctx
, GL_INVALID_VALUE
,
253 "glTexStorage%uD(width, height or depth < 1)", dims
);
259 if (levels
< 1 || height
< 1 || depth
< 1) {
261 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexStorage%uD(levels < 1)",
268 if (!legal_texobj_target(ctx
, dims
, target
)) {
269 _mesa_error(ctx
, GL_INVALID_ENUM
,
270 "glTexStorage%uD(illegal target=%s)",
271 dims
, _mesa_lookup_enum_by_nr(target
));
275 /* check levels against maximum */
276 if (levels
> _mesa_max_texture_levels(ctx
, target
)) {
278 _mesa_error(ctx
, GL_INVALID_OPERATION
,
279 "glTexStorage%uD(levels too large)", dims
);
284 /* check levels against width/height/depth */
285 maxDim
= MAX3(width
, height
, depth
);
286 if (levels
> _mesa_logbase2(maxDim
) + 1) {
288 _mesa_error(ctx
, GL_INVALID_OPERATION
,
289 "glTexStorage%uD(too many levels for max texture dimension)",
295 /* non-default texture object check */
296 texObj
= _mesa_get_current_tex_object(ctx
, target
);
297 if (!texObj
|| (texObj
->Name
== 0 && !isProxy
)) {
299 _mesa_error(ctx
, GL_INVALID_OPERATION
,
300 "glTexStorage%uD(texture object 0)", dims
);
305 /* Check if texObj->Immutable is set */
306 if (texObj
->Immutable
) {
308 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexStorage%uD(immutable)",
319 * Helper used by _mesa_TexStorage1/2/3D().
322 texstorage(GLuint dims
, GLenum target
, GLsizei levels
, GLenum internalformat
,
323 GLsizei width
, GLsizei height
, GLsizei depth
)
325 struct gl_texture_object
*texObj
;
328 GET_CURRENT_CONTEXT(ctx
);
330 texObj
= _mesa_get_current_tex_object(ctx
, target
);
332 error
= tex_storage_error_check(ctx
, dims
, target
, levels
,
333 internalformat
, width
, height
, depth
);
335 setup_texstorage(ctx
, texObj
, dims
, levels
, internalformat
,
336 width
, height
, depth
);
338 else if (_mesa_is_proxy_texture(target
)) {
339 /* clear all image fields for [levels] */
340 clear_image_fields(ctx
, dims
, texObj
);
346 _mesa_TexStorage1D(GLenum target
, GLsizei levels
, GLenum internalformat
,
349 texstorage(1, target
, levels
, internalformat
, width
, 1, 1);
354 _mesa_TexStorage2D(GLenum target
, GLsizei levels
, GLenum internalformat
,
355 GLsizei width
, GLsizei height
)
357 texstorage(2, target
, levels
, internalformat
, width
, height
, 1);
362 _mesa_TexStorage3D(GLenum target
, GLsizei levels
, GLenum internalformat
,
363 GLsizei width
, GLsizei height
, GLsizei depth
)
365 texstorage(3, target
, levels
, internalformat
, width
, height
, depth
);
371 * Note: we don't support GL_EXT_direct_state_access and the spec says
372 * we don't need the following functions. However, glew checks for the
373 * presence of all six functions and will say that GL_ARB_texture_storage
374 * is not supported if these functions are missing.
379 _mesa_TextureStorage1DEXT(GLuint texture
, GLenum target
, GLsizei levels
,
380 GLenum internalformat
,
388 _mesa_TextureStorage2DEXT(GLuint texture
, GLenum target
, GLsizei levels
,
389 GLenum internalformat
,
390 GLsizei width
, GLsizei height
)
398 _mesa_TextureStorage3DEXT(GLuint texture
, GLenum target
, GLsizei levels
,
399 GLenum internalformat
,
400 GLsizei width
, GLsizei height
, GLsizei depth
)