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