mesa: rework texture size error checking
[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 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.
22 */
23
24
25 /**
26 * \file texstorage.c
27 * GL_ARB_texture_storage functions
28 */
29
30
31
32 #include "glheader.h"
33 #include "context.h"
34 #include "enums.h"
35 #include "imports.h"
36 #include "macros.h"
37 #include "mfeatures.h"
38 #include "teximage.h"
39 #include "texobj.h"
40 #include "texstorage.h"
41 #include "mtypes.h"
42
43
44
45 /**
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
49 * to cube maps.
50 */
51 static GLboolean
52 legal_texobj_target(struct gl_context *ctx, GLuint dims, GLenum target)
53 {
54 switch (dims) {
55 case 1:
56 switch (target) {
57 case GL_TEXTURE_1D:
58 case GL_PROXY_TEXTURE_1D:
59 return GL_TRUE;
60 default:
61 return GL_FALSE;
62 }
63 case 2:
64 switch (target) {
65 case GL_TEXTURE_2D:
66 case GL_PROXY_TEXTURE_2D:
67 return GL_TRUE;
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);
78 default:
79 return GL_FALSE;
80 }
81 case 3:
82 switch (target) {
83 case GL_TEXTURE_3D:
84 case GL_PROXY_TEXTURE_3D:
85 return GL_TRUE;
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 default:
91 return GL_FALSE;
92 }
93 default:
94 _mesa_problem(ctx, "invalid dims=%u in legal_texobj_target()", dims);
95 return GL_FALSE;
96 }
97 }
98
99
100 /**
101 * Compute the size of the next mipmap level.
102 */
103 static void
104 next_mipmap_level_size(GLenum target,
105 GLint *width, GLint *height, GLint *depth)
106 {
107 if (*width > 1) {
108 *width /= 2;
109 }
110
111 if ((*height > 1) && (target != GL_TEXTURE_1D_ARRAY)) {
112 *height /= 2;
113 }
114
115 if ((*depth > 1) && (target != GL_TEXTURE_2D_ARRAY)) {
116 *depth /= 2;
117 }
118 }
119
120
121 /**
122 * Do actual memory allocation for glTexStorage1/2/3D().
123 */
124 static void
125 setup_texstorage(struct gl_context *ctx,
126 struct gl_texture_object *texObj,
127 GLuint dims,
128 gl_format texFormat,
129 GLsizei levels, GLenum internalFormat,
130 GLsizei width, GLsizei height, GLsizei depth)
131 {
132 const GLenum target = texObj->Target;
133 const GLuint numFaces = _mesa_num_tex_faces(target);
134 GLint level, levelWidth = width, levelHeight = height, levelDepth = depth;
135 GLuint face;
136
137 assert(levels > 0);
138 assert(width > 0);
139 assert(height > 0);
140 assert(depth > 0);
141
142 /* Set up all the texture object's gl_texture_images */
143 for (level = 0; level < levels; level++) {
144 for (face = 0; face < numFaces; face++) {
145 const GLenum faceTarget =
146 (target == GL_TEXTURE_CUBE_MAP)
147 ? GL_TEXTURE_CUBE_MAP_POSITIVE_X + face : target;
148 struct gl_texture_image *texImage =
149 _mesa_get_tex_image(ctx, texObj, faceTarget, level);
150
151 if (!texImage) {
152 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
153 return;
154 }
155
156 _mesa_init_teximage_fields(ctx, texImage,
157 levelWidth, levelHeight, levelDepth,
158 0, internalFormat, texFormat);
159 }
160
161 next_mipmap_level_size(target, &levelWidth, &levelHeight, &levelDepth);
162 }
163
164 assert(levelWidth > 0);
165 assert(levelHeight > 0);
166 assert(levelDepth > 0);
167
168 if (!_mesa_is_proxy_texture(texObj->Target)) {
169 /* Do actual texture memory allocation */
170 if (!ctx->Driver.AllocTextureStorage(ctx, texObj, levels,
171 width, height, depth)) {
172 /* Reset the texture images' info to zeros.
173 * Strictly speaking, we probably don't have to do this since
174 * generating GL_OUT_OF_MEMORY can leave things in an undefined
175 * state but this puts things in a consistent state.
176 */
177 for (level = 0; level < levels; level++) {
178 for (face = 0; face < numFaces; face++) {
179 struct gl_texture_image *texImage = texObj->Image[face][level];
180 if (texImage) {
181 _mesa_init_teximage_fields(ctx, texImage,
182 0, 0, 0, 0,
183 GL_NONE, MESA_FORMAT_NONE);
184 }
185 }
186 }
187
188 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexStorage%uD", dims);
189
190 return;
191 }
192
193 /* Only set this field for non-proxy texture objects */
194 texObj->Immutable = GL_TRUE;
195 }
196 }
197
198
199 /**
200 * Clear all fields of texture object to zeros. Used for proxy texture tests.
201 */
202 static void
203 clear_image_fields(struct gl_context *ctx,
204 GLuint dims,
205 struct gl_texture_object *texObj)
206 {
207 const GLenum target = texObj->Target;
208 const GLuint numFaces = _mesa_num_tex_faces(target);
209 GLint level;
210 GLuint face;
211
212 for (level = 0; level < Elements(texObj->Image[0]); level++) {
213 for (face = 0; face < numFaces; face++) {
214 const GLenum faceTarget =
215 (target == GL_TEXTURE_CUBE_MAP)
216 ? GL_TEXTURE_CUBE_MAP_POSITIVE_X + face : target;
217 struct gl_texture_image *texImage =
218 _mesa_get_tex_image(ctx, texObj, faceTarget, level);
219
220 if (!texImage) {
221 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexStorage%uD", dims);
222 return;
223 }
224
225 _mesa_init_teximage_fields(ctx, texImage,
226 0, 0, 0, 0, GL_NONE, MESA_FORMAT_NONE);
227 }
228 }
229 }
230
231
232 /**
233 * Do error checking for calls to glTexStorage1/2/3D().
234 * If an error is found, record it with _mesa_error(), unless the target
235 * is a proxy texture.
236 * \return GL_TRUE if any error, GL_FALSE otherwise.
237 */
238 static GLboolean
239 tex_storage_error_check(struct gl_context *ctx, GLuint dims, GLenum target,
240 GLsizei levels, GLenum internalformat,
241 GLsizei width, GLsizei height, GLsizei depth)
242 {
243 struct gl_texture_object *texObj;
244 GLuint maxDim;
245 GLboolean legalFormat;
246
247 /* check internal format - note that only sized formats are allowed */
248 switch (internalformat) {
249 case GL_ALPHA:
250 case GL_LUMINANCE:
251 case GL_LUMINANCE_ALPHA:
252 case GL_INTENSITY:
253 case GL_RED:
254 case GL_RG:
255 case GL_RGB:
256 case GL_RGBA:
257 case GL_BGRA:
258 case GL_DEPTH_COMPONENT:
259 case GL_DEPTH_STENCIL:
260 case GL_COMPRESSED_ALPHA:
261 case GL_COMPRESSED_LUMINANCE_ALPHA:
262 case GL_COMPRESSED_LUMINANCE:
263 case GL_COMPRESSED_INTENSITY:
264 case GL_COMPRESSED_RGB:
265 case GL_COMPRESSED_RGBA:
266 case GL_COMPRESSED_SRGB:
267 case GL_COMPRESSED_SRGB_ALPHA:
268 case GL_COMPRESSED_SLUMINANCE:
269 case GL_COMPRESSED_SLUMINANCE_ALPHA:
270 case GL_RED_INTEGER:
271 case GL_GREEN_INTEGER:
272 case GL_BLUE_INTEGER:
273 case GL_ALPHA_INTEGER:
274 case GL_RGB_INTEGER:
275 case GL_RGBA_INTEGER:
276 case GL_BGR_INTEGER:
277 case GL_BGRA_INTEGER:
278 case GL_LUMINANCE_INTEGER_EXT:
279 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
280 /* these unsized formats are illegal */
281 legalFormat = GL_FALSE;
282 break;
283 default:
284 legalFormat = _mesa_base_tex_format(ctx, internalformat) > 0;
285 }
286
287 if (!legalFormat) {
288 _mesa_error(ctx, GL_INVALID_ENUM,
289 "glTexStorage%uD(internalformat = %s)", dims,
290 _mesa_lookup_enum_by_nr(internalformat));
291 return GL_TRUE;
292 }
293
294 /* size check */
295 if (width < 1 || height < 1 || depth < 1) {
296 _mesa_error(ctx, GL_INVALID_VALUE,
297 "glTexStorage%uD(width, height or depth < 1)", dims);
298 return GL_TRUE;
299 }
300
301 /* levels check */
302 if (levels < 1 || height < 1 || depth < 1) {
303 _mesa_error(ctx, GL_INVALID_VALUE, "glTexStorage%uD(levels < 1)",
304 dims);
305 return GL_TRUE;
306 }
307
308 /* target check */
309 if (!legal_texobj_target(ctx, dims, target)) {
310 _mesa_error(ctx, GL_INVALID_ENUM,
311 "glTexStorage%uD(illegal target=%s)",
312 dims, _mesa_lookup_enum_by_nr(target));
313 return GL_TRUE;
314 }
315
316 /* check levels against maximum */
317 if (levels > _mesa_max_texture_levels(ctx, target)) {
318 _mesa_error(ctx, GL_INVALID_OPERATION,
319 "glTexStorage%uD(levels too large)", dims);
320 return GL_TRUE;
321 }
322
323 /* check levels against width/height/depth */
324 maxDim = MAX3(width, height, depth);
325 if (levels > _mesa_logbase2(maxDim) + 1) {
326 _mesa_error(ctx, GL_INVALID_OPERATION,
327 "glTexStorage%uD(too many levels for max texture dimension)",
328 dims);
329 return GL_TRUE;
330 }
331
332 /* non-default texture object check */
333 texObj = _mesa_get_current_tex_object(ctx, target);
334 if (!texObj || (texObj->Name == 0)) {
335 _mesa_error(ctx, GL_INVALID_OPERATION,
336 "glTexStorage%uD(texture object 0)", dims);
337 return GL_TRUE;
338 }
339
340 /* Check if texObj->Immutable is set */
341 if (texObj->Immutable) {
342 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexStorage%uD(immutable)",
343 dims);
344 return GL_TRUE;
345 }
346
347 return GL_FALSE;
348 }
349
350
351 /**
352 * Helper used by _mesa_TexStorage1/2/3D().
353 */
354 static void
355 texstorage(GLuint dims, GLenum target, GLsizei levels, GLenum internalformat,
356 GLsizei width, GLsizei height, GLsizei depth)
357 {
358 struct gl_texture_object *texObj;
359 GLboolean sizeOK;
360 gl_format texFormat;
361
362 GET_CURRENT_CONTEXT(ctx);
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 sizeOK = ctx->Driver.TestProxyTexImage(ctx, target, 0, texFormat,
377 width, height, depth, 0);
378
379 if (!sizeOK) {
380 if (_mesa_is_proxy_texture(texObj->Target)) {
381 /* clear all image fields for [levels] */
382 clear_image_fields(ctx, dims, texObj);
383 }
384 else {
385 _mesa_error(ctx, GL_INVALID_VALUE,
386 "glTexStorage%uD(invalid width, height or depth)",
387 dims);
388 return;
389 }
390 }
391 else {
392 setup_texstorage(ctx, texObj, dims, texFormat, levels, internalformat,
393 width, height, depth);
394 }
395 }
396
397
398 void GLAPIENTRY
399 _mesa_TexStorage1D(GLenum target, GLsizei levels, GLenum internalformat,
400 GLsizei width)
401 {
402 texstorage(1, target, levels, internalformat, width, 1, 1);
403 }
404
405
406 void GLAPIENTRY
407 _mesa_TexStorage2D(GLenum target, GLsizei levels, GLenum internalformat,
408 GLsizei width, GLsizei height)
409 {
410 texstorage(2, target, levels, internalformat, width, height, 1);
411 }
412
413
414 void GLAPIENTRY
415 _mesa_TexStorage3D(GLenum target, GLsizei levels, GLenum internalformat,
416 GLsizei width, GLsizei height, GLsizei depth)
417 {
418 texstorage(3, target, levels, internalformat, width, height, depth);
419 }
420
421
422
423 /*
424 * Note: we don't support GL_EXT_direct_state_access and the spec says
425 * we don't need the following functions. However, glew checks for the
426 * presence of all six functions and will say that GL_ARB_texture_storage
427 * is not supported if these functions are missing.
428 */
429
430
431 void GLAPIENTRY
432 _mesa_TextureStorage1DEXT(GLuint texture, GLenum target, GLsizei levels,
433 GLenum internalformat,
434 GLsizei width)
435 {
436 /* no-op */
437 }
438
439
440 void GLAPIENTRY
441 _mesa_TextureStorage2DEXT(GLuint texture, GLenum target, GLsizei levels,
442 GLenum internalformat,
443 GLsizei width, GLsizei height)
444 {
445 /* no-op */
446 }
447
448
449
450 void GLAPIENTRY
451 _mesa_TextureStorage3DEXT(GLuint texture, GLenum target, GLsizei levels,
452 GLenum internalformat,
453 GLsizei width, GLsizei height, GLsizei depth)
454 {
455 /* no-op */
456 }