mesa: Validate internalFormat with target in glTexStorage paths
[mesa.git] / src / mesa / main / texstorage.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2011 VMware, Inc. All Rights Reserved.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
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.
23 */
24
25
26 /**
27 * \file texstorage.c
28 * GL_ARB_texture_storage functions
29 */
30
31
32
33 #include "glheader.h"
34 #include "context.h"
35 #include "enums.h"
36 #include "imports.h"
37 #include "macros.h"
38 #include "teximage.h"
39 #include "texobj.h"
40 #include "mipmap.h"
41 #include "texstorage.h"
42 #include "textureview.h"
43 #include "mtypes.h"
44
45
46
47 /**
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
51 * to cube maps.
52 */
53 static GLboolean
54 legal_texobj_target(struct gl_context *ctx, GLuint dims, GLenum target)
55 {
56 switch (dims) {
57 case 1:
58 switch (target) {
59 case GL_TEXTURE_1D:
60 case GL_PROXY_TEXTURE_1D:
61 return GL_TRUE;
62 default:
63 return GL_FALSE;
64 }
65 case 2:
66 switch (target) {
67 case GL_TEXTURE_2D:
68 case GL_PROXY_TEXTURE_2D:
69 return GL_TRUE;
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;
79 default:
80 return GL_FALSE;
81 }
82 case 3:
83 switch (target) {
84 case GL_TEXTURE_3D:
85 case GL_PROXY_TEXTURE_3D:
86 return GL_TRUE;
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;
93 default:
94 return GL_FALSE;
95 }
96 default:
97 _mesa_problem(ctx, "invalid dims=%u in legal_texobj_target()", dims);
98 return GL_FALSE;
99 }
100 }
101
102
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)
108 {
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);
114 }
115
116
117
118 static GLboolean
119 initialize_texture_fields(struct gl_context *ctx,
120 struct gl_texture_object *texObj,
121 GLint levels,
122 GLsizei width, GLsizei height, GLsizei depth,
123 GLenum internalFormat, gl_format texFormat)
124 {
125 const GLenum target = texObj->Target;
126 const GLuint numFaces = _mesa_num_tex_faces(target);
127 GLint level, levelWidth = width, levelHeight = height, levelDepth = depth;
128 GLuint face;
129
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);
135
136 if (!texImage) {
137 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexStorage");
138 return GL_FALSE;
139 }
140
141 _mesa_init_teximage_fields(ctx, texImage,
142 levelWidth, levelHeight, levelDepth,
143 0, internalFormat, texFormat);
144 }
145
146 _mesa_next_mipmap_level_size(target, 0, levelWidth, levelHeight, levelDepth,
147 &levelWidth, &levelHeight, &levelDepth);
148 }
149 return GL_TRUE;
150 }
151
152
153 /**
154 * Clear all fields of texture object to zeros. Used for proxy texture tests.
155 * Used for proxy texture tests (and to clean up when a texture memory
156 * allocation fails).
157 */
158 static void
159 clear_texture_fields(struct gl_context *ctx,
160 struct gl_texture_object *texObj)
161 {
162 const GLenum target = texObj->Target;
163 const GLuint numFaces = _mesa_num_tex_faces(target);
164 GLint level;
165 GLuint face;
166
167 for (level = 0; level < Elements(texObj->Image[0]); level++) {
168 for (face = 0; face < numFaces; face++) {
169 struct gl_texture_image *texImage =
170 get_tex_image(ctx, texObj, face, level);
171
172 if (!texImage) {
173 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexStorage");
174 return;
175 }
176
177 _mesa_init_teximage_fields(ctx, texImage,
178 0, 0, 0, 0, /* w, h, d, border */
179 GL_NONE, MESA_FORMAT_NONE);
180 }
181 }
182 }
183
184
185 GLboolean
186 _mesa_is_legal_tex_storage_format(struct gl_context *ctx, GLenum internalformat)
187 {
188 /* check internal format - note that only sized formats are allowed */
189 switch (internalformat) {
190 case GL_ALPHA:
191 case GL_LUMINANCE:
192 case GL_LUMINANCE_ALPHA:
193 case GL_INTENSITY:
194 case GL_RED:
195 case GL_RG:
196 case GL_RGB:
197 case GL_RGBA:
198 case GL_BGRA:
199 case GL_DEPTH_COMPONENT:
200 case GL_DEPTH_STENCIL:
201 case GL_COMPRESSED_ALPHA:
202 case GL_COMPRESSED_LUMINANCE_ALPHA:
203 case GL_COMPRESSED_LUMINANCE:
204 case GL_COMPRESSED_INTENSITY:
205 case GL_COMPRESSED_RGB:
206 case GL_COMPRESSED_RGBA:
207 case GL_COMPRESSED_SRGB:
208 case GL_COMPRESSED_SRGB_ALPHA:
209 case GL_COMPRESSED_SLUMINANCE:
210 case GL_COMPRESSED_SLUMINANCE_ALPHA:
211 case GL_RED_INTEGER:
212 case GL_GREEN_INTEGER:
213 case GL_BLUE_INTEGER:
214 case GL_ALPHA_INTEGER:
215 case GL_RGB_INTEGER:
216 case GL_RGBA_INTEGER:
217 case GL_BGR_INTEGER:
218 case GL_BGRA_INTEGER:
219 case GL_LUMINANCE_INTEGER_EXT:
220 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
221 /* these unsized formats are illegal */
222 return GL_FALSE;
223 default:
224 return _mesa_base_tex_format(ctx, internalformat) > 0;
225 }
226 }
227
228 /**
229 * Default ctx->Driver.AllocTextureStorage() handler.
230 *
231 * The driver can override this with a more specific implementation if it
232 * desires, but this can be used to get the texture images allocated using the
233 * usual texture image handling code. The immutability of
234 * GL_ARB_texture_storage texture layouts is handled by texObj->Immutable
235 * checks at glTexImage* time.
236 */
237 GLboolean
238 _mesa_alloc_texture_storage(struct gl_context *ctx,
239 struct gl_texture_object *texObj,
240 GLsizei levels, GLsizei width,
241 GLsizei height, GLsizei depth)
242 {
243 const int numFaces = _mesa_num_tex_faces(texObj->Target);
244 int face;
245 int level;
246
247 (void) width;
248 (void) height;
249 (void) depth;
250
251 for (face = 0; face < numFaces; face++) {
252 for (level = 0; level < levels; level++) {
253 struct gl_texture_image *const texImage = texObj->Image[face][level];
254 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage))
255 return GL_FALSE;
256 }
257 }
258
259 return GL_TRUE;
260 }
261
262
263 /**
264 * Do error checking for calls to glTexStorage1/2/3D().
265 * If an error is found, record it with _mesa_error(), unless the target
266 * is a proxy texture.
267 * \return GL_TRUE if any error, GL_FALSE otherwise.
268 */
269 static GLboolean
270 tex_storage_error_check(struct gl_context *ctx, GLuint dims, GLenum target,
271 GLsizei levels, GLenum internalformat,
272 GLsizei width, GLsizei height, GLsizei depth)
273 {
274 struct gl_texture_object *texObj;
275
276 if (!_mesa_is_legal_tex_storage_format(ctx, internalformat)) {
277 _mesa_error(ctx, GL_INVALID_ENUM,
278 "glTexStorage%uD(internalformat = %s)", dims,
279 _mesa_lookup_enum_by_nr(internalformat));
280 return GL_TRUE;
281 }
282
283 /* size check */
284 if (width < 1 || height < 1 || depth < 1) {
285 _mesa_error(ctx, GL_INVALID_VALUE,
286 "glTexStorage%uD(width, height or depth < 1)", dims);
287 return GL_TRUE;
288 }
289
290 /* target check */
291 if (!legal_texobj_target(ctx, dims, target)) {
292 _mesa_error(ctx, GL_INVALID_ENUM,
293 "glTexStorage%uD(illegal target=%s)",
294 dims, _mesa_lookup_enum_by_nr(target));
295 return GL_TRUE;
296 }
297
298 /* levels check */
299 if (levels < 1) {
300 _mesa_error(ctx, GL_INVALID_VALUE, "glTexStorage%uD(levels < 1)",
301 dims);
302 return GL_TRUE;
303 }
304
305 /* check levels against maximum (note different error than above) */
306 if (levels > (GLint) _mesa_max_texture_levels(ctx, target)) {
307 _mesa_error(ctx, GL_INVALID_OPERATION,
308 "glTexStorage%uD(levels too large)", dims);
309 return GL_TRUE;
310 }
311
312 /* check levels against width/height/depth */
313 if (levels > _mesa_get_tex_max_num_levels(target, width, height, depth)) {
314 _mesa_error(ctx, GL_INVALID_OPERATION,
315 "glTexStorage%uD(too many levels for max texture dimension)",
316 dims);
317 return GL_TRUE;
318 }
319
320 /* non-default texture object check */
321 texObj = _mesa_get_current_tex_object(ctx, target);
322 if (!_mesa_is_proxy_texture(target) && (!texObj || (texObj->Name == 0))) {
323 _mesa_error(ctx, GL_INVALID_OPERATION,
324 "glTexStorage%uD(texture object 0)", dims);
325 return GL_TRUE;
326 }
327
328 /* Check if texObj->Immutable is set */
329 if (!_mesa_is_proxy_texture(target) && texObj->Immutable) {
330 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexStorage%uD(immutable)",
331 dims);
332 return GL_TRUE;
333 }
334
335 /* additional checks for depth textures */
336 if (!_mesa_legal_texture_base_format_for_target(ctx, target, internalformat,
337 dims, "glTexStorage"))
338 return GL_TRUE;
339
340 return GL_FALSE;
341 }
342
343
344 /**
345 * Helper used by _mesa_TexStorage1/2/3D().
346 */
347 static void
348 texstorage(GLuint dims, GLenum target, GLsizei levels, GLenum internalformat,
349 GLsizei width, GLsizei height, GLsizei depth)
350 {
351 struct gl_texture_object *texObj;
352 GLboolean sizeOK, dimensionsOK;
353 gl_format texFormat;
354
355 GET_CURRENT_CONTEXT(ctx);
356
357 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
358 _mesa_debug(ctx, "glTexStorage%uD %s %d %s %d %d %d\n",
359 dims,
360 _mesa_lookup_enum_by_nr(target), levels,
361 _mesa_lookup_enum_by_nr(internalformat),
362 width, height, depth);
363
364 if (tex_storage_error_check(ctx, dims, target, levels,
365 internalformat, width, height, depth)) {
366 return; /* error was recorded */
367 }
368
369 texObj = _mesa_get_current_tex_object(ctx, target);
370 assert(texObj);
371
372 texFormat = _mesa_choose_texture_format(ctx, texObj, target, 0,
373 internalformat, GL_NONE, GL_NONE);
374 assert(texFormat != MESA_FORMAT_NONE);
375
376 /* check that width, height, depth are legal for the mipmap level */
377 dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, 0,
378 width, height, depth, 0);
379
380 sizeOK = ctx->Driver.TestProxyTexImage(ctx, target, 0, texFormat,
381 width, height, depth, 0);
382
383 if (_mesa_is_proxy_texture(texObj->Target)) {
384 if (dimensionsOK && sizeOK) {
385 initialize_texture_fields(ctx, texObj, levels, width, height, depth,
386 internalformat, texFormat);
387 }
388 else {
389 /* clear all image fields for [levels] */
390 clear_texture_fields(ctx, texObj);
391 }
392 }
393 else {
394 if (!dimensionsOK) {
395 _mesa_error(ctx, GL_INVALID_VALUE,
396 "glTexStorage%uD(invalid width, height or depth)", dims);
397 return;
398 }
399
400 if (!sizeOK) {
401 _mesa_error(ctx, GL_OUT_OF_MEMORY,
402 "glTexStorage%uD(texture too large)", dims);
403 }
404
405 assert(levels > 0);
406 assert(width > 0);
407 assert(height > 0);
408 assert(depth > 0);
409
410 if (!initialize_texture_fields(ctx, texObj, levels, width, height, depth,
411 internalformat, texFormat)) {
412 return;
413 }
414
415 /* Do actual texture memory allocation */
416 if (!ctx->Driver.AllocTextureStorage(ctx, texObj, levels,
417 width, height, depth)) {
418 /* Reset the texture images' info to zeros.
419 * Strictly speaking, we probably don't have to do this since
420 * generating GL_OUT_OF_MEMORY can leave things in an undefined
421 * state but this puts things in a consistent state.
422 */
423 clear_texture_fields(ctx, texObj);
424 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexStorage%uD", dims);
425 return;
426 }
427
428 _mesa_set_texture_view_state(ctx, texObj, target, levels);
429
430 }
431 }
432
433
434 void GLAPIENTRY
435 _mesa_TexStorage1D(GLenum target, GLsizei levels, GLenum internalformat,
436 GLsizei width)
437 {
438 texstorage(1, target, levels, internalformat, width, 1, 1);
439 }
440
441
442 void GLAPIENTRY
443 _mesa_TexStorage2D(GLenum target, GLsizei levels, GLenum internalformat,
444 GLsizei width, GLsizei height)
445 {
446 texstorage(2, target, levels, internalformat, width, height, 1);
447 }
448
449
450 void GLAPIENTRY
451 _mesa_TexStorage3D(GLenum target, GLsizei levels, GLenum internalformat,
452 GLsizei width, GLsizei height, GLsizei depth)
453 {
454 texstorage(3, target, levels, internalformat, width, height, depth);
455 }
456
457
458
459 /*
460 * Note: we don't support GL_EXT_direct_state_access and the spec says
461 * we don't need the following functions. However, glew checks for the
462 * presence of all six functions and will say that GL_ARB_texture_storage
463 * is not supported if these functions are missing.
464 */
465
466
467 void GLAPIENTRY
468 _mesa_TextureStorage1DEXT(GLuint texture, GLenum target, GLsizei levels,
469 GLenum internalformat,
470 GLsizei width)
471 {
472 GET_CURRENT_CONTEXT(ctx);
473
474 (void) texture;
475 (void) target;
476 (void) levels;
477 (void) internalformat;
478 (void) width;
479
480 _mesa_error(ctx, GL_INVALID_OPERATION,
481 "glTextureStorage1DEXT not supported");
482 }
483
484
485 void GLAPIENTRY
486 _mesa_TextureStorage2DEXT(GLuint texture, GLenum target, GLsizei levels,
487 GLenum internalformat,
488 GLsizei width, GLsizei height)
489 {
490 GET_CURRENT_CONTEXT(ctx);
491
492 (void) texture;
493 (void) target;
494 (void) levels;
495 (void) internalformat;
496 (void) width;
497 (void) height;
498
499 _mesa_error(ctx, GL_INVALID_OPERATION,
500 "glTextureStorage2DEXT not supported");
501 }
502
503
504
505 void GLAPIENTRY
506 _mesa_TextureStorage3DEXT(GLuint texture, GLenum target, GLsizei levels,
507 GLenum internalformat,
508 GLsizei width, GLsizei height, GLsizei depth)
509 {
510 GET_CURRENT_CONTEXT(ctx);
511
512 (void) texture;
513 (void) target;
514 (void) levels;
515 (void) internalformat;
516 (void) width;
517 (void) height;
518 (void) depth;
519
520 _mesa_error(ctx, GL_INVALID_OPERATION,
521 "glTextureStorage3DEXT not supported");
522 }