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