mesa: add some debug info to teximage.c
[mesa.git] / src / mesa / main / teximage.c
1 /*
2 * mesa 3-D graphics library
3 * Version: 7.6
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /**
28 * \file teximage.c
29 * Texture image-related functions.
30 */
31
32
33 #include "glheader.h"
34 #include "bufferobj.h"
35 #include "context.h"
36 #if FEATURE_convolve
37 #include "convolve.h"
38 #endif
39 #include "enums.h"
40 #include "fbobject.h"
41 #include "framebuffer.h"
42 #include "hash.h"
43 #include "image.h"
44 #include "imports.h"
45 #include "macros.h"
46 #include "state.h"
47 #include "texcompress.h"
48 #include "texformat.h"
49 #include "teximage.h"
50 #include "texstate.h"
51 #include "texstore.h"
52 #include "mtypes.h"
53
54
55 /**
56 * State changes which we care about for glCopyTex[Sub]Image() calls.
57 * In particular, we care about pixel transfer state and buffer state
58 * (such as glReadBuffer to make sure we read from the right renderbuffer).
59 */
60 #define NEW_COPY_TEX_STATE (_MESA_NEW_TRANSFER_STATE | \
61 _NEW_BUFFERS | \
62 _NEW_PIXEL)
63
64
65
66 /**
67 * We allocate texture memory on 512-byte boundaries so we can use MMX/SSE
68 * elsewhere.
69 */
70 void *
71 _mesa_alloc_texmemory(GLsizei bytes)
72 {
73 return _mesa_align_malloc(bytes, 512);
74 }
75
76
77 /**
78 * Free texture memory allocated with _mesa_alloc_texmemory()
79 */
80 void
81 _mesa_free_texmemory(void *m)
82 {
83 _mesa_align_free(m);
84 }
85
86
87
88
89 #if 0
90 static void PrintTexture(GLcontext *ctx, const struct gl_texture_image *img)
91 {
92 #if CHAN_TYPE != GL_UNSIGNED_BYTE
93 _mesa_problem(NULL, "PrintTexture not supported");
94 #else
95 GLuint i, j, c;
96 const GLubyte *data = (const GLubyte *) img->Data;
97
98 if (!data) {
99 _mesa_printf("No texture data\n");
100 return;
101 }
102
103 switch (img->Format) {
104 case GL_ALPHA:
105 case GL_LUMINANCE:
106 case GL_INTENSITY:
107 case GL_COLOR_INDEX:
108 c = 1;
109 break;
110 case GL_LUMINANCE_ALPHA:
111 c = 2;
112 break;
113 case GL_RGB:
114 c = 3;
115 break;
116 case GL_RGBA:
117 c = 4;
118 break;
119 default:
120 _mesa_problem(NULL, "error in PrintTexture\n");
121 return;
122 }
123
124 for (i = 0; i < img->Height; i++) {
125 for (j = 0; j < img->Width; j++) {
126 if (c==1)
127 _mesa_printf("%02x ", data[0]);
128 else if (c==2)
129 _mesa_printf("%02x%02x ", data[0], data[1]);
130 else if (c==3)
131 _mesa_printf("%02x%02x%02x ", data[0], data[1], data[2]);
132 else if (c==4)
133 _mesa_printf("%02x%02x%02x%02x ", data[0], data[1], data[2], data[3]);
134 data += (img->RowStride - img->Width) * c;
135 }
136 /* XXX use img->ImageStride here */
137 _mesa_printf("\n");
138 }
139 #endif
140 }
141 #endif
142
143
144 /*
145 * Compute floor(log_base_2(n)).
146 * If n < 0 return -1.
147 */
148 static int
149 logbase2( int n )
150 {
151 GLint i = 1;
152 GLint log2 = 0;
153
154 if (n < 0)
155 return -1;
156
157 if (n == 0)
158 return 0;
159
160 while ( n > i ) {
161 i *= 2;
162 log2++;
163 }
164 if (i != n) {
165 return log2 - 1;
166 }
167 else {
168 return log2;
169 }
170 }
171
172
173
174 /**
175 * Return the simple base format for a given internal texture format.
176 * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
177 *
178 * \param ctx GL context.
179 * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
180 *
181 * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
182 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
183 *
184 * This is the format which is used during texture application (i.e. the
185 * texture format and env mode determine the arithmetic used.
186 *
187 * XXX this could be static
188 */
189 GLint
190 _mesa_base_tex_format( GLcontext *ctx, GLint internalFormat )
191 {
192 switch (internalFormat) {
193 case GL_ALPHA:
194 case GL_ALPHA4:
195 case GL_ALPHA8:
196 case GL_ALPHA12:
197 case GL_ALPHA16:
198 return GL_ALPHA;
199 case 1:
200 case GL_LUMINANCE:
201 case GL_LUMINANCE4:
202 case GL_LUMINANCE8:
203 case GL_LUMINANCE12:
204 case GL_LUMINANCE16:
205 return GL_LUMINANCE;
206 case 2:
207 case GL_LUMINANCE_ALPHA:
208 case GL_LUMINANCE4_ALPHA4:
209 case GL_LUMINANCE6_ALPHA2:
210 case GL_LUMINANCE8_ALPHA8:
211 case GL_LUMINANCE12_ALPHA4:
212 case GL_LUMINANCE12_ALPHA12:
213 case GL_LUMINANCE16_ALPHA16:
214 return GL_LUMINANCE_ALPHA;
215 case GL_INTENSITY:
216 case GL_INTENSITY4:
217 case GL_INTENSITY8:
218 case GL_INTENSITY12:
219 case GL_INTENSITY16:
220 return GL_INTENSITY;
221 case 3:
222 case GL_RGB:
223 case GL_R3_G3_B2:
224 case GL_RGB4:
225 case GL_RGB5:
226 case GL_RGB8:
227 case GL_RGB10:
228 case GL_RGB12:
229 case GL_RGB16:
230 return GL_RGB;
231 case 4:
232 case GL_RGBA:
233 case GL_RGBA2:
234 case GL_RGBA4:
235 case GL_RGB5_A1:
236 case GL_RGBA8:
237 case GL_RGB10_A2:
238 case GL_RGBA12:
239 case GL_RGBA16:
240 return GL_RGBA;
241 default:
242 ; /* fallthrough */
243 }
244
245 if (ctx->Extensions.EXT_paletted_texture) {
246 switch (internalFormat) {
247 case GL_COLOR_INDEX:
248 case GL_COLOR_INDEX1_EXT:
249 case GL_COLOR_INDEX2_EXT:
250 case GL_COLOR_INDEX4_EXT:
251 case GL_COLOR_INDEX8_EXT:
252 case GL_COLOR_INDEX12_EXT:
253 case GL_COLOR_INDEX16_EXT:
254 return GL_COLOR_INDEX;
255 default:
256 ; /* fallthrough */
257 }
258 }
259
260 if (ctx->Extensions.ARB_depth_texture) {
261 switch (internalFormat) {
262 case GL_DEPTH_COMPONENT:
263 case GL_DEPTH_COMPONENT16:
264 case GL_DEPTH_COMPONENT24:
265 case GL_DEPTH_COMPONENT32:
266 return GL_DEPTH_COMPONENT;
267 default:
268 ; /* fallthrough */
269 }
270 }
271
272 switch (internalFormat) {
273 case GL_COMPRESSED_ALPHA:
274 return GL_ALPHA;
275 case GL_COMPRESSED_LUMINANCE:
276 return GL_LUMINANCE;
277 case GL_COMPRESSED_LUMINANCE_ALPHA:
278 return GL_LUMINANCE_ALPHA;
279 case GL_COMPRESSED_INTENSITY:
280 return GL_INTENSITY;
281 case GL_COMPRESSED_RGB:
282 return GL_RGB;
283 case GL_COMPRESSED_RGBA:
284 return GL_RGBA;
285 default:
286 ; /* fallthrough */
287 }
288
289 if (ctx->Extensions.TDFX_texture_compression_FXT1) {
290 switch (internalFormat) {
291 case GL_COMPRESSED_RGB_FXT1_3DFX:
292 return GL_RGB;
293 case GL_COMPRESSED_RGBA_FXT1_3DFX:
294 return GL_RGBA;
295 default:
296 ; /* fallthrough */
297 }
298 }
299
300 if (ctx->Extensions.EXT_texture_compression_s3tc) {
301 switch (internalFormat) {
302 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
303 return GL_RGB;
304 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
305 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
306 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
307 return GL_RGBA;
308 default:
309 ; /* fallthrough */
310 }
311 }
312
313 if (ctx->Extensions.S3_s3tc) {
314 switch (internalFormat) {
315 case GL_RGB_S3TC:
316 case GL_RGB4_S3TC:
317 return GL_RGB;
318 case GL_RGBA_S3TC:
319 case GL_RGBA4_S3TC:
320 return GL_RGBA;
321 default:
322 ; /* fallthrough */
323 }
324 }
325
326 if (ctx->Extensions.MESA_ycbcr_texture) {
327 if (internalFormat == GL_YCBCR_MESA)
328 return GL_YCBCR_MESA;
329 }
330
331 if (ctx->Extensions.ARB_texture_float) {
332 switch (internalFormat) {
333 case GL_ALPHA16F_ARB:
334 case GL_ALPHA32F_ARB:
335 return GL_ALPHA;
336 case GL_RGBA16F_ARB:
337 case GL_RGBA32F_ARB:
338 return GL_RGBA;
339 case GL_RGB16F_ARB:
340 case GL_RGB32F_ARB:
341 return GL_RGB;
342 case GL_INTENSITY16F_ARB:
343 case GL_INTENSITY32F_ARB:
344 return GL_INTENSITY;
345 case GL_LUMINANCE16F_ARB:
346 case GL_LUMINANCE32F_ARB:
347 return GL_LUMINANCE;
348 case GL_LUMINANCE_ALPHA16F_ARB:
349 case GL_LUMINANCE_ALPHA32F_ARB:
350 return GL_LUMINANCE_ALPHA;
351 default:
352 ; /* fallthrough */
353 }
354 }
355
356 if (ctx->Extensions.ATI_envmap_bumpmap) {
357 switch (internalFormat) {
358 case GL_DUDV_ATI:
359 case GL_DU8DV8_ATI:
360 return GL_DUDV_ATI;
361 default:
362 ; /* fallthrough */
363 }
364 }
365
366 if (ctx->Extensions.MESA_texture_signed_rgba) {
367 switch (internalFormat) {
368 case GL_RGBA_SNORM:
369 case GL_RGBA8_SNORM:
370 return GL_RGBA;
371 default:
372 ; /* fallthrough */
373 }
374 }
375
376 if (ctx->Extensions.EXT_packed_depth_stencil) {
377 switch (internalFormat) {
378 case GL_DEPTH_STENCIL_EXT:
379 case GL_DEPTH24_STENCIL8_EXT:
380 return GL_DEPTH_STENCIL_EXT;
381 default:
382 ; /* fallthrough */
383 }
384 }
385
386 #if FEATURE_EXT_texture_sRGB
387 if (ctx->Extensions.EXT_texture_sRGB) {
388 switch (internalFormat) {
389 case GL_SRGB_EXT:
390 case GL_SRGB8_EXT:
391 case GL_COMPRESSED_SRGB_EXT:
392 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
393 return GL_RGB;
394 case GL_SRGB_ALPHA_EXT:
395 case GL_SRGB8_ALPHA8_EXT:
396 case GL_COMPRESSED_SRGB_ALPHA_EXT:
397 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
398 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
399 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
400 return GL_RGBA;
401 case GL_SLUMINANCE_ALPHA_EXT:
402 case GL_SLUMINANCE8_ALPHA8_EXT:
403 case GL_COMPRESSED_SLUMINANCE_EXT:
404 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT:
405 return GL_LUMINANCE_ALPHA;
406 case GL_SLUMINANCE_EXT:
407 case GL_SLUMINANCE8_EXT:
408 return GL_LUMINANCE;
409 default:
410 ; /* fallthrough */
411 }
412 }
413
414 #endif /* FEATURE_EXT_texture_sRGB */
415
416 return -1; /* error */
417 }
418
419
420 /**
421 * Test if it is a supported compressed format.
422 *
423 * \param internalFormat the internal format token provided by the user.
424 *
425 * \ret GL_TRUE if \p internalFormat is a supported compressed format, or
426 * GL_FALSE otherwise.
427 *
428 * Currently only GL_COMPRESSED_RGB_FXT1_3DFX and GL_COMPRESSED_RGBA_FXT1_3DFX
429 * are supported.
430 */
431 static GLboolean
432 is_compressed_format(GLcontext *ctx, GLenum internalFormat)
433 {
434 GLint supported[100]; /* 100 should be plenty */
435 GLuint i, n;
436
437 n = _mesa_get_compressed_formats(ctx, supported, GL_TRUE);
438 ASSERT(n < 100);
439 for (i = 0; i < n; i++) {
440 if ((GLint) internalFormat == supported[i]) {
441 return GL_TRUE;
442 }
443 }
444 return GL_FALSE;
445 }
446
447
448 /**
449 * For cube map faces, return a face index in [0,5].
450 * For other targets return 0;
451 */
452 GLuint
453 _mesa_tex_target_to_face(GLenum target)
454 {
455 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
456 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)
457 return (GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X;
458 else
459 return 0;
460 }
461
462
463
464 /**
465 * Store a gl_texture_image pointer in a gl_texture_object structure
466 * according to the target and level parameters.
467 *
468 * \param tObj texture object.
469 * \param target texture target.
470 * \param level image level.
471 * \param texImage texture image.
472 *
473 * This was basically prompted by the introduction of cube maps.
474 */
475 void
476 _mesa_set_tex_image(struct gl_texture_object *tObj,
477 GLenum target, GLint level,
478 struct gl_texture_image *texImage)
479 {
480 const GLuint face = _mesa_tex_target_to_face(target);
481
482 ASSERT(tObj);
483 ASSERT(texImage);
484 ASSERT(target != GL_TEXTURE_RECTANGLE_NV || level == 0);
485
486 tObj->Image[face][level] = texImage;
487
488 /* Set the 'back' pointer */
489 texImage->TexObject = tObj;
490 }
491
492
493 /**
494 * Allocate a texture image structure.
495 *
496 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
497 * driver.
498 *
499 * \return a pointer to gl_texture_image struct with all fields initialized to
500 * zero.
501 */
502 struct gl_texture_image *
503 _mesa_new_texture_image( GLcontext *ctx )
504 {
505 (void) ctx;
506 return CALLOC_STRUCT(gl_texture_image);
507 }
508
509
510 /**
511 * Free texture image data.
512 * This function is a fallback called via ctx->Driver.FreeTexImageData().
513 *
514 * \param texImage texture image.
515 *
516 * Free the texture image data if it's not marked as client data.
517 */
518 void
519 _mesa_free_texture_image_data(GLcontext *ctx,
520 struct gl_texture_image *texImage)
521 {
522 (void) ctx;
523
524 if (texImage->Data && !texImage->IsClientData) {
525 /* free the old texture data */
526 _mesa_free_texmemory(texImage->Data);
527 }
528
529 texImage->Data = NULL;
530 }
531
532
533 /**
534 * Free texture image.
535 *
536 * \param texImage texture image.
537 *
538 * Free the texture image structure and the associated image data.
539 */
540 void
541 _mesa_delete_texture_image( GLcontext *ctx, struct gl_texture_image *texImage )
542 {
543 /* Free texImage->Data and/or any other driver-specific texture
544 * image storage.
545 */
546 ASSERT(ctx->Driver.FreeTexImageData);
547 ctx->Driver.FreeTexImageData( ctx, texImage );
548
549 ASSERT(texImage->Data == NULL);
550 if (texImage->ImageOffsets)
551 _mesa_free(texImage->ImageOffsets);
552 _mesa_free(texImage);
553 }
554
555
556 /**
557 * Test if a target is a proxy target.
558 *
559 * \param target texture target.
560 *
561 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
562 */
563 GLboolean
564 _mesa_is_proxy_texture(GLenum target)
565 {
566 /* NUM_TEXTURE_TARGETS should match number of terms below */
567 assert(NUM_TEXTURE_TARGETS == 7);
568
569 return (target == GL_PROXY_TEXTURE_1D ||
570 target == GL_PROXY_TEXTURE_2D ||
571 target == GL_PROXY_TEXTURE_3D ||
572 target == GL_PROXY_TEXTURE_CUBE_MAP_ARB ||
573 target == GL_PROXY_TEXTURE_RECTANGLE_NV ||
574 target == GL_PROXY_TEXTURE_1D_ARRAY_EXT ||
575 target == GL_PROXY_TEXTURE_2D_ARRAY_EXT);
576 }
577
578
579 /**
580 * Get the texture object that corresponds to the target of the given texture unit.
581 *
582 * \param ctx GL context.
583 * \param texUnit texture unit.
584 * \param target texture target.
585 *
586 * \return pointer to the texture object on success, or NULL on failure.
587 *
588 * \sa gl_texture_unit.
589 */
590 struct gl_texture_object *
591 _mesa_select_tex_object(GLcontext *ctx, const struct gl_texture_unit *texUnit,
592 GLenum target)
593 {
594 switch (target) {
595 case GL_TEXTURE_1D:
596 return texUnit->CurrentTex[TEXTURE_1D_INDEX];
597 case GL_PROXY_TEXTURE_1D:
598 return ctx->Texture.ProxyTex[TEXTURE_1D_INDEX];
599 case GL_TEXTURE_2D:
600 return texUnit->CurrentTex[TEXTURE_2D_INDEX];
601 case GL_PROXY_TEXTURE_2D:
602 return ctx->Texture.ProxyTex[TEXTURE_2D_INDEX];
603 case GL_TEXTURE_3D:
604 return texUnit->CurrentTex[TEXTURE_3D_INDEX];
605 case GL_PROXY_TEXTURE_3D:
606 return ctx->Texture.ProxyTex[TEXTURE_3D_INDEX];
607 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
608 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
609 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
610 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
611 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
612 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
613 case GL_TEXTURE_CUBE_MAP_ARB:
614 return ctx->Extensions.ARB_texture_cube_map
615 ? texUnit->CurrentTex[TEXTURE_CUBE_INDEX] : NULL;
616 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
617 return ctx->Extensions.ARB_texture_cube_map
618 ? ctx->Texture.ProxyTex[TEXTURE_CUBE_INDEX] : NULL;
619 case GL_TEXTURE_RECTANGLE_NV:
620 return ctx->Extensions.NV_texture_rectangle
621 ? texUnit->CurrentTex[TEXTURE_RECT_INDEX] : NULL;
622 case GL_PROXY_TEXTURE_RECTANGLE_NV:
623 return ctx->Extensions.NV_texture_rectangle
624 ? ctx->Texture.ProxyTex[TEXTURE_RECT_INDEX] : NULL;
625 case GL_TEXTURE_1D_ARRAY_EXT:
626 return ctx->Extensions.MESA_texture_array
627 ? texUnit->CurrentTex[TEXTURE_1D_ARRAY_INDEX] : NULL;
628 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
629 return ctx->Extensions.MESA_texture_array
630 ? ctx->Texture.ProxyTex[TEXTURE_1D_ARRAY_INDEX] : NULL;
631 case GL_TEXTURE_2D_ARRAY_EXT:
632 return ctx->Extensions.MESA_texture_array
633 ? texUnit->CurrentTex[TEXTURE_2D_ARRAY_INDEX] : NULL;
634 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
635 return ctx->Extensions.MESA_texture_array
636 ? ctx->Texture.ProxyTex[TEXTURE_2D_ARRAY_INDEX] : NULL;
637 default:
638 _mesa_problem(NULL, "bad target in _mesa_select_tex_object()");
639 return NULL;
640 }
641 }
642
643
644 /**
645 * Get a texture image pointer from a texture object, given a texture
646 * target and mipmap level. The target and level parameters should
647 * have already been error-checked.
648 *
649 * \param ctx GL context.
650 * \param texObj texture unit.
651 * \param target texture target.
652 * \param level image level.
653 *
654 * \return pointer to the texture image structure, or NULL on failure.
655 */
656 struct gl_texture_image *
657 _mesa_select_tex_image(GLcontext *ctx, const struct gl_texture_object *texObj,
658 GLenum target, GLint level)
659 {
660 const GLuint face = _mesa_tex_target_to_face(target);
661
662 ASSERT(texObj);
663 ASSERT(level >= 0);
664 ASSERT(level < MAX_TEXTURE_LEVELS);
665
666 return texObj->Image[face][level];
667 }
668
669
670 /**
671 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
672 * it and install it. Only return NULL if passed a bad parameter or run
673 * out of memory.
674 */
675 struct gl_texture_image *
676 _mesa_get_tex_image(GLcontext *ctx, struct gl_texture_object *texObj,
677 GLenum target, GLint level)
678 {
679 struct gl_texture_image *texImage;
680
681 if (!texObj)
682 return NULL;
683
684 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
685 if (!texImage) {
686 texImage = ctx->Driver.NewTextureImage(ctx);
687 if (!texImage) {
688 _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture image allocation");
689 return NULL;
690 }
691
692 _mesa_set_tex_image(texObj, target, level, texImage);
693 }
694
695 return texImage;
696 }
697
698
699 /**
700 * Return pointer to the specified proxy texture image.
701 * Note that proxy textures are per-context, not per-texture unit.
702 * \return pointer to texture image or NULL if invalid target, invalid
703 * level, or out of memory.
704 */
705 struct gl_texture_image *
706 _mesa_get_proxy_tex_image(GLcontext *ctx, GLenum target, GLint level)
707 {
708 struct gl_texture_image *texImage;
709 GLuint texIndex;
710
711 if (level < 0 )
712 return NULL;
713
714 switch (target) {
715 case GL_PROXY_TEXTURE_1D:
716 if (level >= ctx->Const.MaxTextureLevels)
717 return NULL;
718 texIndex = TEXTURE_1D_INDEX;
719 break;
720 case GL_PROXY_TEXTURE_2D:
721 if (level >= ctx->Const.MaxTextureLevels)
722 return NULL;
723 texIndex = TEXTURE_2D_INDEX;
724 break;
725 case GL_PROXY_TEXTURE_3D:
726 if (level >= ctx->Const.Max3DTextureLevels)
727 return NULL;
728 texIndex = TEXTURE_3D_INDEX;
729 break;
730 case GL_PROXY_TEXTURE_CUBE_MAP:
731 if (level >= ctx->Const.MaxCubeTextureLevels)
732 return NULL;
733 texIndex = TEXTURE_CUBE_INDEX;
734 break;
735 case GL_PROXY_TEXTURE_RECTANGLE_NV:
736 if (level > 0)
737 return NULL;
738 texIndex = TEXTURE_RECT_INDEX;
739 break;
740 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
741 if (level >= ctx->Const.MaxTextureLevels)
742 return NULL;
743 texIndex = TEXTURE_1D_ARRAY_INDEX;
744 break;
745 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
746 if (level >= ctx->Const.MaxTextureLevels)
747 return NULL;
748 texIndex = TEXTURE_2D_ARRAY_INDEX;
749 break;
750 default:
751 return NULL;
752 }
753
754 texImage = ctx->Texture.ProxyTex[texIndex]->Image[0][level];
755 if (!texImage) {
756 texImage = ctx->Driver.NewTextureImage(ctx);
757 if (!texImage) {
758 _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
759 return NULL;
760 }
761 ctx->Texture.ProxyTex[texIndex]->Image[0][level] = texImage;
762 /* Set the 'back' pointer */
763 texImage->TexObject = ctx->Texture.ProxyTex[texIndex];
764 }
765 return texImage;
766 }
767
768
769 /**
770 * Get the maximum number of allowed mipmap levels.
771 *
772 * \param ctx GL context.
773 * \param target texture target.
774 *
775 * \return the maximum number of allowed mipmap levels for the given
776 * texture target, or zero if passed a bad target.
777 *
778 * \sa gl_constants.
779 */
780 GLint
781 _mesa_max_texture_levels(GLcontext *ctx, GLenum target)
782 {
783 switch (target) {
784 case GL_TEXTURE_1D:
785 case GL_PROXY_TEXTURE_1D:
786 case GL_TEXTURE_2D:
787 case GL_PROXY_TEXTURE_2D:
788 return ctx->Const.MaxTextureLevels;
789 case GL_TEXTURE_3D:
790 case GL_PROXY_TEXTURE_3D:
791 return ctx->Const.Max3DTextureLevels;
792 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
793 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
794 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
795 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
796 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
797 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
798 case GL_TEXTURE_CUBE_MAP_ARB:
799 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
800 return ctx->Extensions.ARB_texture_cube_map
801 ? ctx->Const.MaxCubeTextureLevels : 0;
802 case GL_TEXTURE_RECTANGLE_NV:
803 case GL_PROXY_TEXTURE_RECTANGLE_NV:
804 return ctx->Extensions.NV_texture_rectangle ? 1 : 0;
805 case GL_TEXTURE_1D_ARRAY_EXT:
806 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
807 case GL_TEXTURE_2D_ARRAY_EXT:
808 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
809 return ctx->Extensions.MESA_texture_array
810 ? ctx->Const.MaxTextureLevels : 0;
811 default:
812 return 0; /* bad target */
813 }
814 }
815
816
817
818 #if 000 /* not used anymore */
819 /*
820 * glTexImage[123]D can accept a NULL image pointer. In this case we
821 * create a texture image with unspecified image contents per the OpenGL
822 * spec.
823 */
824 static GLubyte *
825 make_null_texture(GLint width, GLint height, GLint depth, GLenum format)
826 {
827 const GLint components = _mesa_components_in_format(format);
828 const GLint numPixels = width * height * depth;
829 GLubyte *data = (GLubyte *) MALLOC(numPixels * components * sizeof(GLubyte));
830
831 #ifdef DEBUG
832 /*
833 * Let's see if anyone finds this. If glTexImage2D() is called with
834 * a NULL image pointer then load the texture image with something
835 * interesting instead of leaving it indeterminate.
836 */
837 if (data) {
838 static const char message[8][32] = {
839 " X X XXXXX XXX X ",
840 " XX XX X X X X X ",
841 " X X X X X X X ",
842 " X X XXXX XXX XXXXX ",
843 " X X X X X X ",
844 " X X X X X X X ",
845 " X X XXXXX XXX X X ",
846 " "
847 };
848
849 GLubyte *imgPtr = data;
850 GLint h, i, j, k;
851 for (h = 0; h < depth; h++) {
852 for (i = 0; i < height; i++) {
853 GLint srcRow = 7 - (i % 8);
854 for (j = 0; j < width; j++) {
855 GLint srcCol = j % 32;
856 GLubyte texel = (message[srcRow][srcCol]=='X') ? 255 : 70;
857 for (k = 0; k < components; k++) {
858 *imgPtr++ = texel;
859 }
860 }
861 }
862 }
863 }
864 #endif
865
866 return data;
867 }
868 #endif
869
870
871
872 /**
873 * Reset the fields of a gl_texture_image struct to zero.
874 *
875 * \param img texture image structure.
876 *
877 * This is called when a proxy texture test fails, we set all the
878 * image members (except DriverData) to zero.
879 * It's also used in glTexImage[123]D as a safeguard to be sure all
880 * required fields get initialized properly by the Driver.TexImage[123]D
881 * functions.
882 */
883 static void
884 clear_teximage_fields(struct gl_texture_image *img)
885 {
886 ASSERT(img);
887 img->_BaseFormat = 0;
888 img->InternalFormat = 0;
889 img->Border = 0;
890 img->Width = 0;
891 img->Height = 0;
892 img->Depth = 0;
893 img->RowStride = 0;
894 if (img->ImageOffsets) {
895 _mesa_free(img->ImageOffsets);
896 img->ImageOffsets = NULL;
897 }
898 img->Width2 = 0;
899 img->Height2 = 0;
900 img->Depth2 = 0;
901 img->WidthLog2 = 0;
902 img->HeightLog2 = 0;
903 img->DepthLog2 = 0;
904 img->Data = NULL;
905 img->TexFormat = &_mesa_null_texformat;
906 img->FetchTexelc = NULL;
907 img->FetchTexelf = NULL;
908 img->IsCompressed = 0;
909 img->CompressedSize = 0;
910 }
911
912
913 /**
914 * Initialize basic fields of the gl_texture_image struct.
915 *
916 * \param ctx GL context.
917 * \param target texture target (GL_TEXTURE_1D, GL_TEXTURE_RECTANGLE, etc).
918 * \param img texture image structure to be initialized.
919 * \param width image width.
920 * \param height image height.
921 * \param depth image depth.
922 * \param border image border.
923 * \param internalFormat internal format.
924 *
925 * Fills in the fields of \p img with the given information.
926 * Note: width, height and depth include the border.
927 */
928 void
929 _mesa_init_teximage_fields(GLcontext *ctx, GLenum target,
930 struct gl_texture_image *img,
931 GLsizei width, GLsizei height, GLsizei depth,
932 GLint border, GLenum internalFormat)
933 {
934 GLint i;
935
936 ASSERT(img);
937 ASSERT(width >= 0);
938 ASSERT(height >= 0);
939 ASSERT(depth >= 0);
940
941 img->_BaseFormat = _mesa_base_tex_format( ctx, internalFormat );
942 ASSERT(img->_BaseFormat > 0);
943 img->InternalFormat = internalFormat;
944 img->Border = border;
945 img->Width = width;
946 img->Height = height;
947 img->Depth = depth;
948
949 img->Width2 = width - 2 * border; /* == 1 << img->WidthLog2; */
950 img->WidthLog2 = logbase2(img->Width2);
951
952 if (height == 1) { /* 1-D texture */
953 img->Height2 = 1;
954 img->HeightLog2 = 0;
955 }
956 else {
957 img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
958 img->HeightLog2 = logbase2(img->Height2);
959 }
960
961 if (depth == 1) { /* 2-D texture */
962 img->Depth2 = 1;
963 img->DepthLog2 = 0;
964 }
965 else {
966 img->Depth2 = depth - 2 * border; /* == 1 << img->DepthLog2; */
967 img->DepthLog2 = logbase2(img->Depth2);
968 }
969
970 img->MaxLog2 = MAX2(img->WidthLog2, img->HeightLog2);
971
972 img->IsCompressed = GL_FALSE;
973 img->CompressedSize = 0;
974
975 if ((width == 1 || _mesa_is_pow_two(img->Width2)) &&
976 (height == 1 || _mesa_is_pow_two(img->Height2)) &&
977 (depth == 1 || _mesa_is_pow_two(img->Depth2)))
978 img->_IsPowerOfTwo = GL_TRUE;
979 else
980 img->_IsPowerOfTwo = GL_FALSE;
981
982 /* RowStride and ImageOffsets[] describe how to address texels in 'Data' */
983 img->RowStride = width;
984 /* Allocate the ImageOffsets array and initialize to typical values.
985 * We allocate the array for 1D/2D textures too in order to avoid special-
986 * case code in the texstore routines.
987 */
988 if (img->ImageOffsets)
989 _mesa_free(img->ImageOffsets);
990 img->ImageOffsets = (GLuint *) _mesa_malloc(depth * sizeof(GLuint));
991 for (i = 0; i < depth; i++) {
992 img->ImageOffsets[i] = i * width * height;
993 }
994
995 /* Compute Width/Height/DepthScale for mipmap lod computation */
996 if (target == GL_TEXTURE_RECTANGLE_NV) {
997 /* scale = 1.0 since texture coords directly map to texels */
998 img->WidthScale = 1.0;
999 img->HeightScale = 1.0;
1000 img->DepthScale = 1.0;
1001 }
1002 else {
1003 img->WidthScale = (GLfloat) img->Width;
1004 img->HeightScale = (GLfloat) img->Height;
1005 img->DepthScale = (GLfloat) img->Depth;
1006 }
1007 }
1008
1009
1010 /**
1011 * Free and clear fields of the gl_texture_image struct.
1012 *
1013 * \param ctx GL context.
1014 * \param texImage texture image structure to be cleared.
1015 *
1016 * After the call, \p texImage will have no data associated with it. Its
1017 * fields are cleared so that its parent object will test incomplete.
1018 */
1019 void
1020 _mesa_clear_texture_image(GLcontext *ctx, struct gl_texture_image *texImage)
1021 {
1022 ctx->Driver.FreeTexImageData(ctx, texImage);
1023 clear_teximage_fields(texImage);
1024 }
1025
1026
1027 /**
1028 * This is the fallback for Driver.TestProxyTexImage(). Test the texture
1029 * level, width, height and depth against the ctx->Const limits for textures.
1030 *
1031 * A hardware driver might override this function if, for example, the
1032 * max 3D texture size is 512x512x64 (i.e. not a cube).
1033 *
1034 * Note that width, height, depth == 0 is not an error. However, a
1035 * texture with zero width/height/depth will be considered "incomplete"
1036 * and texturing will effectively be disabled.
1037 *
1038 * \param target one of GL_PROXY_TEXTURE_1D, GL_PROXY_TEXTURE_2D,
1039 * GL_PROXY_TEXTURE_3D, GL_PROXY_TEXTURE_RECTANGLE_NV,
1040 * GL_PROXY_TEXTURE_CUBE_MAP_ARB.
1041 * \param level as passed to glTexImage
1042 * \param internalFormat as passed to glTexImage
1043 * \param format as passed to glTexImage
1044 * \param type as passed to glTexImage
1045 * \param width as passed to glTexImage
1046 * \param height as passed to glTexImage
1047 * \param depth as passed to glTexImage
1048 * \param border as passed to glTexImage
1049 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1050 */
1051 GLboolean
1052 _mesa_test_proxy_teximage(GLcontext *ctx, GLenum target, GLint level,
1053 GLint internalFormat, GLenum format, GLenum type,
1054 GLint width, GLint height, GLint depth, GLint border)
1055 {
1056 GLint maxSize;
1057
1058 (void) internalFormat;
1059 (void) format;
1060 (void) type;
1061
1062 switch (target) {
1063 case GL_PROXY_TEXTURE_1D:
1064 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1065 if (width < 2 * border || width > 2 + maxSize ||
1066 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1067 width >0 && !_mesa_is_pow_two(width - 2 * border)) ||
1068 level >= ctx->Const.MaxTextureLevels) {
1069 /* bad width or level */
1070 return GL_FALSE;
1071 }
1072 return GL_TRUE;
1073 case GL_PROXY_TEXTURE_2D:
1074 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1075 if (width < 2 * border || width > 2 + maxSize ||
1076 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1077 width > 0 && !_mesa_is_pow_two(width - 2 * border)) ||
1078 height < 2 * border || height > 2 + maxSize ||
1079 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1080 height > 0 && !_mesa_is_pow_two(height - 2 * border)) ||
1081 level >= ctx->Const.MaxTextureLevels) {
1082 /* bad width or height or level */
1083 return GL_FALSE;
1084 }
1085 return GL_TRUE;
1086 case GL_PROXY_TEXTURE_3D:
1087 maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1088 if (width < 2 * border || width > 2 + maxSize ||
1089 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1090 width > 0 && !_mesa_is_pow_two(width - 2 * border)) ||
1091 height < 2 * border || height > 2 + maxSize ||
1092 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1093 height > 0 && !_mesa_is_pow_two(height - 2 * border)) ||
1094 depth < 2 * border || depth > 2 + maxSize ||
1095 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1096 depth > 0 && !_mesa_is_pow_two(depth - 2 * border)) ||
1097 level >= ctx->Const.Max3DTextureLevels) {
1098 /* bad width or height or depth or level */
1099 return GL_FALSE;
1100 }
1101 return GL_TRUE;
1102 case GL_PROXY_TEXTURE_RECTANGLE_NV:
1103 if (width < 0 || width > ctx->Const.MaxTextureRectSize ||
1104 height < 0 || height > ctx->Const.MaxTextureRectSize ||
1105 level != 0) {
1106 /* bad width or height or level */
1107 return GL_FALSE;
1108 }
1109 return GL_TRUE;
1110 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
1111 maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
1112 if (width < 2 * border || width > 2 + maxSize ||
1113 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1114 width > 0 && !_mesa_is_pow_two(width - 2 * border)) ||
1115 height < 2 * border || height > 2 + maxSize ||
1116 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1117 height > 0 && !_mesa_is_pow_two(height - 2 * border)) ||
1118 level >= ctx->Const.MaxCubeTextureLevels) {
1119 /* bad width or height */
1120 return GL_FALSE;
1121 }
1122 return GL_TRUE;
1123 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1124 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1125 if (width < 2 * border || width > 2 + maxSize ||
1126 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1127 width > 0 && !_mesa_is_pow_two(width - 2 * border)) ||
1128 level >= ctx->Const.MaxTextureLevels) {
1129 /* bad width or level */
1130 return GL_FALSE;
1131 }
1132
1133 if (height < 1 || height > ctx->Const.MaxArrayTextureLayers) {
1134 return GL_FALSE;
1135 }
1136 return GL_TRUE;
1137 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1138 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
1139 if (width < 2 * border || width > 2 + maxSize ||
1140 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1141 width > 0 && !_mesa_is_pow_two(width - 2 * border)) ||
1142 height < 2 * border || height > 2 + maxSize ||
1143 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1144 height > 0 && !_mesa_is_pow_two(height - 2 * border)) ||
1145 level >= ctx->Const.MaxTextureLevels) {
1146 /* bad width or height or level */
1147 return GL_FALSE;
1148 }
1149 if (depth < 1 || depth > ctx->Const.MaxArrayTextureLayers) {
1150 return GL_FALSE;
1151 }
1152 return GL_TRUE;
1153 default:
1154 _mesa_problem(ctx, "Invalid target in _mesa_test_proxy_teximage");
1155 return GL_FALSE;
1156 }
1157 }
1158
1159
1160 /**
1161 * Helper function to determine whether a target supports compressed textures
1162 */
1163 static GLboolean
1164 target_can_be_compressed(GLcontext *ctx, GLenum target)
1165 {
1166 return (((target == GL_TEXTURE_2D || target == GL_PROXY_TEXTURE_2D))
1167 || ((ctx->Extensions.ARB_texture_cube_map &&
1168 (target == GL_PROXY_TEXTURE_CUBE_MAP ||
1169 (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
1170 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z))))
1171 || ((ctx->Extensions.MESA_texture_array &&
1172 ((target == GL_PROXY_TEXTURE_2D_ARRAY_EXT) ||
1173 (target == GL_TEXTURE_2D_ARRAY_EXT)))));
1174 }
1175
1176
1177 /**
1178 * Test the glTexImage[123]D() parameters for errors.
1179 *
1180 * \param ctx GL context.
1181 * \param target texture target given by the user.
1182 * \param level image level given by the user.
1183 * \param internalFormat internal format given by the user.
1184 * \param format pixel data format given by the user.
1185 * \param type pixel data type given by the user.
1186 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1187 * \param width image width given by the user.
1188 * \param height image height given by the user.
1189 * \param depth image depth given by the user.
1190 * \param border image border given by the user.
1191 *
1192 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1193 *
1194 * Verifies each of the parameters against the constants specified in
1195 * __GLcontextRec::Const and the supported extensions, and according to the
1196 * OpenGL specification.
1197 */
1198 static GLboolean
1199 texture_error_check( GLcontext *ctx, GLenum target,
1200 GLint level, GLint internalFormat,
1201 GLenum format, GLenum type,
1202 GLuint dimensions,
1203 GLint width, GLint height,
1204 GLint depth, GLint border )
1205 {
1206 const GLboolean isProxy = _mesa_is_proxy_texture(target);
1207 GLboolean sizeOK = GL_TRUE;
1208 GLboolean colorFormat, indexFormat;
1209 GLenum proxy_target;
1210
1211 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1212 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1213 if (!isProxy) {
1214 _mesa_error(ctx, GL_INVALID_VALUE,
1215 "glTexImage%dD(level=%d)", dimensions, level);
1216 }
1217 return GL_TRUE;
1218 }
1219
1220 /* Check border */
1221 if (border < 0 || border > 1 ||
1222 ((target == GL_TEXTURE_RECTANGLE_NV ||
1223 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1224 if (!isProxy) {
1225 _mesa_error(ctx, GL_INVALID_VALUE,
1226 "glTexImage%dD(border=%d)", dimensions, border);
1227 }
1228 return GL_TRUE;
1229 }
1230
1231 if (width < 0 || height < 0 || depth < 0) {
1232 if (!isProxy) {
1233 _mesa_error(ctx, GL_INVALID_VALUE,
1234 "glTexImage%dD(width, height or depth < 0)", dimensions);
1235 }
1236 return GL_TRUE;
1237 }
1238
1239 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1240 * level, width, height and depth.
1241 */
1242 if (dimensions == 1) {
1243 if (target == GL_PROXY_TEXTURE_1D || target == GL_TEXTURE_1D) {
1244 proxy_target = GL_PROXY_TEXTURE_1D;
1245 height = 1;
1246 depth = 1;
1247 }
1248 else {
1249 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
1250 return GL_TRUE;
1251 }
1252 }
1253 else if (dimensions == 2) {
1254 depth = 1;
1255 if (target == GL_PROXY_TEXTURE_2D || target == GL_TEXTURE_2D) {
1256 proxy_target = GL_PROXY_TEXTURE_2D;
1257 }
1258 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB ||
1259 (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1260 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
1261 if (!ctx->Extensions.ARB_texture_cube_map) {
1262 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage2D(target)");
1263 return GL_TRUE;
1264 }
1265 proxy_target = GL_PROXY_TEXTURE_CUBE_MAP_ARB;
1266 sizeOK = (width == height);
1267 }
1268 else if (target == GL_PROXY_TEXTURE_RECTANGLE_NV ||
1269 target == GL_TEXTURE_RECTANGLE_NV) {
1270 if (!ctx->Extensions.NV_texture_rectangle) {
1271 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage2D(target)");
1272 return GL_TRUE;
1273 }
1274 proxy_target = GL_PROXY_TEXTURE_RECTANGLE_NV;
1275 }
1276 else if (target == GL_PROXY_TEXTURE_1D_ARRAY_EXT ||
1277 target == GL_TEXTURE_1D_ARRAY_EXT) {
1278 proxy_target = GL_PROXY_TEXTURE_1D_ARRAY_EXT;
1279 }
1280 else {
1281 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage2D(target)");
1282 return GL_TRUE;
1283 }
1284 }
1285 else if (dimensions == 3) {
1286 if (target == GL_PROXY_TEXTURE_3D || target == GL_TEXTURE_3D) {
1287 proxy_target = GL_PROXY_TEXTURE_3D;
1288 }
1289 else if (target == GL_PROXY_TEXTURE_2D_ARRAY_EXT ||
1290 target == GL_TEXTURE_2D_ARRAY_EXT) {
1291 proxy_target = GL_PROXY_TEXTURE_2D_ARRAY_EXT;
1292 }
1293 else {
1294 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
1295 return GL_TRUE;
1296 }
1297 }
1298 else {
1299 _mesa_problem( ctx, "bad dims in texture_error_check" );
1300 return GL_TRUE;
1301 }
1302
1303 sizeOK = sizeOK && ctx->Driver.TestProxyTexImage(ctx, proxy_target, level,
1304 internalFormat, format,
1305 type, width, height,
1306 depth, border);
1307 if (!sizeOK) {
1308 if (!isProxy) {
1309 _mesa_error(ctx, GL_INVALID_VALUE,
1310 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
1311 dimensions, level, width, height, depth);
1312 }
1313 return GL_TRUE;
1314 }
1315
1316 /* Check internalFormat */
1317 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
1318 if (!isProxy) {
1319 _mesa_error(ctx, GL_INVALID_VALUE,
1320 "glTexImage%dD(internalFormat=0x%x)",
1321 dimensions, internalFormat);
1322 }
1323 return GL_TRUE;
1324 }
1325
1326 /* Check incoming image format and type */
1327 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
1328 /* Yes, generate GL_INVALID_OPERATION, not GL_INVALID_ENUM, if there
1329 * is a type/format mismatch. See 1.2 spec page 94, sec 3.6.4.
1330 */
1331 if (!isProxy) {
1332 _mesa_error(ctx, GL_INVALID_OPERATION,
1333 "glTexImage%dD(incompatible format 0x%x, type 0x%x)",
1334 dimensions, format, type);
1335 }
1336 return GL_TRUE;
1337 }
1338
1339 /* make sure internal format and format basically agree */
1340 colorFormat = _mesa_is_color_format(format);
1341 indexFormat = _mesa_is_index_format(format);
1342 if ((_mesa_is_color_format(internalFormat) && !colorFormat && !indexFormat) ||
1343 (_mesa_is_index_format(internalFormat) && !indexFormat) ||
1344 (_mesa_is_depth_format(internalFormat) != _mesa_is_depth_format(format)) ||
1345 (_mesa_is_ycbcr_format(internalFormat) != _mesa_is_ycbcr_format(format)) ||
1346 (_mesa_is_depthstencil_format(internalFormat) != _mesa_is_depthstencil_format(format)) ||
1347 (_mesa_is_dudv_format(internalFormat) != _mesa_is_dudv_format(format))) {
1348 if (!isProxy)
1349 _mesa_error(ctx, GL_INVALID_OPERATION,
1350 "glTexImage%dD(incompatible internalFormat 0x%x, format 0x%x)",
1351 dimensions, internalFormat, format);
1352 return GL_TRUE;
1353 }
1354
1355 /* additional checks for ycbcr textures */
1356 if (internalFormat == GL_YCBCR_MESA) {
1357 ASSERT(ctx->Extensions.MESA_ycbcr_texture);
1358 if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
1359 type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
1360 char message[100];
1361 _mesa_sprintf(message,
1362 "glTexImage%d(format/type YCBCR mismatch", dimensions);
1363 _mesa_error(ctx, GL_INVALID_ENUM, message);
1364 return GL_TRUE; /* error */
1365 }
1366 if (target != GL_TEXTURE_2D &&
1367 target != GL_PROXY_TEXTURE_2D &&
1368 target != GL_TEXTURE_RECTANGLE_NV &&
1369 target != GL_PROXY_TEXTURE_RECTANGLE_NV) {
1370 if (!isProxy)
1371 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage(target)");
1372 return GL_TRUE;
1373 }
1374 if (border != 0) {
1375 if (!isProxy) {
1376 char message[100];
1377 _mesa_sprintf(message,
1378 "glTexImage%d(format=GL_YCBCR_MESA and border=%d)",
1379 dimensions, border);
1380 _mesa_error(ctx, GL_INVALID_VALUE, message);
1381 }
1382 return GL_TRUE;
1383 }
1384 }
1385
1386 /* additional checks for depth textures */
1387 if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT) {
1388 /* Only 1D, 2D and rectangular textures supported, not 3D or cubes */
1389 if (target != GL_TEXTURE_1D &&
1390 target != GL_PROXY_TEXTURE_1D &&
1391 target != GL_TEXTURE_2D &&
1392 target != GL_PROXY_TEXTURE_2D &&
1393 target != GL_TEXTURE_RECTANGLE_ARB &&
1394 target != GL_PROXY_TEXTURE_RECTANGLE_ARB) {
1395 if (!isProxy)
1396 _mesa_error(ctx, GL_INVALID_ENUM,
1397 "glTexImage(target/internalFormat)");
1398 return GL_TRUE;
1399 }
1400 }
1401
1402 /* additional checks for compressed textures */
1403 if (is_compressed_format(ctx, internalFormat)) {
1404 if (!target_can_be_compressed(ctx, target) && !isProxy) {
1405 _mesa_error(ctx, GL_INVALID_ENUM,
1406 "glTexImage%d(target)", dimensions);
1407 return GL_TRUE;
1408 }
1409 if (border != 0) {
1410 if (!isProxy) {
1411 _mesa_error(ctx, GL_INVALID_OPERATION,
1412 "glTexImage%D(border!=0)", dimensions);
1413 }
1414 return GL_TRUE;
1415 }
1416 }
1417
1418 /* if we get here, the parameters are OK */
1419 return GL_FALSE;
1420 }
1421
1422
1423 /**
1424 * Test glTexSubImage[123]D() parameters for errors.
1425 *
1426 * \param ctx GL context.
1427 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1428 * \param target texture target given by the user.
1429 * \param level image level given by the user.
1430 * \param xoffset sub-image x offset given by the user.
1431 * \param yoffset sub-image y offset given by the user.
1432 * \param zoffset sub-image z offset given by the user.
1433 * \param format pixel data format given by the user.
1434 * \param type pixel data type given by the user.
1435 * \param width image width given by the user.
1436 * \param height image height given by the user.
1437 * \param depth image depth given by the user.
1438 *
1439 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1440 *
1441 * Verifies each of the parameters against the constants specified in
1442 * __GLcontextRec::Const and the supported extensions, and according to the
1443 * OpenGL specification.
1444 */
1445 static GLboolean
1446 subtexture_error_check( GLcontext *ctx, GLuint dimensions,
1447 GLenum target, GLint level,
1448 GLint xoffset, GLint yoffset, GLint zoffset,
1449 GLint width, GLint height, GLint depth,
1450 GLenum format, GLenum type )
1451 {
1452 /* Check target */
1453 if (dimensions == 1) {
1454 if (target != GL_TEXTURE_1D) {
1455 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage1D(target)" );
1456 return GL_TRUE;
1457 }
1458 }
1459 else if (dimensions == 2) {
1460 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1461 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
1462 if (!ctx->Extensions.ARB_texture_cube_map) {
1463 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1464 return GL_TRUE;
1465 }
1466 }
1467 else if (target == GL_TEXTURE_RECTANGLE_NV) {
1468 if (!ctx->Extensions.NV_texture_rectangle) {
1469 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1470 return GL_TRUE;
1471 }
1472 }
1473 else if (target == GL_TEXTURE_1D_ARRAY_EXT) {
1474 if (!ctx->Extensions.MESA_texture_array) {
1475 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1476 return GL_TRUE;
1477 }
1478 }
1479 else if (target != GL_TEXTURE_2D) {
1480 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1481 return GL_TRUE;
1482 }
1483 }
1484 else if (dimensions == 3) {
1485 if (target == GL_TEXTURE_2D_ARRAY_EXT) {
1486 if (!ctx->Extensions.MESA_texture_array) {
1487 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage3D(target)" );
1488 return GL_TRUE;
1489 }
1490 }
1491 else if (target != GL_TEXTURE_3D) {
1492 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage3D(target)" );
1493 return GL_TRUE;
1494 }
1495 }
1496 else {
1497 _mesa_problem( ctx, "invalid dims in texture_error_check" );
1498 return GL_TRUE;
1499 }
1500
1501 /* Basic level check */
1502 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1503 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage2D(level=%d)", level);
1504 return GL_TRUE;
1505 }
1506
1507 if (width < 0) {
1508 _mesa_error(ctx, GL_INVALID_VALUE,
1509 "glTexSubImage%dD(width=%d)", dimensions, width);
1510 return GL_TRUE;
1511 }
1512 if (height < 0 && dimensions > 1) {
1513 _mesa_error(ctx, GL_INVALID_VALUE,
1514 "glTexSubImage%dD(height=%d)", dimensions, height);
1515 return GL_TRUE;
1516 }
1517 if (depth < 0 && dimensions > 2) {
1518 _mesa_error(ctx, GL_INVALID_VALUE,
1519 "glTexSubImage%dD(depth=%d)", dimensions, depth);
1520 return GL_TRUE;
1521 }
1522
1523 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
1524 _mesa_error(ctx, GL_INVALID_ENUM,
1525 "glTexSubImage%dD(incompatible format 0x%x, type 0x%x)",
1526 dimensions, format, type);
1527 return GL_TRUE;
1528 }
1529
1530 return GL_FALSE;
1531 }
1532
1533 static GLboolean
1534 subtexture_error_check2( GLcontext *ctx, GLuint dimensions,
1535 GLenum target, GLint level,
1536 GLint xoffset, GLint yoffset, GLint zoffset,
1537 GLint width, GLint height, GLint depth,
1538 GLenum format, GLenum type,
1539 const struct gl_texture_image *destTex )
1540 {
1541 if (!destTex) {
1542 /* undefined image level */
1543 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexSubImage%dD", dimensions);
1544 return GL_TRUE;
1545 }
1546
1547 if (xoffset < -((GLint)destTex->Border)) {
1548 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset)",
1549 dimensions);
1550 return GL_TRUE;
1551 }
1552 if (xoffset + width > (GLint) (destTex->Width + destTex->Border)) {
1553 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset+width)",
1554 dimensions);
1555 return GL_TRUE;
1556 }
1557 if (dimensions > 1) {
1558 if (yoffset < -((GLint)destTex->Border)) {
1559 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset)",
1560 dimensions);
1561 return GL_TRUE;
1562 }
1563 if (yoffset + height > (GLint) (destTex->Height + destTex->Border)) {
1564 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset+height)",
1565 dimensions);
1566 return GL_TRUE;
1567 }
1568 }
1569 if (dimensions > 2) {
1570 if (zoffset < -((GLint)destTex->Border)) {
1571 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset)");
1572 return GL_TRUE;
1573 }
1574 if (zoffset + depth > (GLint) (destTex->Depth + destTex->Border)) {
1575 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset+depth)");
1576 return GL_TRUE;
1577 }
1578 }
1579
1580 #if FEATURE_EXT_texture_sRGB
1581 if (destTex->InternalFormat == GL_COMPRESSED_SRGB_S3TC_DXT1_EXT ||
1582 destTex->InternalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT ||
1583 destTex->InternalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT ||
1584 destTex->InternalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT) {
1585 if ((width & 0x3) || (height & 0x3) ||
1586 (xoffset & 0x3) || (yoffset & 0x3))
1587 _mesa_error(ctx, GL_INVALID_OPERATION,
1588 "glTexSubImage%dD(size or offset not multiple of 4)",
1589 dimensions);
1590 return GL_TRUE;
1591 }
1592 #endif
1593
1594 if (destTex->IsCompressed) {
1595 if (!target_can_be_compressed(ctx, target)) {
1596 _mesa_error(ctx, GL_INVALID_ENUM,
1597 "glTexSubImage%D(target)", dimensions);
1598 return GL_TRUE;
1599 }
1600 /* offset must be multiple of 4 */
1601 if ((xoffset & 3) || (yoffset & 3)) {
1602 _mesa_error(ctx, GL_INVALID_OPERATION,
1603 "glTexSubImage%D(xoffset or yoffset)", dimensions);
1604 return GL_TRUE;
1605 }
1606 /* size must be multiple of 4 or equal to whole texture size */
1607 if ((width & 3) && (GLuint) width != destTex->Width) {
1608 _mesa_error(ctx, GL_INVALID_OPERATION,
1609 "glTexSubImage%D(width)", dimensions);
1610 return GL_TRUE;
1611 }
1612 if ((height & 3) && (GLuint) height != destTex->Height) {
1613 _mesa_error(ctx, GL_INVALID_OPERATION,
1614 "glTexSubImage%D(width)", dimensions);
1615 return GL_TRUE;
1616 }
1617 }
1618
1619 return GL_FALSE;
1620 }
1621
1622
1623 /**
1624 * Test glCopyTexImage[12]D() parameters for errors.
1625 *
1626 * \param ctx GL context.
1627 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1628 * \param target texture target given by the user.
1629 * \param level image level given by the user.
1630 * \param internalFormat internal format given by the user.
1631 * \param width image width given by the user.
1632 * \param height image height given by the user.
1633 * \param border texture border.
1634 *
1635 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1636 *
1637 * Verifies each of the parameters against the constants specified in
1638 * __GLcontextRec::Const and the supported extensions, and according to the
1639 * OpenGL specification.
1640 */
1641 static GLboolean
1642 copytexture_error_check( GLcontext *ctx, GLuint dimensions,
1643 GLenum target, GLint level, GLint internalFormat,
1644 GLint width, GLint height, GLint border )
1645 {
1646 GLenum type;
1647 GLboolean sizeOK;
1648 GLint format;
1649
1650 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1651 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1652 _mesa_error(ctx, GL_INVALID_VALUE,
1653 "glCopyTexImage%dD(level=%d)", dimensions, level);
1654 return GL_TRUE;
1655 }
1656
1657 /* Check that the source buffer is complete */
1658 if (ctx->ReadBuffer->Name) {
1659 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
1660 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
1661 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
1662 "glCopyTexImage%dD(invalid readbuffer)", dimensions);
1663 return GL_TRUE;
1664 }
1665 }
1666
1667 /* Check border */
1668 if (border < 0 || border > 1 ||
1669 ((target == GL_TEXTURE_RECTANGLE_NV ||
1670 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1671 return GL_TRUE;
1672 }
1673
1674 format = _mesa_base_tex_format(ctx, internalFormat);
1675 if (format < 0) {
1676 _mesa_error(ctx, GL_INVALID_VALUE,
1677 "glCopyTexImage%dD(internalFormat)", dimensions);
1678 return GL_TRUE;
1679 }
1680
1681 if (!_mesa_source_buffer_exists(ctx, format)) {
1682 _mesa_error(ctx, GL_INVALID_OPERATION,
1683 "glCopyTexImage%dD(missing readbuffer)", dimensions);
1684 return GL_TRUE;
1685 }
1686
1687 /* NOTE: the format and type aren't really significant for
1688 * TestProxyTexImage(). Only the internalformat really matters.
1689 */
1690 type = GL_FLOAT;
1691
1692 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1693 * level, width, height and depth.
1694 */
1695 if (dimensions == 1) {
1696 if (target == GL_TEXTURE_1D) {
1697 sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_1D,
1698 level, internalFormat,
1699 format, type,
1700 width, 1, 1, border);
1701 }
1702 else {
1703 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage1D(target)" );
1704 return GL_TRUE;
1705 }
1706 }
1707 else if (dimensions == 2) {
1708 if (target == GL_TEXTURE_2D) {
1709 sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_2D,
1710 level, internalFormat,
1711 format, type,
1712 width, height, 1, border);
1713 }
1714 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1715 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
1716 if (!ctx->Extensions.ARB_texture_cube_map) {
1717 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
1718 return GL_TRUE;
1719 }
1720 sizeOK = (width == height) &&
1721 ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_CUBE_MAP_ARB,
1722 level, internalFormat, format, type,
1723 width, height, 1, border);
1724 }
1725 else if (target == GL_TEXTURE_RECTANGLE_NV) {
1726 if (!ctx->Extensions.NV_texture_rectangle) {
1727 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
1728 return GL_TRUE;
1729 }
1730 sizeOK = ctx->Driver.TestProxyTexImage(ctx,
1731 GL_PROXY_TEXTURE_RECTANGLE_NV,
1732 level, internalFormat,
1733 format, type,
1734 width, height, 1, border);
1735 }
1736 else if (target == GL_TEXTURE_1D_ARRAY_EXT) {
1737 if (!ctx->Extensions.MESA_texture_array) {
1738 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)");
1739 return GL_TRUE;
1740 }
1741 sizeOK = ctx->Driver.TestProxyTexImage(ctx,
1742 GL_PROXY_TEXTURE_1D_ARRAY_EXT,
1743 level, internalFormat,
1744 format, type,
1745 width, height, 1, border);
1746 }
1747 else {
1748 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
1749 return GL_TRUE;
1750 }
1751 }
1752 else {
1753 _mesa_problem(ctx, "invalid dimensions in copytexture_error_check");
1754 return GL_TRUE;
1755 }
1756
1757 if (!sizeOK) {
1758 if (dimensions == 1) {
1759 _mesa_error(ctx, GL_INVALID_VALUE,
1760 "glCopyTexImage1D(width=%d)", width);
1761 }
1762 else {
1763 ASSERT(dimensions == 2);
1764 _mesa_error(ctx, GL_INVALID_VALUE,
1765 "glCopyTexImage2D(width=%d, height=%d)", width, height);
1766 }
1767 return GL_TRUE;
1768 }
1769
1770 if (is_compressed_format(ctx, internalFormat)) {
1771 if (!target_can_be_compressed(ctx, target)) {
1772 _mesa_error(ctx, GL_INVALID_ENUM,
1773 "glCopyTexImage%d(target)", dimensions);
1774 return GL_TRUE;
1775 }
1776 if (border != 0) {
1777 _mesa_error(ctx, GL_INVALID_OPERATION,
1778 "glCopyTexImage%D(border!=0)", dimensions);
1779 return GL_TRUE;
1780 }
1781 }
1782 else if (_mesa_is_depth_format(internalFormat)) {
1783 /* make sure we have depth/stencil buffers */
1784 if (!ctx->ReadBuffer->_DepthBuffer) {
1785 _mesa_error(ctx, GL_INVALID_OPERATION,
1786 "glCopyTexImage%D(no depth)", dimensions);
1787 return GL_TRUE;
1788 }
1789 }
1790 else if (_mesa_is_depthstencil_format(internalFormat)) {
1791 /* make sure we have depth/stencil buffers */
1792 if (!ctx->ReadBuffer->_DepthBuffer || !ctx->ReadBuffer->_StencilBuffer) {
1793 _mesa_error(ctx, GL_INVALID_OPERATION,
1794 "glCopyTexImage%D(no depth/stencil buffer)", dimensions);
1795 return GL_TRUE;
1796 }
1797 }
1798
1799 /* if we get here, the parameters are OK */
1800 return GL_FALSE;
1801 }
1802
1803
1804 /**
1805 * Test glCopyTexSubImage[12]D() parameters for errors.
1806 * Note that this is the first part of error checking.
1807 * See also copytexsubimage_error_check2() below for the second part.
1808 *
1809 * \param ctx GL context.
1810 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1811 * \param target texture target given by the user.
1812 * \param level image level given by the user.
1813 *
1814 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1815 */
1816 static GLboolean
1817 copytexsubimage_error_check1( GLcontext *ctx, GLuint dimensions,
1818 GLenum target, GLint level)
1819 {
1820 /* Check that the source buffer is complete */
1821 if (ctx->ReadBuffer->Name) {
1822 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
1823 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
1824 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
1825 "glCopyTexImage%dD(invalid readbuffer)", dimensions);
1826 return GL_TRUE;
1827 }
1828 }
1829
1830 /* Check target */
1831 if (dimensions == 1) {
1832 if (target != GL_TEXTURE_1D) {
1833 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage1D(target)" );
1834 return GL_TRUE;
1835 }
1836 }
1837 else if (dimensions == 2) {
1838 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1839 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
1840 if (!ctx->Extensions.ARB_texture_cube_map) {
1841 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1842 return GL_TRUE;
1843 }
1844 }
1845 else if (target == GL_TEXTURE_RECTANGLE_NV) {
1846 if (!ctx->Extensions.NV_texture_rectangle) {
1847 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1848 return GL_TRUE;
1849 }
1850 }
1851 else if (target == GL_TEXTURE_1D_ARRAY_EXT) {
1852 if (!ctx->Extensions.MESA_texture_array) {
1853 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1854 return GL_TRUE;
1855 }
1856 }
1857 else if (target != GL_TEXTURE_2D) {
1858 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
1859 return GL_TRUE;
1860 }
1861 }
1862 else if (dimensions == 3) {
1863 if (((target != GL_TEXTURE_2D_ARRAY_EXT) ||
1864 (!ctx->Extensions.MESA_texture_array))
1865 && (target != GL_TEXTURE_3D)) {
1866 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage3D(target)" );
1867 return GL_TRUE;
1868 }
1869 }
1870
1871 /* Check level */
1872 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1873 _mesa_error(ctx, GL_INVALID_VALUE,
1874 "glCopyTexSubImage%dD(level=%d)", dimensions, level);
1875 return GL_TRUE;
1876 }
1877
1878 return GL_FALSE;
1879 }
1880
1881
1882 /**
1883 * Second part of error checking for glCopyTexSubImage[12]D().
1884 * \param xoffset sub-image x offset given by the user.
1885 * \param yoffset sub-image y offset given by the user.
1886 * \param zoffset sub-image z offset given by the user.
1887 * \param width image width given by the user.
1888 * \param height image height given by the user.
1889 */
1890 static GLboolean
1891 copytexsubimage_error_check2( GLcontext *ctx, GLuint dimensions,
1892 GLenum target, GLint level,
1893 GLint xoffset, GLint yoffset, GLint zoffset,
1894 GLsizei width, GLsizei height,
1895 const struct gl_texture_image *teximage )
1896 {
1897 /* check that dest tex image exists */
1898 if (!teximage) {
1899 _mesa_error(ctx, GL_INVALID_OPERATION,
1900 "glCopyTexSubImage%dD(undefined texture level: %d)",
1901 dimensions, level);
1902 return GL_TRUE;
1903 }
1904
1905 /* Check size */
1906 if (width < 0) {
1907 _mesa_error(ctx, GL_INVALID_VALUE,
1908 "glCopyTexSubImage%dD(width=%d)", dimensions, width);
1909 return GL_TRUE;
1910 }
1911 if (dimensions > 1 && height < 0) {
1912 _mesa_error(ctx, GL_INVALID_VALUE,
1913 "glCopyTexSubImage%dD(height=%d)", dimensions, height);
1914 return GL_TRUE;
1915 }
1916
1917 /* check x/y offsets */
1918 if (xoffset < -((GLint)teximage->Border)) {
1919 _mesa_error(ctx, GL_INVALID_VALUE,
1920 "glCopyTexSubImage%dD(xoffset=%d)", dimensions, xoffset);
1921 return GL_TRUE;
1922 }
1923 if (xoffset + width > (GLint) (teximage->Width + teximage->Border)) {
1924 _mesa_error(ctx, GL_INVALID_VALUE,
1925 "glCopyTexSubImage%dD(xoffset+width)", dimensions);
1926 return GL_TRUE;
1927 }
1928 if (dimensions > 1) {
1929 if (yoffset < -((GLint)teximage->Border)) {
1930 _mesa_error(ctx, GL_INVALID_VALUE,
1931 "glCopyTexSubImage%dD(yoffset=%d)", dimensions, yoffset);
1932 return GL_TRUE;
1933 }
1934 /* NOTE: we're adding the border here, not subtracting! */
1935 if (yoffset + height > (GLint) (teximage->Height + teximage->Border)) {
1936 _mesa_error(ctx, GL_INVALID_VALUE,
1937 "glCopyTexSubImage%dD(yoffset+height)", dimensions);
1938 return GL_TRUE;
1939 }
1940 }
1941
1942 /* check z offset */
1943 if (dimensions > 2) {
1944 if (zoffset < -((GLint)teximage->Border)) {
1945 _mesa_error(ctx, GL_INVALID_VALUE,
1946 "glCopyTexSubImage%dD(zoffset)", dimensions);
1947 return GL_TRUE;
1948 }
1949 if (zoffset > (GLint) (teximage->Depth + teximage->Border)) {
1950 _mesa_error(ctx, GL_INVALID_VALUE,
1951 "glCopyTexSubImage%dD(zoffset+depth)", dimensions);
1952 return GL_TRUE;
1953 }
1954 }
1955
1956 if (teximage->IsCompressed) {
1957 if (!target_can_be_compressed(ctx, target)) {
1958 _mesa_error(ctx, GL_INVALID_ENUM,
1959 "glCopyTexSubImage%d(target)", dimensions);
1960 return GL_TRUE;
1961 }
1962 /* offset must be multiple of 4 */
1963 if ((xoffset & 3) || (yoffset & 3)) {
1964 _mesa_error(ctx, GL_INVALID_VALUE,
1965 "glCopyTexSubImage%D(xoffset or yoffset)", dimensions);
1966 return GL_TRUE;
1967 }
1968 /* size must be multiple of 4 */
1969 if ((width & 3) != 0 && (GLuint) width != teximage->Width) {
1970 _mesa_error(ctx, GL_INVALID_VALUE,
1971 "glCopyTexSubImage%D(width)", dimensions);
1972 return GL_TRUE;
1973 }
1974 if ((height & 3) != 0 && (GLuint) height != teximage->Height) {
1975 _mesa_error(ctx, GL_INVALID_VALUE,
1976 "glCopyTexSubImage%D(height)", dimensions);
1977 return GL_TRUE;
1978 }
1979 }
1980
1981 if (teximage->InternalFormat == GL_YCBCR_MESA) {
1982 _mesa_error(ctx, GL_INVALID_OPERATION, "glCopyTexSubImage2D");
1983 return GL_TRUE;
1984 }
1985
1986 if (!_mesa_source_buffer_exists(ctx, teximage->_BaseFormat)) {
1987 _mesa_error(ctx, GL_INVALID_OPERATION,
1988 "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
1989 dimensions, teximage->_BaseFormat);
1990 return GL_TRUE;
1991 }
1992
1993 if (teximage->_BaseFormat == GL_DEPTH_COMPONENT) {
1994 if (!ctx->ReadBuffer->_DepthBuffer) {
1995 _mesa_error(ctx, GL_INVALID_OPERATION,
1996 "glCopyTexSubImage%D(no depth buffer)",
1997 dimensions);
1998 return GL_TRUE;
1999 }
2000 }
2001 else if (teximage->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
2002 if (!ctx->ReadBuffer->_DepthBuffer || !ctx->ReadBuffer->_StencilBuffer) {
2003 _mesa_error(ctx, GL_INVALID_OPERATION,
2004 "glCopyTexSubImage%D(no depth/stencil buffer)",
2005 dimensions);
2006 return GL_TRUE;
2007 }
2008 }
2009
2010 /* if we get here, the parameters are OK */
2011 return GL_FALSE;
2012 }
2013
2014
2015 /** Callback info for walking over FBO hash table */
2016 struct cb_info
2017 {
2018 GLcontext *ctx;
2019 struct gl_texture_object *texObj;
2020 GLuint level, face;
2021 };
2022
2023
2024 /**
2025 * Check render to texture callback. Called from _mesa_HashWalk().
2026 */
2027 static void
2028 check_rtt_cb(GLuint key, void *data, void *userData)
2029 {
2030 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
2031 const struct cb_info *info = (struct cb_info *) userData;
2032 GLcontext *ctx = info->ctx;
2033 const struct gl_texture_object *texObj = info->texObj;
2034 const GLuint level = info->level, face = info->face;
2035
2036 /* If this is a user-created FBO */
2037 if (fb->Name) {
2038 GLuint i;
2039 /* check if any of the FBO's attachments point to 'texObj' */
2040 for (i = 0; i < BUFFER_COUNT; i++) {
2041 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2042 if (att->Type == GL_TEXTURE &&
2043 att->Texture == texObj &&
2044 att->TextureLevel == level &&
2045 att->CubeMapFace == face) {
2046 ASSERT(att->Texture->Image[att->CubeMapFace][att->TextureLevel]);
2047 /* Tell driver about the new renderbuffer texture */
2048 ctx->Driver.RenderTexture(ctx, ctx->DrawBuffer, att);
2049 /* Mark fb status as indeterminate to force re-validation */
2050 fb->_Status = 0;
2051 }
2052 }
2053 }
2054 }
2055
2056
2057 /**
2058 * When a texture image is specified we have to check if it's bound to
2059 * any framebuffer objects (render to texture) in order to detect changes
2060 * in size or format since that effects FBO completeness.
2061 * Any FBOs rendering into the texture must be re-validated.
2062 */
2063 static void
2064 update_fbo_texture(GLcontext *ctx, struct gl_texture_object *texObj,
2065 GLuint face, GLuint level)
2066 {
2067 /* Only check this texture if it's been marked as RenderToTexture */
2068 if (texObj->_RenderToTexture) {
2069 struct cb_info info;
2070 info.ctx = ctx;
2071 info.texObj = texObj;
2072 info.level = level;
2073 info.face = face;
2074 _mesa_HashWalk(ctx->Shared->FrameBuffers, check_rtt_cb, &info);
2075 }
2076 }
2077
2078
2079 /** Debug helper: override the user-requested internal format */
2080 static GLenum
2081 override_internal_format(GLenum internalFormat, GLint width, GLint height)
2082 {
2083 #if 0
2084 if (internalFormat == GL_RGBA16F_ARB ||
2085 internalFormat == GL_RGBA32F_ARB) {
2086 printf("Convert rgba float tex to int %d x %d\n", width, height);
2087 return GL_RGBA;
2088 }
2089 else if (internalFormat == GL_RGB16F_ARB ||
2090 internalFormat == GL_RGB32F_ARB) {
2091 printf("Convert rgb float tex to int %d x %d\n", width, height);
2092 return GL_RGB;
2093 }
2094 else if (internalFormat == GL_LUMINANCE_ALPHA16F_ARB ||
2095 internalFormat == GL_LUMINANCE_ALPHA32F_ARB) {
2096 printf("Convert luminance float tex to int %d x %d\n", width, height);
2097 return GL_LUMINANCE_ALPHA;
2098 }
2099 else if (internalFormat == GL_LUMINANCE16F_ARB ||
2100 internalFormat == GL_LUMINANCE32F_ARB) {
2101 printf("Convert luminance float tex to int %d x %d\n", width, height);
2102 return GL_LUMINANCE;
2103 }
2104 else if (internalFormat == GL_ALPHA16F_ARB ||
2105 internalFormat == GL_ALPHA32F_ARB) {
2106 printf("Convert luminance float tex to int %d x %d\n", width, height);
2107 return GL_ALPHA;
2108 }
2109 /*
2110 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2111 internalFormat = GL_RGBA;
2112 }
2113 */
2114 else {
2115 return internalFormat;
2116 }
2117 #else
2118 return internalFormat;
2119 #endif
2120 }
2121
2122
2123 /*
2124 * Called from the API. Note that width includes the border.
2125 */
2126 void GLAPIENTRY
2127 _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
2128 GLsizei width, GLint border, GLenum format,
2129 GLenum type, const GLvoid *pixels )
2130 {
2131 GLsizei postConvWidth = width;
2132 GET_CURRENT_CONTEXT(ctx);
2133 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2134
2135 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2136 _mesa_debug(ctx, "glTexImage1D %s %d %s %d %d %s %s %p\n",
2137 _mesa_lookup_enum_by_nr(target), level,
2138 _mesa_lookup_enum_by_nr(internalFormat), width, border,
2139 _mesa_lookup_enum_by_nr(format),
2140 _mesa_lookup_enum_by_nr(type), pixels);
2141
2142 internalFormat = override_internal_format(internalFormat, width, 1);
2143
2144 #if FEATURE_convolve
2145 if (_mesa_is_color_format(internalFormat)) {
2146 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2147 }
2148 #endif
2149
2150 if (target == GL_TEXTURE_1D) {
2151 /* non-proxy target */
2152 struct gl_texture_unit *texUnit;
2153 struct gl_texture_object *texObj;
2154 struct gl_texture_image *texImage;
2155 const GLuint face = _mesa_tex_target_to_face(target);
2156
2157 if (texture_error_check(ctx, target, level, internalFormat,
2158 format, type, 1, postConvWidth, 1, 1, border)) {
2159 return; /* error was recorded */
2160 }
2161
2162 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2163 _mesa_update_state(ctx);
2164
2165 texUnit = _mesa_get_current_tex_unit(ctx);
2166 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2167 _mesa_lock_texture(ctx, texObj);
2168 {
2169 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2170 if (!texImage) {
2171 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage1D");
2172 goto out;
2173 }
2174
2175 if (texImage->Data) {
2176 ctx->Driver.FreeTexImageData( ctx, texImage );
2177 }
2178
2179 ASSERT(texImage->Data == NULL);
2180
2181 clear_teximage_fields(texImage); /* not really needed, but helpful */
2182 _mesa_init_teximage_fields(ctx, target, texImage,
2183 postConvWidth, 1, 1,
2184 border, internalFormat);
2185
2186 ASSERT(ctx->Driver.TexImage1D);
2187
2188 /* Give the texture to the driver! <pixels> may be null! */
2189 (*ctx->Driver.TexImage1D)(ctx, target, level, internalFormat,
2190 width, border, format, type, pixels,
2191 &ctx->Unpack, texObj, texImage);
2192
2193 ASSERT(texImage->TexFormat);
2194
2195 update_fbo_texture(ctx, texObj, face, level);
2196
2197 /* state update */
2198 texObj->_Complete = GL_FALSE;
2199 ctx->NewState |= _NEW_TEXTURE;
2200 }
2201 out:
2202 _mesa_unlock_texture(ctx, texObj);
2203 }
2204 else if (target == GL_PROXY_TEXTURE_1D) {
2205 /* Proxy texture: check for errors and update proxy state */
2206 struct gl_texture_image *texImage;
2207 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2208 if (texture_error_check(ctx, target, level, internalFormat,
2209 format, type, 1, postConvWidth, 1, 1, border)) {
2210 /* when error, clear all proxy texture image parameters */
2211 if (texImage)
2212 clear_teximage_fields(texImage);
2213 }
2214 else {
2215 /* no error, set the tex image parameters */
2216 ASSERT(texImage);
2217 _mesa_init_teximage_fields(ctx, target, texImage,
2218 postConvWidth, 1, 1,
2219 border, internalFormat);
2220 texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2221 internalFormat, format, type);
2222 }
2223 }
2224 else {
2225 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
2226 return;
2227 }
2228 }
2229
2230
2231 void GLAPIENTRY
2232 _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
2233 GLsizei width, GLsizei height, GLint border,
2234 GLenum format, GLenum type,
2235 const GLvoid *pixels )
2236 {
2237 GLsizei postConvWidth = width, postConvHeight = height;
2238 GET_CURRENT_CONTEXT(ctx);
2239 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2240
2241 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2242 _mesa_debug(ctx, "glTexImage2D %s %d %s %d %d %d %s %s %p\n",
2243 _mesa_lookup_enum_by_nr(target), level,
2244 _mesa_lookup_enum_by_nr(internalFormat), width, height,
2245 border, _mesa_lookup_enum_by_nr(format),
2246 _mesa_lookup_enum_by_nr(type), pixels);
2247
2248 internalFormat = override_internal_format(internalFormat, width, height);
2249
2250 #if FEATURE_convolve
2251 if (_mesa_is_color_format(internalFormat)) {
2252 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2253 &postConvHeight);
2254 }
2255 #endif
2256
2257 if (target == GL_TEXTURE_2D ||
2258 (ctx->Extensions.ARB_texture_cube_map &&
2259 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2260 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) ||
2261 (ctx->Extensions.NV_texture_rectangle &&
2262 target == GL_TEXTURE_RECTANGLE_NV) ||
2263 (ctx->Extensions.MESA_texture_array &&
2264 target == GL_TEXTURE_1D_ARRAY_EXT)) {
2265 /* non-proxy target */
2266 struct gl_texture_unit *texUnit;
2267 struct gl_texture_object *texObj;
2268 struct gl_texture_image *texImage;
2269 const GLuint face = _mesa_tex_target_to_face(target);
2270
2271 if (texture_error_check(ctx, target, level, internalFormat,
2272 format, type, 2, postConvWidth, postConvHeight,
2273 1, border)) {
2274 return; /* error was recorded */
2275 }
2276
2277 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2278 _mesa_update_state(ctx);
2279
2280 texUnit = _mesa_get_current_tex_unit(ctx);
2281 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2282 _mesa_lock_texture(ctx, texObj);
2283 {
2284 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2285 if (!texImage) {
2286 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage2D");
2287 goto out;
2288 }
2289
2290 if (texImage->Data) {
2291 ctx->Driver.FreeTexImageData( ctx, texImage );
2292 }
2293
2294 ASSERT(texImage->Data == NULL);
2295 clear_teximage_fields(texImage); /* not really needed, but helpful */
2296 _mesa_init_teximage_fields(ctx, target, texImage,
2297 postConvWidth, postConvHeight, 1,
2298 border, internalFormat);
2299
2300 ASSERT(ctx->Driver.TexImage2D);
2301
2302 /* Give the texture to the driver! <pixels> may be null! */
2303 (*ctx->Driver.TexImage2D)(ctx, target, level, internalFormat,
2304 width, height, border, format, type, pixels,
2305 &ctx->Unpack, texObj, texImage);
2306
2307 ASSERT(texImage->TexFormat);
2308
2309 update_fbo_texture(ctx, texObj, face, level);
2310
2311 /* state update */
2312 texObj->_Complete = GL_FALSE;
2313 ctx->NewState |= _NEW_TEXTURE;
2314 }
2315 out:
2316 _mesa_unlock_texture(ctx, texObj);
2317 }
2318 else if (target == GL_PROXY_TEXTURE_2D ||
2319 (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB &&
2320 ctx->Extensions.ARB_texture_cube_map) ||
2321 (target == GL_PROXY_TEXTURE_RECTANGLE_NV &&
2322 ctx->Extensions.NV_texture_rectangle) ||
2323 (ctx->Extensions.MESA_texture_array &&
2324 target == GL_PROXY_TEXTURE_1D_ARRAY_EXT)) {
2325 /* Proxy texture: check for errors and update proxy state */
2326 struct gl_texture_image *texImage;
2327 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2328 if (texture_error_check(ctx, target, level, internalFormat,
2329 format, type, 2, postConvWidth, postConvHeight,
2330 1, border)) {
2331 /* when error, clear all proxy texture image parameters */
2332 if (texImage)
2333 clear_teximage_fields(texImage);
2334 }
2335 else {
2336 /* no error, set the tex image parameters */
2337 _mesa_init_teximage_fields(ctx, target, texImage,
2338 postConvWidth, postConvHeight, 1,
2339 border, internalFormat);
2340 texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2341 internalFormat, format, type);
2342 }
2343 }
2344 else {
2345 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage2D(target)" );
2346 return;
2347 }
2348 }
2349
2350
2351 /*
2352 * Called by the API or display list executor.
2353 * Note that width and height include the border.
2354 */
2355 void GLAPIENTRY
2356 _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
2357 GLsizei width, GLsizei height, GLsizei depth,
2358 GLint border, GLenum format, GLenum type,
2359 const GLvoid *pixels )
2360 {
2361 GET_CURRENT_CONTEXT(ctx);
2362 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2363
2364 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2365 _mesa_debug(ctx, "glTexImage3D %s %d %s %d %d %d %d %s %s %p\n",
2366 _mesa_lookup_enum_by_nr(target), level,
2367 _mesa_lookup_enum_by_nr(internalFormat), width, height,
2368 depth, border, _mesa_lookup_enum_by_nr(format),
2369 _mesa_lookup_enum_by_nr(type), pixels);
2370
2371 internalFormat = override_internal_format(internalFormat, width, height);
2372
2373 if (target == GL_TEXTURE_3D ||
2374 (ctx->Extensions.MESA_texture_array &&
2375 target == GL_TEXTURE_2D_ARRAY_EXT)) {
2376 /* non-proxy target */
2377 struct gl_texture_unit *texUnit;
2378 struct gl_texture_object *texObj;
2379 struct gl_texture_image *texImage;
2380 const GLuint face = _mesa_tex_target_to_face(target);
2381
2382 if (texture_error_check(ctx, target, level, (GLint) internalFormat,
2383 format, type, 3, width, height, depth, border)) {
2384 return; /* error was recorded */
2385 }
2386
2387 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2388 _mesa_update_state(ctx);
2389
2390 texUnit = _mesa_get_current_tex_unit(ctx);
2391 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2392 _mesa_lock_texture(ctx, texObj);
2393 {
2394 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2395 if (!texImage) {
2396 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage3D");
2397 goto out;
2398 }
2399
2400 if (texImage->Data) {
2401 ctx->Driver.FreeTexImageData( ctx, texImage );
2402 }
2403
2404 ASSERT(texImage->Data == NULL);
2405 clear_teximage_fields(texImage); /* not really needed, but helpful */
2406 _mesa_init_teximage_fields(ctx, target, texImage,
2407 width, height, depth,
2408 border, internalFormat);
2409
2410 ASSERT(ctx->Driver.TexImage3D);
2411
2412 /* Give the texture to the driver! <pixels> may be null! */
2413 (*ctx->Driver.TexImage3D)(ctx, target, level, internalFormat,
2414 width, height, depth, border, format, type,
2415 pixels, &ctx->Unpack, texObj, texImage);
2416
2417 ASSERT(texImage->TexFormat);
2418
2419 update_fbo_texture(ctx, texObj, face, level);
2420
2421 /* state update */
2422 texObj->_Complete = GL_FALSE;
2423 ctx->NewState |= _NEW_TEXTURE;
2424 }
2425 out:
2426 _mesa_unlock_texture(ctx, texObj);
2427 }
2428 else if (target == GL_PROXY_TEXTURE_3D ||
2429 (ctx->Extensions.MESA_texture_array &&
2430 target == GL_PROXY_TEXTURE_2D_ARRAY_EXT)) {
2431 /* Proxy texture: check for errors and update proxy state */
2432 struct gl_texture_image *texImage;
2433 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2434 if (texture_error_check(ctx, target, level, internalFormat,
2435 format, type, 3, width, height, depth, border)) {
2436 /* when error, clear all proxy texture image parameters */
2437 if (texImage)
2438 clear_teximage_fields(texImage);
2439 }
2440 else {
2441 /* no error, set the tex image parameters */
2442 _mesa_init_teximage_fields(ctx, target, texImage, width, height,
2443 depth, border, internalFormat);
2444 texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2445 internalFormat, format, type);
2446 }
2447 }
2448 else {
2449 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
2450 return;
2451 }
2452 }
2453
2454
2455 void GLAPIENTRY
2456 _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
2457 GLsizei width, GLsizei height, GLsizei depth,
2458 GLint border, GLenum format, GLenum type,
2459 const GLvoid *pixels )
2460 {
2461 _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
2462 depth, border, format, type, pixels);
2463 }
2464
2465
2466
2467 void GLAPIENTRY
2468 _mesa_TexSubImage1D( GLenum target, GLint level,
2469 GLint xoffset, GLsizei width,
2470 GLenum format, GLenum type,
2471 const GLvoid *pixels )
2472 {
2473 GLsizei postConvWidth = width;
2474 struct gl_texture_unit *texUnit;
2475 struct gl_texture_object *texObj;
2476 struct gl_texture_image *texImage = NULL;
2477 GET_CURRENT_CONTEXT(ctx);
2478 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2479
2480 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2481 _mesa_debug(ctx, "glTexSubImage1D %s %d %d %d %s %s %p\n",
2482 _mesa_lookup_enum_by_nr(target), level,
2483 xoffset, width, _mesa_lookup_enum_by_nr(format),
2484 _mesa_lookup_enum_by_nr(type), pixels);
2485
2486 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2487 _mesa_update_state(ctx);
2488
2489 #if FEATURE_convolve
2490 /* XXX should test internal format */
2491 if (_mesa_is_color_format(format)) {
2492 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2493 }
2494 #endif
2495
2496 if (subtexture_error_check(ctx, 1, target, level, xoffset, 0, 0,
2497 postConvWidth, 1, 1, format, type)) {
2498 return; /* error was detected */
2499 }
2500
2501
2502 texUnit = _mesa_get_current_tex_unit(ctx);
2503 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2504 assert(texObj);
2505
2506 _mesa_lock_texture(ctx, texObj);
2507 {
2508 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2509
2510 if (subtexture_error_check2(ctx, 1, target, level, xoffset, 0, 0,
2511 postConvWidth, 1, 1, format, type, texImage)) {
2512 goto out; /* error was detected */
2513 }
2514
2515 if (width == 0)
2516 goto out; /* no-op, not an error */
2517
2518 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2519 xoffset += texImage->Border;
2520
2521 ASSERT(ctx->Driver.TexSubImage1D);
2522 (*ctx->Driver.TexSubImage1D)(ctx, target, level, xoffset, width,
2523 format, type, pixels, &ctx->Unpack,
2524 texObj, texImage);
2525 ctx->NewState |= _NEW_TEXTURE;
2526 }
2527 out:
2528 _mesa_unlock_texture(ctx, texObj);
2529 }
2530
2531
2532 void GLAPIENTRY
2533 _mesa_TexSubImage2D( GLenum target, GLint level,
2534 GLint xoffset, GLint yoffset,
2535 GLsizei width, GLsizei height,
2536 GLenum format, GLenum type,
2537 const GLvoid *pixels )
2538 {
2539 GLsizei postConvWidth = width, postConvHeight = height;
2540 struct gl_texture_unit *texUnit;
2541 struct gl_texture_object *texObj;
2542 struct gl_texture_image *texImage;
2543 GET_CURRENT_CONTEXT(ctx);
2544 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2545
2546 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2547 _mesa_debug(ctx, "glTexSubImage2D %s %d %d %d %d %d %s %s %p\n",
2548 _mesa_lookup_enum_by_nr(target), level,
2549 xoffset, yoffset, width, height,
2550 _mesa_lookup_enum_by_nr(format),
2551 _mesa_lookup_enum_by_nr(type), pixels);
2552
2553 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2554 _mesa_update_state(ctx);
2555
2556 #if FEATURE_convolve
2557 /* XXX should test internal format */
2558 if (_mesa_is_color_format(format)) {
2559 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2560 &postConvHeight);
2561 }
2562 #endif
2563
2564 if (subtexture_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
2565 postConvWidth, postConvHeight, 1, format, type)) {
2566 return; /* error was detected */
2567 }
2568
2569 texUnit = _mesa_get_current_tex_unit(ctx);
2570 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2571 _mesa_lock_texture(ctx, texObj);
2572 {
2573 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2574
2575 if (subtexture_error_check2(ctx, 2, target, level, xoffset, yoffset, 0,
2576 postConvWidth, postConvHeight, 1, format, type,
2577 texImage)) {
2578 goto out; /* error was detected */
2579 }
2580
2581 if (width == 0 || height == 0)
2582 goto out; /* no-op, not an error */
2583
2584 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2585 xoffset += texImage->Border;
2586 yoffset += texImage->Border;
2587
2588 ASSERT(ctx->Driver.TexSubImage2D);
2589 (*ctx->Driver.TexSubImage2D)(ctx, target, level, xoffset, yoffset,
2590 width, height, format, type, pixels,
2591 &ctx->Unpack, texObj, texImage);
2592 ctx->NewState |= _NEW_TEXTURE;
2593 }
2594 out:
2595 _mesa_unlock_texture(ctx, texObj);
2596 }
2597
2598
2599
2600 void GLAPIENTRY
2601 _mesa_TexSubImage3D( GLenum target, GLint level,
2602 GLint xoffset, GLint yoffset, GLint zoffset,
2603 GLsizei width, GLsizei height, GLsizei depth,
2604 GLenum format, GLenum type,
2605 const GLvoid *pixels )
2606 {
2607 struct gl_texture_unit *texUnit;
2608 struct gl_texture_object *texObj;
2609 struct gl_texture_image *texImage;
2610 GET_CURRENT_CONTEXT(ctx);
2611 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2612
2613 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2614 _mesa_debug(ctx, "glTexSubImage3D %s %d %d %d %d %d %d %d %s %s %p\n",
2615 _mesa_lookup_enum_by_nr(target), level,
2616 xoffset, yoffset, zoffset, width, height, depth,
2617 _mesa_lookup_enum_by_nr(format),
2618 _mesa_lookup_enum_by_nr(type), pixels);
2619
2620 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2621 _mesa_update_state(ctx);
2622
2623 if (subtexture_error_check(ctx, 3, target, level, xoffset, yoffset, zoffset,
2624 width, height, depth, format, type)) {
2625 return; /* error was detected */
2626 }
2627
2628 texUnit = _mesa_get_current_tex_unit(ctx);
2629 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2630
2631 _mesa_lock_texture(ctx, texObj);
2632 {
2633 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2634
2635 if (subtexture_error_check2(ctx, 3, target, level, xoffset, yoffset, zoffset,
2636 width, height, depth, format, type, texImage)) {
2637 goto out; /* error was detected */
2638 }
2639
2640 if (width == 0 || height == 0 || height == 0)
2641 goto out; /* no-op, not an error */
2642
2643 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2644 xoffset += texImage->Border;
2645 yoffset += texImage->Border;
2646 zoffset += texImage->Border;
2647
2648 ASSERT(ctx->Driver.TexSubImage3D);
2649 (*ctx->Driver.TexSubImage3D)(ctx, target, level,
2650 xoffset, yoffset, zoffset,
2651 width, height, depth,
2652 format, type, pixels,
2653 &ctx->Unpack, texObj, texImage );
2654 ctx->NewState |= _NEW_TEXTURE;
2655 }
2656 out:
2657 _mesa_unlock_texture(ctx, texObj);
2658 }
2659
2660
2661
2662 void GLAPIENTRY
2663 _mesa_CopyTexImage1D( GLenum target, GLint level,
2664 GLenum internalFormat,
2665 GLint x, GLint y,
2666 GLsizei width, GLint border )
2667 {
2668 struct gl_texture_unit *texUnit;
2669 struct gl_texture_object *texObj;
2670 struct gl_texture_image *texImage;
2671 GLsizei postConvWidth = width;
2672 const GLuint face = _mesa_tex_target_to_face(target);
2673 GET_CURRENT_CONTEXT(ctx);
2674 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2675
2676 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2677 _mesa_debug(ctx, "glCopyTexImage1D %s %d %s %d %d %d %d\n",
2678 _mesa_lookup_enum_by_nr(target), level,
2679 _mesa_lookup_enum_by_nr(internalFormat),
2680 x, y, width, border);
2681
2682 if (ctx->NewState & NEW_COPY_TEX_STATE)
2683 _mesa_update_state(ctx);
2684
2685 #if FEATURE_convolve
2686 if (_mesa_is_color_format(internalFormat)) {
2687 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2688 }
2689 #endif
2690
2691 if (copytexture_error_check(ctx, 1, target, level, internalFormat,
2692 postConvWidth, 1, border))
2693 return;
2694
2695 texUnit = _mesa_get_current_tex_unit(ctx);
2696 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2697 _mesa_lock_texture(ctx, texObj);
2698 {
2699 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2700 if (!texImage) {
2701 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage1D");
2702 goto out;
2703 }
2704
2705 if (texImage->Data) {
2706 ctx->Driver.FreeTexImageData( ctx, texImage );
2707 }
2708
2709 ASSERT(texImage->Data == NULL);
2710
2711 clear_teximage_fields(texImage); /* not really needed, but helpful */
2712 _mesa_init_teximage_fields(ctx, target, texImage, postConvWidth, 1, 1,
2713 border, internalFormat);
2714
2715
2716 ASSERT(ctx->Driver.CopyTexImage1D);
2717 (*ctx->Driver.CopyTexImage1D)(ctx, target, level, internalFormat,
2718 x, y, width, border);
2719
2720 ASSERT(texImage->TexFormat);
2721
2722 update_fbo_texture(ctx, texObj, face, level);
2723
2724 /* state update */
2725 texObj->_Complete = GL_FALSE;
2726 ctx->NewState |= _NEW_TEXTURE;
2727 }
2728 out:
2729 _mesa_unlock_texture(ctx, texObj);
2730 }
2731
2732
2733
2734 void GLAPIENTRY
2735 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
2736 GLint x, GLint y, GLsizei width, GLsizei height,
2737 GLint border )
2738 {
2739 struct gl_texture_unit *texUnit;
2740 struct gl_texture_object *texObj;
2741 struct gl_texture_image *texImage;
2742 GLsizei postConvWidth = width, postConvHeight = height;
2743 const GLuint face = _mesa_tex_target_to_face(target);
2744 GET_CURRENT_CONTEXT(ctx);
2745 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2746
2747 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2748 _mesa_debug(ctx, "glCopyTexImage2D %s %d %s %d %d %d %d %d\n",
2749 _mesa_lookup_enum_by_nr(target), level,
2750 _mesa_lookup_enum_by_nr(internalFormat),
2751 x, y, width, height, border);
2752
2753 if (ctx->NewState & NEW_COPY_TEX_STATE)
2754 _mesa_update_state(ctx);
2755
2756 #if FEATURE_convolve
2757 if (_mesa_is_color_format(internalFormat)) {
2758 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2759 &postConvHeight);
2760 }
2761 #endif
2762
2763 if (copytexture_error_check(ctx, 2, target, level, internalFormat,
2764 postConvWidth, postConvHeight, border))
2765 return;
2766
2767 texUnit = _mesa_get_current_tex_unit(ctx);
2768 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2769
2770 _mesa_lock_texture(ctx, texObj);
2771 {
2772 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2773
2774 if (!texImage) {
2775 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage2D");
2776 goto out;
2777 }
2778
2779 if (texImage->Data) {
2780 ctx->Driver.FreeTexImageData( ctx, texImage );
2781 }
2782
2783 ASSERT(texImage->Data == NULL);
2784
2785 clear_teximage_fields(texImage); /* not really needed, but helpful */
2786 _mesa_init_teximage_fields(ctx, target, texImage,
2787 postConvWidth, postConvHeight, 1,
2788 border, internalFormat);
2789
2790 ASSERT(ctx->Driver.CopyTexImage2D);
2791 (*ctx->Driver.CopyTexImage2D)(ctx, target, level, internalFormat,
2792 x, y, width, height, border);
2793
2794 ASSERT(texImage->TexFormat);
2795
2796 update_fbo_texture(ctx, texObj, face, level);
2797
2798 /* state update */
2799 texObj->_Complete = GL_FALSE;
2800 ctx->NewState |= _NEW_TEXTURE;
2801 }
2802 out:
2803 _mesa_unlock_texture(ctx, texObj);
2804 }
2805
2806
2807 void GLAPIENTRY
2808 _mesa_CopyTexSubImage1D( GLenum target, GLint level,
2809 GLint xoffset, GLint x, GLint y, GLsizei width )
2810 {
2811 struct gl_texture_unit *texUnit;
2812 struct gl_texture_object *texObj;
2813 struct gl_texture_image *texImage;
2814 GLsizei postConvWidth = width;
2815 GLint yoffset = 0;
2816 GLsizei height = 1;
2817
2818 GET_CURRENT_CONTEXT(ctx);
2819 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2820
2821 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2822 _mesa_debug(ctx, "glCopyTexSubImage1D %s %d %d %d %d %d\n",
2823 _mesa_lookup_enum_by_nr(target),
2824 level, xoffset, x, y, width);
2825
2826 if (ctx->NewState & NEW_COPY_TEX_STATE)
2827 _mesa_update_state(ctx);
2828
2829 if (copytexsubimage_error_check1(ctx, 1, target, level))
2830 return;
2831
2832 texUnit = _mesa_get_current_tex_unit(ctx);
2833 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2834
2835 _mesa_lock_texture(ctx, texObj);
2836 {
2837 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2838
2839 #if FEATURE_convolve
2840 if (texImage && _mesa_is_color_format(texImage->InternalFormat)) {
2841 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2842 }
2843 #endif
2844
2845 if (copytexsubimage_error_check2(ctx, 1, target, level,
2846 xoffset, 0, 0, postConvWidth, 1,
2847 texImage))
2848 goto out;
2849
2850
2851 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2852 xoffset += texImage->Border;
2853
2854 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
2855 &width, &height)) {
2856 ASSERT(ctx->Driver.CopyTexSubImage1D);
2857 ctx->Driver.CopyTexSubImage1D(ctx, target, level,
2858 xoffset, x, y, width);
2859 }
2860
2861 ctx->NewState |= _NEW_TEXTURE;
2862 }
2863 out:
2864 _mesa_unlock_texture(ctx, texObj);
2865 }
2866
2867
2868
2869 void GLAPIENTRY
2870 _mesa_CopyTexSubImage2D( GLenum target, GLint level,
2871 GLint xoffset, GLint yoffset,
2872 GLint x, GLint y, GLsizei width, GLsizei height )
2873 {
2874 struct gl_texture_unit *texUnit;
2875 struct gl_texture_object *texObj;
2876 struct gl_texture_image *texImage;
2877 GLsizei postConvWidth = width, postConvHeight = height;
2878 GET_CURRENT_CONTEXT(ctx);
2879 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2880
2881 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2882 _mesa_debug(ctx, "glCopyTexSubImage2D %s %d %d %d %d %d %d %d\n",
2883 _mesa_lookup_enum_by_nr(target),
2884 level, xoffset, yoffset, x, y, width, height);
2885
2886 if (ctx->NewState & NEW_COPY_TEX_STATE)
2887 _mesa_update_state(ctx);
2888
2889 if (copytexsubimage_error_check1(ctx, 2, target, level))
2890 return;
2891
2892 texUnit = _mesa_get_current_tex_unit(ctx);
2893 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2894
2895 _mesa_lock_texture(ctx, texObj);
2896 {
2897 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2898
2899 #if FEATURE_convolve
2900 if (texImage && _mesa_is_color_format(texImage->InternalFormat)) {
2901 _mesa_adjust_image_for_convolution(ctx, 2,
2902 &postConvWidth, &postConvHeight);
2903 }
2904 #endif
2905
2906 if (copytexsubimage_error_check2(ctx, 2, target, level, xoffset, yoffset, 0,
2907 postConvWidth, postConvHeight, texImage))
2908 goto out;
2909
2910 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2911 xoffset += texImage->Border;
2912 yoffset += texImage->Border;
2913
2914 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
2915 &width, &height)) {
2916 ASSERT(ctx->Driver.CopyTexSubImage2D);
2917 ctx->Driver.CopyTexSubImage2D(ctx, target, level,
2918 xoffset, yoffset, x, y, width, height);
2919 }
2920
2921 ctx->NewState |= _NEW_TEXTURE;
2922 }
2923 out:
2924 _mesa_unlock_texture(ctx, texObj);
2925 }
2926
2927
2928
2929 void GLAPIENTRY
2930 _mesa_CopyTexSubImage3D( GLenum target, GLint level,
2931 GLint xoffset, GLint yoffset, GLint zoffset,
2932 GLint x, GLint y, GLsizei width, GLsizei height )
2933 {
2934 struct gl_texture_unit *texUnit;
2935 struct gl_texture_object *texObj;
2936 struct gl_texture_image *texImage;
2937 GLsizei postConvWidth = width, postConvHeight = height;
2938 GET_CURRENT_CONTEXT(ctx);
2939 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2940
2941 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2942 _mesa_debug(ctx, "glCopyTexSubImage3D %s %d %d %d %d %d %d %d %d\n",
2943 _mesa_lookup_enum_by_nr(target),
2944 level, xoffset, yoffset, zoffset, x, y, width, height);
2945
2946 if (ctx->NewState & NEW_COPY_TEX_STATE)
2947 _mesa_update_state(ctx);
2948
2949 if (copytexsubimage_error_check1(ctx, 3, target, level))
2950 return;
2951
2952 texUnit = _mesa_get_current_tex_unit(ctx);
2953 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2954
2955 _mesa_lock_texture(ctx, texObj);
2956 {
2957 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2958
2959 #if FEATURE_convolve
2960 if (texImage && _mesa_is_color_format(texImage->InternalFormat)) {
2961 _mesa_adjust_image_for_convolution(ctx, 2,
2962 &postConvWidth, &postConvHeight);
2963 }
2964 #endif
2965
2966 if (copytexsubimage_error_check2(ctx, 3, target, level, xoffset, yoffset,
2967 zoffset, postConvWidth, postConvHeight,
2968 texImage))
2969 goto out;
2970
2971 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2972 xoffset += texImage->Border;
2973 yoffset += texImage->Border;
2974 zoffset += texImage->Border;
2975
2976 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
2977 &width, &height)) {
2978 ASSERT(ctx->Driver.CopyTexSubImage3D);
2979 ctx->Driver.CopyTexSubImage3D(ctx, target, level,
2980 xoffset, yoffset, zoffset,
2981 x, y, width, height);
2982 }
2983
2984 ctx->NewState |= _NEW_TEXTURE;
2985 }
2986 out:
2987 _mesa_unlock_texture(ctx, texObj);
2988 }
2989
2990
2991
2992
2993 /**********************************************************************/
2994 /****** Compressed Textures ******/
2995 /**********************************************************************/
2996
2997
2998 /**
2999 * Error checking for glCompressedTexImage[123]D().
3000 * \return error code or GL_NO_ERROR.
3001 */
3002 static GLenum
3003 compressed_texture_error_check(GLcontext *ctx, GLint dimensions,
3004 GLenum target, GLint level,
3005 GLenum internalFormat, GLsizei width,
3006 GLsizei height, GLsizei depth, GLint border,
3007 GLsizei imageSize)
3008 {
3009 GLint expectedSize, maxLevels = 0, maxTextureSize;
3010
3011 if (dimensions == 1) {
3012 /* 1D compressed textures not allowed */
3013 return GL_INVALID_ENUM;
3014 }
3015 else if (dimensions == 2) {
3016 if (target == GL_PROXY_TEXTURE_2D) {
3017 maxLevels = ctx->Const.MaxTextureLevels;
3018 }
3019 else if (target == GL_TEXTURE_2D) {
3020 maxLevels = ctx->Const.MaxTextureLevels;
3021 }
3022 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
3023 if (!ctx->Extensions.ARB_texture_cube_map)
3024 return GL_INVALID_ENUM; /*target*/
3025 maxLevels = ctx->Const.MaxCubeTextureLevels;
3026 }
3027 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3028 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
3029 if (!ctx->Extensions.ARB_texture_cube_map)
3030 return GL_INVALID_ENUM; /*target*/
3031 maxLevels = ctx->Const.MaxCubeTextureLevels;
3032 }
3033 else {
3034 return GL_INVALID_ENUM; /*target*/
3035 }
3036 }
3037 else if (dimensions == 3) {
3038 /* 3D compressed textures not allowed */
3039 return GL_INVALID_ENUM;
3040 }
3041
3042 maxTextureSize = 1 << (maxLevels - 1);
3043
3044 /* This will detect any invalid internalFormat value */
3045 if (!is_compressed_format(ctx, internalFormat))
3046 return GL_INVALID_ENUM;
3047
3048 /* This should really never fail */
3049 if (_mesa_base_tex_format(ctx, internalFormat) < 0)
3050 return GL_INVALID_ENUM;
3051
3052 if (border != 0)
3053 return GL_INVALID_VALUE;
3054
3055 /*
3056 * XXX We should probably use the proxy texture error check function here.
3057 */
3058 if (width < 1 || width > maxTextureSize ||
3059 (!ctx->Extensions.ARB_texture_non_power_of_two && !_mesa_is_pow_two(width)))
3060 return GL_INVALID_VALUE;
3061
3062 if ((height < 1 || height > maxTextureSize ||
3063 (!ctx->Extensions.ARB_texture_non_power_of_two && !_mesa_is_pow_two(height)))
3064 && dimensions > 1)
3065 return GL_INVALID_VALUE;
3066
3067 if ((depth < 1 || depth > maxTextureSize ||
3068 (!ctx->Extensions.ARB_texture_non_power_of_two && !_mesa_is_pow_two(depth)))
3069 && dimensions > 2)
3070 return GL_INVALID_VALUE;
3071
3072 /* For cube map, width must equal height */
3073 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3074 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB && width != height)
3075 return GL_INVALID_VALUE;
3076
3077 if (level < 0 || level >= maxLevels)
3078 return GL_INVALID_VALUE;
3079
3080 expectedSize = _mesa_compressed_texture_size_glenum(ctx, width, height,
3081 depth, internalFormat);
3082 if (expectedSize != imageSize)
3083 return GL_INVALID_VALUE;
3084
3085 #if FEATURE_EXT_texture_sRGB
3086 if ((internalFormat == GL_COMPRESSED_SRGB_S3TC_DXT1_EXT ||
3087 internalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT ||
3088 internalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT ||
3089 internalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT)
3090 && border != 0) {
3091 return GL_INVALID_OPERATION;
3092 }
3093 #endif
3094
3095 return GL_NO_ERROR;
3096 }
3097
3098
3099 /**
3100 * Error checking for glCompressedTexSubImage[123]D().
3101 * \warning There are some bad assumptions here about the size of compressed
3102 * texture tiles (multiple of 4) used to test the validity of the
3103 * offset and size parameters.
3104 * \return error code or GL_NO_ERROR.
3105 */
3106 static GLenum
3107 compressed_subtexture_error_check(GLcontext *ctx, GLint dimensions,
3108 GLenum target, GLint level,
3109 GLint xoffset, GLint yoffset, GLint zoffset,
3110 GLsizei width, GLsizei height, GLsizei depth,
3111 GLenum format, GLsizei imageSize)
3112 {
3113 GLint expectedSize, maxLevels = 0, maxTextureSize;
3114 (void) zoffset;
3115
3116 if (dimensions == 1) {
3117 /* 1D compressed textures not allowed */
3118 return GL_INVALID_ENUM;
3119 }
3120 else if (dimensions == 2) {
3121 if (target == GL_PROXY_TEXTURE_2D) {
3122 maxLevels = ctx->Const.MaxTextureLevels;
3123 }
3124 else if (target == GL_TEXTURE_2D) {
3125 maxLevels = ctx->Const.MaxTextureLevels;
3126 }
3127 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
3128 if (!ctx->Extensions.ARB_texture_cube_map)
3129 return GL_INVALID_ENUM; /*target*/
3130 maxLevels = ctx->Const.MaxCubeTextureLevels;
3131 }
3132 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3133 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
3134 if (!ctx->Extensions.ARB_texture_cube_map)
3135 return GL_INVALID_ENUM; /*target*/
3136 maxLevels = ctx->Const.MaxCubeTextureLevels;
3137 }
3138 else {
3139 return GL_INVALID_ENUM; /*target*/
3140 }
3141 }
3142 else if (dimensions == 3) {
3143 /* 3D compressed textures not allowed */
3144 return GL_INVALID_ENUM;
3145 }
3146
3147 maxTextureSize = 1 << (maxLevels - 1);
3148
3149 /* this will catch any invalid compressed format token */
3150 if (!is_compressed_format(ctx, format))
3151 return GL_INVALID_ENUM;
3152
3153 if (width < 1 || width > maxTextureSize)
3154 return GL_INVALID_VALUE;
3155
3156 if ((height < 1 || height > maxTextureSize)
3157 && dimensions > 1)
3158 return GL_INVALID_VALUE;
3159
3160 if (level < 0 || level >= maxLevels)
3161 return GL_INVALID_VALUE;
3162
3163 /* XXX these tests are specific to the compressed format.
3164 * this code should be generalized in some way.
3165 */
3166 if ((xoffset & 3) != 0 || (yoffset & 3) != 0)
3167 return GL_INVALID_VALUE;
3168
3169 if ((width & 3) != 0 && width != 2 && width != 1)
3170 return GL_INVALID_VALUE;
3171
3172 if ((height & 3) != 0 && height != 2 && height != 1)
3173 return GL_INVALID_VALUE;
3174
3175 expectedSize = _mesa_compressed_texture_size_glenum(ctx, width, height,
3176 depth, format);
3177 if (expectedSize != imageSize)
3178 return GL_INVALID_VALUE;
3179
3180 return GL_NO_ERROR;
3181 }
3182
3183
3184
3185 void GLAPIENTRY
3186 _mesa_CompressedTexImage1DARB(GLenum target, GLint level,
3187 GLenum internalFormat, GLsizei width,
3188 GLint border, GLsizei imageSize,
3189 const GLvoid *data)
3190 {
3191 GET_CURRENT_CONTEXT(ctx);
3192 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3193
3194 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3195 _mesa_debug(ctx, "glCompressedTexImage1DARB %s %d %s %d %d %d %p\n",
3196 _mesa_lookup_enum_by_nr(target), level,
3197 _mesa_lookup_enum_by_nr(internalFormat),
3198 width, border, imageSize, data);
3199
3200 if (target == GL_TEXTURE_1D) {
3201 /* non-proxy target */
3202 struct gl_texture_unit *texUnit;
3203 struct gl_texture_object *texObj;
3204 struct gl_texture_image *texImage;
3205 GLenum error = compressed_texture_error_check(ctx, 1, target, level,
3206 internalFormat, width, 1, 1, border, imageSize);
3207 if (error) {
3208 _mesa_error(ctx, error, "glCompressedTexImage1D");
3209 return;
3210 }
3211
3212 texUnit = _mesa_get_current_tex_unit(ctx);
3213 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3214
3215 _mesa_lock_texture(ctx, texObj);
3216 {
3217 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3218 if (!texImage) {
3219 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage1D");
3220 goto out;
3221 }
3222
3223 if (texImage->Data) {
3224 ctx->Driver.FreeTexImageData( ctx, texImage );
3225 }
3226 ASSERT(texImage->Data == NULL);
3227
3228 _mesa_init_teximage_fields(ctx, target, texImage, width, 1, 1,
3229 border, internalFormat);
3230
3231 ASSERT(ctx->Driver.CompressedTexImage1D);
3232 (*ctx->Driver.CompressedTexImage1D)(ctx, target, level,
3233 internalFormat, width, border,
3234 imageSize, data,
3235 texObj, texImage);
3236
3237 /* state update */
3238 texObj->_Complete = GL_FALSE;
3239 ctx->NewState |= _NEW_TEXTURE;
3240 }
3241 out:
3242 _mesa_unlock_texture(ctx, texObj);
3243 }
3244 else if (target == GL_PROXY_TEXTURE_1D) {
3245 /* Proxy texture: check for errors and update proxy state */
3246 GLenum error = compressed_texture_error_check(ctx, 1, target, level,
3247 internalFormat, width, 1, 1, border, imageSize);
3248 if (!error) {
3249 ASSERT(ctx->Driver.TestProxyTexImage);
3250 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3251 internalFormat, GL_NONE, GL_NONE,
3252 width, 1, 1, border);
3253 }
3254 if (error) {
3255 /* if error, clear all proxy texture image parameters */
3256 struct gl_texture_image *texImage;
3257 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3258 if (texImage)
3259 clear_teximage_fields(texImage);
3260 }
3261 else {
3262 /* store the teximage parameters */
3263 struct gl_texture_unit *texUnit;
3264 struct gl_texture_object *texObj;
3265 struct gl_texture_image *texImage;
3266 texUnit = _mesa_get_current_tex_unit(ctx);
3267 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3268
3269 _mesa_lock_texture(ctx, texObj);
3270 {
3271 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3272 _mesa_init_teximage_fields(ctx, target, texImage, width, 1, 1,
3273 border, internalFormat);
3274 }
3275 _mesa_unlock_texture(ctx, texObj);
3276 }
3277 }
3278 else {
3279 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage1D(target)");
3280 return;
3281 }
3282 }
3283
3284
3285 void GLAPIENTRY
3286 _mesa_CompressedTexImage2DARB(GLenum target, GLint level,
3287 GLenum internalFormat, GLsizei width,
3288 GLsizei height, GLint border, GLsizei imageSize,
3289 const GLvoid *data)
3290 {
3291 GET_CURRENT_CONTEXT(ctx);
3292 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3293
3294 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3295 _mesa_debug(ctx, "glCompressedTexImage2DARB %s %d %s %d %d %d %d %p\n",
3296 _mesa_lookup_enum_by_nr(target), level,
3297 _mesa_lookup_enum_by_nr(internalFormat),
3298 width, height, border, imageSize, data);
3299
3300 if (target == GL_TEXTURE_2D ||
3301 (ctx->Extensions.ARB_texture_cube_map &&
3302 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3303 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
3304 /* non-proxy target */
3305 struct gl_texture_unit *texUnit;
3306 struct gl_texture_object *texObj;
3307 struct gl_texture_image *texImage;
3308 GLenum error = compressed_texture_error_check(ctx, 2, target, level,
3309 internalFormat, width, height, 1, border, imageSize);
3310 if (error) {
3311 _mesa_error(ctx, error, "glCompressedTexImage2D");
3312 return;
3313 }
3314
3315 texUnit = _mesa_get_current_tex_unit(ctx);
3316 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3317
3318 _mesa_lock_texture(ctx, texObj);
3319 {
3320 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3321 if (!texImage) {
3322 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage2D");
3323 goto out;
3324 }
3325
3326 if (texImage->Data) {
3327 ctx->Driver.FreeTexImageData( ctx, texImage );
3328 }
3329 ASSERT(texImage->Data == NULL);
3330
3331 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
3332 border, internalFormat);
3333
3334 ASSERT(ctx->Driver.CompressedTexImage2D);
3335 (*ctx->Driver.CompressedTexImage2D)(ctx, target, level,
3336 internalFormat, width, height,
3337 border, imageSize, data,
3338 texObj, texImage);
3339
3340 /* state update */
3341 texObj->_Complete = GL_FALSE;
3342 ctx->NewState |= _NEW_TEXTURE;
3343 }
3344 out:
3345 _mesa_unlock_texture(ctx, texObj);
3346 }
3347 else if (target == GL_PROXY_TEXTURE_2D ||
3348 (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB &&
3349 ctx->Extensions.ARB_texture_cube_map)) {
3350 /* Proxy texture: check for errors and update proxy state */
3351 GLenum error = compressed_texture_error_check(ctx, 2, target, level,
3352 internalFormat, width, height, 1, border, imageSize);
3353 if (!error) {
3354 ASSERT(ctx->Driver.TestProxyTexImage);
3355 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3356 internalFormat, GL_NONE, GL_NONE,
3357 width, height, 1, border);
3358 }
3359 if (error) {
3360 /* if error, clear all proxy texture image parameters */
3361 struct gl_texture_image *texImage;
3362 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3363 if (texImage)
3364 clear_teximage_fields(texImage);
3365 }
3366 else {
3367 /* store the teximage parameters */
3368 struct gl_texture_unit *texUnit;
3369 struct gl_texture_object *texObj;
3370 struct gl_texture_image *texImage;
3371 texUnit = _mesa_get_current_tex_unit(ctx);
3372 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3373
3374 _mesa_lock_texture(ctx, texObj);
3375 {
3376 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3377 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
3378 border, internalFormat);
3379 }
3380 _mesa_unlock_texture(ctx, texObj);
3381 }
3382 }
3383 else {
3384 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage2D(target)");
3385 return;
3386 }
3387 }
3388
3389
3390 void GLAPIENTRY
3391 _mesa_CompressedTexImage3DARB(GLenum target, GLint level,
3392 GLenum internalFormat, GLsizei width,
3393 GLsizei height, GLsizei depth, GLint border,
3394 GLsizei imageSize, const GLvoid *data)
3395 {
3396 GET_CURRENT_CONTEXT(ctx);
3397 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3398
3399 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3400 _mesa_debug(ctx, "glCompressedTexImage3DARB %s %d %s %d %d %d %d %d %p\n",
3401 _mesa_lookup_enum_by_nr(target), level,
3402 _mesa_lookup_enum_by_nr(internalFormat),
3403 width, height, depth, border, imageSize, data);
3404
3405 if (target == GL_TEXTURE_3D) {
3406 /* non-proxy target */
3407 struct gl_texture_unit *texUnit;
3408 struct gl_texture_object *texObj;
3409 struct gl_texture_image *texImage;
3410 GLenum error = compressed_texture_error_check(ctx, 3, target, level,
3411 internalFormat, width, height, depth, border, imageSize);
3412 if (error) {
3413 _mesa_error(ctx, error, "glCompressedTexImage3D");
3414 return;
3415 }
3416
3417 texUnit = _mesa_get_current_tex_unit(ctx);
3418 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3419 _mesa_lock_texture(ctx, texObj);
3420 {
3421 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3422 if (!texImage) {
3423 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage3D");
3424 goto out;
3425 }
3426
3427 if (texImage->Data) {
3428 ctx->Driver.FreeTexImageData( ctx, texImage );
3429 }
3430 ASSERT(texImage->Data == NULL);
3431
3432 _mesa_init_teximage_fields(ctx, target, texImage, width, height, depth,
3433 border, internalFormat);
3434
3435 ASSERT(ctx->Driver.CompressedTexImage3D);
3436 (*ctx->Driver.CompressedTexImage3D)(ctx, target, level,
3437 internalFormat,
3438 width, height, depth,
3439 border, imageSize, data,
3440 texObj, texImage);
3441
3442 /* state update */
3443 texObj->_Complete = GL_FALSE;
3444 ctx->NewState |= _NEW_TEXTURE;
3445 }
3446 out:
3447 _mesa_unlock_texture(ctx, texObj);
3448 }
3449 else if (target == GL_PROXY_TEXTURE_3D) {
3450 /* Proxy texture: check for errors and update proxy state */
3451 GLenum error = compressed_texture_error_check(ctx, 3, target, level,
3452 internalFormat, width, height, depth, border, imageSize);
3453 if (!error) {
3454 ASSERT(ctx->Driver.TestProxyTexImage);
3455 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3456 internalFormat, GL_NONE, GL_NONE,
3457 width, height, depth, border);
3458 }
3459 if (error) {
3460 /* if error, clear all proxy texture image parameters */
3461 struct gl_texture_image *texImage;
3462 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3463 if (texImage)
3464 clear_teximage_fields(texImage);
3465 }
3466 else {
3467 /* store the teximage parameters */
3468 struct gl_texture_unit *texUnit;
3469 struct gl_texture_object *texObj;
3470 struct gl_texture_image *texImage;
3471 texUnit = _mesa_get_current_tex_unit(ctx);
3472 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3473 _mesa_lock_texture(ctx, texObj);
3474 {
3475 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3476 _mesa_init_teximage_fields(ctx, target, texImage, width, height,
3477 depth, border, internalFormat);
3478 }
3479 _mesa_unlock_texture(ctx, texObj);
3480 }
3481 }
3482 else {
3483 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage3D(target)");
3484 return;
3485 }
3486 }
3487
3488
3489 void GLAPIENTRY
3490 _mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
3491 GLsizei width, GLenum format,
3492 GLsizei imageSize, const GLvoid *data)
3493 {
3494 struct gl_texture_unit *texUnit;
3495 struct gl_texture_object *texObj;
3496 struct gl_texture_image *texImage;
3497 GLenum error;
3498 GET_CURRENT_CONTEXT(ctx);
3499 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3500
3501 error = compressed_subtexture_error_check(ctx, 1, target, level,
3502 xoffset, 0, 0, /* pos */
3503 width, 1, 1, /* size */
3504 format, imageSize);
3505 if (error) {
3506 _mesa_error(ctx, error, "glCompressedTexSubImage1D");
3507 return;
3508 }
3509
3510 texUnit = _mesa_get_current_tex_unit(ctx);
3511 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3512 _mesa_lock_texture(ctx, texObj);
3513 {
3514 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3515 assert(texImage);
3516
3517 if ((GLint) format != texImage->InternalFormat) {
3518 _mesa_error(ctx, GL_INVALID_OPERATION,
3519 "glCompressedTexSubImage1D(format)");
3520 goto out;
3521 }
3522
3523 if ((width == 1 || width == 2) && (GLuint) width != texImage->Width) {
3524 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage1D(width)");
3525 goto out;
3526 }
3527
3528 if (width == 0)
3529 goto out; /* no-op, not an error */
3530
3531 if (ctx->Driver.CompressedTexSubImage1D) {
3532 (*ctx->Driver.CompressedTexSubImage1D)(ctx, target, level,
3533 xoffset, width,
3534 format, imageSize, data,
3535 texObj, texImage);
3536 }
3537 ctx->NewState |= _NEW_TEXTURE;
3538 }
3539 out:
3540 _mesa_unlock_texture(ctx, texObj);
3541 }
3542
3543
3544 void GLAPIENTRY
3545 _mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
3546 GLint yoffset, GLsizei width, GLsizei height,
3547 GLenum format, GLsizei imageSize,
3548 const GLvoid *data)
3549 {
3550 struct gl_texture_unit *texUnit;
3551 struct gl_texture_object *texObj;
3552 struct gl_texture_image *texImage;
3553 GLenum error;
3554 GET_CURRENT_CONTEXT(ctx);
3555 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3556
3557 error = compressed_subtexture_error_check(ctx, 2, target, level,
3558 xoffset, yoffset, 0, /* pos */
3559 width, height, 1, /* size */
3560 format, imageSize);
3561 if (error) {
3562 /* XXX proxy target? */
3563 _mesa_error(ctx, error, "glCompressedTexSubImage2D");
3564 return;
3565 }
3566
3567 texUnit = _mesa_get_current_tex_unit(ctx);
3568 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3569 _mesa_lock_texture(ctx, texObj);
3570 {
3571 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3572 assert(texImage);
3573
3574 if ((GLint) format != texImage->InternalFormat) {
3575 _mesa_error(ctx, GL_INVALID_OPERATION,
3576 "glCompressedTexSubImage2D(format)");
3577 goto out;
3578 }
3579
3580 if (((width == 1 || width == 2) && (GLuint) width != texImage->Width) ||
3581 ((height == 1 || height == 2) && (GLuint) height != texImage->Height)) {
3582 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage2D(size)");
3583 goto out;
3584 }
3585
3586 if (width == 0 || height == 0)
3587 goto out; /* no-op, not an error */
3588
3589 if (ctx->Driver.CompressedTexSubImage2D) {
3590 (*ctx->Driver.CompressedTexSubImage2D)(ctx, target, level,
3591 xoffset, yoffset, width, height,
3592 format, imageSize, data,
3593 texObj, texImage);
3594 }
3595 ctx->NewState |= _NEW_TEXTURE;
3596 }
3597 out:
3598 _mesa_unlock_texture(ctx, texObj);
3599 }
3600
3601
3602 void GLAPIENTRY
3603 _mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
3604 GLint yoffset, GLint zoffset, GLsizei width,
3605 GLsizei height, GLsizei depth, GLenum format,
3606 GLsizei imageSize, const GLvoid *data)
3607 {
3608 struct gl_texture_unit *texUnit;
3609 struct gl_texture_object *texObj;
3610 struct gl_texture_image *texImage;
3611 GLenum error;
3612 GET_CURRENT_CONTEXT(ctx);
3613 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3614
3615 error = compressed_subtexture_error_check(ctx, 3, target, level,
3616 xoffset, yoffset, zoffset,/*pos*/
3617 width, height, depth, /*size*/
3618 format, imageSize);
3619 if (error) {
3620 _mesa_error(ctx, error, "glCompressedTexSubImage3D");
3621 return;
3622 }
3623
3624 texUnit = _mesa_get_current_tex_unit(ctx);
3625 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3626 _mesa_lock_texture(ctx, texObj);
3627 {
3628 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3629 assert(texImage);
3630
3631 if ((GLint) format != texImage->InternalFormat) {
3632 _mesa_error(ctx, GL_INVALID_OPERATION,
3633 "glCompressedTexSubImage3D(format)");
3634 goto out;
3635 }
3636
3637 if (((width == 1 || width == 2) && (GLuint) width != texImage->Width) ||
3638 ((height == 1 || height == 2) && (GLuint) height != texImage->Height) ||
3639 ((depth == 1 || depth == 2) && (GLuint) depth != texImage->Depth)) {
3640 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage3D(size)");
3641 goto out;
3642 }
3643
3644 if (width == 0 || height == 0 || depth == 0)
3645 goto out; /* no-op, not an error */
3646
3647 if (ctx->Driver.CompressedTexSubImage3D) {
3648 (*ctx->Driver.CompressedTexSubImage3D)(ctx, target, level,
3649 xoffset, yoffset, zoffset,
3650 width, height, depth,
3651 format, imageSize, data,
3652 texObj, texImage);
3653 }
3654 ctx->NewState |= _NEW_TEXTURE;
3655 }
3656 out:
3657 _mesa_unlock_texture(ctx, texObj);
3658 }
3659
3660