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"
48 * Check if the given texture target is a legal texture object target
49 * for a glTexStorage() command.
50 * This is a bit different than legal_teximage_target() when it comes
54 legal_texobj_target(struct gl_context
*ctx
, GLuint dims
, GLenum target
)
60 case GL_PROXY_TEXTURE_1D
:
68 case GL_PROXY_TEXTURE_2D
:
70 case GL_TEXTURE_CUBE_MAP
:
71 case GL_PROXY_TEXTURE_CUBE_MAP
:
72 return ctx
->Extensions
.ARB_texture_cube_map
;
73 case GL_TEXTURE_RECTANGLE
:
74 case GL_PROXY_TEXTURE_RECTANGLE
:
75 return ctx
->Extensions
.NV_texture_rectangle
;
76 case GL_TEXTURE_1D_ARRAY
:
77 case GL_PROXY_TEXTURE_1D_ARRAY
:
78 return ctx
->Extensions
.EXT_texture_array
;
85 case GL_PROXY_TEXTURE_3D
:
87 case GL_TEXTURE_2D_ARRAY
:
88 case GL_PROXY_TEXTURE_2D_ARRAY
:
89 return 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
);
103 /** Helper to get a particular texture image in a texture object */
104 static struct gl_texture_image
*
105 get_tex_image(struct gl_context
*ctx
,
106 struct gl_texture_object
*texObj
,
107 GLuint face
, GLuint level
)
109 const GLenum faceTarget
=
110 (texObj
->Target
== GL_TEXTURE_CUBE_MAP
||
111 texObj
->Target
== GL_PROXY_TEXTURE_CUBE_MAP
)
112 ? GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ face
: texObj
->Target
;
113 return _mesa_get_tex_image(ctx
, texObj
, faceTarget
, level
);
119 initialize_texture_fields(struct gl_context
*ctx
,
120 struct gl_texture_object
*texObj
,
122 GLsizei width
, GLsizei height
, GLsizei depth
,
123 GLenum internalFormat
, mesa_format texFormat
)
125 const GLenum target
= texObj
->Target
;
126 const GLuint numFaces
= _mesa_num_tex_faces(target
);
127 GLint level
, levelWidth
= width
, levelHeight
= height
, levelDepth
= depth
;
130 /* Set up all the texture object's gl_texture_images */
131 for (level
= 0; level
< levels
; level
++) {
132 for (face
= 0; face
< numFaces
; face
++) {
133 struct gl_texture_image
*texImage
=
134 get_tex_image(ctx
, texObj
, face
, level
);
137 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexStorage");
141 _mesa_init_teximage_fields(ctx
, texImage
,
142 levelWidth
, levelHeight
, levelDepth
,
143 0, internalFormat
, texFormat
);
146 _mesa_next_mipmap_level_size(target
, 0, levelWidth
, levelHeight
, levelDepth
,
147 &levelWidth
, &levelHeight
, &levelDepth
);
154 * Clear all fields of texture object to zeros. Used for proxy texture tests
155 * and to clean up when a texture memory allocation fails.
158 clear_texture_fields(struct gl_context
*ctx
,
159 struct gl_texture_object
*texObj
)
161 const GLenum target
= texObj
->Target
;
162 const GLuint numFaces
= _mesa_num_tex_faces(target
);
166 for (level
= 0; level
< Elements(texObj
->Image
[0]); level
++) {
167 for (face
= 0; face
< numFaces
; face
++) {
168 struct gl_texture_image
*texImage
=
169 get_tex_image(ctx
, texObj
, face
, level
);
172 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexStorage");
176 _mesa_init_teximage_fields(ctx
, texImage
,
177 0, 0, 0, 0, /* w, h, d, border */
178 GL_NONE
, MESA_FORMAT_NONE
);
185 _mesa_is_legal_tex_storage_format(struct gl_context
*ctx
, GLenum internalformat
)
187 /* check internal format - note that only sized formats are allowed */
188 switch (internalformat
) {
191 case GL_LUMINANCE_ALPHA
:
198 case GL_DEPTH_COMPONENT
:
199 case GL_DEPTH_STENCIL
:
200 case GL_COMPRESSED_ALPHA
:
201 case GL_COMPRESSED_LUMINANCE_ALPHA
:
202 case GL_COMPRESSED_LUMINANCE
:
203 case GL_COMPRESSED_INTENSITY
:
204 case GL_COMPRESSED_RGB
:
205 case GL_COMPRESSED_RGBA
:
206 case GL_COMPRESSED_SRGB
:
207 case GL_COMPRESSED_SRGB_ALPHA
:
208 case GL_COMPRESSED_SLUMINANCE
:
209 case GL_COMPRESSED_SLUMINANCE_ALPHA
:
211 case GL_GREEN_INTEGER
:
212 case GL_BLUE_INTEGER
:
213 case GL_ALPHA_INTEGER
:
215 case GL_RGBA_INTEGER
:
217 case GL_BGRA_INTEGER
:
218 case GL_LUMINANCE_INTEGER_EXT
:
219 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
220 /* these unsized formats are illegal */
223 return _mesa_base_tex_format(ctx
, internalformat
) > 0;
228 * Default ctx->Driver.AllocTextureStorage() handler.
230 * The driver can override this with a more specific implementation if it
231 * desires, but this can be used to get the texture images allocated using the
232 * usual texture image handling code. The immutability of
233 * GL_ARB_texture_storage texture layouts is handled by texObj->Immutable
234 * checks at glTexImage* time.
237 _mesa_alloc_texture_storage(struct gl_context
*ctx
,
238 struct gl_texture_object
*texObj
,
239 GLsizei levels
, GLsizei width
,
240 GLsizei height
, GLsizei depth
)
242 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
250 for (face
= 0; face
< numFaces
; face
++) {
251 for (level
= 0; level
< levels
; level
++) {
252 struct gl_texture_image
*const texImage
= texObj
->Image
[face
][level
];
253 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
))
263 * Do error checking for calls to glTexStorage1/2/3D().
264 * If an error is found, record it with _mesa_error(), unless the target
265 * is a proxy texture.
266 * \return GL_TRUE if any error, GL_FALSE otherwise.
269 tex_storage_error_check(struct gl_context
*ctx
, GLuint dims
, GLenum target
,
270 GLsizei levels
, GLenum internalformat
,
271 GLsizei width
, GLsizei height
, GLsizei depth
)
273 struct gl_texture_object
*texObj
;
275 if (!_mesa_is_legal_tex_storage_format(ctx
, internalformat
)) {
276 _mesa_error(ctx
, GL_INVALID_ENUM
,
277 "glTexStorage%uD(internalformat = %s)", dims
,
278 _mesa_lookup_enum_by_nr(internalformat
));
283 if (width
< 1 || height
< 1 || depth
< 1) {
284 _mesa_error(ctx
, GL_INVALID_VALUE
,
285 "glTexStorage%uD(width, height or depth < 1)", dims
);
290 if (!legal_texobj_target(ctx
, dims
, target
)) {
291 _mesa_error(ctx
, GL_INVALID_ENUM
,
292 "glTexStorage%uD(illegal target=%s)",
293 dims
, _mesa_lookup_enum_by_nr(target
));
299 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTexStorage%uD(levels < 1)",
304 /* check levels against maximum (note different error than above) */
305 if (levels
> (GLint
) _mesa_max_texture_levels(ctx
, target
)) {
306 _mesa_error(ctx
, GL_INVALID_OPERATION
,
307 "glTexStorage%uD(levels too large)", dims
);
311 /* check levels against width/height/depth */
312 if (levels
> _mesa_get_tex_max_num_levels(target
, width
, height
, depth
)) {
313 _mesa_error(ctx
, GL_INVALID_OPERATION
,
314 "glTexStorage%uD(too many levels for max texture dimension)",
319 /* non-default texture object check */
320 texObj
= _mesa_get_current_tex_object(ctx
, target
);
321 if (!_mesa_is_proxy_texture(target
) && (!texObj
|| (texObj
->Name
== 0))) {
322 _mesa_error(ctx
, GL_INVALID_OPERATION
,
323 "glTexStorage%uD(texture object 0)", dims
);
327 /* Check if texObj->Immutable is set */
328 if (!_mesa_is_proxy_texture(target
) && texObj
->Immutable
) {
329 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTexStorage%uD(immutable)",
334 /* additional checks for depth textures */
335 if (!_mesa_legal_texture_base_format_for_target(ctx
, target
, internalformat
,
336 dims
, "glTexStorage"))
344 * Helper used by _mesa_TexStorage1/2/3D().
347 texstorage(GLuint dims
, GLenum target
, GLsizei levels
, GLenum internalformat
,
348 GLsizei width
, GLsizei height
, GLsizei depth
)
350 struct gl_texture_object
*texObj
;
351 GLboolean sizeOK
, dimensionsOK
;
352 mesa_format texFormat
;
354 GET_CURRENT_CONTEXT(ctx
);
356 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
357 _mesa_debug(ctx
, "glTexStorage%uD %s %d %s %d %d %d\n",
359 _mesa_lookup_enum_by_nr(target
), levels
,
360 _mesa_lookup_enum_by_nr(internalformat
),
361 width
, height
, depth
);
363 if (tex_storage_error_check(ctx
, dims
, target
, levels
,
364 internalformat
, width
, height
, depth
)) {
365 return; /* error was recorded */
368 texObj
= _mesa_get_current_tex_object(ctx
, target
);
371 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, 0,
372 internalformat
, GL_NONE
, GL_NONE
);
373 assert(texFormat
!= MESA_FORMAT_NONE
);
375 /* check that width, height, depth are legal for the mipmap level */
376 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, 0,
377 width
, height
, depth
, 0);
379 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, target
, 0, texFormat
,
380 width
, height
, depth
, 0);
382 if (_mesa_is_proxy_texture(texObj
->Target
)) {
383 if (dimensionsOK
&& sizeOK
) {
384 initialize_texture_fields(ctx
, texObj
, levels
, width
, height
, depth
,
385 internalformat
, texFormat
);
388 /* clear all image fields for [levels] */
389 clear_texture_fields(ctx
, texObj
);
394 _mesa_error(ctx
, GL_INVALID_VALUE
,
395 "glTexStorage%uD(invalid width, height or depth)", dims
);
400 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
401 "glTexStorage%uD(texture too large)", dims
);
409 if (!initialize_texture_fields(ctx
, texObj
, levels
, width
, height
, depth
,
410 internalformat
, texFormat
)) {
414 /* Do actual texture memory allocation */
415 if (!ctx
->Driver
.AllocTextureStorage(ctx
, texObj
, levels
,
416 width
, height
, depth
)) {
417 /* Reset the texture images' info to zeros.
418 * Strictly speaking, we probably don't have to do this since
419 * generating GL_OUT_OF_MEMORY can leave things in an undefined
420 * state but this puts things in a consistent state.
422 clear_texture_fields(ctx
, texObj
);
423 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexStorage%uD", dims
);
427 _mesa_set_texture_view_state(ctx
, texObj
, target
, levels
);
434 _mesa_TexStorage1D(GLenum target
, GLsizei levels
, GLenum internalformat
,
437 texstorage(1, target
, levels
, internalformat
, width
, 1, 1);
442 _mesa_TexStorage2D(GLenum target
, GLsizei levels
, GLenum internalformat
,
443 GLsizei width
, GLsizei height
)
445 texstorage(2, target
, levels
, internalformat
, width
, height
, 1);
450 _mesa_TexStorage3D(GLenum target
, GLsizei levels
, GLenum internalformat
,
451 GLsizei width
, GLsizei height
, GLsizei depth
)
453 texstorage(3, target
, levels
, internalformat
, width
, height
, depth
);
459 * Note: we don't support GL_EXT_direct_state_access and the spec says
460 * we don't need the following functions. However, glew checks for the
461 * presence of all six functions and will say that GL_ARB_texture_storage
462 * is not supported if these functions are missing.
467 _mesa_TextureStorage1DEXT(GLuint texture
, GLenum target
, GLsizei levels
,
468 GLenum internalformat
,
471 GET_CURRENT_CONTEXT(ctx
);
476 (void) internalformat
;
479 _mesa_error(ctx
, GL_INVALID_OPERATION
,
480 "glTextureStorage1DEXT not supported");
485 _mesa_TextureStorage2DEXT(GLuint texture
, GLenum target
, GLsizei levels
,
486 GLenum internalformat
,
487 GLsizei width
, GLsizei height
)
489 GET_CURRENT_CONTEXT(ctx
);
494 (void) internalformat
;
498 _mesa_error(ctx
, GL_INVALID_OPERATION
,
499 "glTextureStorage2DEXT not supported");
505 _mesa_TextureStorage3DEXT(GLuint texture
, GLenum target
, GLsizei levels
,
506 GLenum internalformat
,
507 GLsizei width
, GLsizei height
, GLsizei depth
)
509 GET_CURRENT_CONTEXT(ctx
);
514 (void) internalformat
;
519 _mesa_error(ctx
, GL_INVALID_OPERATION
,
520 "glTextureStorage3DEXT not supported");