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