mesa: whitespace, comment fixes in texstorage.c
[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_init_teximage_fields(ctx, texImage,
183 0, 0, 0, 0, /* w, h, d, border */
184 GL_NONE, MESA_FORMAT_NONE);
185 }
186 }
187 }
188
189
190 /**
191 * Update/re-validate framebuffer object.
192 */
193 static void
194 update_fbo_texture(struct gl_context *ctx, struct gl_texture_object *texObj)
195 {
196 const unsigned numFaces = _mesa_num_tex_faces(texObj->Target);
197 for (int level = 0; level < ARRAY_SIZE(texObj->Image[0]); level++) {
198 for (unsigned face = 0; face < numFaces; face++)
199 _mesa_update_fbo_texture(ctx, texObj, face, level);
200 }
201 }
202
203
204 GLboolean
205 _mesa_is_legal_tex_storage_format(struct gl_context *ctx, GLenum internalformat)
206 {
207 /* check internal format - note that only sized formats are allowed */
208 switch (internalformat) {
209 case GL_ALPHA:
210 case GL_LUMINANCE:
211 case GL_LUMINANCE_ALPHA:
212 case GL_INTENSITY:
213 case GL_RED:
214 case GL_RG:
215 case GL_RGB:
216 case GL_RGBA:
217 case GL_BGRA:
218 case GL_DEPTH_COMPONENT:
219 case GL_DEPTH_STENCIL:
220 case GL_COMPRESSED_ALPHA:
221 case GL_COMPRESSED_LUMINANCE_ALPHA:
222 case GL_COMPRESSED_LUMINANCE:
223 case GL_COMPRESSED_INTENSITY:
224 case GL_COMPRESSED_RGB:
225 case GL_COMPRESSED_RGBA:
226 case GL_COMPRESSED_SRGB:
227 case GL_COMPRESSED_SRGB_ALPHA:
228 case GL_COMPRESSED_SLUMINANCE:
229 case GL_COMPRESSED_SLUMINANCE_ALPHA:
230 case GL_RED_INTEGER:
231 case GL_GREEN_INTEGER:
232 case GL_BLUE_INTEGER:
233 case GL_ALPHA_INTEGER:
234 case GL_RGB_INTEGER:
235 case GL_RGBA_INTEGER:
236 case GL_BGR_INTEGER:
237 case GL_BGRA_INTEGER:
238 case GL_LUMINANCE_INTEGER_EXT:
239 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
240 /* these unsized formats are illegal */
241 return GL_FALSE;
242 default:
243 return _mesa_base_tex_format(ctx, internalformat) > 0;
244 }
245 }
246
247
248 /**
249 * Default ctx->Driver.AllocTextureStorage() handler.
250 *
251 * The driver can override this with a more specific implementation if it
252 * desires, but this can be used to get the texture images allocated using the
253 * usual texture image handling code. The immutability of
254 * GL_ARB_texture_storage texture layouts is handled by texObj->Immutable
255 * checks at glTexImage* time.
256 */
257 GLboolean
258 _mesa_AllocTextureStorage_sw(struct gl_context *ctx,
259 struct gl_texture_object *texObj,
260 GLsizei levels, GLsizei width,
261 GLsizei height, GLsizei depth)
262 {
263 const int numFaces = _mesa_num_tex_faces(texObj->Target);
264 int face;
265 int level;
266
267 (void) width;
268 (void) height;
269 (void) depth;
270
271 for (face = 0; face < numFaces; face++) {
272 for (level = 0; level < levels; level++) {
273 struct gl_texture_image *const texImage = texObj->Image[face][level];
274 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage))
275 return GL_FALSE;
276 }
277 }
278
279 return GL_TRUE;
280 }
281
282
283 /**
284 * Do error checking for calls to glTexStorage1/2/3D().
285 * If an error is found, record it with _mesa_error(), unless the target
286 * is a proxy texture.
287 * \return GL_TRUE if any error, GL_FALSE otherwise.
288 */
289 static GLboolean
290 tex_storage_error_check(struct gl_context *ctx,
291 struct gl_texture_object *texObj,
292 GLuint dims, GLenum target,
293 GLsizei levels, GLenum internalformat,
294 GLsizei width, GLsizei height, GLsizei depth,
295 bool dsa)
296 {
297 const char* suffix = dsa ? "ture" : "";
298
299 /* Legal format checking has been moved to texstorage and texturestorage in
300 * order to allow meta functions to use legacy formats. */
301
302 /* size check */
303 if (!_mesa_valid_tex_storage_dim(width, height, depth)) {
304 _mesa_error(ctx, GL_INVALID_VALUE,
305 "glTex%sStorage%uD(width, height or depth < 1)",
306 suffix, dims);
307 return GL_TRUE;
308 }
309
310 if (_mesa_is_compressed_format(ctx, internalformat)) {
311 GLenum err;
312 if (!_mesa_target_can_be_compressed(ctx, target, internalformat, &err)) {
313 _mesa_error(ctx, err,
314 "glTex%sStorage%dD(internalformat = %s)", suffix, dims,
315 _mesa_enum_to_string(internalformat));
316 return GL_TRUE;
317 }
318 }
319
320 /* levels check */
321 if (levels < 1) {
322 _mesa_error(ctx, GL_INVALID_VALUE, "glTex%sStorage%uD(levels < 1)",
323 suffix, dims);
324 return GL_TRUE;
325 }
326
327 /* check levels against maximum (note different error than above) */
328 if (levels > (GLint) _mesa_max_texture_levels(ctx, target)) {
329 _mesa_error(ctx, GL_INVALID_OPERATION,
330 "glTex%sStorage%uD(levels too large)",
331 suffix, dims);
332 return GL_TRUE;
333 }
334
335 /* check levels against width/height/depth */
336 if (levels > _mesa_get_tex_max_num_levels(target, width, height, depth)) {
337 _mesa_error(ctx, GL_INVALID_OPERATION,
338 "glTex%sStorage%uD(too many levels"
339 " for max texture dimension)",
340 suffix, dims);
341 return GL_TRUE;
342 }
343
344 /* non-default texture object check */
345 if (!_mesa_is_proxy_texture(target) && (!texObj || (texObj->Name == 0))) {
346 _mesa_error(ctx, GL_INVALID_OPERATION,
347 "glTex%sStorage%uD(texture object 0)",
348 suffix, dims);
349 return GL_TRUE;
350 }
351
352 /* Check if texObj->Immutable is set */
353 if (!_mesa_is_proxy_texture(target) && texObj->Immutable) {
354 _mesa_error(ctx, GL_INVALID_OPERATION, "glTex%sStorage%uD(immutable)",
355 suffix, dims);
356 return GL_TRUE;
357 }
358
359 /* additional checks for depth textures */
360 if (!_mesa_legal_texture_base_format_for_target(ctx, target, internalformat,
361 dims, dsa ?
362 "glTextureStorage" :
363 "glTexStorage"))
364 return GL_TRUE;
365
366 return GL_FALSE;
367 }
368
369
370 /**
371 * Helper that does the storage allocation for _mesa_TexStorage1/2/3D()
372 * and _mesa_TextureStorage1/2/3D().
373 */
374 void
375 _mesa_texture_storage(struct gl_context *ctx, GLuint dims,
376 struct gl_texture_object *texObj,
377 GLenum target, GLsizei levels,
378 GLenum internalformat, GLsizei width,
379 GLsizei height, GLsizei depth, bool dsa)
380 {
381 GLboolean sizeOK, dimensionsOK;
382 mesa_format texFormat;
383 const char* suffix = dsa ? "ture" : "";
384
385 assert(texObj);
386
387 if (tex_storage_error_check(ctx, texObj, dims, target, levels,
388 internalformat, width, height, depth, dsa)) {
389 return; /* error was recorded */
390 }
391
392 texFormat = _mesa_choose_texture_format(ctx, texObj, target, 0,
393 internalformat, GL_NONE, GL_NONE);
394 assert(texFormat != MESA_FORMAT_NONE);
395
396 /* check that width, height, depth are legal for the mipmap level */
397 dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, 0,
398 width, height, depth, 0);
399
400 sizeOK = ctx->Driver.TestProxyTexImage(ctx, target, 0, texFormat,
401 width, height, depth, 0);
402
403 if (_mesa_is_proxy_texture(target)) {
404 if (dimensionsOK && sizeOK) {
405 initialize_texture_fields(ctx, texObj, levels, width, height, depth,
406 internalformat, texFormat);
407 }
408 else {
409 /* clear all image fields for [levels] */
410 clear_texture_fields(ctx, texObj);
411 }
412 }
413 else {
414 if (!dimensionsOK) {
415 _mesa_error(ctx, GL_INVALID_VALUE,
416 "glTex%sStorage%uD(invalid width, height or depth)",
417 suffix, dims);
418 return;
419 }
420
421 if (!sizeOK) {
422 _mesa_error(ctx, GL_OUT_OF_MEMORY,
423 "glTex%sStorage%uD(texture too large)",
424 suffix, dims);
425 }
426
427 assert(levels > 0);
428 assert(width > 0);
429 assert(height > 0);
430 assert(depth > 0);
431
432 if (!initialize_texture_fields(ctx, texObj, levels, width, height, depth,
433 internalformat, texFormat)) {
434 return;
435 }
436
437 /* Do actual texture memory allocation */
438 if (!ctx->Driver.AllocTextureStorage(ctx, texObj, levels,
439 width, height, depth)) {
440 /* Reset the texture images' info to zeros.
441 * Strictly speaking, we probably don't have to do this since
442 * generating GL_OUT_OF_MEMORY can leave things in an undefined
443 * state but this puts things in a consistent state.
444 */
445 clear_texture_fields(ctx, texObj);
446 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTex%sStorage%uD",
447 suffix, dims);
448 return;
449 }
450
451 _mesa_set_texture_view_state(ctx, texObj, target, levels);
452
453 update_fbo_texture(ctx, texObj);
454 }
455 }
456
457
458 /**
459 * Helper used by _mesa_TexStorage1/2/3D().
460 */
461 static void
462 texstorage(GLuint dims, GLenum target, GLsizei levels, GLenum internalformat,
463 GLsizei width, GLsizei height, GLsizei depth)
464 {
465 struct gl_texture_object *texObj;
466 GET_CURRENT_CONTEXT(ctx);
467
468 /* Check target. This is done here so that _mesa_texture_storage
469 * can receive unsized formats.
470 */
471 if (!legal_texobj_target(ctx, dims, target)) {
472 _mesa_error(ctx, GL_INVALID_ENUM,
473 "glTexStorage%uD(illegal target=%s)",
474 dims, _mesa_enum_to_string(target));
475 return;
476 }
477
478 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
479 _mesa_debug(ctx, "glTexStorage%uD %s %d %s %d %d %d\n",
480 dims,
481 _mesa_enum_to_string(target), levels,
482 _mesa_enum_to_string(internalformat),
483 width, height, depth);
484
485 /* Check the format to make sure it is sized. */
486 if (!_mesa_is_legal_tex_storage_format(ctx, internalformat)) {
487 _mesa_error(ctx, GL_INVALID_ENUM,
488 "glTexStorage%uD(internalformat = %s)", dims,
489 _mesa_enum_to_string(internalformat));
490 return;
491 }
492
493 texObj = _mesa_get_current_tex_object(ctx, target);
494 if (!texObj)
495 return;
496
497 _mesa_texture_storage(ctx, dims, texObj, target, levels,
498 internalformat, width, height, depth, false);
499 }
500
501
502 /**
503 * Helper used by _mesa_TextureStorage1/2/3D().
504 */
505 static void
506 texturestorage(GLuint dims, GLuint texture, GLsizei levels,
507 GLenum internalformat, GLsizei width, GLsizei height,
508 GLsizei depth)
509 {
510 struct gl_texture_object *texObj;
511 GET_CURRENT_CONTEXT(ctx);
512
513 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
514 _mesa_debug(ctx, "glTextureStorage%uD %d %d %s %d %d %d\n",
515 dims, texture, levels,
516 _mesa_enum_to_string(internalformat),
517 width, height, depth);
518
519 /* Check the format to make sure it is sized. */
520 if (!_mesa_is_legal_tex_storage_format(ctx, internalformat)) {
521 _mesa_error(ctx, GL_INVALID_ENUM,
522 "glTextureStorage%uD(internalformat = %s)", dims,
523 _mesa_enum_to_string(internalformat));
524 return;
525 }
526
527 /* Get the texture object by Name. */
528 texObj = _mesa_lookup_texture(ctx, texture);
529 if (!texObj) {
530 _mesa_error(ctx, GL_INVALID_OPERATION,
531 "glTextureStorage%uD(texture = %d)", dims, texture);
532 return;
533 }
534
535 /* Check target. This is done here so that _mesa_texture_storage
536 * can receive unsized formats.
537 */
538 if (!legal_texobj_target(ctx, dims, texObj->Target)) {
539 _mesa_error(ctx, GL_INVALID_ENUM,
540 "glTextureStorage%uD(illegal target=%s)",
541 dims, _mesa_enum_to_string(texObj->Target));
542 return;
543 }
544
545 _mesa_texture_storage(ctx, dims, texObj, texObj->Target,
546 levels, internalformat, width, height, depth, true);
547 }
548
549
550 void GLAPIENTRY
551 _mesa_TexStorage1D(GLenum target, GLsizei levels, GLenum internalformat,
552 GLsizei width)
553 {
554 texstorage(1, target, levels, internalformat, width, 1, 1);
555 }
556
557
558 void GLAPIENTRY
559 _mesa_TexStorage2D(GLenum target, GLsizei levels, GLenum internalformat,
560 GLsizei width, GLsizei height)
561 {
562 texstorage(2, target, levels, internalformat, width, height, 1);
563 }
564
565
566 void GLAPIENTRY
567 _mesa_TexStorage3D(GLenum target, GLsizei levels, GLenum internalformat,
568 GLsizei width, GLsizei height, GLsizei depth)
569 {
570 texstorage(3, target, levels, internalformat, width, height, depth);
571 }
572
573
574 void GLAPIENTRY
575 _mesa_TextureStorage1D(GLuint texture, GLsizei levels, GLenum internalformat,
576 GLsizei width)
577 {
578 texturestorage(1, texture, levels, internalformat, width, 1, 1);
579 }
580
581
582 void GLAPIENTRY
583 _mesa_TextureStorage2D(GLuint texture, GLsizei levels,
584 GLenum internalformat,
585 GLsizei width, GLsizei height)
586 {
587 texturestorage(2, texture, levels, internalformat, width, height, 1);
588 }
589
590
591 void GLAPIENTRY
592 _mesa_TextureStorage3D(GLuint texture, GLsizei levels, GLenum internalformat,
593 GLsizei width, GLsizei height, GLsizei depth)
594 {
595 texturestorage(3, texture, levels, internalformat, width, height, depth);
596 }
597
598
599 /*
600 * Note: we don't support GL_EXT_direct_state_access and the spec says
601 * we don't need the following functions. However, glew checks for the
602 * presence of all six functions and will say that GL_ARB_texture_storage
603 * is not supported if these functions are missing.
604 */
605
606
607 void GLAPIENTRY
608 _mesa_TextureStorage1DEXT(GLuint texture, GLenum target, GLsizei levels,
609 GLenum internalformat,
610 GLsizei width)
611 {
612 GET_CURRENT_CONTEXT(ctx);
613
614 (void) texture;
615 (void) target;
616 (void) levels;
617 (void) internalformat;
618 (void) width;
619
620 _mesa_error(ctx, GL_INVALID_OPERATION,
621 "glTextureStorage1DEXT not supported");
622 }
623
624
625 void GLAPIENTRY
626 _mesa_TextureStorage2DEXT(GLuint texture, GLenum target, GLsizei levels,
627 GLenum internalformat,
628 GLsizei width, GLsizei height)
629 {
630 GET_CURRENT_CONTEXT(ctx);
631
632 (void) texture;
633 (void) target;
634 (void) levels;
635 (void) internalformat;
636 (void) width;
637 (void) height;
638
639 _mesa_error(ctx, GL_INVALID_OPERATION,
640 "glTextureStorage2DEXT not supported");
641 }
642
643
644 void GLAPIENTRY
645 _mesa_TextureStorage3DEXT(GLuint texture, GLenum target, GLsizei levels,
646 GLenum internalformat,
647 GLsizei width, GLsizei height, GLsizei depth)
648 {
649 GET_CURRENT_CONTEXT(ctx);
650
651 (void) texture;
652 (void) target;
653 (void) levels;
654 (void) internalformat;
655 (void) width;
656 (void) height;
657 (void) depth;
658
659 _mesa_error(ctx, GL_INVALID_OPERATION,
660 "glTextureStorage3DEXT not supported");
661 }