mesa: Fix indirect draw buffer size check on 32-bit systems.
[mesa.git] / src / mesa / main / teximage.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /**
28 * \file teximage.c
29 * Texture image-related functions.
30 */
31
32 #include <stdbool.h>
33 #include "glheader.h"
34 #include "bufferobj.h"
35 #include "context.h"
36 #include "enums.h"
37 #include "fbobject.h"
38 #include "framebuffer.h"
39 #include "hash.h"
40 #include "image.h"
41 #include "imports.h"
42 #include "macros.h"
43 #include "multisample.h"
44 #include "pixelstore.h"
45 #include "state.h"
46 #include "texcompress.h"
47 #include "texcompress_cpal.h"
48 #include "teximage.h"
49 #include "texobj.h"
50 #include "texstate.h"
51 #include "texstorage.h"
52 #include "textureview.h"
53 #include "mtypes.h"
54 #include "glformats.h"
55 #include "texstore.h"
56 #include "pbo.h"
57
58
59 /**
60 * State changes which we care about for glCopyTex[Sub]Image() calls.
61 * In particular, we care about pixel transfer state and buffer state
62 * (such as glReadBuffer to make sure we read from the right renderbuffer).
63 */
64 #define NEW_COPY_TEX_STATE (_NEW_BUFFERS | _NEW_PIXEL)
65
66 /**
67 * Returns a corresponding internal floating point format for a given base
68 * format as specifed by OES_texture_float. In case of GL_FLOAT, the internal
69 * format needs to be a 32 bit component and in case of GL_HALF_FLOAT_OES it
70 * needs to be a 16 bit component.
71 *
72 * For example, given base format GL_RGBA, type GL_Float return GL_RGBA32F_ARB.
73 */
74 static GLenum
75 adjust_for_oes_float_texture(GLenum format, GLenum type)
76 {
77 switch (type) {
78 case GL_FLOAT:
79 switch (format) {
80 case GL_RGBA:
81 return GL_RGBA32F;
82 case GL_RGB:
83 return GL_RGB32F;
84 case GL_ALPHA:
85 return GL_ALPHA32F_ARB;
86 case GL_LUMINANCE:
87 return GL_LUMINANCE32F_ARB;
88 case GL_LUMINANCE_ALPHA:
89 return GL_LUMINANCE_ALPHA32F_ARB;
90 default:
91 break;
92 }
93 break;
94
95 case GL_HALF_FLOAT_OES:
96 switch (format) {
97 case GL_RGBA:
98 return GL_RGBA16F;
99 case GL_RGB:
100 return GL_RGB16F;
101 case GL_ALPHA:
102 return GL_ALPHA16F_ARB;
103 case GL_LUMINANCE:
104 return GL_LUMINANCE16F_ARB;
105 case GL_LUMINANCE_ALPHA:
106 return GL_LUMINANCE_ALPHA16F_ARB;
107 default:
108 break;
109 }
110 break;
111
112 default:
113 break;
114 }
115
116 return format;
117 }
118
119
120 /**
121 * Install gl_texture_image in a gl_texture_object according to the target
122 * and level parameters.
123 *
124 * \param tObj texture object.
125 * \param target texture target.
126 * \param level image level.
127 * \param texImage texture image.
128 */
129 static void
130 set_tex_image(struct gl_texture_object *tObj,
131 GLenum target, GLint level,
132 struct gl_texture_image *texImage)
133 {
134 const GLuint face = _mesa_tex_target_to_face(target);
135
136 assert(tObj);
137 assert(texImage);
138 if (target == GL_TEXTURE_RECTANGLE_NV || target == GL_TEXTURE_EXTERNAL_OES)
139 assert(level == 0);
140
141 tObj->Image[face][level] = texImage;
142
143 /* Set the 'back' pointer */
144 texImage->TexObject = tObj;
145 texImage->Level = level;
146 texImage->Face = face;
147 }
148
149
150 /**
151 * Allocate a texture image structure.
152 *
153 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
154 * driver.
155 *
156 * \return a pointer to gl_texture_image struct with all fields initialized to
157 * zero.
158 */
159 struct gl_texture_image *
160 _mesa_new_texture_image( struct gl_context *ctx )
161 {
162 (void) ctx;
163 return CALLOC_STRUCT(gl_texture_image);
164 }
165
166
167 /**
168 * Free a gl_texture_image and associated data.
169 * This function is a fallback called via ctx->Driver.DeleteTextureImage().
170 *
171 * \param texImage texture image.
172 *
173 * Free the texture image structure and the associated image data.
174 */
175 void
176 _mesa_delete_texture_image(struct gl_context *ctx,
177 struct gl_texture_image *texImage)
178 {
179 /* Free texImage->Data and/or any other driver-specific texture
180 * image storage.
181 */
182 assert(ctx->Driver.FreeTextureImageBuffer);
183 ctx->Driver.FreeTextureImageBuffer( ctx, texImage );
184 free(texImage);
185 }
186
187
188 /**
189 * Test if a target is a proxy target.
190 *
191 * \param target texture target.
192 *
193 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
194 */
195 GLboolean
196 _mesa_is_proxy_texture(GLenum target)
197 {
198 unsigned i;
199 static const GLenum targets[] = {
200 GL_PROXY_TEXTURE_1D,
201 GL_PROXY_TEXTURE_2D,
202 GL_PROXY_TEXTURE_3D,
203 GL_PROXY_TEXTURE_CUBE_MAP,
204 GL_PROXY_TEXTURE_RECTANGLE,
205 GL_PROXY_TEXTURE_1D_ARRAY,
206 GL_PROXY_TEXTURE_2D_ARRAY,
207 GL_PROXY_TEXTURE_CUBE_MAP_ARRAY,
208 GL_PROXY_TEXTURE_2D_MULTISAMPLE,
209 GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
210 };
211 /*
212 * NUM_TEXTURE_TARGETS should match number of terms above, except there's no
213 * proxy for GL_TEXTURE_BUFFER and GL_TEXTURE_EXTERNAL_OES.
214 */
215 STATIC_ASSERT(NUM_TEXTURE_TARGETS == ARRAY_SIZE(targets) + 2);
216
217 for (i = 0; i < ARRAY_SIZE(targets); ++i)
218 if (target == targets[i])
219 return GL_TRUE;
220 return GL_FALSE;
221 }
222
223
224 /**
225 * Test if a target is an array target.
226 *
227 * \param target texture target.
228 *
229 * \return true if the target is an array target, false otherwise.
230 */
231 bool
232 _mesa_is_array_texture(GLenum target)
233 {
234 switch (target) {
235 case GL_TEXTURE_1D_ARRAY:
236 case GL_TEXTURE_2D_ARRAY:
237 case GL_TEXTURE_CUBE_MAP_ARRAY:
238 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
239 return true;
240 default:
241 return false;
242 };
243 }
244
245 /**
246 * Test if a target is a cube map.
247 *
248 * \param target texture target.
249 *
250 * \return true if the target is a cube map, false otherwise.
251 */
252 bool
253 _mesa_is_cube_map_texture(GLenum target)
254 {
255 switch(target) {
256 case GL_TEXTURE_CUBE_MAP:
257 case GL_TEXTURE_CUBE_MAP_ARRAY:
258 return true;
259 default:
260 return false;
261 }
262 }
263
264 /**
265 * Return the proxy target which corresponds to the given texture target
266 */
267 static GLenum
268 proxy_target(GLenum target)
269 {
270 switch (target) {
271 case GL_TEXTURE_1D:
272 case GL_PROXY_TEXTURE_1D:
273 return GL_PROXY_TEXTURE_1D;
274 case GL_TEXTURE_2D:
275 case GL_PROXY_TEXTURE_2D:
276 return GL_PROXY_TEXTURE_2D;
277 case GL_TEXTURE_3D:
278 case GL_PROXY_TEXTURE_3D:
279 return GL_PROXY_TEXTURE_3D;
280 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
281 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
282 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
283 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
284 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
285 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
286 case GL_TEXTURE_CUBE_MAP:
287 case GL_PROXY_TEXTURE_CUBE_MAP:
288 return GL_PROXY_TEXTURE_CUBE_MAP;
289 case GL_TEXTURE_RECTANGLE_NV:
290 case GL_PROXY_TEXTURE_RECTANGLE_NV:
291 return GL_PROXY_TEXTURE_RECTANGLE_NV;
292 case GL_TEXTURE_1D_ARRAY_EXT:
293 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
294 return GL_PROXY_TEXTURE_1D_ARRAY_EXT;
295 case GL_TEXTURE_2D_ARRAY_EXT:
296 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
297 return GL_PROXY_TEXTURE_2D_ARRAY_EXT;
298 case GL_TEXTURE_CUBE_MAP_ARRAY:
299 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
300 return GL_PROXY_TEXTURE_CUBE_MAP_ARRAY;
301 case GL_TEXTURE_2D_MULTISAMPLE:
302 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
303 return GL_PROXY_TEXTURE_2D_MULTISAMPLE;
304 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
305 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
306 return GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY;
307 default:
308 _mesa_problem(NULL, "unexpected target in proxy_target()");
309 return 0;
310 }
311 }
312
313
314
315
316 /**
317 * Get a texture image pointer from a texture object, given a texture
318 * target and mipmap level. The target and level parameters should
319 * have already been error-checked.
320 *
321 * \param texObj texture unit.
322 * \param target texture target.
323 * \param level image level.
324 *
325 * \return pointer to the texture image structure, or NULL on failure.
326 */
327 struct gl_texture_image *
328 _mesa_select_tex_image(const struct gl_texture_object *texObj,
329 GLenum target, GLint level)
330 {
331 const GLuint face = _mesa_tex_target_to_face(target);
332
333 assert(texObj);
334 assert(level >= 0);
335 assert(level < MAX_TEXTURE_LEVELS);
336
337 return texObj->Image[face][level];
338 }
339
340
341 /**
342 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
343 * it and install it. Only return NULL if passed a bad parameter or run
344 * out of memory.
345 */
346 struct gl_texture_image *
347 _mesa_get_tex_image(struct gl_context *ctx, struct gl_texture_object *texObj,
348 GLenum target, GLint level)
349 {
350 struct gl_texture_image *texImage;
351
352 if (!texObj)
353 return NULL;
354
355 texImage = _mesa_select_tex_image(texObj, target, level);
356 if (!texImage) {
357 texImage = ctx->Driver.NewTextureImage(ctx);
358 if (!texImage) {
359 _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture image allocation");
360 return NULL;
361 }
362
363 set_tex_image(texObj, target, level, texImage);
364 }
365
366 return texImage;
367 }
368
369
370 /**
371 * Return pointer to the specified proxy texture image.
372 * Note that proxy textures are per-context, not per-texture unit.
373 * \return pointer to texture image or NULL if invalid target, invalid
374 * level, or out of memory.
375 */
376 static struct gl_texture_image *
377 get_proxy_tex_image(struct gl_context *ctx, GLenum target, GLint level)
378 {
379 struct gl_texture_image *texImage;
380 GLuint texIndex;
381
382 if (level < 0)
383 return NULL;
384
385 switch (target) {
386 case GL_PROXY_TEXTURE_1D:
387 if (level >= ctx->Const.MaxTextureLevels)
388 return NULL;
389 texIndex = TEXTURE_1D_INDEX;
390 break;
391 case GL_PROXY_TEXTURE_2D:
392 if (level >= ctx->Const.MaxTextureLevels)
393 return NULL;
394 texIndex = TEXTURE_2D_INDEX;
395 break;
396 case GL_PROXY_TEXTURE_3D:
397 if (level >= ctx->Const.Max3DTextureLevels)
398 return NULL;
399 texIndex = TEXTURE_3D_INDEX;
400 break;
401 case GL_PROXY_TEXTURE_CUBE_MAP:
402 if (level >= ctx->Const.MaxCubeTextureLevels)
403 return NULL;
404 texIndex = TEXTURE_CUBE_INDEX;
405 break;
406 case GL_PROXY_TEXTURE_RECTANGLE_NV:
407 if (level > 0)
408 return NULL;
409 texIndex = TEXTURE_RECT_INDEX;
410 break;
411 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
412 if (level >= ctx->Const.MaxTextureLevels)
413 return NULL;
414 texIndex = TEXTURE_1D_ARRAY_INDEX;
415 break;
416 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
417 if (level >= ctx->Const.MaxTextureLevels)
418 return NULL;
419 texIndex = TEXTURE_2D_ARRAY_INDEX;
420 break;
421 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
422 if (level >= ctx->Const.MaxCubeTextureLevels)
423 return NULL;
424 texIndex = TEXTURE_CUBE_ARRAY_INDEX;
425 break;
426 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
427 if (level > 0)
428 return 0;
429 texIndex = TEXTURE_2D_MULTISAMPLE_INDEX;
430 break;
431 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
432 if (level > 0)
433 return 0;
434 texIndex = TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX;
435 break;
436 default:
437 return NULL;
438 }
439
440 texImage = ctx->Texture.ProxyTex[texIndex]->Image[0][level];
441 if (!texImage) {
442 texImage = ctx->Driver.NewTextureImage(ctx);
443 if (!texImage) {
444 _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
445 return NULL;
446 }
447 ctx->Texture.ProxyTex[texIndex]->Image[0][level] = texImage;
448 /* Set the 'back' pointer */
449 texImage->TexObject = ctx->Texture.ProxyTex[texIndex];
450 }
451 return texImage;
452 }
453
454
455 /**
456 * Get the maximum number of allowed mipmap levels.
457 *
458 * \param ctx GL context.
459 * \param target texture target.
460 *
461 * \return the maximum number of allowed mipmap levels for the given
462 * texture target, or zero if passed a bad target.
463 *
464 * \sa gl_constants.
465 */
466 GLint
467 _mesa_max_texture_levels(struct gl_context *ctx, GLenum target)
468 {
469 switch (target) {
470 case GL_TEXTURE_1D:
471 case GL_PROXY_TEXTURE_1D:
472 case GL_TEXTURE_2D:
473 case GL_PROXY_TEXTURE_2D:
474 return ctx->Const.MaxTextureLevels;
475 case GL_TEXTURE_3D:
476 case GL_PROXY_TEXTURE_3D:
477 return ctx->Const.Max3DTextureLevels;
478 case GL_TEXTURE_CUBE_MAP:
479 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
480 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
481 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
482 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
483 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
484 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
485 case GL_PROXY_TEXTURE_CUBE_MAP:
486 return ctx->Extensions.ARB_texture_cube_map
487 ? ctx->Const.MaxCubeTextureLevels : 0;
488 case GL_TEXTURE_RECTANGLE_NV:
489 case GL_PROXY_TEXTURE_RECTANGLE_NV:
490 return ctx->Extensions.NV_texture_rectangle ? 1 : 0;
491 case GL_TEXTURE_1D_ARRAY_EXT:
492 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
493 case GL_TEXTURE_2D_ARRAY_EXT:
494 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
495 return ctx->Extensions.EXT_texture_array
496 ? ctx->Const.MaxTextureLevels : 0;
497 case GL_TEXTURE_CUBE_MAP_ARRAY:
498 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
499 return ctx->Extensions.ARB_texture_cube_map_array
500 ? ctx->Const.MaxCubeTextureLevels : 0;
501 case GL_TEXTURE_BUFFER:
502 return (_mesa_has_ARB_texture_buffer_object(ctx) ||
503 _mesa_has_OES_texture_buffer(ctx)) ? 1 : 0;
504 case GL_TEXTURE_2D_MULTISAMPLE:
505 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
506 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
507 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
508 return (_mesa_is_desktop_gl(ctx) || _mesa_is_gles31(ctx))
509 && ctx->Extensions.ARB_texture_multisample
510 ? 1 : 0;
511 case GL_TEXTURE_EXTERNAL_OES:
512 /* fall-through */
513 default:
514 return 0; /* bad target */
515 }
516 }
517
518
519 /**
520 * Return number of dimensions per mipmap level for the given texture target.
521 */
522 GLint
523 _mesa_get_texture_dimensions(GLenum target)
524 {
525 switch (target) {
526 case GL_TEXTURE_1D:
527 case GL_PROXY_TEXTURE_1D:
528 return 1;
529 case GL_TEXTURE_2D:
530 case GL_TEXTURE_RECTANGLE:
531 case GL_TEXTURE_CUBE_MAP:
532 case GL_PROXY_TEXTURE_2D:
533 case GL_PROXY_TEXTURE_RECTANGLE:
534 case GL_PROXY_TEXTURE_CUBE_MAP:
535 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
536 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
537 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
538 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
539 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
540 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
541 case GL_TEXTURE_1D_ARRAY:
542 case GL_PROXY_TEXTURE_1D_ARRAY:
543 case GL_TEXTURE_EXTERNAL_OES:
544 case GL_TEXTURE_2D_MULTISAMPLE:
545 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
546 return 2;
547 case GL_TEXTURE_3D:
548 case GL_PROXY_TEXTURE_3D:
549 case GL_TEXTURE_2D_ARRAY:
550 case GL_PROXY_TEXTURE_2D_ARRAY:
551 case GL_TEXTURE_CUBE_MAP_ARRAY:
552 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
553 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
554 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
555 return 3;
556 case GL_TEXTURE_BUFFER:
557 /* fall-through */
558 default:
559 _mesa_problem(NULL, "invalid target 0x%x in get_texture_dimensions()",
560 target);
561 return 2;
562 }
563 }
564
565
566 /**
567 * Check if a texture target can have more than one layer.
568 */
569 GLboolean
570 _mesa_tex_target_is_layered(GLenum target)
571 {
572 switch (target) {
573 case GL_TEXTURE_1D:
574 case GL_PROXY_TEXTURE_1D:
575 case GL_TEXTURE_2D:
576 case GL_PROXY_TEXTURE_2D:
577 case GL_TEXTURE_RECTANGLE:
578 case GL_PROXY_TEXTURE_RECTANGLE:
579 case GL_TEXTURE_2D_MULTISAMPLE:
580 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
581 case GL_TEXTURE_BUFFER:
582 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
583 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
584 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
585 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
586 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
587 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
588 case GL_TEXTURE_EXTERNAL_OES:
589 return GL_FALSE;
590
591 case GL_TEXTURE_3D:
592 case GL_PROXY_TEXTURE_3D:
593 case GL_TEXTURE_CUBE_MAP:
594 case GL_PROXY_TEXTURE_CUBE_MAP:
595 case GL_TEXTURE_1D_ARRAY:
596 case GL_PROXY_TEXTURE_1D_ARRAY:
597 case GL_TEXTURE_2D_ARRAY:
598 case GL_PROXY_TEXTURE_2D_ARRAY:
599 case GL_TEXTURE_CUBE_MAP_ARRAY:
600 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
601 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
602 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
603 return GL_TRUE;
604
605 default:
606 assert(!"Invalid texture target.");
607 return GL_FALSE;
608 }
609 }
610
611
612 /**
613 * Return the number of layers present in the given level of an array,
614 * cubemap or 3D texture. If the texture is not layered return zero.
615 */
616 GLuint
617 _mesa_get_texture_layers(const struct gl_texture_object *texObj, GLint level)
618 {
619 assert(level >= 0 && level < MAX_TEXTURE_LEVELS);
620
621 switch (texObj->Target) {
622 case GL_TEXTURE_1D:
623 case GL_TEXTURE_2D:
624 case GL_TEXTURE_RECTANGLE:
625 case GL_TEXTURE_2D_MULTISAMPLE:
626 case GL_TEXTURE_BUFFER:
627 case GL_TEXTURE_EXTERNAL_OES:
628 return 0;
629
630 case GL_TEXTURE_CUBE_MAP:
631 return 6;
632
633 case GL_TEXTURE_1D_ARRAY: {
634 struct gl_texture_image *img = texObj->Image[0][level];
635 return img ? img->Height : 0;
636 }
637
638 case GL_TEXTURE_3D:
639 case GL_TEXTURE_2D_ARRAY:
640 case GL_TEXTURE_CUBE_MAP_ARRAY:
641 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY: {
642 struct gl_texture_image *img = texObj->Image[0][level];
643 return img ? img->Depth : 0;
644 }
645
646 default:
647 assert(!"Invalid texture target.");
648 return 0;
649 }
650 }
651
652
653 /**
654 * Return the maximum number of mipmap levels for the given target
655 * and the dimensions.
656 * The dimensions are expected not to include the border.
657 */
658 GLsizei
659 _mesa_get_tex_max_num_levels(GLenum target, GLsizei width, GLsizei height,
660 GLsizei depth)
661 {
662 GLsizei size;
663
664 switch (target) {
665 case GL_TEXTURE_1D:
666 case GL_TEXTURE_1D_ARRAY:
667 case GL_PROXY_TEXTURE_1D:
668 case GL_PROXY_TEXTURE_1D_ARRAY:
669 size = width;
670 break;
671 case GL_TEXTURE_CUBE_MAP:
672 case GL_TEXTURE_CUBE_MAP_ARRAY:
673 case GL_PROXY_TEXTURE_CUBE_MAP:
674 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
675 size = width;
676 break;
677 case GL_TEXTURE_2D:
678 case GL_TEXTURE_2D_ARRAY:
679 case GL_PROXY_TEXTURE_2D:
680 case GL_PROXY_TEXTURE_2D_ARRAY:
681 size = MAX2(width, height);
682 break;
683 case GL_TEXTURE_3D:
684 case GL_PROXY_TEXTURE_3D:
685 size = MAX3(width, height, depth);
686 break;
687 case GL_TEXTURE_RECTANGLE:
688 case GL_TEXTURE_EXTERNAL_OES:
689 case GL_TEXTURE_2D_MULTISAMPLE:
690 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
691 case GL_PROXY_TEXTURE_RECTANGLE:
692 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
693 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
694 return 1;
695 default:
696 assert(0);
697 return 1;
698 }
699
700 return _mesa_logbase2(size) + 1;
701 }
702
703
704 #if 000 /* not used anymore */
705 /*
706 * glTexImage[123]D can accept a NULL image pointer. In this case we
707 * create a texture image with unspecified image contents per the OpenGL
708 * spec.
709 */
710 static GLubyte *
711 make_null_texture(GLint width, GLint height, GLint depth, GLenum format)
712 {
713 const GLint components = _mesa_components_in_format(format);
714 const GLint numPixels = width * height * depth;
715 GLubyte *data = (GLubyte *) malloc(numPixels * components * sizeof(GLubyte));
716
717 #ifdef DEBUG
718 /*
719 * Let's see if anyone finds this. If glTexImage2D() is called with
720 * a NULL image pointer then load the texture image with something
721 * interesting instead of leaving it indeterminate.
722 */
723 if (data) {
724 static const char message[8][32] = {
725 " X X XXXXX XXX X ",
726 " XX XX X X X X X ",
727 " X X X X X X X ",
728 " X X XXXX XXX XXXXX ",
729 " X X X X X X ",
730 " X X X X X X X ",
731 " X X XXXXX XXX X X ",
732 " "
733 };
734
735 GLubyte *imgPtr = data;
736 GLint h, i, j, k;
737 for (h = 0; h < depth; h++) {
738 for (i = 0; i < height; i++) {
739 GLint srcRow = 7 - (i % 8);
740 for (j = 0; j < width; j++) {
741 GLint srcCol = j % 32;
742 GLubyte texel = (message[srcRow][srcCol]=='X') ? 255 : 70;
743 for (k = 0; k < components; k++) {
744 *imgPtr++ = texel;
745 }
746 }
747 }
748 }
749 }
750 #endif
751
752 return data;
753 }
754 #endif
755
756
757
758 /**
759 * Set the size and format-related fields of a gl_texture_image struct
760 * to zero. This is used when a proxy texture test fails.
761 */
762 static void
763 clear_teximage_fields(struct gl_texture_image *img)
764 {
765 assert(img);
766 img->_BaseFormat = 0;
767 img->InternalFormat = 0;
768 img->Border = 0;
769 img->Width = 0;
770 img->Height = 0;
771 img->Depth = 0;
772 img->Width2 = 0;
773 img->Height2 = 0;
774 img->Depth2 = 0;
775 img->WidthLog2 = 0;
776 img->HeightLog2 = 0;
777 img->DepthLog2 = 0;
778 img->TexFormat = MESA_FORMAT_NONE;
779 img->NumSamples = 0;
780 img->FixedSampleLocations = GL_TRUE;
781 }
782
783
784 /**
785 * Initialize basic fields of the gl_texture_image struct.
786 *
787 * \param ctx GL context.
788 * \param img texture image structure to be initialized.
789 * \param width image width.
790 * \param height image height.
791 * \param depth image depth.
792 * \param border image border.
793 * \param internalFormat internal format.
794 * \param format the actual hardware format (one of MESA_FORMAT_*)
795 * \param numSamples number of samples per texel, or zero for non-MS.
796 * \param fixedSampleLocations are sample locations fixed?
797 *
798 * Fills in the fields of \p img with the given information.
799 * Note: width, height and depth include the border.
800 */
801 static void
802 init_teximage_fields_ms(struct gl_context *ctx,
803 struct gl_texture_image *img,
804 GLsizei width, GLsizei height, GLsizei depth,
805 GLint border, GLenum internalFormat,
806 mesa_format format,
807 GLuint numSamples, GLboolean fixedSampleLocations)
808 {
809 GLenum target;
810 assert(img);
811 assert(width >= 0);
812 assert(height >= 0);
813 assert(depth >= 0);
814
815 target = img->TexObject->Target;
816 img->_BaseFormat = _mesa_base_tex_format( ctx, internalFormat );
817 assert(img->_BaseFormat != -1);
818 img->InternalFormat = internalFormat;
819 img->Border = border;
820 img->Width = width;
821 img->Height = height;
822 img->Depth = depth;
823
824 img->Width2 = width - 2 * border; /* == 1 << img->WidthLog2; */
825 img->WidthLog2 = _mesa_logbase2(img->Width2);
826
827 img->NumSamples = 0;
828 img->FixedSampleLocations = GL_TRUE;
829
830 switch(target) {
831 case GL_TEXTURE_1D:
832 case GL_TEXTURE_BUFFER:
833 case GL_PROXY_TEXTURE_1D:
834 if (height == 0)
835 img->Height2 = 0;
836 else
837 img->Height2 = 1;
838 img->HeightLog2 = 0;
839 if (depth == 0)
840 img->Depth2 = 0;
841 else
842 img->Depth2 = 1;
843 img->DepthLog2 = 0;
844 break;
845 case GL_TEXTURE_1D_ARRAY:
846 case GL_PROXY_TEXTURE_1D_ARRAY:
847 img->Height2 = height; /* no border */
848 img->HeightLog2 = 0; /* not used */
849 if (depth == 0)
850 img->Depth2 = 0;
851 else
852 img->Depth2 = 1;
853 img->DepthLog2 = 0;
854 break;
855 case GL_TEXTURE_2D:
856 case GL_TEXTURE_RECTANGLE:
857 case GL_TEXTURE_CUBE_MAP:
858 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
859 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
860 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
861 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
862 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
863 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
864 case GL_TEXTURE_EXTERNAL_OES:
865 case GL_PROXY_TEXTURE_2D:
866 case GL_PROXY_TEXTURE_RECTANGLE:
867 case GL_PROXY_TEXTURE_CUBE_MAP:
868 case GL_TEXTURE_2D_MULTISAMPLE:
869 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
870 img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
871 img->HeightLog2 = _mesa_logbase2(img->Height2);
872 if (depth == 0)
873 img->Depth2 = 0;
874 else
875 img->Depth2 = 1;
876 img->DepthLog2 = 0;
877 break;
878 case GL_TEXTURE_2D_ARRAY:
879 case GL_PROXY_TEXTURE_2D_ARRAY:
880 case GL_TEXTURE_CUBE_MAP_ARRAY:
881 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
882 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
883 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
884 img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
885 img->HeightLog2 = _mesa_logbase2(img->Height2);
886 img->Depth2 = depth; /* no border */
887 img->DepthLog2 = 0; /* not used */
888 break;
889 case GL_TEXTURE_3D:
890 case GL_PROXY_TEXTURE_3D:
891 img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
892 img->HeightLog2 = _mesa_logbase2(img->Height2);
893 img->Depth2 = depth - 2 * border; /* == 1 << img->DepthLog2; */
894 img->DepthLog2 = _mesa_logbase2(img->Depth2);
895 break;
896 default:
897 _mesa_problem(NULL, "invalid target 0x%x in _mesa_init_teximage_fields()",
898 target);
899 }
900
901 img->MaxNumLevels =
902 _mesa_get_tex_max_num_levels(target,
903 img->Width2, img->Height2, img->Depth2);
904 img->TexFormat = format;
905 img->NumSamples = numSamples;
906 img->FixedSampleLocations = fixedSampleLocations;
907 }
908
909
910 void
911 _mesa_init_teximage_fields(struct gl_context *ctx,
912 struct gl_texture_image *img,
913 GLsizei width, GLsizei height, GLsizei depth,
914 GLint border, GLenum internalFormat,
915 mesa_format format)
916 {
917 init_teximage_fields_ms(ctx, img, width, height, depth, border,
918 internalFormat, format, 0, GL_TRUE);
919 }
920
921
922 /**
923 * Free and clear fields of the gl_texture_image struct.
924 *
925 * \param ctx GL context.
926 * \param texImage texture image structure to be cleared.
927 *
928 * After the call, \p texImage will have no data associated with it. Its
929 * fields are cleared so that its parent object will test incomplete.
930 */
931 void
932 _mesa_clear_texture_image(struct gl_context *ctx,
933 struct gl_texture_image *texImage)
934 {
935 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
936 clear_teximage_fields(texImage);
937 }
938
939
940 /**
941 * Check the width, height, depth and border of a texture image are legal.
942 * Used by all the glTexImage, glCompressedTexImage and glCopyTexImage
943 * functions.
944 * The target and level parameters will have already been validated.
945 * \return GL_TRUE if size is OK, GL_FALSE otherwise.
946 */
947 GLboolean
948 _mesa_legal_texture_dimensions(struct gl_context *ctx, GLenum target,
949 GLint level, GLint width, GLint height,
950 GLint depth, GLint border)
951 {
952 GLint maxSize;
953
954 switch (target) {
955 case GL_TEXTURE_1D:
956 case GL_PROXY_TEXTURE_1D:
957 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1); /* level zero size */
958 maxSize >>= level; /* level size */
959 if (width < 2 * border || width > 2 * border + maxSize)
960 return GL_FALSE;
961 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
962 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
963 return GL_FALSE;
964 }
965 return GL_TRUE;
966
967 case GL_TEXTURE_2D:
968 case GL_PROXY_TEXTURE_2D:
969 case GL_TEXTURE_2D_MULTISAMPLE:
970 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
971 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
972 maxSize >>= level;
973 if (width < 2 * border || width > 2 * border + maxSize)
974 return GL_FALSE;
975 if (height < 2 * border || height > 2 * border + maxSize)
976 return GL_FALSE;
977 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
978 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
979 return GL_FALSE;
980 if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
981 return GL_FALSE;
982 }
983 return GL_TRUE;
984
985 case GL_TEXTURE_3D:
986 case GL_PROXY_TEXTURE_3D:
987 maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
988 maxSize >>= level;
989 if (width < 2 * border || width > 2 * border + maxSize)
990 return GL_FALSE;
991 if (height < 2 * border || height > 2 * border + maxSize)
992 return GL_FALSE;
993 if (depth < 2 * border || depth > 2 * border + maxSize)
994 return GL_FALSE;
995 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
996 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
997 return GL_FALSE;
998 if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
999 return GL_FALSE;
1000 if (depth > 0 && !_mesa_is_pow_two(depth - 2 * border))
1001 return GL_FALSE;
1002 }
1003 return GL_TRUE;
1004
1005 case GL_TEXTURE_RECTANGLE_NV:
1006 case GL_PROXY_TEXTURE_RECTANGLE_NV:
1007 if (level != 0)
1008 return GL_FALSE;
1009 maxSize = ctx->Const.MaxTextureRectSize;
1010 if (width < 0 || width > maxSize)
1011 return GL_FALSE;
1012 if (height < 0 || height > maxSize)
1013 return GL_FALSE;
1014 return GL_TRUE;
1015
1016 case GL_TEXTURE_CUBE_MAP:
1017 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1018 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1019 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1020 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1021 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1022 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1023 case GL_PROXY_TEXTURE_CUBE_MAP:
1024 maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
1025 maxSize >>= level;
1026 if (width != height)
1027 return GL_FALSE;
1028 if (width < 2 * border || width > 2 * border + maxSize)
1029 return GL_FALSE;
1030 if (height < 2 * border || height > 2 * border + maxSize)
1031 return GL_FALSE;
1032 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1033 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1034 return GL_FALSE;
1035 if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1036 return GL_FALSE;
1037 }
1038 return GL_TRUE;
1039
1040 case GL_TEXTURE_1D_ARRAY_EXT:
1041 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1042 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1043 maxSize >>= level;
1044 if (width < 2 * border || width > 2 * border + maxSize)
1045 return GL_FALSE;
1046 if (height < 0 || height > ctx->Const.MaxArrayTextureLayers)
1047 return GL_FALSE;
1048 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1049 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1050 return GL_FALSE;
1051 }
1052 return GL_TRUE;
1053
1054 case GL_TEXTURE_2D_ARRAY_EXT:
1055 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1056 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1057 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
1058 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1059 maxSize >>= level;
1060 if (width < 2 * border || width > 2 * border + maxSize)
1061 return GL_FALSE;
1062 if (height < 2 * border || height > 2 * border + maxSize)
1063 return GL_FALSE;
1064 if (depth < 0 || depth > ctx->Const.MaxArrayTextureLayers)
1065 return GL_FALSE;
1066 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1067 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1068 return GL_FALSE;
1069 if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1070 return GL_FALSE;
1071 }
1072 return GL_TRUE;
1073
1074 case GL_TEXTURE_CUBE_MAP_ARRAY:
1075 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1076 maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
1077 if (width < 2 * border || width > 2 * border + maxSize)
1078 return GL_FALSE;
1079 if (height < 2 * border || height > 2 * border + maxSize)
1080 return GL_FALSE;
1081 if (depth < 0 || depth > ctx->Const.MaxArrayTextureLayers || depth % 6)
1082 return GL_FALSE;
1083 if (width != height)
1084 return GL_FALSE;
1085 if (level >= ctx->Const.MaxCubeTextureLevels)
1086 return GL_FALSE;
1087 if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1088 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
1089 return GL_FALSE;
1090 if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
1091 return GL_FALSE;
1092 }
1093 return GL_TRUE;
1094 default:
1095 _mesa_problem(ctx, "Invalid target in _mesa_legal_texture_dimensions()");
1096 return GL_FALSE;
1097 }
1098 }
1099
1100
1101 /**
1102 * Do error checking of xoffset, yoffset, zoffset, width, height and depth
1103 * for glTexSubImage, glCopyTexSubImage and glCompressedTexSubImage.
1104 * \param destImage the destination texture image.
1105 * \return GL_TRUE if error found, GL_FALSE otherwise.
1106 */
1107 static GLboolean
1108 error_check_subtexture_dimensions(struct gl_context *ctx, GLuint dims,
1109 const struct gl_texture_image *destImage,
1110 GLint xoffset, GLint yoffset, GLint zoffset,
1111 GLsizei subWidth, GLsizei subHeight,
1112 GLsizei subDepth, const char *func)
1113 {
1114 const GLenum target = destImage->TexObject->Target;
1115 GLuint bw, bh;
1116
1117 /* Check size */
1118 if (subWidth < 0) {
1119 _mesa_error(ctx, GL_INVALID_VALUE,
1120 "%s(width=%d)", func, subWidth);
1121 return GL_TRUE;
1122 }
1123
1124 if (dims > 1 && subHeight < 0) {
1125 _mesa_error(ctx, GL_INVALID_VALUE,
1126 "%s(height=%d)", func, subHeight);
1127 return GL_TRUE;
1128 }
1129
1130 if (dims > 2 && subDepth < 0) {
1131 _mesa_error(ctx, GL_INVALID_VALUE,
1132 "%s(depth=%d)", func, subDepth);
1133 return GL_TRUE;
1134 }
1135
1136 /* check xoffset and width */
1137 if (xoffset < - (GLint) destImage->Border) {
1138 _mesa_error(ctx, GL_INVALID_VALUE, "%s(xoffset)", func);
1139 return GL_TRUE;
1140 }
1141
1142 if (xoffset + subWidth > (GLint) destImage->Width) {
1143 _mesa_error(ctx, GL_INVALID_VALUE, "%s(xoffset+width)", func);
1144 return GL_TRUE;
1145 }
1146
1147 /* check yoffset and height */
1148 if (dims > 1) {
1149 GLint yBorder = (target == GL_TEXTURE_1D_ARRAY) ? 0 : destImage->Border;
1150 if (yoffset < -yBorder) {
1151 _mesa_error(ctx, GL_INVALID_VALUE, "%s(yoffset)", func);
1152 return GL_TRUE;
1153 }
1154 if (yoffset + subHeight > (GLint) destImage->Height) {
1155 _mesa_error(ctx, GL_INVALID_VALUE, "%s(yoffset+height)", func);
1156 return GL_TRUE;
1157 }
1158 }
1159
1160 /* check zoffset and depth */
1161 if (dims > 2) {
1162 GLint depth;
1163 GLint zBorder = (target == GL_TEXTURE_2D_ARRAY ||
1164 target == GL_TEXTURE_CUBE_MAP_ARRAY) ?
1165 0 : destImage->Border;
1166
1167 if (zoffset < -zBorder) {
1168 _mesa_error(ctx, GL_INVALID_VALUE, "%s(zoffset)", func);
1169 return GL_TRUE;
1170 }
1171
1172 depth = (GLint) destImage->Depth;
1173 if (target == GL_TEXTURE_CUBE_MAP)
1174 depth = 6;
1175 if (zoffset + subDepth > depth) {
1176 _mesa_error(ctx, GL_INVALID_VALUE, "%s(zoffset+depth)", func);
1177 return GL_TRUE;
1178 }
1179 }
1180
1181 /*
1182 * The OpenGL spec (and GL_ARB_texture_compression) says only whole
1183 * compressed texture images can be updated. But, that restriction may be
1184 * relaxed for particular compressed formats. At this time, all the
1185 * compressed formats supported by Mesa allow sub-textures to be updated
1186 * along compressed block boundaries.
1187 */
1188 _mesa_get_format_block_size(destImage->TexFormat, &bw, &bh);
1189
1190 if (bw != 1 || bh != 1) {
1191 /* offset must be multiple of block size */
1192 if ((xoffset % bw != 0) || (yoffset % bh != 0)) {
1193 _mesa_error(ctx, GL_INVALID_OPERATION,
1194 "%s(xoffset = %d, yoffset = %d)",
1195 func, xoffset, yoffset);
1196 return GL_TRUE;
1197 }
1198
1199 /* The size must be a multiple of bw x bh, or we must be using a
1200 * offset+size that exactly hits the edge of the image. This
1201 * is important for small mipmap levels (1x1, 2x1, etc) and for
1202 * NPOT textures.
1203 */
1204 if ((subWidth % bw != 0) &&
1205 (xoffset + subWidth != (GLint) destImage->Width)) {
1206 _mesa_error(ctx, GL_INVALID_OPERATION,
1207 "%s(width = %d)", func, subWidth);
1208 return GL_TRUE;
1209 }
1210
1211 if ((subHeight % bh != 0) &&
1212 (yoffset + subHeight != (GLint) destImage->Height)) {
1213 _mesa_error(ctx, GL_INVALID_OPERATION,
1214 "%s(height = %d)", func, subHeight);
1215 return GL_TRUE;
1216 }
1217 }
1218
1219 return GL_FALSE;
1220 }
1221
1222
1223
1224
1225 /**
1226 * This is the fallback for Driver.TestProxyTexImage() for doing device-
1227 * specific texture image size checks.
1228 *
1229 * A hardware driver might override this function if, for example, the
1230 * max 3D texture size is 512x512x64 (i.e. not a cube).
1231 *
1232 * Note that width, height, depth == 0 is not an error. However, a
1233 * texture with zero width/height/depth will be considered "incomplete"
1234 * and texturing will effectively be disabled.
1235 *
1236 * \param target any texture target/type
1237 * \param level as passed to glTexImage
1238 * \param format the MESA_FORMAT_x for the tex image
1239 * \param width as passed to glTexImage
1240 * \param height as passed to glTexImage
1241 * \param depth as passed to glTexImage
1242 * \param border as passed to glTexImage
1243 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1244 */
1245 GLboolean
1246 _mesa_test_proxy_teximage(struct gl_context *ctx, GLenum target, GLint level,
1247 mesa_format format,
1248 GLint width, GLint height, GLint depth, GLint border)
1249 {
1250 /* We just check if the image size is less than MaxTextureMbytes.
1251 * Some drivers may do more specific checks.
1252 */
1253 uint64_t bytes = _mesa_format_image_size64(format, width, height, depth);
1254 uint64_t mbytes = bytes / (1024 * 1024); /* convert to MB */
1255 mbytes *= _mesa_num_tex_faces(target);
1256 return mbytes <= (uint64_t) ctx->Const.MaxTextureMbytes;
1257 }
1258
1259
1260 /**
1261 * Return true if the format is only valid for glCompressedTexImage.
1262 */
1263 static bool
1264 compressedteximage_only_format(const struct gl_context *ctx, GLenum format)
1265 {
1266 switch (format) {
1267 case GL_ETC1_RGB8_OES:
1268 case GL_PALETTE4_RGB8_OES:
1269 case GL_PALETTE4_RGBA8_OES:
1270 case GL_PALETTE4_R5_G6_B5_OES:
1271 case GL_PALETTE4_RGBA4_OES:
1272 case GL_PALETTE4_RGB5_A1_OES:
1273 case GL_PALETTE8_RGB8_OES:
1274 case GL_PALETTE8_RGBA8_OES:
1275 case GL_PALETTE8_R5_G6_B5_OES:
1276 case GL_PALETTE8_RGBA4_OES:
1277 case GL_PALETTE8_RGB5_A1_OES:
1278 return true;
1279 default:
1280 return false;
1281 }
1282 }
1283
1284 /**
1285 * Return true if the format doesn't support online compression.
1286 */
1287 bool
1288 _mesa_format_no_online_compression(const struct gl_context *ctx, GLenum format)
1289 {
1290 return _mesa_is_astc_format(format) ||
1291 compressedteximage_only_format(ctx, format);
1292 }
1293
1294 /* Writes to an GL error pointer if non-null and returns whether or not the
1295 * error is GL_NO_ERROR */
1296 static bool
1297 write_error(GLenum *err_ptr, GLenum error)
1298 {
1299 if (err_ptr)
1300 *err_ptr = error;
1301
1302 return error == GL_NO_ERROR;
1303 }
1304
1305 /**
1306 * Helper function to determine whether a target and specific compression
1307 * format are supported. The error parameter returns GL_NO_ERROR if the
1308 * target can be compressed. Otherwise it returns either GL_INVALID_OPERATION
1309 * or GL_INVALID_ENUM, whichever is more appropriate.
1310 */
1311 GLboolean
1312 _mesa_target_can_be_compressed(const struct gl_context *ctx, GLenum target,
1313 GLenum intFormat, GLenum *error)
1314 {
1315 GLboolean target_can_be_compresed = GL_FALSE;
1316 mesa_format format = _mesa_glenum_to_compressed_format(intFormat);
1317 enum mesa_format_layout layout = _mesa_get_format_layout(format);
1318
1319 switch (target) {
1320 case GL_TEXTURE_2D:
1321 case GL_PROXY_TEXTURE_2D:
1322 target_can_be_compresed = GL_TRUE; /* true for any compressed format so far */
1323 break;
1324 case GL_PROXY_TEXTURE_CUBE_MAP:
1325 case GL_TEXTURE_CUBE_MAP:
1326 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1327 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1328 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1329 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1330 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1331 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1332 target_can_be_compresed = ctx->Extensions.ARB_texture_cube_map;
1333 break;
1334 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1335 case GL_TEXTURE_2D_ARRAY_EXT:
1336 target_can_be_compresed = ctx->Extensions.EXT_texture_array;
1337 break;
1338 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1339 case GL_TEXTURE_CUBE_MAP_ARRAY:
1340 /* From the KHR_texture_compression_astc_hdr spec:
1341 *
1342 * Add a second new column "3D Tex." which is empty for all non-ASTC
1343 * formats. If only the LDR profile is supported by the
1344 * implementation, this column is also empty for all ASTC formats. If
1345 * both the LDR and HDR profiles are supported only, this column is
1346 * checked for all ASTC formats.
1347 *
1348 * Add a third new column "Cube Map Array Tex." which is empty for all
1349 * non-ASTC formats, and checked for all ASTC formats.
1350 *
1351 * and,
1352 *
1353 * 'An INVALID_OPERATION error is generated by CompressedTexImage3D
1354 * if <internalformat> is TEXTURE_CUBE_MAP_ARRAY and the
1355 * "Cube Map Array" column of table 8.19 is *not* checked, or if
1356 * <internalformat> is TEXTURE_3D and the "3D Tex." column of table
1357 * 8.19 is *not* checked'
1358 *
1359 * The instances of <internalformat> above should say <target>.
1360 *
1361 * ETC2/EAC formats are the only alternative in GLES and thus such errors
1362 * have already been handled by normal ETC2/EAC behavior.
1363 */
1364
1365 /* From section 3.8.6, page 146 of OpenGL ES 3.0 spec:
1366 *
1367 * "The ETC2/EAC texture compression algorithm supports only
1368 * two-dimensional images. If internalformat is an ETC2/EAC format,
1369 * glCompressedTexImage3D will generate an INVALID_OPERATION error if
1370 * target is not TEXTURE_2D_ARRAY."
1371 *
1372 * This should also be applicable for glTexStorage3D(). Other available
1373 * targets for these functions are: TEXTURE_3D and TEXTURE_CUBE_MAP_ARRAY.
1374 */
1375 if (layout == MESA_FORMAT_LAYOUT_ETC2 && _mesa_is_gles3(ctx))
1376 return write_error(error, GL_INVALID_OPERATION);
1377 target_can_be_compresed = ctx->Extensions.ARB_texture_cube_map_array;
1378 break;
1379 case GL_TEXTURE_3D:
1380 switch (layout) {
1381 case MESA_FORMAT_LAYOUT_ETC2:
1382 /* See ETC2/EAC comment in case GL_TEXTURE_CUBE_MAP_ARRAY. */
1383 if (_mesa_is_gles3(ctx))
1384 return write_error(error, GL_INVALID_OPERATION);
1385 break;
1386 case MESA_FORMAT_LAYOUT_BPTC:
1387 target_can_be_compresed = ctx->Extensions.ARB_texture_compression_bptc;
1388 break;
1389 case MESA_FORMAT_LAYOUT_ASTC:
1390 target_can_be_compresed =
1391 ctx->Extensions.KHR_texture_compression_astc_hdr;
1392
1393 /* Throw an INVALID_OPERATION error if the target is TEXTURE_3D and
1394 * and the hdr extension is not supported.
1395 * See comment in switch case GL_TEXTURE_CUBE_MAP_ARRAY for more info.
1396 */
1397 if (!target_can_be_compresed)
1398 return write_error(error, GL_INVALID_OPERATION);
1399 break;
1400 default:
1401 break;
1402 }
1403 default:
1404 break;
1405 }
1406 return write_error(error,
1407 target_can_be_compresed ? GL_NO_ERROR : GL_INVALID_ENUM);
1408 }
1409
1410
1411 /**
1412 * Check if the given texture target value is legal for a
1413 * glTexImage1/2/3D call.
1414 */
1415 static GLboolean
1416 legal_teximage_target(struct gl_context *ctx, GLuint dims, GLenum target)
1417 {
1418 switch (dims) {
1419 case 1:
1420 switch (target) {
1421 case GL_TEXTURE_1D:
1422 case GL_PROXY_TEXTURE_1D:
1423 return _mesa_is_desktop_gl(ctx);
1424 default:
1425 return GL_FALSE;
1426 }
1427 case 2:
1428 switch (target) {
1429 case GL_TEXTURE_2D:
1430 return GL_TRUE;
1431 case GL_PROXY_TEXTURE_2D:
1432 return _mesa_is_desktop_gl(ctx);
1433 case GL_PROXY_TEXTURE_CUBE_MAP:
1434 return _mesa_is_desktop_gl(ctx)
1435 && ctx->Extensions.ARB_texture_cube_map;
1436 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1437 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1438 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1439 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1440 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1441 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1442 return ctx->Extensions.ARB_texture_cube_map;
1443 case GL_TEXTURE_RECTANGLE_NV:
1444 case GL_PROXY_TEXTURE_RECTANGLE_NV:
1445 return _mesa_is_desktop_gl(ctx)
1446 && ctx->Extensions.NV_texture_rectangle;
1447 case GL_TEXTURE_1D_ARRAY_EXT:
1448 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1449 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
1450 default:
1451 return GL_FALSE;
1452 }
1453 case 3:
1454 switch (target) {
1455 case GL_TEXTURE_3D:
1456 return GL_TRUE;
1457 case GL_PROXY_TEXTURE_3D:
1458 return _mesa_is_desktop_gl(ctx);
1459 case GL_TEXTURE_2D_ARRAY_EXT:
1460 return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array)
1461 || _mesa_is_gles3(ctx);
1462 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1463 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
1464 case GL_TEXTURE_CUBE_MAP_ARRAY:
1465 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1466 return ctx->Extensions.ARB_texture_cube_map_array;
1467 default:
1468 return GL_FALSE;
1469 }
1470 default:
1471 _mesa_problem(ctx, "invalid dims=%u in legal_teximage_target()", dims);
1472 return GL_FALSE;
1473 }
1474 }
1475
1476
1477 /**
1478 * Check if the given texture target value is legal for a
1479 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1480 * The difference compared to legal_teximage_target() above is that
1481 * proxy targets are not supported.
1482 */
1483 static GLboolean
1484 legal_texsubimage_target(struct gl_context *ctx, GLuint dims, GLenum target,
1485 bool dsa)
1486 {
1487 switch (dims) {
1488 case 1:
1489 return _mesa_is_desktop_gl(ctx) && target == GL_TEXTURE_1D;
1490 case 2:
1491 switch (target) {
1492 case GL_TEXTURE_2D:
1493 return GL_TRUE;
1494 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1495 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1496 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1497 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1498 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1499 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1500 return ctx->Extensions.ARB_texture_cube_map;
1501 case GL_TEXTURE_RECTANGLE_NV:
1502 return _mesa_is_desktop_gl(ctx)
1503 && ctx->Extensions.NV_texture_rectangle;
1504 case GL_TEXTURE_1D_ARRAY_EXT:
1505 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
1506 default:
1507 return GL_FALSE;
1508 }
1509 case 3:
1510 switch (target) {
1511 case GL_TEXTURE_3D:
1512 return GL_TRUE;
1513 case GL_TEXTURE_2D_ARRAY_EXT:
1514 return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array)
1515 || _mesa_is_gles3(ctx);
1516 case GL_TEXTURE_CUBE_MAP_ARRAY:
1517 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1518 return ctx->Extensions.ARB_texture_cube_map_array;
1519
1520 /* Table 8.15 of the OpenGL 4.5 core profile spec
1521 * (20141030) says that TEXTURE_CUBE_MAP is valid for TextureSubImage3D
1522 * and CopyTextureSubImage3D.
1523 */
1524 case GL_TEXTURE_CUBE_MAP:
1525 return dsa;
1526 default:
1527 return GL_FALSE;
1528 }
1529 default:
1530 _mesa_problem(ctx, "invalid dims=%u in legal_texsubimage_target()",
1531 dims);
1532 return GL_FALSE;
1533 }
1534 }
1535
1536
1537 /**
1538 * Helper function to determine if a texture object is mutable (in terms
1539 * of GL_ARB_texture_storage).
1540 */
1541 static GLboolean
1542 mutable_tex_object(struct gl_context *ctx, GLenum target)
1543 {
1544 struct gl_texture_object *texObj = _mesa_get_current_tex_object(ctx, target);
1545 if (!texObj)
1546 return GL_FALSE;
1547
1548 return !texObj->Immutable;
1549 }
1550
1551
1552 /**
1553 * Return expected size of a compressed texture.
1554 */
1555 static GLuint
1556 compressed_tex_size(GLsizei width, GLsizei height, GLsizei depth,
1557 GLenum glformat)
1558 {
1559 mesa_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
1560 return _mesa_format_image_size(mesaFormat, width, height, depth);
1561 }
1562
1563 /**
1564 * Verify that a texture format is valid with a particular target
1565 *
1566 * In particular, textures with base format of \c GL_DEPTH_COMPONENT or
1567 * \c GL_DEPTH_STENCIL are only valid with certain, context dependent texture
1568 * targets.
1569 *
1570 * \param ctx GL context
1571 * \param target Texture target
1572 * \param internalFormat Internal format of the texture image
1573 *
1574 * \returns true if the combination is legal, false otherwise.
1575 */
1576 bool
1577 _mesa_legal_texture_base_format_for_target(struct gl_context *ctx,
1578 GLenum target, GLenum internalFormat)
1579 {
1580 if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT
1581 || _mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_STENCIL
1582 || _mesa_base_tex_format(ctx, internalFormat) == GL_STENCIL_INDEX) {
1583 /* Section 3.8.3 (Texture Image Specification) of the OpenGL 3.3 Core
1584 * Profile spec says:
1585 *
1586 * "Textures with a base internal format of DEPTH_COMPONENT or
1587 * DEPTH_STENCIL are supported by texture image specification
1588 * commands only if target is TEXTURE_1D, TEXTURE_2D,
1589 * TEXTURE_1D_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_RECTANGLE,
1590 * TEXTURE_CUBE_MAP, PROXY_TEXTURE_1D, PROXY_TEXTURE_2D,
1591 * PROXY_TEXTURE_1D_ARRAY, PROXY_TEXTURE_2D_ARRAY,
1592 * PROXY_TEXTURE_RECTANGLE, or PROXY_TEXTURE_CUBE_MAP. Using these
1593 * formats in conjunction with any other target will result in an
1594 * INVALID_OPERATION error."
1595 *
1596 * Cubemaps are only supported with desktop OpenGL version >= 3.0,
1597 * EXT_gpu_shader4, or, on OpenGL ES 2.0+, OES_depth_texture_cube_map.
1598 */
1599 if (target != GL_TEXTURE_1D &&
1600 target != GL_PROXY_TEXTURE_1D &&
1601 target != GL_TEXTURE_2D &&
1602 target != GL_PROXY_TEXTURE_2D &&
1603 target != GL_TEXTURE_1D_ARRAY &&
1604 target != GL_PROXY_TEXTURE_1D_ARRAY &&
1605 target != GL_TEXTURE_2D_ARRAY &&
1606 target != GL_PROXY_TEXTURE_2D_ARRAY &&
1607 target != GL_TEXTURE_RECTANGLE_ARB &&
1608 target != GL_PROXY_TEXTURE_RECTANGLE_ARB &&
1609 !((_mesa_is_cube_face(target) ||
1610 target == GL_TEXTURE_CUBE_MAP ||
1611 target == GL_PROXY_TEXTURE_CUBE_MAP) &&
1612 (ctx->Version >= 30 || ctx->Extensions.EXT_gpu_shader4
1613 || (ctx->API == API_OPENGLES2 && ctx->Extensions.OES_depth_texture_cube_map))) &&
1614 !((target == GL_TEXTURE_CUBE_MAP_ARRAY ||
1615 target == GL_PROXY_TEXTURE_CUBE_MAP_ARRAY) &&
1616 ctx->Extensions.ARB_texture_cube_map_array)) {
1617 return false;
1618 }
1619 }
1620
1621 return true;
1622 }
1623
1624 static bool
1625 texture_formats_agree(GLenum internalFormat,
1626 GLenum format)
1627 {
1628 GLboolean colorFormat;
1629 GLboolean is_format_depth_or_depthstencil;
1630 GLboolean is_internalFormat_depth_or_depthstencil;
1631
1632 /* Even though there are no color-index textures, we still have to support
1633 * uploading color-index data and remapping it to RGB via the
1634 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
1635 */
1636 const GLboolean indexFormat = (format == GL_COLOR_INDEX);
1637
1638 is_internalFormat_depth_or_depthstencil =
1639 _mesa_is_depth_format(internalFormat) ||
1640 _mesa_is_depthstencil_format(internalFormat);
1641
1642 is_format_depth_or_depthstencil =
1643 _mesa_is_depth_format(format) ||
1644 _mesa_is_depthstencil_format(format);
1645
1646 colorFormat = _mesa_is_color_format(format);
1647
1648 if (_mesa_is_color_format(internalFormat) && !colorFormat && !indexFormat)
1649 return false;
1650
1651 if (is_internalFormat_depth_or_depthstencil !=
1652 is_format_depth_or_depthstencil)
1653 return false;
1654
1655 if (_mesa_is_ycbcr_format(internalFormat) != _mesa_is_ycbcr_format(format))
1656 return false;
1657
1658 return true;
1659 }
1660
1661 /**
1662 * Test the combination of format, type and internal format arguments of
1663 * different texture operations on GLES.
1664 *
1665 * \param ctx GL context.
1666 * \param format pixel data format given by the user.
1667 * \param type pixel data type given by the user.
1668 * \param internalFormat internal format given by the user.
1669 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1670 * \param callerName name of the caller function to print in the error message
1671 *
1672 * \return true if a error is found, false otherwise
1673 *
1674 * Currently, it is used by texture_error_check() and texsubimage_error_check().
1675 */
1676 static bool
1677 texture_format_error_check_gles(struct gl_context *ctx, GLenum format,
1678 GLenum type, GLenum internalFormat,
1679 GLuint dimensions, const char *callerName)
1680 {
1681 GLenum err;
1682
1683 if (_mesa_is_gles3(ctx)) {
1684 err = _mesa_es3_error_check_format_and_type(ctx, format, type,
1685 internalFormat);
1686 if (err != GL_NO_ERROR) {
1687 _mesa_error(ctx, err,
1688 "%s(format = %s, type = %s, internalformat = %s)",
1689 callerName, _mesa_enum_to_string(format),
1690 _mesa_enum_to_string(type),
1691 _mesa_enum_to_string(internalFormat));
1692 return true;
1693 }
1694 }
1695 else {
1696 err = _mesa_es_error_check_format_and_type(ctx, format, type, dimensions);
1697 if (err != GL_NO_ERROR) {
1698 _mesa_error(ctx, err, "%s(format = %s, type = %s)",
1699 callerName, _mesa_enum_to_string(format),
1700 _mesa_enum_to_string(type));
1701 return true;
1702 }
1703 }
1704
1705 return false;
1706 }
1707
1708 /**
1709 * Test the glTexImage[123]D() parameters for errors.
1710 *
1711 * \param ctx GL context.
1712 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1713 * \param target texture target given by the user (already validated).
1714 * \param level image level given by the user.
1715 * \param internalFormat internal format given by the user.
1716 * \param format pixel data format given by the user.
1717 * \param type pixel data type given by the user.
1718 * \param width image width given by the user.
1719 * \param height image height given by the user.
1720 * \param depth image depth given by the user.
1721 * \param border image border given by the user.
1722 *
1723 * \return GL_TRUE if a error is found, GL_FALSE otherwise
1724 *
1725 * Verifies each of the parameters against the constants specified in
1726 * __struct gl_contextRec::Const and the supported extensions, and according
1727 * to the OpenGL specification.
1728 * Note that we don't fully error-check the width, height, depth values
1729 * here. That's done in _mesa_legal_texture_dimensions() which is used
1730 * by several other GL entrypoints. Plus, texture dims have a special
1731 * interaction with proxy textures.
1732 */
1733 static GLboolean
1734 texture_error_check( struct gl_context *ctx,
1735 GLuint dimensions, GLenum target,
1736 GLint level, GLint internalFormat,
1737 GLenum format, GLenum type,
1738 GLint width, GLint height,
1739 GLint depth, GLint border,
1740 const GLvoid *pixels )
1741 {
1742 GLenum err;
1743
1744 /* Note: for proxy textures, some error conditions immediately generate
1745 * a GL error in the usual way. But others do not generate a GL error.
1746 * Instead, they cause the width, height, depth, format fields of the
1747 * texture image to be zeroed-out. The GL spec seems to indicate that the
1748 * zero-out behaviour is only used in cases related to memory allocation.
1749 */
1750
1751 /* level check */
1752 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
1753 _mesa_error(ctx, GL_INVALID_VALUE,
1754 "glTexImage%dD(level=%d)", dimensions, level);
1755 return GL_TRUE;
1756 }
1757
1758 /* Check border */
1759 if (border < 0 || border > 1 ||
1760 ((ctx->API != API_OPENGL_COMPAT ||
1761 target == GL_TEXTURE_RECTANGLE_NV ||
1762 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1763 _mesa_error(ctx, GL_INVALID_VALUE,
1764 "glTexImage%dD(border=%d)", dimensions, border);
1765 return GL_TRUE;
1766 }
1767
1768 if (width < 0 || height < 0 || depth < 0) {
1769 _mesa_error(ctx, GL_INVALID_VALUE,
1770 "glTexImage%dD(width, height or depth < 0)", dimensions);
1771 return GL_TRUE;
1772 }
1773
1774 /* Check incoming image format and type */
1775 err = _mesa_error_check_format_and_type(ctx, format, type);
1776 if (err != GL_NO_ERROR) {
1777 /* Prior to OpenGL-ES 2.0, an INVALID_VALUE is expected instead of
1778 * INVALID_ENUM. From page 73 OpenGL ES 1.1 spec:
1779 *
1780 * "Specifying a value for internalformat that is not one of the
1781 * above (acceptable) values generates the error INVALID VALUE."
1782 */
1783 if (err == GL_INVALID_ENUM && _mesa_is_gles(ctx) && ctx->Version < 20)
1784 err = GL_INVALID_VALUE;
1785
1786 _mesa_error(ctx, err,
1787 "glTexImage%dD(incompatible format = %s, type = %s)",
1788 dimensions, _mesa_enum_to_string(format),
1789 _mesa_enum_to_string(type));
1790 return GL_TRUE;
1791 }
1792
1793 /* Check internalFormat */
1794 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
1795 _mesa_error(ctx, GL_INVALID_VALUE,
1796 "glTexImage%dD(internalFormat=%s)",
1797 dimensions, _mesa_enum_to_string(internalFormat));
1798 return GL_TRUE;
1799 }
1800
1801 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1802 * combinations of format, internalFormat, and type that can be used.
1803 * Formats and types that require additional extensions (e.g., GL_FLOAT
1804 * requires GL_OES_texture_float) are filtered elsewhere.
1805 */
1806 if (_mesa_is_gles(ctx) &&
1807 texture_format_error_check_gles(ctx, format, type, internalFormat,
1808 dimensions, "glTexImage%dD")) {
1809 return GL_TRUE;
1810 }
1811
1812 /* validate the bound PBO, if any */
1813 if (!_mesa_validate_pbo_source(ctx, dimensions, &ctx->Unpack,
1814 width, height, depth, format, type,
1815 INT_MAX, pixels, "glTexImage")) {
1816 return GL_TRUE;
1817 }
1818
1819 /* make sure internal format and format basically agree */
1820 if (!texture_formats_agree(internalFormat, format)) {
1821 _mesa_error(ctx, GL_INVALID_OPERATION,
1822 "glTexImage%dD(incompatible internalFormat = %s, format = %s)",
1823 dimensions, _mesa_enum_to_string(internalFormat),
1824 _mesa_enum_to_string(format));
1825 return GL_TRUE;
1826 }
1827
1828 /* additional checks for ycbcr textures */
1829 if (internalFormat == GL_YCBCR_MESA) {
1830 assert(ctx->Extensions.MESA_ycbcr_texture);
1831 if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
1832 type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
1833 char message[100];
1834 _mesa_snprintf(message, sizeof(message),
1835 "glTexImage%dD(format/type YCBCR mismatch)",
1836 dimensions);
1837 _mesa_error(ctx, GL_INVALID_ENUM, "%s", message);
1838 return GL_TRUE; /* error */
1839 }
1840 if (target != GL_TEXTURE_2D &&
1841 target != GL_PROXY_TEXTURE_2D &&
1842 target != GL_TEXTURE_RECTANGLE_NV &&
1843 target != GL_PROXY_TEXTURE_RECTANGLE_NV) {
1844 _mesa_error(ctx, GL_INVALID_ENUM,
1845 "glTexImage%dD(bad target for YCbCr texture)",
1846 dimensions);
1847 return GL_TRUE;
1848 }
1849 if (border != 0) {
1850 char message[100];
1851 _mesa_snprintf(message, sizeof(message),
1852 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1853 dimensions, border);
1854 _mesa_error(ctx, GL_INVALID_VALUE, "%s", message);
1855 return GL_TRUE;
1856 }
1857 }
1858
1859 /* additional checks for depth textures */
1860 if (!_mesa_legal_texture_base_format_for_target(ctx, target, internalFormat)) {
1861 _mesa_error(ctx, GL_INVALID_OPERATION,
1862 "glTexImage%dD(bad target for texture)", dimensions);
1863 return GL_TRUE;
1864 }
1865
1866 /* additional checks for compressed textures */
1867 if (_mesa_is_compressed_format(ctx, internalFormat)) {
1868 GLenum err;
1869 if (!_mesa_target_can_be_compressed(ctx, target, internalFormat, &err)) {
1870 _mesa_error(ctx, err,
1871 "glTexImage%dD(target can't be compressed)", dimensions);
1872 return GL_TRUE;
1873 }
1874 if (_mesa_format_no_online_compression(ctx, internalFormat)) {
1875 _mesa_error(ctx, GL_INVALID_OPERATION,
1876 "glTexImage%dD(no compression for format)", dimensions);
1877 return GL_TRUE;
1878 }
1879 if (border != 0) {
1880 _mesa_error(ctx, GL_INVALID_OPERATION,
1881 "glTexImage%dD(border!=0)", dimensions);
1882 return GL_TRUE;
1883 }
1884 }
1885
1886 /* additional checks for integer textures */
1887 if ((ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) &&
1888 (_mesa_is_enum_format_integer(format) !=
1889 _mesa_is_enum_format_integer(internalFormat))) {
1890 _mesa_error(ctx, GL_INVALID_OPERATION,
1891 "glTexImage%dD(integer/non-integer format mismatch)",
1892 dimensions);
1893 return GL_TRUE;
1894 }
1895
1896 if (!mutable_tex_object(ctx, target)) {
1897 _mesa_error(ctx, GL_INVALID_OPERATION,
1898 "glTexImage%dD(immutable texture)", dimensions);
1899 return GL_TRUE;
1900 }
1901
1902 /* if we get here, the parameters are OK */
1903 return GL_FALSE;
1904 }
1905
1906
1907 /**
1908 * Error checking for glCompressedTexImage[123]D().
1909 * Note that the width, height and depth values are not fully error checked
1910 * here.
1911 * \return GL_TRUE if a error is found, GL_FALSE otherwise
1912 */
1913 static GLenum
1914 compressed_texture_error_check(struct gl_context *ctx, GLint dimensions,
1915 GLenum target, GLint level,
1916 GLenum internalFormat, GLsizei width,
1917 GLsizei height, GLsizei depth, GLint border,
1918 GLsizei imageSize, const GLvoid *data)
1919 {
1920 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
1921 GLint expectedSize;
1922 GLenum error = GL_NO_ERROR;
1923 char *reason = ""; /* no error */
1924
1925 if (!_mesa_target_can_be_compressed(ctx, target, internalFormat, &error)) {
1926 reason = "target";
1927 goto error;
1928 }
1929
1930 /* This will detect any invalid internalFormat value */
1931 if (!_mesa_is_compressed_format(ctx, internalFormat)) {
1932 _mesa_error(ctx, GL_INVALID_ENUM,
1933 "glCompressedTexImage%dD(internalFormat=%s)",
1934 dimensions, _mesa_enum_to_string(internalFormat));
1935 return GL_TRUE;
1936 }
1937
1938 /* validate the bound PBO, if any */
1939 if (!_mesa_validate_pbo_source_compressed(ctx, dimensions, &ctx->Unpack,
1940 imageSize, data,
1941 "glCompressedTexImage")) {
1942 return GL_TRUE;
1943 }
1944
1945 switch (internalFormat) {
1946 case GL_PALETTE4_RGB8_OES:
1947 case GL_PALETTE4_RGBA8_OES:
1948 case GL_PALETTE4_R5_G6_B5_OES:
1949 case GL_PALETTE4_RGBA4_OES:
1950 case GL_PALETTE4_RGB5_A1_OES:
1951 case GL_PALETTE8_RGB8_OES:
1952 case GL_PALETTE8_RGBA8_OES:
1953 case GL_PALETTE8_R5_G6_B5_OES:
1954 case GL_PALETTE8_RGBA4_OES:
1955 case GL_PALETTE8_RGB5_A1_OES:
1956 /* check level (note that level should be zero or less!) */
1957 if (level > 0 || level < -maxLevels) {
1958 reason = "level";
1959 error = GL_INVALID_VALUE;
1960 goto error;
1961 }
1962
1963 if (dimensions != 2) {
1964 reason = "compressed paletted textures must be 2D";
1965 error = GL_INVALID_OPERATION;
1966 goto error;
1967 }
1968
1969 /* Figure out the expected texture size (in bytes). This will be
1970 * checked against the actual size later.
1971 */
1972 expectedSize = _mesa_cpal_compressed_size(level, internalFormat,
1973 width, height);
1974
1975 /* This is for the benefit of the TestProxyTexImage below. It expects
1976 * level to be non-negative. OES_compressed_paletted_texture uses a
1977 * weird mechanism where the level specified to glCompressedTexImage2D
1978 * is -(n-1) number of levels in the texture, and the data specifies the
1979 * complete mipmap stack. This is done to ensure the palette is the
1980 * same for all levels.
1981 */
1982 level = -level;
1983 break;
1984
1985 default:
1986 /* check level */
1987 if (level < 0 || level >= maxLevels) {
1988 reason = "level";
1989 error = GL_INVALID_VALUE;
1990 goto error;
1991 }
1992
1993 /* Figure out the expected texture size (in bytes). This will be
1994 * checked against the actual size later.
1995 */
1996 expectedSize = compressed_tex_size(width, height, depth, internalFormat);
1997 break;
1998 }
1999
2000 /* This should really never fail */
2001 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
2002 reason = "internalFormat";
2003 error = GL_INVALID_ENUM;
2004 goto error;
2005 }
2006
2007 /* No compressed formats support borders at this time */
2008 if (border != 0) {
2009 reason = "border != 0";
2010 error = GL_INVALID_VALUE;
2011 goto error;
2012 }
2013
2014 /* Check for invalid pixel storage modes */
2015 if (!_mesa_compressed_pixel_storage_error_check(ctx, dimensions,
2016 &ctx->Unpack,
2017 "glCompressedTexImage")) {
2018 return GL_FALSE;
2019 }
2020
2021 /* check image size in bytes */
2022 if (expectedSize != imageSize) {
2023 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
2024 * if <imageSize> is not consistent with the format, dimensions, and
2025 * contents of the specified image.
2026 */
2027 reason = "imageSize inconsistent with width/height/format";
2028 error = GL_INVALID_VALUE;
2029 goto error;
2030 }
2031
2032 if (!mutable_tex_object(ctx, target)) {
2033 reason = "immutable texture";
2034 error = GL_INVALID_OPERATION;
2035 goto error;
2036 }
2037
2038 return GL_FALSE;
2039
2040 error:
2041 /* Note: not all error paths exit through here. */
2042 _mesa_error(ctx, error, "glCompressedTexImage%dD(%s)",
2043 dimensions, reason);
2044 return GL_TRUE;
2045 }
2046
2047
2048
2049 /**
2050 * Test glTexSubImage[123]D() parameters for errors.
2051 *
2052 * \param ctx GL context.
2053 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2054 * \param target texture target given by the user (already validated)
2055 * \param level image level given by the user.
2056 * \param xoffset sub-image x offset given by the user.
2057 * \param yoffset sub-image y offset given by the user.
2058 * \param zoffset sub-image z offset given by the user.
2059 * \param format pixel data format given by the user.
2060 * \param type pixel data type given by the user.
2061 * \param width image width given by the user.
2062 * \param height image height given by the user.
2063 * \param depth image depth given by the user.
2064 *
2065 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2066 *
2067 * Verifies each of the parameters against the constants specified in
2068 * __struct gl_contextRec::Const and the supported extensions, and according
2069 * to the OpenGL specification.
2070 */
2071 static GLboolean
2072 texsubimage_error_check(struct gl_context *ctx, GLuint dimensions,
2073 struct gl_texture_object *texObj,
2074 GLenum target, GLint level,
2075 GLint xoffset, GLint yoffset, GLint zoffset,
2076 GLint width, GLint height, GLint depth,
2077 GLenum format, GLenum type, const GLvoid *pixels,
2078 bool dsa, const char *callerName)
2079 {
2080 struct gl_texture_image *texImage;
2081 GLenum err;
2082
2083 if (!texObj) {
2084 /* must be out of memory */
2085 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s()", callerName);
2086 return GL_TRUE;
2087 }
2088
2089 /* level check */
2090 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2091 _mesa_error(ctx, GL_INVALID_VALUE, "%s(level=%d)", callerName, level);
2092 return GL_TRUE;
2093 }
2094
2095 texImage = _mesa_select_tex_image(texObj, target, level);
2096 if (!texImage) {
2097 /* non-existant texture level */
2098 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid texture image)",
2099 callerName);
2100 return GL_TRUE;
2101 }
2102
2103 err = _mesa_error_check_format_and_type(ctx, format, type);
2104 if (err != GL_NO_ERROR) {
2105 _mesa_error(ctx, err,
2106 "%s(incompatible format = %s, type = %s)",
2107 callerName, _mesa_enum_to_string(format),
2108 _mesa_enum_to_string(type));
2109 return GL_TRUE;
2110 }
2111
2112 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2113 * combinations of format, internalFormat, and type that can be used.
2114 * Formats and types that require additional extensions (e.g., GL_FLOAT
2115 * requires GL_OES_texture_float) are filtered elsewhere.
2116 */
2117 if (_mesa_is_gles(ctx) &&
2118 texture_format_error_check_gles(ctx, format, type,
2119 texImage->InternalFormat,
2120 dimensions, callerName)) {
2121 return GL_TRUE;
2122 }
2123
2124 /* validate the bound PBO, if any */
2125 if (!_mesa_validate_pbo_source(ctx, dimensions, &ctx->Unpack,
2126 width, height, depth, format, type,
2127 INT_MAX, pixels, callerName)) {
2128 return GL_TRUE;
2129 }
2130
2131 if (error_check_subtexture_dimensions(ctx, dimensions,
2132 texImage, xoffset, yoffset, zoffset,
2133 width, height, depth, callerName)) {
2134 return GL_TRUE;
2135 }
2136
2137 if (_mesa_is_format_compressed(texImage->TexFormat)) {
2138 if (_mesa_format_no_online_compression(ctx, texImage->InternalFormat)) {
2139 _mesa_error(ctx, GL_INVALID_OPERATION,
2140 "%s(no compression for format)", callerName);
2141 return GL_TRUE;
2142 }
2143 }
2144
2145 if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
2146 /* both source and dest must be integer-valued, or neither */
2147 if (_mesa_is_format_integer_color(texImage->TexFormat) !=
2148 _mesa_is_enum_format_integer(format)) {
2149 _mesa_error(ctx, GL_INVALID_OPERATION,
2150 "%s(integer/non-integer format mismatch)", callerName);
2151 return GL_TRUE;
2152 }
2153 }
2154
2155 return GL_FALSE;
2156 }
2157
2158
2159 /**
2160 * Test glCopyTexImage[12]D() parameters for errors.
2161 *
2162 * \param ctx GL context.
2163 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2164 * \param target texture target given by the user.
2165 * \param level image level given by the user.
2166 * \param internalFormat internal format given by the user.
2167 * \param width image width given by the user.
2168 * \param height image height given by the user.
2169 * \param border texture border.
2170 *
2171 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2172 *
2173 * Verifies each of the parameters against the constants specified in
2174 * __struct gl_contextRec::Const and the supported extensions, and according
2175 * to the OpenGL specification.
2176 */
2177 static GLboolean
2178 copytexture_error_check( struct gl_context *ctx, GLuint dimensions,
2179 GLenum target, GLint level, GLint internalFormat,
2180 GLint width, GLint height, GLint border )
2181 {
2182 GLint baseFormat;
2183 GLint rb_base_format;
2184 struct gl_renderbuffer *rb;
2185 GLenum rb_internal_format;
2186
2187 /* check target */
2188 if (!legal_texsubimage_target(ctx, dimensions, target, false)) {
2189 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexImage%uD(target=%s)",
2190 dimensions, _mesa_enum_to_string(target));
2191 return GL_TRUE;
2192 }
2193
2194 /* level check */
2195 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2196 _mesa_error(ctx, GL_INVALID_VALUE,
2197 "glCopyTexImage%dD(level=%d)", dimensions, level);
2198 return GL_TRUE;
2199 }
2200
2201 /* Check that the source buffer is complete */
2202 if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2203 if (ctx->ReadBuffer->_Status == 0) {
2204 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2205 }
2206 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2207 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2208 "glCopyTexImage%dD(invalid readbuffer)", dimensions);
2209 return GL_TRUE;
2210 }
2211
2212 if (ctx->ReadBuffer->Visual.samples > 0) {
2213 _mesa_error(ctx, GL_INVALID_OPERATION,
2214 "glCopyTexImage%dD(multisample FBO)", dimensions);
2215 return GL_TRUE;
2216 }
2217 }
2218
2219 /* Check border */
2220 if (border < 0 || border > 1 ||
2221 ((ctx->API != API_OPENGL_COMPAT ||
2222 target == GL_TEXTURE_RECTANGLE_NV ||
2223 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
2224 _mesa_error(ctx, GL_INVALID_VALUE,
2225 "glCopyTexImage%dD(border=%d)", dimensions, border);
2226 return GL_TRUE;
2227 }
2228
2229 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2230 * internalFormat.
2231 */
2232 if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
2233 switch (internalFormat) {
2234 case GL_ALPHA:
2235 case GL_RGB:
2236 case GL_RGBA:
2237 case GL_LUMINANCE:
2238 case GL_LUMINANCE_ALPHA:
2239 break;
2240 default:
2241 _mesa_error(ctx, GL_INVALID_ENUM,
2242 "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2243 _mesa_enum_to_string(internalFormat));
2244 return GL_TRUE;
2245 }
2246 } else {
2247 /*
2248 * Section 8.6 (Alternate Texture Image Specification Commands) of the
2249 * OpenGL 4.5 (Compatibility Profile) spec says:
2250 *
2251 * "Parameters level, internalformat, and border are specified using
2252 * the same values, with the same meanings, as the corresponding
2253 * arguments of TexImage2D, except that internalformat may not be
2254 * specified as 1, 2, 3, or 4."
2255 */
2256 if (internalFormat >= 1 && internalFormat <= 4) {
2257 _mesa_error(ctx, GL_INVALID_ENUM,
2258 "glCopyTexImage%dD(internalFormat=%d)", dimensions,
2259 internalFormat);
2260 return GL_TRUE;
2261 }
2262 }
2263
2264 baseFormat = _mesa_base_tex_format(ctx, internalFormat);
2265 if (baseFormat < 0) {
2266 _mesa_error(ctx, GL_INVALID_ENUM,
2267 "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2268 _mesa_enum_to_string(internalFormat));
2269 return GL_TRUE;
2270 }
2271
2272 rb = _mesa_get_read_renderbuffer_for_format(ctx, internalFormat);
2273 if (rb == NULL) {
2274 _mesa_error(ctx, GL_INVALID_OPERATION,
2275 "glCopyTexImage%dD(read buffer)", dimensions);
2276 return GL_TRUE;
2277 }
2278
2279 rb_internal_format = rb->InternalFormat;
2280 rb_base_format = _mesa_base_tex_format(ctx, rb->InternalFormat);
2281 if (_mesa_is_color_format(internalFormat)) {
2282 if (rb_base_format < 0) {
2283 _mesa_error(ctx, GL_INVALID_VALUE,
2284 "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2285 _mesa_enum_to_string(internalFormat));
2286 return GL_TRUE;
2287 }
2288 }
2289
2290 if (_mesa_is_gles(ctx)) {
2291 bool valid = true;
2292 if (_mesa_base_format_component_count(baseFormat) >
2293 _mesa_base_format_component_count(rb_base_format)) {
2294 valid = false;
2295 }
2296 if (baseFormat == GL_DEPTH_COMPONENT ||
2297 baseFormat == GL_DEPTH_STENCIL ||
2298 baseFormat == GL_STENCIL_INDEX ||
2299 rb_base_format == GL_DEPTH_COMPONENT ||
2300 rb_base_format == GL_DEPTH_STENCIL ||
2301 rb_base_format == GL_STENCIL_INDEX ||
2302 ((baseFormat == GL_LUMINANCE_ALPHA ||
2303 baseFormat == GL_ALPHA) &&
2304 rb_base_format != GL_RGBA) ||
2305 internalFormat == GL_RGB9_E5) {
2306 valid = false;
2307 }
2308 if (internalFormat == GL_RGB9_E5) {
2309 valid = false;
2310 }
2311 if (!valid) {
2312 _mesa_error(ctx, GL_INVALID_OPERATION,
2313 "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2314 _mesa_enum_to_string(internalFormat));
2315 return GL_TRUE;
2316 }
2317 }
2318
2319 if (_mesa_is_gles3(ctx)) {
2320 bool rb_is_srgb = false;
2321 bool dst_is_srgb = false;
2322
2323 if (ctx->Extensions.EXT_framebuffer_sRGB &&
2324 _mesa_get_format_color_encoding(rb->Format) == GL_SRGB) {
2325 rb_is_srgb = true;
2326 }
2327
2328 if (_mesa_get_linear_internalformat(internalFormat) != internalFormat) {
2329 dst_is_srgb = true;
2330 }
2331
2332 if (rb_is_srgb != dst_is_srgb) {
2333 /* Page 137 (page 149 of the PDF) in section 3.8.5 of the
2334 * OpenGLES 3.0.0 spec says:
2335 *
2336 * "The error INVALID_OPERATION is also generated if the
2337 * value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the
2338 * framebuffer attachment corresponding to the read buffer
2339 * is LINEAR (see section 6.1.13) and internalformat is
2340 * one of the sRGB formats described in section 3.8.16, or
2341 * if the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING is
2342 * SRGB and internalformat is not one of the sRGB formats."
2343 */
2344 _mesa_error(ctx, GL_INVALID_OPERATION,
2345 "glCopyTexImage%dD(srgb usage mismatch)", dimensions);
2346 return GL_TRUE;
2347 }
2348
2349 /* Page 139, Table 3.15 of OpenGL ES 3.0 spec does not define ReadPixels
2350 * types for SNORM formats. Also, conversion to SNORM formats is not
2351 * allowed by Table 3.2 on Page 110.
2352 */
2353 if (_mesa_is_enum_format_snorm(internalFormat)) {
2354 _mesa_error(ctx, GL_INVALID_OPERATION,
2355 "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2356 _mesa_enum_to_string(internalFormat));
2357 return GL_TRUE;
2358 }
2359 }
2360
2361 if (!_mesa_source_buffer_exists(ctx, baseFormat)) {
2362 _mesa_error(ctx, GL_INVALID_OPERATION,
2363 "glCopyTexImage%dD(missing readbuffer)", dimensions);
2364 return GL_TRUE;
2365 }
2366
2367 /* From the EXT_texture_integer spec:
2368 *
2369 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2370 * if the texture internalformat is an integer format and the read color
2371 * buffer is not an integer format, or if the internalformat is not an
2372 * integer format and the read color buffer is an integer format."
2373 */
2374 if (_mesa_is_color_format(internalFormat)) {
2375 bool is_int = _mesa_is_enum_format_integer(internalFormat);
2376 bool is_rbint = _mesa_is_enum_format_integer(rb_internal_format);
2377 bool is_unorm = _mesa_is_enum_format_unorm(internalFormat);
2378 bool is_rbunorm = _mesa_is_enum_format_unorm(rb_internal_format);
2379 if (is_int || is_rbint) {
2380 if (is_int != is_rbint) {
2381 _mesa_error(ctx, GL_INVALID_OPERATION,
2382 "glCopyTexImage%dD(integer vs non-integer)", dimensions);
2383 return GL_TRUE;
2384 } else if (_mesa_is_gles(ctx) &&
2385 _mesa_is_enum_format_unsigned_int(internalFormat) !=
2386 _mesa_is_enum_format_unsigned_int(rb_internal_format)) {
2387 _mesa_error(ctx, GL_INVALID_OPERATION,
2388 "glCopyTexImage%dD(signed vs unsigned integer)", dimensions);
2389 return GL_TRUE;
2390 }
2391 }
2392
2393 /* From page 138 of OpenGL ES 3.0 spec:
2394 * "The error INVALID_OPERATION is generated if floating-point RGBA
2395 * data is required; if signed integer RGBA data is required and the
2396 * format of the current color buffer is not signed integer; if
2397 * unsigned integer RGBA data is required and the format of the
2398 * current color buffer is not unsigned integer; or if fixed-point
2399 * RGBA data is required and the format of the current color buffer
2400 * is not fixed-point.
2401 */
2402 if (_mesa_is_gles(ctx) && is_unorm != is_rbunorm)
2403 _mesa_error(ctx, GL_INVALID_OPERATION,
2404 "glCopyTexImage%dD(unorm vs non-unorm)", dimensions);
2405 }
2406
2407 if (_mesa_is_compressed_format(ctx, internalFormat)) {
2408 GLenum err;
2409 if (!_mesa_target_can_be_compressed(ctx, target, internalFormat, &err)) {
2410 _mesa_error(ctx, err,
2411 "glCopyTexImage%dD(target can't be compressed)", dimensions);
2412 return GL_TRUE;
2413 }
2414 if (_mesa_format_no_online_compression(ctx, internalFormat)) {
2415 _mesa_error(ctx, GL_INVALID_OPERATION,
2416 "glCopyTexImage%dD(no compression for format)", dimensions);
2417 return GL_TRUE;
2418 }
2419 if (border != 0) {
2420 _mesa_error(ctx, GL_INVALID_OPERATION,
2421 "glCopyTexImage%dD(border!=0)", dimensions);
2422 return GL_TRUE;
2423 }
2424 }
2425
2426 if (!mutable_tex_object(ctx, target)) {
2427 _mesa_error(ctx, GL_INVALID_OPERATION,
2428 "glCopyTexImage%dD(immutable texture)", dimensions);
2429 return GL_TRUE;
2430 }
2431
2432 /* if we get here, the parameters are OK */
2433 return GL_FALSE;
2434 }
2435
2436
2437 /**
2438 * Test glCopyTexSubImage[12]D() parameters for errors.
2439 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2440 */
2441 static GLboolean
2442 copytexsubimage_error_check(struct gl_context *ctx, GLuint dimensions,
2443 const struct gl_texture_object *texObj,
2444 GLenum target, GLint level,
2445 GLint xoffset, GLint yoffset, GLint zoffset,
2446 GLint width, GLint height, const char *caller)
2447 {
2448 struct gl_texture_image *texImage;
2449
2450 /* Check that the source buffer is complete */
2451 if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2452 if (ctx->ReadBuffer->_Status == 0) {
2453 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2454 }
2455 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2456 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2457 "%s(invalid readbuffer)", caller);
2458 return GL_TRUE;
2459 }
2460
2461 if (ctx->ReadBuffer->Visual.samples > 0) {
2462 _mesa_error(ctx, GL_INVALID_OPERATION,
2463 "%s(multisample FBO)", caller);
2464 return GL_TRUE;
2465 }
2466 }
2467
2468 /* Check level */
2469 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2470 _mesa_error(ctx, GL_INVALID_VALUE, "%s(level=%d)", caller, level);
2471 return GL_TRUE;
2472 }
2473
2474 /* Get dest image pointers */
2475 if (!texObj) {
2476 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s()", caller);
2477 return GL_TRUE;
2478 }
2479
2480 texImage = _mesa_select_tex_image(texObj, target, level);
2481 if (!texImage) {
2482 /* destination image does not exist */
2483 _mesa_error(ctx, GL_INVALID_OPERATION,
2484 "%s(invalid texture image)", caller);
2485 return GL_TRUE;
2486 }
2487
2488 if (error_check_subtexture_dimensions(ctx, dimensions, texImage,
2489 xoffset, yoffset, zoffset,
2490 width, height, 1, caller)) {
2491 return GL_TRUE;
2492 }
2493
2494 if (_mesa_is_format_compressed(texImage->TexFormat)) {
2495 if (_mesa_format_no_online_compression(ctx, texImage->InternalFormat)) {
2496 _mesa_error(ctx, GL_INVALID_OPERATION,
2497 "%s(no compression for format)", caller);
2498 return GL_TRUE;
2499 }
2500 }
2501
2502 if (texImage->InternalFormat == GL_YCBCR_MESA) {
2503 _mesa_error(ctx, GL_INVALID_OPERATION, "%s()", caller);
2504 return GL_TRUE;
2505 }
2506
2507 if (!_mesa_source_buffer_exists(ctx, texImage->_BaseFormat)) {
2508 _mesa_error(ctx, GL_INVALID_OPERATION,
2509 "%s(missing readbuffer, format=%s)", caller,
2510 _mesa_enum_to_string(texImage->_BaseFormat));
2511 return GL_TRUE;
2512 }
2513
2514 /* From the EXT_texture_integer spec:
2515 *
2516 * "INVALID_OPERATION is generated by CopyTexImage* and
2517 * CopyTexSubImage* if the texture internalformat is an integer format
2518 * and the read color buffer is not an integer format, or if the
2519 * internalformat is not an integer format and the read color buffer
2520 * is an integer format."
2521 */
2522 if (_mesa_is_color_format(texImage->InternalFormat)) {
2523 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
2524
2525 if (_mesa_is_format_integer_color(rb->Format) !=
2526 _mesa_is_format_integer_color(texImage->TexFormat)) {
2527 _mesa_error(ctx, GL_INVALID_OPERATION,
2528 "%s(integer vs non-integer)", caller);
2529 return GL_TRUE;
2530 }
2531 }
2532
2533 /* In the ES 3.2 specification's Table 8.13 (Valid CopyTexImage source
2534 * framebuffer/destination texture base internal format combinations),
2535 * all the entries for stencil are left blank (unsupported).
2536 */
2537 if (_mesa_is_gles(ctx) && _mesa_is_stencil_format(texImage->_BaseFormat)) {
2538 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(stencil disallowed)", caller);
2539 return GL_TRUE;
2540 }
2541
2542 /* if we get here, the parameters are OK */
2543 return GL_FALSE;
2544 }
2545
2546
2547 /** Callback info for walking over FBO hash table */
2548 struct cb_info
2549 {
2550 struct gl_context *ctx;
2551 struct gl_texture_object *texObj;
2552 GLuint level, face;
2553 };
2554
2555
2556 /**
2557 * Check render to texture callback. Called from _mesa_HashWalk().
2558 */
2559 static void
2560 check_rtt_cb(GLuint key, void *data, void *userData)
2561 {
2562 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
2563 const struct cb_info *info = (struct cb_info *) userData;
2564 struct gl_context *ctx = info->ctx;
2565 const struct gl_texture_object *texObj = info->texObj;
2566 const GLuint level = info->level, face = info->face;
2567
2568 /* If this is a user-created FBO */
2569 if (_mesa_is_user_fbo(fb)) {
2570 GLuint i;
2571 /* check if any of the FBO's attachments point to 'texObj' */
2572 for (i = 0; i < BUFFER_COUNT; i++) {
2573 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2574 if (att->Type == GL_TEXTURE &&
2575 att->Texture == texObj &&
2576 att->TextureLevel == level &&
2577 att->CubeMapFace == face) {
2578 _mesa_update_texture_renderbuffer(ctx, ctx->DrawBuffer, att);
2579 assert(att->Renderbuffer->TexImage);
2580 /* Mark fb status as indeterminate to force re-validation */
2581 fb->_Status = 0;
2582 }
2583 }
2584 }
2585 }
2586
2587
2588 /**
2589 * When a texture image is specified we have to check if it's bound to
2590 * any framebuffer objects (render to texture) in order to detect changes
2591 * in size or format since that effects FBO completeness.
2592 * Any FBOs rendering into the texture must be re-validated.
2593 */
2594 void
2595 _mesa_update_fbo_texture(struct gl_context *ctx,
2596 struct gl_texture_object *texObj,
2597 GLuint face, GLuint level)
2598 {
2599 /* Only check this texture if it's been marked as RenderToTexture */
2600 if (texObj->_RenderToTexture) {
2601 struct cb_info info;
2602 info.ctx = ctx;
2603 info.texObj = texObj;
2604 info.level = level;
2605 info.face = face;
2606 _mesa_HashWalk(ctx->Shared->FrameBuffers, check_rtt_cb, &info);
2607 }
2608 }
2609
2610
2611 /**
2612 * If the texture object's GenerateMipmap flag is set and we've
2613 * changed the texture base level image, regenerate the rest of the
2614 * mipmap levels now.
2615 */
2616 static inline void
2617 check_gen_mipmap(struct gl_context *ctx, GLenum target,
2618 struct gl_texture_object *texObj, GLint level)
2619 {
2620 if (texObj->GenerateMipmap &&
2621 level == texObj->BaseLevel &&
2622 level < texObj->MaxLevel) {
2623 assert(ctx->Driver.GenerateMipmap);
2624 ctx->Driver.GenerateMipmap(ctx, target, texObj);
2625 }
2626 }
2627
2628
2629 /** Debug helper: override the user-requested internal format */
2630 static GLenum
2631 override_internal_format(GLenum internalFormat, GLint width, GLint height)
2632 {
2633 #if 0
2634 if (internalFormat == GL_RGBA16F_ARB ||
2635 internalFormat == GL_RGBA32F_ARB) {
2636 printf("Convert rgba float tex to int %d x %d\n", width, height);
2637 return GL_RGBA;
2638 }
2639 else if (internalFormat == GL_RGB16F_ARB ||
2640 internalFormat == GL_RGB32F_ARB) {
2641 printf("Convert rgb float tex to int %d x %d\n", width, height);
2642 return GL_RGB;
2643 }
2644 else if (internalFormat == GL_LUMINANCE_ALPHA16F_ARB ||
2645 internalFormat == GL_LUMINANCE_ALPHA32F_ARB) {
2646 printf("Convert luminance float tex to int %d x %d\n", width, height);
2647 return GL_LUMINANCE_ALPHA;
2648 }
2649 else if (internalFormat == GL_LUMINANCE16F_ARB ||
2650 internalFormat == GL_LUMINANCE32F_ARB) {
2651 printf("Convert luminance float tex to int %d x %d\n", width, height);
2652 return GL_LUMINANCE;
2653 }
2654 else if (internalFormat == GL_ALPHA16F_ARB ||
2655 internalFormat == GL_ALPHA32F_ARB) {
2656 printf("Convert luminance float tex to int %d x %d\n", width, height);
2657 return GL_ALPHA;
2658 }
2659 /*
2660 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2661 internalFormat = GL_RGBA;
2662 }
2663 */
2664 else {
2665 return internalFormat;
2666 }
2667 #else
2668 return internalFormat;
2669 #endif
2670 }
2671
2672
2673 /**
2674 * Choose the actual hardware format for a texture image.
2675 * Try to use the same format as the previous image level when possible.
2676 * Otherwise, ask the driver for the best format.
2677 * It's important to try to choose a consistant format for all levels
2678 * for efficient texture memory layout/allocation. In particular, this
2679 * comes up during automatic mipmap generation.
2680 */
2681 mesa_format
2682 _mesa_choose_texture_format(struct gl_context *ctx,
2683 struct gl_texture_object *texObj,
2684 GLenum target, GLint level,
2685 GLenum internalFormat, GLenum format, GLenum type)
2686 {
2687 mesa_format f;
2688
2689 /* see if we've already chosen a format for the previous level */
2690 if (level > 0) {
2691 struct gl_texture_image *prevImage =
2692 _mesa_select_tex_image(texObj, target, level - 1);
2693 /* See if the prev level is defined and has an internal format which
2694 * matches the new internal format.
2695 */
2696 if (prevImage &&
2697 prevImage->Width > 0 &&
2698 prevImage->InternalFormat == internalFormat) {
2699 /* use the same format */
2700 assert(prevImage->TexFormat != MESA_FORMAT_NONE);
2701 return prevImage->TexFormat;
2702 }
2703 }
2704
2705 /* If the application requested compression to an S3TC format but we don't
2706 * have the DXTn library, force a generic compressed format instead.
2707 */
2708 if (internalFormat != format && format != GL_NONE) {
2709 const GLenum before = internalFormat;
2710
2711 switch (internalFormat) {
2712 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2713 if (!ctx->Mesa_DXTn)
2714 internalFormat = GL_COMPRESSED_RGB;
2715 break;
2716 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
2717 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
2718 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
2719 if (!ctx->Mesa_DXTn)
2720 internalFormat = GL_COMPRESSED_RGBA;
2721 break;
2722 default:
2723 break;
2724 }
2725
2726 if (before != internalFormat) {
2727 _mesa_warning(ctx,
2728 "DXT compression requested (%s), "
2729 "but libtxc_dxtn library not installed. Using %s "
2730 "instead.",
2731 _mesa_enum_to_string(before),
2732 _mesa_enum_to_string(internalFormat));
2733 }
2734 }
2735
2736 /* choose format from scratch */
2737 f = ctx->Driver.ChooseTextureFormat(ctx, target, internalFormat,
2738 format, type);
2739 assert(f != MESA_FORMAT_NONE);
2740 return f;
2741 }
2742
2743
2744 /**
2745 * Adjust pixel unpack params and image dimensions to strip off the
2746 * one-pixel texture border.
2747 *
2748 * Gallium and intel don't support texture borders. They've seldem been used
2749 * and seldom been implemented correctly anyway.
2750 *
2751 * \param unpackNew returns the new pixel unpack parameters
2752 */
2753 static void
2754 strip_texture_border(GLenum target,
2755 GLint *width, GLint *height, GLint *depth,
2756 const struct gl_pixelstore_attrib *unpack,
2757 struct gl_pixelstore_attrib *unpackNew)
2758 {
2759 assert(width);
2760 assert(height);
2761 assert(depth);
2762
2763 *unpackNew = *unpack;
2764
2765 if (unpackNew->RowLength == 0)
2766 unpackNew->RowLength = *width;
2767
2768 if (unpackNew->ImageHeight == 0)
2769 unpackNew->ImageHeight = *height;
2770
2771 assert(*width >= 3);
2772 unpackNew->SkipPixels++; /* skip the border */
2773 *width = *width - 2; /* reduce the width by two border pixels */
2774
2775 /* The min height of a texture with a border is 3 */
2776 if (*height >= 3 && target != GL_TEXTURE_1D_ARRAY) {
2777 unpackNew->SkipRows++; /* skip the border */
2778 *height = *height - 2; /* reduce the height by two border pixels */
2779 }
2780
2781 if (*depth >= 3 &&
2782 target != GL_TEXTURE_2D_ARRAY &&
2783 target != GL_TEXTURE_CUBE_MAP_ARRAY) {
2784 unpackNew->SkipImages++; /* skip the border */
2785 *depth = *depth - 2; /* reduce the depth by two border pixels */
2786 }
2787 }
2788
2789
2790 /**
2791 * Common code to implement all the glTexImage1D/2D/3D functions
2792 * as well as glCompressedTexImage1D/2D/3D.
2793 * \param compressed only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
2794 * \param format the user's image format (only used if !compressed)
2795 * \param type the user's image type (only used if !compressed)
2796 * \param imageSize only used for glCompressedTexImage1D/2D/3D calls.
2797 */
2798 static void
2799 teximage(struct gl_context *ctx, GLboolean compressed, GLuint dims,
2800 GLenum target, GLint level, GLint internalFormat,
2801 GLsizei width, GLsizei height, GLsizei depth,
2802 GLint border, GLenum format, GLenum type,
2803 GLsizei imageSize, const GLvoid *pixels)
2804 {
2805 const char *func = compressed ? "glCompressedTexImage" : "glTexImage";
2806 struct gl_pixelstore_attrib unpack_no_border;
2807 const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
2808 struct gl_texture_object *texObj;
2809 mesa_format texFormat;
2810 GLboolean dimensionsOK, sizeOK;
2811
2812 FLUSH_VERTICES(ctx, 0);
2813
2814 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) {
2815 if (compressed)
2816 _mesa_debug(ctx,
2817 "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
2818 dims,
2819 _mesa_enum_to_string(target), level,
2820 _mesa_enum_to_string(internalFormat),
2821 width, height, depth, border, pixels);
2822 else
2823 _mesa_debug(ctx,
2824 "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2825 dims,
2826 _mesa_enum_to_string(target), level,
2827 _mesa_enum_to_string(internalFormat),
2828 width, height, depth, border,
2829 _mesa_enum_to_string(format),
2830 _mesa_enum_to_string(type), pixels);
2831 }
2832
2833 internalFormat = override_internal_format(internalFormat, width, height);
2834
2835 /* target error checking */
2836 if (!legal_teximage_target(ctx, dims, target)) {
2837 _mesa_error(ctx, GL_INVALID_ENUM, "%s%uD(target=%s)",
2838 func, dims, _mesa_enum_to_string(target));
2839 return;
2840 }
2841
2842 /* general error checking */
2843 if (compressed) {
2844 if (compressed_texture_error_check(ctx, dims, target, level,
2845 internalFormat,
2846 width, height, depth,
2847 border, imageSize, pixels))
2848 return;
2849 }
2850 else {
2851 if (texture_error_check(ctx, dims, target, level, internalFormat,
2852 format, type, width, height, depth, border,
2853 pixels))
2854 return;
2855 }
2856
2857 /* Here we convert a cpal compressed image into a regular glTexImage2D
2858 * call by decompressing the texture. If we really want to support cpal
2859 * textures in any driver this would have to be changed.
2860 */
2861 if (ctx->API == API_OPENGLES && compressed && dims == 2) {
2862 switch (internalFormat) {
2863 case GL_PALETTE4_RGB8_OES:
2864 case GL_PALETTE4_RGBA8_OES:
2865 case GL_PALETTE4_R5_G6_B5_OES:
2866 case GL_PALETTE4_RGBA4_OES:
2867 case GL_PALETTE4_RGB5_A1_OES:
2868 case GL_PALETTE8_RGB8_OES:
2869 case GL_PALETTE8_RGBA8_OES:
2870 case GL_PALETTE8_R5_G6_B5_OES:
2871 case GL_PALETTE8_RGBA4_OES:
2872 case GL_PALETTE8_RGB5_A1_OES:
2873 _mesa_cpal_compressed_teximage2d(target, level, internalFormat,
2874 width, height, imageSize, pixels);
2875 return;
2876 }
2877 }
2878
2879 texObj = _mesa_get_current_tex_object(ctx, target);
2880 assert(texObj);
2881
2882 if (compressed) {
2883 /* For glCompressedTexImage() the driver has no choice about the
2884 * texture format since we'll never transcode the user's compressed
2885 * image data. The internalFormat was error checked earlier.
2886 */
2887 texFormat = _mesa_glenum_to_compressed_format(internalFormat);
2888 }
2889 else {
2890 /* In case of HALF_FLOAT_OES or FLOAT_OES, find corresponding sized
2891 * internal floating point format for the given base format.
2892 */
2893 if (_mesa_is_gles(ctx) && format == internalFormat) {
2894 if (type == GL_FLOAT) {
2895 texObj->_IsFloat = GL_TRUE;
2896 } else if (type == GL_HALF_FLOAT_OES || type == GL_HALF_FLOAT) {
2897 texObj->_IsHalfFloat = GL_TRUE;
2898 }
2899
2900 internalFormat = adjust_for_oes_float_texture(format, type);
2901 }
2902
2903 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
2904 internalFormat, format, type);
2905 }
2906
2907 assert(texFormat != MESA_FORMAT_NONE);
2908
2909 /* check that width, height, depth are legal for the mipmap level */
2910 dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, level, width,
2911 height, depth, border);
2912
2913 /* check that the texture won't take too much memory, etc */
2914 sizeOK = ctx->Driver.TestProxyTexImage(ctx, proxy_target(target),
2915 level, texFormat,
2916 width, height, depth, border);
2917
2918 if (_mesa_is_proxy_texture(target)) {
2919 /* Proxy texture: just clear or set state depending on error checking */
2920 struct gl_texture_image *texImage =
2921 get_proxy_tex_image(ctx, target, level);
2922
2923 if (!texImage)
2924 return; /* GL_OUT_OF_MEMORY already recorded */
2925
2926 if (dimensionsOK && sizeOK) {
2927 _mesa_init_teximage_fields(ctx, texImage, width, height, depth,
2928 border, internalFormat, texFormat);
2929 }
2930 else {
2931 clear_teximage_fields(texImage);
2932 }
2933 }
2934 else {
2935 /* non-proxy target */
2936 const GLuint face = _mesa_tex_target_to_face(target);
2937 struct gl_texture_image *texImage;
2938
2939 if (!dimensionsOK) {
2940 _mesa_error(ctx, GL_INVALID_VALUE,
2941 "%s%uD(invalid width or height or depth)",
2942 func, dims);
2943 return;
2944 }
2945
2946 if (!sizeOK) {
2947 _mesa_error(ctx, GL_OUT_OF_MEMORY,
2948 "%s%uD(image too large: %d x %d x %d, %s format)",
2949 func, dims, width, height, depth,
2950 _mesa_enum_to_string(internalFormat));
2951 return;
2952 }
2953
2954 /* Allow a hardware driver to just strip out the border, to provide
2955 * reliable but slightly incorrect hardware rendering instead of
2956 * rarely-tested software fallback rendering.
2957 */
2958 if (border && ctx->Const.StripTextureBorder) {
2959 strip_texture_border(target, &width, &height, &depth, unpack,
2960 &unpack_no_border);
2961 border = 0;
2962 unpack = &unpack_no_border;
2963 }
2964
2965 if (ctx->NewState & _NEW_PIXEL)
2966 _mesa_update_state(ctx);
2967
2968 _mesa_lock_texture(ctx, texObj);
2969 {
2970 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2971
2972 if (!texImage) {
2973 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s%uD", func, dims);
2974 }
2975 else {
2976 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
2977
2978 _mesa_init_teximage_fields(ctx, texImage,
2979 width, height, depth,
2980 border, internalFormat, texFormat);
2981
2982 /* Give the texture to the driver. <pixels> may be null. */
2983 if (width > 0 && height > 0 && depth > 0) {
2984 if (compressed) {
2985 ctx->Driver.CompressedTexImage(ctx, dims, texImage,
2986 imageSize, pixels);
2987 }
2988 else {
2989 ctx->Driver.TexImage(ctx, dims, texImage, format,
2990 type, pixels, unpack);
2991 }
2992 }
2993
2994 check_gen_mipmap(ctx, target, texObj, level);
2995
2996 _mesa_update_fbo_texture(ctx, texObj, face, level);
2997
2998 _mesa_dirty_texobj(ctx, texObj);
2999 }
3000 }
3001 _mesa_unlock_texture(ctx, texObj);
3002 }
3003 }
3004
3005
3006
3007 /*
3008 * Called from the API. Note that width includes the border.
3009 */
3010 void GLAPIENTRY
3011 _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
3012 GLsizei width, GLint border, GLenum format,
3013 GLenum type, const GLvoid *pixels )
3014 {
3015 GET_CURRENT_CONTEXT(ctx);
3016 teximage(ctx, GL_FALSE, 1, target, level, internalFormat, width, 1, 1,
3017 border, format, type, 0, pixels);
3018 }
3019
3020
3021 void GLAPIENTRY
3022 _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
3023 GLsizei width, GLsizei height, GLint border,
3024 GLenum format, GLenum type,
3025 const GLvoid *pixels )
3026 {
3027 GET_CURRENT_CONTEXT(ctx);
3028 teximage(ctx, GL_FALSE, 2, target, level, internalFormat, width, height, 1,
3029 border, format, type, 0, pixels);
3030 }
3031
3032
3033 /*
3034 * Called by the API or display list executor.
3035 * Note that width and height include the border.
3036 */
3037 void GLAPIENTRY
3038 _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
3039 GLsizei width, GLsizei height, GLsizei depth,
3040 GLint border, GLenum format, GLenum type,
3041 const GLvoid *pixels )
3042 {
3043 GET_CURRENT_CONTEXT(ctx);
3044 teximage(ctx, GL_FALSE, 3, target, level, internalFormat,
3045 width, height, depth,
3046 border, format, type, 0, pixels);
3047 }
3048
3049
3050 void GLAPIENTRY
3051 _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
3052 GLsizei width, GLsizei height, GLsizei depth,
3053 GLint border, GLenum format, GLenum type,
3054 const GLvoid *pixels )
3055 {
3056 _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
3057 depth, border, format, type, pixels);
3058 }
3059
3060
3061 void GLAPIENTRY
3062 _mesa_EGLImageTargetTexture2DOES (GLenum target, GLeglImageOES image)
3063 {
3064 struct gl_texture_object *texObj;
3065 struct gl_texture_image *texImage;
3066 bool valid_target;
3067 GET_CURRENT_CONTEXT(ctx);
3068 FLUSH_VERTICES(ctx, 0);
3069
3070 switch (target) {
3071 case GL_TEXTURE_2D:
3072 valid_target = ctx->Extensions.OES_EGL_image;
3073 break;
3074 case GL_TEXTURE_EXTERNAL_OES:
3075 valid_target =
3076 _mesa_is_gles(ctx) ? ctx->Extensions.OES_EGL_image_external : false;
3077 break;
3078 default:
3079 valid_target = false;
3080 break;
3081 }
3082
3083 if (!valid_target) {
3084 _mesa_error(ctx, GL_INVALID_ENUM,
3085 "glEGLImageTargetTexture2D(target=%d)", target);
3086 return;
3087 }
3088
3089 if (!image) {
3090 _mesa_error(ctx, GL_INVALID_OPERATION,
3091 "glEGLImageTargetTexture2D(image=%p)", image);
3092 return;
3093 }
3094
3095 if (ctx->NewState & _NEW_PIXEL)
3096 _mesa_update_state(ctx);
3097
3098 texObj = _mesa_get_current_tex_object(ctx, target);
3099 if (!texObj)
3100 return;
3101
3102 _mesa_lock_texture(ctx, texObj);
3103
3104 if (texObj->Immutable) {
3105 _mesa_error(ctx, GL_INVALID_OPERATION,
3106 "glEGLImageTargetTexture2D(texture is immutable)");
3107 _mesa_unlock_texture(ctx, texObj);
3108 return;
3109 }
3110
3111 texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
3112 if (!texImage) {
3113 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glEGLImageTargetTexture2D");
3114 } else {
3115 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3116
3117 ctx->Driver.EGLImageTargetTexture2D(ctx, target,
3118 texObj, texImage, image);
3119
3120 _mesa_dirty_texobj(ctx, texObj);
3121 }
3122 _mesa_unlock_texture(ctx, texObj);
3123 }
3124
3125
3126 /**
3127 * Helper that implements the glTexSubImage1/2/3D()
3128 * and glTextureSubImage1/2/3D() functions.
3129 */
3130 void
3131 _mesa_texture_sub_image(struct gl_context *ctx, GLuint dims,
3132 struct gl_texture_object *texObj,
3133 struct gl_texture_image *texImage,
3134 GLenum target, GLint level,
3135 GLint xoffset, GLint yoffset, GLint zoffset,
3136 GLsizei width, GLsizei height, GLsizei depth,
3137 GLenum format, GLenum type, const GLvoid *pixels,
3138 bool dsa)
3139 {
3140 FLUSH_VERTICES(ctx, 0);
3141
3142 if (ctx->NewState & _NEW_PIXEL)
3143 _mesa_update_state(ctx);
3144
3145 _mesa_lock_texture(ctx, texObj);
3146 {
3147 if (width > 0 && height > 0 && depth > 0) {
3148 /* If we have a border, offset=-1 is legal. Bias by border width. */
3149 switch (dims) {
3150 case 3:
3151 if (target != GL_TEXTURE_2D_ARRAY)
3152 zoffset += texImage->Border;
3153 /* fall-through */
3154 case 2:
3155 if (target != GL_TEXTURE_1D_ARRAY)
3156 yoffset += texImage->Border;
3157 /* fall-through */
3158 case 1:
3159 xoffset += texImage->Border;
3160 }
3161
3162 ctx->Driver.TexSubImage(ctx, dims, texImage,
3163 xoffset, yoffset, zoffset,
3164 width, height, depth,
3165 format, type, pixels, &ctx->Unpack);
3166
3167 check_gen_mipmap(ctx, target, texObj, level);
3168
3169 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3170 * the texel data, not the texture format, size, etc.
3171 */
3172 }
3173 }
3174 _mesa_unlock_texture(ctx, texObj);
3175 }
3176
3177 /**
3178 * Implement all the glTexSubImage1/2/3D() functions.
3179 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3180 */
3181 static void
3182 texsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3183 GLint xoffset, GLint yoffset, GLint zoffset,
3184 GLsizei width, GLsizei height, GLsizei depth,
3185 GLenum format, GLenum type, const GLvoid *pixels,
3186 const char *callerName)
3187 {
3188 struct gl_texture_object *texObj;
3189 struct gl_texture_image *texImage;
3190
3191 /* check target (proxies not allowed) */
3192 if (!legal_texsubimage_target(ctx, dims, target, false)) {
3193 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(target=%s)",
3194 dims, _mesa_enum_to_string(target));
3195 return;
3196 }
3197
3198 texObj = _mesa_get_current_tex_object(ctx, target);
3199 if (!texObj)
3200 return;
3201
3202 if (texsubimage_error_check(ctx, dims, texObj, target, level,
3203 xoffset, yoffset, zoffset,
3204 width, height, depth, format, type,
3205 pixels, false, callerName)) {
3206 return; /* error was detected */
3207 }
3208
3209 texImage = _mesa_select_tex_image(texObj, target, level);
3210 /* texsubimage_error_check ensures that texImage is not NULL */
3211
3212 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3213 _mesa_debug(ctx, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3214 dims,
3215 _mesa_enum_to_string(target), level,
3216 xoffset, yoffset, zoffset, width, height, depth,
3217 _mesa_enum_to_string(format),
3218 _mesa_enum_to_string(type), pixels);
3219
3220 _mesa_texture_sub_image(ctx, dims, texObj, texImage, target, level,
3221 xoffset, yoffset, zoffset, width, height, depth,
3222 format, type, pixels, false);
3223 }
3224
3225
3226 /**
3227 * Implement all the glTextureSubImage1/2/3D() functions.
3228 * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3229 */
3230 static void
3231 texturesubimage(struct gl_context *ctx, GLuint dims,
3232 GLuint texture, GLint level,
3233 GLint xoffset, GLint yoffset, GLint zoffset,
3234 GLsizei width, GLsizei height, GLsizei depth,
3235 GLenum format, GLenum type, const GLvoid *pixels,
3236 const char *callerName)
3237 {
3238 struct gl_texture_object *texObj;
3239 struct gl_texture_image *texImage;
3240 int i;
3241
3242 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3243 _mesa_debug(ctx,
3244 "glTextureSubImage%uD %d %d %d %d %d %d %d %d %s %s %p\n",
3245 dims, texture, level,
3246 xoffset, yoffset, zoffset, width, height, depth,
3247 _mesa_enum_to_string(format),
3248 _mesa_enum_to_string(type), pixels);
3249
3250 /* Get the texture object by Name. */
3251 texObj = _mesa_lookup_texture(ctx, texture);
3252 if (!texObj) {
3253 _mesa_error(ctx, GL_INVALID_OPERATION, "glTextureSubImage%uD(texture)",
3254 dims);
3255 return;
3256 }
3257
3258 /* check target (proxies not allowed) */
3259 if (!legal_texsubimage_target(ctx, dims, texObj->Target, true)) {
3260 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target=%s)",
3261 callerName, _mesa_enum_to_string(texObj->Target));
3262 return;
3263 }
3264
3265 if (texsubimage_error_check(ctx, dims, texObj, texObj->Target, level,
3266 xoffset, yoffset, zoffset,
3267 width, height, depth, format, type,
3268 pixels, true, callerName)) {
3269 return; /* error was detected */
3270 }
3271
3272
3273 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
3274 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
3275 GLint imageStride;
3276
3277 /*
3278 * What do we do if the user created a texture with the following code
3279 * and then called this function with its handle?
3280 *
3281 * GLuint tex;
3282 * glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &tex);
3283 * glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
3284 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, ...);
3285 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, ...);
3286 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, ...);
3287 * // Note: GL_TEXTURE_CUBE_MAP_NEGATIVE_Y not set, or given the
3288 * // wrong format, or given the wrong size, etc.
3289 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, ...);
3290 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, ...);
3291 *
3292 * A bug has been filed against the spec for this case. In the
3293 * meantime, we will check for cube completeness.
3294 *
3295 * According to Section 8.17 Texture Completeness in the OpenGL 4.5
3296 * Core Profile spec (30.10.2014):
3297 * "[A] cube map texture is cube complete if the
3298 * following conditions all hold true: The [base level] texture
3299 * images of each of the six cube map faces have identical, positive,
3300 * and square dimensions. The [base level] images were each specified
3301 * with the same internal format."
3302 *
3303 * It seems reasonable to check for cube completeness of an arbitrary
3304 * level here so that the image data has a consistent format and size.
3305 */
3306 if (!_mesa_cube_level_complete(texObj, level)) {
3307 _mesa_error(ctx, GL_INVALID_OPERATION,
3308 "glTextureSubImage%uD(cube map incomplete)",
3309 dims);
3310 return;
3311 }
3312
3313 imageStride = _mesa_image_image_stride(&ctx->Unpack, width, height,
3314 format, type);
3315 /* Copy in each face. */
3316 for (i = zoffset; i < zoffset + depth; ++i) {
3317 texImage = texObj->Image[i][level];
3318 assert(texImage);
3319
3320 _mesa_texture_sub_image(ctx, 3, texObj, texImage, texObj->Target,
3321 level, xoffset, yoffset, 0,
3322 width, height, 1, format,
3323 type, pixels, true);
3324 pixels = (GLubyte *) pixels + imageStride;
3325 }
3326 }
3327 else {
3328 texImage = _mesa_select_tex_image(texObj, texObj->Target, level);
3329 assert(texImage);
3330
3331 _mesa_texture_sub_image(ctx, dims, texObj, texImage, texObj->Target,
3332 level, xoffset, yoffset, zoffset,
3333 width, height, depth, format,
3334 type, pixels, true);
3335 }
3336 }
3337
3338
3339 void GLAPIENTRY
3340 _mesa_TexSubImage1D( GLenum target, GLint level,
3341 GLint xoffset, GLsizei width,
3342 GLenum format, GLenum type,
3343 const GLvoid *pixels )
3344 {
3345 GET_CURRENT_CONTEXT(ctx);
3346 texsubimage(ctx, 1, target, level,
3347 xoffset, 0, 0,
3348 width, 1, 1,
3349 format, type, pixels, "glTexSubImage1D");
3350 }
3351
3352
3353 void GLAPIENTRY
3354 _mesa_TexSubImage2D( GLenum target, GLint level,
3355 GLint xoffset, GLint yoffset,
3356 GLsizei width, GLsizei height,
3357 GLenum format, GLenum type,
3358 const GLvoid *pixels )
3359 {
3360 GET_CURRENT_CONTEXT(ctx);
3361 texsubimage(ctx, 2, target, level,
3362 xoffset, yoffset, 0,
3363 width, height, 1,
3364 format, type, pixels, "glTexSubImage2D");
3365 }
3366
3367
3368
3369 void GLAPIENTRY
3370 _mesa_TexSubImage3D( GLenum target, GLint level,
3371 GLint xoffset, GLint yoffset, GLint zoffset,
3372 GLsizei width, GLsizei height, GLsizei depth,
3373 GLenum format, GLenum type,
3374 const GLvoid *pixels )
3375 {
3376 GET_CURRENT_CONTEXT(ctx);
3377 texsubimage(ctx, 3, target, level,
3378 xoffset, yoffset, zoffset,
3379 width, height, depth,
3380 format, type, pixels, "glTexSubImage3D");
3381 }
3382
3383 void GLAPIENTRY
3384 _mesa_TextureSubImage1D(GLuint texture, GLint level,
3385 GLint xoffset, GLsizei width,
3386 GLenum format, GLenum type,
3387 const GLvoid *pixels)
3388 {
3389 GET_CURRENT_CONTEXT(ctx);
3390 texturesubimage(ctx, 1, texture, level,
3391 xoffset, 0, 0,
3392 width, 1, 1,
3393 format, type, pixels, "glTextureSubImage1D");
3394 }
3395
3396
3397 void GLAPIENTRY
3398 _mesa_TextureSubImage2D(GLuint texture, GLint level,
3399 GLint xoffset, GLint yoffset,
3400 GLsizei width, GLsizei height,
3401 GLenum format, GLenum type,
3402 const GLvoid *pixels)
3403 {
3404 GET_CURRENT_CONTEXT(ctx);
3405 texturesubimage(ctx, 2, texture, level,
3406 xoffset, yoffset, 0,
3407 width, height, 1,
3408 format, type, pixels, "glTextureSubImage2D");
3409 }
3410
3411
3412 void GLAPIENTRY
3413 _mesa_TextureSubImage3D(GLuint texture, GLint level,
3414 GLint xoffset, GLint yoffset, GLint zoffset,
3415 GLsizei width, GLsizei height, GLsizei depth,
3416 GLenum format, GLenum type,
3417 const GLvoid *pixels)
3418 {
3419 GET_CURRENT_CONTEXT(ctx);
3420 texturesubimage(ctx, 3, texture, level,
3421 xoffset, yoffset, zoffset,
3422 width, height, depth,
3423 format, type, pixels, "glTextureSubImage3D");
3424 }
3425
3426
3427 /**
3428 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3429 * from. This depends on whether the texture contains color or depth values.
3430 */
3431 static struct gl_renderbuffer *
3432 get_copy_tex_image_source(struct gl_context *ctx, mesa_format texFormat)
3433 {
3434 if (_mesa_get_format_bits(texFormat, GL_DEPTH_BITS) > 0) {
3435 /* reading from depth/stencil buffer */
3436 return ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
3437 } else if (_mesa_get_format_bits(texFormat, GL_STENCIL_BITS) > 0) {
3438 return ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
3439 } else {
3440 /* copying from color buffer */
3441 return ctx->ReadBuffer->_ColorReadBuffer;
3442 }
3443 }
3444
3445 static void
3446 copytexsubimage_by_slice(struct gl_context *ctx,
3447 struct gl_texture_image *texImage,
3448 GLuint dims,
3449 GLint xoffset, GLint yoffset, GLint zoffset,
3450 struct gl_renderbuffer *rb,
3451 GLint x, GLint y,
3452 GLsizei width, GLsizei height)
3453 {
3454 if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
3455 int slice;
3456
3457 /* For 1D arrays, we copy each scanline of the source rectangle into the
3458 * next array slice.
3459 */
3460 assert(zoffset == 0);
3461
3462 for (slice = 0; slice < height; slice++) {
3463 assert(yoffset + slice < texImage->Height);
3464 ctx->Driver.CopyTexSubImage(ctx, 2, texImage,
3465 xoffset, 0, yoffset + slice,
3466 rb, x, y + slice, width, 1);
3467 }
3468 } else {
3469 ctx->Driver.CopyTexSubImage(ctx, dims, texImage,
3470 xoffset, yoffset, zoffset,
3471 rb, x, y, width, height);
3472 }
3473 }
3474
3475 static GLboolean
3476 formats_differ_in_component_sizes(mesa_format f1, mesa_format f2)
3477 {
3478 GLint f1_r_bits = _mesa_get_format_bits(f1, GL_RED_BITS);
3479 GLint f1_g_bits = _mesa_get_format_bits(f1, GL_GREEN_BITS);
3480 GLint f1_b_bits = _mesa_get_format_bits(f1, GL_BLUE_BITS);
3481 GLint f1_a_bits = _mesa_get_format_bits(f1, GL_ALPHA_BITS);
3482
3483 GLint f2_r_bits = _mesa_get_format_bits(f2, GL_RED_BITS);
3484 GLint f2_g_bits = _mesa_get_format_bits(f2, GL_GREEN_BITS);
3485 GLint f2_b_bits = _mesa_get_format_bits(f2, GL_BLUE_BITS);
3486 GLint f2_a_bits = _mesa_get_format_bits(f2, GL_ALPHA_BITS);
3487
3488 if ((f1_r_bits && f2_r_bits && f1_r_bits != f2_r_bits)
3489 || (f1_g_bits && f2_g_bits && f1_g_bits != f2_g_bits)
3490 || (f1_b_bits && f2_b_bits && f1_b_bits != f2_b_bits)
3491 || (f1_a_bits && f2_a_bits && f1_a_bits != f2_a_bits))
3492 return GL_TRUE;
3493
3494 return GL_FALSE;
3495 }
3496
3497 static bool
3498 can_avoid_reallocation(struct gl_texture_image *texImage, GLenum internalFormat,
3499 mesa_format texFormat, GLint x, GLint y, GLsizei width,
3500 GLsizei height, GLint border)
3501 {
3502 if (texImage->InternalFormat != internalFormat)
3503 return false;
3504 if (texImage->TexFormat != texFormat)
3505 return false;
3506 if (texImage->Border != border)
3507 return false;
3508 if (texImage->Width2 != width)
3509 return false;
3510 if (texImage->Height2 != height)
3511 return false;
3512 return true;
3513 }
3514
3515 /**
3516 * Implement the glCopyTexImage1/2D() functions.
3517 */
3518 static void
3519 copyteximage(struct gl_context *ctx, GLuint dims,
3520 GLenum target, GLint level, GLenum internalFormat,
3521 GLint x, GLint y, GLsizei width, GLsizei height, GLint border )
3522 {
3523 struct gl_texture_object *texObj;
3524 struct gl_texture_image *texImage;
3525 const GLuint face = _mesa_tex_target_to_face(target);
3526 mesa_format texFormat;
3527 struct gl_renderbuffer *rb;
3528
3529 FLUSH_VERTICES(ctx, 0);
3530
3531 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3532 _mesa_debug(ctx, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3533 dims,
3534 _mesa_enum_to_string(target), level,
3535 _mesa_enum_to_string(internalFormat),
3536 x, y, width, height, border);
3537
3538 if (ctx->NewState & NEW_COPY_TEX_STATE)
3539 _mesa_update_state(ctx);
3540
3541 if (copytexture_error_check(ctx, dims, target, level, internalFormat,
3542 width, height, border))
3543 return;
3544
3545 if (!_mesa_legal_texture_dimensions(ctx, target, level, width, height,
3546 1, border)) {
3547 _mesa_error(ctx, GL_INVALID_VALUE,
3548 "glCopyTexImage%uD(invalid width or height)", dims);
3549 return;
3550 }
3551
3552 texObj = _mesa_get_current_tex_object(ctx, target);
3553 assert(texObj);
3554
3555 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
3556 internalFormat, GL_NONE, GL_NONE);
3557
3558 /* First check if reallocating the texture buffer can be avoided.
3559 * Without the realloc the copy can be 20x faster.
3560 */
3561 _mesa_lock_texture(ctx, texObj);
3562 {
3563 texImage = _mesa_select_tex_image(texObj, target, level);
3564 if (texImage && can_avoid_reallocation(texImage, internalFormat, texFormat,
3565 x, y, width, height, border)) {
3566 _mesa_unlock_texture(ctx, texObj);
3567 return _mesa_copy_texture_sub_image(ctx, dims, texObj, target, level,
3568 0, 0, 0, x, y, width, height,
3569 "CopyTexImage");
3570 }
3571 }
3572 _mesa_unlock_texture(ctx, texObj);
3573 _mesa_perf_debug(ctx, MESA_DEBUG_SEVERITY_LOW, "glCopyTexImage "
3574 "can't avoid reallocating texture storage\n");
3575
3576 rb = _mesa_get_read_renderbuffer_for_format(ctx, internalFormat);
3577
3578 if (_mesa_is_gles3(ctx)) {
3579 if (_mesa_is_enum_format_unsized(internalFormat)) {
3580 /* Conversion from GL_RGB10_A2 source buffer format is not allowed in
3581 * OpenGL ES 3.0. Khronos bug# 9807.
3582 */
3583 if (rb->InternalFormat == GL_RGB10_A2) {
3584 _mesa_error(ctx, GL_INVALID_OPERATION,
3585 "glCopyTexImage%uD(Reading from GL_RGB10_A2 buffer"
3586 " and writing to unsized internal format)", dims);
3587 return;
3588 }
3589 }
3590 /* From Page 139 of OpenGL ES 3.0 spec:
3591 * "If internalformat is sized, the internal format of the new texel
3592 * array is internalformat, and this is also the new texel array’s
3593 * effective internal format. If the component sizes of internalformat
3594 * do not exactly match the corresponding component sizes of the source
3595 * buffer’s effective internal format, described below, an
3596 * INVALID_OPERATION error is generated. If internalformat is unsized,
3597 * the internal format of the new texel array is the effective internal
3598 * format of the source buffer, and this is also the new texel array’s
3599 * effective internal format.
3600 */
3601 else if (formats_differ_in_component_sizes (texFormat, rb->Format)) {
3602 _mesa_error(ctx, GL_INVALID_OPERATION,
3603 "glCopyTexImage%uD(componenet size changed in"
3604 " internal format)", dims);
3605 return;
3606 }
3607 }
3608
3609 assert(texFormat != MESA_FORMAT_NONE);
3610
3611 if (!ctx->Driver.TestProxyTexImage(ctx, proxy_target(target),
3612 level, texFormat,
3613 width, height, 1, border)) {
3614 _mesa_error(ctx, GL_OUT_OF_MEMORY,
3615 "glCopyTexImage%uD(image too large)", dims);
3616 return;
3617 }
3618
3619 if (border && ctx->Const.StripTextureBorder) {
3620 x += border;
3621 width -= border * 2;
3622 if (dims == 2) {
3623 y += border;
3624 height -= border * 2;
3625 }
3626 border = 0;
3627 }
3628
3629 _mesa_lock_texture(ctx, texObj);
3630 {
3631 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3632
3633 if (!texImage) {
3634 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
3635 }
3636 else {
3637 GLint srcX = x, srcY = y, dstX = 0, dstY = 0, dstZ = 0;
3638
3639 /* Free old texture image */
3640 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3641
3642 _mesa_init_teximage_fields(ctx, texImage, width, height, 1,
3643 border, internalFormat, texFormat);
3644
3645 if (width && height) {
3646 /* Allocate texture memory (no pixel data yet) */
3647 ctx->Driver.AllocTextureImageBuffer(ctx, texImage);
3648
3649 if (_mesa_clip_copytexsubimage(ctx, &dstX, &dstY, &srcX, &srcY,
3650 &width, &height)) {
3651 struct gl_renderbuffer *srcRb =
3652 get_copy_tex_image_source(ctx, texImage->TexFormat);
3653
3654 copytexsubimage_by_slice(ctx, texImage, dims,
3655 dstX, dstY, dstZ,
3656 srcRb, srcX, srcY, width, height);
3657 }
3658
3659 check_gen_mipmap(ctx, target, texObj, level);
3660 }
3661
3662 _mesa_update_fbo_texture(ctx, texObj, face, level);
3663
3664 _mesa_dirty_texobj(ctx, texObj);
3665 }
3666 }
3667 _mesa_unlock_texture(ctx, texObj);
3668 }
3669
3670
3671
3672 void GLAPIENTRY
3673 _mesa_CopyTexImage1D( GLenum target, GLint level,
3674 GLenum internalFormat,
3675 GLint x, GLint y,
3676 GLsizei width, GLint border )
3677 {
3678 GET_CURRENT_CONTEXT(ctx);
3679 copyteximage(ctx, 1, target, level, internalFormat, x, y, width, 1, border);
3680 }
3681
3682
3683
3684 void GLAPIENTRY
3685 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
3686 GLint x, GLint y, GLsizei width, GLsizei height,
3687 GLint border )
3688 {
3689 GET_CURRENT_CONTEXT(ctx);
3690 copyteximage(ctx, 2, target, level, internalFormat,
3691 x, y, width, height, border);
3692 }
3693
3694 /**
3695 * Implementation for glCopyTex(ture)SubImage1/2/3D() functions.
3696 */
3697 void
3698 _mesa_copy_texture_sub_image(struct gl_context *ctx, GLuint dims,
3699 struct gl_texture_object *texObj,
3700 GLenum target, GLint level,
3701 GLint xoffset, GLint yoffset, GLint zoffset,
3702 GLint x, GLint y,
3703 GLsizei width, GLsizei height,
3704 const char *caller)
3705 {
3706 struct gl_texture_image *texImage;
3707
3708 FLUSH_VERTICES(ctx, 0);
3709
3710 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3711 _mesa_debug(ctx, "%s %s %d %d %d %d %d %d %d %d\n", caller,
3712 _mesa_enum_to_string(target),
3713 level, xoffset, yoffset, zoffset, x, y, width, height);
3714
3715 if (ctx->NewState & NEW_COPY_TEX_STATE)
3716 _mesa_update_state(ctx);
3717
3718 if (copytexsubimage_error_check(ctx, dims, texObj, target, level,
3719 xoffset, yoffset, zoffset,
3720 width, height, caller)) {
3721 return;
3722 }
3723
3724 _mesa_lock_texture(ctx, texObj);
3725 {
3726 texImage = _mesa_select_tex_image(texObj, target, level);
3727
3728 /* If we have a border, offset=-1 is legal. Bias by border width. */
3729 switch (dims) {
3730 case 3:
3731 if (target != GL_TEXTURE_2D_ARRAY)
3732 zoffset += texImage->Border;
3733 /* fall-through */
3734 case 2:
3735 if (target != GL_TEXTURE_1D_ARRAY)
3736 yoffset += texImage->Border;
3737 /* fall-through */
3738 case 1:
3739 xoffset += texImage->Border;
3740 }
3741
3742 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
3743 &width, &height)) {
3744 struct gl_renderbuffer *srcRb =
3745 get_copy_tex_image_source(ctx, texImage->TexFormat);
3746
3747 copytexsubimage_by_slice(ctx, texImage, dims,
3748 xoffset, yoffset, zoffset,
3749 srcRb, x, y, width, height);
3750
3751 check_gen_mipmap(ctx, target, texObj, level);
3752
3753 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3754 * the texel data, not the texture format, size, etc.
3755 */
3756 }
3757 }
3758 _mesa_unlock_texture(ctx, texObj);
3759 }
3760
3761 void GLAPIENTRY
3762 _mesa_CopyTexSubImage1D( GLenum target, GLint level,
3763 GLint xoffset, GLint x, GLint y, GLsizei width )
3764 {
3765 struct gl_texture_object* texObj;
3766 const char *self = "glCopyTexSubImage1D";
3767 GET_CURRENT_CONTEXT(ctx);
3768
3769 /* Check target (proxies not allowed). Target must be checked prior to
3770 * calling _mesa_get_current_tex_object.
3771 */
3772 if (!legal_texsubimage_target(ctx, 1, target, false)) {
3773 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
3774 _mesa_enum_to_string(target));
3775 return;
3776 }
3777
3778 texObj = _mesa_get_current_tex_object(ctx, target);
3779 if (!texObj)
3780 return;
3781
3782 _mesa_copy_texture_sub_image(ctx, 1, texObj, target, level, xoffset, 0, 0,
3783 x, y, width, 1, self);
3784 }
3785
3786
3787
3788 void GLAPIENTRY
3789 _mesa_CopyTexSubImage2D( GLenum target, GLint level,
3790 GLint xoffset, GLint yoffset,
3791 GLint x, GLint y, GLsizei width, GLsizei height )
3792 {
3793 struct gl_texture_object* texObj;
3794 const char *self = "glCopyTexSubImage2D";
3795 GET_CURRENT_CONTEXT(ctx);
3796
3797 /* Check target (proxies not allowed). Target must be checked prior to
3798 * calling _mesa_get_current_tex_object.
3799 */
3800 if (!legal_texsubimage_target(ctx, 2, target, false)) {
3801 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
3802 _mesa_enum_to_string(target));
3803 return;
3804 }
3805
3806 texObj = _mesa_get_current_tex_object(ctx, target);
3807 if (!texObj)
3808 return;
3809
3810 _mesa_copy_texture_sub_image(ctx, 2, texObj, target, level,
3811 xoffset, yoffset, 0,
3812 x, y, width, height, self);
3813 }
3814
3815
3816
3817 void GLAPIENTRY
3818 _mesa_CopyTexSubImage3D( GLenum target, GLint level,
3819 GLint xoffset, GLint yoffset, GLint zoffset,
3820 GLint x, GLint y, GLsizei width, GLsizei height )
3821 {
3822 struct gl_texture_object* texObj;
3823 const char *self = "glCopyTexSubImage3D";
3824 GET_CURRENT_CONTEXT(ctx);
3825
3826 /* Check target (proxies not allowed). Target must be checked prior to
3827 * calling _mesa_get_current_tex_object.
3828 */
3829 if (!legal_texsubimage_target(ctx, 3, target, false)) {
3830 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
3831 _mesa_enum_to_string(target));
3832 return;
3833 }
3834
3835 texObj = _mesa_get_current_tex_object(ctx, target);
3836 if (!texObj)
3837 return;
3838
3839 _mesa_copy_texture_sub_image(ctx, 3, texObj, target, level,
3840 xoffset, yoffset, zoffset,
3841 x, y, width, height, self);
3842 }
3843
3844 void GLAPIENTRY
3845 _mesa_CopyTextureSubImage1D(GLuint texture, GLint level,
3846 GLint xoffset, GLint x, GLint y, GLsizei width)
3847 {
3848 struct gl_texture_object* texObj;
3849 const char *self = "glCopyTextureSubImage1D";
3850 GET_CURRENT_CONTEXT(ctx);
3851
3852 texObj = _mesa_lookup_texture_err(ctx, texture, self);
3853 if (!texObj)
3854 return;
3855
3856 /* Check target (proxies not allowed). */
3857 if (!legal_texsubimage_target(ctx, 1, texObj->Target, true)) {
3858 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
3859 _mesa_enum_to_string(texObj->Target));
3860 return;
3861 }
3862
3863 _mesa_copy_texture_sub_image(ctx, 1, texObj, texObj->Target, level,
3864 xoffset, 0, 0, x, y, width, 1, self);
3865 }
3866
3867 void GLAPIENTRY
3868 _mesa_CopyTextureSubImage2D(GLuint texture, GLint level,
3869 GLint xoffset, GLint yoffset,
3870 GLint x, GLint y, GLsizei width, GLsizei height)
3871 {
3872 struct gl_texture_object* texObj;
3873 const char *self = "glCopyTextureSubImage2D";
3874 GET_CURRENT_CONTEXT(ctx);
3875
3876 texObj = _mesa_lookup_texture_err(ctx, texture, self);
3877 if (!texObj)
3878 return;
3879
3880 /* Check target (proxies not allowed). */
3881 if (!legal_texsubimage_target(ctx, 2, texObj->Target, true)) {
3882 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
3883 _mesa_enum_to_string(texObj->Target));
3884 return;
3885 }
3886
3887 _mesa_copy_texture_sub_image(ctx, 2, texObj, texObj->Target, level,
3888 xoffset, yoffset, 0,
3889 x, y, width, height, self);
3890 }
3891
3892
3893
3894 void GLAPIENTRY
3895 _mesa_CopyTextureSubImage3D(GLuint texture, GLint level,
3896 GLint xoffset, GLint yoffset, GLint zoffset,
3897 GLint x, GLint y, GLsizei width, GLsizei height)
3898 {
3899 struct gl_texture_object* texObj;
3900 const char *self = "glCopyTextureSubImage3D";
3901 GET_CURRENT_CONTEXT(ctx);
3902
3903 texObj = _mesa_lookup_texture_err(ctx, texture, self);
3904 if (!texObj)
3905 return;
3906
3907 /* Check target (proxies not allowed). */
3908 if (!legal_texsubimage_target(ctx, 3, texObj->Target, true)) {
3909 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
3910 _mesa_enum_to_string(texObj->Target));
3911 return;
3912 }
3913
3914 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
3915 /* Act like CopyTexSubImage2D */
3916 _mesa_copy_texture_sub_image(ctx, 2, texObj,
3917 GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset,
3918 level, xoffset, yoffset, 0,
3919 x, y, width, height, self);
3920 }
3921 else
3922 _mesa_copy_texture_sub_image(ctx, 3, texObj, texObj->Target, level,
3923 xoffset, yoffset, zoffset,
3924 x, y, width, height, self);
3925 }
3926
3927 static bool
3928 check_clear_tex_image(struct gl_context *ctx,
3929 const char *function,
3930 struct gl_texture_image *texImage,
3931 GLenum format, GLenum type,
3932 const void *data,
3933 GLubyte *clearValue)
3934 {
3935 struct gl_texture_object *texObj = texImage->TexObject;
3936 static const GLubyte zeroData[MAX_PIXEL_BYTES];
3937 GLenum internalFormat = texImage->InternalFormat;
3938 GLenum err;
3939
3940 if (texObj->Target == GL_TEXTURE_BUFFER) {
3941 _mesa_error(ctx, GL_INVALID_OPERATION,
3942 "%s(buffer texture)", function);
3943 return false;
3944 }
3945
3946 if (_mesa_is_compressed_format(ctx, internalFormat)) {
3947 _mesa_error(ctx, GL_INVALID_OPERATION,
3948 "%s(compressed texture)", function);
3949 return false;
3950 }
3951
3952 err = _mesa_error_check_format_and_type(ctx, format, type);
3953 if (err != GL_NO_ERROR) {
3954 _mesa_error(ctx, err,
3955 "%s(incompatible format = %s, type = %s)",
3956 function,
3957 _mesa_enum_to_string(format),
3958 _mesa_enum_to_string(type));
3959 return false;
3960 }
3961
3962 /* make sure internal format and format basically agree */
3963 if (!texture_formats_agree(internalFormat, format)) {
3964 _mesa_error(ctx, GL_INVALID_OPERATION,
3965 "%s(incompatible internalFormat = %s, format = %s)",
3966 function,
3967 _mesa_enum_to_string(internalFormat),
3968 _mesa_enum_to_string(format));
3969 return false;
3970 }
3971
3972 if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
3973 /* both source and dest must be integer-valued, or neither */
3974 if (_mesa_is_format_integer_color(texImage->TexFormat) !=
3975 _mesa_is_enum_format_integer(format)) {
3976 _mesa_error(ctx, GL_INVALID_OPERATION,
3977 "%s(integer/non-integer format mismatch)",
3978 function);
3979 return false;
3980 }
3981 }
3982
3983 if (!_mesa_texstore(ctx,
3984 1, /* dims */
3985 texImage->_BaseFormat,
3986 texImage->TexFormat,
3987 0, /* dstRowStride */
3988 &clearValue,
3989 1, 1, 1, /* srcWidth/Height/Depth */
3990 format, type,
3991 data ? data : zeroData,
3992 &ctx->DefaultPacking)) {
3993 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid format)", function);
3994 return false;
3995 }
3996
3997 return true;
3998 }
3999
4000 static struct gl_texture_object *
4001 get_tex_obj_for_clear(struct gl_context *ctx,
4002 const char *function,
4003 GLuint texture)
4004 {
4005 struct gl_texture_object *texObj;
4006
4007 if (texture == 0) {
4008 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(zero texture)", function);
4009 return NULL;
4010 }
4011
4012 texObj = _mesa_lookup_texture(ctx, texture);
4013
4014 if (texObj == NULL) {
4015 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(non-gen name)", function);
4016 return NULL;
4017 }
4018
4019 if (texObj->Target == 0) {
4020 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unbound tex)", function);
4021 return NULL;
4022 }
4023
4024 return texObj;
4025 }
4026
4027 static int
4028 get_tex_images_for_clear(struct gl_context *ctx,
4029 const char *function,
4030 struct gl_texture_object *texObj,
4031 GLint level,
4032 struct gl_texture_image **texImages)
4033 {
4034 GLenum target;
4035 int i;
4036
4037 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
4038 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid level)", function);
4039 return 0;
4040 }
4041
4042 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
4043 for (i = 0; i < MAX_FACES; i++) {
4044 target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + i;
4045
4046 texImages[i] = _mesa_select_tex_image(texObj, target, level);
4047 if (texImages[i] == NULL) {
4048 _mesa_error(ctx, GL_INVALID_OPERATION,
4049 "%s(invalid level)", function);
4050 return 0;
4051 }
4052 }
4053
4054 return MAX_FACES;
4055 }
4056
4057 texImages[0] = _mesa_select_tex_image(texObj, texObj->Target, level);
4058
4059 if (texImages[0] == NULL) {
4060 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid level)", function);
4061 return 0;
4062 }
4063
4064 return 1;
4065 }
4066
4067 void GLAPIENTRY
4068 _mesa_ClearTexSubImage( GLuint texture, GLint level,
4069 GLint xoffset, GLint yoffset, GLint zoffset,
4070 GLsizei width, GLsizei height, GLsizei depth,
4071 GLenum format, GLenum type, const void *data )
4072 {
4073 GET_CURRENT_CONTEXT(ctx);
4074 struct gl_texture_object *texObj;
4075 struct gl_texture_image *texImages[MAX_FACES];
4076 GLubyte clearValue[MAX_FACES][MAX_PIXEL_BYTES];
4077 int i, numImages;
4078 int minDepth, maxDepth;
4079
4080 texObj = get_tex_obj_for_clear(ctx, "glClearTexSubImage", texture);
4081
4082 if (texObj == NULL)
4083 return;
4084
4085 _mesa_lock_texture(ctx, texObj);
4086
4087 numImages = get_tex_images_for_clear(ctx, "glClearTexSubImage",
4088 texObj, level, texImages);
4089 if (numImages == 0)
4090 goto out;
4091
4092 if (numImages == 1) {
4093 minDepth = -(int) texImages[0]->Border;
4094 maxDepth = texImages[0]->Depth;
4095 } else {
4096 minDepth = 0;
4097 maxDepth = numImages;
4098 }
4099
4100 if (xoffset < -(GLint) texImages[0]->Border ||
4101 yoffset < -(GLint) texImages[0]->Border ||
4102 zoffset < minDepth ||
4103 width < 0 ||
4104 height < 0 ||
4105 depth < 0 ||
4106 xoffset + width > texImages[0]->Width ||
4107 yoffset + height > texImages[0]->Height ||
4108 zoffset + depth > maxDepth) {
4109 _mesa_error(ctx, GL_INVALID_OPERATION,
4110 "glClearSubTexImage(invalid dimensions)");
4111 goto out;
4112 }
4113
4114 if (numImages == 1) {
4115 if (check_clear_tex_image(ctx, "glClearTexSubImage",
4116 texImages[0],
4117 format, type, data, clearValue[0])) {
4118 ctx->Driver.ClearTexSubImage(ctx,
4119 texImages[0],
4120 xoffset, yoffset, zoffset,
4121 width, height, depth,
4122 data ? clearValue[0] : NULL);
4123 }
4124 } else {
4125 for (i = zoffset; i < zoffset + depth; i++) {
4126 if (!check_clear_tex_image(ctx, "glClearTexSubImage",
4127 texImages[i],
4128 format, type, data, clearValue[i]))
4129 goto out;
4130 }
4131 for (i = zoffset; i < zoffset + depth; i++) {
4132 ctx->Driver.ClearTexSubImage(ctx,
4133 texImages[i],
4134 xoffset, yoffset, 0,
4135 width, height, 1,
4136 data ? clearValue[i] : NULL);
4137 }
4138 }
4139
4140 out:
4141 _mesa_unlock_texture(ctx, texObj);
4142 }
4143
4144 void GLAPIENTRY
4145 _mesa_ClearTexImage( GLuint texture, GLint level,
4146 GLenum format, GLenum type, const void *data )
4147 {
4148 GET_CURRENT_CONTEXT(ctx);
4149 struct gl_texture_object *texObj;
4150 struct gl_texture_image *texImages[MAX_FACES];
4151 GLubyte clearValue[MAX_FACES][MAX_PIXEL_BYTES];
4152 int i, numImages;
4153
4154 texObj = get_tex_obj_for_clear(ctx, "glClearTexImage", texture);
4155
4156 if (texObj == NULL)
4157 return;
4158
4159 _mesa_lock_texture(ctx, texObj);
4160
4161 numImages = get_tex_images_for_clear(ctx, "glClearTexImage",
4162 texObj, level, texImages);
4163
4164 for (i = 0; i < numImages; i++) {
4165 if (!check_clear_tex_image(ctx, "glClearTexImage",
4166 texImages[i],
4167 format, type, data,
4168 clearValue[i]))
4169 goto out;
4170 }
4171
4172 for (i = 0; i < numImages; i++) {
4173 ctx->Driver.ClearTexSubImage(ctx, texImages[i],
4174 -(GLint) texImages[i]->Border, /* xoffset */
4175 -(GLint) texImages[i]->Border, /* yoffset */
4176 -(GLint) texImages[i]->Border, /* zoffset */
4177 texImages[i]->Width,
4178 texImages[i]->Height,
4179 texImages[i]->Depth,
4180 data ? clearValue[i] : NULL);
4181 }
4182
4183 out:
4184 _mesa_unlock_texture(ctx, texObj);
4185 }
4186
4187
4188
4189
4190 /**********************************************************************/
4191 /****** Compressed Textures ******/
4192 /**********************************************************************/
4193
4194
4195 /**
4196 * Target checking for glCompressedTexSubImage[123]D().
4197 * \return GL_TRUE if error, GL_FALSE if no error
4198 * Must come before other error checking so that the texture object can
4199 * be correctly retrieved using _mesa_get_current_tex_object.
4200 */
4201 static GLboolean
4202 compressed_subtexture_target_check(struct gl_context *ctx, GLenum target,
4203 GLint dims, GLenum format, bool dsa,
4204 const char *caller)
4205 {
4206 GLboolean targetOK;
4207
4208 if (dsa && target == GL_TEXTURE_RECTANGLE) {
4209 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", caller,
4210 _mesa_enum_to_string(target));
4211 return GL_TRUE;
4212 }
4213
4214 switch (dims) {
4215 case 2:
4216 switch (target) {
4217 case GL_TEXTURE_2D:
4218 targetOK = GL_TRUE;
4219 break;
4220 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
4221 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
4222 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
4223 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
4224 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
4225 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
4226 targetOK = ctx->Extensions.ARB_texture_cube_map;
4227 break;
4228 default:
4229 targetOK = GL_FALSE;
4230 break;
4231 }
4232 break;
4233 case 3:
4234 switch (target) {
4235 case GL_TEXTURE_CUBE_MAP:
4236 targetOK = dsa && ctx->Extensions.ARB_texture_cube_map;
4237 break;
4238 case GL_TEXTURE_2D_ARRAY:
4239 targetOK = _mesa_is_gles3(ctx) ||
4240 (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array);
4241 break;
4242 case GL_TEXTURE_CUBE_MAP_ARRAY:
4243 targetOK = ctx->Extensions.ARB_texture_cube_map_array;
4244 break;
4245 case GL_TEXTURE_3D:
4246 targetOK = GL_TRUE;
4247 /*
4248 * OpenGL 4.5 spec (30.10.2014) says in Section 8.7 Compressed Texture
4249 * Images:
4250 * "An INVALID_OPERATION error is generated by
4251 * CompressedTex*SubImage3D if the internal format of the texture
4252 * is one of the EAC, ETC2, or RGTC formats and either border is
4253 * non-zero, or the effective target for the texture is not
4254 * TEXTURE_2D_ARRAY."
4255 *
4256 * NOTE: that's probably a spec error. It should probably say
4257 * "... or the effective target for the texture is not
4258 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP, nor
4259 * GL_TEXTURE_CUBE_MAP_ARRAY."
4260 * since those targets are 2D images and they support all compression
4261 * formats.
4262 *
4263 * Instead of listing all these, just list those which are allowed,
4264 * which is (at this time) only bptc. Otherwise we'd say s3tc (and
4265 * more) are valid here, which they are not, but of course not
4266 * mentioned by core spec.
4267 */
4268 switch (format) {
4269 /* These are the only 3D compression formats supported at this time */
4270 case GL_COMPRESSED_RGBA_BPTC_UNORM:
4271 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM:
4272 case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT:
4273 case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT:
4274 /* valid format */
4275 break;
4276 default:
4277 /* invalid format */
4278 _mesa_error(ctx, GL_INVALID_OPERATION,
4279 "%s(invalid target %s for format %s)", caller,
4280 _mesa_enum_to_string(target),
4281 _mesa_enum_to_string(format));
4282 return GL_TRUE;
4283 }
4284 break;
4285 default:
4286 targetOK = GL_FALSE;
4287 }
4288
4289 break;
4290 default:
4291 assert(dims == 1);
4292 /* no 1D compressed textures at this time */
4293 targetOK = GL_FALSE;
4294 break;
4295 }
4296
4297 if (!targetOK) {
4298 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", caller,
4299 _mesa_enum_to_string(target));
4300 return GL_TRUE;
4301 }
4302
4303 return GL_FALSE;
4304 }
4305
4306 /**
4307 * Error checking for glCompressedTexSubImage[123]D().
4308 * \return GL_TRUE if error, GL_FALSE if no error
4309 */
4310 static GLboolean
4311 compressed_subtexture_error_check(struct gl_context *ctx, GLint dims,
4312 const struct gl_texture_object *texObj,
4313 GLenum target, GLint level,
4314 GLint xoffset, GLint yoffset, GLint zoffset,
4315 GLsizei width, GLsizei height, GLsizei depth,
4316 GLenum format, GLsizei imageSize,
4317 const GLvoid *data, const char *callerName)
4318 {
4319 struct gl_texture_image *texImage;
4320 GLint expectedSize;
4321
4322 /* this will catch any invalid compressed format token */
4323 if (!_mesa_is_compressed_format(ctx, format)) {
4324 _mesa_error(ctx, GL_INVALID_ENUM,
4325 "%s(format)", callerName);
4326 return GL_TRUE;
4327 }
4328
4329 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
4330 _mesa_error(ctx, GL_INVALID_VALUE,
4331 "%s(level=%d)",
4332 callerName, level);
4333 return GL_TRUE;
4334 }
4335
4336 /* validate the bound PBO, if any */
4337 if (!_mesa_validate_pbo_source_compressed(ctx, dims, &ctx->Unpack,
4338 imageSize, data, callerName)) {
4339 return GL_TRUE;
4340 }
4341
4342 /* Check for invalid pixel storage modes */
4343 if (!_mesa_compressed_pixel_storage_error_check(ctx, dims,
4344 &ctx->Unpack, callerName)) {
4345 return GL_TRUE;
4346 }
4347
4348 expectedSize = compressed_tex_size(width, height, depth, format);
4349 if (expectedSize != imageSize) {
4350 _mesa_error(ctx, GL_INVALID_VALUE,
4351 "%s(size=%d)",
4352 callerName, imageSize);
4353 return GL_TRUE;
4354 }
4355
4356 texImage = _mesa_select_tex_image(texObj, target, level);
4357 if (!texImage) {
4358 _mesa_error(ctx, GL_INVALID_OPERATION,
4359 "%s(invalid texture image)",
4360 callerName);
4361 return GL_TRUE;
4362 }
4363
4364 if ((GLint) format != texImage->InternalFormat) {
4365 _mesa_error(ctx, GL_INVALID_OPERATION,
4366 "%s(format=%s)",
4367 callerName, _mesa_enum_to_string(format));
4368 return GL_TRUE;
4369 }
4370
4371 if (compressedteximage_only_format(ctx, format)) {
4372 _mesa_error(ctx, GL_INVALID_OPERATION,
4373 "%s(format=%s cannot be updated)",
4374 callerName, _mesa_enum_to_string(format));
4375 return GL_TRUE;
4376 }
4377
4378 if (error_check_subtexture_dimensions(ctx, dims,
4379 texImage, xoffset, yoffset, zoffset,
4380 width, height, depth,
4381 callerName)) {
4382 return GL_TRUE;
4383 }
4384
4385 return GL_FALSE;
4386 }
4387
4388
4389 void GLAPIENTRY
4390 _mesa_CompressedTexImage1D(GLenum target, GLint level,
4391 GLenum internalFormat, GLsizei width,
4392 GLint border, GLsizei imageSize,
4393 const GLvoid *data)
4394 {
4395 GET_CURRENT_CONTEXT(ctx);
4396 teximage(ctx, GL_TRUE, 1, target, level, internalFormat,
4397 width, 1, 1, border, GL_NONE, GL_NONE, imageSize, data);
4398 }
4399
4400
4401 void GLAPIENTRY
4402 _mesa_CompressedTexImage2D(GLenum target, GLint level,
4403 GLenum internalFormat, GLsizei width,
4404 GLsizei height, GLint border, GLsizei imageSize,
4405 const GLvoid *data)
4406 {
4407 GET_CURRENT_CONTEXT(ctx);
4408 teximage(ctx, GL_TRUE, 2, target, level, internalFormat,
4409 width, height, 1, border, GL_NONE, GL_NONE, imageSize, data);
4410 }
4411
4412
4413 void GLAPIENTRY
4414 _mesa_CompressedTexImage3D(GLenum target, GLint level,
4415 GLenum internalFormat, GLsizei width,
4416 GLsizei height, GLsizei depth, GLint border,
4417 GLsizei imageSize, const GLvoid *data)
4418 {
4419 GET_CURRENT_CONTEXT(ctx);
4420 teximage(ctx, GL_TRUE, 3, target, level, internalFormat,
4421 width, height, depth, border, GL_NONE, GL_NONE, imageSize, data);
4422 }
4423
4424
4425 /**
4426 * Common helper for glCompressedTexSubImage1/2/3D() and
4427 * glCompressedTextureSubImage1/2/3D().
4428 */
4429 void
4430 _mesa_compressed_texture_sub_image(struct gl_context *ctx, GLuint dims,
4431 struct gl_texture_object *texObj,
4432 struct gl_texture_image *texImage,
4433 GLenum target, GLint level,
4434 GLint xoffset, GLint yoffset,
4435 GLint zoffset,
4436 GLsizei width, GLsizei height,
4437 GLsizei depth,
4438 GLenum format, GLsizei imageSize,
4439 const GLvoid *data)
4440 {
4441 FLUSH_VERTICES(ctx, 0);
4442
4443 _mesa_lock_texture(ctx, texObj);
4444 {
4445 if (width > 0 && height > 0 && depth > 0) {
4446 ctx->Driver.CompressedTexSubImage(ctx, dims, texImage,
4447 xoffset, yoffset, zoffset,
4448 width, height, depth,
4449 format, imageSize, data);
4450
4451 check_gen_mipmap(ctx, target, texObj, level);
4452
4453 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
4454 * the texel data, not the texture format, size, etc.
4455 */
4456 }
4457 }
4458 _mesa_unlock_texture(ctx, texObj);
4459 }
4460
4461
4462 void GLAPIENTRY
4463 _mesa_CompressedTexSubImage1D(GLenum target, GLint level, GLint xoffset,
4464 GLsizei width, GLenum format,
4465 GLsizei imageSize, const GLvoid *data)
4466 {
4467 struct gl_texture_object *texObj;
4468 struct gl_texture_image *texImage;
4469
4470 GET_CURRENT_CONTEXT(ctx);
4471
4472 if (compressed_subtexture_target_check(ctx, target, 1, format, false,
4473 "glCompressedTexSubImage1D")) {
4474 return;
4475 }
4476
4477 texObj = _mesa_get_current_tex_object(ctx, target);
4478 if (!texObj)
4479 return;
4480
4481 if (compressed_subtexture_error_check(ctx, 1, texObj, target,
4482 level, xoffset, 0, 0,
4483 width, 1, 1,
4484 format, imageSize, data,
4485 "glCompressedTexSubImage1D")) {
4486 return;
4487 }
4488
4489 texImage = _mesa_select_tex_image(texObj, target, level);
4490 assert(texImage);
4491
4492 _mesa_compressed_texture_sub_image(ctx, 1, texObj, texImage, target, level,
4493 xoffset, 0, 0, width, 1, 1,
4494 format, imageSize, data);
4495 }
4496
4497 void GLAPIENTRY
4498 _mesa_CompressedTextureSubImage1D(GLuint texture, GLint level, GLint xoffset,
4499 GLsizei width, GLenum format,
4500 GLsizei imageSize, const GLvoid *data)
4501 {
4502 struct gl_texture_object *texObj;
4503 struct gl_texture_image *texImage;
4504
4505 GET_CURRENT_CONTEXT(ctx);
4506
4507 texObj = _mesa_lookup_texture_err(ctx, texture,
4508 "glCompressedTextureSubImage1D");
4509 if (!texObj)
4510 return;
4511
4512 if (compressed_subtexture_target_check(ctx, texObj->Target, 1, format, true,
4513 "glCompressedTextureSubImage1D")) {
4514 return;
4515 }
4516
4517 if (compressed_subtexture_error_check(ctx, 1, texObj, texObj->Target,
4518 level, xoffset, 0, 0,
4519 width, 1, 1,
4520 format, imageSize, data,
4521 "glCompressedTextureSubImage1D")) {
4522 return;
4523 }
4524
4525 texImage = _mesa_select_tex_image(texObj, texObj->Target, level);
4526 assert(texImage);
4527
4528 _mesa_compressed_texture_sub_image(ctx, 1, texObj, texImage,
4529 texObj->Target, level,
4530 xoffset, 0, 0, width, 1, 1,
4531 format, imageSize, data);
4532 }
4533
4534
4535 void GLAPIENTRY
4536 _mesa_CompressedTexSubImage2D(GLenum target, GLint level, GLint xoffset,
4537 GLint yoffset, GLsizei width, GLsizei height,
4538 GLenum format, GLsizei imageSize,
4539 const GLvoid *data)
4540 {
4541 struct gl_texture_object *texObj;
4542 struct gl_texture_image *texImage;
4543
4544 GET_CURRENT_CONTEXT(ctx);
4545
4546 if (compressed_subtexture_target_check(ctx, target, 2, format, false,
4547 "glCompressedTexSubImage2D")) {
4548 return;
4549 }
4550
4551 texObj = _mesa_get_current_tex_object(ctx, target);
4552 if (!texObj)
4553 return;
4554
4555 if (compressed_subtexture_error_check(ctx, 2, texObj, target,
4556 level, xoffset, yoffset, 0,
4557 width, height, 1,
4558 format, imageSize, data,
4559 "glCompressedTexSubImage2D")) {
4560 return;
4561 }
4562
4563
4564 texImage = _mesa_select_tex_image(texObj, target, level);
4565 assert(texImage);
4566
4567 _mesa_compressed_texture_sub_image(ctx, 2, texObj, texImage, target, level,
4568 xoffset, yoffset, 0, width, height, 1,
4569 format, imageSize, data);
4570 }
4571
4572 void GLAPIENTRY
4573 _mesa_CompressedTextureSubImage2D(GLuint texture, GLint level, GLint xoffset,
4574 GLint yoffset,
4575 GLsizei width, GLsizei height,
4576 GLenum format, GLsizei imageSize,
4577 const GLvoid *data)
4578 {
4579 struct gl_texture_object *texObj;
4580 struct gl_texture_image *texImage;
4581
4582 GET_CURRENT_CONTEXT(ctx);
4583
4584 texObj = _mesa_lookup_texture_err(ctx, texture,
4585 "glCompressedTextureSubImage2D");
4586 if (!texObj)
4587 return;
4588
4589 if (compressed_subtexture_target_check(ctx, texObj->Target, 2, format, true,
4590 "glCompressedTextureSubImage2D")) {
4591 return;
4592 }
4593
4594 if (compressed_subtexture_error_check(ctx, 2, texObj, texObj->Target,
4595 level, xoffset, yoffset, 0,
4596 width, height, 1,
4597 format, imageSize, data,
4598 "glCompressedTextureSubImage2D")) {
4599 return;
4600 }
4601
4602 texImage = _mesa_select_tex_image(texObj, texObj->Target, level);
4603 assert(texImage);
4604
4605 _mesa_compressed_texture_sub_image(ctx, 2, texObj, texImage,
4606 texObj->Target, level,
4607 xoffset, yoffset, 0, width, height, 1,
4608 format, imageSize, data);
4609 }
4610
4611 void GLAPIENTRY
4612 _mesa_CompressedTexSubImage3D(GLenum target, GLint level, GLint xoffset,
4613 GLint yoffset, GLint zoffset, GLsizei width,
4614 GLsizei height, GLsizei depth, GLenum format,
4615 GLsizei imageSize, const GLvoid *data)
4616 {
4617 struct gl_texture_object *texObj;
4618 struct gl_texture_image *texImage;
4619
4620 GET_CURRENT_CONTEXT(ctx);
4621
4622 if (compressed_subtexture_target_check(ctx, target, 3, format, false,
4623 "glCompressedTexSubImage3D")) {
4624 return;
4625 }
4626
4627 texObj = _mesa_get_current_tex_object(ctx, target);
4628 if (!texObj)
4629 return;
4630
4631 if (compressed_subtexture_error_check(ctx, 3, texObj, target,
4632 level, xoffset, yoffset, zoffset,
4633 width, height, depth,
4634 format, imageSize, data,
4635 "glCompressedTexSubImage3D")) {
4636 return;
4637 }
4638
4639
4640 texImage = _mesa_select_tex_image(texObj, target, level);
4641 assert(texImage);
4642
4643 _mesa_compressed_texture_sub_image(ctx, 3, texObj, texImage, target, level,
4644 xoffset, yoffset, zoffset,
4645 width, height, depth,
4646 format, imageSize, data);
4647 }
4648
4649 void GLAPIENTRY
4650 _mesa_CompressedTextureSubImage3D(GLuint texture, GLint level, GLint xoffset,
4651 GLint yoffset, GLint zoffset, GLsizei width,
4652 GLsizei height, GLsizei depth,
4653 GLenum format, GLsizei imageSize,
4654 const GLvoid *data)
4655 {
4656 struct gl_texture_object *texObj;
4657 struct gl_texture_image *texImage;
4658
4659 GET_CURRENT_CONTEXT(ctx);
4660
4661 texObj = _mesa_lookup_texture_err(ctx, texture,
4662 "glCompressedTextureSubImage3D");
4663 if (!texObj)
4664 return;
4665
4666 if (compressed_subtexture_target_check(ctx, texObj->Target, 3, format, true,
4667 "glCompressedTextureSubImage3D")) {
4668 return;
4669 }
4670
4671 if (compressed_subtexture_error_check(ctx, 3, texObj, texObj->Target,
4672 level, xoffset, yoffset, zoffset,
4673 width, height, depth,
4674 format, imageSize, data,
4675 "glCompressedTextureSubImage3D")) {
4676 return;
4677 }
4678
4679 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
4680 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
4681 const char *pixels = data;
4682 int i;
4683 GLint image_stride;
4684
4685 /* Make sure the texture object is a proper cube.
4686 * (See texturesubimage in teximage.c for details on why this check is
4687 * performed.)
4688 */
4689 if (!_mesa_cube_level_complete(texObj, level)) {
4690 _mesa_error(ctx, GL_INVALID_OPERATION,
4691 "glCompressedTextureSubImage3D(cube map incomplete)");
4692 return;
4693 }
4694
4695 /* Copy in each face. */
4696 for (i = 0; i < 6; ++i) {
4697 texImage = texObj->Image[i][level];
4698 assert(texImage);
4699
4700 _mesa_compressed_texture_sub_image(ctx, 3, texObj, texImage,
4701 texObj->Target, level,
4702 xoffset, yoffset, zoffset,
4703 width, height, 1,
4704 format, imageSize, pixels);
4705
4706 /* Compressed images don't have a client format */
4707 image_stride = _mesa_format_image_size(texImage->TexFormat,
4708 texImage->Width,
4709 texImage->Height, 1);
4710
4711 pixels += image_stride;
4712 imageSize -= image_stride;
4713 }
4714 }
4715 else {
4716 texImage = _mesa_select_tex_image(texObj, texObj->Target, level);
4717 assert(texImage);
4718
4719 _mesa_compressed_texture_sub_image(ctx, 3, texObj, texImage,
4720 texObj->Target, level,
4721 xoffset, yoffset, zoffset,
4722 width, height, depth,
4723 format, imageSize, data);
4724 }
4725 }
4726
4727 static mesa_format
4728 get_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
4729 {
4730 if (ctx->API == API_OPENGL_COMPAT) {
4731 switch (internalFormat) {
4732 case GL_ALPHA8:
4733 return MESA_FORMAT_A_UNORM8;
4734 case GL_ALPHA16:
4735 return MESA_FORMAT_A_UNORM16;
4736 case GL_ALPHA16F_ARB:
4737 return MESA_FORMAT_A_FLOAT16;
4738 case GL_ALPHA32F_ARB:
4739 return MESA_FORMAT_A_FLOAT32;
4740 case GL_ALPHA8I_EXT:
4741 return MESA_FORMAT_A_SINT8;
4742 case GL_ALPHA16I_EXT:
4743 return MESA_FORMAT_A_SINT16;
4744 case GL_ALPHA32I_EXT:
4745 return MESA_FORMAT_A_SINT32;
4746 case GL_ALPHA8UI_EXT:
4747 return MESA_FORMAT_A_UINT8;
4748 case GL_ALPHA16UI_EXT:
4749 return MESA_FORMAT_A_UINT16;
4750 case GL_ALPHA32UI_EXT:
4751 return MESA_FORMAT_A_UINT32;
4752 case GL_LUMINANCE8:
4753 return MESA_FORMAT_L_UNORM8;
4754 case GL_LUMINANCE16:
4755 return MESA_FORMAT_L_UNORM16;
4756 case GL_LUMINANCE16F_ARB:
4757 return MESA_FORMAT_L_FLOAT16;
4758 case GL_LUMINANCE32F_ARB:
4759 return MESA_FORMAT_L_FLOAT32;
4760 case GL_LUMINANCE8I_EXT:
4761 return MESA_FORMAT_L_SINT8;
4762 case GL_LUMINANCE16I_EXT:
4763 return MESA_FORMAT_L_SINT16;
4764 case GL_LUMINANCE32I_EXT:
4765 return MESA_FORMAT_L_SINT32;
4766 case GL_LUMINANCE8UI_EXT:
4767 return MESA_FORMAT_L_UINT8;
4768 case GL_LUMINANCE16UI_EXT:
4769 return MESA_FORMAT_L_UINT16;
4770 case GL_LUMINANCE32UI_EXT:
4771 return MESA_FORMAT_L_UINT32;
4772 case GL_LUMINANCE8_ALPHA8:
4773 return MESA_FORMAT_L8A8_UNORM;
4774 case GL_LUMINANCE16_ALPHA16:
4775 return MESA_FORMAT_L16A16_UNORM;
4776 case GL_LUMINANCE_ALPHA16F_ARB:
4777 return MESA_FORMAT_LA_FLOAT16;
4778 case GL_LUMINANCE_ALPHA32F_ARB:
4779 return MESA_FORMAT_LA_FLOAT32;
4780 case GL_LUMINANCE_ALPHA8I_EXT:
4781 return MESA_FORMAT_LA_SINT8;
4782 case GL_LUMINANCE_ALPHA16I_EXT:
4783 return MESA_FORMAT_LA_SINT16;
4784 case GL_LUMINANCE_ALPHA32I_EXT:
4785 return MESA_FORMAT_LA_SINT32;
4786 case GL_LUMINANCE_ALPHA8UI_EXT:
4787 return MESA_FORMAT_LA_UINT8;
4788 case GL_LUMINANCE_ALPHA16UI_EXT:
4789 return MESA_FORMAT_LA_UINT16;
4790 case GL_LUMINANCE_ALPHA32UI_EXT:
4791 return MESA_FORMAT_LA_UINT32;
4792 case GL_INTENSITY8:
4793 return MESA_FORMAT_I_UNORM8;
4794 case GL_INTENSITY16:
4795 return MESA_FORMAT_I_UNORM16;
4796 case GL_INTENSITY16F_ARB:
4797 return MESA_FORMAT_I_FLOAT16;
4798 case GL_INTENSITY32F_ARB:
4799 return MESA_FORMAT_I_FLOAT32;
4800 case GL_INTENSITY8I_EXT:
4801 return MESA_FORMAT_I_SINT8;
4802 case GL_INTENSITY16I_EXT:
4803 return MESA_FORMAT_I_SINT16;
4804 case GL_INTENSITY32I_EXT:
4805 return MESA_FORMAT_I_SINT32;
4806 case GL_INTENSITY8UI_EXT:
4807 return MESA_FORMAT_I_UINT8;
4808 case GL_INTENSITY16UI_EXT:
4809 return MESA_FORMAT_I_UINT16;
4810 case GL_INTENSITY32UI_EXT:
4811 return MESA_FORMAT_I_UINT32;
4812 default:
4813 break;
4814 }
4815 }
4816
4817 if (_mesa_has_ARB_texture_buffer_object_rgb32(ctx) ||
4818 _mesa_has_OES_texture_buffer(ctx)) {
4819 switch (internalFormat) {
4820 case GL_RGB32F:
4821 return MESA_FORMAT_RGB_FLOAT32;
4822 case GL_RGB32UI:
4823 return MESA_FORMAT_RGB_UINT32;
4824 case GL_RGB32I:
4825 return MESA_FORMAT_RGB_SINT32;
4826 default:
4827 break;
4828 }
4829 }
4830
4831 switch (internalFormat) {
4832 case GL_RGBA8:
4833 return MESA_FORMAT_R8G8B8A8_UNORM;
4834 case GL_RGBA16:
4835 if (_mesa_is_gles(ctx))
4836 return MESA_FORMAT_NONE;
4837 return MESA_FORMAT_RGBA_UNORM16;
4838 case GL_RGBA16F_ARB:
4839 return MESA_FORMAT_RGBA_FLOAT16;
4840 case GL_RGBA32F_ARB:
4841 return MESA_FORMAT_RGBA_FLOAT32;
4842 case GL_RGBA8I_EXT:
4843 return MESA_FORMAT_RGBA_SINT8;
4844 case GL_RGBA16I_EXT:
4845 return MESA_FORMAT_RGBA_SINT16;
4846 case GL_RGBA32I_EXT:
4847 return MESA_FORMAT_RGBA_SINT32;
4848 case GL_RGBA8UI_EXT:
4849 return MESA_FORMAT_RGBA_UINT8;
4850 case GL_RGBA16UI_EXT:
4851 return MESA_FORMAT_RGBA_UINT16;
4852 case GL_RGBA32UI_EXT:
4853 return MESA_FORMAT_RGBA_UINT32;
4854
4855 case GL_RG8:
4856 return MESA_FORMAT_R8G8_UNORM;
4857 case GL_RG16:
4858 if (_mesa_is_gles(ctx))
4859 return MESA_FORMAT_NONE;
4860 return MESA_FORMAT_R16G16_UNORM;
4861 case GL_RG16F:
4862 return MESA_FORMAT_RG_FLOAT16;
4863 case GL_RG32F:
4864 return MESA_FORMAT_RG_FLOAT32;
4865 case GL_RG8I:
4866 return MESA_FORMAT_RG_SINT8;
4867 case GL_RG16I:
4868 return MESA_FORMAT_RG_SINT16;
4869 case GL_RG32I:
4870 return MESA_FORMAT_RG_SINT32;
4871 case GL_RG8UI:
4872 return MESA_FORMAT_RG_UINT8;
4873 case GL_RG16UI:
4874 return MESA_FORMAT_RG_UINT16;
4875 case GL_RG32UI:
4876 return MESA_FORMAT_RG_UINT32;
4877
4878 case GL_R8:
4879 return MESA_FORMAT_R_UNORM8;
4880 case GL_R16:
4881 if (_mesa_is_gles(ctx))
4882 return MESA_FORMAT_NONE;
4883 return MESA_FORMAT_R_UNORM16;
4884 case GL_R16F:
4885 return MESA_FORMAT_R_FLOAT16;
4886 case GL_R32F:
4887 return MESA_FORMAT_R_FLOAT32;
4888 case GL_R8I:
4889 return MESA_FORMAT_R_SINT8;
4890 case GL_R16I:
4891 return MESA_FORMAT_R_SINT16;
4892 case GL_R32I:
4893 return MESA_FORMAT_R_SINT32;
4894 case GL_R8UI:
4895 return MESA_FORMAT_R_UINT8;
4896 case GL_R16UI:
4897 return MESA_FORMAT_R_UINT16;
4898 case GL_R32UI:
4899 return MESA_FORMAT_R_UINT32;
4900
4901 default:
4902 return MESA_FORMAT_NONE;
4903 }
4904 }
4905
4906
4907 mesa_format
4908 _mesa_validate_texbuffer_format(const struct gl_context *ctx,
4909 GLenum internalFormat)
4910 {
4911 mesa_format format = get_texbuffer_format(ctx, internalFormat);
4912 GLenum datatype;
4913
4914 if (format == MESA_FORMAT_NONE)
4915 return MESA_FORMAT_NONE;
4916
4917 datatype = _mesa_get_format_datatype(format);
4918
4919 /* The GL_ARB_texture_buffer_object spec says:
4920 *
4921 * "If ARB_texture_float is not supported, references to the
4922 * floating-point internal formats provided by that extension should be
4923 * removed, and such formats may not be passed to TexBufferARB."
4924 *
4925 * As a result, GL_HALF_FLOAT internal format depends on both
4926 * GL_ARB_texture_float and GL_ARB_half_float_pixel.
4927 */
4928 if ((datatype == GL_FLOAT || datatype == GL_HALF_FLOAT) &&
4929 !ctx->Extensions.ARB_texture_float)
4930 return MESA_FORMAT_NONE;
4931
4932 if (!ctx->Extensions.ARB_texture_rg) {
4933 GLenum base_format = _mesa_get_format_base_format(format);
4934 if (base_format == GL_R || base_format == GL_RG)
4935 return MESA_FORMAT_NONE;
4936 }
4937
4938 if (!ctx->Extensions.ARB_texture_buffer_object_rgb32) {
4939 GLenum base_format = _mesa_get_format_base_format(format);
4940 if (base_format == GL_RGB)
4941 return MESA_FORMAT_NONE;
4942 }
4943 return format;
4944 }
4945
4946
4947 void
4948 _mesa_texture_buffer_range(struct gl_context *ctx,
4949 struct gl_texture_object *texObj,
4950 GLenum internalFormat,
4951 struct gl_buffer_object *bufObj,
4952 GLintptr offset, GLsizeiptr size,
4953 const char *caller)
4954 {
4955 mesa_format format;
4956
4957 /* NOTE: ARB_texture_buffer_object has interactions with
4958 * the compatibility profile that are not implemented.
4959 */
4960 if (!_mesa_has_ARB_texture_buffer_object(ctx) &&
4961 !_mesa_has_OES_texture_buffer(ctx)) {
4962 _mesa_error(ctx, GL_INVALID_OPERATION,
4963 "%s(ARB_texture_buffer_object is not"
4964 " implemented for the compatibility profile)", caller);
4965 return;
4966 }
4967
4968 format = _mesa_validate_texbuffer_format(ctx, internalFormat);
4969 if (format == MESA_FORMAT_NONE) {
4970 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat %s)",
4971 caller, _mesa_enum_to_string(internalFormat));
4972 return;
4973 }
4974
4975 FLUSH_VERTICES(ctx, 0);
4976
4977 _mesa_lock_texture(ctx, texObj);
4978 {
4979 _mesa_reference_buffer_object(ctx, &texObj->BufferObject, bufObj);
4980 texObj->BufferObjectFormat = internalFormat;
4981 texObj->_BufferObjectFormat = format;
4982 texObj->BufferOffset = offset;
4983 texObj->BufferSize = size;
4984 }
4985 _mesa_unlock_texture(ctx, texObj);
4986
4987 ctx->NewDriverState |= ctx->DriverFlags.NewTextureBuffer;
4988
4989 if (bufObj) {
4990 bufObj->UsageHistory |= USAGE_TEXTURE_BUFFER;
4991 }
4992 }
4993
4994
4995 /**
4996 * Make sure the texture buffer target is GL_TEXTURE_BUFFER.
4997 * Return true if it is, and return false if it is not
4998 * (and throw INVALID ENUM as dictated in the OpenGL 4.5
4999 * core spec, 02.02.2015, PDF page 245).
5000 */
5001 static bool
5002 check_texture_buffer_target(struct gl_context *ctx, GLenum target,
5003 const char *caller)
5004 {
5005 if (target != GL_TEXTURE_BUFFER_ARB) {
5006 _mesa_error(ctx, GL_INVALID_ENUM,
5007 "%s(texture target is not GL_TEXTURE_BUFFER)", caller);
5008 return false;
5009 }
5010 else
5011 return true;
5012 }
5013
5014 /**
5015 * Check for errors related to the texture buffer range.
5016 * Return false if errors are found, true if none are found.
5017 */
5018 static bool
5019 check_texture_buffer_range(struct gl_context *ctx,
5020 struct gl_buffer_object *bufObj,
5021 GLintptr offset, GLsizeiptr size,
5022 const char *caller)
5023 {
5024 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5025 * Textures (PDF page 245):
5026 * "An INVALID_VALUE error is generated if offset is negative, if
5027 * size is less than or equal to zero, or if offset + size is greater
5028 * than the value of BUFFER_SIZE for the buffer bound to target."
5029 */
5030 if (offset < 0) {
5031 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset=%d < 0)", caller,
5032 (int) offset);
5033 return false;
5034 }
5035
5036 if (size <= 0) {
5037 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d <= 0)", caller,
5038 (int) size);
5039 return false;
5040 }
5041
5042 if (offset + size > bufObj->Size) {
5043 _mesa_error(ctx, GL_INVALID_VALUE,
5044 "%s(offset=%d + size=%d > buffer_size=%d)", caller,
5045 (int) offset, (int) size, (int) bufObj->Size);
5046 return false;
5047 }
5048
5049 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5050 * Textures (PDF page 245):
5051 * "An INVALID_VALUE error is generated if offset is not an integer
5052 * multiple of the value of TEXTURE_BUFFER_OFFSET_ALIGNMENT."
5053 */
5054 if (offset % ctx->Const.TextureBufferOffsetAlignment) {
5055 _mesa_error(ctx, GL_INVALID_VALUE,
5056 "%s(invalid offset alignment)", caller);
5057 return false;
5058 }
5059
5060 return true;
5061 }
5062
5063
5064 /** GL_ARB_texture_buffer_object */
5065 void GLAPIENTRY
5066 _mesa_TexBuffer(GLenum target, GLenum internalFormat, GLuint buffer)
5067 {
5068 struct gl_texture_object *texObj;
5069 struct gl_buffer_object *bufObj;
5070
5071 GET_CURRENT_CONTEXT(ctx);
5072
5073 /* Need to catch a bad target before it gets to
5074 * _mesa_get_current_tex_object.
5075 */
5076 if (!check_texture_buffer_target(ctx, target, "glTexBuffer"))
5077 return;
5078
5079 if (buffer) {
5080 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTexBuffer");
5081 if (!bufObj)
5082 return;
5083 } else
5084 bufObj = NULL;
5085
5086 texObj = _mesa_get_current_tex_object(ctx, target);
5087 if (!texObj)
5088 return;
5089
5090 _mesa_texture_buffer_range(ctx, texObj, internalFormat, bufObj, 0,
5091 buffer ? -1 : 0, "glTexBuffer");
5092 }
5093
5094
5095 /** GL_ARB_texture_buffer_range */
5096 void GLAPIENTRY
5097 _mesa_TexBufferRange(GLenum target, GLenum internalFormat, GLuint buffer,
5098 GLintptr offset, GLsizeiptr size)
5099 {
5100 struct gl_texture_object *texObj;
5101 struct gl_buffer_object *bufObj;
5102
5103 GET_CURRENT_CONTEXT(ctx);
5104
5105 /* Need to catch a bad target before it gets to
5106 * _mesa_get_current_tex_object.
5107 */
5108 if (!check_texture_buffer_target(ctx, target, "glTexBufferRange"))
5109 return;
5110
5111 if (buffer) {
5112 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTexBufferRange");
5113 if (!bufObj)
5114 return;
5115
5116 if (!check_texture_buffer_range(ctx, bufObj, offset, size,
5117 "glTexBufferRange"))
5118 return;
5119
5120 } else {
5121 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5122 * Textures (PDF page 254):
5123 * "If buffer is zero, then any buffer object attached to the buffer
5124 * texture is detached, the values offset and size are ignored and
5125 * the state for offset and size for the buffer texture are reset to
5126 * zero."
5127 */
5128 offset = 0;
5129 size = 0;
5130 bufObj = NULL;
5131 }
5132
5133 texObj = _mesa_get_current_tex_object(ctx, target);
5134 if (!texObj)
5135 return;
5136
5137 _mesa_texture_buffer_range(ctx, texObj, internalFormat, bufObj,
5138 offset, size, "glTexBufferRange");
5139 }
5140
5141 void GLAPIENTRY
5142 _mesa_TextureBuffer(GLuint texture, GLenum internalFormat, GLuint buffer)
5143 {
5144 struct gl_texture_object *texObj;
5145 struct gl_buffer_object *bufObj;
5146
5147 GET_CURRENT_CONTEXT(ctx);
5148
5149 if (buffer) {
5150 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTextureBuffer");
5151 if (!bufObj)
5152 return;
5153 } else
5154 bufObj = NULL;
5155
5156 /* Get the texture object by Name. */
5157 texObj = _mesa_lookup_texture_err(ctx, texture, "glTextureBuffer");
5158 if (!texObj)
5159 return;
5160
5161 if (!check_texture_buffer_target(ctx, texObj->Target, "glTextureBuffer"))
5162 return;
5163
5164 _mesa_texture_buffer_range(ctx, texObj, internalFormat,
5165 bufObj, 0, buffer ? -1 : 0, "glTextureBuffer");
5166 }
5167
5168 void GLAPIENTRY
5169 _mesa_TextureBufferRange(GLuint texture, GLenum internalFormat, GLuint buffer,
5170 GLintptr offset, GLsizeiptr size)
5171 {
5172 struct gl_texture_object *texObj;
5173 struct gl_buffer_object *bufObj;
5174
5175 GET_CURRENT_CONTEXT(ctx);
5176
5177 if (buffer) {
5178 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
5179 "glTextureBufferRange");
5180 if (!bufObj)
5181 return;
5182
5183 if (!check_texture_buffer_range(ctx, bufObj, offset, size,
5184 "glTextureBufferRange"))
5185 return;
5186
5187 } else {
5188 /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
5189 * Textures (PDF page 254):
5190 * "If buffer is zero, then any buffer object attached to the buffer
5191 * texture is detached, the values offset and size are ignored and
5192 * the state for offset and size for the buffer texture are reset to
5193 * zero."
5194 */
5195 offset = 0;
5196 size = 0;
5197 bufObj = NULL;
5198 }
5199
5200 /* Get the texture object by Name. */
5201 texObj = _mesa_lookup_texture_err(ctx, texture, "glTextureBufferRange");
5202 if (!texObj)
5203 return;
5204
5205 if (!check_texture_buffer_target(ctx, texObj->Target,
5206 "glTextureBufferRange"))
5207 return;
5208
5209 _mesa_texture_buffer_range(ctx, texObj, internalFormat,
5210 bufObj, offset, size, "glTextureBufferRange");
5211 }
5212
5213 GLboolean
5214 _mesa_is_renderable_texture_format(struct gl_context *ctx, GLenum internalformat)
5215 {
5216 /* Everything that is allowed for renderbuffers,
5217 * except for a base format of GL_STENCIL_INDEX, unless supported.
5218 */
5219 GLenum baseFormat = _mesa_base_fbo_format(ctx, internalformat);
5220 if (ctx->Extensions.ARB_texture_stencil8)
5221 return baseFormat != 0;
5222 else
5223 return baseFormat != 0 && baseFormat != GL_STENCIL_INDEX;
5224 }
5225
5226
5227 /** GL_ARB_texture_multisample */
5228 static GLboolean
5229 check_multisample_target(GLuint dims, GLenum target, bool dsa)
5230 {
5231 switch(target) {
5232 case GL_TEXTURE_2D_MULTISAMPLE:
5233 return dims == 2;
5234 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
5235 return dims == 2 && !dsa;
5236 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
5237 return dims == 3;
5238 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
5239 return dims == 3 && !dsa;
5240 default:
5241 return GL_FALSE;
5242 }
5243 }
5244
5245
5246 static void
5247 texture_image_multisample(struct gl_context *ctx, GLuint dims,
5248 struct gl_texture_object *texObj,
5249 GLenum target, GLsizei samples,
5250 GLint internalformat, GLsizei width,
5251 GLsizei height, GLsizei depth,
5252 GLboolean fixedsamplelocations,
5253 GLboolean immutable, const char *func)
5254 {
5255 struct gl_texture_image *texImage;
5256 GLboolean sizeOK, dimensionsOK, samplesOK;
5257 mesa_format texFormat;
5258 GLenum sample_count_error;
5259 bool dsa = strstr(func, "ture") ? true : false;
5260
5261 if (!((ctx->Extensions.ARB_texture_multisample
5262 && _mesa_is_desktop_gl(ctx))) && !_mesa_is_gles31(ctx)) {
5263 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unsupported)", func);
5264 return;
5265 }
5266
5267 if (samples < 1) {
5268 _mesa_error(ctx, GL_INVALID_VALUE, "%s(samples < 1)", func);
5269 return;
5270 }
5271
5272 if (!check_multisample_target(dims, target, dsa)) {
5273 if (dsa) {
5274 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(target)", func);
5275 return;
5276 }
5277 else {
5278 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
5279 return;
5280 }
5281 }
5282
5283 /* check that the specified internalformat is color/depth/stencil-renderable;
5284 * refer GL3.1 spec 4.4.4
5285 */
5286
5287 if (immutable && !_mesa_is_legal_tex_storage_format(ctx, internalformat)) {
5288 _mesa_error(ctx, GL_INVALID_ENUM,
5289 "%s(internalformat=%s not legal for immutable-format)",
5290 func, _mesa_enum_to_string(internalformat));
5291 return;
5292 }
5293
5294 if (!_mesa_is_renderable_texture_format(ctx, internalformat)) {
5295 /* Page 172 of OpenGL ES 3.1 spec says:
5296 * "An INVALID_ENUM error is generated if sizedinternalformat is not
5297 * color-renderable, depth-renderable, or stencil-renderable (as
5298 * defined in section 9.4).
5299 *
5300 * (Same error is also defined for desktop OpenGL for multisample
5301 * teximage/texstorage functions.)
5302 */
5303 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalformat=%s)", func,
5304 _mesa_enum_to_string(internalformat));
5305 return;
5306 }
5307
5308 sample_count_error = _mesa_check_sample_count(ctx, target,
5309 internalformat, samples);
5310 samplesOK = sample_count_error == GL_NO_ERROR;
5311
5312 /* Page 254 of OpenGL 4.4 spec says:
5313 * "Proxy arrays for two-dimensional multisample and two-dimensional
5314 * multisample array textures are operated on in the same way when
5315 * TexImage2DMultisample is called with target specified as
5316 * PROXY_TEXTURE_2D_MULTISAMPLE, or TexImage3DMultisample is called
5317 * with target specified as PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY.
5318 * However, if samples is not supported, then no error is generated.
5319 */
5320 if (!samplesOK && !_mesa_is_proxy_texture(target)) {
5321 _mesa_error(ctx, sample_count_error, "%s(samples)", func);
5322 return;
5323 }
5324
5325 if (immutable && (!texObj || (texObj->Name == 0))) {
5326 _mesa_error(ctx, GL_INVALID_OPERATION,
5327 "%s(texture object 0)",
5328 func);
5329 return;
5330 }
5331
5332 texImage = _mesa_get_tex_image(ctx, texObj, 0, 0);
5333
5334 if (texImage == NULL) {
5335 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s()", func);
5336 return;
5337 }
5338
5339 texFormat = _mesa_choose_texture_format(ctx, texObj, target, 0,
5340 internalformat, GL_NONE, GL_NONE);
5341 assert(texFormat != MESA_FORMAT_NONE);
5342
5343 dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, 0,
5344 width, height, depth, 0);
5345
5346 sizeOK = ctx->Driver.TestProxyTexImage(ctx, target, 0, texFormat,
5347 width, height, depth, 0);
5348
5349 if (_mesa_is_proxy_texture(target)) {
5350 if (samplesOK && dimensionsOK && sizeOK) {
5351 init_teximage_fields_ms(ctx, texImage, width, height, depth, 0,
5352 internalformat, texFormat,
5353 samples, fixedsamplelocations);
5354 }
5355 else {
5356 /* clear all image fields */
5357 clear_teximage_fields(texImage);
5358 }
5359 }
5360 else {
5361 if (!dimensionsOK) {
5362 _mesa_error(ctx, GL_INVALID_VALUE,
5363 "%s(invalid width or height)", func);
5364 return;
5365 }
5366
5367 if (!sizeOK) {
5368 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s(texture too large)", func);
5369 return;
5370 }
5371
5372 /* Check if texObj->Immutable is set */
5373 if (texObj->Immutable) {
5374 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
5375 return;
5376 }
5377
5378 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
5379
5380 init_teximage_fields_ms(ctx, texImage, width, height, depth, 0,
5381 internalformat, texFormat,
5382 samples, fixedsamplelocations);
5383
5384 if (width > 0 && height > 0 && depth > 0) {
5385 if (!ctx->Driver.AllocTextureStorage(ctx, texObj, 1,
5386 width, height, depth)) {
5387 /* tidy up the texture image state. strictly speaking,
5388 * we're allowed to just leave this in whatever state we
5389 * like, but being tidy is good.
5390 */
5391 _mesa_init_teximage_fields(ctx, texImage,
5392 0, 0, 0, 0, GL_NONE, MESA_FORMAT_NONE);
5393 }
5394 }
5395
5396 texObj->Immutable |= immutable;
5397
5398 if (immutable) {
5399 _mesa_set_texture_view_state(ctx, texObj, target, 1);
5400 }
5401
5402 _mesa_update_fbo_texture(ctx, texObj, 0, 0);
5403 }
5404 }
5405
5406
5407 void GLAPIENTRY
5408 _mesa_TexImage2DMultisample(GLenum target, GLsizei samples,
5409 GLenum internalformat, GLsizei width,
5410 GLsizei height, GLboolean fixedsamplelocations)
5411 {
5412 struct gl_texture_object *texObj;
5413 GET_CURRENT_CONTEXT(ctx);
5414
5415 texObj = _mesa_get_current_tex_object(ctx, target);
5416 if (!texObj)
5417 return;
5418
5419 texture_image_multisample(ctx, 2, texObj, target, samples,
5420 internalformat, width, height, 1,
5421 fixedsamplelocations, GL_FALSE,
5422 "glTexImage2DMultisample");
5423 }
5424
5425
5426 void GLAPIENTRY
5427 _mesa_TexImage3DMultisample(GLenum target, GLsizei samples,
5428 GLenum internalformat, GLsizei width,
5429 GLsizei height, GLsizei depth,
5430 GLboolean fixedsamplelocations)
5431 {
5432 struct gl_texture_object *texObj;
5433 GET_CURRENT_CONTEXT(ctx);
5434
5435 texObj = _mesa_get_current_tex_object(ctx, target);
5436 if (!texObj)
5437 return;
5438
5439 texture_image_multisample(ctx, 3, texObj, target, samples,
5440 internalformat, width, height, depth,
5441 fixedsamplelocations, GL_FALSE,
5442 "glTexImage3DMultisample");
5443 }
5444
5445 static bool
5446 valid_texstorage_ms_parameters(GLsizei width, GLsizei height, GLsizei depth,
5447 GLsizei samples, unsigned dims)
5448 {
5449 GET_CURRENT_CONTEXT(ctx);
5450
5451 if (!_mesa_valid_tex_storage_dim(width, height, depth)) {
5452 _mesa_error(ctx, GL_INVALID_VALUE,
5453 "glTexStorage%uDMultisample(width=%d,height=%d,depth=%d)",
5454 dims, width, height, depth);
5455 return false;
5456 }
5457 return true;
5458 }
5459
5460 void GLAPIENTRY
5461 _mesa_TexStorage2DMultisample(GLenum target, GLsizei samples,
5462 GLenum internalformat, GLsizei width,
5463 GLsizei height, GLboolean fixedsamplelocations)
5464 {
5465 struct gl_texture_object *texObj;
5466 GET_CURRENT_CONTEXT(ctx);
5467
5468 texObj = _mesa_get_current_tex_object(ctx, target);
5469 if (!texObj)
5470 return;
5471
5472 if (!valid_texstorage_ms_parameters(width, height, 1, samples, 2))
5473 return;
5474
5475 texture_image_multisample(ctx, 2, texObj, target, samples,
5476 internalformat, width, height, 1,
5477 fixedsamplelocations, GL_TRUE,
5478 "glTexStorage2DMultisample");
5479 }
5480
5481 void GLAPIENTRY
5482 _mesa_TexStorage3DMultisample(GLenum target, GLsizei samples,
5483 GLenum internalformat, GLsizei width,
5484 GLsizei height, GLsizei depth,
5485 GLboolean fixedsamplelocations)
5486 {
5487 struct gl_texture_object *texObj;
5488 GET_CURRENT_CONTEXT(ctx);
5489
5490 texObj = _mesa_get_current_tex_object(ctx, target);
5491 if (!texObj)
5492 return;
5493
5494 if (!valid_texstorage_ms_parameters(width, height, depth, samples, 3))
5495 return;
5496
5497 texture_image_multisample(ctx, 3, texObj, target, samples,
5498 internalformat, width, height, depth,
5499 fixedsamplelocations, GL_TRUE,
5500 "glTexStorage3DMultisample");
5501 }
5502
5503 void GLAPIENTRY
5504 _mesa_TextureStorage2DMultisample(GLuint texture, GLsizei samples,
5505 GLenum internalformat, GLsizei width,
5506 GLsizei height,
5507 GLboolean fixedsamplelocations)
5508 {
5509 struct gl_texture_object *texObj;
5510 GET_CURRENT_CONTEXT(ctx);
5511
5512 texObj = _mesa_lookup_texture_err(ctx, texture,
5513 "glTextureStorage2DMultisample");
5514 if (!texObj)
5515 return;
5516
5517 if (!valid_texstorage_ms_parameters(width, height, 1, samples, 2))
5518 return;
5519
5520 texture_image_multisample(ctx, 2, texObj, texObj->Target, samples,
5521 internalformat, width, height, 1,
5522 fixedsamplelocations, GL_TRUE,
5523 "glTextureStorage2DMultisample");
5524 }
5525
5526 void GLAPIENTRY
5527 _mesa_TextureStorage3DMultisample(GLuint texture, GLsizei samples,
5528 GLenum internalformat, GLsizei width,
5529 GLsizei height, GLsizei depth,
5530 GLboolean fixedsamplelocations)
5531 {
5532 struct gl_texture_object *texObj;
5533 GET_CURRENT_CONTEXT(ctx);
5534
5535 /* Get the texture object by Name. */
5536 texObj = _mesa_lookup_texture_err(ctx, texture,
5537 "glTextureStorage3DMultisample");
5538 if (!texObj)
5539 return;
5540
5541 if (!valid_texstorage_ms_parameters(width, height, depth, samples, 3))
5542 return;
5543
5544 texture_image_multisample(ctx, 3, texObj, texObj->Target, samples,
5545 internalformat, width, height, depth,
5546 fixedsamplelocations, GL_TRUE,
5547 "glTextureStorage3DMultisample");
5548 }