mesa: consolidate multiple next_mipmap_level_size
[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 "mtypes.h"
43
44
45
46 /**
47 * Check if the given texture target is a legal texture object target
48 * for a glTexStorage() command.
49 * This is a bit different than legal_teximage_target() when it comes
50 * to cube maps.
51 */
52 static GLboolean
53 legal_texobj_target(struct gl_context *ctx, GLuint dims, GLenum target)
54 {
55 switch (dims) {
56 case 1:
57 switch (target) {
58 case GL_TEXTURE_1D:
59 case GL_PROXY_TEXTURE_1D:
60 return GL_TRUE;
61 default:
62 return GL_FALSE;
63 }
64 case 2:
65 switch (target) {
66 case GL_TEXTURE_2D:
67 case GL_PROXY_TEXTURE_2D:
68 return GL_TRUE;
69 case GL_TEXTURE_CUBE_MAP:
70 case GL_PROXY_TEXTURE_CUBE_MAP:
71 return ctx->Extensions.ARB_texture_cube_map;
72 case GL_TEXTURE_RECTANGLE:
73 case GL_PROXY_TEXTURE_RECTANGLE:
74 return ctx->Extensions.NV_texture_rectangle;
75 case GL_TEXTURE_1D_ARRAY:
76 case GL_PROXY_TEXTURE_1D_ARRAY:
77 return 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.EXT_texture_array;
89 case GL_TEXTURE_CUBE_MAP_ARRAY:
90 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
91 return ctx->Extensions.ARB_texture_cube_map_array;
92 default:
93 return GL_FALSE;
94 }
95 default:
96 _mesa_problem(ctx, "invalid dims=%u in legal_texobj_target()", dims);
97 return GL_FALSE;
98 }
99 }
100
101
102 /** Helper to get a particular texture image in a texture object */
103 static struct gl_texture_image *
104 get_tex_image(struct gl_context *ctx,
105 struct gl_texture_object *texObj,
106 GLuint face, GLuint level)
107 {
108 const GLenum faceTarget =
109 (texObj->Target == GL_TEXTURE_CUBE_MAP ||
110 texObj->Target == GL_PROXY_TEXTURE_CUBE_MAP)
111 ? GL_TEXTURE_CUBE_MAP_POSITIVE_X + face : texObj->Target;
112 return _mesa_get_tex_image(ctx, texObj, faceTarget, level);
113 }
114
115
116
117 static GLboolean
118 initialize_texture_fields(struct gl_context *ctx,
119 struct gl_texture_object *texObj,
120 GLint levels,
121 GLsizei width, GLsizei height, GLsizei depth,
122 GLenum internalFormat, gl_format texFormat)
123 {
124 const GLenum target = texObj->Target;
125 const GLuint numFaces = _mesa_num_tex_faces(target);
126 GLint level, levelWidth = width, levelHeight = height, levelDepth = depth;
127 GLuint face;
128
129 /* Set up all the texture object's gl_texture_images */
130 for (level = 0; level < levels; level++) {
131 for (face = 0; face < numFaces; face++) {
132 struct gl_texture_image *texImage =
133 get_tex_image(ctx, texObj, face, level);
134
135 if (!texImage) {
136 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexStorage");
137 return GL_FALSE;
138 }
139
140 _mesa_init_teximage_fields(ctx, texImage,
141 levelWidth, levelHeight, levelDepth,
142 0, internalFormat, texFormat);
143 }
144
145 _mesa_next_mipmap_level_size(target, 0, levelWidth, levelHeight, levelDepth,
146 &levelWidth, &levelHeight, &levelDepth);
147 }
148 return GL_TRUE;
149 }
150
151
152 /**
153 * Clear all fields of texture object to zeros. Used for proxy texture tests.
154 * Used for proxy texture tests (and to clean up when a texture memory
155 * allocation fails).
156 */
157 static void
158 clear_texture_fields(struct gl_context *ctx,
159 struct gl_texture_object *texObj)
160 {
161 const GLenum target = texObj->Target;
162 const GLuint numFaces = _mesa_num_tex_faces(target);
163 GLint level;
164 GLuint face;
165
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);
170
171 if (!texImage) {
172 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexStorage");
173 return;
174 }
175
176 _mesa_init_teximage_fields(ctx, texImage,
177 0, 0, 0, 0, /* w, h, d, border */
178 GL_NONE, MESA_FORMAT_NONE);
179 }
180 }
181 }
182
183
184 GLboolean
185 _mesa_is_legal_tex_storage_format(struct gl_context *ctx, GLenum internalformat)
186 {
187 /* check internal format - note that only sized formats are allowed */
188 switch (internalformat) {
189 case GL_ALPHA:
190 case GL_LUMINANCE:
191 case GL_LUMINANCE_ALPHA:
192 case GL_INTENSITY:
193 case GL_RED:
194 case GL_RG:
195 case GL_RGB:
196 case GL_RGBA:
197 case GL_BGRA:
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:
210 case GL_RED_INTEGER:
211 case GL_GREEN_INTEGER:
212 case GL_BLUE_INTEGER:
213 case GL_ALPHA_INTEGER:
214 case GL_RGB_INTEGER:
215 case GL_RGBA_INTEGER:
216 case GL_BGR_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 */
221 return GL_FALSE;
222 default:
223 return _mesa_base_tex_format(ctx, internalformat) > 0;
224 }
225 }
226
227 /**
228 * Default ctx->Driver.AllocTextureStorage() handler.
229 *
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.
235 */
236 GLboolean
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)
241 {
242 const int numFaces = _mesa_num_tex_faces(texObj->Target);
243 int face;
244 int level;
245
246 for (face = 0; face < numFaces; face++) {
247 for (level = 0; level < levels; level++) {
248 struct gl_texture_image *const texImage = texObj->Image[face][level];
249 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage))
250 return GL_FALSE;
251 }
252 }
253
254 return GL_TRUE;
255 }
256
257
258 /**
259 * Do error checking for calls to glTexStorage1/2/3D().
260 * If an error is found, record it with _mesa_error(), unless the target
261 * is a proxy texture.
262 * \return GL_TRUE if any error, GL_FALSE otherwise.
263 */
264 static GLboolean
265 tex_storage_error_check(struct gl_context *ctx, GLuint dims, GLenum target,
266 GLsizei levels, GLenum internalformat,
267 GLsizei width, GLsizei height, GLsizei depth)
268 {
269 struct gl_texture_object *texObj;
270
271 if (!_mesa_is_legal_tex_storage_format(ctx, internalformat)) {
272 _mesa_error(ctx, GL_INVALID_ENUM,
273 "glTexStorage%uD(internalformat = %s)", dims,
274 _mesa_lookup_enum_by_nr(internalformat));
275 return GL_TRUE;
276 }
277
278 /* size check */
279 if (width < 1 || height < 1 || depth < 1) {
280 _mesa_error(ctx, GL_INVALID_VALUE,
281 "glTexStorage%uD(width, height or depth < 1)", dims);
282 return GL_TRUE;
283 }
284
285 /* target check */
286 if (!legal_texobj_target(ctx, dims, target)) {
287 _mesa_error(ctx, GL_INVALID_ENUM,
288 "glTexStorage%uD(illegal target=%s)",
289 dims, _mesa_lookup_enum_by_nr(target));
290 return GL_TRUE;
291 }
292
293 /* levels check */
294 if (levels < 1) {
295 _mesa_error(ctx, GL_INVALID_VALUE, "glTexStorage%uD(levels < 1)",
296 dims);
297 return GL_TRUE;
298 }
299
300 /* check levels against maximum (note different error than above) */
301 if (levels > (GLint) _mesa_max_texture_levels(ctx, target)) {
302 _mesa_error(ctx, GL_INVALID_OPERATION,
303 "glTexStorage%uD(levels too large)", dims);
304 return GL_TRUE;
305 }
306
307 /* check levels against width/height/depth */
308 if (levels > _mesa_get_tex_max_num_levels(target, width, height, depth)) {
309 _mesa_error(ctx, GL_INVALID_OPERATION,
310 "glTexStorage%uD(too many levels for max texture dimension)",
311 dims);
312 return GL_TRUE;
313 }
314
315 /* non-default texture object check */
316 texObj = _mesa_get_current_tex_object(ctx, target);
317 if (!_mesa_is_proxy_texture(target) && (!texObj || (texObj->Name == 0))) {
318 _mesa_error(ctx, GL_INVALID_OPERATION,
319 "glTexStorage%uD(texture object 0)", dims);
320 return GL_TRUE;
321 }
322
323 /* Check if texObj->Immutable is set */
324 if (!_mesa_is_proxy_texture(target) && texObj->Immutable) {
325 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexStorage%uD(immutable)",
326 dims);
327 return GL_TRUE;
328 }
329
330 return GL_FALSE;
331 }
332
333
334 /**
335 * Helper used by _mesa_TexStorage1/2/3D().
336 */
337 static void
338 texstorage(GLuint dims, GLenum target, GLsizei levels, GLenum internalformat,
339 GLsizei width, GLsizei height, GLsizei depth)
340 {
341 struct gl_texture_object *texObj;
342 GLboolean sizeOK, dimensionsOK;
343 gl_format texFormat;
344
345 GET_CURRENT_CONTEXT(ctx);
346
347 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
348 _mesa_debug(ctx, "glTexStorage%uD %s %d %s %d %d %d\n",
349 dims,
350 _mesa_lookup_enum_by_nr(target), levels,
351 _mesa_lookup_enum_by_nr(internalformat),
352 width, height, depth);
353
354 if (tex_storage_error_check(ctx, dims, target, levels,
355 internalformat, width, height, depth)) {
356 return; /* error was recorded */
357 }
358
359 texObj = _mesa_get_current_tex_object(ctx, target);
360 assert(texObj);
361
362 texFormat = _mesa_choose_texture_format(ctx, texObj, target, 0,
363 internalformat, GL_NONE, GL_NONE);
364 assert(texFormat != MESA_FORMAT_NONE);
365
366 /* check that width, height, depth are legal for the mipmap level */
367 dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, 0,
368 width, height, depth, 0);
369
370 sizeOK = ctx->Driver.TestProxyTexImage(ctx, target, 0, texFormat,
371 width, height, depth, 0);
372
373 if (_mesa_is_proxy_texture(texObj->Target)) {
374 if (dimensionsOK && sizeOK) {
375 initialize_texture_fields(ctx, texObj, levels, width, height, depth,
376 internalformat, texFormat);
377 }
378 else {
379 /* clear all image fields for [levels] */
380 clear_texture_fields(ctx, texObj);
381 }
382 }
383 else {
384 if (!dimensionsOK) {
385 _mesa_error(ctx, GL_INVALID_VALUE,
386 "glTexStorage%uD(invalid width, height or depth)", dims);
387 return;
388 }
389
390 if (!sizeOK) {
391 _mesa_error(ctx, GL_OUT_OF_MEMORY,
392 "glTexStorage%uD(texture too large)", dims);
393 }
394
395 assert(levels > 0);
396 assert(width > 0);
397 assert(height > 0);
398 assert(depth > 0);
399
400 if (!initialize_texture_fields(ctx, texObj, levels, width, height, depth,
401 internalformat, texFormat)) {
402 return;
403 }
404
405 /* Do actual texture memory allocation */
406 if (!ctx->Driver.AllocTextureStorage(ctx, texObj, levels,
407 width, height, depth)) {
408 /* Reset the texture images' info to zeros.
409 * Strictly speaking, we probably don't have to do this since
410 * generating GL_OUT_OF_MEMORY can leave things in an undefined
411 * state but this puts things in a consistent state.
412 */
413 clear_texture_fields(ctx, texObj);
414 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexStorage%uD", dims);
415 return;
416 }
417
418 texObj->Immutable = GL_TRUE;
419 texObj->ImmutableLevels = levels;
420 }
421 }
422
423
424 void GLAPIENTRY
425 _mesa_TexStorage1D(GLenum target, GLsizei levels, GLenum internalformat,
426 GLsizei width)
427 {
428 texstorage(1, target, levels, internalformat, width, 1, 1);
429 }
430
431
432 void GLAPIENTRY
433 _mesa_TexStorage2D(GLenum target, GLsizei levels, GLenum internalformat,
434 GLsizei width, GLsizei height)
435 {
436 texstorage(2, target, levels, internalformat, width, height, 1);
437 }
438
439
440 void GLAPIENTRY
441 _mesa_TexStorage3D(GLenum target, GLsizei levels, GLenum internalformat,
442 GLsizei width, GLsizei height, GLsizei depth)
443 {
444 texstorage(3, target, levels, internalformat, width, height, depth);
445 }
446
447
448
449 /*
450 * Note: we don't support GL_EXT_direct_state_access and the spec says
451 * we don't need the following functions. However, glew checks for the
452 * presence of all six functions and will say that GL_ARB_texture_storage
453 * is not supported if these functions are missing.
454 */
455
456
457 void GLAPIENTRY
458 _mesa_TextureStorage1DEXT(GLuint texture, GLenum target, GLsizei levels,
459 GLenum internalformat,
460 GLsizei width)
461 {
462 /* no-op */
463 }
464
465
466 void GLAPIENTRY
467 _mesa_TextureStorage2DEXT(GLuint texture, GLenum target, GLsizei levels,
468 GLenum internalformat,
469 GLsizei width, GLsizei height)
470 {
471 /* no-op */
472 }
473
474
475
476 void GLAPIENTRY
477 _mesa_TextureStorage3DEXT(GLuint texture, GLenum target, GLsizei levels,
478 GLenum internalformat,
479 GLsizei width, GLsizei height, GLsizei depth)
480 {
481 /* no-op */
482 }