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