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