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