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