mesa/main: Fix missing return in non void function
[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 * \file texstorage.c
27 * GL_ARB_texture_storage functions
28 */
29
30 #include "glheader.h"
31 #include "context.h"
32 #include "enums.h"
33 #include "imports.h"
34 #include "macros.h"
35 #include "teximage.h"
36 #include "texobj.h"
37 #include "mipmap.h"
38 #include "texstorage.h"
39 #include "textureview.h"
40 #include "mtypes.h"
41 #include "glformats.h"
42 #include "hash.h"
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 if (_mesa_is_gles3(ctx)
55 && target != GL_TEXTURE_2D
56 && target != GL_TEXTURE_CUBE_MAP
57 && target != GL_TEXTURE_3D
58 && target != GL_TEXTURE_2D_ARRAY)
59 return GL_FALSE;
60
61 switch (dims) {
62 case 1:
63 switch (target) {
64 case GL_TEXTURE_1D:
65 case GL_PROXY_TEXTURE_1D:
66 return GL_TRUE;
67 default:
68 return GL_FALSE;
69 }
70 case 2:
71 switch (target) {
72 case GL_TEXTURE_2D:
73 case GL_PROXY_TEXTURE_2D:
74 return GL_TRUE;
75 case GL_TEXTURE_CUBE_MAP:
76 case GL_PROXY_TEXTURE_CUBE_MAP:
77 return ctx->Extensions.ARB_texture_cube_map;
78 case GL_TEXTURE_RECTANGLE:
79 case GL_PROXY_TEXTURE_RECTANGLE:
80 return ctx->Extensions.NV_texture_rectangle;
81 case GL_TEXTURE_1D_ARRAY:
82 case GL_PROXY_TEXTURE_1D_ARRAY:
83 return ctx->Extensions.EXT_texture_array;
84 default:
85 return GL_FALSE;
86 }
87 case 3:
88 switch (target) {
89 case GL_TEXTURE_3D:
90 case GL_PROXY_TEXTURE_3D:
91 return GL_TRUE;
92 case GL_TEXTURE_2D_ARRAY:
93 case GL_PROXY_TEXTURE_2D_ARRAY:
94 return ctx->Extensions.EXT_texture_array;
95 case GL_TEXTURE_CUBE_MAP_ARRAY:
96 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
97 return ctx->Extensions.ARB_texture_cube_map_array;
98 default:
99 return GL_FALSE;
100 }
101 default:
102 _mesa_problem(ctx, "invalid dims=%u in legal_texobj_target()", dims);
103 return GL_FALSE;
104 }
105 }
106
107
108 /** Helper to get a particular texture image in a texture object */
109 static struct gl_texture_image *
110 get_tex_image(struct gl_context *ctx,
111 struct gl_texture_object *texObj,
112 GLuint face, GLuint level)
113 {
114 const GLenum faceTarget =
115 (texObj->Target == GL_TEXTURE_CUBE_MAP ||
116 texObj->Target == GL_PROXY_TEXTURE_CUBE_MAP)
117 ? GL_TEXTURE_CUBE_MAP_POSITIVE_X + face : texObj->Target;
118 return _mesa_get_tex_image(ctx, texObj, faceTarget, level);
119 }
120
121
122
123 static GLboolean
124 initialize_texture_fields(struct gl_context *ctx,
125 struct gl_texture_object *texObj,
126 GLint levels,
127 GLsizei width, GLsizei height, GLsizei depth,
128 GLenum internalFormat, mesa_format texFormat)
129 {
130 const GLenum target = texObj->Target;
131 const GLuint numFaces = _mesa_num_tex_faces(target);
132 GLint level, levelWidth = width, levelHeight = height, levelDepth = depth;
133 GLuint face;
134
135 /* Set up all the texture object's gl_texture_images */
136 for (level = 0; level < levels; level++) {
137 for (face = 0; face < numFaces; face++) {
138 struct gl_texture_image *texImage =
139 get_tex_image(ctx, texObj, face, level);
140
141 if (!texImage) {
142 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexStorage");
143 return GL_FALSE;
144 }
145
146 _mesa_init_teximage_fields(ctx, texImage,
147 levelWidth, levelHeight, levelDepth,
148 0, internalFormat, texFormat);
149 }
150
151 _mesa_next_mipmap_level_size(target, 0,
152 levelWidth, levelHeight, levelDepth,
153 &levelWidth, &levelHeight, &levelDepth);
154 }
155 return GL_TRUE;
156 }
157
158
159 /**
160 * Clear all fields of texture object to zeros. Used for proxy texture tests
161 * and to clean up when a texture memory allocation fails.
162 */
163 static void
164 clear_texture_fields(struct gl_context *ctx,
165 struct gl_texture_object *texObj)
166 {
167 const GLenum target = texObj->Target;
168 const GLuint numFaces = _mesa_num_tex_faces(target);
169 GLint level;
170 GLuint face;
171
172 for (level = 0; level < ARRAY_SIZE(texObj->Image[0]); level++) {
173 for (face = 0; face < numFaces; face++) {
174 struct gl_texture_image *texImage =
175 get_tex_image(ctx, texObj, face, level);
176
177 if (!texImage) {
178 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexStorage");
179 return;
180 }
181
182 _mesa_clear_texture_image(ctx, texImage);
183 }
184 }
185 }
186
187
188 /**
189 * Update/re-validate framebuffer object.
190 */
191 static void
192 update_fbo_texture(struct gl_context *ctx, struct gl_texture_object *texObj)
193 {
194 const unsigned numFaces = _mesa_num_tex_faces(texObj->Target);
195 for (int level = 0; level < ARRAY_SIZE(texObj->Image[0]); level++) {
196 for (unsigned face = 0; face < numFaces; face++)
197 _mesa_update_fbo_texture(ctx, texObj, face, level);
198 }
199 }
200
201
202 GLboolean
203 _mesa_is_legal_tex_storage_format(const struct gl_context *ctx,
204 GLenum internalformat)
205 {
206 /* check internal format - note that only sized formats are allowed */
207 switch (internalformat) {
208 case GL_ALPHA:
209 case GL_LUMINANCE:
210 case GL_LUMINANCE_ALPHA:
211 case GL_INTENSITY:
212 case GL_RED:
213 case GL_RG:
214 case GL_RGB:
215 case GL_RGBA:
216 case GL_BGRA:
217 case GL_DEPTH_COMPONENT:
218 case GL_DEPTH_STENCIL:
219 case GL_COMPRESSED_ALPHA:
220 case GL_COMPRESSED_LUMINANCE_ALPHA:
221 case GL_COMPRESSED_LUMINANCE:
222 case GL_COMPRESSED_INTENSITY:
223 case GL_COMPRESSED_RGB:
224 case GL_COMPRESSED_RGBA:
225 case GL_COMPRESSED_SRGB:
226 case GL_COMPRESSED_SRGB_ALPHA:
227 case GL_COMPRESSED_SLUMINANCE:
228 case GL_COMPRESSED_SLUMINANCE_ALPHA:
229 case GL_RED_INTEGER:
230 case GL_GREEN_INTEGER:
231 case GL_BLUE_INTEGER:
232 case GL_ALPHA_INTEGER:
233 case GL_RGB_INTEGER:
234 case GL_RGBA_INTEGER:
235 case GL_BGR_INTEGER:
236 case GL_BGRA_INTEGER:
237 case GL_LUMINANCE_INTEGER_EXT:
238 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
239 /* these unsized formats are illegal */
240 return GL_FALSE;
241 default:
242 return _mesa_base_tex_format(ctx, internalformat) > 0;
243 }
244 }
245
246
247 /**
248 * Default ctx->Driver.AllocTextureStorage() handler.
249 *
250 * The driver can override this with a more specific implementation if it
251 * desires, but this can be used to get the texture images allocated using the
252 * usual texture image handling code. The immutability of
253 * GL_ARB_texture_storage texture layouts is handled by texObj->Immutable
254 * checks at glTexImage* time.
255 */
256 GLboolean
257 _mesa_AllocTextureStorage_sw(struct gl_context *ctx,
258 struct gl_texture_object *texObj,
259 GLsizei levels, GLsizei width,
260 GLsizei height, GLsizei depth)
261 {
262 const int numFaces = _mesa_num_tex_faces(texObj->Target);
263 int face;
264 int level;
265
266 (void) width;
267 (void) height;
268 (void) depth;
269
270 for (face = 0; face < numFaces; face++) {
271 for (level = 0; level < levels; level++) {
272 struct gl_texture_image *const texImage = texObj->Image[face][level];
273 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage))
274 return GL_FALSE;
275 }
276 }
277
278 return GL_TRUE;
279 }
280
281
282 /**
283 * Do error checking for calls to glTexStorage1/2/3D().
284 * If an error is found, record it with _mesa_error(), unless the target
285 * is a proxy texture.
286 * \return GL_TRUE if any error, GL_FALSE otherwise.
287 */
288 static GLboolean
289 tex_storage_error_check(struct gl_context *ctx,
290 struct gl_texture_object *texObj,
291 GLuint dims, GLenum target,
292 GLsizei levels, GLenum internalformat,
293 GLsizei width, GLsizei height, GLsizei depth,
294 bool dsa)
295 {
296 const char* suffix = dsa ? "ture" : "";
297
298 /* Legal format checking has been moved to texstorage and texturestorage in
299 * order to allow meta functions to use legacy formats. */
300
301 /* size check */
302 if (!_mesa_valid_tex_storage_dim(width, height, depth)) {
303 _mesa_error(ctx, GL_INVALID_VALUE,
304 "glTex%sStorage%uD(width, height or depth < 1)",
305 suffix, dims);
306 return GL_TRUE;
307 }
308
309 if (_mesa_is_compressed_format(ctx, internalformat)) {
310 GLenum err;
311 if (!_mesa_target_can_be_compressed(ctx, target, internalformat, &err)) {
312 _mesa_error(ctx, err,
313 "glTex%sStorage%dD(internalformat = %s)", suffix, dims,
314 _mesa_enum_to_string(internalformat));
315 return GL_TRUE;
316 }
317 }
318
319 /* levels check */
320 if (levels < 1) {
321 _mesa_error(ctx, GL_INVALID_VALUE, "glTex%sStorage%uD(levels < 1)",
322 suffix, dims);
323 return GL_TRUE;
324 }
325
326 /* check levels against maximum (note different error than above) */
327 if (levels > (GLint) _mesa_max_texture_levels(ctx, target)) {
328 _mesa_error(ctx, GL_INVALID_OPERATION,
329 "glTex%sStorage%uD(levels too large)",
330 suffix, dims);
331 return GL_TRUE;
332 }
333
334 /* check levels against width/height/depth */
335 if (levels > _mesa_get_tex_max_num_levels(target, width, height, depth)) {
336 _mesa_error(ctx, GL_INVALID_OPERATION,
337 "glTex%sStorage%uD(too many levels"
338 " for max texture dimension)",
339 suffix, dims);
340 return GL_TRUE;
341 }
342
343 /* non-default texture object check */
344 if (!_mesa_is_proxy_texture(target) && (!texObj || (texObj->Name == 0))) {
345 _mesa_error(ctx, GL_INVALID_OPERATION,
346 "glTex%sStorage%uD(texture object 0)",
347 suffix, dims);
348 return GL_TRUE;
349 }
350
351 /* Check if texObj->Immutable is set */
352 if (!_mesa_is_proxy_texture(target) && texObj->Immutable) {
353 _mesa_error(ctx, GL_INVALID_OPERATION, "glTex%sStorage%uD(immutable)",
354 suffix, dims);
355 return GL_TRUE;
356 }
357
358 /* additional checks for depth textures */
359 if (!_mesa_legal_texture_base_format_for_target(ctx, target, internalformat)) {
360 _mesa_error(ctx, GL_INVALID_OPERATION, "glTex%sStorage%uD(bad target for texture)",
361 suffix, dims);
362 return GL_TRUE;
363 }
364
365 return GL_FALSE;
366 }
367
368
369 /**
370 * Helper that does the storage allocation for _mesa_TexStorage1/2/3D()
371 * and _mesa_TextureStorage1/2/3D().
372 */
373 void
374 _mesa_texture_storage(struct gl_context *ctx, GLuint dims,
375 struct gl_texture_object *texObj,
376 GLenum target, GLsizei levels,
377 GLenum internalformat, GLsizei width,
378 GLsizei height, GLsizei depth, bool dsa)
379 {
380 GLboolean sizeOK, dimensionsOK;
381 mesa_format texFormat;
382 const char* suffix = dsa ? "ture" : "";
383
384 assert(texObj);
385
386 if (tex_storage_error_check(ctx, texObj, dims, target, levels,
387 internalformat, width, height, depth, dsa)) {
388 return; /* error was recorded */
389 }
390
391 texFormat = _mesa_choose_texture_format(ctx, texObj, target, 0,
392 internalformat, GL_NONE, GL_NONE);
393 assert(texFormat != MESA_FORMAT_NONE);
394
395 /* check that width, height, depth are legal for the mipmap level */
396 dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, 0,
397 width, height, depth, 0);
398
399 sizeOK = ctx->Driver.TestProxyTexImage(ctx, target, levels, 0, texFormat,
400 1, width, height, depth);
401
402 if (_mesa_is_proxy_texture(target)) {
403 if (dimensionsOK && sizeOK) {
404 initialize_texture_fields(ctx, texObj, levels, width, height, depth,
405 internalformat, texFormat);
406 }
407 else {
408 /* clear all image fields for [levels] */
409 clear_texture_fields(ctx, texObj);
410 }
411 }
412 else {
413 if (!dimensionsOK) {
414 _mesa_error(ctx, GL_INVALID_VALUE,
415 "glTex%sStorage%uD(invalid width, height or depth)",
416 suffix, dims);
417 return;
418 }
419
420 if (!sizeOK) {
421 _mesa_error(ctx, GL_OUT_OF_MEMORY,
422 "glTex%sStorage%uD(texture too large)",
423 suffix, dims);
424 }
425
426 assert(levels > 0);
427 assert(width > 0);
428 assert(height > 0);
429 assert(depth > 0);
430
431 if (!initialize_texture_fields(ctx, texObj, levels, width, height, depth,
432 internalformat, texFormat)) {
433 return;
434 }
435
436 /* Do actual texture memory allocation */
437 if (!ctx->Driver.AllocTextureStorage(ctx, texObj, levels,
438 width, height, depth)) {
439 /* Reset the texture images' info to zeros.
440 * Strictly speaking, we probably don't have to do this since
441 * generating GL_OUT_OF_MEMORY can leave things in an undefined
442 * state but this puts things in a consistent state.
443 */
444 clear_texture_fields(ctx, texObj);
445 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTex%sStorage%uD",
446 suffix, dims);
447 return;
448 }
449
450 _mesa_set_texture_view_state(ctx, texObj, target, levels);
451
452 update_fbo_texture(ctx, texObj);
453 }
454 }
455
456
457 /**
458 * Helper used by _mesa_TexStorage1/2/3D().
459 */
460 static void
461 texstorage(GLuint dims, GLenum target, GLsizei levels, GLenum internalformat,
462 GLsizei width, GLsizei height, GLsizei depth)
463 {
464 struct gl_texture_object *texObj;
465 GET_CURRENT_CONTEXT(ctx);
466
467 /* Check target. This is done here so that _mesa_texture_storage
468 * can receive unsized formats.
469 */
470 if (!legal_texobj_target(ctx, dims, target)) {
471 _mesa_error(ctx, GL_INVALID_ENUM,
472 "glTexStorage%uD(illegal target=%s)",
473 dims, _mesa_enum_to_string(target));
474 return;
475 }
476
477 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
478 _mesa_debug(ctx, "glTexStorage%uD %s %d %s %d %d %d\n",
479 dims,
480 _mesa_enum_to_string(target), levels,
481 _mesa_enum_to_string(internalformat),
482 width, height, depth);
483
484 /* Check the format to make sure it is sized. */
485 if (!_mesa_is_legal_tex_storage_format(ctx, internalformat)) {
486 _mesa_error(ctx, GL_INVALID_ENUM,
487 "glTexStorage%uD(internalformat = %s)", dims,
488 _mesa_enum_to_string(internalformat));
489 return;
490 }
491
492 texObj = _mesa_get_current_tex_object(ctx, target);
493 if (!texObj)
494 return;
495
496 _mesa_texture_storage(ctx, dims, texObj, target, levels,
497 internalformat, width, height, depth, false);
498 }
499
500
501 /**
502 * Helper used by _mesa_TextureStorage1/2/3D().
503 */
504 static void
505 texturestorage(GLuint dims, GLuint texture, GLsizei levels,
506 GLenum internalformat, GLsizei width, GLsizei height,
507 GLsizei depth)
508 {
509 struct gl_texture_object *texObj;
510 GET_CURRENT_CONTEXT(ctx);
511
512 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
513 _mesa_debug(ctx, "glTextureStorage%uD %d %d %s %d %d %d\n",
514 dims, texture, levels,
515 _mesa_enum_to_string(internalformat),
516 width, height, depth);
517
518 /* Check the format to make sure it is sized. */
519 if (!_mesa_is_legal_tex_storage_format(ctx, internalformat)) {
520 _mesa_error(ctx, GL_INVALID_ENUM,
521 "glTextureStorage%uD(internalformat = %s)", dims,
522 _mesa_enum_to_string(internalformat));
523 return;
524 }
525
526 /* Get the texture object by Name. */
527 texObj = _mesa_lookup_texture(ctx, texture);
528 if (!texObj) {
529 _mesa_error(ctx, GL_INVALID_OPERATION,
530 "glTextureStorage%uD(texture = %d)", dims, texture);
531 return;
532 }
533
534 /* Check target. This is done here so that _mesa_texture_storage
535 * can receive unsized formats.
536 */
537 if (!legal_texobj_target(ctx, dims, texObj->Target)) {
538 _mesa_error(ctx, GL_INVALID_ENUM,
539 "glTextureStorage%uD(illegal target=%s)",
540 dims, _mesa_enum_to_string(texObj->Target));
541 return;
542 }
543
544 _mesa_texture_storage(ctx, dims, texObj, texObj->Target,
545 levels, internalformat, width, height, depth, true);
546 }
547
548
549 void GLAPIENTRY
550 _mesa_TexStorage1D(GLenum target, GLsizei levels, GLenum internalformat,
551 GLsizei width)
552 {
553 texstorage(1, target, levels, internalformat, width, 1, 1);
554 }
555
556
557 void GLAPIENTRY
558 _mesa_TexStorage2D(GLenum target, GLsizei levels, GLenum internalformat,
559 GLsizei width, GLsizei height)
560 {
561 texstorage(2, target, levels, internalformat, width, height, 1);
562 }
563
564
565 void GLAPIENTRY
566 _mesa_TexStorage3D(GLenum target, GLsizei levels, GLenum internalformat,
567 GLsizei width, GLsizei height, GLsizei depth)
568 {
569 texstorage(3, target, levels, internalformat, width, height, depth);
570 }
571
572
573 void GLAPIENTRY
574 _mesa_TextureStorage1D(GLuint texture, GLsizei levels, GLenum internalformat,
575 GLsizei width)
576 {
577 texturestorage(1, texture, levels, internalformat, width, 1, 1);
578 }
579
580
581 void GLAPIENTRY
582 _mesa_TextureStorage2D(GLuint texture, GLsizei levels,
583 GLenum internalformat,
584 GLsizei width, GLsizei height)
585 {
586 texturestorage(2, texture, levels, internalformat, width, height, 1);
587 }
588
589
590 void GLAPIENTRY
591 _mesa_TextureStorage3D(GLuint texture, GLsizei levels, GLenum internalformat,
592 GLsizei width, GLsizei height, GLsizei depth)
593 {
594 texturestorage(3, texture, levels, internalformat, width, height, depth);
595 }
596
597
598 /*
599 * Note: we don't support GL_EXT_direct_state_access and the spec says
600 * we don't need the following functions. However, glew checks for the
601 * presence of all six functions and will say that GL_ARB_texture_storage
602 * is not supported if these functions are missing.
603 */
604
605
606 void GLAPIENTRY
607 _mesa_TextureStorage1DEXT(GLuint texture, GLenum target, GLsizei levels,
608 GLenum internalformat,
609 GLsizei width)
610 {
611 GET_CURRENT_CONTEXT(ctx);
612
613 (void) texture;
614 (void) target;
615 (void) levels;
616 (void) internalformat;
617 (void) width;
618
619 _mesa_error(ctx, GL_INVALID_OPERATION,
620 "glTextureStorage1DEXT not supported");
621 }
622
623
624 void GLAPIENTRY
625 _mesa_TextureStorage2DEXT(GLuint texture, GLenum target, GLsizei levels,
626 GLenum internalformat,
627 GLsizei width, GLsizei height)
628 {
629 GET_CURRENT_CONTEXT(ctx);
630
631 (void) texture;
632 (void) target;
633 (void) levels;
634 (void) internalformat;
635 (void) width;
636 (void) height;
637
638 _mesa_error(ctx, GL_INVALID_OPERATION,
639 "glTextureStorage2DEXT not supported");
640 }
641
642
643 void GLAPIENTRY
644 _mesa_TextureStorage3DEXT(GLuint texture, GLenum target, GLsizei levels,
645 GLenum internalformat,
646 GLsizei width, GLsizei height, GLsizei depth)
647 {
648 GET_CURRENT_CONTEXT(ctx);
649
650 (void) texture;
651 (void) target;
652 (void) levels;
653 (void) internalformat;
654 (void) width;
655 (void) height;
656 (void) depth;
657
658 _mesa_error(ctx, GL_INVALID_OPERATION,
659 "glTextureStorage3DEXT not supported");
660 }