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