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