Merge remote-tracking branch '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 * \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(const struct gl_context *ctx,
206 GLenum internalformat)
207 {
208 /* check internal format - note that only sized formats are allowed */
209 switch (internalformat) {
210 case GL_ALPHA:
211 case GL_LUMINANCE:
212 case GL_LUMINANCE_ALPHA:
213 case GL_INTENSITY:
214 case GL_RED:
215 case GL_RG:
216 case GL_RGB:
217 case GL_RGBA:
218 case GL_BGRA:
219 case GL_DEPTH_COMPONENT:
220 case GL_DEPTH_STENCIL:
221 case GL_COMPRESSED_ALPHA:
222 case GL_COMPRESSED_LUMINANCE_ALPHA:
223 case GL_COMPRESSED_LUMINANCE:
224 case GL_COMPRESSED_INTENSITY:
225 case GL_COMPRESSED_RGB:
226 case GL_COMPRESSED_RGBA:
227 case GL_COMPRESSED_SRGB:
228 case GL_COMPRESSED_SRGB_ALPHA:
229 case GL_COMPRESSED_SLUMINANCE:
230 case GL_COMPRESSED_SLUMINANCE_ALPHA:
231 case GL_RED_INTEGER:
232 case GL_GREEN_INTEGER:
233 case GL_BLUE_INTEGER:
234 case GL_ALPHA_INTEGER:
235 case GL_RGB_INTEGER:
236 case GL_RGBA_INTEGER:
237 case GL_BGR_INTEGER:
238 case GL_BGRA_INTEGER:
239 case GL_LUMINANCE_INTEGER_EXT:
240 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
241 /* these unsized formats are illegal */
242 return GL_FALSE;
243 default:
244 return _mesa_base_tex_format(ctx, internalformat) > 0;
245 }
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 _mesa_error(ctx, GL_INVALID_OPERATION, "glTex%sStorage%uD(bad target for texture)",
363 suffix, dims);
364 return GL_TRUE;
365 }
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 texFormat = _mesa_choose_texture_format(ctx, texObj, target, 0,
394 internalformat, GL_NONE, GL_NONE);
395 assert(texFormat != MESA_FORMAT_NONE);
396
397 /* check that width, height, depth are legal for the mipmap level */
398 dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, 0,
399 width, height, depth, 0);
400
401 sizeOK = ctx->Driver.TestProxyTexImage(ctx, target, 0, texFormat,
402 width, height, depth, 0);
403
404 if (_mesa_is_proxy_texture(target)) {
405 if (dimensionsOK && sizeOK) {
406 initialize_texture_fields(ctx, texObj, levels, width, height, depth,
407 internalformat, texFormat);
408 }
409 else {
410 /* clear all image fields for [levels] */
411 clear_texture_fields(ctx, texObj);
412 }
413 }
414 else {
415 if (!dimensionsOK) {
416 _mesa_error(ctx, GL_INVALID_VALUE,
417 "glTex%sStorage%uD(invalid width, height or depth)",
418 suffix, dims);
419 return;
420 }
421
422 if (!sizeOK) {
423 _mesa_error(ctx, GL_OUT_OF_MEMORY,
424 "glTex%sStorage%uD(texture too large)",
425 suffix, dims);
426 }
427
428 assert(levels > 0);
429 assert(width > 0);
430 assert(height > 0);
431 assert(depth > 0);
432
433 if (!initialize_texture_fields(ctx, texObj, levels, width, height, depth,
434 internalformat, texFormat)) {
435 return;
436 }
437
438 /* Do actual texture memory allocation */
439 if (!ctx->Driver.AllocTextureStorage(ctx, texObj, levels,
440 width, height, depth)) {
441 /* Reset the texture images' info to zeros.
442 * Strictly speaking, we probably don't have to do this since
443 * generating GL_OUT_OF_MEMORY can leave things in an undefined
444 * state but this puts things in a consistent state.
445 */
446 clear_texture_fields(ctx, texObj);
447 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTex%sStorage%uD",
448 suffix, dims);
449 return;
450 }
451
452 _mesa_set_texture_view_state(ctx, texObj, target, levels);
453
454 update_fbo_texture(ctx, texObj);
455 }
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 /* Check target. This is done here so that _mesa_texture_storage
470 * can receive unsized formats.
471 */
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
486 /* Check the format to make sure it is sized. */
487 if (!_mesa_is_legal_tex_storage_format(ctx, internalformat)) {
488 _mesa_error(ctx, GL_INVALID_ENUM,
489 "glTexStorage%uD(internalformat = %s)", dims,
490 _mesa_enum_to_string(internalformat));
491 return;
492 }
493
494 texObj = _mesa_get_current_tex_object(ctx, target);
495 if (!texObj)
496 return;
497
498 _mesa_texture_storage(ctx, dims, texObj, target, levels,
499 internalformat, width, height, depth, false);
500 }
501
502
503 /**
504 * Helper used by _mesa_TextureStorage1/2/3D().
505 */
506 static void
507 texturestorage(GLuint dims, GLuint texture, GLsizei levels,
508 GLenum internalformat, GLsizei width, GLsizei height,
509 GLsizei depth)
510 {
511 struct gl_texture_object *texObj;
512 GET_CURRENT_CONTEXT(ctx);
513
514 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
515 _mesa_debug(ctx, "glTextureStorage%uD %d %d %s %d %d %d\n",
516 dims, texture, levels,
517 _mesa_enum_to_string(internalformat),
518 width, height, depth);
519
520 /* Check the format to make sure it is sized. */
521 if (!_mesa_is_legal_tex_storage_format(ctx, internalformat)) {
522 _mesa_error(ctx, GL_INVALID_ENUM,
523 "glTextureStorage%uD(internalformat = %s)", dims,
524 _mesa_enum_to_string(internalformat));
525 return;
526 }
527
528 /* Get the texture object by Name. */
529 texObj = _mesa_lookup_texture(ctx, texture);
530 if (!texObj) {
531 _mesa_error(ctx, GL_INVALID_OPERATION,
532 "glTextureStorage%uD(texture = %d)", dims, texture);
533 return;
534 }
535
536 /* Check target. This is done here so that _mesa_texture_storage
537 * can receive unsized formats.
538 */
539 if (!legal_texobj_target(ctx, dims, texObj->Target)) {
540 _mesa_error(ctx, GL_INVALID_ENUM,
541 "glTextureStorage%uD(illegal target=%s)",
542 dims, _mesa_enum_to_string(texObj->Target));
543 return;
544 }
545
546 _mesa_texture_storage(ctx, dims, texObj, texObj->Target,
547 levels, internalformat, width, height, depth, true);
548 }
549
550
551 void GLAPIENTRY
552 _mesa_TexStorage1D(GLenum target, GLsizei levels, GLenum internalformat,
553 GLsizei width)
554 {
555 texstorage(1, target, levels, internalformat, width, 1, 1);
556 }
557
558
559 void GLAPIENTRY
560 _mesa_TexStorage2D(GLenum target, GLsizei levels, GLenum internalformat,
561 GLsizei width, GLsizei height)
562 {
563 texstorage(2, target, levels, internalformat, width, height, 1);
564 }
565
566
567 void GLAPIENTRY
568 _mesa_TexStorage3D(GLenum target, GLsizei levels, GLenum internalformat,
569 GLsizei width, GLsizei height, GLsizei depth)
570 {
571 texstorage(3, target, levels, internalformat, width, height, depth);
572 }
573
574
575 void GLAPIENTRY
576 _mesa_TextureStorage1D(GLuint texture, GLsizei levels, GLenum internalformat,
577 GLsizei width)
578 {
579 texturestorage(1, texture, levels, internalformat, width, 1, 1);
580 }
581
582
583 void GLAPIENTRY
584 _mesa_TextureStorage2D(GLuint texture, GLsizei levels,
585 GLenum internalformat,
586 GLsizei width, GLsizei height)
587 {
588 texturestorage(2, texture, levels, internalformat, width, height, 1);
589 }
590
591
592 void GLAPIENTRY
593 _mesa_TextureStorage3D(GLuint texture, GLsizei levels, GLenum internalformat,
594 GLsizei width, GLsizei height, GLsizei depth)
595 {
596 texturestorage(3, texture, levels, internalformat, width, height, depth);
597 }
598
599
600 /*
601 * Note: we don't support GL_EXT_direct_state_access and the spec says
602 * we don't need the following functions. However, glew checks for the
603 * presence of all six functions and will say that GL_ARB_texture_storage
604 * is not supported if these functions are missing.
605 */
606
607
608 void GLAPIENTRY
609 _mesa_TextureStorage1DEXT(GLuint texture, GLenum target, GLsizei levels,
610 GLenum internalformat,
611 GLsizei width)
612 {
613 GET_CURRENT_CONTEXT(ctx);
614
615 (void) texture;
616 (void) target;
617 (void) levels;
618 (void) internalformat;
619 (void) width;
620
621 _mesa_error(ctx, GL_INVALID_OPERATION,
622 "glTextureStorage1DEXT not supported");
623 }
624
625
626 void GLAPIENTRY
627 _mesa_TextureStorage2DEXT(GLuint texture, GLenum target, GLsizei levels,
628 GLenum internalformat,
629 GLsizei width, GLsizei height)
630 {
631 GET_CURRENT_CONTEXT(ctx);
632
633 (void) texture;
634 (void) target;
635 (void) levels;
636 (void) internalformat;
637 (void) width;
638 (void) height;
639
640 _mesa_error(ctx, GL_INVALID_OPERATION,
641 "glTextureStorage2DEXT not supported");
642 }
643
644
645 void GLAPIENTRY
646 _mesa_TextureStorage3DEXT(GLuint texture, GLenum target, GLsizei levels,
647 GLenum internalformat,
648 GLsizei width, GLsizei height, GLsizei depth)
649 {
650 GET_CURRENT_CONTEXT(ctx);
651
652 (void) texture;
653 (void) target;
654 (void) levels;
655 (void) internalformat;
656 (void) width;
657 (void) height;
658 (void) depth;
659
660 _mesa_error(ctx, GL_INVALID_OPERATION,
661 "glTextureStorage3DEXT not supported");
662 }