mesa: add support for ATI_envmap_bumpmap
[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 texUnit 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(format or type)", dimensions);
1556 }
1557 return GL_TRUE;
1558 }
1559
1560 /* make sure internal format and format basically agree */
1561 colorFormat = _mesa_is_color_format(format);
1562 indexFormat = is_index_format(format);
1563 if ((_mesa_is_color_format(internalFormat) && !colorFormat && !indexFormat) ||
1564 (is_index_format(internalFormat) && !indexFormat) ||
1565 (is_depth_format(internalFormat) != is_depth_format(format)) ||
1566 (is_ycbcr_format(internalFormat) != is_ycbcr_format(format)) ||
1567 (is_depthstencil_format(internalFormat) != is_depthstencil_format(format)) ||
1568 (is_dudv_format(internalFormat) != is_dudv_format(format))) {
1569 if (!isProxy)
1570 _mesa_error(ctx, GL_INVALID_OPERATION,
1571 "glTexImage(internalFormat/format)");
1572 return GL_TRUE;
1573 }
1574
1575 /* additional checks for ycbcr textures */
1576 if (internalFormat == GL_YCBCR_MESA) {
1577 ASSERT(ctx->Extensions.MESA_ycbcr_texture);
1578 if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
1579 type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
1580 char message[100];
1581 _mesa_sprintf(message,
1582 "glTexImage%d(format/type YCBCR mismatch", dimensions);
1583 _mesa_error(ctx, GL_INVALID_ENUM, message);
1584 return GL_TRUE; /* error */
1585 }
1586 if (target != GL_TEXTURE_2D &&
1587 target != GL_PROXY_TEXTURE_2D &&
1588 target != GL_TEXTURE_RECTANGLE_NV &&
1589 target != GL_PROXY_TEXTURE_RECTANGLE_NV) {
1590 if (!isProxy)
1591 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage(target)");
1592 return GL_TRUE;
1593 }
1594 if (border != 0) {
1595 if (!isProxy) {
1596 char message[100];
1597 _mesa_sprintf(message,
1598 "glTexImage%d(format=GL_YCBCR_MESA and border=%d)",
1599 dimensions, border);
1600 _mesa_error(ctx, GL_INVALID_VALUE, message);
1601 }
1602 return GL_TRUE;
1603 }
1604 }
1605
1606 /* additional checks for depth textures */
1607 if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT) {
1608 /* Only 1D, 2D and rectangular textures supported, not 3D or cubes */
1609 if (target != GL_TEXTURE_1D &&
1610 target != GL_PROXY_TEXTURE_1D &&
1611 target != GL_TEXTURE_2D &&
1612 target != GL_PROXY_TEXTURE_2D &&
1613 target != GL_TEXTURE_RECTANGLE_ARB &&
1614 target != GL_PROXY_TEXTURE_RECTANGLE_ARB) {
1615 if (!isProxy)
1616 _mesa_error(ctx, GL_INVALID_ENUM,
1617 "glTexImage(target/internalFormat)");
1618 return GL_TRUE;
1619 }
1620 }
1621
1622 /* additional checks for compressed textures */
1623 if (is_compressed_format(ctx, internalFormat)) {
1624 if (!target_can_be_compressed(ctx, target) && !isProxy) {
1625 _mesa_error(ctx, GL_INVALID_ENUM,
1626 "glTexImage%d(target)", dimensions);
1627 return GL_TRUE;
1628 }
1629 if (border != 0) {
1630 if (!isProxy) {
1631 _mesa_error(ctx, GL_INVALID_OPERATION,
1632 "glTexImage%D(border!=0)", dimensions);
1633 }
1634 return GL_TRUE;
1635 }
1636 }
1637
1638 /* if we get here, the parameters are OK */
1639 return GL_FALSE;
1640 }
1641
1642
1643 /**
1644 * Test glTexSubImage[123]D() parameters for errors.
1645 *
1646 * \param ctx GL context.
1647 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1648 * \param target texture target given by the user.
1649 * \param level image level given by the user.
1650 * \param xoffset sub-image x offset given by the user.
1651 * \param yoffset sub-image y offset given by the user.
1652 * \param zoffset sub-image z offset given by the user.
1653 * \param format pixel data format given by the user.
1654 * \param type pixel data type given by the user.
1655 * \param width image width given by the user.
1656 * \param height image height given by the user.
1657 * \param depth image depth given by the user.
1658 *
1659 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1660 *
1661 * Verifies each of the parameters against the constants specified in
1662 * __GLcontextRec::Const and the supported extensions, and according to the
1663 * OpenGL specification.
1664 */
1665 static GLboolean
1666 subtexture_error_check( GLcontext *ctx, GLuint dimensions,
1667 GLenum target, GLint level,
1668 GLint xoffset, GLint yoffset, GLint zoffset,
1669 GLint width, GLint height, GLint depth,
1670 GLenum format, GLenum type )
1671 {
1672 /* Check target */
1673 if (dimensions == 1) {
1674 if (target != GL_TEXTURE_1D) {
1675 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage1D(target)" );
1676 return GL_TRUE;
1677 }
1678 }
1679 else if (dimensions == 2) {
1680 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1681 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
1682 if (!ctx->Extensions.ARB_texture_cube_map) {
1683 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1684 return GL_TRUE;
1685 }
1686 }
1687 else if (target == GL_TEXTURE_RECTANGLE_NV) {
1688 if (!ctx->Extensions.NV_texture_rectangle) {
1689 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1690 return GL_TRUE;
1691 }
1692 }
1693 else if (target == GL_TEXTURE_1D_ARRAY_EXT) {
1694 if (!ctx->Extensions.MESA_texture_array) {
1695 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1696 return GL_TRUE;
1697 }
1698 }
1699 else if (target != GL_TEXTURE_2D) {
1700 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
1701 return GL_TRUE;
1702 }
1703 }
1704 else if (dimensions == 3) {
1705 if (target == GL_TEXTURE_2D_ARRAY_EXT) {
1706 if (!ctx->Extensions.MESA_texture_array) {
1707 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage3D(target)" );
1708 return GL_TRUE;
1709 }
1710 }
1711 else if (target != GL_TEXTURE_3D) {
1712 _mesa_error( ctx, GL_INVALID_ENUM, "glTexSubImage3D(target)" );
1713 return GL_TRUE;
1714 }
1715 }
1716 else {
1717 _mesa_problem( ctx, "invalid dims in texture_error_check" );
1718 return GL_TRUE;
1719 }
1720
1721 /* Basic level check */
1722 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1723 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage2D(level=%d)", level);
1724 return GL_TRUE;
1725 }
1726
1727 if (width < 0) {
1728 _mesa_error(ctx, GL_INVALID_VALUE,
1729 "glTexSubImage%dD(width=%d)", dimensions, width);
1730 return GL_TRUE;
1731 }
1732 if (height < 0 && dimensions > 1) {
1733 _mesa_error(ctx, GL_INVALID_VALUE,
1734 "glTexSubImage%dD(height=%d)", dimensions, height);
1735 return GL_TRUE;
1736 }
1737 if (depth < 0 && dimensions > 2) {
1738 _mesa_error(ctx, GL_INVALID_VALUE,
1739 "glTexSubImage%dD(depth=%d)", dimensions, depth);
1740 return GL_TRUE;
1741 }
1742
1743 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
1744 _mesa_error(ctx, GL_INVALID_ENUM,
1745 "glTexSubImage%dD(format or type)", dimensions);
1746 return GL_TRUE;
1747 }
1748
1749 return GL_FALSE;
1750 }
1751
1752 static GLboolean
1753 subtexture_error_check2( GLcontext *ctx, GLuint dimensions,
1754 GLenum target, GLint level,
1755 GLint xoffset, GLint yoffset, GLint zoffset,
1756 GLint width, GLint height, GLint depth,
1757 GLenum format, GLenum type,
1758 const struct gl_texture_image *destTex )
1759 {
1760 if (!destTex) {
1761 /* undefined image level */
1762 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexSubImage%dD", dimensions);
1763 return GL_TRUE;
1764 }
1765
1766 if (xoffset < -((GLint)destTex->Border)) {
1767 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset)",
1768 dimensions);
1769 return GL_TRUE;
1770 }
1771 if (xoffset + width > (GLint) (destTex->Width + destTex->Border)) {
1772 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset+width)",
1773 dimensions);
1774 return GL_TRUE;
1775 }
1776 if (dimensions > 1) {
1777 if (yoffset < -((GLint)destTex->Border)) {
1778 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset)",
1779 dimensions);
1780 return GL_TRUE;
1781 }
1782 if (yoffset + height > (GLint) (destTex->Height + destTex->Border)) {
1783 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset+height)",
1784 dimensions);
1785 return GL_TRUE;
1786 }
1787 }
1788 if (dimensions > 2) {
1789 if (zoffset < -((GLint)destTex->Border)) {
1790 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset)");
1791 return GL_TRUE;
1792 }
1793 if (zoffset + depth > (GLint) (destTex->Depth + destTex->Border)) {
1794 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset+depth)");
1795 return GL_TRUE;
1796 }
1797 }
1798
1799 #if FEATURE_EXT_texture_sRGB
1800 if (destTex->InternalFormat == GL_COMPRESSED_SRGB_S3TC_DXT1_EXT ||
1801 destTex->InternalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT ||
1802 destTex->InternalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT ||
1803 destTex->InternalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT) {
1804 if ((width & 0x3) || (height & 0x3) ||
1805 (xoffset & 0x3) || (yoffset & 0x3))
1806 _mesa_error(ctx, GL_INVALID_OPERATION,
1807 "glTexSubImage%dD(size or offset not multiple of 4)",
1808 dimensions);
1809 return GL_TRUE;
1810 }
1811 #endif
1812
1813 if (destTex->IsCompressed) {
1814 if (!target_can_be_compressed(ctx, target)) {
1815 _mesa_error(ctx, GL_INVALID_ENUM,
1816 "glTexSubImage%D(target)", dimensions);
1817 return GL_TRUE;
1818 }
1819 /* offset must be multiple of 4 */
1820 if ((xoffset & 3) || (yoffset & 3)) {
1821 _mesa_error(ctx, GL_INVALID_OPERATION,
1822 "glTexSubImage%D(xoffset or yoffset)", dimensions);
1823 return GL_TRUE;
1824 }
1825 /* size must be multiple of 4 or equal to whole texture size */
1826 if ((width & 3) && (GLuint) width != destTex->Width) {
1827 _mesa_error(ctx, GL_INVALID_OPERATION,
1828 "glTexSubImage%D(width)", dimensions);
1829 return GL_TRUE;
1830 }
1831 if ((height & 3) && (GLuint) height != destTex->Height) {
1832 _mesa_error(ctx, GL_INVALID_OPERATION,
1833 "glTexSubImage%D(width)", dimensions);
1834 return GL_TRUE;
1835 }
1836 }
1837
1838 return GL_FALSE;
1839 }
1840
1841
1842 /**
1843 * Test glCopyTexImage[12]D() parameters for errors.
1844 *
1845 * \param ctx GL context.
1846 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1847 * \param target texture target given by the user.
1848 * \param level image level given by the user.
1849 * \param internalFormat internal format given by the user.
1850 * \param width image width given by the user.
1851 * \param height image height given by the user.
1852 * \param depth image depth given by the user.
1853 * \param border texture border.
1854 *
1855 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1856 *
1857 * Verifies each of the parameters against the constants specified in
1858 * __GLcontextRec::Const and the supported extensions, and according to the
1859 * OpenGL specification.
1860 */
1861 static GLboolean
1862 copytexture_error_check( GLcontext *ctx, GLuint dimensions,
1863 GLenum target, GLint level, GLint internalFormat,
1864 GLint width, GLint height, GLint border )
1865 {
1866 GLenum type;
1867 GLboolean sizeOK;
1868 GLint format;
1869
1870 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1871 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1872 _mesa_error(ctx, GL_INVALID_VALUE,
1873 "glCopyTexImage%dD(level=%d)", dimensions, level);
1874 return GL_TRUE;
1875 }
1876
1877 /* Check that the source buffer is complete */
1878 if (ctx->ReadBuffer->Name) {
1879 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
1880 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
1881 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
1882 "glCopyTexImage%dD(invalid readbuffer)", dimensions);
1883 return GL_TRUE;
1884 }
1885 }
1886
1887 /* Check border */
1888 if (border < 0 || border > 1 ||
1889 ((target == GL_TEXTURE_RECTANGLE_NV ||
1890 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1891 return GL_TRUE;
1892 }
1893
1894 format = _mesa_base_tex_format(ctx, internalFormat);
1895 if (format < 0) {
1896 _mesa_error(ctx, GL_INVALID_VALUE,
1897 "glCopyTexImage%dD(internalFormat)", dimensions);
1898 return GL_TRUE;
1899 }
1900
1901 /* NOTE: the format and type aren't really significant for
1902 * TestProxyTexImage(). Only the internalformat really matters.
1903 if (!_mesa_source_buffer_exists(ctx, format)) {
1904 _mesa_error(ctx, GL_INVALID_OPERATION,
1905 "glCopyTexImage%dD(missing readbuffer)", dimensions);
1906 return GL_TRUE;
1907 }
1908
1909 */
1910 type = GL_FLOAT;
1911
1912 /* Check target and call ctx->Driver.TestProxyTexImage() to check the
1913 * level, width, height and depth.
1914 */
1915 if (dimensions == 1) {
1916 if (target == GL_TEXTURE_1D) {
1917 sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_1D,
1918 level, internalFormat,
1919 format, type,
1920 width, 1, 1, border);
1921 }
1922 else {
1923 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage1D(target)" );
1924 return GL_TRUE;
1925 }
1926 }
1927 else if (dimensions == 2) {
1928 if (target == GL_TEXTURE_2D) {
1929 sizeOK = ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_2D,
1930 level, internalFormat,
1931 format, type,
1932 width, height, 1, border);
1933 }
1934 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1935 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
1936 if (!ctx->Extensions.ARB_texture_cube_map) {
1937 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
1938 return GL_TRUE;
1939 }
1940 sizeOK = (width == height) &&
1941 ctx->Driver.TestProxyTexImage(ctx, GL_PROXY_TEXTURE_CUBE_MAP_ARB,
1942 level, internalFormat, format, type,
1943 width, height, 1, border);
1944 }
1945 else if (target == GL_TEXTURE_RECTANGLE_NV) {
1946 if (!ctx->Extensions.NV_texture_rectangle) {
1947 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
1948 return GL_TRUE;
1949 }
1950 sizeOK = ctx->Driver.TestProxyTexImage(ctx,
1951 GL_PROXY_TEXTURE_RECTANGLE_NV,
1952 level, internalFormat,
1953 format, type,
1954 width, height, 1, border);
1955 }
1956 else if (target == GL_TEXTURE_1D_ARRAY_EXT) {
1957 if (!ctx->Extensions.MESA_texture_array) {
1958 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)");
1959 return GL_TRUE;
1960 }
1961 sizeOK = ctx->Driver.TestProxyTexImage(ctx,
1962 GL_PROXY_TEXTURE_1D_ARRAY_EXT,
1963 level, internalFormat,
1964 format, type,
1965 width, height, 1, border);
1966 }
1967 else {
1968 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
1969 return GL_TRUE;
1970 }
1971 }
1972 else {
1973 _mesa_problem(ctx, "invalid dimensions in copytexture_error_check");
1974 return GL_TRUE;
1975 }
1976
1977 if (!sizeOK) {
1978 if (dimensions == 1) {
1979 _mesa_error(ctx, GL_INVALID_VALUE,
1980 "glCopyTexImage1D(width=%d)", width);
1981 }
1982 else {
1983 ASSERT(dimensions == 2);
1984 _mesa_error(ctx, GL_INVALID_VALUE,
1985 "glCopyTexImage2D(width=%d, height=%d)", width, height);
1986 }
1987 return GL_TRUE;
1988 }
1989
1990 if (is_compressed_format(ctx, internalFormat)) {
1991 if (!target_can_be_compressed(ctx, target)) {
1992 _mesa_error(ctx, GL_INVALID_ENUM,
1993 "glCopyTexImage%d(target)", dimensions);
1994 return GL_TRUE;
1995 }
1996 if (border != 0) {
1997 _mesa_error(ctx, GL_INVALID_OPERATION,
1998 "glCopyTexImage%D(border!=0)", dimensions);
1999 return GL_TRUE;
2000 }
2001 }
2002 else if (is_depth_format(internalFormat)) {
2003 /* make sure we have depth/stencil buffers */
2004 if (!ctx->ReadBuffer->_DepthBuffer) {
2005 _mesa_error(ctx, GL_INVALID_OPERATION,
2006 "glCopyTexImage%D(no depth)", dimensions);
2007 return GL_TRUE;
2008 }
2009 }
2010 else if (is_depthstencil_format(internalFormat)) {
2011 /* make sure we have depth/stencil buffers */
2012 if (!ctx->ReadBuffer->_DepthBuffer || !ctx->ReadBuffer->_StencilBuffer) {
2013 _mesa_error(ctx, GL_INVALID_OPERATION,
2014 "glCopyTexImage%D(no depth/stencil buffer)", dimensions);
2015 return GL_TRUE;
2016 }
2017 }
2018
2019 /* if we get here, the parameters are OK */
2020 return GL_FALSE;
2021 }
2022
2023
2024 /**
2025 * Test glCopyTexSubImage[12]D() parameters for errors.
2026 * Note that this is the first part of error checking.
2027 * See also copytexsubimage_error_check2() below for the second part.
2028 *
2029 * \param ctx GL context.
2030 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2031 * \param target texture target given by the user.
2032 * \param level image level given by the user.
2033 *
2034 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2035 */
2036 static GLboolean
2037 copytexsubimage_error_check1( GLcontext *ctx, GLuint dimensions,
2038 GLenum target, GLint level)
2039 {
2040 /* Check that the source buffer is complete */
2041 if (ctx->ReadBuffer->Name) {
2042 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2043 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2044 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2045 "glCopyTexImage%dD(invalid readbuffer)", dimensions);
2046 return GL_TRUE;
2047 }
2048 }
2049
2050 /* Check target */
2051 if (dimensions == 1) {
2052 if (target != GL_TEXTURE_1D) {
2053 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage1D(target)" );
2054 return GL_TRUE;
2055 }
2056 }
2057 else if (dimensions == 2) {
2058 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2059 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
2060 if (!ctx->Extensions.ARB_texture_cube_map) {
2061 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
2062 return GL_TRUE;
2063 }
2064 }
2065 else if (target == GL_TEXTURE_RECTANGLE_NV) {
2066 if (!ctx->Extensions.NV_texture_rectangle) {
2067 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
2068 return GL_TRUE;
2069 }
2070 }
2071 else if (target == GL_TEXTURE_1D_ARRAY_EXT) {
2072 if (!ctx->Extensions.MESA_texture_array) {
2073 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
2074 return GL_TRUE;
2075 }
2076 }
2077 else if (target != GL_TEXTURE_2D) {
2078 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
2079 return GL_TRUE;
2080 }
2081 }
2082 else if (dimensions == 3) {
2083 if (((target != GL_TEXTURE_2D_ARRAY_EXT) ||
2084 (!ctx->Extensions.MESA_texture_array))
2085 && (target != GL_TEXTURE_3D)) {
2086 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage3D(target)" );
2087 return GL_TRUE;
2088 }
2089 }
2090
2091 /* Check level */
2092 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
2093 _mesa_error(ctx, GL_INVALID_VALUE,
2094 "glCopyTexSubImage%dD(level=%d)", dimensions, level);
2095 return GL_TRUE;
2096 }
2097
2098 return GL_FALSE;
2099 }
2100
2101
2102 /**
2103 * Second part of error checking for glCopyTexSubImage[12]D().
2104 * \param xoffset sub-image x offset given by the user.
2105 * \param yoffset sub-image y offset given by the user.
2106 * \param zoffset sub-image z offset given by the user.
2107 * \param width image width given by the user.
2108 * \param height image height given by the user.
2109 */
2110 static GLboolean
2111 copytexsubimage_error_check2( GLcontext *ctx, GLuint dimensions,
2112 GLenum target, GLint level,
2113 GLint xoffset, GLint yoffset, GLint zoffset,
2114 GLsizei width, GLsizei height,
2115 const struct gl_texture_image *teximage )
2116 {
2117 /* check that dest tex image exists */
2118 if (!teximage) {
2119 _mesa_error(ctx, GL_INVALID_OPERATION,
2120 "glCopyTexSubImage%dD(undefined texture level: %d)",
2121 dimensions, level);
2122 return GL_TRUE;
2123 }
2124
2125 /* Check size */
2126 if (width < 0) {
2127 _mesa_error(ctx, GL_INVALID_VALUE,
2128 "glCopyTexSubImage%dD(width=%d)", dimensions, width);
2129 return GL_TRUE;
2130 }
2131 if (dimensions > 1 && height < 0) {
2132 _mesa_error(ctx, GL_INVALID_VALUE,
2133 "glCopyTexSubImage%dD(height=%d)", dimensions, height);
2134 return GL_TRUE;
2135 }
2136
2137 /* check x/y offsets */
2138 if (xoffset < -((GLint)teximage->Border)) {
2139 _mesa_error(ctx, GL_INVALID_VALUE,
2140 "glCopyTexSubImage%dD(xoffset=%d)", dimensions, xoffset);
2141 return GL_TRUE;
2142 }
2143 if (xoffset + width > (GLint) (teximage->Width + teximage->Border)) {
2144 _mesa_error(ctx, GL_INVALID_VALUE,
2145 "glCopyTexSubImage%dD(xoffset+width)", dimensions);
2146 return GL_TRUE;
2147 }
2148 if (dimensions > 1) {
2149 if (yoffset < -((GLint)teximage->Border)) {
2150 _mesa_error(ctx, GL_INVALID_VALUE,
2151 "glCopyTexSubImage%dD(yoffset=%d)", dimensions, yoffset);
2152 return GL_TRUE;
2153 }
2154 /* NOTE: we're adding the border here, not subtracting! */
2155 if (yoffset + height > (GLint) (teximage->Height + teximage->Border)) {
2156 _mesa_error(ctx, GL_INVALID_VALUE,
2157 "glCopyTexSubImage%dD(yoffset+height)", dimensions);
2158 return GL_TRUE;
2159 }
2160 }
2161
2162 /* check z offset */
2163 if (dimensions > 2) {
2164 if (zoffset < -((GLint)teximage->Border)) {
2165 _mesa_error(ctx, GL_INVALID_VALUE,
2166 "glCopyTexSubImage%dD(zoffset)", dimensions);
2167 return GL_TRUE;
2168 }
2169 if (zoffset > (GLint) (teximage->Depth + teximage->Border)) {
2170 _mesa_error(ctx, GL_INVALID_VALUE,
2171 "glCopyTexSubImage%dD(zoffset+depth)", dimensions);
2172 return GL_TRUE;
2173 }
2174 }
2175
2176 if (teximage->IsCompressed) {
2177 if (!target_can_be_compressed(ctx, target)) {
2178 _mesa_error(ctx, GL_INVALID_ENUM,
2179 "glCopyTexSubImage%d(target)", dimensions);
2180 return GL_TRUE;
2181 }
2182 /* offset must be multiple of 4 */
2183 if ((xoffset & 3) || (yoffset & 3)) {
2184 _mesa_error(ctx, GL_INVALID_VALUE,
2185 "glCopyTexSubImage%D(xoffset or yoffset)", dimensions);
2186 return GL_TRUE;
2187 }
2188 /* size must be multiple of 4 */
2189 if ((width & 3) != 0 && (GLuint) width != teximage->Width) {
2190 _mesa_error(ctx, GL_INVALID_VALUE,
2191 "glCopyTexSubImage%D(width)", dimensions);
2192 return GL_TRUE;
2193 }
2194 if ((height & 3) != 0 && (GLuint) height != teximage->Height) {
2195 _mesa_error(ctx, GL_INVALID_VALUE,
2196 "glCopyTexSubImage%D(height)", dimensions);
2197 return GL_TRUE;
2198 }
2199 }
2200
2201 if (teximage->InternalFormat == GL_YCBCR_MESA) {
2202 _mesa_error(ctx, GL_INVALID_OPERATION, "glCopyTexSubImage2D");
2203 return GL_TRUE;
2204 }
2205
2206 if (!_mesa_source_buffer_exists(ctx, teximage->_BaseFormat)) {
2207 _mesa_error(ctx, GL_INVALID_OPERATION,
2208 "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
2209 dimensions, teximage->_BaseFormat);
2210 return GL_TRUE;
2211 }
2212
2213 if (teximage->_BaseFormat == GL_DEPTH_COMPONENT) {
2214 if (!ctx->ReadBuffer->_DepthBuffer) {
2215 _mesa_error(ctx, GL_INVALID_OPERATION,
2216 "glCopyTexSubImage%D(no depth buffer)",
2217 dimensions);
2218 return GL_TRUE;
2219 }
2220 }
2221 else if (teximage->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
2222 if (!ctx->ReadBuffer->_DepthBuffer || !ctx->ReadBuffer->_StencilBuffer) {
2223 _mesa_error(ctx, GL_INVALID_OPERATION,
2224 "glCopyTexSubImage%D(no depth/stencil buffer)",
2225 dimensions);
2226 return GL_TRUE;
2227 }
2228 }
2229
2230 /* if we get here, the parameters are OK */
2231 return GL_FALSE;
2232 }
2233
2234
2235 /**
2236 * Get texture image. Called by glGetTexImage.
2237 *
2238 * \param target texture target.
2239 * \param level image level.
2240 * \param format pixel data format for returned image.
2241 * \param type pixel data type for returned image.
2242 * \param pixels returned pixel data.
2243 */
2244 void GLAPIENTRY
2245 _mesa_GetTexImage( GLenum target, GLint level, GLenum format,
2246 GLenum type, GLvoid *pixels )
2247 {
2248 const struct gl_texture_unit *texUnit;
2249 struct gl_texture_object *texObj;
2250 struct gl_texture_image *texImage;
2251 GLint maxLevels = 0;
2252 GET_CURRENT_CONTEXT(ctx);
2253 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2254
2255 texUnit = &(ctx->Texture.Unit[ctx->Texture.CurrentUnit]);
2256 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2257 if (!texObj || _mesa_is_proxy_texture(target)) {
2258 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target)");
2259 return;
2260 }
2261
2262 maxLevels = _mesa_max_texture_levels(ctx, target);
2263 ASSERT(maxLevels > 0); /* 0 indicates bad target, caught above */
2264
2265 if (level < 0 || level >= maxLevels) {
2266 _mesa_error( ctx, GL_INVALID_VALUE, "glGetTexImage(level)" );
2267 return;
2268 }
2269
2270 if (_mesa_sizeof_packed_type(type) <= 0) {
2271 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexImage(type)" );
2272 return;
2273 }
2274
2275 if (_mesa_components_in_format(format) <= 0 ||
2276 format == GL_STENCIL_INDEX) {
2277 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexImage(format)" );
2278 return;
2279 }
2280
2281 if (!ctx->Extensions.EXT_paletted_texture && is_index_format(format)) {
2282 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
2283 return;
2284 }
2285
2286 if (!ctx->Extensions.ARB_depth_texture && is_depth_format(format)) {
2287 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
2288 return;
2289 }
2290
2291 if (!ctx->Extensions.MESA_ycbcr_texture && is_ycbcr_format(format)) {
2292 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
2293 return;
2294 }
2295
2296 if (!ctx->Extensions.EXT_packed_depth_stencil
2297 && is_depthstencil_format(format)) {
2298 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
2299 return;
2300 }
2301
2302 if (!ctx->Extensions.ATI_envmap_bumpmap
2303 && is_dudv_format(format)) {
2304 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
2305 return;
2306 }
2307
2308 _mesa_lock_texture(ctx, texObj);
2309 {
2310 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2311 if (!texImage) {
2312 /* invalid mipmap level, not an error */
2313 goto out;
2314 }
2315
2316
2317 /* Make sure the requested image format is compatible with the
2318 * texture's format. Note that a color index texture can be converted
2319 * to RGBA so that combo is allowed.
2320 */
2321 if (_mesa_is_color_format(format)
2322 && !_mesa_is_color_format(texImage->TexFormat->BaseFormat)
2323 && !is_index_format(texImage->TexFormat->BaseFormat)) {
2324 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
2325 goto out;
2326 }
2327 else if (is_index_format(format)
2328 && !is_index_format(texImage->TexFormat->BaseFormat)) {
2329 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
2330 goto out;
2331 }
2332 else if (is_depth_format(format)
2333 && !is_depth_format(texImage->TexFormat->BaseFormat)
2334 && !is_depthstencil_format(texImage->TexFormat->BaseFormat)) {
2335 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
2336 goto out;
2337 }
2338 else if (is_ycbcr_format(format)
2339 && !is_ycbcr_format(texImage->TexFormat->BaseFormat)) {
2340 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
2341 goto out;
2342 }
2343 else if (is_depthstencil_format(format)
2344 && !is_depthstencil_format(texImage->TexFormat->BaseFormat)) {
2345 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
2346 goto out;
2347 }
2348 else if (is_dudv_format(format)
2349 && !is_dudv_format(texImage->TexFormat->BaseFormat)) {
2350 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
2351 goto out;
2352 }
2353
2354 if (ctx->Pack.BufferObj->Name) {
2355 /* packing texture image into a PBO */
2356 const GLuint dimensions = (target == GL_TEXTURE_3D) ? 3 : 2;
2357 if (!_mesa_validate_pbo_access(dimensions, &ctx->Pack, texImage->Width,
2358 texImage->Height, texImage->Depth,
2359 format, type, pixels)) {
2360 _mesa_error(ctx, GL_INVALID_OPERATION,
2361 "glGetTexImage(invalid PBO access)");
2362 goto out;
2363 }
2364 }
2365
2366 /* typically, this will call _mesa_get_teximage() */
2367 ctx->Driver.GetTexImage(ctx, target, level, format, type, pixels,
2368 texObj, texImage);
2369
2370 }
2371 out:
2372 _mesa_unlock_texture(ctx, texObj);
2373 }
2374
2375
2376 /** Callback info for walking over FBO hash table */
2377 struct cb_info
2378 {
2379 GLcontext *ctx;
2380 struct gl_texture_object *texObj;
2381 GLuint level, face;
2382 };
2383
2384
2385 /**
2386 * Check render to texture callback. Called from _mesa_HashWalk().
2387 */
2388 static void
2389 check_rtt_cb(GLuint key, void *data, void *userData)
2390 {
2391 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
2392 const struct cb_info *info = (struct cb_info *) userData;
2393 GLcontext *ctx = info->ctx;
2394 const struct gl_texture_object *texObj = info->texObj;
2395 const GLuint level = info->level, face = info->face;
2396
2397 /* If this is a user-created FBO */
2398 if (fb->Name) {
2399 GLuint i;
2400 /* check if any of the FBO's attachments point to 'texObj' */
2401 for (i = 0; i < BUFFER_COUNT; i++) {
2402 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2403 if (att->Type == GL_TEXTURE &&
2404 att->Texture == texObj &&
2405 att->TextureLevel == level &&
2406 att->CubeMapFace == face) {
2407 ASSERT(att->Texture->Image[att->CubeMapFace][att->TextureLevel]);
2408 /* Tell driver about the new renderbuffer texture */
2409 ctx->Driver.RenderTexture(ctx, ctx->DrawBuffer, att);
2410 /* Mark fb status as indeterminate to force re-validation */
2411 fb->_Status = 0;
2412 }
2413 }
2414 }
2415 }
2416
2417
2418 /**
2419 * When a texture image is specified we have to check if it's bound to
2420 * any framebuffer objects (render to texture) in order to detect changes
2421 * in size or format since that effects FBO completeness.
2422 * Any FBOs rendering into the texture must be re-validated.
2423 */
2424 static void
2425 update_fbo_texture(GLcontext *ctx, struct gl_texture_object *texObj,
2426 GLuint face, GLuint level)
2427 {
2428 /* Only check this texture if it's been marked as RenderToTexture */
2429 if (texObj->_RenderToTexture) {
2430 struct cb_info info;
2431 info.ctx = ctx;
2432 info.texObj = texObj;
2433 info.level = level;
2434 info.face = face;
2435 _mesa_HashWalk(ctx->Shared->FrameBuffers, check_rtt_cb, &info);
2436 }
2437 }
2438
2439
2440
2441 /*
2442 * Called from the API. Note that width includes the border.
2443 */
2444 void GLAPIENTRY
2445 _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
2446 GLsizei width, GLint border, GLenum format,
2447 GLenum type, const GLvoid *pixels )
2448 {
2449 GLsizei postConvWidth = width;
2450 GET_CURRENT_CONTEXT(ctx);
2451 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2452
2453 #if FEATURE_convolve
2454 if (_mesa_is_color_format(internalFormat)) {
2455 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2456 }
2457 #endif
2458
2459 if (target == GL_TEXTURE_1D) {
2460 /* non-proxy target */
2461 struct gl_texture_unit *texUnit;
2462 struct gl_texture_object *texObj;
2463 struct gl_texture_image *texImage;
2464 const GLuint face = _mesa_tex_target_to_face(target);
2465
2466 if (texture_error_check(ctx, target, level, internalFormat,
2467 format, type, 1, postConvWidth, 1, 1, border)) {
2468 return; /* error was recorded */
2469 }
2470
2471 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2472 _mesa_update_state(ctx);
2473
2474 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2475 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2476 _mesa_lock_texture(ctx, texObj);
2477 {
2478 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2479 if (!texImage) {
2480 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage1D");
2481 goto out;
2482 }
2483
2484 if (texImage->Data) {
2485 ctx->Driver.FreeTexImageData( ctx, texImage );
2486 }
2487
2488 ASSERT(texImage->Data == NULL);
2489
2490 clear_teximage_fields(texImage); /* not really needed, but helpful */
2491 _mesa_init_teximage_fields(ctx, target, texImage,
2492 postConvWidth, 1, 1,
2493 border, internalFormat);
2494
2495 ASSERT(ctx->Driver.TexImage1D);
2496
2497 /* Give the texture to the driver! <pixels> may be null! */
2498 (*ctx->Driver.TexImage1D)(ctx, target, level, internalFormat,
2499 width, border, format, type, pixels,
2500 &ctx->Unpack, texObj, texImage);
2501
2502 ASSERT(texImage->TexFormat);
2503
2504 update_fbo_texture(ctx, texObj, face, level);
2505
2506 /* state update */
2507 texObj->_Complete = GL_FALSE;
2508 ctx->NewState |= _NEW_TEXTURE;
2509 }
2510 out:
2511 _mesa_unlock_texture(ctx, texObj);
2512 }
2513 else if (target == GL_PROXY_TEXTURE_1D) {
2514 /* Proxy texture: check for errors and update proxy state */
2515 struct gl_texture_image *texImage;
2516 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2517 if (texture_error_check(ctx, target, level, internalFormat,
2518 format, type, 1, postConvWidth, 1, 1, border)) {
2519 /* when error, clear all proxy texture image parameters */
2520 if (texImage)
2521 clear_teximage_fields(texImage);
2522 }
2523 else {
2524 /* no error, set the tex image parameters */
2525 ASSERT(texImage);
2526 _mesa_init_teximage_fields(ctx, target, texImage,
2527 postConvWidth, 1, 1,
2528 border, internalFormat);
2529 texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2530 internalFormat, format, type);
2531 }
2532 }
2533 else {
2534 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
2535 return;
2536 }
2537 }
2538
2539
2540 void GLAPIENTRY
2541 _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
2542 GLsizei width, GLsizei height, GLint border,
2543 GLenum format, GLenum type,
2544 const GLvoid *pixels )
2545 {
2546 GLsizei postConvWidth = width, postConvHeight = height;
2547 GET_CURRENT_CONTEXT(ctx);
2548 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2549
2550 #if FEATURE_convolve
2551 if (_mesa_is_color_format(internalFormat)) {
2552 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2553 &postConvHeight);
2554 }
2555 #endif
2556
2557 if (target == GL_TEXTURE_2D ||
2558 (ctx->Extensions.ARB_texture_cube_map &&
2559 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
2560 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) ||
2561 (ctx->Extensions.NV_texture_rectangle &&
2562 target == GL_TEXTURE_RECTANGLE_NV) ||
2563 (ctx->Extensions.MESA_texture_array &&
2564 target == GL_TEXTURE_1D_ARRAY_EXT)) {
2565 /* non-proxy target */
2566 struct gl_texture_unit *texUnit;
2567 struct gl_texture_object *texObj;
2568 struct gl_texture_image *texImage;
2569 const GLuint face = _mesa_tex_target_to_face(target);
2570
2571 if (texture_error_check(ctx, target, level, internalFormat,
2572 format, type, 2, postConvWidth, postConvHeight,
2573 1, border)) {
2574 return; /* error was recorded */
2575 }
2576
2577 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2578 _mesa_update_state(ctx);
2579
2580 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2581 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2582 _mesa_lock_texture(ctx, texObj);
2583 {
2584 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2585 if (!texImage) {
2586 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage2D");
2587 goto out;
2588 }
2589
2590 if (texImage->Data) {
2591 ctx->Driver.FreeTexImageData( ctx, texImage );
2592 }
2593
2594 ASSERT(texImage->Data == NULL);
2595 clear_teximage_fields(texImage); /* not really needed, but helpful */
2596 _mesa_init_teximage_fields(ctx, target, texImage,
2597 postConvWidth, postConvHeight, 1,
2598 border, internalFormat);
2599
2600 ASSERT(ctx->Driver.TexImage2D);
2601
2602 /* Give the texture to the driver! <pixels> may be null! */
2603 (*ctx->Driver.TexImage2D)(ctx, target, level, internalFormat,
2604 width, height, border, format, type, pixels,
2605 &ctx->Unpack, texObj, texImage);
2606
2607 ASSERT(texImage->TexFormat);
2608
2609 update_fbo_texture(ctx, texObj, face, level);
2610
2611 /* state update */
2612 texObj->_Complete = GL_FALSE;
2613 ctx->NewState |= _NEW_TEXTURE;
2614 }
2615 out:
2616 _mesa_unlock_texture(ctx, texObj);
2617 }
2618 else if (target == GL_PROXY_TEXTURE_2D ||
2619 (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB &&
2620 ctx->Extensions.ARB_texture_cube_map) ||
2621 (target == GL_PROXY_TEXTURE_RECTANGLE_NV &&
2622 ctx->Extensions.NV_texture_rectangle) ||
2623 (ctx->Extensions.MESA_texture_array &&
2624 target == GL_PROXY_TEXTURE_1D_ARRAY_EXT)) {
2625 /* Proxy texture: check for errors and update proxy state */
2626 struct gl_texture_image *texImage;
2627 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2628 if (texture_error_check(ctx, target, level, internalFormat,
2629 format, type, 2, postConvWidth, postConvHeight,
2630 1, border)) {
2631 /* when error, clear all proxy texture image parameters */
2632 if (texImage)
2633 clear_teximage_fields(texImage);
2634 }
2635 else {
2636 /* no error, set the tex image parameters */
2637 _mesa_init_teximage_fields(ctx, target, texImage,
2638 postConvWidth, postConvHeight, 1,
2639 border, internalFormat);
2640 texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2641 internalFormat, format, type);
2642 }
2643 }
2644 else {
2645 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage2D(target)" );
2646 return;
2647 }
2648 }
2649
2650
2651 /*
2652 * Called by the API or display list executor.
2653 * Note that width and height include the border.
2654 */
2655 void GLAPIENTRY
2656 _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
2657 GLsizei width, GLsizei height, GLsizei depth,
2658 GLint border, GLenum format, GLenum type,
2659 const GLvoid *pixels )
2660 {
2661 GET_CURRENT_CONTEXT(ctx);
2662 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2663
2664 if (target == GL_TEXTURE_3D ||
2665 (ctx->Extensions.MESA_texture_array &&
2666 target == GL_TEXTURE_2D_ARRAY_EXT)) {
2667 /* non-proxy target */
2668 struct gl_texture_unit *texUnit;
2669 struct gl_texture_object *texObj;
2670 struct gl_texture_image *texImage;
2671 const GLuint face = _mesa_tex_target_to_face(target);
2672
2673 if (texture_error_check(ctx, target, level, (GLint) internalFormat,
2674 format, type, 3, width, height, depth, border)) {
2675 return; /* error was recorded */
2676 }
2677
2678 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2679 _mesa_update_state(ctx);
2680
2681 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2682 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2683 _mesa_lock_texture(ctx, texObj);
2684 {
2685 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2686 if (!texImage) {
2687 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage3D");
2688 goto out;
2689 }
2690
2691 if (texImage->Data) {
2692 ctx->Driver.FreeTexImageData( ctx, texImage );
2693 }
2694
2695 ASSERT(texImage->Data == NULL);
2696 clear_teximage_fields(texImage); /* not really needed, but helpful */
2697 _mesa_init_teximage_fields(ctx, target, texImage,
2698 width, height, depth,
2699 border, internalFormat);
2700
2701 ASSERT(ctx->Driver.TexImage3D);
2702
2703 /* Give the texture to the driver! <pixels> may be null! */
2704 (*ctx->Driver.TexImage3D)(ctx, target, level, internalFormat,
2705 width, height, depth, border, format, type,
2706 pixels, &ctx->Unpack, texObj, texImage);
2707
2708 ASSERT(texImage->TexFormat);
2709
2710 update_fbo_texture(ctx, texObj, face, level);
2711
2712 /* state update */
2713 texObj->_Complete = GL_FALSE;
2714 ctx->NewState |= _NEW_TEXTURE;
2715 }
2716 out:
2717 _mesa_unlock_texture(ctx, texObj);
2718 }
2719 else if (target == GL_PROXY_TEXTURE_3D ||
2720 (ctx->Extensions.MESA_texture_array &&
2721 target == GL_PROXY_TEXTURE_2D_ARRAY_EXT)) {
2722 /* Proxy texture: check for errors and update proxy state */
2723 struct gl_texture_image *texImage;
2724 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
2725 if (texture_error_check(ctx, target, level, internalFormat,
2726 format, type, 3, width, height, depth, border)) {
2727 /* when error, clear all proxy texture image parameters */
2728 if (texImage)
2729 clear_teximage_fields(texImage);
2730 }
2731 else {
2732 /* no error, set the tex image parameters */
2733 _mesa_init_teximage_fields(ctx, target, texImage, width, height,
2734 depth, border, internalFormat);
2735 texImage->TexFormat = (*ctx->Driver.ChooseTextureFormat)(ctx,
2736 internalFormat, format, type);
2737 }
2738 }
2739 else {
2740 _mesa_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
2741 return;
2742 }
2743 }
2744
2745
2746 void GLAPIENTRY
2747 _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
2748 GLsizei width, GLsizei height, GLsizei depth,
2749 GLint border, GLenum format, GLenum type,
2750 const GLvoid *pixels )
2751 {
2752 _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
2753 depth, border, format, type, pixels);
2754 }
2755
2756
2757
2758 void GLAPIENTRY
2759 _mesa_TexSubImage1D( GLenum target, GLint level,
2760 GLint xoffset, GLsizei width,
2761 GLenum format, GLenum type,
2762 const GLvoid *pixels )
2763 {
2764 GLsizei postConvWidth = width;
2765 struct gl_texture_unit *texUnit;
2766 struct gl_texture_object *texObj;
2767 struct gl_texture_image *texImage = NULL;
2768 GET_CURRENT_CONTEXT(ctx);
2769 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2770
2771 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2772 _mesa_update_state(ctx);
2773
2774 #if FEATURE_convolve
2775 /* XXX should test internal format */
2776 if (_mesa_is_color_format(format)) {
2777 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2778 }
2779 #endif
2780
2781 if (subtexture_error_check(ctx, 1, target, level, xoffset, 0, 0,
2782 postConvWidth, 1, 1, format, type)) {
2783 return; /* error was detected */
2784 }
2785
2786
2787 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2788 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2789 assert(texObj);
2790
2791 _mesa_lock_texture(ctx, texObj);
2792 {
2793 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2794
2795 if (subtexture_error_check2(ctx, 1, target, level, xoffset, 0, 0,
2796 postConvWidth, 1, 1, format, type, texImage)) {
2797 goto out; /* error was detected */
2798 }
2799
2800 if (width == 0)
2801 goto out; /* no-op, not an error */
2802
2803 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2804 xoffset += texImage->Border;
2805
2806 ASSERT(ctx->Driver.TexSubImage1D);
2807 (*ctx->Driver.TexSubImage1D)(ctx, target, level, xoffset, width,
2808 format, type, pixels, &ctx->Unpack,
2809 texObj, texImage);
2810 ctx->NewState |= _NEW_TEXTURE;
2811 }
2812 out:
2813 _mesa_unlock_texture(ctx, texObj);
2814 }
2815
2816
2817 void GLAPIENTRY
2818 _mesa_TexSubImage2D( GLenum target, GLint level,
2819 GLint xoffset, GLint yoffset,
2820 GLsizei width, GLsizei height,
2821 GLenum format, GLenum type,
2822 const GLvoid *pixels )
2823 {
2824 GLsizei postConvWidth = width, postConvHeight = height;
2825 struct gl_texture_unit *texUnit;
2826 struct gl_texture_object *texObj;
2827 struct gl_texture_image *texImage;
2828 GET_CURRENT_CONTEXT(ctx);
2829 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2830
2831 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2832 _mesa_update_state(ctx);
2833
2834 #if FEATURE_convolve
2835 /* XXX should test internal format */
2836 if (_mesa_is_color_format(format)) {
2837 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
2838 &postConvHeight);
2839 }
2840 #endif
2841
2842 if (subtexture_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
2843 postConvWidth, postConvHeight, 1, format, type)) {
2844 return; /* error was detected */
2845 }
2846
2847 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2848 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2849 _mesa_lock_texture(ctx, texObj);
2850 {
2851 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2852
2853 if (subtexture_error_check2(ctx, 2, target, level, xoffset, yoffset, 0,
2854 postConvWidth, postConvHeight, 1, format, type,
2855 texImage)) {
2856 goto out; /* error was detected */
2857 }
2858
2859 if (width == 0 || height == 0)
2860 goto out; /* no-op, not an error */
2861
2862 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2863 xoffset += texImage->Border;
2864 yoffset += texImage->Border;
2865
2866 ASSERT(ctx->Driver.TexSubImage2D);
2867 (*ctx->Driver.TexSubImage2D)(ctx, target, level, xoffset, yoffset,
2868 width, height, format, type, pixels,
2869 &ctx->Unpack, texObj, texImage);
2870 ctx->NewState |= _NEW_TEXTURE;
2871 }
2872 out:
2873 _mesa_unlock_texture(ctx, texObj);
2874 }
2875
2876
2877
2878 void GLAPIENTRY
2879 _mesa_TexSubImage3D( GLenum target, GLint level,
2880 GLint xoffset, GLint yoffset, GLint zoffset,
2881 GLsizei width, GLsizei height, GLsizei depth,
2882 GLenum format, GLenum type,
2883 const GLvoid *pixels )
2884 {
2885 struct gl_texture_unit *texUnit;
2886 struct gl_texture_object *texObj;
2887 struct gl_texture_image *texImage;
2888 GET_CURRENT_CONTEXT(ctx);
2889 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2890
2891 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2892 _mesa_update_state(ctx);
2893
2894 if (subtexture_error_check(ctx, 3, target, level, xoffset, yoffset, zoffset,
2895 width, height, depth, format, type)) {
2896 return; /* error was detected */
2897 }
2898
2899 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2900 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2901
2902 _mesa_lock_texture(ctx, texObj);
2903 {
2904 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2905
2906 if (subtexture_error_check2(ctx, 3, target, level, xoffset, yoffset, zoffset,
2907 width, height, depth, format, type, texImage)) {
2908 goto out; /* error was detected */
2909 }
2910
2911 if (width == 0 || height == 0 || height == 0)
2912 goto out; /* no-op, not an error */
2913
2914 /* If we have a border, xoffset=-1 is legal. Bias by border width */
2915 xoffset += texImage->Border;
2916 yoffset += texImage->Border;
2917 zoffset += texImage->Border;
2918
2919 ASSERT(ctx->Driver.TexSubImage3D);
2920 (*ctx->Driver.TexSubImage3D)(ctx, target, level,
2921 xoffset, yoffset, zoffset,
2922 width, height, depth,
2923 format, type, pixels,
2924 &ctx->Unpack, texObj, texImage );
2925 ctx->NewState |= _NEW_TEXTURE;
2926 }
2927 out:
2928 _mesa_unlock_texture(ctx, texObj);
2929 }
2930
2931
2932
2933 void GLAPIENTRY
2934 _mesa_CopyTexImage1D( GLenum target, GLint level,
2935 GLenum internalFormat,
2936 GLint x, GLint y,
2937 GLsizei width, GLint border )
2938 {
2939 struct gl_texture_unit *texUnit;
2940 struct gl_texture_object *texObj;
2941 struct gl_texture_image *texImage;
2942 GLsizei postConvWidth = width;
2943 const GLuint face = _mesa_tex_target_to_face(target);
2944 GET_CURRENT_CONTEXT(ctx);
2945 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2946
2947 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
2948 _mesa_update_state(ctx);
2949
2950 #if FEATURE_convolve
2951 if (_mesa_is_color_format(internalFormat)) {
2952 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
2953 }
2954 #endif
2955
2956 if (copytexture_error_check(ctx, 1, target, level, internalFormat,
2957 postConvWidth, 1, border))
2958 return;
2959
2960 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2961 texObj = _mesa_select_tex_object(ctx, texUnit, target);
2962 _mesa_lock_texture(ctx, texObj);
2963 {
2964 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2965 if (!texImage) {
2966 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage1D");
2967 goto out;
2968 }
2969
2970 if (texImage->Data) {
2971 ctx->Driver.FreeTexImageData( ctx, texImage );
2972 }
2973
2974 ASSERT(texImage->Data == NULL);
2975
2976 clear_teximage_fields(texImage); /* not really needed, but helpful */
2977 _mesa_init_teximage_fields(ctx, target, texImage, postConvWidth, 1, 1,
2978 border, internalFormat);
2979
2980
2981 ASSERT(ctx->Driver.CopyTexImage1D);
2982 (*ctx->Driver.CopyTexImage1D)(ctx, target, level, internalFormat,
2983 x, y, width, border);
2984
2985 ASSERT(texImage->TexFormat);
2986
2987 update_fbo_texture(ctx, texObj, face, level);
2988
2989 /* state update */
2990 texObj->_Complete = GL_FALSE;
2991 ctx->NewState |= _NEW_TEXTURE;
2992 }
2993 out:
2994 _mesa_unlock_texture(ctx, texObj);
2995 }
2996
2997
2998
2999 void GLAPIENTRY
3000 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
3001 GLint x, GLint y, GLsizei width, GLsizei height,
3002 GLint border )
3003 {
3004 struct gl_texture_unit *texUnit;
3005 struct gl_texture_object *texObj;
3006 struct gl_texture_image *texImage;
3007 GLsizei postConvWidth = width, postConvHeight = height;
3008 const GLuint face = _mesa_tex_target_to_face(target);
3009 GET_CURRENT_CONTEXT(ctx);
3010 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3011
3012 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
3013 _mesa_update_state(ctx);
3014
3015 #if FEATURE_convolve
3016 if (_mesa_is_color_format(internalFormat)) {
3017 _mesa_adjust_image_for_convolution(ctx, 2, &postConvWidth,
3018 &postConvHeight);
3019 }
3020 #endif
3021
3022 if (copytexture_error_check(ctx, 2, target, level, internalFormat,
3023 postConvWidth, postConvHeight, border))
3024 return;
3025
3026 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3027 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3028
3029 _mesa_lock_texture(ctx, texObj);
3030 {
3031 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3032
3033 if (!texImage) {
3034 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage2D");
3035 goto out;
3036 }
3037
3038 if (texImage->Data) {
3039 ctx->Driver.FreeTexImageData( ctx, texImage );
3040 }
3041
3042 ASSERT(texImage->Data == NULL);
3043
3044 clear_teximage_fields(texImage); /* not really needed, but helpful */
3045 _mesa_init_teximage_fields(ctx, target, texImage,
3046 postConvWidth, postConvHeight, 1,
3047 border, internalFormat);
3048
3049 ASSERT(ctx->Driver.CopyTexImage2D);
3050 (*ctx->Driver.CopyTexImage2D)(ctx, target, level, internalFormat,
3051 x, y, width, height, border);
3052
3053 ASSERT(texImage->TexFormat);
3054
3055 update_fbo_texture(ctx, texObj, face, level);
3056
3057 /* state update */
3058 texObj->_Complete = GL_FALSE;
3059 ctx->NewState |= _NEW_TEXTURE;
3060 }
3061 out:
3062 _mesa_unlock_texture(ctx, texObj);
3063 }
3064
3065
3066 void GLAPIENTRY
3067 _mesa_CopyTexSubImage1D( GLenum target, GLint level,
3068 GLint xoffset, GLint x, GLint y, GLsizei width )
3069 {
3070 struct gl_texture_unit *texUnit;
3071 struct gl_texture_object *texObj;
3072 struct gl_texture_image *texImage;
3073 GLsizei postConvWidth = width;
3074 GLint yoffset = 0;
3075 GLsizei height = 1;
3076
3077 GET_CURRENT_CONTEXT(ctx);
3078 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3079
3080 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
3081 _mesa_update_state(ctx);
3082
3083 if (copytexsubimage_error_check1(ctx, 1, target, level))
3084 return;
3085
3086 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3087 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3088
3089 _mesa_lock_texture(ctx, texObj);
3090 {
3091 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3092
3093 #if FEATURE_convolve
3094 if (texImage && _mesa_is_color_format(texImage->InternalFormat)) {
3095 _mesa_adjust_image_for_convolution(ctx, 1, &postConvWidth, NULL);
3096 }
3097 #endif
3098
3099 if (copytexsubimage_error_check2(ctx, 1, target, level,
3100 xoffset, 0, 0, postConvWidth, 1,
3101 texImage))
3102 goto out;
3103
3104
3105 /* If we have a border, xoffset=-1 is legal. Bias by border width */
3106 xoffset += texImage->Border;
3107
3108 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
3109 &width, &height)) {
3110 ASSERT(ctx->Driver.CopyTexSubImage1D);
3111 ctx->Driver.CopyTexSubImage1D(ctx, target, level,
3112 xoffset, x, y, width);
3113 }
3114
3115 ctx->NewState |= _NEW_TEXTURE;
3116 }
3117 out:
3118 _mesa_unlock_texture(ctx, texObj);
3119 }
3120
3121
3122
3123 void GLAPIENTRY
3124 _mesa_CopyTexSubImage2D( GLenum target, GLint level,
3125 GLint xoffset, GLint yoffset,
3126 GLint x, GLint y, GLsizei width, GLsizei height )
3127 {
3128 struct gl_texture_unit *texUnit;
3129 struct gl_texture_object *texObj;
3130 struct gl_texture_image *texImage;
3131 GLsizei postConvWidth = width, postConvHeight = height;
3132 GET_CURRENT_CONTEXT(ctx);
3133 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3134
3135 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
3136 _mesa_update_state(ctx);
3137
3138 if (copytexsubimage_error_check1(ctx, 2, target, level))
3139 return;
3140
3141 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3142 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3143
3144 _mesa_lock_texture(ctx, texObj);
3145 {
3146 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3147
3148 #if FEATURE_convolve
3149 if (texImage && _mesa_is_color_format(texImage->InternalFormat)) {
3150 _mesa_adjust_image_for_convolution(ctx, 2,
3151 &postConvWidth, &postConvHeight);
3152 }
3153 #endif
3154
3155 if (copytexsubimage_error_check2(ctx, 2, target, level, xoffset, yoffset, 0,
3156 postConvWidth, postConvHeight, texImage))
3157 goto out;
3158
3159 /* If we have a border, xoffset=-1 is legal. Bias by border width */
3160 xoffset += texImage->Border;
3161 yoffset += texImage->Border;
3162
3163 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
3164 &width, &height)) {
3165 ASSERT(ctx->Driver.CopyTexSubImage2D);
3166 ctx->Driver.CopyTexSubImage2D(ctx, target, level,
3167 xoffset, yoffset, x, y, width, height);
3168 }
3169
3170 ctx->NewState |= _NEW_TEXTURE;
3171 }
3172 out:
3173 _mesa_unlock_texture(ctx, texObj);
3174 }
3175
3176
3177
3178 void GLAPIENTRY
3179 _mesa_CopyTexSubImage3D( GLenum target, GLint level,
3180 GLint xoffset, GLint yoffset, GLint zoffset,
3181 GLint x, GLint y, GLsizei width, GLsizei height )
3182 {
3183 struct gl_texture_unit *texUnit;
3184 struct gl_texture_object *texObj;
3185 struct gl_texture_image *texImage;
3186 GLsizei postConvWidth = width, postConvHeight = height;
3187 GET_CURRENT_CONTEXT(ctx);
3188 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3189
3190 if (ctx->NewState & _MESA_NEW_TRANSFER_STATE)
3191 _mesa_update_state(ctx);
3192
3193 if (copytexsubimage_error_check1(ctx, 3, target, level))
3194 return;
3195
3196 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3197 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3198
3199 _mesa_lock_texture(ctx, texObj);
3200 {
3201 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3202
3203 #if FEATURE_convolve
3204 if (texImage && _mesa_is_color_format(texImage->InternalFormat)) {
3205 _mesa_adjust_image_for_convolution(ctx, 2,
3206 &postConvWidth, &postConvHeight);
3207 }
3208 #endif
3209
3210 if (copytexsubimage_error_check2(ctx, 3, target, level, xoffset, yoffset,
3211 zoffset, postConvWidth, postConvHeight,
3212 texImage))
3213 goto out;
3214
3215 /* If we have a border, xoffset=-1 is legal. Bias by border width */
3216 xoffset += texImage->Border;
3217 yoffset += texImage->Border;
3218 zoffset += texImage->Border;
3219
3220 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
3221 &width, &height)) {
3222 ASSERT(ctx->Driver.CopyTexSubImage3D);
3223 ctx->Driver.CopyTexSubImage3D(ctx, target, level,
3224 xoffset, yoffset, zoffset,
3225 x, y, width, height);
3226 }
3227
3228 ctx->NewState |= _NEW_TEXTURE;
3229 }
3230 out:
3231 _mesa_unlock_texture(ctx, texObj);
3232 }
3233
3234
3235
3236
3237 /**********************************************************************/
3238 /****** Compressed Textures ******/
3239 /**********************************************************************/
3240
3241
3242 /**
3243 * Error checking for glCompressedTexImage[123]D().
3244 * \return error code or GL_NO_ERROR.
3245 */
3246 static GLenum
3247 compressed_texture_error_check(GLcontext *ctx, GLint dimensions,
3248 GLenum target, GLint level,
3249 GLenum internalFormat, GLsizei width,
3250 GLsizei height, GLsizei depth, GLint border,
3251 GLsizei imageSize)
3252 {
3253 GLint expectedSize, maxLevels = 0, maxTextureSize;
3254
3255 if (dimensions == 1) {
3256 /* 1D compressed textures not allowed */
3257 return GL_INVALID_ENUM;
3258 }
3259 else if (dimensions == 2) {
3260 if (target == GL_PROXY_TEXTURE_2D) {
3261 maxLevels = ctx->Const.MaxTextureLevels;
3262 }
3263 else if (target == GL_TEXTURE_2D) {
3264 maxLevels = ctx->Const.MaxTextureLevels;
3265 }
3266 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
3267 if (!ctx->Extensions.ARB_texture_cube_map)
3268 return GL_INVALID_ENUM; /*target*/
3269 maxLevels = ctx->Const.MaxCubeTextureLevels;
3270 }
3271 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3272 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
3273 if (!ctx->Extensions.ARB_texture_cube_map)
3274 return GL_INVALID_ENUM; /*target*/
3275 maxLevels = ctx->Const.MaxCubeTextureLevels;
3276 }
3277 else {
3278 return GL_INVALID_ENUM; /*target*/
3279 }
3280 }
3281 else if (dimensions == 3) {
3282 /* 3D compressed textures not allowed */
3283 return GL_INVALID_ENUM;
3284 }
3285
3286 maxTextureSize = 1 << (maxLevels - 1);
3287
3288 /* This will detect any invalid internalFormat value */
3289 if (!is_compressed_format(ctx, internalFormat))
3290 return GL_INVALID_ENUM;
3291
3292 /* This should really never fail */
3293 if (_mesa_base_tex_format(ctx, internalFormat) < 0)
3294 return GL_INVALID_ENUM;
3295
3296 if (border != 0)
3297 return GL_INVALID_VALUE;
3298
3299 /*
3300 * XXX We should probably use the proxy texture error check function here.
3301 */
3302 if (width < 1 || width > maxTextureSize ||
3303 (!ctx->Extensions.ARB_texture_non_power_of_two && !_mesa_is_pow_two(width)))
3304 return GL_INVALID_VALUE;
3305
3306 if ((height < 1 || height > maxTextureSize ||
3307 (!ctx->Extensions.ARB_texture_non_power_of_two && !_mesa_is_pow_two(height)))
3308 && dimensions > 1)
3309 return GL_INVALID_VALUE;
3310
3311 if ((depth < 1 || depth > maxTextureSize ||
3312 (!ctx->Extensions.ARB_texture_non_power_of_two && !_mesa_is_pow_two(depth)))
3313 && dimensions > 2)
3314 return GL_INVALID_VALUE;
3315
3316 /* For cube map, width must equal height */
3317 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3318 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB && width != height)
3319 return GL_INVALID_VALUE;
3320
3321 if (level < 0 || level >= maxLevels)
3322 return GL_INVALID_VALUE;
3323
3324 expectedSize = _mesa_compressed_texture_size_glenum(ctx, width, height,
3325 depth, internalFormat);
3326 if (expectedSize != imageSize)
3327 return GL_INVALID_VALUE;
3328
3329 #if FEATURE_EXT_texture_sRGB
3330 if ((internalFormat == GL_COMPRESSED_SRGB_S3TC_DXT1_EXT ||
3331 internalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT ||
3332 internalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT ||
3333 internalFormat == GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT)
3334 && border != 0) {
3335 return GL_INVALID_OPERATION;
3336 }
3337 #endif
3338
3339 return GL_NO_ERROR;
3340 }
3341
3342
3343 /**
3344 * Error checking for glCompressedTexSubImage[123]D().
3345 * \warning There are some bad assumptions here about the size of compressed
3346 * texture tiles (multiple of 4) used to test the validity of the
3347 * offset and size parameters.
3348 * \return error code or GL_NO_ERROR.
3349 */
3350 static GLenum
3351 compressed_subtexture_error_check(GLcontext *ctx, GLint dimensions,
3352 GLenum target, GLint level,
3353 GLint xoffset, GLint yoffset, GLint zoffset,
3354 GLsizei width, GLsizei height, GLsizei depth,
3355 GLenum format, GLsizei imageSize)
3356 {
3357 GLint expectedSize, maxLevels = 0, maxTextureSize;
3358 (void) zoffset;
3359
3360 if (dimensions == 1) {
3361 /* 1D compressed textures not allowed */
3362 return GL_INVALID_ENUM;
3363 }
3364 else if (dimensions == 2) {
3365 if (target == GL_PROXY_TEXTURE_2D) {
3366 maxLevels = ctx->Const.MaxTextureLevels;
3367 }
3368 else if (target == GL_TEXTURE_2D) {
3369 maxLevels = ctx->Const.MaxTextureLevels;
3370 }
3371 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
3372 if (!ctx->Extensions.ARB_texture_cube_map)
3373 return GL_INVALID_ENUM; /*target*/
3374 maxLevels = ctx->Const.MaxCubeTextureLevels;
3375 }
3376 else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3377 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
3378 if (!ctx->Extensions.ARB_texture_cube_map)
3379 return GL_INVALID_ENUM; /*target*/
3380 maxLevels = ctx->Const.MaxCubeTextureLevels;
3381 }
3382 else {
3383 return GL_INVALID_ENUM; /*target*/
3384 }
3385 }
3386 else if (dimensions == 3) {
3387 /* 3D compressed textures not allowed */
3388 return GL_INVALID_ENUM;
3389 }
3390
3391 maxTextureSize = 1 << (maxLevels - 1);
3392
3393 /* this will catch any invalid compressed format token */
3394 if (!is_compressed_format(ctx, format))
3395 return GL_INVALID_ENUM;
3396
3397 if (width < 1 || width > maxTextureSize)
3398 return GL_INVALID_VALUE;
3399
3400 if ((height < 1 || height > maxTextureSize)
3401 && dimensions > 1)
3402 return GL_INVALID_VALUE;
3403
3404 if (level < 0 || level >= maxLevels)
3405 return GL_INVALID_VALUE;
3406
3407 /* XXX these tests are specific to the compressed format.
3408 * this code should be generalized in some way.
3409 */
3410 if ((xoffset & 3) != 0 || (yoffset & 3) != 0)
3411 return GL_INVALID_VALUE;
3412
3413 if ((width & 3) != 0 && width != 2 && width != 1)
3414 return GL_INVALID_VALUE;
3415
3416 if ((height & 3) != 0 && height != 2 && height != 1)
3417 return GL_INVALID_VALUE;
3418
3419 expectedSize = _mesa_compressed_texture_size_glenum(ctx, width, height,
3420 depth, format);
3421 if (expectedSize != imageSize)
3422 return GL_INVALID_VALUE;
3423
3424 return GL_NO_ERROR;
3425 }
3426
3427
3428
3429 void GLAPIENTRY
3430 _mesa_CompressedTexImage1DARB(GLenum target, GLint level,
3431 GLenum internalFormat, GLsizei width,
3432 GLint border, GLsizei imageSize,
3433 const GLvoid *data)
3434 {
3435 GET_CURRENT_CONTEXT(ctx);
3436 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3437
3438 if (target == GL_TEXTURE_1D) {
3439 /* non-proxy target */
3440 struct gl_texture_unit *texUnit;
3441 struct gl_texture_object *texObj;
3442 struct gl_texture_image *texImage;
3443 GLenum error = compressed_texture_error_check(ctx, 1, target, level,
3444 internalFormat, width, 1, 1, border, imageSize);
3445 if (error) {
3446 _mesa_error(ctx, error, "glCompressedTexImage1D");
3447 return;
3448 }
3449
3450 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3451 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3452
3453 _mesa_lock_texture(ctx, texObj);
3454 {
3455 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3456 if (!texImage) {
3457 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage1D");
3458 goto out;
3459 }
3460
3461 if (texImage->Data) {
3462 ctx->Driver.FreeTexImageData( ctx, texImage );
3463 }
3464 ASSERT(texImage->Data == NULL);
3465
3466 _mesa_init_teximage_fields(ctx, target, texImage, width, 1, 1,
3467 border, internalFormat);
3468
3469 ASSERT(ctx->Driver.CompressedTexImage1D);
3470 (*ctx->Driver.CompressedTexImage1D)(ctx, target, level,
3471 internalFormat, width, border,
3472 imageSize, data,
3473 texObj, texImage);
3474
3475 /* state update */
3476 texObj->_Complete = GL_FALSE;
3477 ctx->NewState |= _NEW_TEXTURE;
3478 }
3479 out:
3480 _mesa_unlock_texture(ctx, texObj);
3481 }
3482 else if (target == GL_PROXY_TEXTURE_1D) {
3483 /* Proxy texture: check for errors and update proxy state */
3484 GLenum error = compressed_texture_error_check(ctx, 1, target, level,
3485 internalFormat, width, 1, 1, border, imageSize);
3486 if (!error) {
3487 ASSERT(ctx->Driver.TestProxyTexImage);
3488 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3489 internalFormat, GL_NONE, GL_NONE,
3490 width, 1, 1, border);
3491 }
3492 if (error) {
3493 /* if error, clear all proxy texture image parameters */
3494 struct gl_texture_image *texImage;
3495 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3496 if (texImage)
3497 clear_teximage_fields(texImage);
3498 }
3499 else {
3500 /* store the teximage parameters */
3501 struct gl_texture_unit *texUnit;
3502 struct gl_texture_object *texObj;
3503 struct gl_texture_image *texImage;
3504 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3505 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3506
3507 _mesa_lock_texture(ctx, texObj);
3508 {
3509 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3510 _mesa_init_teximage_fields(ctx, target, texImage, width, 1, 1,
3511 border, internalFormat);
3512 }
3513 _mesa_unlock_texture(ctx, texObj);
3514 }
3515 }
3516 else {
3517 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage1D(target)");
3518 return;
3519 }
3520 }
3521
3522
3523 void GLAPIENTRY
3524 _mesa_CompressedTexImage2DARB(GLenum target, GLint level,
3525 GLenum internalFormat, GLsizei width,
3526 GLsizei height, GLint border, GLsizei imageSize,
3527 const GLvoid *data)
3528 {
3529 GET_CURRENT_CONTEXT(ctx);
3530 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3531
3532 if (target == GL_TEXTURE_2D ||
3533 (ctx->Extensions.ARB_texture_cube_map &&
3534 target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
3535 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
3536 /* non-proxy target */
3537 struct gl_texture_unit *texUnit;
3538 struct gl_texture_object *texObj;
3539 struct gl_texture_image *texImage;
3540 GLenum error = compressed_texture_error_check(ctx, 2, target, level,
3541 internalFormat, width, height, 1, border, imageSize);
3542 if (error) {
3543 _mesa_error(ctx, error, "glCompressedTexImage2D");
3544 return;
3545 }
3546
3547 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3548 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3549
3550 _mesa_lock_texture(ctx, texObj);
3551 {
3552 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3553 if (!texImage) {
3554 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage2D");
3555 goto out;
3556 }
3557
3558 if (texImage->Data) {
3559 ctx->Driver.FreeTexImageData( ctx, texImage );
3560 }
3561 ASSERT(texImage->Data == NULL);
3562
3563 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
3564 border, internalFormat);
3565
3566 ASSERT(ctx->Driver.CompressedTexImage2D);
3567 (*ctx->Driver.CompressedTexImage2D)(ctx, target, level,
3568 internalFormat, width, height,
3569 border, imageSize, data,
3570 texObj, texImage);
3571
3572 /* state update */
3573 texObj->_Complete = GL_FALSE;
3574 ctx->NewState |= _NEW_TEXTURE;
3575 }
3576 out:
3577 _mesa_unlock_texture(ctx, texObj);
3578 }
3579 else if (target == GL_PROXY_TEXTURE_2D ||
3580 (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB &&
3581 ctx->Extensions.ARB_texture_cube_map)) {
3582 /* Proxy texture: check for errors and update proxy state */
3583 GLenum error = compressed_texture_error_check(ctx, 2, target, level,
3584 internalFormat, width, height, 1, border, imageSize);
3585 if (!error) {
3586 ASSERT(ctx->Driver.TestProxyTexImage);
3587 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3588 internalFormat, GL_NONE, GL_NONE,
3589 width, height, 1, border);
3590 }
3591 if (error) {
3592 /* if error, clear all proxy texture image parameters */
3593 struct gl_texture_image *texImage;
3594 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3595 if (texImage)
3596 clear_teximage_fields(texImage);
3597 }
3598 else {
3599 /* store the teximage parameters */
3600 struct gl_texture_unit *texUnit;
3601 struct gl_texture_object *texObj;
3602 struct gl_texture_image *texImage;
3603 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3604 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3605
3606 _mesa_lock_texture(ctx, texObj);
3607 {
3608 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3609 _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
3610 border, internalFormat);
3611 }
3612 _mesa_unlock_texture(ctx, texObj);
3613 }
3614 }
3615 else {
3616 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage2D(target)");
3617 return;
3618 }
3619 }
3620
3621
3622 void GLAPIENTRY
3623 _mesa_CompressedTexImage3DARB(GLenum target, GLint level,
3624 GLenum internalFormat, GLsizei width,
3625 GLsizei height, GLsizei depth, GLint border,
3626 GLsizei imageSize, const GLvoid *data)
3627 {
3628 GET_CURRENT_CONTEXT(ctx);
3629 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3630
3631 if (target == GL_TEXTURE_3D) {
3632 /* non-proxy target */
3633 struct gl_texture_unit *texUnit;
3634 struct gl_texture_object *texObj;
3635 struct gl_texture_image *texImage;
3636 GLenum error = compressed_texture_error_check(ctx, 3, target, level,
3637 internalFormat, width, height, depth, border, imageSize);
3638 if (error) {
3639 _mesa_error(ctx, error, "glCompressedTexImage3D");
3640 return;
3641 }
3642
3643 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3644 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3645 _mesa_lock_texture(ctx, texObj);
3646 {
3647 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3648 if (!texImage) {
3649 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage3D");
3650 goto out;
3651 }
3652
3653 if (texImage->Data) {
3654 ctx->Driver.FreeTexImageData( ctx, texImage );
3655 }
3656 ASSERT(texImage->Data == NULL);
3657
3658 _mesa_init_teximage_fields(ctx, target, texImage, width, height, depth,
3659 border, internalFormat);
3660
3661 ASSERT(ctx->Driver.CompressedTexImage3D);
3662 (*ctx->Driver.CompressedTexImage3D)(ctx, target, level,
3663 internalFormat,
3664 width, height, depth,
3665 border, imageSize, data,
3666 texObj, texImage);
3667
3668 /* state update */
3669 texObj->_Complete = GL_FALSE;
3670 ctx->NewState |= _NEW_TEXTURE;
3671 }
3672 out:
3673 _mesa_unlock_texture(ctx, texObj);
3674 }
3675 else if (target == GL_PROXY_TEXTURE_3D) {
3676 /* Proxy texture: check for errors and update proxy state */
3677 GLenum error = compressed_texture_error_check(ctx, 3, target, level,
3678 internalFormat, width, height, depth, border, imageSize);
3679 if (!error) {
3680 ASSERT(ctx->Driver.TestProxyTexImage);
3681 error = !(*ctx->Driver.TestProxyTexImage)(ctx, target, level,
3682 internalFormat, GL_NONE, GL_NONE,
3683 width, height, depth, border);
3684 }
3685 if (error) {
3686 /* if error, clear all proxy texture image parameters */
3687 struct gl_texture_image *texImage;
3688 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3689 if (texImage)
3690 clear_teximage_fields(texImage);
3691 }
3692 else {
3693 /* store the teximage parameters */
3694 struct gl_texture_unit *texUnit;
3695 struct gl_texture_object *texObj;
3696 struct gl_texture_image *texImage;
3697 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3698 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3699 _mesa_lock_texture(ctx, texObj);
3700 {
3701 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3702 _mesa_init_teximage_fields(ctx, target, texImage, width, height,
3703 depth, border, internalFormat);
3704 }
3705 _mesa_unlock_texture(ctx, texObj);
3706 }
3707 }
3708 else {
3709 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage3D(target)");
3710 return;
3711 }
3712 }
3713
3714
3715 void GLAPIENTRY
3716 _mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
3717 GLsizei width, GLenum format,
3718 GLsizei imageSize, const GLvoid *data)
3719 {
3720 struct gl_texture_unit *texUnit;
3721 struct gl_texture_object *texObj;
3722 struct gl_texture_image *texImage;
3723 GLenum error;
3724 GET_CURRENT_CONTEXT(ctx);
3725 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3726
3727 error = compressed_subtexture_error_check(ctx, 1, target, level,
3728 xoffset, 0, 0, /* pos */
3729 width, 1, 1, /* size */
3730 format, imageSize);
3731 if (error) {
3732 _mesa_error(ctx, error, "glCompressedTexSubImage1D");
3733 return;
3734 }
3735
3736 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3737 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3738 _mesa_lock_texture(ctx, texObj);
3739 {
3740 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3741 assert(texImage);
3742
3743 if ((GLint) format != texImage->InternalFormat) {
3744 _mesa_error(ctx, GL_INVALID_OPERATION,
3745 "glCompressedTexSubImage1D(format)");
3746 goto out;
3747 }
3748
3749 if ((width == 1 || width == 2) && (GLuint) width != texImage->Width) {
3750 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage1D(width)");
3751 goto out;
3752 }
3753
3754 if (width == 0)
3755 goto out; /* no-op, not an error */
3756
3757 if (ctx->Driver.CompressedTexSubImage1D) {
3758 (*ctx->Driver.CompressedTexSubImage1D)(ctx, target, level,
3759 xoffset, width,
3760 format, imageSize, data,
3761 texObj, texImage);
3762 }
3763 ctx->NewState |= _NEW_TEXTURE;
3764 }
3765 out:
3766 _mesa_unlock_texture(ctx, texObj);
3767 }
3768
3769
3770 void GLAPIENTRY
3771 _mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
3772 GLint yoffset, GLsizei width, GLsizei height,
3773 GLenum format, GLsizei imageSize,
3774 const GLvoid *data)
3775 {
3776 struct gl_texture_unit *texUnit;
3777 struct gl_texture_object *texObj;
3778 struct gl_texture_image *texImage;
3779 GLenum error;
3780 GET_CURRENT_CONTEXT(ctx);
3781 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3782
3783 error = compressed_subtexture_error_check(ctx, 2, target, level,
3784 xoffset, yoffset, 0, /* pos */
3785 width, height, 1, /* size */
3786 format, imageSize);
3787 if (error) {
3788 /* XXX proxy target? */
3789 _mesa_error(ctx, error, "glCompressedTexSubImage2D");
3790 return;
3791 }
3792
3793 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3794 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3795 _mesa_lock_texture(ctx, texObj);
3796 {
3797 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3798 assert(texImage);
3799
3800 if ((GLint) format != texImage->InternalFormat) {
3801 _mesa_error(ctx, GL_INVALID_OPERATION,
3802 "glCompressedTexSubImage2D(format)");
3803 goto out;
3804 }
3805
3806 if (((width == 1 || width == 2) && (GLuint) width != texImage->Width) ||
3807 ((height == 1 || height == 2) && (GLuint) height != texImage->Height)) {
3808 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage2D(size)");
3809 goto out;
3810 }
3811
3812 if (width == 0 || height == 0)
3813 goto out; /* no-op, not an error */
3814
3815 if (ctx->Driver.CompressedTexSubImage2D) {
3816 (*ctx->Driver.CompressedTexSubImage2D)(ctx, target, level,
3817 xoffset, yoffset, width, height,
3818 format, imageSize, data,
3819 texObj, texImage);
3820 }
3821 ctx->NewState |= _NEW_TEXTURE;
3822 }
3823 out:
3824 _mesa_unlock_texture(ctx, texObj);
3825 }
3826
3827
3828 void GLAPIENTRY
3829 _mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
3830 GLint yoffset, GLint zoffset, GLsizei width,
3831 GLsizei height, GLsizei depth, GLenum format,
3832 GLsizei imageSize, const GLvoid *data)
3833 {
3834 struct gl_texture_unit *texUnit;
3835 struct gl_texture_object *texObj;
3836 struct gl_texture_image *texImage;
3837 GLenum error;
3838 GET_CURRENT_CONTEXT(ctx);
3839 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3840
3841 error = compressed_subtexture_error_check(ctx, 3, target, level,
3842 xoffset, yoffset, zoffset,/*pos*/
3843 width, height, depth, /*size*/
3844 format, imageSize);
3845 if (error) {
3846 _mesa_error(ctx, error, "glCompressedTexSubImage3D");
3847 return;
3848 }
3849
3850 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3851 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3852 _mesa_lock_texture(ctx, texObj);
3853 {
3854 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3855 assert(texImage);
3856
3857 if ((GLint) format != texImage->InternalFormat) {
3858 _mesa_error(ctx, GL_INVALID_OPERATION,
3859 "glCompressedTexSubImage3D(format)");
3860 goto out;
3861 }
3862
3863 if (((width == 1 || width == 2) && (GLuint) width != texImage->Width) ||
3864 ((height == 1 || height == 2) && (GLuint) height != texImage->Height) ||
3865 ((depth == 1 || depth == 2) && (GLuint) depth != texImage->Depth)) {
3866 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage3D(size)");
3867 goto out;
3868 }
3869
3870 if (width == 0 || height == 0 || depth == 0)
3871 goto out; /* no-op, not an error */
3872
3873 if (ctx->Driver.CompressedTexSubImage3D) {
3874 (*ctx->Driver.CompressedTexSubImage3D)(ctx, target, level,
3875 xoffset, yoffset, zoffset,
3876 width, height, depth,
3877 format, imageSize, data,
3878 texObj, texImage);
3879 }
3880 ctx->NewState |= _NEW_TEXTURE;
3881 }
3882 out:
3883 _mesa_unlock_texture(ctx, texObj);
3884 }
3885
3886
3887 void GLAPIENTRY
3888 _mesa_GetCompressedTexImageARB(GLenum target, GLint level, GLvoid *img)
3889 {
3890 const struct gl_texture_unit *texUnit;
3891 struct gl_texture_object *texObj;
3892 struct gl_texture_image *texImage;
3893 GLint maxLevels;
3894 GET_CURRENT_CONTEXT(ctx);
3895 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3896
3897 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
3898 texObj = _mesa_select_tex_object(ctx, texUnit, target);
3899 if (!texObj) {
3900 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB");
3901 return;
3902 }
3903
3904 maxLevels = _mesa_max_texture_levels(ctx, target);
3905 ASSERT(maxLevels > 0); /* 0 indicates bad target, caught above */
3906
3907 if (level < 0 || level >= maxLevels) {
3908 _mesa_error(ctx, GL_INVALID_VALUE, "glGetCompressedTexImageARB(level)");
3909 return;
3910 }
3911
3912 if (_mesa_is_proxy_texture(target)) {
3913 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB(target)");
3914 return;
3915 }
3916
3917 _mesa_lock_texture(ctx, texObj);
3918 {
3919 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3920 if (texImage) {
3921 if (texImage->IsCompressed) {
3922 /* this typically calls _mesa_get_compressed_teximage() */
3923 ctx->Driver.GetCompressedTexImage(ctx, target, level, img,
3924 texObj, texImage);
3925 }
3926 else {
3927 _mesa_error(ctx, GL_INVALID_OPERATION,
3928 "glGetCompressedTexImageARB");
3929 }
3930 }
3931 else {
3932 /* probably invalid mipmap level */
3933 _mesa_error(ctx, GL_INVALID_VALUE,
3934 "glGetCompressedTexImageARB(level)");
3935 }
3936 }
3937 _mesa_unlock_texture(ctx, texObj);
3938 }