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