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