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
);
90 case GL_TEXTURE_CUBE_MAP_ARRAY
:
91 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
92 return ctx
->Extensions
.ARB_texture_cube_map_array
;
97 _mesa_problem(ctx
, "invalid dims=%u in legal_texobj_target()", dims
);
104 * Compute the size of the next mipmap level.
107 next_mipmap_level_size(GLenum target
,
108 GLint
*width
, GLint
*height
, GLint
*depth
)
114 if ((*height
> 1) && (target
!= GL_TEXTURE_1D_ARRAY
)) {
118 if ((*depth
> 1) && (target
!= GL_TEXTURE_2D_ARRAY
)) {
124 /** Helper to get a particular texture image in a texture object */
125 static struct gl_texture_image
*
126 get_tex_image(struct gl_context
*ctx
,
127 struct gl_texture_object
*texObj
,
128 GLuint face
, GLuint level
)
130 const GLenum faceTarget
=
131 (texObj
->Target
== GL_TEXTURE_CUBE_MAP
||
132 texObj
->Target
== GL_PROXY_TEXTURE_CUBE_MAP
)
133 ? GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ face
: texObj
->Target
;
134 return _mesa_get_tex_image(ctx
, texObj
, faceTarget
, level
);
140 initialize_texture_fields(struct gl_context
*ctx
,
141 struct gl_texture_object
*texObj
,
143 GLsizei width
, GLsizei height
, GLsizei depth
,
144 GLenum internalFormat
, gl_format texFormat
)
146 const GLenum target
= texObj
->Target
;
147 const GLuint numFaces
= _mesa_num_tex_faces(target
);
148 GLint level
, levelWidth
= width
, levelHeight
= height
, levelDepth
= depth
;
151 /* Set up all the texture object's gl_texture_images */
152 for (level
= 0; level
< levels
; level
++) {
153 for (face
= 0; face
< numFaces
; face
++) {
154 struct gl_texture_image
*texImage
=
155 get_tex_image(ctx
, texObj
, face
, level
);
158 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexStorage");
162 _mesa_init_teximage_fields(ctx
, texImage
,
163 levelWidth
, levelHeight
, levelDepth
,
164 0, internalFormat
, texFormat
);
167 next_mipmap_level_size(target
, &levelWidth
, &levelHeight
, &levelDepth
);
174 * Clear all fields of texture object to zeros. Used for proxy texture tests.
175 * Used for proxy texture tests (and to clean up when a texture memory
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
< Elements(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_init_teximage_fields(ctx
, texImage
,
198 0, 0, 0, 0, /* w, h, d, border */
199 GL_NONE
, MESA_FORMAT_NONE
);
206 * Do error checking for calls to glTexStorage1/2/3D().
207 * If an error is found, record it with _mesa_error(), unless the target
208 * is a proxy texture.
209 * \return GL_TRUE if any error, GL_FALSE otherwise.
212 tex_storage_error_check(struct gl_context
*ctx
, GLuint dims
, GLenum target
,
213 GLsizei levels
, GLenum internalformat
,
214 GLsizei width
, GLsizei height
, GLsizei depth
)
216 struct gl_texture_object
*texObj
;
218 GLboolean legalFormat
;
220 /* check internal format - note that only sized formats are allowed */
221 switch (internalformat
) {
224 case GL_LUMINANCE_ALPHA
:
231 case GL_DEPTH_COMPONENT
:
232 case GL_DEPTH_STENCIL
:
233 case GL_COMPRESSED_ALPHA
:
234 case GL_COMPRESSED_LUMINANCE_ALPHA
:
235 case GL_COMPRESSED_LUMINANCE
:
236 case GL_COMPRESSED_INTENSITY
:
237 case GL_COMPRESSED_RGB
:
238 case GL_COMPRESSED_RGBA
:
239 case GL_COMPRESSED_SRGB
:
240 case GL_COMPRESSED_SRGB_ALPHA
:
241 case GL_COMPRESSED_SLUMINANCE
:
242 case GL_COMPRESSED_SLUMINANCE_ALPHA
:
244 case GL_GREEN_INTEGER
:
245 case GL_BLUE_INTEGER
:
246 case GL_ALPHA_INTEGER
:
248 case GL_RGBA_INTEGER
:
250 case GL_BGRA_INTEGER
:
251 case GL_LUMINANCE_INTEGER_EXT
:
252 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
253 /* these unsized formats are illegal */
254 legalFormat
= GL_FALSE
;
257 legalFormat
= _mesa_base_tex_format(ctx
, internalformat
) > 0;
261 _mesa_error(ctx
, GL_INVALID_ENUM
,
262 "glTexStorage%uD(internalformat = %s)", dims
,
263 _mesa_lookup_enum_by_nr(internalformat
));
268 if (width
< 1 || height
< 1 || depth
< 1) {
269 _mesa_error(ctx
, GL_INVALID_VALUE
,
270 "glTexStorage%uD(width, height or depth < 1)", dims
);
275 if (!legal_texobj_target(ctx
, dims
, target
)) {
276 _mesa_error(ctx
, GL_INVALID_ENUM
,
277 "glTexStorage%uD(illegal target=%s)",
278 dims
, _mesa_lookup_enum_by_nr(target
));
284 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexStorage%uD(levels < 1)",
289 /* check levels against maximum (note different error than above) */
290 if (levels
> (GLint
) _mesa_max_texture_levels(ctx
, target
)) {
291 _mesa_error(ctx
, GL_INVALID_OPERATION
,
292 "glTexStorage%uD(levels too large)", dims
);
296 /* check levels against width/height/depth */
297 maxDim
= MAX3(width
, height
, depth
);
298 if (levels
> (GLint
) _mesa_logbase2(maxDim
) + 1) {
299 _mesa_error(ctx
, GL_INVALID_OPERATION
,
300 "glTexStorage%uD(too many levels for max texture dimension)",
305 /* non-default texture object check */
306 texObj
= _mesa_get_current_tex_object(ctx
, target
);
307 if (!texObj
|| (texObj
->Name
== 0)) {
308 _mesa_error(ctx
, GL_INVALID_OPERATION
,
309 "glTexStorage%uD(texture object 0)", dims
);
313 /* Check if texObj->Immutable is set */
314 if (texObj
->Immutable
) {
315 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexStorage%uD(immutable)",
325 * Helper used by _mesa_TexStorage1/2/3D().
328 texstorage(GLuint dims
, GLenum target
, GLsizei levels
, GLenum internalformat
,
329 GLsizei width
, GLsizei height
, GLsizei depth
)
331 struct gl_texture_object
*texObj
;
332 GLboolean sizeOK
, dimensionsOK
;
335 GET_CURRENT_CONTEXT(ctx
);
337 if (tex_storage_error_check(ctx
, dims
, target
, levels
,
338 internalformat
, width
, height
, depth
)) {
339 return; /* error was recorded */
342 texObj
= _mesa_get_current_tex_object(ctx
, target
);
345 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, 0,
346 internalformat
, GL_NONE
, GL_NONE
);
347 assert(texFormat
!= MESA_FORMAT_NONE
);
349 /* check that width, height, depth are legal for the mipmap level */
350 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, 0,
351 width
, height
, depth
, 0);
353 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, target
, 0, texFormat
,
354 width
, height
, depth
, 0);
356 if (_mesa_is_proxy_texture(texObj
->Target
)) {
357 if (dimensionsOK
&& sizeOK
) {
358 initialize_texture_fields(ctx
, texObj
, levels
, width
, height
, depth
,
359 internalformat
, texFormat
);
362 /* clear all image fields for [levels] */
363 clear_texture_fields(ctx
, texObj
);
368 _mesa_error(ctx
, GL_INVALID_VALUE
,
369 "glTexStorage%uD(invalid width, height or depth)", dims
);
374 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
375 "glTexStorage%uD(texture too large)", dims
);
383 if (!initialize_texture_fields(ctx
, texObj
, levels
, width
, height
, depth
,
384 internalformat
, texFormat
)) {
388 /* Do actual texture memory allocation */
389 if (!ctx
->Driver
.AllocTextureStorage(ctx
, texObj
, levels
,
390 width
, height
, depth
)) {
391 /* Reset the texture images' info to zeros.
392 * Strictly speaking, we probably don't have to do this since
393 * generating GL_OUT_OF_MEMORY can leave things in an undefined
394 * state but this puts things in a consistent state.
396 clear_texture_fields(ctx
, texObj
);
397 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexStorage%uD", dims
);
401 texObj
->Immutable
= GL_TRUE
;
407 _mesa_TexStorage1D(GLenum target
, GLsizei levels
, GLenum internalformat
,
410 texstorage(1, target
, levels
, internalformat
, width
, 1, 1);
415 _mesa_TexStorage2D(GLenum target
, GLsizei levels
, GLenum internalformat
,
416 GLsizei width
, GLsizei height
)
418 texstorage(2, target
, levels
, internalformat
, width
, height
, 1);
423 _mesa_TexStorage3D(GLenum target
, GLsizei levels
, GLenum internalformat
,
424 GLsizei width
, GLsizei height
, GLsizei depth
)
426 texstorage(3, target
, levels
, internalformat
, width
, height
, depth
);
432 * Note: we don't support GL_EXT_direct_state_access and the spec says
433 * we don't need the following functions. However, glew checks for the
434 * presence of all six functions and will say that GL_ARB_texture_storage
435 * is not supported if these functions are missing.
440 _mesa_TextureStorage1DEXT(GLuint texture
, GLenum target
, GLsizei levels
,
441 GLenum internalformat
,
449 _mesa_TextureStorage2DEXT(GLuint texture
, GLenum target
, GLsizei levels
,
450 GLenum internalformat
,
451 GLsizei width
, GLsizei height
)
459 _mesa_TextureStorage3DEXT(GLuint texture
, GLenum target
, GLsizei levels
,
460 GLenum internalformat
,
461 GLsizei width
, GLsizei height
, GLsizei depth
)